Refaction all files into component based folders

This commit is contained in:
runebaas 2018-05-03 00:56:06 +02:00
parent 55e152f4aa
commit e3adf55742
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
102 changed files with 816 additions and 709 deletions

View file

@ -0,0 +1,72 @@
using System;
using System.Threading.Tasks;
using Discord.Commands;
using Geekbot.net.Lib;
using Geekbot.net.Lib.ErrorHandling;
using Geekbot.net.Lib.UserRepository;
namespace Geekbot.net.Commands.Games
{
[Group("battletag")]
public class BattleTag : ModuleBase
{
private readonly IErrorHandler _errorHandler;
private readonly IUserRepository _userRepository;
public BattleTag(IErrorHandler errorHandler, IUserRepository userRepository)
{
_errorHandler = errorHandler;
_userRepository = userRepository;
}
[Command(RunMode = RunMode.Async)]
[Remarks(CommandCategories.Games)]
[Summary("Get your battletag")]
public async Task BattleTagCmd()
{
try
{
var tag = _userRepository.GetUserSetting(Context.User.Id, "BattleTag");
if (!string.IsNullOrEmpty(tag))
await ReplyAsync($"Your BattleTag is {tag}");
else
await ReplyAsync("You haven't set your BattleTag, set it with `!battletag user#1234`");
}
catch (Exception e)
{
_errorHandler.HandleCommandException(e, Context);
}
}
[Command(RunMode = RunMode.Async)]
[Remarks(CommandCategories.Games)]
[Summary("Save your battletag")]
public async Task BattleTagCmd([Summary("Battletag")] string tag)
{
try
{
if (IsValidTag(tag))
{
_userRepository.SaveUserSetting(Context.User.Id, "BattleTag", tag);
await ReplyAsync("Saved!");
}
else
{
await ReplyAsync("That doesn't seem to be a valid battletag");
}
}
catch (Exception e)
{
_errorHandler.HandleCommandException(e, Context);
}
}
public static bool IsValidTag(string tag)
{
var splited = tag.Split("#");
if (splited.Length != 2) return false;
if (!int.TryParse(splited[1], out var discriminator)) return false;
return splited[1].Length == 4 || splited[1].Length == 5;
}
}
}

View file

@ -0,0 +1,131 @@
using System;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Geekbot.net.Lib;
using Geekbot.net.Lib.ErrorHandling;
using Geekbot.net.Lib.UserRepository;
using OverwatchAPI;
using OverwatchAPI.Config;
namespace Geekbot.net.Commands.Games
{
[Group("ow")]
public class Overwatch : ModuleBase
{
private readonly IErrorHandler _errorHandler;
private readonly IUserRepository _userRepository;
public Overwatch(IErrorHandler errorHandler, IUserRepository userRepository)
{
_errorHandler = errorHandler;
_userRepository = userRepository;
}
[Command("profile", RunMode = RunMode.Async)]
[Summary("Get someones overwatch profile. EU on PC only. Default battletag is your own (if set).")]
[Remarks(CommandCategories.Games)]
public async Task OwProfile()
{
try
{
var tag = _userRepository.GetUserSetting(Context.User.Id, "BattleTag");
if (string.IsNullOrEmpty(tag))
{
await ReplyAsync("You have no battle Tag saved, use `!battletag`");
return;
}
var profile = await CreateProfile(tag);
if (profile == null)
{
await ReplyAsync("That player doesn't seem to exist");
return;
}
await ReplyAsync("", false, profile.Build());
}
catch (Exception e)
{
_errorHandler.HandleCommandException(e, Context);
}
}
[Command("profile", RunMode = RunMode.Async)]
[Summary("Get someones overwatch profile. EU on PC only. Default battletag is your own (if set).")]
[Remarks(CommandCategories.Games)]
public async Task OwProfile([Summary("BattleTag")] string tag)
{
try
{
if (!BattleTag.IsValidTag(tag))
{
await ReplyAsync("That doesn't seem to be a valid battletag...");
return;
}
var profile = await CreateProfile(tag);
if (profile == null)
{
await ReplyAsync("That player doesn't seem to exist");
return;
}
await ReplyAsync("", false, profile.Build());
}
catch (Exception e)
{
_errorHandler.HandleCommandException(e, Context);
}
}
[Command("profile", RunMode = RunMode.Async)]
[Summary("Get someones overwatch profile. EU on PC only.")]
[Remarks(CommandCategories.Games)]
public async Task OwProfile([Summary("@someone")] IUser user)
{
try
{
var tag = _userRepository.GetUserSetting(user.Id, "BattleTag");
if (string.IsNullOrEmpty(tag))
{
await ReplyAsync("This user didn't set a battletag");
return;
}
var profile = await CreateProfile(tag);
if (profile == null)
{
await ReplyAsync("That player doesn't seem to exist");
return;
}
await ReplyAsync("", false, profile.Build());
}
catch (Exception e)
{
_errorHandler.HandleCommandException(e, Context);
}
}
private async Task<EmbedBuilder> CreateProfile(string battletag)
{
var owConfig = new OverwatchConfig.Builder().WithPlatforms(Platform.Pc);
using (var owClient = new OverwatchClient(owConfig))
{
var player = await owClient.GetPlayerAsync(battletag);
if (player.Username == null) return null;
var eb = new EmbedBuilder();
eb.WithAuthor(new EmbedAuthorBuilder()
.WithIconUrl(player.ProfilePortraitUrl)
.WithName(player.Username));
eb.Url = player.ProfileUrl;
eb.AddInlineField("Level", player.PlayerLevel);
eb.AddInlineField("Current Rank",
player.CompetitiveRank > 0 ? player.CompetitiveRank.ToString() : "Unranked");
return eb;
}
}
}
}

View file

@ -0,0 +1,77 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Geekbot.net.Lib;
using Geekbot.net.Lib.ErrorHandling;
using PokeAPI;
namespace Geekbot.net.Commands.Games
{
public class Pokedex : ModuleBase
{
private readonly IErrorHandler _errorHandler;
public Pokedex(IErrorHandler errorHandler)
{
_errorHandler = errorHandler;
}
[Command("pokedex", RunMode = RunMode.Async)]
[Remarks(CommandCategories.Helpers)]
[Summary("A Pokedex Tool")]
public async Task GetPokemon([Summary("pokemonName")] string pokemonName)
{
try
{
DataFetcher.ShouldCacheData = true;
Pokemon pokemon;
try
{
pokemon = await DataFetcher.GetNamedApiObject<Pokemon>(pokemonName.ToLower());
}
catch
{
await ReplyAsync("I couldn't find that pokemon :confused:");
return;
}
var embed = await PokemonEmbedBuilder(pokemon);
await ReplyAsync("", false, embed.Build());
}
catch (Exception e)
{
_errorHandler.HandleCommandException(e, Context);
}
}
private async Task<EmbedBuilder> PokemonEmbedBuilder(Pokemon pokemon)
{
var eb = new EmbedBuilder();
var species = await DataFetcher.GetApiObject<PokemonSpecies>(pokemon.ID);
eb.Title = $"#{pokemon.ID} {ToUpper(pokemon.Name)}";
eb.Description = species.FlavorTexts[1].FlavorText;
eb.ThumbnailUrl = pokemon.Sprites.FrontMale ?? pokemon.Sprites.FrontFemale;
eb.AddInlineField(GetSingularOrPlural(pokemon.Types.Length, "Type"),
string.Join(", ", pokemon.Types.Select(t => ToUpper(t.Type.Name))));
eb.AddInlineField(GetSingularOrPlural(pokemon.Abilities.Length, "Ability"),
string.Join(", ", pokemon.Abilities.Select(t => ToUpper(t.Ability.Name))));
eb.AddInlineField("Height", pokemon.Height);
eb.AddInlineField("Weight", pokemon.Mass);
return eb;
}
private string GetSingularOrPlural(int lenght, string word)
{
if (lenght == 1) return word;
return word.EndsWith("y") ? $"{word.Remove(word.Length - 1)}ies" : $"{word}s";
}
private string ToUpper(string s)
{
if (string.IsNullOrEmpty(s)) return string.Empty;
return char.ToUpper(s[0]) + s.Substring(1);
}
}
}

View file

@ -0,0 +1,64 @@
using System;
using System.Threading.Tasks;
using Discord.Commands;
using Geekbot.net.Lib;
using Geekbot.net.Lib.ErrorHandling;
using Geekbot.net.Lib.Localization;
using StackExchange.Redis;
namespace Geekbot.net.Commands.Games
{
public class Roll : ModuleBase
{
private readonly IErrorHandler _errorHandler;
private readonly IDatabase _redis;
private readonly ITranslationHandler _translation;
public Roll(IDatabase redis, IErrorHandler errorHandler, ITranslationHandler translation)
{
_redis = redis;
_translation = translation;
_errorHandler = errorHandler;
}
[Command("roll", RunMode = RunMode.Async)]
[Remarks(CommandCategories.Fun)]
[Summary("Guess which number the bot will roll (1-100")]
public async Task RollCommand([Remainder] [Summary("guess")] string stuff = "noGuess")
{
try
{
var number = new Random().Next(1, 100);
var guess = 1000;
int.TryParse(stuff, out guess);
var transDict = _translation.GetDict(Context);
if (guess <= 100 && guess > 0)
{
var prevRoll = _redis.HashGet($"{Context.Guild.Id}:RollsPrevious", Context.Message.Author.Id);
if (!prevRoll.IsNullOrEmpty && prevRoll.ToString() == guess.ToString())
{
await ReplyAsync(string.Format(transDict["NoPrevGuess"], Context.Message.Author.Mention));
return;
}
_redis.HashSet($"{Context.Guild.Id}:RollsPrevious",
new[] {new HashEntry(Context.Message.Author.Id, guess)});
await ReplyAsync(string.Format(transDict["Rolled"], Context.Message.Author.Mention, number, guess));
if (guess == number)
{
await ReplyAsync(string.Format(transDict["Gratz"], Context.Message.Author));
_redis.HashIncrement($"{Context.Guild.Id}:Rolls", Context.User.Id.ToString());
}
}
else
{
await ReplyAsync(string.Format(transDict["RolledNoGuess"], Context.Message.Author.Mention, number));
}
}
catch (Exception e)
{
_errorHandler.HandleCommandException(e, Context);
}
}
}
}