From 70d92706e25e70c6c2f5df96a3ca6aef045e9285 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 21 Jan 2018 23:42:27 +0100 Subject: [PATCH 001/443] Add (broken) Context handler --- Geekbot.net/Commands/ContextHandler.cs | 97 ++++++++++++++++++++++++++ Geekbot.net/Commands/Say.cs | 37 +++++++++- Geekbot.net/Handlers.cs | 12 +++- Geekbot.net/Lib/ErrorHandler.cs | 4 +- Geekbot.net/Program.cs | 5 +- 5 files changed, 149 insertions(+), 6 deletions(-) create mode 100644 Geekbot.net/Commands/ContextHandler.cs diff --git a/Geekbot.net/Commands/ContextHandler.cs b/Geekbot.net/Commands/ContextHandler.cs new file mode 100644 index 0000000..4e978b2 --- /dev/null +++ b/Geekbot.net/Commands/ContextHandler.cs @@ -0,0 +1,97 @@ +using System; +using System.Collections.Generic; +using System.Dynamic; +using System.Threading.Tasks; +using Discord; +using Discord.Commands; + +namespace Geekbot.net.Commands +{ + public class ContextHandler : IContextHandler + { + private Dictionary _store; + private readonly IDiscordClient _client; + private readonly CommandService _commandService; + private readonly IServiceProvider _servicesProvider; + + public ContextHandler(IDiscordClient client, CommandService commandService, IServiceProvider servicesProvider) + { + _store = new Dictionary(); + _commandService = commandService; + _servicesProvider = servicesProvider; + _client = client; + } + + public ContextReference HasContext(IUserMessage message) + { + if (_store.ContainsKey(message.Author.Id)) return ContextReference.User; + + if (_store.ContainsKey(message.Channel.Id)) return ContextReference.Channel; + + return ContextReference.None; + } + + public void SaveContext(ContextReference type, ICommandContext context, string commandName) + { + var contextStore = new ContextStore() + { + CommandName = commandName + }; + var id = GetId(type, context.Message); + _store.Add(id, contextStore); + } + + public async void ExecuteOnContext(ContextReference type, IUserMessage message) + { + var id = GetId(type, message); + var obj = _store[id]; + var context = new CommandContext(_client, message); + var rest = await _commandService.ExecuteAsync(context, $"{obj.CommandSearch} {context.Message?.Content}", _servicesProvider); + if (!rest.IsSuccess) + { + await context.Channel.SendMessageAsync(rest.ErrorReason); + } + } + + public void ClearContext(ulong id) + { + _store.Remove(id); + } + + private ulong GetId(ContextReference type, IUserMessage message) + { + if (type == ContextReference.Channel) + { + return message.Author.Id; + } + + if (type == ContextReference.User) + { + return message.Author.Id; + } + + throw new Exception("No Context Object Found"); + } + + private class ContextStore + { + public string CommandName { get; set; } + public string CommandSearch => $"{CommandName} ctx"; + } + } + + public enum ContextReference + { + User, + Channel, + None + } + + public interface IContextHandler + { + ContextReference HasContext(IUserMessage message); + void SaveContext(ContextReference type, ICommandContext context, string commandName); + void ExecuteOnContext(ContextReference type, IUserMessage message); + void ClearContext(ulong id); + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/Say.cs b/Geekbot.net/Commands/Say.cs index 4c878c4..d2bc22e 100644 --- a/Geekbot.net/Commands/Say.cs +++ b/Geekbot.net/Commands/Say.cs @@ -6,17 +6,48 @@ using Geekbot.net.Lib; namespace Geekbot.net.Commands { + [Group("say")] public class Say : ModuleBase { private readonly IErrorHandler _errorHandler; - - public Say(IErrorHandler errorHandler) + private readonly IContextHandler _contextHandler; + + public Say(IErrorHandler errorHandler, IContextHandler contextHandler) { _errorHandler = errorHandler; + _contextHandler = contextHandler; } [RequireUserPermission(GuildPermission.Administrator)] - [Command("say", RunMode = RunMode.Async)] + [Command("record", RunMode = RunMode.Async)] + [Remarks(CommandCategories.Admin)] + [Summary("Say Something.")] + public async Task recordEcho() + { + try + { + _contextHandler.SaveContext(ContextReference.User, Context, "say"); + await ReplyAsync("Recording..."); + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + } + } + + [RequireUserPermission(GuildPermission.Administrator)] + [Command("ctx", RunMode = RunMode.Async)] + [Remarks(CommandCategories.Admin)] + [Summary("Say Something.")] + public async Task recordEcho([Remainder] [Summary("What?")] string echo) + { + Console.WriteLine("actually got here..."); + _contextHandler.ClearContext(Context.User.Id); + await Context.Channel.SendMessageAsync(echo); + } + + [RequireUserPermission(GuildPermission.Administrator)] + [Command("ss", RunMode = RunMode.Async)] [Remarks(CommandCategories.Admin)] [Summary("Say Something.")] public async Task Echo([Remainder] [Summary("What?")] string echo) diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index 13ce041..fc0f871 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Discord.WebSocket; +using Geekbot.net.Commands; using Geekbot.net.Lib; using Serilog; using StackExchange.Redis; @@ -19,8 +20,9 @@ namespace Geekbot.net private readonly IServiceProvider _servicesProvider; private readonly CommandService _commands; private readonly IUserRepository _userRepository; + private readonly IContextHandler _contextHandler; - public Handlers(IDiscordClient client, IGeekbotLogger logger, IDatabase redis, IServiceProvider servicesProvider, CommandService commands, IUserRepository userRepository) + public Handlers(IDiscordClient client, IGeekbotLogger logger, IDatabase redis, IServiceProvider servicesProvider, CommandService commands, IUserRepository userRepository, IContextHandler contextHandler) { _client = client; _logger = logger; @@ -28,6 +30,7 @@ namespace Geekbot.net _servicesProvider = servicesProvider; _commands = commands; _userRepository = userRepository; + _contextHandler = contextHandler; } // @@ -52,6 +55,13 @@ namespace Geekbot.net message.Channel.SendMessageAsync("hui!!!"); return Task.CompletedTask; } + + var contextType = _contextHandler.HasContext(message); + if (contextType != ContextReference.None) + { + _contextHandler.ExecuteOnContext(contextType, message); + return Task.CompletedTask; + } if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos))) return Task.CompletedTask; var context = new CommandContext(_client, message); diff --git a/Geekbot.net/Lib/ErrorHandler.cs b/Geekbot.net/Lib/ErrorHandler.cs index 6cc6644..332a824 100644 --- a/Geekbot.net/Lib/ErrorHandler.cs +++ b/Geekbot.net/Lib/ErrorHandler.cs @@ -47,7 +47,9 @@ namespace Geekbot.net.Lib { Context.Channel.SendMessageAsync(errorString); } - + + if (e.Message.Contains("50013")) return; + if (e.Message.Contains("50007")) return; if (_raven == null) return; var sentryEvent = new SentryEvent(e) diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index da74508..c7d2216 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -10,6 +10,7 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Discord.WebSocket; +using Geekbot.net.Commands; using Geekbot.net.Lib; using Geekbot.net.Lib.Media; using Microsoft.Extensions.DependencyInjection; @@ -136,13 +137,15 @@ namespace Geekbot.net var translationHandler = new TranslationHandler(client.Guilds, redis, logger); var errorHandler = new ErrorHandler(logger, translationHandler); await commands.AddModulesAsync(Assembly.GetEntryAssembly()); + var contextHandler = new ContextHandler(client, commands, servicesProvider); services.AddSingleton(commands); + services.AddSingleton(contextHandler); services.AddSingleton(errorHandler); services.AddSingleton(translationHandler); services.AddSingleton(client); servicesProvider = services.BuildServiceProvider(); - var handlers = new Handlers(client, logger, redis, servicesProvider, commands, userRepository); + var handlers = new Handlers(client, logger, redis, servicesProvider, commands, userRepository, contextHandler); client.MessageReceived += handlers.RunCommand; client.MessageReceived += handlers.UpdateStats; From ffaedfa96214d07c700b8787f3ea59e5b8a572a9 Mon Sep 17 00:00:00 2001 From: runebaas Date: Mon, 29 Jan 2018 23:27:28 +0100 Subject: [PATCH 002/443] Add space to emojis in emojihandler --- Geekbot.net/Lib/EmojiConverter.cs | 84 +++++++++++++++---------------- Tests/Lib/EmojiConverter.test.cs | 4 +- 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/Geekbot.net/Lib/EmojiConverter.cs b/Geekbot.net/Lib/EmojiConverter.cs index 60e75f4..7091f0c 100644 --- a/Geekbot.net/Lib/EmojiConverter.cs +++ b/Geekbot.net/Lib/EmojiConverter.cs @@ -37,48 +37,48 @@ namespace Geekbot.net.Lib { var emojiMap = new Hashtable { - ['A'] = ":regional_indicator_a:", - ['B'] = ":b:", - ['C'] = ":regional_indicator_c:", - ['D'] = ":regional_indicator_d:", - ['E'] = ":regional_indicator_e:", - ['F'] = ":regional_indicator_f:", - ['G'] = ":regional_indicator_g:", - ['H'] = ":regional_indicator_h:", - ['I'] = ":regional_indicator_i:", - ['J'] = ":regional_indicator_j:", - ['K'] = ":regional_indicator_k:", - ['L'] = ":regional_indicator_l:", - ['M'] = ":regional_indicator_m:", - ['N'] = ":regional_indicator_n:", - ['O'] = ":regional_indicator_o:", - ['P'] = ":regional_indicator_p:", - ['Q'] = ":regional_indicator_q:", - ['R'] = ":regional_indicator_r:", - ['S'] = ":regional_indicator_s:", - ['T'] = ":regional_indicator_t:", - ['U'] = ":regional_indicator_u:", - ['V'] = ":regional_indicator_v:", - ['W'] = ":regional_indicator_w:", - ['X'] = ":regional_indicator_x:", - ['Y'] = ":regional_indicator_y:", - ['Z'] = ":regional_indicator_z:", - ['!'] = ":exclamation:", - ['?'] = ":question:", - ['#'] = ":hash:", - ['*'] = ":star2:", - ['+'] = ":heavy_plus_sign:", - ['0'] = ":zero:", - ['1'] = ":one:", - ['2'] = ":two:", - ['3'] = ":three:", - ['4'] = ":four:", - ['5'] = ":five:", - ['6'] = ":six:", - ['7'] = ":seven:", - ['8'] = ":eight:", - ['9'] = ":nine:", - [' '] = " " + ['A'] = ":regional_indicator_a: ", + ['B'] = ":b: ", + ['C'] = ":regional_indicator_c: ", + ['D'] = ":regional_indicator_d: ", + ['E'] = ":regional_indicator_e: ", + ['F'] = ":regional_indicator_f: ", + ['G'] = ":regional_indicator_g: ", + ['H'] = ":regional_indicator_h: ", + ['I'] = ":regional_indicator_i: ", + ['J'] = ":regional_indicator_j: ", + ['K'] = ":regional_indicator_k: ", + ['L'] = ":regional_indicator_l: ", + ['M'] = ":regional_indicator_m: ", + ['N'] = ":regional_indicator_n: ", + ['O'] = ":regional_indicator_o: ", + ['P'] = ":regional_indicator_p: ", + ['Q'] = ":regional_indicator_q: ", + ['R'] = ":regional_indicator_r: ", + ['S'] = ":regional_indicator_s: ", + ['T'] = ":regional_indicator_t: ", + ['U'] = ":regional_indicator_u: ", + ['V'] = ":regional_indicator_v: ", + ['W'] = ":regional_indicator_w: ", + ['X'] = ":regional_indicator_x: ", + ['Y'] = ":regional_indicator_y: ", + ['Z'] = ":regional_indicator_z: ", + ['!'] = ":exclamation: ", + ['?'] = ":question: ", + ['#'] = ":hash: ", + ['*'] = ":star2: ", + ['+'] = ":heavy_plus_sign: ", + ['0'] = ":zero: ", + ['1'] = ":one: ", + ['2'] = ":two: ", + ['3'] = ":three: ", + ['4'] = ":four: ", + ['5'] = ":five: ", + ['6'] = ":six: ", + ['7'] = ":seven: ", + ['8'] = ":eight: ", + ['9'] = ":nine: ", + [' '] = " " }; var letters = text.ToUpper().ToCharArray(); var returnString = new StringBuilder(); diff --git a/Tests/Lib/EmojiConverter.test.cs b/Tests/Lib/EmojiConverter.test.cs index ca4ecbc..bf41596 100644 --- a/Tests/Lib/EmojiConverter.test.cs +++ b/Tests/Lib/EmojiConverter.test.cs @@ -53,12 +53,12 @@ namespace Tests.Lib yield return new object[] { "test", - ":regional_indicator_t::regional_indicator_e::regional_indicator_s::regional_indicator_t:" + ":regional_indicator_t: :regional_indicator_e: :regional_indicator_s: :regional_indicator_t: " }; yield return new object[] { "Best3+?", - ":b::regional_indicator_e::regional_indicator_s::regional_indicator_t::three::heavy_plus_sign::question:" + ":b: :regional_indicator_e: :regional_indicator_s: :regional_indicator_t: :three: :heavy_plus_sign: :question: " }; } } From 91d178049b19fbd7e951eeec245abf3ff698de64 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Sat, 3 Feb 2018 15:14:11 +0100 Subject: [PATCH 003/443] Error when removing non-existing role from whitelist --- Geekbot.net/Commands/Role.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Geekbot.net/Commands/Role.cs b/Geekbot.net/Commands/Role.cs index 77e917f..80ef4af 100644 --- a/Geekbot.net/Commands/Role.cs +++ b/Geekbot.net/Commands/Role.cs @@ -134,8 +134,16 @@ namespace Geekbot.net.Commands { try { - _redis.HashDelete($"{Context.Guild.Id}:RoleWhitelist", roleName); - await ReplyAsync($"Removed {roleName} from the whitelist"); + + var success = _redis.HashDelete($"{Context.Guild.Id}:RoleWhitelist", roleName.ToLower()); + if (success) + { + await ReplyAsync($"Removed {roleName} from the whitelist"); + return; + } + + await ReplyAsync("There is not whitelisted role with that name..."); + } catch (Exception e) { From ff1619a62ff12727c1552ba9fe267b10dfd694a6 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 4 Feb 2018 14:47:10 +0100 Subject: [PATCH 004/443] Ignore error 50007 and 50013 --- Geekbot.net/Lib/ErrorHandler.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Geekbot.net/Lib/ErrorHandler.cs b/Geekbot.net/Lib/ErrorHandler.cs index 6cc6644..e6ea44e 100644 --- a/Geekbot.net/Lib/ErrorHandler.cs +++ b/Geekbot.net/Lib/ErrorHandler.cs @@ -42,6 +42,8 @@ namespace Geekbot.net.Lib { var errorString = errorMessage == "def" ? _translation.GetString(Context.Guild.Id, "errorHandler", "SomethingWentWrong") : errorMessage; var errorObj = SimpleConextConverter.ConvertContext(Context); + if (errorObj.Message.Content.Contains("50007")) return; + if (errorObj.Message.Content.Contains("50013")) return; _logger.Error("Geekbot", "An error ocured", e, errorObj); if (!string.IsNullOrEmpty(errorMessage)) { @@ -49,7 +51,7 @@ namespace Geekbot.net.Lib } if (_raven == null) return; - + var sentryEvent = new SentryEvent(e) { Tags = From 8d23420031aa6e27c7ea96131ee2bf5ef7a57d13 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 4 Feb 2018 14:52:30 +0100 Subject: [PATCH 005/443] Remove test warnings --- Tests/Lib/EmojiConverter.test.cs | 4 ++-- Tests/Lib/LevelCalc.test.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Tests/Lib/EmojiConverter.test.cs b/Tests/Lib/EmojiConverter.test.cs index bf41596..b9e7b4f 100644 --- a/Tests/Lib/EmojiConverter.test.cs +++ b/Tests/Lib/EmojiConverter.test.cs @@ -39,7 +39,7 @@ namespace Tests.Lib [Theory, MemberData(nameof(NumberToEmojiTestData))] - public async Task NumberToEmoji(int number, string expectedResult) + public void NumberToEmoji(int number, string expectedResult) { var emojiConverter = new EmojiConverter(); var result = emojiConverter.numberToEmoji(number); @@ -65,7 +65,7 @@ namespace Tests.Lib [Theory, MemberData(nameof(textToEmojiTestData))] - public async Task TextToEmoji(string text, string expectedResult) + public void TextToEmoji(string text, string expectedResult) { var emojiConverter = new EmojiConverter(); var result = emojiConverter.textToEmoji(text); diff --git a/Tests/Lib/LevelCalc.test.cs b/Tests/Lib/LevelCalc.test.cs index 74e6105..7c00392 100644 --- a/Tests/Lib/LevelCalc.test.cs +++ b/Tests/Lib/LevelCalc.test.cs @@ -39,7 +39,7 @@ namespace Tests.Lib [Theory, MemberData(nameof(LevelCalcTestData))] - public async Task GetLevel(int messages, int expectedResult) + public void GetLevel(int messages, int expectedResult) { var levelCalc = new LevelCalc(); var result = levelCalc.GetLevel(messages); From 46696549d77a2f17c9350f0d45716c6f0cb70471 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 4 Feb 2018 17:36:55 +0100 Subject: [PATCH 006/443] Check correct string to ignore errors *facepalm* --- Geekbot.net/Lib/ErrorHandler.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Geekbot.net/Lib/ErrorHandler.cs b/Geekbot.net/Lib/ErrorHandler.cs index e6ea44e..bd82668 100644 --- a/Geekbot.net/Lib/ErrorHandler.cs +++ b/Geekbot.net/Lib/ErrorHandler.cs @@ -42,8 +42,8 @@ namespace Geekbot.net.Lib { var errorString = errorMessage == "def" ? _translation.GetString(Context.Guild.Id, "errorHandler", "SomethingWentWrong") : errorMessage; var errorObj = SimpleConextConverter.ConvertContext(Context); - if (errorObj.Message.Content.Contains("50007")) return; - if (errorObj.Message.Content.Contains("50013")) return; + if (e.Message.Contains("50007")) return; + if (e.Message.Contains("50013")) return; _logger.Error("Geekbot", "An error ocured", e, errorObj); if (!string.IsNullOrEmpty(errorMessage)) { From 6fd36e0bb254a2bb81e242db0df17809ba9a5315 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Tue, 13 Feb 2018 22:29:37 +0100 Subject: [PATCH 007/443] Show mana costs as emojis in mtg --- Geekbot.net/Commands/MagicTheGathering.cs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/Geekbot.net/Commands/MagicTheGathering.cs b/Geekbot.net/Commands/MagicTheGathering.cs index e2702b1..bd0fd0a 100644 --- a/Geekbot.net/Commands/MagicTheGathering.cs +++ b/Geekbot.net/Commands/MagicTheGathering.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Text.RegularExpressions; using System.Threading.Tasks; using Discord; using Discord.Commands; @@ -12,10 +13,12 @@ namespace Geekbot.net.Commands public class Magicthegathering : ModuleBase { private readonly IErrorHandler _errorHandler; + private readonly IEmojiConverter _emojiConverter; - public Magicthegathering(IErrorHandler errorHandler) + public Magicthegathering(IErrorHandler errorHandler, IEmojiConverter emojiConverter) { _errorHandler = errorHandler; + _emojiConverter = emojiConverter; } [Command("mtg", RunMode = RunMode.Async)] @@ -51,7 +54,7 @@ namespace Geekbot.net.Commands if (!string.IsNullOrEmpty(card.Loyalty)) eb.AddInlineField("Loyality", card.Loyalty); if (!string.IsNullOrEmpty(card.Toughness)) eb.AddInlineField("Thoughness", card.Toughness); - if (!string.IsNullOrEmpty(card.ManaCost)) eb.AddInlineField("Cost", card.ManaCost); + if (!string.IsNullOrEmpty(card.ManaCost)) eb.AddInlineField("Cost", ManaConverter(card.ManaCost)); if (!string.IsNullOrEmpty(card.Rarity)) eb.AddInlineField("Rarity", card.Rarity); if (card.Legalities != null) @@ -84,5 +87,21 @@ namespace Geekbot.net.Commands return new Color(255, 252, 214); } } + + private string ManaConverter(string mana) + { + var rgx = new Regex("{(\\d)}"); + var groups = rgx.Match(mana).Groups; + if (groups.Count == 2) + { + mana = mana.Replace(groups[0].Value, _emojiConverter.numberToEmoji(int.Parse(groups[1].Value))); + } + return mana + .Replace("{W}", ":sunny:") + .Replace("{U}", ":droplet:") + .Replace("{B}", ":skull:") + .Replace("{R}", ":fire:") + .Replace("{G}", ":deciduous_tree:"); + } } } \ No newline at end of file From 03ed80d3d452f476ebd920e526a49909d7671fea Mon Sep 17 00:00:00 2001 From: Runebaas Date: Tue, 13 Feb 2018 23:42:31 +0100 Subject: [PATCH 008/443] Log command usage --- Geekbot.net/Handlers.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index 13ce041..1aa2a05 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -56,6 +56,9 @@ namespace Geekbot.net message.HasMentionPrefix(_client.CurrentUser, ref argPos))) return Task.CompletedTask; var context = new CommandContext(_client, message); var commandExec = _commands.ExecuteAsync(context, argPos, _servicesProvider); + _logger.Information("Command", + context.Message.Content.Split(" ")[0].Replace("!", ""), + SimpleConextConverter.ConvertContext(context)); return Task.CompletedTask; } catch (Exception e) From cabf9423623ff554e10eb3b886df7b4962320cc1 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Wed, 14 Feb 2018 11:35:12 +0100 Subject: [PATCH 009/443] unpin --- Geekbot.net/Handlers.cs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index 1aa2a05..aa8a154 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -42,11 +42,6 @@ namespace Geekbot.net if (message.Author.IsBot) return Task.CompletedTask; var argPos = 0; var lowCaseMsg = message.ToString().ToLower(); - if (lowCaseMsg.Equals("ping") || lowCaseMsg.StartsWith("ping ")) - { - message.Channel.SendMessageAsync("pong"); - return Task.CompletedTask; - } if (lowCaseMsg.StartsWith("hui")) { message.Channel.SendMessageAsync("hui!!!"); @@ -186,4 +181,4 @@ namespace Geekbot.net } } } -} \ No newline at end of file +} From acc8947782fb848afb7b7154c534ca962490c4c0 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Wed, 14 Feb 2018 23:01:28 +0100 Subject: [PATCH 010/443] Make random more random than the google bot --- Geekbot.net/Commands/CheckEm.cs | 6 ++---- Geekbot.net/Commands/Choose.cs | 6 ++---- Geekbot.net/Commands/Dice.cs | 9 +-------- Geekbot.net/Commands/EightBall.cs | 6 ++---- Geekbot.net/Commands/Quote.cs | 2 +- Geekbot.net/Commands/Roll.cs | 6 ++---- Geekbot.net/Commands/Ship.cs | 6 ++---- Geekbot.net/Commands/Slap.cs | 6 ++---- Geekbot.net/Lib/Media/FortunesProvider.cs | 6 ++---- Geekbot.net/Lib/Media/MediaProvider.cs | 4 ++-- Geekbot.net/Program.cs | 6 ++---- 11 files changed, 20 insertions(+), 43 deletions(-) diff --git a/Geekbot.net/Commands/CheckEm.cs b/Geekbot.net/Commands/CheckEm.cs index 11d0cef..61de091 100644 --- a/Geekbot.net/Commands/CheckEm.cs +++ b/Geekbot.net/Commands/CheckEm.cs @@ -12,11 +12,9 @@ namespace Geekbot.net.Commands { private readonly IMediaProvider _checkEmImages; private readonly IErrorHandler _errorHandler; - private readonly Random _rnd; - public CheckEm(Random RandomClient, IMediaProvider mediaProvider, IErrorHandler errorHandler) + public CheckEm(IMediaProvider mediaProvider, IErrorHandler errorHandler) { - _rnd = RandomClient; _checkEmImages = mediaProvider; _errorHandler = errorHandler; } @@ -28,7 +26,7 @@ namespace Geekbot.net.Commands { try { - var number = _rnd.Next(10000000, 99999999); + var number = new Random().Next(10000000, 99999999); var dubtriqua = ""; var ns = GetIntArray(number); diff --git a/Geekbot.net/Commands/Choose.cs b/Geekbot.net/Commands/Choose.cs index 214a1a8..a726741 100644 --- a/Geekbot.net/Commands/Choose.cs +++ b/Geekbot.net/Commands/Choose.cs @@ -8,12 +8,10 @@ namespace Geekbot.net.Commands public class Choose : ModuleBase { private readonly IErrorHandler _errorHandler; - private readonly Random _rnd; private readonly ITranslationHandler _translation; - public Choose(Random RandomClient, IErrorHandler errorHandler, ITranslationHandler translation) + public Choose(IErrorHandler errorHandler, ITranslationHandler translation) { - _rnd = RandomClient; _errorHandler = errorHandler; _translation = translation; } @@ -28,7 +26,7 @@ namespace Geekbot.net.Commands { var transDict = _translation.GetDict(Context); var choicesArray = choices.Split(';'); - var choice = _rnd.Next(choicesArray.Length); + var choice = new Random().Next(choicesArray.Length); await ReplyAsync(string.Format(transDict["Choice"], choicesArray[choice])); } catch (Exception e) diff --git a/Geekbot.net/Commands/Dice.cs b/Geekbot.net/Commands/Dice.cs index 9195be8..774f8b9 100644 --- a/Geekbot.net/Commands/Dice.cs +++ b/Geekbot.net/Commands/Dice.cs @@ -10,13 +10,6 @@ namespace Geekbot.net.Commands { public class Dice : ModuleBase { - private readonly Random _rnd; - - public Dice(Random RandomClient) - { - _rnd = RandomClient; - } - [Command("dice", RunMode = RunMode.Async)] [Remarks(CommandCategories.Randomness)] [Summary("Roll a dice.")] @@ -75,7 +68,7 @@ namespace Geekbot.net.Commands var results = new List(); for (var i = 0; i < dice.times; i++) { - var roll = _rnd.Next(1, dice.sides); + var roll = new Random().Next(1, dice.sides); total += roll; results.Add(roll); if (roll == dice.sides) extraText = "**Critical Hit!**"; diff --git a/Geekbot.net/Commands/EightBall.cs b/Geekbot.net/Commands/EightBall.cs index 5cadee5..94b01a1 100644 --- a/Geekbot.net/Commands/EightBall.cs +++ b/Geekbot.net/Commands/EightBall.cs @@ -9,11 +9,9 @@ namespace Geekbot.net.Commands public class EightBall : ModuleBase { private readonly IErrorHandler _errorHandler; - private readonly Random _rnd; - public EightBall(Random RandomClient, IErrorHandler errorHandler) + public EightBall(IErrorHandler errorHandler) { - _rnd = RandomClient; _errorHandler = errorHandler; } @@ -48,7 +46,7 @@ namespace Geekbot.net.Commands "Very doubtful" }; - var answer = _rnd.Next(replies.Count); + var answer = new Random().Next(replies.Count); await ReplyAsync(replies[answer]); } catch (Exception e) diff --git a/Geekbot.net/Commands/Quote.cs b/Geekbot.net/Commands/Quote.cs index c3bb235..fc4e2ce 100644 --- a/Geekbot.net/Commands/Quote.cs +++ b/Geekbot.net/Commands/Quote.cs @@ -15,7 +15,7 @@ namespace Geekbot.net.Commands private readonly IErrorHandler _errorHandler; private readonly IDatabase _redis; - public Quote(IDatabase redis, IErrorHandler errorHandler, Random random) + public Quote(IDatabase redis, IErrorHandler errorHandler) { _redis = redis; _errorHandler = errorHandler; diff --git a/Geekbot.net/Commands/Roll.cs b/Geekbot.net/Commands/Roll.cs index 33cdf67..8d4854c 100644 --- a/Geekbot.net/Commands/Roll.cs +++ b/Geekbot.net/Commands/Roll.cs @@ -10,13 +10,11 @@ namespace Geekbot.net.Commands { private readonly IErrorHandler _errorHandler; private readonly IDatabase _redis; - private readonly Random _rnd; private readonly ITranslationHandler _translation; - public Roll(IDatabase redis, Random RandomClient, IErrorHandler errorHandler, ITranslationHandler translation) + public Roll(IDatabase redis, IErrorHandler errorHandler, ITranslationHandler translation) { _redis = redis; - _rnd = RandomClient; _translation = translation; _errorHandler = errorHandler; } @@ -28,7 +26,7 @@ namespace Geekbot.net.Commands { try { - var number = _rnd.Next(1, 100); + var number = new Random().Next(1, 100); var guess = 1000; int.TryParse(stuff, out guess); var transDict = _translation.GetDict(Context); diff --git a/Geekbot.net/Commands/Ship.cs b/Geekbot.net/Commands/Ship.cs index 7b85eb0..4727bc8 100644 --- a/Geekbot.net/Commands/Ship.cs +++ b/Geekbot.net/Commands/Ship.cs @@ -11,12 +11,10 @@ namespace Geekbot.net.Commands { private readonly IErrorHandler _errorHandler; private readonly IDatabase _redis; - private readonly Random _rnd; - public Ship(IDatabase redis, Random randomClient, IErrorHandler errorHandler) + public Ship(IDatabase redis, IErrorHandler errorHandler) { _redis = redis; - _rnd = randomClient; _errorHandler = errorHandler; } @@ -37,7 +35,7 @@ namespace Geekbot.net.Commands var shippingRate = 0; if (dbval.IsNullOrEmpty) { - shippingRate = _rnd.Next(1, 100); + shippingRate = new Random().Next(1, 100); _redis.HashSet($"{Context.Guild.Id}:Ships", dbstring, shippingRate); } else diff --git a/Geekbot.net/Commands/Slap.cs b/Geekbot.net/Commands/Slap.cs index 134752c..24ba835 100644 --- a/Geekbot.net/Commands/Slap.cs +++ b/Geekbot.net/Commands/Slap.cs @@ -11,13 +11,11 @@ namespace Geekbot.net.Commands public class Slap : ModuleBase { private readonly IErrorHandler _errorHandler; - private readonly Random _random; private readonly IDatabase _redis; - public Slap(IErrorHandler errorHandler, Random random, IDatabase redis) + public Slap(IErrorHandler errorHandler, IDatabase redis) { _errorHandler = errorHandler; - _random = random; _redis = redis; } @@ -61,7 +59,7 @@ namespace Geekbot.net.Commands _redis.HashIncrement($"{Context.Guild.Id}:SlapsRecieved", user.Id.ToString()); _redis.HashIncrement($"{Context.Guild.Id}:SlapsGiven", Context.User.Id.ToString()); - await ReplyAsync($"{Context.User.Username} slapped {user.Username} with a {things[_random.Next(things.Count - 1)]}"); + await ReplyAsync($"{Context.User.Username} slapped {user.Username} with a {things[new Random().Next(things.Count - 1)]}"); } catch (Exception e) { diff --git a/Geekbot.net/Lib/Media/FortunesProvider.cs b/Geekbot.net/Lib/Media/FortunesProvider.cs index f536074..8a9b6f0 100644 --- a/Geekbot.net/Lib/Media/FortunesProvider.cs +++ b/Geekbot.net/Lib/Media/FortunesProvider.cs @@ -7,10 +7,9 @@ namespace Geekbot.net.Lib.Media internal class FortunesProvider : IFortunesProvider { private readonly string[] fortuneArray; - private readonly Random rnd; private readonly int totalFortunes; - public FortunesProvider(Random rnd, IGeekbotLogger logger) + public FortunesProvider(IGeekbotLogger logger) { var path = Path.GetFullPath("./Storage/fortunes"); if (File.Exists(path)) @@ -18,7 +17,6 @@ namespace Geekbot.net.Lib.Media var rawFortunes = File.ReadAllText(path); fortuneArray = rawFortunes.Split("%"); totalFortunes = fortuneArray.Length; - this.rnd = rnd; logger.Debug("Geekbot", "Loaded {totalFortunes} Fortunes"); } else @@ -29,7 +27,7 @@ namespace Geekbot.net.Lib.Media public string GetRandomFortune() { - return fortuneArray[rnd.Next(0, totalFortunes)]; + return fortuneArray[new Random().Next(0, totalFortunes)]; } } diff --git a/Geekbot.net/Lib/Media/MediaProvider.cs b/Geekbot.net/Lib/Media/MediaProvider.cs index ce5a0cd..0a3ff6c 100644 --- a/Geekbot.net/Lib/Media/MediaProvider.cs +++ b/Geekbot.net/Lib/Media/MediaProvider.cs @@ -17,9 +17,9 @@ namespace Geekbot.net.Lib.Media private string[] _pumpkinImages; private string[] _turtlesImages; - public MediaProvider(Random rnd, IGeekbotLogger logger) + public MediaProvider(IGeekbotLogger logger) { - _random = rnd; + _random = new Random(); _logger = logger; logger.Information("Geekbot", "Loading Media Files"); diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index da74508..c47fdb4 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -94,9 +94,8 @@ namespace Geekbot.net services = new ServiceCollection(); userRepository = new UserRepository(redis, logger); - var randomClient = new Random(); - var fortunes = new FortunesProvider(randomClient, logger); - var mediaProvider = new MediaProvider(randomClient, logger); + var fortunes = new FortunesProvider(logger); + var mediaProvider = new MediaProvider(logger); var malClient = new MalClient(redis, logger); var levelCalc = new LevelCalc(); var emojiConverter = new EmojiConverter(); @@ -108,7 +107,6 @@ namespace Geekbot.net services.AddSingleton(levelCalc); services.AddSingleton(emojiConverter); services.AddSingleton(audioUtils); - services.AddSingleton(randomClient); services.AddSingleton(fortunes); services.AddSingleton(mediaProvider); services.AddSingleton(malClient); From d40d89506e8a04c3c931628be218705a98d37408 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Wed, 14 Feb 2018 23:40:44 +0100 Subject: [PATCH 011/443] Upgrade UTF8JSON to 1.3.7 --- Geekbot.net/Geekbot.net.csproj | 2 +- Geekbot.net/Lib/ErrorHandler.cs | 6 ------ Geekbot.net/Lib/GeekbotLogger.cs | 4 +--- 3 files changed, 2 insertions(+), 10 deletions(-) diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 82b605a..89aab56 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -42,7 +42,7 @@ 4.3.0 - + diff --git a/Geekbot.net/Lib/ErrorHandler.cs b/Geekbot.net/Lib/ErrorHandler.cs index bd82668..3410e91 100644 --- a/Geekbot.net/Lib/ErrorHandler.cs +++ b/Geekbot.net/Lib/ErrorHandler.cs @@ -1,15 +1,9 @@ using System; using System.Net; -using System.Runtime.InteropServices.ComTypes; -using System.Security.Principal; using Discord.Commands; using Discord.Net; -using Nancy.Extensions; -using Serilog; using SharpRaven; using SharpRaven.Data; -using SharpRaven.Utilities; -using Utf8Json; namespace Geekbot.net.Lib { diff --git a/Geekbot.net/Lib/GeekbotLogger.cs b/Geekbot.net/Lib/GeekbotLogger.cs index 18e8c54..f2feda7 100644 --- a/Geekbot.net/Lib/GeekbotLogger.cs +++ b/Geekbot.net/Lib/GeekbotLogger.cs @@ -2,8 +2,6 @@ using System.Threading.Tasks; using Serilog; using Utf8Json; -using Utf8Json.Formatters; -using Utf8Json.Resolvers; namespace Geekbot.net.Lib { @@ -39,7 +37,7 @@ namespace Geekbot.net.Lib private Task HandleLogObject(string type, string source, string message, Exception stackTrace = null, object extra = null) { - var logJson = CreateLogObject(type, source, message, null, extra); + var logJson = CreateLogObject(type, source, message, stackTrace, extra); // fuck serilog _serilog.Information(logJson + "}"); return Task.CompletedTask; From 1c88dea796ea9c5ad91f6db6be20d10bca1823eb Mon Sep 17 00:00:00 2001 From: Runebaas Date: Thu, 15 Feb 2018 00:07:11 +0100 Subject: [PATCH 012/443] Don't error in rank when no entries are found or bot is not included --- Geekbot.net/Commands/Rank.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Geekbot.net/Commands/Rank.cs b/Geekbot.net/Commands/Rank.cs index 516c2aa..8c11171 100644 --- a/Geekbot.net/Commands/Rank.cs +++ b/Geekbot.net/Commands/Rank.cs @@ -56,9 +56,18 @@ namespace Geekbot.net.Commands } var messageList = _redis.HashGetAll($"{Context.Guild.Id}:{type}"); + if (messageList.Length == 0) + { + await ReplyAsync($"No {type.ToLowerInvariant()} found on this server"); + return; + } var sortedList = messageList.OrderByDescending(e => e.Value).ToList(); var guildMessages = (int) sortedList.First().Value; - sortedList.Remove(sortedList.Single(e => e.Name.ToString().Equals(_client.CurrentUser.Id.ToString()))); + var theBot = sortedList.FirstOrDefault(e => e.Name.ToString().Equals(_client.CurrentUser.Id.ToString())); + if (!string.IsNullOrEmpty(theBot.Name)) + { + sortedList.Remove(theBot); + } if (type == "Messages") sortedList.RemoveAt(0); var highscoreUsers = new Dictionary(); From 0498998630c1236dafb447ce78fded1fe7872723 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Fri, 16 Feb 2018 08:36:26 +0100 Subject: [PATCH 013/443] Add more things to slap --- Geekbot.net/Commands/Slap.cs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Geekbot.net/Commands/Slap.cs b/Geekbot.net/Commands/Slap.cs index 24ba835..09a6c15 100644 --- a/Geekbot.net/Commands/Slap.cs +++ b/Geekbot.net/Commands/Slap.cs @@ -53,7 +53,28 @@ namespace Geekbot.net.Commands "sunflower", "mousepad", "lolipop", - "bottle of rum" + "bottle of rum", + "cheese slice", + "critical 1", + "natural 20", + "mjölnir (aka mewmew)", + "kamehameha", + "copy of Twilight", + "med pack (get ready for the end boss)", + "derp", + "condom (used)", + "gremlin fed after midnight", + "wet baguette", + "exploding kitten", + "shiny piece of shit", + "mismatched pair of socks", + "horcrux", + "tuna", + "suggestion", + "teapot", + "candle", + "dictionary", + "powerless banhammer" }; _redis.HashIncrement($"{Context.Guild.Id}:SlapsRecieved", user.Id.ToString()); From 3e859e85332caac505963d864052bfb04cac9662 Mon Sep 17 00:00:00 2001 From: runebaas Date: Mon, 19 Feb 2018 21:35:45 +0100 Subject: [PATCH 014/443] Add real mana icons to mtg command --- Geekbot.net/Commands/MagicTheGathering.cs | 26 ++----- Geekbot.net/Lib/MtgManaConverter.cs | 85 +++++++++++++++++++++++ Geekbot.net/Lib/SimpleConextConverter.cs | 28 +------- Geekbot.net/Program.cs | 2 + 4 files changed, 93 insertions(+), 48 deletions(-) create mode 100644 Geekbot.net/Lib/MtgManaConverter.cs diff --git a/Geekbot.net/Commands/MagicTheGathering.cs b/Geekbot.net/Commands/MagicTheGathering.cs index bd0fd0a..e3cc864 100644 --- a/Geekbot.net/Commands/MagicTheGathering.cs +++ b/Geekbot.net/Commands/MagicTheGathering.cs @@ -13,12 +13,12 @@ namespace Geekbot.net.Commands public class Magicthegathering : ModuleBase { private readonly IErrorHandler _errorHandler; - private readonly IEmojiConverter _emojiConverter; + private readonly IMtgManaConverter _manaConverter; - public Magicthegathering(IErrorHandler errorHandler, IEmojiConverter emojiConverter) + public Magicthegathering(IErrorHandler errorHandler, IMtgManaConverter manaConverter) { _errorHandler = errorHandler; - _emojiConverter = emojiConverter; + _manaConverter = manaConverter; } [Command("mtg", RunMode = RunMode.Async)] @@ -46,7 +46,7 @@ namespace Geekbot.net.Commands if (card.ImageUrl != null) eb.ImageUrl = card.ImageUrl.ToString(); - if (!string.IsNullOrEmpty(card.Text)) eb.AddField("Text", card.Text); + if (!string.IsNullOrEmpty(card.Text)) eb.AddField("Text", _manaConverter.ConvertMana(card.Text)); if (!string.IsNullOrEmpty(card.Flavor)) eb.AddField("Flavor", card.Flavor); if (!string.IsNullOrEmpty(card.SetName)) eb.AddInlineField("Set", card.SetName); @@ -54,7 +54,7 @@ namespace Geekbot.net.Commands if (!string.IsNullOrEmpty(card.Loyalty)) eb.AddInlineField("Loyality", card.Loyalty); if (!string.IsNullOrEmpty(card.Toughness)) eb.AddInlineField("Thoughness", card.Toughness); - if (!string.IsNullOrEmpty(card.ManaCost)) eb.AddInlineField("Cost", ManaConverter(card.ManaCost)); + if (!string.IsNullOrEmpty(card.ManaCost)) eb.AddInlineField("Cost", _manaConverter.ConvertMana(card.ManaCost)); if (!string.IsNullOrEmpty(card.Rarity)) eb.AddInlineField("Rarity", card.Rarity); if (card.Legalities != null) @@ -87,21 +87,5 @@ namespace Geekbot.net.Commands return new Color(255, 252, 214); } } - - private string ManaConverter(string mana) - { - var rgx = new Regex("{(\\d)}"); - var groups = rgx.Match(mana).Groups; - if (groups.Count == 2) - { - mana = mana.Replace(groups[0].Value, _emojiConverter.numberToEmoji(int.Parse(groups[1].Value))); - } - return mana - .Replace("{W}", ":sunny:") - .Replace("{U}", ":droplet:") - .Replace("{B}", ":skull:") - .Replace("{R}", ":fire:") - .Replace("{G}", ":deciduous_tree:"); - } } } \ No newline at end of file diff --git a/Geekbot.net/Lib/MtgManaConverter.cs b/Geekbot.net/Lib/MtgManaConverter.cs new file mode 100644 index 0000000..176c1a4 --- /dev/null +++ b/Geekbot.net/Lib/MtgManaConverter.cs @@ -0,0 +1,85 @@ +using System.Collections.Generic; +using System.Linq; +using System.Text.RegularExpressions; + +namespace Geekbot.net.Lib +{ + public class MtgManaConverter : IMtgManaConverter + { + private Dictionary _manaDict; + + public MtgManaConverter() + { + // these emotes can be found at https://discord.gg/bz8HyA7 + + var manaDict = new Dictionary(); + manaDict.Add("{0}", "<:mtg_0:415216130043412482>"); + manaDict.Add("{1}", "<:mtg_1:415216130253389835>"); + manaDict.Add("{2}", "<:mtg_2:415216130031091713>"); + manaDict.Add("{3}", "<:mtg_3:415216130467037194>"); + manaDict.Add("{4}", "<:mtg_4:415216130026635295>"); + manaDict.Add("{5}", "<:mtg_5:415216130492203008>"); + manaDict.Add("{6}", "<:mtg_6:415216130458779658>"); + manaDict.Add("{7}", "<:mtg_7:415216130190475265>"); + manaDict.Add("{8}", "<:mtg_8:415216130517630986>"); + manaDict.Add("{9}", "<:mtg_9:415216130500722689>"); + manaDict.Add("{10", "<:mtg_10:415216130450391051>"); + manaDict.Add("{11}", "<:mtg_11:415216130811101185>"); + manaDict.Add("{12}", "<:mtg_12:415216130525888532>"); + manaDict.Add("{13}", "<:mtg_13:415216130517631000>"); + manaDict.Add("{14}", "<:mtg_14:415216130165178370>"); + manaDict.Add("{15}", "<:mtg_15:415216130576089108>"); + manaDict.Add("{16}", "<:mtg_16:415216130358247425>"); + manaDict.Add("{17}", "<:mtg_17:415216130601517056>"); + manaDict.Add("{18}", "<:mtg_18:415216130462842891>"); + manaDict.Add("{19}", "<:mtg_19:415216130614099988>"); + manaDict.Add("{20}", "<:mtg_20:415216130656043038>"); + manaDict.Add("{W}", "<:mtg_white:415216131515744256>"); + manaDict.Add("{U}", "<:mtg_blue:415216130521694209>"); + manaDict.Add("{B}", "<:mtg_black:415216130873884683>"); + manaDict.Add("{R}", "<:mtg_red:415216131322806272>"); + manaDict.Add("{G}", "<:mtg_green:415216131180331009>"); + manaDict.Add("{S}", "<:mtg_s:415216131293446144>"); + manaDict.Add("{2/W}", "<:mtg_2w:415216130446065664>"); + manaDict.Add("{2/U}", "<:mtg_2u:415216130429550592>"); + manaDict.Add("{2/B}", "<:mtg_2b:415216130160984065>"); + manaDict.Add("{2/R}", "<:mtg_2r:415216130454716436>"); + manaDict.Add("{2/G}", "<:mtg_2g:415216130420899840>"); + manaDict.Add("{W/U}", "<:mtg_wu:415216130970484736>"); + manaDict.Add("{W/B}", "<:mtg_wb:415216131222011914>"); + manaDict.Add("{U/R}", "<:mtg_ur:415216130962096128>"); + manaDict.Add("{U/B}", "<:mtg_ub:415216130865758218>"); + manaDict.Add("{R/W}", "<:mtg_rw:415216130878210057>"); + manaDict.Add("{G/W}", "<:mtg_gw:415216130567962646>"); + manaDict.Add("{G/U}", "<:mtg_gu:415216130739666945>"); + manaDict.Add("{B/R}", "<:mtg_br:415216130580283394>"); + manaDict.Add("{B/G}", "<:mtg_bg:415216130781609994>"); + manaDict.Add("{U/P}", "<:mtg_up:415216130861432842>"); + manaDict.Add("{R/P}", "<:mtg_rp:415216130597322783>"); + manaDict.Add("{G/P}", "<:mtg_gp:415216130760769546>"); + manaDict.Add("{W/P}", "<:mtg_wp:415216131541041172>"); + manaDict.Add("{B/P}", "<:mtg_bp:415216130664169482>"); + + _manaDict = manaDict; + } + + public string ConvertMana(string mana) + { + var rgx = Regex.Matches(mana, "(\\{(.*?)\\})"); + foreach (Match manaTypes in rgx) + { + var m = _manaDict.FirstOrDefault(x => x.Key == manaTypes.Value).Value; + if (!string.IsNullOrEmpty(m)) + { + mana = mana.Replace(manaTypes.Value, m); + } + } + return mana; + } + } + + public interface IMtgManaConverter + { + string ConvertMana(string mana); + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/SimpleConextConverter.cs b/Geekbot.net/Lib/SimpleConextConverter.cs index 3dbc58b..2e12e50 100644 --- a/Geekbot.net/Lib/SimpleConextConverter.cs +++ b/Geekbot.net/Lib/SimpleConextConverter.cs @@ -8,33 +8,7 @@ namespace Geekbot.net.Lib { public static MessageDto ConvertContext(ICommandContext context) { - return new MessageDto() - { - Message = new MessageDto.MessageContent() - { - Content = context.Message.Content, - Id = context.Message.Id.ToString(), - Attachments = context.Message.Attachments.Count, - ChannelMentions = context.Message.MentionedChannelIds.Count, - UserMentions = context.Message.MentionedUserIds.Count, - RoleMentions = context.Message.MentionedRoleIds.Count - }, - User = new MessageDto.IdAndName() - { - Id = context.User.Id.ToString(), - Name = $"{context.User.Username}#{context.User.Discriminator}" - }, - Guild = new MessageDto.IdAndName() - { - Id = context.Guild.Id.ToString(), - Name = context.Guild.Name - }, - Channel = new MessageDto.IdAndName() - { - Id = context.Channel.Id.ToString(), - Name = context.Channel.Name - } - }; + return ConvertSocketMessage((SocketMessage) context.Message); } public static MessageDto ConvertSocketMessage(SocketMessage message) { diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index c47fdb4..266f562 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -100,6 +100,7 @@ namespace Geekbot.net var levelCalc = new LevelCalc(); var emojiConverter = new EmojiConverter(); var audioUtils = new AudioUtils(); + var mtgManaConverter = new MtgManaConverter(); services.AddSingleton(redis); services.AddSingleton(logger); @@ -110,6 +111,7 @@ namespace Geekbot.net services.AddSingleton(fortunes); services.AddSingleton(mediaProvider); services.AddSingleton(malClient); + services.AddSingleton(mtgManaConverter); logger.Information("Geekbot", "Connecting to Discord"); From 4c1cdc361205f2272bdd67438769b510bf3fb112 Mon Sep 17 00:00:00 2001 From: runebaas Date: Mon, 19 Feb 2018 22:31:40 +0100 Subject: [PATCH 015/443] Merge Rolebot into Geekbot --- Geekbot.net/Commands/Role.cs | 43 ++++++++++++- Geekbot.net/Handlers.cs | 24 ++++++- Geekbot.net/Lib/MtgManaConverter.cs | 1 + Geekbot.net/Lib/ReactionListener.cs | 98 +++++++++++++++++++++++++++++ Geekbot.net/Program.cs | 6 +- 5 files changed, 167 insertions(+), 5 deletions(-) create mode 100644 Geekbot.net/Lib/ReactionListener.cs diff --git a/Geekbot.net/Commands/Role.cs b/Geekbot.net/Commands/Role.cs index 80ef4af..1de55ab 100644 --- a/Geekbot.net/Commands/Role.cs +++ b/Geekbot.net/Commands/Role.cs @@ -15,11 +15,13 @@ namespace Geekbot.net.Commands { private readonly IErrorHandler _errorHandler; private readonly IDatabase _redis; + private readonly IReactionListener _reactionListener; - public Role(IErrorHandler errorHandler, IDatabase redis) + public Role(IErrorHandler errorHandler, IDatabase redis, IReactionListener reactionListener) { _errorHandler = errorHandler; _redis = redis; + _reactionListener = reactionListener; } [Command(RunMode = RunMode.Async)] @@ -91,7 +93,7 @@ namespace Geekbot.net.Commands } } - [RequireUserPermission(GuildPermission.Administrator)] + [RequireUserPermission(GuildPermission.ManageRoles)] [Command("add", RunMode = RunMode.Async)] [Remarks(CommandCategories.Admin)] [Summary("Add a role to the whitelist.")] @@ -126,7 +128,7 @@ namespace Geekbot.net.Commands } } - [RequireUserPermission(GuildPermission.Administrator)] + [RequireUserPermission(GuildPermission.ManageRoles)] [Command("remove", RunMode = RunMode.Async)] [Remarks(CommandCategories.Admin)] [Summary("Remove a role from the whitelist.")] @@ -150,5 +152,40 @@ namespace Geekbot.net.Commands _errorHandler.HandleCommandException(e, Context); } } + + [RequireUserPermission(GuildPermission.ManageRoles)] + [Remarks(CommandCategories.Admin)] + [Summary("Give a role by clicking on an emoji")] + [Command("listen", RunMode = RunMode.Async)] + public async Task AddListener([Summary("messageID")] string messageId, [Summary("Emoji")] string emoji, [Summary("@role")] IRole role) + { + try + { + var message = (IUserMessage) await Context.Channel.GetMessageAsync(ulong.Parse(messageId)); + IEmote emote; + if (!emoji.StartsWith('<')) + { + var emo = new Emoji(emoji); + emote = (IEmote) emo; + } + else + { + emote = Emote.Parse(emoji); + } + await message.AddReactionAsync(emote); + await _reactionListener.AddRoleToListener(messageId, emote, role); + await Context.Message.DeleteAsync(); + } + catch (HttpException e) + { + await Context.Channel.SendMessageAsync("Custom emojis from other servers are not supported"); + Console.WriteLine(e); + } + catch (Exception e) + { + await Context.Channel.SendMessageAsync("Something went wrong... please try again on a new message"); + Console.WriteLine(e); + } + } } } \ No newline at end of file diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index aa8a154..7ca9390 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -19,8 +19,9 @@ namespace Geekbot.net private readonly IServiceProvider _servicesProvider; private readonly CommandService _commands; private readonly IUserRepository _userRepository; + private readonly IReactionListener _reactionListener; - public Handlers(IDiscordClient client, IGeekbotLogger logger, IDatabase redis, IServiceProvider servicesProvider, CommandService commands, IUserRepository userRepository) + public Handlers(IDiscordClient client, IGeekbotLogger logger, IDatabase redis, IServiceProvider servicesProvider, CommandService commands, IUserRepository userRepository, IReactionListener reactionListener) { _client = client; _logger = logger; @@ -28,6 +29,7 @@ namespace Geekbot.net _servicesProvider = servicesProvider; _commands = commands; _userRepository = userRepository; + _reactionListener = reactionListener; } // @@ -180,5 +182,25 @@ namespace Geekbot.net _logger.Error("Geekbot", "Failed to send delete message...", e); } } + + // + // Reactions + // + + public Task ReactionAdded(Cacheable cacheable, ISocketMessageChannel socketMessageChannel, SocketReaction reaction) + { + if (reaction.User.Value.IsBot) return Task.CompletedTask; + if (!_reactionListener.IsListener(reaction.MessageId)) return Task.CompletedTask; + _reactionListener.GiveRole(socketMessageChannel, reaction); + return Task.CompletedTask; + } + + public Task ReactionRemoved(Cacheable cacheable, ISocketMessageChannel socketMessageChannel, SocketReaction reaction) + { + if (reaction.User.Value.IsBot) return Task.CompletedTask; + if (!_reactionListener.IsListener(reaction.MessageId)) return Task.CompletedTask; + _reactionListener.RemoveRole(socketMessageChannel, reaction); + return Task.CompletedTask; + } } } diff --git a/Geekbot.net/Lib/MtgManaConverter.cs b/Geekbot.net/Lib/MtgManaConverter.cs index 176c1a4..6385fa8 100644 --- a/Geekbot.net/Lib/MtgManaConverter.cs +++ b/Geekbot.net/Lib/MtgManaConverter.cs @@ -40,6 +40,7 @@ namespace Geekbot.net.Lib manaDict.Add("{R}", "<:mtg_red:415216131322806272>"); manaDict.Add("{G}", "<:mtg_green:415216131180331009>"); manaDict.Add("{S}", "<:mtg_s:415216131293446144>"); + manaDict.Add("{T}", "<:mtg_tap:415258392727257088>"); manaDict.Add("{2/W}", "<:mtg_2w:415216130446065664>"); manaDict.Add("{2/U}", "<:mtg_2u:415216130429550592>"); manaDict.Add("{2/B}", "<:mtg_2b:415216130160984065>"); diff --git a/Geekbot.net/Lib/ReactionListener.cs b/Geekbot.net/Lib/ReactionListener.cs new file mode 100644 index 0000000..f0e75a6 --- /dev/null +++ b/Geekbot.net/Lib/ReactionListener.cs @@ -0,0 +1,98 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Discord; +using Discord.WebSocket; +using StackExchange.Redis; + +namespace Geekbot.net.Lib +{ + public class ReactionListener : IReactionListener + { + private readonly IDatabase _database; + private Dictionary> _listener; + + public ReactionListener(IDatabase database) + { + _database = database; + LoadListeners(); + } + + private Task LoadListeners() + { + var ids = _database.SetMembers("MessageIds"); + _listener = new Dictionary>(); + foreach (var id in ids) + { + var reactions = _database.HashGetAll($"Messages:{id}"); + var messageId = id; + var emojiDict = new Dictionary(); + foreach (var r in reactions) + { + IEmote emote; + if (!r.Name.ToString().StartsWith('<')) + { + var emo = new Emoji(r.Name); + emote = (IEmote) emo; + } + else + { + emote = Emote.Parse(r.Name); + } + emojiDict.Add(emote, ulong.Parse(r.Value)); + } + _listener.Add(messageId, emojiDict); + } + + return Task.CompletedTask; + } + + public bool IsListener(ulong id) + { + return _listener.ContainsKey(id.ToString()); + } + + public Task AddRoleToListener(string messageId, IEmote emoji, IRole role) + { + if (_database.SetMembers("MessageIds").All(e => e.ToString() != messageId)) + { + _database.SetAdd("MessageIds", messageId); + } + _database.HashSet($"Messages:{messageId}", new[] {new HashEntry(emoji.ToString(), role.Id.ToString())}); + _database.SetAdd("MessageIds", messageId); + if (_listener.ContainsKey(messageId)) + { + _listener[messageId].Add(emoji, role.Id); + return Task.CompletedTask; + } + var dict = new Dictionary(); + dict.Add(emoji, role.Id); + _listener.Add(messageId, dict); + return Task.CompletedTask; + } + + public async void RemoveRole(ISocketMessageChannel channel, SocketReaction reaction) + { + var roleId = _listener[reaction.MessageId.ToString()][reaction.Emote]; + var guild = (SocketGuildChannel) channel; + var role = guild.Guild.GetRole(roleId); + await ((IGuildUser) reaction.User.Value).RemoveRoleAsync(role); + } + + public async void GiveRole(ISocketMessageChannel channel, SocketReaction reaction) + { + var roleId = _listener[reaction.MessageId.ToString()][reaction.Emote]; + var guild = (SocketGuildChannel) channel; + var role = guild.Guild.GetRole(roleId); + await ((IGuildUser) reaction.User.Value).AddRoleAsync(role); + } + } + + public interface IReactionListener + { + bool IsListener(ulong id); + Task AddRoleToListener(string messageId, IEmote emoji, IRole role); + void RemoveRole(ISocketMessageChannel channel, SocketReaction reaction); + void GiveRole(ISocketMessageChannel message, SocketReaction reaction); + } +} \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 266f562..3f31945 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -135,14 +135,16 @@ namespace Geekbot.net logger.Information("Geekbot", "Registering Stuff"); var translationHandler = new TranslationHandler(client.Guilds, redis, logger); var errorHandler = new ErrorHandler(logger, translationHandler); + var reactionListener = new ReactionListener(redis); await commands.AddModulesAsync(Assembly.GetEntryAssembly()); services.AddSingleton(commands); services.AddSingleton(errorHandler); services.AddSingleton(translationHandler); services.AddSingleton(client); + services.AddSingleton(reactionListener); servicesProvider = services.BuildServiceProvider(); - var handlers = new Handlers(client, logger, redis, servicesProvider, commands, userRepository); + var handlers = new Handlers(client, logger, redis, servicesProvider, commands, userRepository, reactionListener); client.MessageReceived += handlers.RunCommand; client.MessageReceived += handlers.UpdateStats; @@ -150,6 +152,8 @@ namespace Geekbot.net client.UserJoined += handlers.UserJoined; client.UserUpdated += handlers.UserUpdated; client.UserLeft += handlers.UserLeft; + client.ReactionAdded += handlers.ReactionAdded; + client.ReactionRemoved += handlers.ReactionRemoved; if (firstStart || args.Contains("--reset")) { From a616d4254384b0424768025392374c13174b3d1d Mon Sep 17 00:00:00 2001 From: runebaas Date: Mon, 19 Feb 2018 22:55:22 +0100 Subject: [PATCH 016/443] Add colorless mana to mtg mana converter --- Geekbot.net/Lib/MtgManaConverter.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Geekbot.net/Lib/MtgManaConverter.cs b/Geekbot.net/Lib/MtgManaConverter.cs index 6385fa8..4337e68 100644 --- a/Geekbot.net/Lib/MtgManaConverter.cs +++ b/Geekbot.net/Lib/MtgManaConverter.cs @@ -41,6 +41,7 @@ namespace Geekbot.net.Lib manaDict.Add("{G}", "<:mtg_green:415216131180331009>"); manaDict.Add("{S}", "<:mtg_s:415216131293446144>"); manaDict.Add("{T}", "<:mtg_tap:415258392727257088>"); + manaDict.Add("{C}", "<:mtg_colorless:415216130706374666>"); manaDict.Add("{2/W}", "<:mtg_2w:415216130446065664>"); manaDict.Add("{2/U}", "<:mtg_2u:415216130429550592>"); manaDict.Add("{2/B}", "<:mtg_2b:415216130160984065>"); From e158e2196fdaa900de7c7274cadcf3c0ef066d17 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Tue, 20 Feb 2018 09:34:26 +0100 Subject: [PATCH 017/443] Revert SimpleContextConverter --- Geekbot.net/Lib/SimpleConextConverter.cs | 28 +++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/Geekbot.net/Lib/SimpleConextConverter.cs b/Geekbot.net/Lib/SimpleConextConverter.cs index 2e12e50..3dbc58b 100644 --- a/Geekbot.net/Lib/SimpleConextConverter.cs +++ b/Geekbot.net/Lib/SimpleConextConverter.cs @@ -8,7 +8,33 @@ namespace Geekbot.net.Lib { public static MessageDto ConvertContext(ICommandContext context) { - return ConvertSocketMessage((SocketMessage) context.Message); + return new MessageDto() + { + Message = new MessageDto.MessageContent() + { + Content = context.Message.Content, + Id = context.Message.Id.ToString(), + Attachments = context.Message.Attachments.Count, + ChannelMentions = context.Message.MentionedChannelIds.Count, + UserMentions = context.Message.MentionedUserIds.Count, + RoleMentions = context.Message.MentionedRoleIds.Count + }, + User = new MessageDto.IdAndName() + { + Id = context.User.Id.ToString(), + Name = $"{context.User.Username}#{context.User.Discriminator}" + }, + Guild = new MessageDto.IdAndName() + { + Id = context.Guild.Id.ToString(), + Name = context.Guild.Name + }, + Channel = new MessageDto.IdAndName() + { + Id = context.Channel.Id.ToString(), + Name = context.Channel.Name + } + }; } public static MessageDto ConvertSocketMessage(SocketMessage message) { From 8189d78ceacb5019c26f4c1947eb9f2b3e7f51a4 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Tue, 20 Feb 2018 16:26:56 +0100 Subject: [PATCH 018/443] Optimize dictionary in mtgmanaconverter --- Geekbot.net/Lib/MtgManaConverter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Geekbot.net/Lib/MtgManaConverter.cs b/Geekbot.net/Lib/MtgManaConverter.cs index 4337e68..aeb1c9e 100644 --- a/Geekbot.net/Lib/MtgManaConverter.cs +++ b/Geekbot.net/Lib/MtgManaConverter.cs @@ -70,7 +70,7 @@ namespace Geekbot.net.Lib var rgx = Regex.Matches(mana, "(\\{(.*?)\\})"); foreach (Match manaTypes in rgx) { - var m = _manaDict.FirstOrDefault(x => x.Key == manaTypes.Value).Value; + var m = _manaDict.GetValueOrDefault(manaTypes.Value); if (!string.IsNullOrEmpty(m)) { mana = mana.Replace(manaTypes.Value, m); From 440f3a97c298f5813349ac5f7a1970fe854efed2 Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 2 Mar 2018 23:05:14 +0100 Subject: [PATCH 019/443] Revive ping pong --- Geekbot.net/Commands/Admin.cs | 21 +++++++++++++++++++-- Geekbot.net/Handlers.cs | 9 +++++++++ Geekbot.net/Lib/MtgManaConverter.cs | 2 +- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/Geekbot.net/Commands/Admin.cs b/Geekbot.net/Commands/Admin.cs index 0b76b4b..681ff99 100644 --- a/Geekbot.net/Commands/Admin.cs +++ b/Geekbot.net/Commands/Admin.cs @@ -34,8 +34,7 @@ namespace Geekbot.net.Commands { _redis.HashSet($"{Context.Guild.Id}:Settings", new[] {new HashEntry("WelcomeMsg", welcomeMessage)}); var formatedMessage = welcomeMessage.Replace("$user", Context.User.Mention); - await ReplyAsync("Welcome message has been changed\r\nHere is an example of how it would look:\r\n" + - formatedMessage); + await ReplyAsync($"Welcome message has been changed\r\nHere is an example of how it would look:\r\n{formatedMessage}"); } [Command("modchannel", RunMode = RunMode.Async)] @@ -155,5 +154,23 @@ namespace Geekbot.net.Commands _errorHandler.HandleCommandException(e, Context); } } + + [Command("ping", RunMode = RunMode.Async)] + [Remarks(CommandCategories.Admin)] + [Summary("Enable the ping reply.")] + public async Task togglePing() + { + try + { + bool.TryParse(_redis.HashGet($"{Context.Guild.Id}:Settings", "ping"), out var current); + _redis.HashSet($"{Context.Guild.Id}:Settings", new[] {new HashEntry("ping", current ? "false" : "true"), }); + await ReplyAsync(!current ? "i will reply to ping now" : "No more pongs..."); + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + } + } + } } \ No newline at end of file diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index 7ca9390..c1e0ee3 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -49,6 +49,15 @@ namespace Geekbot.net message.Channel.SendMessageAsync("hui!!!"); return Task.CompletedTask; } + if (lowCaseMsg.StartsWith("ping ") || lowCaseMsg.Equals("ping")) + { + bool.TryParse(_redis.HashGet($"{((SocketGuildChannel) message.Channel).Guild.Id}:Settings", "ping"), out var allowPings); + if (allowPings) + { + message.Channel.SendMessageAsync("pong"); + return Task.CompletedTask; + } + } if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos))) return Task.CompletedTask; var context = new CommandContext(_client, message); diff --git a/Geekbot.net/Lib/MtgManaConverter.cs b/Geekbot.net/Lib/MtgManaConverter.cs index aeb1c9e..098542e 100644 --- a/Geekbot.net/Lib/MtgManaConverter.cs +++ b/Geekbot.net/Lib/MtgManaConverter.cs @@ -67,7 +67,7 @@ namespace Geekbot.net.Lib public string ConvertMana(string mana) { - var rgx = Regex.Matches(mana, "(\\{(.*?)\\})"); + var rgx = Regex.Matches(mana, @"(\{(.*?)\})"); foreach (Match manaTypes in rgx) { var m = _manaDict.GetValueOrDefault(manaTypes.Value); From 75cbcfff76adc01655edb8225c8259c760b6ec4d Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 2 Mar 2018 23:35:14 +0100 Subject: [PATCH 020/443] Add a check to help --- Geekbot.net/Commands/Help.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Geekbot.net/Commands/Help.cs b/Geekbot.net/Commands/Help.cs index aa2cc57..83374a8 100644 --- a/Geekbot.net/Commands/Help.cs +++ b/Geekbot.net/Commands/Help.cs @@ -1,6 +1,7 @@ using System; using System.Text; using System.Threading.Tasks; +using Discord; using Discord.Commands; using Geekbot.net.Lib; @@ -28,6 +29,7 @@ namespace Geekbot.net.Commands sb.AppendLine("https://geekbot.pizzaandcoffee.rocks/commands"); var dm = await Context.User.GetOrCreateDMChannelAsync(); await dm.SendMessageAsync(sb.ToString()); + Context.Message.AddReactionAsync(new Emoji("✅")); } catch (Exception e) { From 6bc6fb69afefd5f28dbd0fb08c26c2196f17c411 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 28 Mar 2018 21:28:16 +0200 Subject: [PATCH 021/443] Now with 100% more pinguins --- Geekbot.net/Commands/RandomAnimals.cs | 27 +++++++++++++++++++++----- Geekbot.net/Geekbot.net.csproj | 3 +++ Geekbot.net/Lib/Media/MediaProvider.cs | 15 ++++++++++++++ Geekbot.net/Storage/pinguins | 13 +++++++++++++ 4 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 Geekbot.net/Storage/pinguins diff --git a/Geekbot.net/Commands/RandomAnimals.cs b/Geekbot.net/Commands/RandomAnimals.cs index 29930eb..84f0323 100644 --- a/Geekbot.net/Commands/RandomAnimals.cs +++ b/Geekbot.net/Commands/RandomAnimals.cs @@ -1,4 +1,5 @@ using System.Threading.Tasks; +using Discord; using Discord.Commands; using Geekbot.net.Lib; using Geekbot.net.Lib.Media; @@ -19,7 +20,7 @@ namespace Geekbot.net.Commands [Summary("Get a random panda image")] public async Task panda() { - await ReplyAsync(_mediaProvider.getPanda()); + await ReplyAsync("", false, eb(_mediaProvider.getPanda())); } [Command("croissant", RunMode = RunMode.Async)] @@ -28,7 +29,7 @@ namespace Geekbot.net.Commands [Summary("Get a random croissant image")] public async Task croissant() { - await ReplyAsync(_mediaProvider.getCrossant()); + await ReplyAsync("", false, eb(_mediaProvider.getCrossant())); } [Command("pumpkin", RunMode = RunMode.Async)] @@ -36,7 +37,7 @@ namespace Geekbot.net.Commands [Summary("Get a random pumpkin image")] public async Task pumpkin() { - await ReplyAsync(_mediaProvider.getPumpkin()); + await ReplyAsync("", false, eb(_mediaProvider.getPumpkin())); } [Command("squirrel", RunMode = RunMode.Async)] @@ -44,7 +45,7 @@ namespace Geekbot.net.Commands [Summary("Get a random squirrel image")] public async Task squirrel() { - await ReplyAsync(_mediaProvider.getSquirrel()); + await ReplyAsync("", false, eb(_mediaProvider.getSquirrel())); } [Command("turtle", RunMode = RunMode.Async)] @@ -52,7 +53,23 @@ namespace Geekbot.net.Commands [Summary("Get a random turtle image")] public async Task turtle() { - await ReplyAsync(_mediaProvider.getTurtle()); + await ReplyAsync("", false, eb(_mediaProvider.getTurtle())); + } + + [Command("pinguin", RunMode = RunMode.Async)] + [Alias("pingu")] + [Remarks(CommandCategories.Randomness)] + [Summary("Get a random turtle image")] + public async Task pinguin() + { + await ReplyAsync("", false, eb(_mediaProvider.getPinguin())); + } + + private EmbedBuilder eb(string image) + { + var eb = new EmbedBuilder(); + eb.ImageUrl = image; + return eb; } } } \ No newline at end of file diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 89aab56..0a772d8 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -69,5 +69,8 @@ PreserveNewest + + PreserveNewest + \ No newline at end of file diff --git a/Geekbot.net/Lib/Media/MediaProvider.cs b/Geekbot.net/Lib/Media/MediaProvider.cs index 0a3ff6c..2f0204e 100644 --- a/Geekbot.net/Lib/Media/MediaProvider.cs +++ b/Geekbot.net/Lib/Media/MediaProvider.cs @@ -16,6 +16,7 @@ namespace Geekbot.net.Lib.Media private string[] _squirrelImages; private string[] _pumpkinImages; private string[] _turtlesImages; + private string[] _pinguinImages; public MediaProvider(IGeekbotLogger logger) { @@ -30,6 +31,7 @@ namespace Geekbot.net.Lib.Media LoadSquirrels(); LoadPumpkins(); LoadTurtles(); + LoadPinguins(); } private void LoadCheckem() @@ -74,6 +76,13 @@ namespace Geekbot.net.Lib.Media _logger.Debug("Geekbot", $"Loaded {_turtlesImages.Length} Turtle Images"); } + private void LoadPinguins() + { + var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/pinguins")); + _pinguinImages = rawLinks.Split("\n"); + _logger.Debug("Geekbot", $"Loaded {_pinguinImages.Length} Pinguin Images"); + } + public string getCheckem() { return _checkemImages[_random.Next(0, _checkemImages.Length)]; @@ -103,6 +112,11 @@ namespace Geekbot.net.Lib.Media { return _turtlesImages[_random.Next(0, _turtlesImages.Length)]; } + + public string getPinguin() + { + return _pinguinImages[_random.Next(0, _pinguinImages.Length)]; + } } public interface IMediaProvider @@ -113,5 +127,6 @@ namespace Geekbot.net.Lib.Media string getSquirrel(); string getPumpkin(); string getTurtle(); + string getPinguin(); } } \ No newline at end of file diff --git a/Geekbot.net/Storage/pinguins b/Geekbot.net/Storage/pinguins new file mode 100644 index 0000000..631f9d0 --- /dev/null +++ b/Geekbot.net/Storage/pinguins @@ -0,0 +1,13 @@ +https://i.ytimg.com/vi/Qr6sULJnu2o/maxresdefault.jpg +https://www.apex-expeditions.com/wp-content/uploads/2015/08/newzealandSlider_Macquarie_ElephantSealKingPenguins_GRiehle_1366x601.jpg +https://www.birdlife.org/sites/default/files/styles/1600/public/slide.jpg?itok=HRhQfA1S +http://experimentexchange.com/wp-content/uploads/2016/07/penguins-fact.jpg +http://images.mentalfloss.com/sites/default/files/styles/mf_image_16x9/public/istock-511366776.jpg?itok=cWhdWNZ8&resize=1100x619 +https://www.thevaporplace.ch/media/catalog/product/cache/1/thumbnail/800x800/9df78eab33525d08d6e5fb8d27136e95/a/t/atopack_penguin-15.jpg +https://www.superfastbusiness.com/wp-content/uploads/2015/10/real-time-penguin-algorithm-featured.jpg +http://www.antarctica.gov.au/__data/assets/image/0011/147737/varieties/antarctic.jpg +https://vignette.wikia.nocookie.net/robloxcreepypasta/images/1/11/AAEAAQAAAAAAAAdkAAAAJDc3YzkyYjJhLTYyZjctNDY2Mi04M2VjLTg4NjY4ZjgwYzRmNg.png/revision/latest?cb=20180207200526 +https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR3xV0lhpZuhT8Nmm6LaITsppZ7VfWcWXuyu2cPHrlv_dt_M92K5g +http://goboiano.com/wp-content/uploads/2017/04/Penguin-Kemeno-Friends-Waifu.jpg +https://cdn.yoast.com/app/uploads/2015/10/Penguins_1200x628.png +https://images.justwatch.com/backdrop/8611153/s1440/pingu \ No newline at end of file From f127e0c02ba0552abff4847873cc79020c58b861 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 28 Mar 2018 21:29:42 +0200 Subject: [PATCH 022/443] Disable !cat --- Geekbot.net/Commands/Cat.cs | 39 +++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/Geekbot.net/Commands/Cat.cs b/Geekbot.net/Commands/Cat.cs index 2120fe7..e096f20 100644 --- a/Geekbot.net/Commands/Cat.cs +++ b/Geekbot.net/Commands/Cat.cs @@ -24,25 +24,26 @@ namespace Geekbot.net.Commands { try { - using (var client = new HttpClient()) - { - try - { - client.BaseAddress = new Uri("http://random.cat"); - var response = await client.GetAsync("/meow.php"); - response.EnsureSuccessStatusCode(); - - var stringResponse = await response.Content.ReadAsStringAsync(); - var catFile = JsonConvert.DeserializeObject(stringResponse); - var eb = new EmbedBuilder(); - eb.ImageUrl = catFile.file; - await ReplyAsync("", false, eb.Build()); - } - catch (HttpRequestException e) - { - await ReplyAsync($"Seems like the dog cought the cat (error occured)\r\n{e.Message}"); - } - } +// using (var client = new HttpClient()) +// { +// try +// { +// client.BaseAddress = new Uri("http://random.cat"); +// var response = await client.GetAsync("/meow.php"); +// response.EnsureSuccessStatusCode(); +// +// var stringResponse = await response.Content.ReadAsStringAsync(); +// var catFile = JsonConvert.DeserializeObject(stringResponse); +// var eb = new EmbedBuilder(); +// eb.ImageUrl = catFile.file; +// await ReplyAsync("", false, eb.Build()); +// } +// catch (HttpRequestException e) +// { +// await ReplyAsync($"Seems like the dog cought the cat (error occured)\r\n{e.Message}"); +// } +// } + await ReplyAsync("Command disabled for now because http://random.cat is down"); } catch (Exception e) { From 3f02f90d70f1855db303619555078a180cea49b9 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 28 Mar 2018 21:45:29 +0200 Subject: [PATCH 023/443] Enable cat command again --- Geekbot.net/Commands/Cat.cs | 115 ++++++++++++++++++------------------ 1 file changed, 57 insertions(+), 58 deletions(-) diff --git a/Geekbot.net/Commands/Cat.cs b/Geekbot.net/Commands/Cat.cs index e096f20..1f97d5a 100644 --- a/Geekbot.net/Commands/Cat.cs +++ b/Geekbot.net/Commands/Cat.cs @@ -1,59 +1,58 @@ -using System; -using System.Net.Http; -using System.Threading.Tasks; -using Discord; -using Discord.Commands; -using Geekbot.net.Lib; -using Newtonsoft.Json; - -namespace Geekbot.net.Commands -{ - public class Cat : ModuleBase - { - private readonly IErrorHandler _errorHandler; - - public Cat(IErrorHandler errorHandler) - { - _errorHandler = errorHandler; - } - - [Command("cat", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Randomness)] - [Summary("Return a random image of a cat.")] - public async Task Say() - { - try - { -// using (var client = new HttpClient()) -// { -// try -// { -// client.BaseAddress = new Uri("http://random.cat"); -// var response = await client.GetAsync("/meow.php"); -// response.EnsureSuccessStatusCode(); -// -// var stringResponse = await response.Content.ReadAsStringAsync(); -// var catFile = JsonConvert.DeserializeObject(stringResponse); -// var eb = new EmbedBuilder(); -// eb.ImageUrl = catFile.file; -// await ReplyAsync("", false, eb.Build()); -// } -// catch (HttpRequestException e) -// { -// await ReplyAsync($"Seems like the dog cought the cat (error occured)\r\n{e.Message}"); -// } -// } - await ReplyAsync("Command disabled for now because http://random.cat is down"); - } - catch (Exception e) - { - _errorHandler.HandleCommandException(e, Context); - } - } - - private class CatResponse - { - public string file { get; set; } - } - } +using System; +using System.Net.Http; +using System.Threading.Tasks; +using Discord; +using Discord.Commands; +using Geekbot.net.Lib; +using Newtonsoft.Json; + +namespace Geekbot.net.Commands +{ + public class Cat : ModuleBase + { + private readonly IErrorHandler _errorHandler; + + public Cat(IErrorHandler errorHandler) + { + _errorHandler = errorHandler; + } + + [Command("cat", RunMode = RunMode.Async)] + [Remarks(CommandCategories.Randomness)] + [Summary("Return a random image of a cat.")] + public async Task Say() + { + try + { + using (var client = new HttpClient()) + { + try + { + client.BaseAddress = new Uri("https://aws.random.cat"); + var response = await client.GetAsync("/meow"); + response.EnsureSuccessStatusCode(); + + var stringResponse = await response.Content.ReadAsStringAsync(); + var catFile = JsonConvert.DeserializeObject(stringResponse); + var eb = new EmbedBuilder(); + eb.ImageUrl = catFile.file; + await ReplyAsync("", false, eb.Build()); + } + catch + { + await ReplyAsync("Seems like the dog cought the cat (error occured)"); + } + } + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + } + } + + private class CatResponse + { + public string file { get; set; } + } + } } \ No newline at end of file From 8974d6df7edc3bd369a4013d71ce3adcdb3ff3a4 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 28 Mar 2018 22:14:07 +0200 Subject: [PATCH 024/443] Add in some foxes aswell --- Geekbot.net/Commands/RandomAnimals.cs | 8 +++++++ Geekbot.net/Geekbot.net.csproj | 3 +++ Geekbot.net/Lib/Media/MediaProvider.cs | 15 +++++++++++++ Geekbot.net/Storage/foxes | 29 ++++++++++++++++++++++++++ 4 files changed, 55 insertions(+) create mode 100644 Geekbot.net/Storage/foxes diff --git a/Geekbot.net/Commands/RandomAnimals.cs b/Geekbot.net/Commands/RandomAnimals.cs index 84f0323..f9dead9 100644 --- a/Geekbot.net/Commands/RandomAnimals.cs +++ b/Geekbot.net/Commands/RandomAnimals.cs @@ -64,6 +64,14 @@ namespace Geekbot.net.Commands { await ReplyAsync("", false, eb(_mediaProvider.getPinguin())); } + + [Command("fox", RunMode = RunMode.Async)] + [Remarks(CommandCategories.Randomness)] + [Summary("Get a random turtle image")] + public async Task fox() + { + await ReplyAsync("", false, eb(_mediaProvider.getFox())); + } private EmbedBuilder eb(string image) { diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 0a772d8..1a11cd9 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -72,5 +72,8 @@ PreserveNewest + + PreserveNewest + \ No newline at end of file diff --git a/Geekbot.net/Lib/Media/MediaProvider.cs b/Geekbot.net/Lib/Media/MediaProvider.cs index 2f0204e..b8aeaa3 100644 --- a/Geekbot.net/Lib/Media/MediaProvider.cs +++ b/Geekbot.net/Lib/Media/MediaProvider.cs @@ -17,6 +17,7 @@ namespace Geekbot.net.Lib.Media private string[] _pumpkinImages; private string[] _turtlesImages; private string[] _pinguinImages; + private string[] _foxImages; public MediaProvider(IGeekbotLogger logger) { @@ -32,6 +33,7 @@ namespace Geekbot.net.Lib.Media LoadPumpkins(); LoadTurtles(); LoadPinguins(); + LoadFoxes(); } private void LoadCheckem() @@ -83,6 +85,13 @@ namespace Geekbot.net.Lib.Media _logger.Debug("Geekbot", $"Loaded {_pinguinImages.Length} Pinguin Images"); } + private void LoadFoxes() + { + var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/foxes")); + _foxImages = rawLinks.Split("\n"); + _logger.Debug("Geekbot", $"Loaded {_foxImages.Length} Foxes Images"); + } + public string getCheckem() { return _checkemImages[_random.Next(0, _checkemImages.Length)]; @@ -117,6 +126,11 @@ namespace Geekbot.net.Lib.Media { return _pinguinImages[_random.Next(0, _pinguinImages.Length)]; } + + public string getFox() + { + return _foxImages[_random.Next(0, _foxImages.Length)]; + } } public interface IMediaProvider @@ -128,5 +142,6 @@ namespace Geekbot.net.Lib.Media string getPumpkin(); string getTurtle(); string getPinguin(); + string getFox(); } } \ No newline at end of file diff --git a/Geekbot.net/Storage/foxes b/Geekbot.net/Storage/foxes new file mode 100644 index 0000000..020c1cf --- /dev/null +++ b/Geekbot.net/Storage/foxes @@ -0,0 +1,29 @@ +https://i.ytimg.com/vi/qF6OOGuT_hI/maxresdefault.jpg +https://www.hd-wallpapersdownload.com/script/bulk-upload/desktop-funny-fox-wallpaper.jpg +http://moziru.com/images/drawn-fox-funny-18.jpg +https://static.tumblr.com/bb34d8f163098ad1daafcffbdbb03975/rk23uap/Nwwp0rmi2/tumblr_static_tumblr_static__640.jpg +https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQoHUFOnZ3wJ2kT1skNdztFXXSvpU8bEoGS1alNZiuyLXvGJhcY +http://childrenstorytales.com/wp-content/uploads/2011/03/how-to-draw-a-red-fox-in-the-snow.jpg +https://www.popsci.com/sites/popsci.com/files/styles/1000_1x_/public/import/2013/images/2013/09/redfoxyawn.jpg?itok=yRkSVe8T +https://hdqwalls.com/wallpapers/wild-fox-art.jpg +https://ae01.alicdn.com/kf/HTB1Q9dpLpXXXXbhXpXXq6xXFXXXl/new-cute-fox-toy-lifelike-soft-long-yellow-fox-doll-gift-about-73cm.jpg_640x640.jpg +https://i.imgur.com/ktK9yXX.jpg +https://res.cloudinary.com/teepublic/image/private/s--yTx2ncFA--/t_Preview/b_rgb:c8e0ec,c_limit,f_auto,h_313,q_90,w_313/v1506478249/production/designs/1932607_0 +http://4.bp.blogspot.com/-Hz-o_KYj3Xk/Vlm2mwbztjI/AAAAAAAA8Ss/jbH5ovjmC9A/s1600/ScreenShot5502.jpg +https://i.pinimg.com/originals/1e/d5/2f/1ed52f70873a95ac02fa074e48edfb71.jpg +https://i.imgur.com/2vCrtap.jpg +https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSfukWGu_IBaDeJOMBqOhVAwsDfqEPw0BFpCn5_-Iyr_xjd7zi9 +https://cdn.pixabay.com/photo/2017/01/31/18/36/animal-2026297_960_720.png +https://i.pinimg.com/originals/e2/63/67/e26367a0844633b2a697b0a9d69e8cc9.jpg +https://i.ebayimg.com/images/g/BvkAAOSwqxdTqrip/s-l300.jpg +https://res.cloudinary.com/teepublic/image/private/s--1R53bger--/t_Preview/b_rgb:eae0c7,c_limit,f_jpg,h_630,q_90,w_630/v1481013120/production/designs/914528_1.jpg +https://i.pinimg.com/originals/97/fe/69/97fe698462afde7b4209ccefeecbce71.jpg +https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT6G0ch6g-wG1TuDJ6BbkOFelMNnkgFXC6CxOw7qSNjoFkx-BCe +https://wallpaperscraft.com/image/fox_forest_grass_117190_540x960.jpg +https://image.freepik.com/free-vector/cartoon-flat-illustration-funny-cute-fox_6317-1174.jpg +https://orig00.deviantart.net/2feb/f/2013/137/a/f/fox_and_curious_squirrel_by_tamarar-d65ju8d.jpg +https://res.cloudinary.com/teepublic/image/private/s--dICeNmBx--/t_Preview/b_rgb:6e2229,c_limit,f_jpg,h_630,q_90,w_630/v1505243196/production/designs/1890493_1.jpg +https://vignette.wikia.nocookie.net/puppyinmypocketfanon/images/4/49/L-Baby-Fox.jpg/revision/latest?cb=20130421001806 +http://7-themes.com/data_images/out/69/7009194-fox-puppy.jpg +http://www.tehcute.com/pics/201401/little-fox-big.jpg +https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR6QXB1APLdUsyzO39kPvhnC9cOvcwzEtsxown9QjWilWppia2mwg \ No newline at end of file From f4ced55d15abff3ad019c9e69811d7be0856b9a7 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 4 Apr 2018 01:41:37 +0200 Subject: [PATCH 025/443] Disable voice features --- Geekbot.net/Commands/Voice.cs | 204 +++++++++++++++++----------------- 1 file changed, 102 insertions(+), 102 deletions(-) diff --git a/Geekbot.net/Commands/Voice.cs b/Geekbot.net/Commands/Voice.cs index cda1f8b..ca65913 100644 --- a/Geekbot.net/Commands/Voice.cs +++ b/Geekbot.net/Commands/Voice.cs @@ -1,102 +1,102 @@ -using System; -using System.IO; -using System.Threading.Tasks; -using Discord; -using Discord.Commands; -using Geekbot.net.Lib; - -namespace Geekbot.net.Commands -{ - public class Voice : ModuleBase - { - private readonly IAudioUtils _audioUtils; - private readonly IErrorHandler _errorHandler; - - public Voice(IErrorHandler errorHandler, IAudioUtils audioUtils) - { - _errorHandler = errorHandler; - _audioUtils = audioUtils; - } - - [Command("join")] - public async Task JoinChannel() - { - try - { - // Get the audio channel - var channel = (Context.User as IGuildUser)?.VoiceChannel; - if (channel == null) - { - await Context.Channel.SendMessageAsync( - "User must be in a voice channel, or a voice channel must be passed as an argument."); - return; - } - - // For the next step with transmitting audio, you would want to pass this Audio Client in to a service. - var audioClient = await channel.ConnectAsync(); - _audioUtils.StoreAudioClient(Context.Guild.Id, audioClient); - await ReplyAsync($"Connected to {channel.Name}"); - } - catch (Exception e) - { - _errorHandler.HandleCommandException(e, Context); - } - } - - [Command("disconnect")] - public async Task DisconnectChannel() - { - try - { - var audioClient = _audioUtils.GetAudioClient(Context.Guild.Id); - if (audioClient == null) - { - await Context.Channel.SendMessageAsync("I'm not in a voice channel at the moment"); - return; - } - - await audioClient.StopAsync(); - await ReplyAsync("Disconnected from channel!"); - _audioUtils.Cleanup(Context.Guild.Id); - } - catch (Exception e) - { - _errorHandler.HandleCommandException(e, Context); - _audioUtils.Cleanup(Context.Guild.Id); - } - } - - [Command("ytplay")] - public async Task ytplay(string url) - { - try - { - if (!url.Contains("youtube")) - { - await ReplyAsync("I can only play youtube videos"); - return; - } - var audioClient = _audioUtils.GetAudioClient(Context.Guild.Id); - if (audioClient == null) - { - await ReplyAsync("I'm not in a voice channel at the moment"); - return; - } - - var message = await Context.Channel.SendMessageAsync("Just a second, i'm still a bit slow at this"); - var ffmpeg = _audioUtils.CreateStreamFromYoutube(url, Context.Guild.Id); - var output = ffmpeg.StandardOutput.BaseStream; - await message.ModifyAsync(msg => msg.Content = "**Playing!** Please note that this feature is experimental"); - var discord = audioClient.CreatePCMStream(Discord.Audio.AudioApplication.Mixed); - await output.CopyToAsync(discord); - await discord.FlushAsync(); - _audioUtils.Cleanup(Context.Guild.Id); - } - catch (Exception e) - { - _errorHandler.HandleCommandException(e, Context); - _audioUtils.Cleanup(Context.Guild.Id); - } - } - } -} \ No newline at end of file +//using System; +//using System.IO; +//using System.Threading.Tasks; +//using Discord; +//using Discord.Commands; +//using Geekbot.net.Lib; +// +//namespace Geekbot.net.Commands +//{ +// public class Voice : ModuleBase +// { +// private readonly IAudioUtils _audioUtils; +// private readonly IErrorHandler _errorHandler; +// +// public Voice(IErrorHandler errorHandler, IAudioUtils audioUtils) +// { +// _errorHandler = errorHandler; +// _audioUtils = audioUtils; +// } +// +// [Command("join")] +// public async Task JoinChannel() +// { +// try +// { +// // Get the audio channel +// var channel = (Context.User as IGuildUser)?.VoiceChannel; +// if (channel == null) +// { +// await Context.Channel.SendMessageAsync( +// "User must be in a voice channel, or a voice channel must be passed as an argument."); +// return; +// } +// +// // For the next step with transmitting audio, you would want to pass this Audio Client in to a service. +// var audioClient = await channel.ConnectAsync(); +// _audioUtils.StoreAudioClient(Context.Guild.Id, audioClient); +// await ReplyAsync($"Connected to {channel.Name}"); +// } +// catch (Exception e) +// { +// _errorHandler.HandleCommandException(e, Context); +// } +// } +// +// [Command("disconnect")] +// public async Task DisconnectChannel() +// { +// try +// { +// var audioClient = _audioUtils.GetAudioClient(Context.Guild.Id); +// if (audioClient == null) +// { +// await Context.Channel.SendMessageAsync("I'm not in a voice channel at the moment"); +// return; +// } +// +// await audioClient.StopAsync(); +// await ReplyAsync("Disconnected from channel!"); +// _audioUtils.Cleanup(Context.Guild.Id); +// } +// catch (Exception e) +// { +// _errorHandler.HandleCommandException(e, Context); +// _audioUtils.Cleanup(Context.Guild.Id); +// } +// } +// +// [Command("ytplay")] +// public async Task ytplay(string url) +// { +// try +// { +// if (!url.Contains("youtube")) +// { +// await ReplyAsync("I can only play youtube videos"); +// return; +// } +// var audioClient = _audioUtils.GetAudioClient(Context.Guild.Id); +// if (audioClient == null) +// { +// await ReplyAsync("I'm not in a voice channel at the moment"); +// return; +// } +// +// var message = await Context.Channel.SendMessageAsync("Just a second, i'm still a bit slow at this"); +// var ffmpeg = _audioUtils.CreateStreamFromYoutube(url, Context.Guild.Id); +// var output = ffmpeg.StandardOutput.BaseStream; +// await message.ModifyAsync(msg => msg.Content = "**Playing!** Please note that this feature is experimental"); +// var discord = audioClient.CreatePCMStream(Discord.Audio.AudioApplication.Mixed); +// await output.CopyToAsync(discord); +// await discord.FlushAsync(); +// _audioUtils.Cleanup(Context.Guild.Id); +// } +// catch (Exception e) +// { +// _errorHandler.HandleCommandException(e, Context); +// _audioUtils.Cleanup(Context.Guild.Id); +// } +// } +// } +//} \ No newline at end of file From 846c928f5f5dd551963b61e5a4490fd42d3e2353 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 28 Apr 2018 01:01:48 +0200 Subject: [PATCH 026/443] Add wikipedia api client, add wikipedia command, show errors in chat when debugging --- .gitignore | 4 + Geekbot.net.sln | 6 ++ Geekbot.net/Commands/Wikipedia.cs | 94 +++++++++++++++++++ Geekbot.net/Geekbot.net.csproj | 6 +- Geekbot.net/Lib/ErrorHandler.cs | 18 +++- Geekbot.net/Program.cs | 9 +- WikipediaApi/IWikipediaClient.cs | 10 ++ WikipediaApi/Page/PageApiUrls.cs | 14 +++ WikipediaApi/Page/PageContentUrlCollection.cs | 8 ++ WikipediaApi/Page/PageContentUrls.cs | 12 +++ WikipediaApi/Page/PageCoordinates.cs | 8 ++ WikipediaApi/Page/PageImage.cs | 12 +++ WikipediaApi/Page/PageNamespace.cs | 8 ++ WikipediaApi/Page/PagePreview.cs | 68 ++++++++++++++ WikipediaApi/Page/PageTitles.cs | 10 ++ WikipediaApi/Page/PageTypes.cs | 19 ++++ WikipediaApi/WikipediaApi.csproj | 8 ++ WikipediaApi/WikipediaClient.cs | 29 ++++++ 18 files changed, 334 insertions(+), 9 deletions(-) create mode 100644 Geekbot.net/Commands/Wikipedia.cs create mode 100644 WikipediaApi/IWikipediaClient.cs create mode 100644 WikipediaApi/Page/PageApiUrls.cs create mode 100644 WikipediaApi/Page/PageContentUrlCollection.cs create mode 100644 WikipediaApi/Page/PageContentUrls.cs create mode 100644 WikipediaApi/Page/PageCoordinates.cs create mode 100644 WikipediaApi/Page/PageImage.cs create mode 100644 WikipediaApi/Page/PageNamespace.cs create mode 100644 WikipediaApi/Page/PagePreview.cs create mode 100644 WikipediaApi/Page/PageTitles.cs create mode 100644 WikipediaApi/Page/PageTypes.cs create mode 100644 WikipediaApi/WikipediaApi.csproj create mode 100644 WikipediaApi/WikipediaClient.cs diff --git a/.gitignore b/.gitignore index c99d9e6..fe7e3d4 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,7 @@ UpgradeLog.htm .vscode Geekbot.net/Logs/* !/Geekbot.net/Logs/.keep +Geekbot.net.sln.DotSettings.user +Geekbot.net/temp/ +WikipediaApi/bin/ +WikipediaApi/obj/ diff --git a/Geekbot.net.sln b/Geekbot.net.sln index 5011ad4..b542f25 100644 --- a/Geekbot.net.sln +++ b/Geekbot.net.sln @@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Geekbot.net", "Geekbot.net/ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{4CAF5F02-EFFE-4FDA-BD44-EEADDBA9600E}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WikipediaApi", "WikipediaApi\WikipediaApi.csproj", "{1084D499-EF94-4834-9E6A-B2AD81B60078}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,6 +23,10 @@ Global {4CAF5F02-EFFE-4FDA-BD44-EEADDBA9600E}.Debug|Any CPU.Build.0 = Debug|Any CPU {4CAF5F02-EFFE-4FDA-BD44-EEADDBA9600E}.Release|Any CPU.ActiveCfg = Release|Any CPU {4CAF5F02-EFFE-4FDA-BD44-EEADDBA9600E}.Release|Any CPU.Build.0 = Release|Any CPU + {1084D499-EF94-4834-9E6A-B2AD81B60078}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1084D499-EF94-4834-9E6A-B2AD81B60078}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1084D499-EF94-4834-9E6A-B2AD81B60078}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1084D499-EF94-4834-9E6A-B2AD81B60078}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Geekbot.net/Commands/Wikipedia.cs b/Geekbot.net/Commands/Wikipedia.cs new file mode 100644 index 0000000..d0ea945 --- /dev/null +++ b/Geekbot.net/Commands/Wikipedia.cs @@ -0,0 +1,94 @@ +using System; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using Discord; +using Discord.Commands; +using Discord.Net; +using Geekbot.net.Lib; +using HtmlAgilityPack; +using WikipediaApi; +using WikipediaApi.Page; + +namespace Geekbot.net.Commands +{ + public class Wikipedia : ModuleBase + { + private readonly IErrorHandler _errorHandler; + private readonly IWikipediaClient _wikipediaClient; + + public Wikipedia(IErrorHandler errorHandler, IWikipediaClient wikipediaClient) + { + _errorHandler = errorHandler; + _wikipediaClient = wikipediaClient; + } + + [Command("wiki", RunMode = RunMode.Async)] + [Remarks(CommandCategories.Helpers)] + [Summary("Get an article from wikipedia.")] + public async Task GetPreview([Remainder] [Summary("Article")] string articleName) + { + try + { + var article = await _wikipediaClient.GetPreview(articleName.Replace(" ", "_")); + + if (article.Type != PageTypes.Standard) + { + switch (article.Type) + { + case PageTypes.Disambiguation: + await ReplyAsync($"**__Disambiguation__**\r\n{DisambiguationExtractor(article.ExtractHtml)}"); + break; + case PageTypes.MainPage: + await ReplyAsync("The main page is not supported"); + break; + case PageTypes.NoExtract: + await ReplyAsync($"This page has no summary, here is the link: {article.ContentUrls.Desktop.Page}"); + break; + case PageTypes.Standard: + break; + default: + await ReplyAsync($"This page type is currently not supported, here is the link: {article.ContentUrls.Desktop.Page}"); + break; + } + return; + } + + var eb = new EmbedBuilder + { + Title = article.Title, + Description = article.Extract, + ImageUrl = article.Thumbnail.Source.ToString(), + Url = article.ContentUrls.Desktop.Page.ToString() + }; + await ReplyAsync("", false, eb.Build()); + } + catch (HttpRequestException e) + { + await ReplyAsync("I couldn't find that article"); + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + } + } + + private string DisambiguationExtractor(string extractHtml) + { + var doc = new HtmlDocument(); + doc.LoadHtml(extractHtml); + var nodes = doc.DocumentNode.SelectNodes("//li"); + var sb = new StringBuilder(); + foreach (var node in nodes) + { + var split = node.InnerText.Split(','); + var title = split.First(); + var desc = string.Join(",", split.Skip(1)); + sb.AppendLine($"**{title}** - {desc}"); + } + + return sb.ToString(); + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 1a11cd9..6fb13f9 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -15,6 +15,7 @@ 1.0.2 + @@ -22,7 +23,7 @@ - + @@ -76,4 +77,7 @@ PreserveNewest + + + \ No newline at end of file diff --git a/Geekbot.net/Lib/ErrorHandler.cs b/Geekbot.net/Lib/ErrorHandler.cs index 3410e91..c0c3d95 100644 --- a/Geekbot.net/Lib/ErrorHandler.cs +++ b/Geekbot.net/Lib/ErrorHandler.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Diagnostics; using System.Net; using Discord.Commands; using Discord.Net; @@ -12,12 +14,14 @@ namespace Geekbot.net.Lib private readonly IGeekbotLogger _logger; private readonly ITranslationHandler _translation; private readonly IRavenClient _raven; + private readonly bool _errorsInChat; - public ErrorHandler(IGeekbotLogger logger, ITranslationHandler translation) + public ErrorHandler(IGeekbotLogger logger, ITranslationHandler translation, bool errorsInChat) { _logger = logger; _translation = translation; - + _errorsInChat = errorsInChat; + var sentryDsn = Environment.GetEnvironmentVariable("SENTRY"); if (!string.IsNullOrEmpty(sentryDsn)) { @@ -41,7 +45,15 @@ namespace Geekbot.net.Lib _logger.Error("Geekbot", "An error ocured", e, errorObj); if (!string.IsNullOrEmpty(errorMessage)) { - Context.Channel.SendMessageAsync(errorString); + if (_errorsInChat) + { + Context.Channel.SendMessageAsync($"{e.Message}\r\n```\r\n{e.InnerException}\r\n```"); + } + else + { + Context.Channel.SendMessageAsync(errorString); + } + } if (_raven == null) return; diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 3f31945..b805e5f 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -1,10 +1,7 @@ using System; -using System.Collections.Generic; -using System.IO; using System.Linq; using System.Net; using System.Reflection; -using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using Discord; @@ -14,8 +11,8 @@ using Geekbot.net.Lib; using Geekbot.net.Lib.Media; using Microsoft.Extensions.DependencyInjection; using Nancy.Hosting.Self; -using Serilog; using StackExchange.Redis; +using WikipediaApi; namespace Geekbot.net { @@ -101,6 +98,7 @@ namespace Geekbot.net var emojiConverter = new EmojiConverter(); var audioUtils = new AudioUtils(); var mtgManaConverter = new MtgManaConverter(); + var wikipediaClient = new WikipediaClient(); services.AddSingleton(redis); services.AddSingleton(logger); @@ -112,6 +110,7 @@ namespace Geekbot.net services.AddSingleton(mediaProvider); services.AddSingleton(malClient); services.AddSingleton(mtgManaConverter); + services.AddSingleton(wikipediaClient); logger.Information("Geekbot", "Connecting to Discord"); @@ -134,7 +133,7 @@ namespace Geekbot.net logger.Information("Geekbot", "Registering Stuff"); var translationHandler = new TranslationHandler(client.Guilds, redis, logger); - var errorHandler = new ErrorHandler(logger, translationHandler); + var errorHandler = new ErrorHandler(logger, translationHandler, args.Contains("--expose-errors")); var reactionListener = new ReactionListener(redis); await commands.AddModulesAsync(Assembly.GetEntryAssembly()); services.AddSingleton(commands); diff --git a/WikipediaApi/IWikipediaClient.cs b/WikipediaApi/IWikipediaClient.cs new file mode 100644 index 0000000..3fad554 --- /dev/null +++ b/WikipediaApi/IWikipediaClient.cs @@ -0,0 +1,10 @@ +using System.Threading.Tasks; +using WikipediaApi.Page; + +namespace WikipediaApi +{ + public interface IWikipediaClient + { + Task GetPreview(string pageName); + } +} \ No newline at end of file diff --git a/WikipediaApi/Page/PageApiUrls.cs b/WikipediaApi/Page/PageApiUrls.cs new file mode 100644 index 0000000..8a121be --- /dev/null +++ b/WikipediaApi/Page/PageApiUrls.cs @@ -0,0 +1,14 @@ +using System; + +namespace WikipediaApi.Page +{ + public class PageApiUrls + { + public Uri Summary { get; set; } + public Uri Metadata { get; set; } + public Uri References { get; set; } + public Uri Media { get; set; } + public Uri EditHtml { get; set; } + public Uri TalkPageHtml { get; set; } + } +} \ No newline at end of file diff --git a/WikipediaApi/Page/PageContentUrlCollection.cs b/WikipediaApi/Page/PageContentUrlCollection.cs new file mode 100644 index 0000000..f6c680b --- /dev/null +++ b/WikipediaApi/Page/PageContentUrlCollection.cs @@ -0,0 +1,8 @@ +namespace WikipediaApi.Page +{ + public class PageContentUrlCollection + { + public PageContentUrls Desktop { get; set; } + public PageContentUrls Mobile { get; set; } + } +} \ No newline at end of file diff --git a/WikipediaApi/Page/PageContentUrls.cs b/WikipediaApi/Page/PageContentUrls.cs new file mode 100644 index 0000000..64a80dc --- /dev/null +++ b/WikipediaApi/Page/PageContentUrls.cs @@ -0,0 +1,12 @@ +using System; + +namespace WikipediaApi.Page +{ + public class PageContentUrls + { + public Uri Page { get; set; } + public Uri Revisions { get; set; } + public Uri Edit { get; set; } + public Uri Talk { get; set; } + } +} \ No newline at end of file diff --git a/WikipediaApi/Page/PageCoordinates.cs b/WikipediaApi/Page/PageCoordinates.cs new file mode 100644 index 0000000..f86bb09 --- /dev/null +++ b/WikipediaApi/Page/PageCoordinates.cs @@ -0,0 +1,8 @@ +namespace WikipediaApi.Page +{ + public class PageCoordinates + { + public float lat { get; set; } + public float lon { get; set; } + } +} \ No newline at end of file diff --git a/WikipediaApi/Page/PageImage.cs b/WikipediaApi/Page/PageImage.cs new file mode 100644 index 0000000..4a8b28d --- /dev/null +++ b/WikipediaApi/Page/PageImage.cs @@ -0,0 +1,12 @@ +using System; + +namespace WikipediaApi.Page +{ + public class PageImage + { + public Uri Source { get; set; } + public int Width { get; set; } + public int Height { get; set; } + + } +} \ No newline at end of file diff --git a/WikipediaApi/Page/PageNamespace.cs b/WikipediaApi/Page/PageNamespace.cs new file mode 100644 index 0000000..66600b6 --- /dev/null +++ b/WikipediaApi/Page/PageNamespace.cs @@ -0,0 +1,8 @@ +namespace WikipediaApi.Page +{ + public class PageNamespace + { + public ulong Id { get; set; } + public string Text { get; set; } + } +} \ No newline at end of file diff --git a/WikipediaApi/Page/PagePreview.cs b/WikipediaApi/Page/PagePreview.cs new file mode 100644 index 0000000..41ab6f3 --- /dev/null +++ b/WikipediaApi/Page/PagePreview.cs @@ -0,0 +1,68 @@ +using System; +using System.Runtime.Serialization; +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; + +namespace WikipediaApi.Page +{ + public class PagePreview + { + [JsonProperty("type")] + [JsonConverter(typeof(StringEnumConverter))] + public PageTypes Type { get; set; } = PageTypes.NoExtract; + + [JsonProperty("title")] + public string Title { get; set; } + + [JsonProperty("displaytitle")] + public string Displaytitle { get; set; } + + [JsonProperty("namespace")] + public PageNamespace @Namespace { get; set; } + + [JsonProperty("titles")] + public PageTitles Titles { get; set; } + + [JsonProperty("pageid")] + public ulong Pageid { get; set; } + + [JsonProperty("thumbnail")] + public PageImage Thumbnail { get; set; } + + [JsonProperty("originalimage")] + public PageImage Originalimage { get; set; } + + [JsonProperty("lang")] + public string Lang { get; set; } + + [JsonProperty("dir")] + public string Dir { get; set; } + + [JsonProperty("revision")] + public ulong Revision { get; set; } + + [JsonProperty("tid")] + public string Tid { get; set; } + + [JsonProperty("timestamp")] + public DateTimeOffset Timestamp { get; set; } + + [JsonProperty("description")] + public string Description { get; set; } + + [JsonProperty("coordinates")] + public PageCoordinates Coordinates { get; set; } + + [JsonProperty("content_urls")] + public PageContentUrlCollection ContentUrls { get; set; } + + [JsonProperty("api_urls")] + public PageApiUrls ApiUrls { get; set; } + + [JsonProperty("extract")] + public string Extract { get; set; } + + [JsonProperty("extract_html")] + public string ExtractHtml { get; set; } + } +} \ No newline at end of file diff --git a/WikipediaApi/Page/PageTitles.cs b/WikipediaApi/Page/PageTitles.cs new file mode 100644 index 0000000..31a55b9 --- /dev/null +++ b/WikipediaApi/Page/PageTitles.cs @@ -0,0 +1,10 @@ +namespace WikipediaApi.Page +{ + public class PageTitles + { + public string Canonical { get; set; } + public string Normalized { get; set; } + public string Display { get; set; } + + } +} \ No newline at end of file diff --git a/WikipediaApi/Page/PageTypes.cs b/WikipediaApi/Page/PageTypes.cs new file mode 100644 index 0000000..a415d75 --- /dev/null +++ b/WikipediaApi/Page/PageTypes.cs @@ -0,0 +1,19 @@ +using System.Runtime.Serialization; + +namespace WikipediaApi.Page +{ + public enum PageTypes + { + [EnumMember(Value = "standard")] + Standard, + + [EnumMember(Value = "disambiguation")] + Disambiguation, + + [EnumMember(Value = "mainpage")] + MainPage, + + [EnumMember(Value = "no-extract")] + NoExtract + } +} \ No newline at end of file diff --git a/WikipediaApi/WikipediaApi.csproj b/WikipediaApi/WikipediaApi.csproj new file mode 100644 index 0000000..f182d90 --- /dev/null +++ b/WikipediaApi/WikipediaApi.csproj @@ -0,0 +1,8 @@ + + + netcoreapp2.0 + + + + + \ No newline at end of file diff --git a/WikipediaApi/WikipediaClient.cs b/WikipediaApi/WikipediaClient.cs new file mode 100644 index 0000000..9bd3059 --- /dev/null +++ b/WikipediaApi/WikipediaClient.cs @@ -0,0 +1,29 @@ +using System; +using System.Net.Http; +using System.Threading.Tasks; +using Newtonsoft.Json; +using WikipediaApi.Page; + +namespace WikipediaApi +{ + public class WikipediaClient : IWikipediaClient + { + private readonly HttpClient _httpClient; + public WikipediaClient() + { + _httpClient = new HttpClient + { + BaseAddress = new Uri("https://en.wikipedia.org") + }; + } + + public async Task GetPreview(string pageName) + { + var response = await _httpClient.GetAsync($"/api/rest_v1/page/summary/{pageName}"); + response.EnsureSuccessStatusCode(); + + var stringResponse = await response.Content.ReadAsStringAsync(); + return JsonConvert.DeserializeObject(stringResponse); + } + } +} \ No newline at end of file From 4548c6083aa5c6cecf0ef511e58c6e0ee35588f6 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 28 Apr 2018 01:28:48 +0200 Subject: [PATCH 027/443] Dependency updates and mtg color fixes --- Geekbot.net/Commands/MagicTheGathering.cs | 12 ++++++------ Geekbot.net/Commands/Overwatch.cs | 2 +- Geekbot.net/Geekbot.net.csproj | 16 ++++++++-------- WikipediaApi/WikipediaClient.cs | 4 ++-- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/Geekbot.net/Commands/MagicTheGathering.cs b/Geekbot.net/Commands/MagicTheGathering.cs index e3cc864..044c287 100644 --- a/Geekbot.net/Commands/MagicTheGathering.cs +++ b/Geekbot.net/Commands/MagicTheGathering.cs @@ -74,17 +74,17 @@ namespace Geekbot.net.Commands switch (color) { case "Black": - return new Color(177, 171, 170); + return new Color(203, 194, 191); case "White": - return new Color(255, 252, 214); + return new Color(255, 251, 213); case "Blue": - return new Color(156, 189, 204); + return new Color(170, 224, 250); case "Red": - return new Color(204, 156, 140); + return new Color(250, 170, 143); case "Green": - return new Color(147, 181, 159); + return new Color(155, 211, 174); default: - return new Color(255, 252, 214); + return new Color(204, 194, 212); } } } diff --git a/Geekbot.net/Commands/Overwatch.cs b/Geekbot.net/Commands/Overwatch.cs index 10dc459..fbb9699 100644 --- a/Geekbot.net/Commands/Overwatch.cs +++ b/Geekbot.net/Commands/Overwatch.cs @@ -110,7 +110,7 @@ namespace Geekbot.net.Commands private async Task createProfile(string battletag) { - var owConfig = new OverwatchConfig.Builder().WithRegions(Region.Eu).WithPlatforms(Platform.Pc); + var owConfig = new OverwatchConfig.Builder().WithPlatforms(Platform.Pc); using (var owClient = new OverwatchClient(owConfig)) { var player = await owClient.GetPlayerAsync(battletag); diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 6fb13f9..c91512f 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -14,24 +14,24 @@ 1.0.2 - + - - - - + + + + - + - + - + 1.2.6 diff --git a/WikipediaApi/WikipediaClient.cs b/WikipediaApi/WikipediaClient.cs index 9bd3059..25bf299 100644 --- a/WikipediaApi/WikipediaClient.cs +++ b/WikipediaApi/WikipediaClient.cs @@ -9,11 +9,11 @@ namespace WikipediaApi public class WikipediaClient : IWikipediaClient { private readonly HttpClient _httpClient; - public WikipediaClient() + public WikipediaClient(string language = "en") { _httpClient = new HttpClient { - BaseAddress = new Uri("https://en.wikipedia.org") + BaseAddress = new Uri($"https://{language}.wikipedia.org") }; } From f5fd9ba017d3493730d72e815b58278457399c9a Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 28 Apr 2018 02:46:30 +0200 Subject: [PATCH 028/443] Bug fixes in !wiki and errorHandler --- Geekbot.net/Commands/Help.cs | 2 +- Geekbot.net/Commands/Wikipedia.cs | 5 +++-- Geekbot.net/Lib/ErrorHandler.cs | 5 ++++- WikipediaApi/Page/PagePreview.cs | 2 +- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Geekbot.net/Commands/Help.cs b/Geekbot.net/Commands/Help.cs index 83374a8..1b3e285 100644 --- a/Geekbot.net/Commands/Help.cs +++ b/Geekbot.net/Commands/Help.cs @@ -29,7 +29,7 @@ namespace Geekbot.net.Commands sb.AppendLine("https://geekbot.pizzaandcoffee.rocks/commands"); var dm = await Context.User.GetOrCreateDMChannelAsync(); await dm.SendMessageAsync(sb.ToString()); - Context.Message.AddReactionAsync(new Emoji("✅")); + await Context.Message.AddReactionAsync(new Emoji("✅")); } catch (Exception e) { diff --git a/Geekbot.net/Commands/Wikipedia.cs b/Geekbot.net/Commands/Wikipedia.cs index d0ea945..e7b36cf 100644 --- a/Geekbot.net/Commands/Wikipedia.cs +++ b/Geekbot.net/Commands/Wikipedia.cs @@ -59,12 +59,12 @@ namespace Geekbot.net.Commands { Title = article.Title, Description = article.Extract, - ImageUrl = article.Thumbnail.Source.ToString(), + ImageUrl = article.Thumbnail?.Source.ToString(), Url = article.ContentUrls.Desktop.Page.ToString() }; await ReplyAsync("", false, eb.Build()); } - catch (HttpRequestException e) + catch (HttpRequestException) { await ReplyAsync("I couldn't find that article"); } @@ -79,6 +79,7 @@ namespace Geekbot.net.Commands var doc = new HtmlDocument(); doc.LoadHtml(extractHtml); var nodes = doc.DocumentNode.SelectNodes("//li"); + if (nodes == null) return "(List is to long to show)"; var sb = new StringBuilder(); foreach (var node in nodes) { diff --git a/Geekbot.net/Lib/ErrorHandler.cs b/Geekbot.net/Lib/ErrorHandler.cs index c0c3d95..dc67db2 100644 --- a/Geekbot.net/Lib/ErrorHandler.cs +++ b/Geekbot.net/Lib/ErrorHandler.cs @@ -47,7 +47,9 @@ namespace Geekbot.net.Lib { if (_errorsInChat) { - Context.Channel.SendMessageAsync($"{e.Message}\r\n```\r\n{e.InnerException}\r\n```"); + var resStackTrace = string.IsNullOrEmpty(e.InnerException?.ToString()) ? e.StackTrace : e.InnerException.ToString(); + var maxLen = Math.Min(resStackTrace.Length, 1850); + Context.Channel.SendMessageAsync($"{e.Message}\r\n```\r\n{resStackTrace?.Substring(0, maxLen)}\r\n```"); } else { @@ -72,6 +74,7 @@ namespace Geekbot.net.Lib } catch (Exception ex) { + Context.Channel.SendMessageAsync("Something went really really wrong here"); _logger.Error("Geekbot", "Errorception", ex); } } diff --git a/WikipediaApi/Page/PagePreview.cs b/WikipediaApi/Page/PagePreview.cs index 41ab6f3..c1de2d7 100644 --- a/WikipediaApi/Page/PagePreview.cs +++ b/WikipediaApi/Page/PagePreview.cs @@ -9,7 +9,7 @@ namespace WikipediaApi.Page { [JsonProperty("type")] [JsonConverter(typeof(StringEnumConverter))] - public PageTypes Type { get; set; } = PageTypes.NoExtract; + public PageTypes Type { get; set; } [JsonProperty("title")] public string Title { get; set; } From b97fca787ce6262f1a084135757066e12968da2c Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 28 Apr 2018 17:38:45 +0200 Subject: [PATCH 029/443] Ability to change wikipedia instance and make !wiki nicer --- Geekbot.net/Commands/Admin.cs | 18 ++++++++++++++++++ Geekbot.net/Commands/Wikipedia.cs | 31 ++++++++++++++++++++++++------- WikipediaApi/IWikipediaClient.cs | 2 +- WikipediaApi/WikipediaClient.cs | 11 ++++------- 4 files changed, 47 insertions(+), 15 deletions(-) diff --git a/Geekbot.net/Commands/Admin.cs b/Geekbot.net/Commands/Admin.cs index 681ff99..4e94673 100644 --- a/Geekbot.net/Commands/Admin.cs +++ b/Geekbot.net/Commands/Admin.cs @@ -138,6 +138,24 @@ namespace Geekbot.net.Commands _errorHandler.HandleCommandException(e, Context); } } + + [Command("wiki", RunMode = RunMode.Async)] + [Remarks(CommandCategories.Admin)] + [Summary("Change the wikipedia instance (use lang code in xx.wikipedia.org)")] + public async Task setWikiLanguage([Summary("language")] string languageRaw) + { + try + { + var language = languageRaw.ToLower(); + _redis.HashSet($"{Context.Guild.Id}:Settings", new[] {new HashEntry("WikiLang", language) }); + + await ReplyAsync($"Now using the {language} wikipedia"); + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + } + } [Command("lang", RunMode = RunMode.Async)] [Remarks(CommandCategories.Admin)] diff --git a/Geekbot.net/Commands/Wikipedia.cs b/Geekbot.net/Commands/Wikipedia.cs index e7b36cf..77fb41d 100644 --- a/Geekbot.net/Commands/Wikipedia.cs +++ b/Geekbot.net/Commands/Wikipedia.cs @@ -5,9 +5,9 @@ using System.Text; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Discord.Net; using Geekbot.net.Lib; using HtmlAgilityPack; +using StackExchange.Redis; using WikipediaApi; using WikipediaApi.Page; @@ -17,11 +17,13 @@ namespace Geekbot.net.Commands { private readonly IErrorHandler _errorHandler; private readonly IWikipediaClient _wikipediaClient; - - public Wikipedia(IErrorHandler errorHandler, IWikipediaClient wikipediaClient) + private readonly IDatabase _redis; + + public Wikipedia(IErrorHandler errorHandler, IWikipediaClient wikipediaClient, IDatabase redis) { _errorHandler = errorHandler; _wikipediaClient = wikipediaClient; + _redis = redis; } [Command("wiki", RunMode = RunMode.Async)] @@ -31,7 +33,12 @@ namespace Geekbot.net.Commands { try { - var article = await _wikipediaClient.GetPreview(articleName.Replace(" ", "_")); + var wikiLang = _redis.HashGet($"{Context.Guild.Id}:Settings", "WikiLang").ToString(); + if (string.IsNullOrEmpty(wikiLang)) + { + wikiLang = "en"; + } + var article = await _wikipediaClient.GetPreview(articleName.Replace(" ", "_"), wikiLang); if (article.Type != PageTypes.Standard) { @@ -58,10 +65,20 @@ namespace Geekbot.net.Commands var eb = new EmbedBuilder { Title = article.Title, - Description = article.Extract, + Description = article.Description, ImageUrl = article.Thumbnail?.Source.ToString(), - Url = article.ContentUrls.Desktop.Page.ToString() + Url = article.ContentUrls.Desktop.Page.ToString(), + Color = new Color(246,246,246), + Timestamp = article.Timestamp, + Footer = new EmbedFooterBuilder + { + Text = "Last Edit", + IconUrl = "http://icons.iconarchive.com/icons/sykonist/popular-sites/256/Wikipedia-icon.png" + } }; + + eb.AddField("Description", article.Extract); + if (article.Coordinates != null) eb.AddField("Coordinates", $"{article.Coordinates.lat} Lat {article.Coordinates.lon} Lon"); await ReplyAsync("", false, eb.Build()); } catch (HttpRequestException) @@ -86,7 +103,7 @@ namespace Geekbot.net.Commands var split = node.InnerText.Split(','); var title = split.First(); var desc = string.Join(",", split.Skip(1)); - sb.AppendLine($"**{title}** - {desc}"); + sb.AppendLine($"• **{title}** -{desc}"); } return sb.ToString(); diff --git a/WikipediaApi/IWikipediaClient.cs b/WikipediaApi/IWikipediaClient.cs index 3fad554..4d1dae9 100644 --- a/WikipediaApi/IWikipediaClient.cs +++ b/WikipediaApi/IWikipediaClient.cs @@ -5,6 +5,6 @@ namespace WikipediaApi { public interface IWikipediaClient { - Task GetPreview(string pageName); + Task GetPreview(string pageName, string language = "en"); } } \ No newline at end of file diff --git a/WikipediaApi/WikipediaClient.cs b/WikipediaApi/WikipediaClient.cs index 25bf299..161cbce 100644 --- a/WikipediaApi/WikipediaClient.cs +++ b/WikipediaApi/WikipediaClient.cs @@ -9,17 +9,14 @@ namespace WikipediaApi public class WikipediaClient : IWikipediaClient { private readonly HttpClient _httpClient; - public WikipediaClient(string language = "en") + public WikipediaClient() { - _httpClient = new HttpClient - { - BaseAddress = new Uri($"https://{language}.wikipedia.org") - }; + _httpClient = new HttpClient(); } - public async Task GetPreview(string pageName) + public async Task GetPreview(string pageName, string language = "en") { - var response = await _httpClient.GetAsync($"/api/rest_v1/page/summary/{pageName}"); + var response = await _httpClient.GetAsync($"https://{language}.wikipedia.org/api/rest_v1/page/summary/{pageName}"); response.EnsureSuccessStatusCode(); var stringResponse = await response.Content.ReadAsStringAsync(); From b81284bfe7adda7a9df2877392ee164740ec3faa Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 28 Apr 2018 19:27:41 +0200 Subject: [PATCH 030/443] Add !chuck and !dad --- Geekbot.net/Commands/ChuckNorrisJokes.cs | 61 ++++++++++++++++++++++++ Geekbot.net/Commands/DadJokes.cs | 60 +++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 Geekbot.net/Commands/ChuckNorrisJokes.cs create mode 100644 Geekbot.net/Commands/DadJokes.cs diff --git a/Geekbot.net/Commands/ChuckNorrisJokes.cs b/Geekbot.net/Commands/ChuckNorrisJokes.cs new file mode 100644 index 0000000..1ac6c4d --- /dev/null +++ b/Geekbot.net/Commands/ChuckNorrisJokes.cs @@ -0,0 +1,61 @@ +using System; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Threading.Tasks; +using Discord.Commands; +using Geekbot.net.Lib; +using Newtonsoft.Json; + +namespace Geekbot.net.Commands +{ + public class ChuckNorrisJokes : ModuleBase + { + private readonly IErrorHandler _errorHandler; + + public ChuckNorrisJokes(IErrorHandler errorHandler) + { + _errorHandler = errorHandler; + } + + [Command("chuck", RunMode = RunMode.Async)] + [Remarks(CommandCategories.Randomness)] + [Summary("A random chuck norris joke")] + public async Task Say() + { + try + { + using (var client = new HttpClient()) + { + try + { + client.DefaultRequestHeaders.Accept.Clear(); + client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json")); + var response = await client.GetAsync("https://api.chucknorris.io/jokes/random"); + response.EnsureSuccessStatusCode(); + + var stringResponse = await response.Content.ReadAsStringAsync(); + var data = JsonConvert.DeserializeObject(stringResponse); + await ReplyAsync(data.value); + } + catch (HttpRequestException) + { + await ReplyAsync("Api down..."); + } + } + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + } + } + + private class ChuckNorrisJokeResponse + { + public string category { get; set; } + public string icon_url { get; set; } + public string id { get; set; } + public string url { get; set; } + public string value { get; set; } + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/DadJokes.cs b/Geekbot.net/Commands/DadJokes.cs new file mode 100644 index 0000000..615f3d5 --- /dev/null +++ b/Geekbot.net/Commands/DadJokes.cs @@ -0,0 +1,60 @@ +using System; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Threading.Tasks; +using Discord; +using Discord.Commands; +using Geekbot.net.Lib; +using Newtonsoft.Json; + +namespace Geekbot.net.Commands +{ + public class DadJokes : ModuleBase + { + private readonly IErrorHandler _errorHandler; + + public DadJokes(IErrorHandler errorHandler) + { + _errorHandler = errorHandler; + } + + [Command("dad", RunMode = RunMode.Async)] + [Remarks(CommandCategories.Randomness)] + [Summary("A random dad joke")] + public async Task Say() + { + try + { + using (var client = new HttpClient()) + { + try + { + client.DefaultRequestHeaders.Accept.Clear(); + client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json")); + var response = await client.GetAsync("https://icanhazdadjoke.com/"); + response.EnsureSuccessStatusCode(); + + var stringResponse = await response.Content.ReadAsStringAsync(); + var data = JsonConvert.DeserializeObject(stringResponse); + await ReplyAsync(data.joke); + } + catch (HttpRequestException) + { + await ReplyAsync("Api down..."); + } + } + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + } + } + + private class DadJokeResponse + { + public string id { get; set; } + public string joke { get; set; } + public string status { get; set; } + } + } +} \ No newline at end of file From cdb104cacc7440f9d7b6a414056efd1a7e980916 Mon Sep 17 00:00:00 2001 From: runebaas Date: Mon, 30 Apr 2018 23:44:19 +0200 Subject: [PATCH 031/443] Resharper suggestions --- Geekbot.net/Commands/Admin.cs | 16 +- Geekbot.net/Commands/AvatarGetter.cs | 2 +- Geekbot.net/Commands/BattleTag.cs | 11 +- Geekbot.net/Commands/Cat.cs | 4 +- Geekbot.net/Commands/Changelog.cs | 22 ++- Geekbot.net/Commands/CheckEm.cs | 2 +- Geekbot.net/Commands/ChuckNorrisJokes.cs | 8 +- Geekbot.net/Commands/DadJokes.cs | 7 +- Geekbot.net/Commands/Dice.cs | 38 ++--- Geekbot.net/Commands/Dog.cs | 4 +- Geekbot.net/Commands/Emojify.cs | 2 +- Geekbot.net/Commands/Gdq.cs | 4 +- Geekbot.net/Commands/Google.cs | 56 ++++--- Geekbot.net/Commands/GuildInfo.cs | 2 +- Geekbot.net/Commands/Karma.cs | 4 +- Geekbot.net/Commands/MagicTheGathering.cs | 3 +- Geekbot.net/Commands/Mod.cs | 4 +- Geekbot.net/Commands/Overwatch.cs | 23 ++- Geekbot.net/Commands/Owner.cs | 6 +- Geekbot.net/Commands/Pokedex.cs | 18 +-- Geekbot.net/Commands/Poll.cs | 8 +- Geekbot.net/Commands/Quote.cs | 68 ++++----- Geekbot.net/Commands/RandomAnimals.cs | 30 ++-- Geekbot.net/Commands/Rank.cs | 3 +- Geekbot.net/Commands/Role.cs | 10 +- Geekbot.net/Commands/Ship.cs | 4 +- Geekbot.net/Commands/Slap.cs | 2 +- Geekbot.net/Commands/UrbanDictionary.cs | 40 +++-- Geekbot.net/Commands/Wikipedia.cs | 2 +- Geekbot.net/Commands/mal.cs | 16 +- Geekbot.net/Handlers.cs | 2 - Geekbot.net/Lib/AudioClientCache.cs | 106 ------------- Geekbot.net/Lib/CommandCategories.cs | 2 +- Geekbot.net/Lib/Constants.cs | 2 +- Geekbot.net/Lib/EmojiConverter.cs | 12 +- Geekbot.net/Lib/ErrorHandler.cs | 24 ++- Geekbot.net/Lib/GeekbotLogger.cs | 4 +- Geekbot.net/Lib/LevelCalc.cs | 1 - Geekbot.net/Lib/LoggerFactory.cs | 3 +- Geekbot.net/Lib/MalClient.cs | 18 +-- Geekbot.net/Lib/Media/FortunesProvider.cs | 11 +- Geekbot.net/Lib/Media/MediaProvider.cs | 35 ++--- Geekbot.net/Lib/MtgManaConverter.cs | 1 - Geekbot.net/Lib/ReactionListener.cs | 2 +- Geekbot.net/Lib/SimpleConextConverter.cs | 25 ++-- Geekbot.net/Lib/TranslationHandler.cs | 6 +- Geekbot.net/Lib/UserRepository.cs | 21 ++- Geekbot.net/Program.cs | 174 +++++++++++----------- Geekbot.net/WebApi/HelpController.cs | 34 ++--- Geekbot.net/WebApi/StatusController.cs | 6 +- Tests/Lib/EmojiConverter.test.cs | 11 +- Tests/Lib/LevelCalc.test.cs | 3 +- WikipediaApi/Page/PageCoordinates.cs | 4 +- WikipediaApi/Page/PagePreview.cs | 3 +- WikipediaApi/WikipediaClient.cs | 3 +- 55 files changed, 382 insertions(+), 550 deletions(-) delete mode 100644 Geekbot.net/Lib/AudioClientCache.cs diff --git a/Geekbot.net/Commands/Admin.cs b/Geekbot.net/Commands/Admin.cs index 4e94673..481eab1 100644 --- a/Geekbot.net/Commands/Admin.cs +++ b/Geekbot.net/Commands/Admin.cs @@ -40,7 +40,7 @@ namespace Geekbot.net.Commands [Command("modchannel", RunMode = RunMode.Async)] [Remarks(CommandCategories.Admin)] [Summary("Set a channel for moderation purposes")] - public async Task selectModChannel([Summary("#Channel")] ISocketMessageChannel channel) + public async Task SelectModChannel([Summary("#Channel")] ISocketMessageChannel channel) { try { @@ -61,7 +61,7 @@ namespace Geekbot.net.Commands [Command("showleave", RunMode = RunMode.Async)] [Remarks(CommandCategories.Admin)] [Summary("Notify modchannel when someone leaves")] - public async Task showLeave([Summary("true/false")] bool enabled) + public async Task ShowLeave([Summary("true/false")] bool enabled) { var modChannelId = ulong.Parse(_redis.HashGet($"{Context.Guild.Id}:Settings", "ModChannel")); try @@ -88,7 +88,7 @@ namespace Geekbot.net.Commands [Command("showdel", RunMode = RunMode.Async)] [Remarks(CommandCategories.Admin)] [Summary("Notify modchannel when someone deletes a message")] - public async Task showDelete([Summary("true/false")] bool enabled) + public async Task ShowDelete([Summary("true/false")] bool enabled) { var modChannelId = ulong.Parse(_redis.HashGet($"{Context.Guild.Id}:Settings", "ModChannel")); try @@ -117,7 +117,7 @@ namespace Geekbot.net.Commands [Command("setlang", RunMode = RunMode.Async)] [Remarks(CommandCategories.Admin)] [Summary("Change the bots language")] - public async Task setLanguage([Summary("language")] string languageRaw) + public async Task SetLanguage([Summary("language")] string languageRaw) { try { @@ -142,7 +142,7 @@ namespace Geekbot.net.Commands [Command("wiki", RunMode = RunMode.Async)] [Remarks(CommandCategories.Admin)] [Summary("Change the wikipedia instance (use lang code in xx.wikipedia.org)")] - public async Task setWikiLanguage([Summary("language")] string languageRaw) + public async Task SetWikiLanguage([Summary("language")] string languageRaw) { try { @@ -160,7 +160,7 @@ namespace Geekbot.net.Commands [Command("lang", RunMode = RunMode.Async)] [Remarks(CommandCategories.Admin)] [Summary("Change the bots language")] - public async Task getLanguage() + public async Task GetLanguage() { try { @@ -176,12 +176,12 @@ namespace Geekbot.net.Commands [Command("ping", RunMode = RunMode.Async)] [Remarks(CommandCategories.Admin)] [Summary("Enable the ping reply.")] - public async Task togglePing() + public async Task TogglePing() { try { bool.TryParse(_redis.HashGet($"{Context.Guild.Id}:Settings", "ping"), out var current); - _redis.HashSet($"{Context.Guild.Id}:Settings", new[] {new HashEntry("ping", current ? "false" : "true"), }); + _redis.HashSet($"{Context.Guild.Id}:Settings", new[] {new HashEntry("ping", current ? "false" : "true") }); await ReplyAsync(!current ? "i will reply to ping now" : "No more pongs..."); } catch (Exception e) diff --git a/Geekbot.net/Commands/AvatarGetter.cs b/Geekbot.net/Commands/AvatarGetter.cs index 9e31b6b..d8a04e9 100644 --- a/Geekbot.net/Commands/AvatarGetter.cs +++ b/Geekbot.net/Commands/AvatarGetter.cs @@ -18,7 +18,7 @@ namespace Geekbot.net.Commands [Command("avatar", RunMode = RunMode.Async)] [Remarks(CommandCategories.Helpers)] [Summary("Get someones avatar")] - public async Task getAvatar([Remainder] [Summary("user")] IUser user = null) + public async Task GetAvatar([Remainder] [Summary("user")] IUser user = null) { try { diff --git a/Geekbot.net/Commands/BattleTag.cs b/Geekbot.net/Commands/BattleTag.cs index cbc5ecc..eb6fca1 100644 --- a/Geekbot.net/Commands/BattleTag.cs +++ b/Geekbot.net/Commands/BattleTag.cs @@ -24,7 +24,7 @@ namespace Geekbot.net.Commands { try { - var tag = _userRepository.getUserSetting(Context.User.Id, "BattleTag"); + var tag = _userRepository.GetUserSetting(Context.User.Id, "BattleTag"); if (!string.IsNullOrEmpty(tag)) await ReplyAsync($"Your BattleTag is {tag}"); else @@ -43,9 +43,9 @@ namespace Geekbot.net.Commands { try { - if (isValidTag(tag)) + if (IsValidTag(tag)) { - _userRepository.saveUserSetting(Context.User.Id, "BattleTag", tag); + _userRepository.SaveUserSetting(Context.User.Id, "BattleTag", tag); await ReplyAsync("Saved!"); } else @@ -59,13 +59,12 @@ namespace Geekbot.net.Commands } } - public static bool isValidTag(string tag) + 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; - if (splited[1].Length == 4 || splited[1].Length == 5) return true; - return false; + return splited[1].Length == 4 || splited[1].Length == 5; } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/Cat.cs b/Geekbot.net/Commands/Cat.cs index 1f97d5a..0545894 100644 --- a/Geekbot.net/Commands/Cat.cs +++ b/Geekbot.net/Commands/Cat.cs @@ -35,7 +35,7 @@ namespace Geekbot.net.Commands var stringResponse = await response.Content.ReadAsStringAsync(); var catFile = JsonConvert.DeserializeObject(stringResponse); var eb = new EmbedBuilder(); - eb.ImageUrl = catFile.file; + eb.ImageUrl = catFile.File; await ReplyAsync("", false, eb.Build()); } catch @@ -52,7 +52,7 @@ namespace Geekbot.net.Commands private class CatResponse { - public string file { get; set; } + public string File { get; set; } } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/Changelog.cs b/Geekbot.net/Commands/Changelog.cs index e060152..70c4c17 100644 --- a/Geekbot.net/Commands/Changelog.cs +++ b/Geekbot.net/Commands/Changelog.cs @@ -27,7 +27,7 @@ namespace Geekbot.net.Commands [Alias("updates")] [Remarks(CommandCategories.Helpers)] [Summary("Show the latest 5 updates")] - public async Task getChangelog() + public async Task GetChangelog() { try { @@ -40,7 +40,7 @@ namespace Geekbot.net.Commands response.EnsureSuccessStatusCode(); var stringResponse = await response.Content.ReadAsStringAsync(); - var commits = JsonConvert.DeserializeObject>(stringResponse); + var commits = JsonConvert.DeserializeObject>(stringResponse); var eb = new EmbedBuilder(); eb.WithColor(new Color(143, 165, 102)); eb.WithAuthor(new EmbedAuthorBuilder @@ -51,7 +51,7 @@ namespace Geekbot.net.Commands }); var sb = new StringBuilder(); foreach (var commit in commits.Take(10)) - sb.AppendLine($"- {commit.commit.message} ({commit.commit.author.date:yyyy-MM-dd})"); + sb.AppendLine($"- {commit.Commit.Message} ({commit.Commit.Author.Date:yyyy-MM-dd})"); eb.Description = sb.ToString(); eb.WithFooter(new EmbedFooterBuilder { @@ -66,24 +66,20 @@ namespace Geekbot.net.Commands } } - private class Commit + private class CommitDto { - public string sha { get; set; } - public CommitInfo commit { get; set; } - public Uri html_url { get; set; } + public CommitInfo Commit { get; set; } } private class CommitInfo { - public commitAuthor author { get; set; } - public string message { get; set; } + public CommitAuthor Author { get; set; } + public string Message { get; set; } } - private class commitAuthor + private class CommitAuthor { - public string name { get; set; } - public string email { get; set; } - public DateTimeOffset date { get; set; } + public DateTimeOffset Date { get; set; } } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/CheckEm.cs b/Geekbot.net/Commands/CheckEm.cs index 61de091..67df682 100644 --- a/Geekbot.net/Commands/CheckEm.cs +++ b/Geekbot.net/Commands/CheckEm.cs @@ -46,7 +46,7 @@ namespace Geekbot.net.Commands sb.AppendLine($"**{number}**"); if (!string.IsNullOrEmpty(dubtriqua)) sb.AppendLine($":tada: {dubtriqua} :tada:"); - sb.AppendLine(_checkEmImages.getCheckem()); + sb.AppendLine(_checkEmImages.GetCheckem()); await ReplyAsync(sb.ToString()); } diff --git a/Geekbot.net/Commands/ChuckNorrisJokes.cs b/Geekbot.net/Commands/ChuckNorrisJokes.cs index 1ac6c4d..1c3b009 100644 --- a/Geekbot.net/Commands/ChuckNorrisJokes.cs +++ b/Geekbot.net/Commands/ChuckNorrisJokes.cs @@ -35,7 +35,7 @@ namespace Geekbot.net.Commands var stringResponse = await response.Content.ReadAsStringAsync(); var data = JsonConvert.DeserializeObject(stringResponse); - await ReplyAsync(data.value); + await ReplyAsync(data.Value); } catch (HttpRequestException) { @@ -51,11 +51,7 @@ namespace Geekbot.net.Commands private class ChuckNorrisJokeResponse { - public string category { get; set; } - public string icon_url { get; set; } - public string id { get; set; } - public string url { get; set; } - public string value { get; set; } + public string Value { get; set; } } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/DadJokes.cs b/Geekbot.net/Commands/DadJokes.cs index 615f3d5..897c3e0 100644 --- a/Geekbot.net/Commands/DadJokes.cs +++ b/Geekbot.net/Commands/DadJokes.cs @@ -2,7 +2,6 @@ using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; -using Discord; using Discord.Commands; using Geekbot.net.Lib; using Newtonsoft.Json; @@ -36,7 +35,7 @@ namespace Geekbot.net.Commands var stringResponse = await response.Content.ReadAsStringAsync(); var data = JsonConvert.DeserializeObject(stringResponse); - await ReplyAsync(data.joke); + await ReplyAsync(data.Joke); } catch (HttpRequestException) { @@ -52,9 +51,7 @@ namespace Geekbot.net.Commands private class DadJokeResponse { - public string id { get; set; } - public string joke { get; set; } - public string status { get; set; } + public string Joke { get; set; } } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/Dice.cs b/Geekbot.net/Commands/Dice.cs index 774f8b9..89b3a11 100644 --- a/Geekbot.net/Commands/Dice.cs +++ b/Geekbot.net/Commands/Dice.cs @@ -20,12 +20,12 @@ namespace Geekbot.net.Commands var mod = 0; foreach (var i in splitedDices) { - var dice = toDice(i); - if (dice.sides != 0 && dice.times != 0) + var dice = ToDice(i); + if (dice.Sides != 0 && dice.Times != 0) { dices.Add(dice); } - else if (dice.mod != 0) + else if (dice.Mod != 0) { if (mod != 0) { @@ -33,7 +33,7 @@ namespace Geekbot.net.Commands return; } - mod = dice.mod; + mod = dice.Mod; } } @@ -45,13 +45,13 @@ namespace Geekbot.net.Commands } - if (dices.Any(d => d.times > 20)) + if (dices.Any(d => d.Times > 20)) { await ReplyAsync("You can't throw more than 20 dices"); return; } - if (dices.Any(d => d.sides > 120)) + if (dices.Any(d => d.Sides > 120)) { await ReplyAsync("A dice can't have more than 120 sides"); return; @@ -66,16 +66,16 @@ namespace Geekbot.net.Commands foreach (var dice in dices) { var results = new List(); - for (var i = 0; i < dice.times; i++) + for (var i = 0; i < dice.Times; i++) { - var roll = new Random().Next(1, dice.sides); + var roll = new Random().Next(1, dice.Sides); total += roll; results.Add(roll); - if (roll == dice.sides) extraText = "**Critical Hit!**"; + if (roll == dice.Sides) extraText = "**Critical Hit!**"; if (roll == 1) extraText = "**Critical Fail!**"; } - resultStrings.Add($"{dice.diceType} ({string.Join(",", results)})"); + resultStrings.Add($"{dice.DiceType} ({string.Join(",", results)})"); } rep.Append(string.Join(" + ", resultStrings)); @@ -91,7 +91,7 @@ namespace Geekbot.net.Commands await ReplyAsync(rep.ToString()); } - private DiceTypeDto toDice(string dice) + private DiceTypeDto ToDice(string dice) { var diceParts = dice.Split('d'); if (diceParts.Length == 2 @@ -99,15 +99,15 @@ namespace Geekbot.net.Commands && int.TryParse(diceParts[1], out var max)) return new DiceTypeDto { - diceType = dice, - times = times, - sides = max + DiceType = dice, + Times = times, + Sides = max }; if (dice.Length == 1 && int.TryParse(diceParts[0], out var mod)) return new DiceTypeDto { - mod = mod + Mod = mod }; return new DiceTypeDto(); } @@ -115,9 +115,9 @@ namespace Geekbot.net.Commands internal class DiceTypeDto { - public string diceType { get; set; } - public int times { get; set; } - public int sides { get; set; } - public int mod { get; set; } + public string DiceType { get; set; } + public int Times { get; set; } + public int Sides { get; set; } + public int Mod { get; set; } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/Dog.cs b/Geekbot.net/Commands/Dog.cs index 485b7f0..30c0301 100644 --- a/Geekbot.net/Commands/Dog.cs +++ b/Geekbot.net/Commands/Dog.cs @@ -35,7 +35,7 @@ namespace Geekbot.net.Commands var stringResponse = await response.Content.ReadAsStringAsync(); var dogFile = JsonConvert.DeserializeObject(stringResponse); var eb = new EmbedBuilder(); - eb.ImageUrl = dogFile.url; + eb.ImageUrl = dogFile.Url; await ReplyAsync("", false, eb.Build()); } catch (HttpRequestException e) @@ -52,7 +52,7 @@ namespace Geekbot.net.Commands private class DogResponse { - public string url { get; set; } + public string Url { get; set; } } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/Emojify.cs b/Geekbot.net/Commands/Emojify.cs index 7bdbf8a..739a4dc 100644 --- a/Geekbot.net/Commands/Emojify.cs +++ b/Geekbot.net/Commands/Emojify.cs @@ -23,7 +23,7 @@ namespace Geekbot.net.Commands { try { - var emojis = _emojiConverter.textToEmoji(text); + var emojis = _emojiConverter.TextToEmoji(text); if (emojis.Length > 1999) { await ReplyAsync("I can't take that much at once!"); diff --git a/Geekbot.net/Commands/Gdq.cs b/Geekbot.net/Commands/Gdq.cs index b191d6c..fef1c96 100644 --- a/Geekbot.net/Commands/Gdq.cs +++ b/Geekbot.net/Commands/Gdq.cs @@ -1,8 +1,6 @@ using System; -using System.Linq; using System.Net; using System.Threading.Tasks; -using Discord; using Discord.Commands; using Geekbot.net.Lib; @@ -20,7 +18,7 @@ namespace Geekbot.net.Commands [Command("gdq", RunMode = RunMode.Async)] [Remarks(CommandCategories.Games)] [Summary("Get a quote from the GDQ donation generator.")] - public async Task getQuote() + public async Task GetQuote() { try { diff --git a/Geekbot.net/Commands/Google.cs b/Geekbot.net/Commands/Google.cs index b10bc02..19d3efb 100644 --- a/Geekbot.net/Commands/Google.cs +++ b/Geekbot.net/Commands/Google.cs @@ -2,14 +2,12 @@ using System.Collections.Generic; using System.Linq; using System.Net; -using System.Net.Http; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Discord.Net; using Geekbot.net.Lib; -using Newtonsoft.Json; using StackExchange.Redis; +using Utf8Json; namespace Geekbot.net.Commands { @@ -27,7 +25,7 @@ namespace Geekbot.net.Commands [Command("google", RunMode = RunMode.Async)] [Remarks(CommandCategories.Helpers)] [Summary("Google Something.")] - public async Task askGoogle([Remainder, Summary("SearchText")] string searchText) + public async Task AskGoogle([Remainder, Summary("SearchText")] string searchText) { try { @@ -42,21 +40,21 @@ namespace Geekbot.net.Commands var url = new Uri($"https://kgsearch.googleapis.com/v1/entities:search?languages=en&limit=1&query={searchText}&key={apiKey}"); var responseString = client.DownloadString(url); - var response = Utf8Json.JsonSerializer.Deserialize(responseString); + var response = JsonSerializer.Deserialize(responseString); - if (!response.itemListElement.Any()) + if (!response.ItemListElement.Any()) { await ReplyAsync("No results were found..."); return; } - var data = response.itemListElement.First().result; + var data = response.ItemListElement.First().Result; var eb = new EmbedBuilder(); - eb.Title = data.name; - if(!string.IsNullOrEmpty(data.description)) eb.WithDescription(data.description); - if(!string.IsNullOrEmpty(data.detailedDescription?.url)) eb.WithUrl(data.detailedDescription.url); - if(!string.IsNullOrEmpty(data.detailedDescription?.articleBody)) eb.AddField("Details", data.detailedDescription.articleBody); - if(!string.IsNullOrEmpty(data.image?.contentUrl)) eb.WithThumbnailUrl(data.image.contentUrl); + eb.Title = data.Name; + if(!string.IsNullOrEmpty(data.Description)) eb.WithDescription(data.Description); + if(!string.IsNullOrEmpty(data.DetailedDescription?.Url)) eb.WithUrl(data.DetailedDescription.Url); + if(!string.IsNullOrEmpty(data.DetailedDescription?.ArticleBody)) eb.AddField("Details", data.DetailedDescription.ArticleBody); + if(!string.IsNullOrEmpty(data.Image?.ContentUrl)) eb.WithThumbnailUrl(data.Image.ContentUrl); await ReplyAsync("", false, eb.Build()); } @@ -67,35 +65,35 @@ namespace Geekbot.net.Commands } } - public class GoogleKGApiResponse + public class GoogleKgApiResponse { - public List itemListElement { get; set; } + public List ItemListElement { get; set; } - public class GoogleKGApiElement + public class GoogleKgApiElement { - public GoogleKGApiResult result { get; set; } - public double resultScore { get; set; } + public GoogleKgApiResult Result { get; set; } + public double ResultScore { get; set; } } - public class GoogleKGApiResult + public class GoogleKgApiResult { - public string name { get; set; } - public string description { get; set; } - public GoogleKGApiImage image { get; set; } - public GoogleKGApiDetailed detailedDescription { get; set; } + public string Name { get; set; } + public string Description { get; set; } + public GoogleKgApiImage Image { get; set; } + public GoogleKgApiDetailed DetailedDescription { get; set; } } - public class GoogleKGApiImage + public class GoogleKgApiImage { - public string contentUrl { get; set; } - public string url { get; set; } + public string ContentUrl { get; set; } + public string Url { get; set; } } - public class GoogleKGApiDetailed + public class GoogleKgApiDetailed { - public string articleBody { get; set; } - public string url { get; set; } - public string license { get; set; } + public string ArticleBody { get; set; } + public string Url { get; set; } + public string License { get; set; } } } } diff --git a/Geekbot.net/Commands/GuildInfo.cs b/Geekbot.net/Commands/GuildInfo.cs index 2ca76c6..62e0fd1 100644 --- a/Geekbot.net/Commands/GuildInfo.cs +++ b/Geekbot.net/Commands/GuildInfo.cs @@ -24,7 +24,7 @@ namespace Geekbot.net.Commands [Command("serverstats", RunMode = RunMode.Async)] [Remarks(CommandCategories.Statistics)] [Summary("Show some info about the bot.")] - public async Task getInfo() + public async Task GetInfo() { try { diff --git a/Geekbot.net/Commands/Karma.cs b/Geekbot.net/Commands/Karma.cs index 1affa82..d379b06 100644 --- a/Geekbot.net/Commands/Karma.cs +++ b/Geekbot.net/Commands/Karma.cs @@ -110,9 +110,7 @@ namespace Geekbot.net.Commands private DateTimeOffset ConvertToDateTimeOffset(string dateTimeOffsetString) { - if (string.IsNullOrEmpty(dateTimeOffsetString)) - return DateTimeOffset.Now.Subtract(new TimeSpan(7, 18, 0, 0)); - return DateTimeOffset.Parse(dateTimeOffsetString); + return string.IsNullOrEmpty(dateTimeOffsetString) ? DateTimeOffset.Now.Subtract(new TimeSpan(7, 18, 0, 0)) : DateTimeOffset.Parse(dateTimeOffsetString); } private bool TimeoutFinished(DateTimeOffset lastKarma) diff --git a/Geekbot.net/Commands/MagicTheGathering.cs b/Geekbot.net/Commands/MagicTheGathering.cs index 044c287..9c7379f 100644 --- a/Geekbot.net/Commands/MagicTheGathering.cs +++ b/Geekbot.net/Commands/MagicTheGathering.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text.RegularExpressions; using System.Threading.Tasks; using Discord; using Discord.Commands; @@ -24,7 +23,7 @@ namespace Geekbot.net.Commands [Command("mtg", RunMode = RunMode.Async)] [Remarks(CommandCategories.Games)] [Summary("Find a Magic The Gathering Card.")] - public async Task getCard([Remainder] [Summary("name")] string cardName) + public async Task GetCard([Remainder] [Summary("name")] string cardName) { try { diff --git a/Geekbot.net/Commands/Mod.cs b/Geekbot.net/Commands/Mod.cs index c927b0d..e8751f6 100644 --- a/Geekbot.net/Commands/Mod.cs +++ b/Geekbot.net/Commands/Mod.cs @@ -32,7 +32,7 @@ namespace Geekbot.net.Commands [Command("namehistory", RunMode = RunMode.Async)] [Remarks(CommandCategories.Admin)] [Summary("See past usernames of an user")] - public async Task usernameHistory([Summary("@user")] IUser user) + public async Task UsernameHistory([Summary("@user")] IUser user) { try { @@ -52,7 +52,7 @@ namespace Geekbot.net.Commands [Command("kick", RunMode = RunMode.Async)] [Remarks(CommandCategories.Admin)] [Summary("Ban a user")] - public async Task kick([Summary("@user")] IUser userNormal, + public async Task Kick([Summary("@user")] IUser userNormal, [Summary("reason")] [Remainder] string reason = "none") { try diff --git a/Geekbot.net/Commands/Overwatch.cs b/Geekbot.net/Commands/Overwatch.cs index fbb9699..0a39662 100644 --- a/Geekbot.net/Commands/Overwatch.cs +++ b/Geekbot.net/Commands/Overwatch.cs @@ -5,7 +5,6 @@ using Discord.Commands; using Geekbot.net.Lib; using OverwatchAPI; using OverwatchAPI.Config; -using Serilog; using StackExchange.Redis; namespace Geekbot.net.Commands @@ -16,7 +15,7 @@ namespace Geekbot.net.Commands private readonly IErrorHandler _errorHandler; private readonly IUserRepository _userRepository; - public Overwatch(IErrorHandler errorHandler, IDatabase redis, IUserRepository userRepository) + public Overwatch(IErrorHandler errorHandler, IUserRepository userRepository) { _errorHandler = errorHandler; _userRepository = userRepository; @@ -25,18 +24,18 @@ namespace Geekbot.net.Commands [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() + public async Task OwProfile() { try { - var tag = _userRepository.getUserSetting(Context.User.Id, "BattleTag"); + 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); + var profile = await CreateProfile(tag); if (profile == null) { await ReplyAsync("That player doesn't seem to exist"); @@ -54,17 +53,17 @@ namespace Geekbot.net.Commands [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) + public async Task OwProfile([Summary("BattleTag")] string tag) { try { - if (!BattleTag.isValidTag(tag)) + if (!BattleTag.IsValidTag(tag)) { await ReplyAsync("That doesn't seem to be a valid battletag..."); return; } - var profile = await createProfile(tag); + var profile = await CreateProfile(tag); if (profile == null) { await ReplyAsync("That player doesn't seem to exist"); @@ -82,18 +81,18 @@ namespace Geekbot.net.Commands [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) + public async Task OwProfile([Summary("@someone")] IUser user) { try { - var tag = _userRepository.getUserSetting(user.Id, "BattleTag"); + 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); + var profile = await CreateProfile(tag); if (profile == null) { await ReplyAsync("That player doesn't seem to exist"); @@ -108,7 +107,7 @@ namespace Geekbot.net.Commands } } - private async Task createProfile(string battletag) + private async Task CreateProfile(string battletag) { var owConfig = new OverwatchConfig.Builder().WithPlatforms(Platform.Pc); using (var owClient = new OverwatchClient(owConfig)) diff --git a/Geekbot.net/Commands/Owner.cs b/Geekbot.net/Commands/Owner.cs index c4adb25..b73797f 100644 --- a/Geekbot.net/Commands/Owner.cs +++ b/Geekbot.net/Commands/Owner.cs @@ -4,7 +4,6 @@ using Discord; using Discord.Commands; using Discord.WebSocket; using Geekbot.net.Lib; -using Serilog; using StackExchange.Redis; namespace Geekbot.net.Commands @@ -68,7 +67,7 @@ namespace Geekbot.net.Commands [Command("popuserrepo", RunMode = RunMode.Async)] [Remarks(CommandCategories.Admin)] [Summary("Populate user cache")] - public async Task popUserRepoCommand() + public async Task PopUserRepoCommand() { try { @@ -82,8 +81,7 @@ namespace Geekbot.net.Commands } catch (Exception) { - await ReplyAsync( - $"Sorry, only the botowner can do this"); + await ReplyAsync("Sorry, only the botowner can do this"); return; } diff --git a/Geekbot.net/Commands/Pokedex.cs b/Geekbot.net/Commands/Pokedex.cs index 2b43693..fa8746f 100644 --- a/Geekbot.net/Commands/Pokedex.cs +++ b/Geekbot.net/Commands/Pokedex.cs @@ -36,7 +36,7 @@ namespace Geekbot.net.Commands return; } - var embed = await pokemonEmbedBuilder(pokemon); + var embed = await PokemonEmbedBuilder(pokemon); await ReplyAsync("", false, embed.Build()); } catch (Exception e) @@ -45,29 +45,29 @@ namespace Geekbot.net.Commands } } - private async Task pokemonEmbedBuilder(Pokemon pokemon) + private async Task PokemonEmbedBuilder(Pokemon pokemon) { var eb = new EmbedBuilder(); var species = await DataFetcher.GetApiObject(pokemon.ID); - eb.Title = $"#{pokemon.ID} {toUpper(pokemon.Name)}"; + 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(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) + 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) + private string ToUpper(string s) { if (string.IsNullOrEmpty(s)) return string.Empty; return char.ToUpper(s[0]) + s.Substring(1); diff --git a/Geekbot.net/Commands/Poll.cs b/Geekbot.net/Commands/Poll.cs index 51b6742..cf80835 100644 --- a/Geekbot.net/Commands/Poll.cs +++ b/Geekbot.net/Commands/Poll.cs @@ -82,14 +82,14 @@ namespace Geekbot.net.Commands var i = 1; pollList.ForEach(option => { - eb.AddInlineField($"Option {_emojiConverter.numberToEmoji(i)}", option); + eb.AddInlineField($"Option {_emojiConverter.NumberToEmoji(i)}", option); i++; }); var pollMessage = await ReplyAsync("", false, eb.Build()); i = 1; pollList.ForEach(option => { - pollMessage.AddReactionAsync(new Emoji(_emojiConverter.numberToEmoji(i))); + pollMessage.AddReactionAsync(new Emoji(_emojiConverter.NumberToEmoji(i))); i++; }); var poll = new PollData @@ -123,7 +123,7 @@ namespace Geekbot.net.Commands return; } - var results = await getPollResults(currentPoll); + var results = await GetPollResults(currentPoll); var sb = new StringBuilder(); sb.AppendLine("**Poll Results**"); sb.AppendLine(currentPoll.Question); @@ -152,7 +152,7 @@ namespace Geekbot.net.Commands } } - private async Task> getPollResults(PollData poll) + private async Task> GetPollResults(PollData poll) { var message = (IUserMessage) await Context.Channel.GetMessageAsync(poll.MessageId); var results = new List(); diff --git a/Geekbot.net/Commands/Quote.cs b/Geekbot.net/Commands/Quote.cs index fc4e2ce..4b4cfc3 100644 --- a/Geekbot.net/Commands/Quote.cs +++ b/Geekbot.net/Commands/Quote.cs @@ -24,7 +24,7 @@ namespace Geekbot.net.Commands [Command] [Remarks(CommandCategories.Quotes)] [Summary("Return a random quoute from the database")] - public async Task getRandomQuote() + public async Task GetRandomQuote() { try { @@ -32,7 +32,7 @@ namespace Geekbot.net.Commands var randomNumber = new Random().Next(randomQuotes.Length - 1); var randomQuote = randomQuotes[randomNumber]; var quote = JsonConvert.DeserializeObject(randomQuote); - var embed = quoteBuilder(quote, randomNumber + 1); + var embed = QuoteBuilder(quote, randomNumber + 1); await ReplyAsync("", false, embed.Build()); } catch (Exception e) @@ -44,7 +44,7 @@ namespace Geekbot.net.Commands [Command("save")] [Remarks(CommandCategories.Quotes)] [Summary("Save a quote from the last sent message by @user")] - public async Task saveQuote([Summary("@user")] IUser user) + public async Task SaveQuote([Summary("@user")] IUser user) { try { @@ -60,11 +60,11 @@ namespace Geekbot.net.Commands return; } - var lastMessage = await getLastMessageByUser(user); - var quote = createQuoteObject(lastMessage); + var lastMessage = await GetLastMessageByUser(user); + var quote = CreateQuoteObject(lastMessage); var quoteStore = JsonConvert.SerializeObject(quote); _redis.SetAdd($"{Context.Guild.Id}:Quotes", quoteStore); - var embed = quoteBuilder(quote); + var embed = QuoteBuilder(quote); await ReplyAsync("**Quote Added**", false, embed.Build()); } catch (Exception e) @@ -77,7 +77,7 @@ namespace Geekbot.net.Commands [Command("save")] [Remarks(CommandCategories.Quotes)] [Summary("Save a quote from a message id")] - public async Task saveQuote([Summary("messageId")] ulong messageId) + public async Task SaveQuote([Summary("messageId")] ulong messageId) { try { @@ -94,10 +94,10 @@ namespace Geekbot.net.Commands return; } - var quote = createQuoteObject(message); + var quote = CreateQuoteObject(message); var quoteStore = JsonConvert.SerializeObject(quote); _redis.SetAdd($"{Context.Guild.Id}:Quotes", quoteStore); - var embed = quoteBuilder(quote); + var embed = QuoteBuilder(quote); await ReplyAsync("**Quote Added**", false, embed.Build()); } catch (Exception e) @@ -110,13 +110,13 @@ namespace Geekbot.net.Commands [Command("make")] [Remarks(CommandCategories.Quotes)] [Summary("Create a quote from the last sent message by @user")] - public async Task returnSpecifiedQuote([Summary("@user")] IUser user) + public async Task ReturnSpecifiedQuote([Summary("@user")] IUser user) { try { - var lastMessage = await getLastMessageByUser(user); - var quote = createQuoteObject(lastMessage); - var embed = quoteBuilder(quote); + var lastMessage = await GetLastMessageByUser(user); + var quote = CreateQuoteObject(lastMessage); + var embed = QuoteBuilder(quote); await ReplyAsync("", false, embed.Build()); } catch (Exception e) @@ -129,13 +129,13 @@ namespace Geekbot.net.Commands [Command("make")] [Remarks(CommandCategories.Quotes)] [Summary("Create a quote from a message id")] - public async Task returnSpecifiedQuote([Summary("messageId")] ulong messageId) + public async Task ReturnSpecifiedQuote([Summary("messageId")] ulong messageId) { try { var message = await Context.Channel.GetMessageAsync(messageId); - var quote = createQuoteObject(message); - var embed = quoteBuilder(quote); + var quote = CreateQuoteObject(message); + var embed = QuoteBuilder(quote); await ReplyAsync("", false, embed.Build()); } catch (Exception e) @@ -151,7 +151,7 @@ namespace Geekbot.net.Commands [RequireUserPermission(GuildPermission.ManageRoles)] [Remarks(CommandCategories.Quotes)] [Summary("Remove a quote (required mod permissions)")] - public async Task removeQuote([Summary("quoteId")] int id) + public async Task RemoveQuote([Summary("quoteId")] int id) { try { @@ -160,12 +160,12 @@ namespace Geekbot.net.Commands if (success) { var quote = JsonConvert.DeserializeObject(quotes[id - 1]); - var embed = quoteBuilder(quote); + var embed = QuoteBuilder(quote); await ReplyAsync($"**Removed #{id}**", false, embed.Build()); } else { - await ReplyAsync($"I couldn't find a quote with that id :disappointed:"); + await ReplyAsync("I couldn't find a quote with that id :disappointed:"); } } catch (Exception e) @@ -175,7 +175,7 @@ namespace Geekbot.net.Commands } } - private async Task getLastMessageByUser(IUser user) + private async Task GetLastMessageByUser(IUser user) { var list = Context.Channel.GetMessagesAsync().Flatten(); await list; @@ -186,20 +186,20 @@ namespace Geekbot.net.Commands && !msg.Content.ToLower().StartsWith("!")); } - private EmbedBuilder quoteBuilder(QuoteObject quote, int id = 0) + private EmbedBuilder QuoteBuilder(QuoteObject quote, int id = 0) { - var user = Context.Client.GetUserAsync(quote.userId).Result; + var user = Context.Client.GetUserAsync(quote.UserId).Result; var eb = new EmbedBuilder(); eb.WithColor(new Color(143, 167, 232)); eb.Title = id == 0 ? "" : $"#{id} | "; - eb.Title += $"{user.Username} @ {quote.time.Day}.{quote.time.Month}.{quote.time.Year}"; - eb.Description = quote.quote; + eb.Title += $"{user.Username} @ {quote.Time.Day}.{quote.Time.Month}.{quote.Time.Year}"; + eb.Description = quote.Quote; eb.ThumbnailUrl = user.GetAvatarUrl(); - if (quote.image != null) eb.ImageUrl = quote.image; + if (quote.Image != null) eb.ImageUrl = quote.Image; return eb; } - private QuoteObject createQuoteObject(IMessage message) + private QuoteObject CreateQuoteObject(IMessage message) { string image; try @@ -213,19 +213,19 @@ namespace Geekbot.net.Commands return new QuoteObject { - userId = message.Author.Id, - time = message.Timestamp.DateTime, - quote = message.Content, - image = image + UserId = message.Author.Id, + Time = message.Timestamp.DateTime, + Quote = message.Content, + Image = image }; } } public class QuoteObject { - public ulong userId { get; set; } - public string quote { get; set; } - public DateTime time { get; set; } - public string image { get; set; } + public ulong UserId { get; set; } + public string Quote { get; set; } + public DateTime Time { get; set; } + public string Image { get; set; } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/RandomAnimals.cs b/Geekbot.net/Commands/RandomAnimals.cs index f9dead9..2101c33 100644 --- a/Geekbot.net/Commands/RandomAnimals.cs +++ b/Geekbot.net/Commands/RandomAnimals.cs @@ -18,62 +18,62 @@ namespace Geekbot.net.Commands [Command("panda", RunMode = RunMode.Async)] [Remarks(CommandCategories.Randomness)] [Summary("Get a random panda image")] - public async Task panda() + public async Task Panda() { - await ReplyAsync("", false, eb(_mediaProvider.getPanda())); + await ReplyAsync("", false, Eb(_mediaProvider.GetPanda())); } [Command("croissant", RunMode = RunMode.Async)] [Alias("gipfeli")] [Remarks(CommandCategories.Randomness)] [Summary("Get a random croissant image")] - public async Task croissant() + public async Task Croissant() { - await ReplyAsync("", false, eb(_mediaProvider.getCrossant())); + await ReplyAsync("", false, Eb(_mediaProvider.GetCrossant())); } [Command("pumpkin", RunMode = RunMode.Async)] [Remarks(CommandCategories.Randomness)] [Summary("Get a random pumpkin image")] - public async Task pumpkin() + public async Task Pumpkin() { - await ReplyAsync("", false, eb(_mediaProvider.getPumpkin())); + await ReplyAsync("", false, Eb(_mediaProvider.GetPumpkin())); } [Command("squirrel", RunMode = RunMode.Async)] [Remarks(CommandCategories.Randomness)] [Summary("Get a random squirrel image")] - public async Task squirrel() + public async Task Squirrel() { - await ReplyAsync("", false, eb(_mediaProvider.getSquirrel())); + await ReplyAsync("", false, Eb(_mediaProvider.GetSquirrel())); } [Command("turtle", RunMode = RunMode.Async)] [Remarks(CommandCategories.Randomness)] [Summary("Get a random turtle image")] - public async Task turtle() + public async Task Turtle() { - await ReplyAsync("", false, eb(_mediaProvider.getTurtle())); + await ReplyAsync("", false, Eb(_mediaProvider.GetTurtle())); } [Command("pinguin", RunMode = RunMode.Async)] [Alias("pingu")] [Remarks(CommandCategories.Randomness)] [Summary("Get a random turtle image")] - public async Task pinguin() + public async Task Pinguin() { - await ReplyAsync("", false, eb(_mediaProvider.getPinguin())); + await ReplyAsync("", false, Eb(_mediaProvider.GetPinguin())); } [Command("fox", RunMode = RunMode.Async)] [Remarks(CommandCategories.Randomness)] [Summary("Get a random turtle image")] - public async Task fox() + public async Task Fox() { - await ReplyAsync("", false, eb(_mediaProvider.getFox())); + await ReplyAsync("", false, Eb(_mediaProvider.GetFox())); } - private EmbedBuilder eb(string image) + private EmbedBuilder Eb(string image) { var eb = new EmbedBuilder(); eb.ImageUrl = image; diff --git a/Geekbot.net/Commands/Rank.cs b/Geekbot.net/Commands/Rank.cs index 8c11171..92f78bd 100644 --- a/Geekbot.net/Commands/Rank.cs +++ b/Geekbot.net/Commands/Rank.cs @@ -6,7 +6,6 @@ using System.Threading.Tasks; using Discord.Commands; using Discord.WebSocket; using Geekbot.net.Lib; -using Serilog; using StackExchange.Redis; namespace Geekbot.net.Commands @@ -110,7 +109,7 @@ namespace Geekbot.net.Commands foreach (var user in highscoreUsers) { replyBuilder.Append(highscorePlace < 11 - ? $"{_emojiConverter.numberToEmoji(highscorePlace)} " + ? $"{_emojiConverter.NumberToEmoji(highscorePlace)} " : $"`{highscorePlace}.` "); replyBuilder.Append(user.Key.Username != null diff --git a/Geekbot.net/Commands/Role.cs b/Geekbot.net/Commands/Role.cs index 1de55ab..eb67bbf 100644 --- a/Geekbot.net/Commands/Role.cs +++ b/Geekbot.net/Commands/Role.cs @@ -27,7 +27,7 @@ namespace Geekbot.net.Commands [Command(RunMode = RunMode.Async)] [Remarks(CommandCategories.Helpers)] [Summary("Get a list of all available roles.")] - public async Task getAllRoles() + public async Task GetAllRoles() { try { @@ -53,7 +53,7 @@ namespace Geekbot.net.Commands [Command(RunMode = RunMode.Async)] [Remarks(CommandCategories.Helpers)] [Summary("Get a role by mentioning it.")] - public async Task giveRole([Summary("roleNickname")] string roleNameRaw) + public async Task GiveRole([Summary("roleNickname")] string roleNameRaw) { try { @@ -97,7 +97,7 @@ namespace Geekbot.net.Commands [Command("add", RunMode = RunMode.Async)] [Remarks(CommandCategories.Admin)] [Summary("Add a role to the whitelist.")] - public async Task addRole([Summary("@role")] IRole role, [Summary("alias")] string roleName) + public async Task AddRole([Summary("@role")] IRole role, [Summary("alias")] string roleName) { try { @@ -132,7 +132,7 @@ namespace Geekbot.net.Commands [Command("remove", RunMode = RunMode.Async)] [Remarks(CommandCategories.Admin)] [Summary("Remove a role from the whitelist.")] - public async Task removeRole([Summary("roleNickname")] string roleName) + public async Task RemoveRole([Summary("roleNickname")] string roleName) { try { @@ -166,7 +166,7 @@ namespace Geekbot.net.Commands if (!emoji.StartsWith('<')) { var emo = new Emoji(emoji); - emote = (IEmote) emo; + emote = emo; } else { diff --git a/Geekbot.net/Commands/Ship.cs b/Geekbot.net/Commands/Ship.cs index 4727bc8..d7dfd0e 100644 --- a/Geekbot.net/Commands/Ship.cs +++ b/Geekbot.net/Commands/Ship.cs @@ -64,9 +64,7 @@ namespace Geekbot.net.Commands return "There might be a chance"; if (rate >= 60 && rate < 80) return "Almost a match, but could work"; - if (rate >= 80) - return "It's a match"; - return "a"; + return rate >= 80 ? "It's a match" : "a"; } private string BlockCounter(int rate) diff --git a/Geekbot.net/Commands/Slap.cs b/Geekbot.net/Commands/Slap.cs index 09a6c15..668d33e 100644 --- a/Geekbot.net/Commands/Slap.cs +++ b/Geekbot.net/Commands/Slap.cs @@ -32,7 +32,7 @@ namespace Geekbot.net.Commands return; } - var things = new List() + var things = new List { "thing", "rubber chicken", diff --git a/Geekbot.net/Commands/UrbanDictionary.cs b/Geekbot.net/Commands/UrbanDictionary.cs index 573b289..a951cb8 100644 --- a/Geekbot.net/Commands/UrbanDictionary.cs +++ b/Geekbot.net/Commands/UrbanDictionary.cs @@ -22,7 +22,7 @@ namespace Geekbot.net.Commands [Command("urban", RunMode = RunMode.Async)] [Remarks(CommandCategories.Helpers)] [Summary("Lookup something on urban dictionary")] - public async Task urbanDefine([Remainder] [Summary("word")] string word) + public async Task UrbanDefine([Remainder] [Summary("word")] string word) { try { @@ -34,26 +34,26 @@ namespace Geekbot.net.Commands var stringResponse = await response.Content.ReadAsStringAsync(); var definitions = JsonConvert.DeserializeObject(stringResponse); - if (definitions.list.Count == 0) + if (definitions.List.Count == 0) { await ReplyAsync("That word hasn't been defined..."); return; } - var definition = definitions.list.First(e => !string.IsNullOrWhiteSpace(e.example)); + var definition = definitions.List.First(e => !string.IsNullOrWhiteSpace(e.Example)); var eb = new EmbedBuilder(); eb.WithAuthor(new EmbedAuthorBuilder { - Name = definition.word, - Url = definition.permalink + Name = definition.Word, + Url = definition.Permalink }); eb.WithColor(new Color(239, 255, 0)); - eb.Description = definition.definition; - eb.AddField("Example", definition.example ?? "(no example given...)"); - eb.AddInlineField("Upvotes", definition.thumbs_up); - eb.AddInlineField("Downvotes", definition.thumbs_down); - if (definitions.tags.Length > 0) eb.AddField("Tags", string.Join(", ", definitions.tags)); + eb.Description = definition.Definition; + eb.AddField("Example", definition.Example ?? "(no example given...)"); + eb.AddInlineField("Upvotes", definition.ThumbsUp); + eb.AddInlineField("Downvotes", definition.ThumbsDown); + if (definitions.Tags.Length > 0) eb.AddField("Tags", string.Join(", ", definitions.Tags)); await ReplyAsync("", false, eb.Build()); } @@ -66,22 +66,18 @@ namespace Geekbot.net.Commands private class UrbanResponse { - public string[] tags { get; set; } - public string result_type { get; set; } - public List list { get; set; } + public string[] Tags { get; set; } + public List List { get; set; } } private class UrbanListItem { - public string definition { get; set; } - public string permalink { get; set; } - public string thumbs_up { get; set; } - public string author { get; set; } - public string word { get; set; } - public string defid { get; set; } - public string current_vote { get; set; } - public string example { get; set; } - public string thumbs_down { get; set; } + public string Definition { get; set; } + public string Permalink { get; set; } + public string ThumbsUp { get; set; } + public string Word { get; set; } + public string Example { get; set; } + public string ThumbsDown { get; set; } } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/Wikipedia.cs b/Geekbot.net/Commands/Wikipedia.cs index 77fb41d..05b21a6 100644 --- a/Geekbot.net/Commands/Wikipedia.cs +++ b/Geekbot.net/Commands/Wikipedia.cs @@ -78,7 +78,7 @@ namespace Geekbot.net.Commands }; eb.AddField("Description", article.Extract); - if (article.Coordinates != null) eb.AddField("Coordinates", $"{article.Coordinates.lat} Lat {article.Coordinates.lon} Lon"); + if (article.Coordinates != null) eb.AddField("Coordinates", $"{article.Coordinates.Lat} Lat {article.Coordinates.Lon} Lon"); await ReplyAsync("", false, eb.Build()); } catch (HttpRequestException) diff --git a/Geekbot.net/Commands/mal.cs b/Geekbot.net/Commands/mal.cs index 22b3d4a..8c16936 100644 --- a/Geekbot.net/Commands/mal.cs +++ b/Geekbot.net/Commands/mal.cs @@ -7,12 +7,12 @@ using Geekbot.net.Lib; namespace Geekbot.net.Commands { - public class mal : ModuleBase + public class Mal : ModuleBase { private readonly IErrorHandler _errorHandler; private readonly IMalClient _malClient; - public mal(IMalClient malClient, IErrorHandler errorHandler) + public Mal(IMalClient malClient, IErrorHandler errorHandler) { _malClient = malClient; _errorHandler = errorHandler; @@ -21,13 +21,13 @@ namespace Geekbot.net.Commands [Command("anime", RunMode = RunMode.Async)] [Remarks(CommandCategories.Helpers)] [Summary("Show Info about an Anime.")] - public async Task searchAnime([Remainder] [Summary("AnimeName")] string animeName) + public async Task SearchAnime([Remainder] [Summary("AnimeName")] string animeName) { try { - if (_malClient.isLoggedIn()) + if (_malClient.IsLoggedIn()) { - var anime = await _malClient.getAnime(animeName); + var anime = await _malClient.GetAnime(animeName); if (anime != null) { var eb = new EmbedBuilder(); @@ -70,13 +70,13 @@ namespace Geekbot.net.Commands [Command("manga", RunMode = RunMode.Async)] [Remarks(CommandCategories.Helpers)] [Summary("Show Info about a Manga.")] - public async Task searchManga([Remainder] [Summary("MangaName")] string mangaName) + public async Task SearchManga([Remainder] [Summary("MangaName")] string mangaName) { try { - if (_malClient.isLoggedIn()) + if (_malClient.IsLoggedIn()) { - var manga = await _malClient.getManga(mangaName); + var manga = await _malClient.GetManga(mangaName); if (manga != null) { var eb = new EmbedBuilder(); diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index c1e0ee3..31608b2 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -1,12 +1,10 @@ using System; -using System.Collections; using System.Text; using System.Threading.Tasks; using Discord; using Discord.Commands; using Discord.WebSocket; using Geekbot.net.Lib; -using Serilog; using StackExchange.Redis; namespace Geekbot.net diff --git a/Geekbot.net/Lib/AudioClientCache.cs b/Geekbot.net/Lib/AudioClientCache.cs deleted file mode 100644 index d43b98a..0000000 --- a/Geekbot.net/Lib/AudioClientCache.cs +++ /dev/null @@ -1,106 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Net; -using System.Security.Cryptography; -using Discord.Audio; -using Discord.Net; - -namespace Geekbot.net.Lib -{ - public class AudioUtils : IAudioUtils - { - private string _tempFolderPath; - private Dictionary _audioClients; - - public AudioUtils() - { - _audioClients = new Dictionary(); - _tempFolderPath = Path.GetFullPath("./tmp/"); - if (Directory.Exists(_tempFolderPath)) - { - Directory.Delete(_tempFolderPath, true); - } - Directory.CreateDirectory(_tempFolderPath); - } - - public IAudioClient GetAudioClient(ulong guildId) - { - return _audioClients[guildId]; - } - - public void StoreAudioClient(ulong guildId, IAudioClient client) - { - _audioClients[guildId] = client; - } - - public Process CreateStreamFromFile(string path) - { - var ffmpeg = new ProcessStartInfo - { - FileName = "ffmpeg", - Arguments = $"-i {path} -ac 2 -f s16le -ar 48000 pipe:1", - UseShellExecute = false, - RedirectStandardOutput = true, - }; - return Process.Start(ffmpeg); - } - - public Process CreateStreamFromYoutube(string url, ulong guildId) - { - var ytdlMediaUrl = GetYoutubeMediaUrl(url); - DownloadMediaUrl(ytdlMediaUrl, guildId); - return CreateStreamFromFile($"{_tempFolderPath}{guildId}"); - } - - public void Cleanup(ulong guildId) - { - File.Delete($"{_tempFolderPath}{guildId}"); - } - - private string GetYoutubeMediaUrl(string url) - { - var ytdl = new ProcessStartInfo() - { - FileName = "youtube-dl", - Arguments = $"-f bestaudio -g {url}", - UseShellExecute = false, - RedirectStandardOutput = true - }; - var output = Process.Start(ytdl).StandardOutput.ReadToEnd(); - if (string.IsNullOrWhiteSpace(output)) - { - throw new Exception("Could not get Youtube Media URL"); - } - return output; - } - - private void DownloadMediaUrl(string url, ulong guildId) - { - using (var web = new WebClient()) - { - web.DownloadFile(url, $"{_tempFolderPath}{guildId}"); - } -// var ffmpeg = new ProcessStartInfo -// { -// FileName = "ffmpeg", -// Arguments = $"-i \"{_tempFolderPath}{guildId}\" -c:a mp3 -b:a 256k {_tempFolderPath}{guildId}.mp3", -// UseShellExecute = false, -// RedirectStandardOutput = true, -// }; -// Process.Start(ffmpeg).WaitForExit(); -// File.Delete($"{_tempFolderPath}{guildId}"); - return; - } - } - - public interface IAudioUtils - { - IAudioClient GetAudioClient(ulong guildId); - void StoreAudioClient(ulong guildId, IAudioClient client); - Process CreateStreamFromFile(string path); - Process CreateStreamFromYoutube(string url, ulong guildId); - void Cleanup(ulong guildId); - } -} \ No newline at end of file diff --git a/Geekbot.net/Lib/CommandCategories.cs b/Geekbot.net/Lib/CommandCategories.cs index ca7ea58..d67123b 100644 --- a/Geekbot.net/Lib/CommandCategories.cs +++ b/Geekbot.net/Lib/CommandCategories.cs @@ -1,6 +1,6 @@ namespace Geekbot.net.Lib { - public class CommandCategories + public static class CommandCategories { public const string Randomness = "Randomness"; public const string Karma = "Karma"; diff --git a/Geekbot.net/Lib/Constants.cs b/Geekbot.net/Lib/Constants.cs index db5c875..d754b5c 100644 --- a/Geekbot.net/Lib/Constants.cs +++ b/Geekbot.net/Lib/Constants.cs @@ -3,7 +3,7 @@ public class Constants { public const string Name = "Geekbot"; - public const double BotVersion = 3.5; + public const double BotVersion = 3.6; public const double ApiVersion = 1; } } \ No newline at end of file diff --git a/Geekbot.net/Lib/EmojiConverter.cs b/Geekbot.net/Lib/EmojiConverter.cs index 7091f0c..b836485 100644 --- a/Geekbot.net/Lib/EmojiConverter.cs +++ b/Geekbot.net/Lib/EmojiConverter.cs @@ -5,13 +5,13 @@ namespace Geekbot.net.Lib { public class EmojiConverter : IEmojiConverter { - public string numberToEmoji(int number) + public string NumberToEmoji(int number) { if (number == 10) { return "🔟"; } - var emojiMap = new string[] + var emojiMap = new[] { ":zero:", ":one:", @@ -22,7 +22,7 @@ namespace Geekbot.net.Lib ":six:", ":seven:", ":eight:", - ":nine:", + ":nine:" }; var numbers = number.ToString().ToCharArray(); var returnString = new StringBuilder(); @@ -33,7 +33,7 @@ namespace Geekbot.net.Lib return returnString.ToString(); } - public string textToEmoji(string text) + public string TextToEmoji(string text) { var emojiMap = new Hashtable { @@ -93,7 +93,7 @@ namespace Geekbot.net.Lib public interface IEmojiConverter { - string numberToEmoji(int number); - string textToEmoji(string text); + string NumberToEmoji(int number); + string TextToEmoji(string text); } } \ No newline at end of file diff --git a/Geekbot.net/Lib/ErrorHandler.cs b/Geekbot.net/Lib/ErrorHandler.cs index dc67db2..9bf99f4 100644 --- a/Geekbot.net/Lib/ErrorHandler.cs +++ b/Geekbot.net/Lib/ErrorHandler.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Diagnostics; using System.Net; using Discord.Commands; using Discord.Net; @@ -34,12 +32,12 @@ namespace Geekbot.net.Lib } } - public void HandleCommandException(Exception e, ICommandContext Context, string errorMessage = "def") + public void HandleCommandException(Exception e, ICommandContext context, string errorMessage = "def") { try { - var errorString = errorMessage == "def" ? _translation.GetString(Context.Guild.Id, "errorHandler", "SomethingWentWrong") : errorMessage; - var errorObj = SimpleConextConverter.ConvertContext(Context); + var errorString = errorMessage == "def" ? _translation.GetString(context.Guild.Id, "errorHandler", "SomethingWentWrong") : errorMessage; + var errorObj = SimpleConextConverter.ConvertContext(context); if (e.Message.Contains("50007")) return; if (e.Message.Contains("50013")) return; _logger.Error("Geekbot", "An error ocured", e, errorObj); @@ -49,11 +47,11 @@ namespace Geekbot.net.Lib { var resStackTrace = string.IsNullOrEmpty(e.InnerException?.ToString()) ? e.StackTrace : e.InnerException.ToString(); var maxLen = Math.Min(resStackTrace.Length, 1850); - Context.Channel.SendMessageAsync($"{e.Message}\r\n```\r\n{resStackTrace?.Substring(0, maxLen)}\r\n```"); + context.Channel.SendMessageAsync($"{e.Message}\r\n```\r\n{resStackTrace.Substring(0, maxLen)}\r\n```"); } else { - Context.Channel.SendMessageAsync(errorString); + context.Channel.SendMessageAsync(errorString); } } @@ -74,18 +72,18 @@ namespace Geekbot.net.Lib } catch (Exception ex) { - Context.Channel.SendMessageAsync("Something went really really wrong here"); + context.Channel.SendMessageAsync("Something went really really wrong here"); _logger.Error("Geekbot", "Errorception", ex); } } - public async void HandleHttpException(HttpException e, ICommandContext Context) + public async void HandleHttpException(HttpException e, ICommandContext context) { - var errorStrings = _translation.GetDict(Context, "httpErrors"); + var errorStrings = _translation.GetDict(context, "httpErrors"); switch(e.HttpCode) { case HttpStatusCode.Forbidden: - await Context.Channel.SendMessageAsync(errorStrings["403"]); + await context.Channel.SendMessageAsync(errorStrings["403"]); break; } } @@ -95,7 +93,7 @@ namespace Geekbot.net.Lib public interface IErrorHandler { - void HandleCommandException(Exception e, ICommandContext Context, string errorMessage = "def"); - void HandleHttpException(HttpException e, ICommandContext Context); + void HandleCommandException(Exception e, ICommandContext context, string errorMessage = "def"); + void HandleHttpException(HttpException e, ICommandContext context); } } \ No newline at end of file diff --git a/Geekbot.net/Lib/GeekbotLogger.cs b/Geekbot.net/Lib/GeekbotLogger.cs index f2feda7..b5214b0 100644 --- a/Geekbot.net/Lib/GeekbotLogger.cs +++ b/Geekbot.net/Lib/GeekbotLogger.cs @@ -10,7 +10,7 @@ namespace Geekbot.net.Lib private readonly ILogger _serilog; public GeekbotLogger() { - _serilog = LoggerFactory.createLogger(); + _serilog = LoggerFactory.CreateLogger(); //JsonSerializer.SetDefaultResolver(StandardResolver.AllowPrivateExcludeNullSnakeCase); Information("Geekbot", "Using GeekbotLogger"); } @@ -45,7 +45,7 @@ namespace Geekbot.net.Lib private string CreateLogObject(string type, string source, string message, Exception stackTrace = null, object extra = null) { - var logObject = new GeekbotLoggerObject() + var logObject = new GeekbotLoggerObject { Timestamp = DateTime.Now, Type = type, diff --git a/Geekbot.net/Lib/LevelCalc.cs b/Geekbot.net/Lib/LevelCalc.cs index d075754..75df83d 100644 --- a/Geekbot.net/Lib/LevelCalc.cs +++ b/Geekbot.net/Lib/LevelCalc.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; namespace Geekbot.net.Lib { diff --git a/Geekbot.net/Lib/LoggerFactory.cs b/Geekbot.net/Lib/LoggerFactory.cs index 789bdc6..9bff873 100644 --- a/Geekbot.net/Lib/LoggerFactory.cs +++ b/Geekbot.net/Lib/LoggerFactory.cs @@ -1,13 +1,12 @@ using System; using Serilog; -using Serilog.Formatting.Json; using Serilog.Sinks.SumoLogic; namespace Geekbot.net.Lib { public class LoggerFactory { - public static ILogger createLogger() + public static ILogger CreateLogger() { var loggerCreation = new LoggerConfiguration(); var template = "{Message}{NewLine}"; diff --git a/Geekbot.net/Lib/MalClient.cs b/Geekbot.net/Lib/MalClient.cs index bc09036..dcf4fe3 100644 --- a/Geekbot.net/Lib/MalClient.cs +++ b/Geekbot.net/Lib/MalClient.cs @@ -2,7 +2,6 @@ using MyAnimeListSharp.Auth; using MyAnimeListSharp.Core; using MyAnimeListSharp.Facade.Async; -using Serilog; using StackExchange.Redis; namespace Geekbot.net.Lib @@ -19,10 +18,10 @@ namespace Geekbot.net.Lib { _redis = redis; _logger = logger; - reloadClient(); + ReloadClient(); } - public bool reloadClient() + public bool ReloadClient() { var malCredentials = _redis.HashGetAll("malCredentials"); if (malCredentials.Length != 0) @@ -50,18 +49,18 @@ namespace Geekbot.net.Lib } - public bool isLoggedIn() + public bool IsLoggedIn() { return _credentials != null; } - public async Task getAnime(string query) + public async Task GetAnime(string query) { var response = await _animeSearch.SearchDeserializedAsync(query); return response.Entries.Count == 0 ? null : response.Entries[0]; } - public async Task getManga(string query) + public async Task GetManga(string query) { var response = await _mangaSearch.SearchDeserializedAsync(query); return response.Entries.Count == 0 ? null : response.Entries[0]; @@ -70,9 +69,8 @@ namespace Geekbot.net.Lib public interface IMalClient { - bool reloadClient(); - bool isLoggedIn(); - Task getAnime(string query); - Task getManga(string query); + bool IsLoggedIn(); + Task GetAnime(string query); + Task GetManga(string query); } } \ No newline at end of file diff --git a/Geekbot.net/Lib/Media/FortunesProvider.cs b/Geekbot.net/Lib/Media/FortunesProvider.cs index 8a9b6f0..5988660 100644 --- a/Geekbot.net/Lib/Media/FortunesProvider.cs +++ b/Geekbot.net/Lib/Media/FortunesProvider.cs @@ -1,13 +1,12 @@ using System; using System.IO; -using Serilog; namespace Geekbot.net.Lib.Media { internal class FortunesProvider : IFortunesProvider { - private readonly string[] fortuneArray; - private readonly int totalFortunes; + private readonly string[] _fortuneArray; + private readonly int _totalFortunes; public FortunesProvider(IGeekbotLogger logger) { @@ -15,8 +14,8 @@ namespace Geekbot.net.Lib.Media if (File.Exists(path)) { var rawFortunes = File.ReadAllText(path); - fortuneArray = rawFortunes.Split("%"); - totalFortunes = fortuneArray.Length; + _fortuneArray = rawFortunes.Split("%"); + _totalFortunes = _fortuneArray.Length; logger.Debug("Geekbot", "Loaded {totalFortunes} Fortunes"); } else @@ -27,7 +26,7 @@ namespace Geekbot.net.Lib.Media public string GetRandomFortune() { - return fortuneArray[new Random().Next(0, totalFortunes)]; + return _fortuneArray[new Random().Next(0, _totalFortunes)]; } } diff --git a/Geekbot.net/Lib/Media/MediaProvider.cs b/Geekbot.net/Lib/Media/MediaProvider.cs index b8aeaa3..568f67d 100644 --- a/Geekbot.net/Lib/Media/MediaProvider.cs +++ b/Geekbot.net/Lib/Media/MediaProvider.cs @@ -1,8 +1,5 @@ using System; using System.IO; -using System.Runtime.CompilerServices; -using System.Threading.Tasks; -using Serilog; namespace Geekbot.net.Lib.Media { @@ -92,42 +89,42 @@ namespace Geekbot.net.Lib.Media _logger.Debug("Geekbot", $"Loaded {_foxImages.Length} Foxes Images"); } - public string getCheckem() + public string GetCheckem() { return _checkemImages[_random.Next(0, _checkemImages.Length)]; } - public string getPanda() + public string GetPanda() { return _pandaImages[_random.Next(0, _pandaImages.Length)]; } - public string getCrossant() + public string GetCrossant() { return _croissantImages[_random.Next(0, _croissantImages.Length)]; } - public string getSquirrel() + public string GetSquirrel() { return _squirrelImages[_random.Next(0, _squirrelImages.Length)]; } - public string getPumpkin() + public string GetPumpkin() { return _pumpkinImages[_random.Next(0, _pumpkinImages.Length)]; } - public string getTurtle() + public string GetTurtle() { return _turtlesImages[_random.Next(0, _turtlesImages.Length)]; } - public string getPinguin() + public string GetPinguin() { return _pinguinImages[_random.Next(0, _pinguinImages.Length)]; } - public string getFox() + public string GetFox() { return _foxImages[_random.Next(0, _foxImages.Length)]; } @@ -135,13 +132,13 @@ namespace Geekbot.net.Lib.Media public interface IMediaProvider { - string getCheckem(); - string getPanda(); - string getCrossant(); - string getSquirrel(); - string getPumpkin(); - string getTurtle(); - string getPinguin(); - string getFox(); + string GetCheckem(); + string GetPanda(); + string GetCrossant(); + string GetSquirrel(); + string GetPumpkin(); + string GetTurtle(); + string GetPinguin(); + string GetFox(); } } \ No newline at end of file diff --git a/Geekbot.net/Lib/MtgManaConverter.cs b/Geekbot.net/Lib/MtgManaConverter.cs index 098542e..83b6767 100644 --- a/Geekbot.net/Lib/MtgManaConverter.cs +++ b/Geekbot.net/Lib/MtgManaConverter.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Linq; using System.Text.RegularExpressions; namespace Geekbot.net.Lib diff --git a/Geekbot.net/Lib/ReactionListener.cs b/Geekbot.net/Lib/ReactionListener.cs index f0e75a6..f837280 100644 --- a/Geekbot.net/Lib/ReactionListener.cs +++ b/Geekbot.net/Lib/ReactionListener.cs @@ -33,7 +33,7 @@ namespace Geekbot.net.Lib if (!r.Name.ToString().StartsWith('<')) { var emo = new Emoji(r.Name); - emote = (IEmote) emo; + emote = emo; } else { diff --git a/Geekbot.net/Lib/SimpleConextConverter.cs b/Geekbot.net/Lib/SimpleConextConverter.cs index 3dbc58b..ad029c7 100644 --- a/Geekbot.net/Lib/SimpleConextConverter.cs +++ b/Geekbot.net/Lib/SimpleConextConverter.cs @@ -1,5 +1,4 @@ -using System; -using Discord.Commands; +using Discord.Commands; using Discord.WebSocket; namespace Geekbot.net.Lib @@ -8,9 +7,9 @@ namespace Geekbot.net.Lib { public static MessageDto ConvertContext(ICommandContext context) { - return new MessageDto() + return new MessageDto { - Message = new MessageDto.MessageContent() + Message = new MessageDto.MessageContent { Content = context.Message.Content, Id = context.Message.Id.ToString(), @@ -19,17 +18,17 @@ namespace Geekbot.net.Lib UserMentions = context.Message.MentionedUserIds.Count, RoleMentions = context.Message.MentionedRoleIds.Count }, - User = new MessageDto.IdAndName() + User = new MessageDto.IdAndName { Id = context.User.Id.ToString(), Name = $"{context.User.Username}#{context.User.Discriminator}" }, - Guild = new MessageDto.IdAndName() + Guild = new MessageDto.IdAndName { Id = context.Guild.Id.ToString(), Name = context.Guild.Name }, - Channel = new MessageDto.IdAndName() + Channel = new MessageDto.IdAndName { Id = context.Channel.Id.ToString(), Name = context.Channel.Name @@ -39,9 +38,9 @@ namespace Geekbot.net.Lib public static MessageDto ConvertSocketMessage(SocketMessage message) { var channel = (SocketGuildChannel) message.Channel; - return new MessageDto() + return new MessageDto { - Message = new MessageDto.MessageContent() + Message = new MessageDto.MessageContent { Content = message.Content, Id = message.Id.ToString(), @@ -50,21 +49,21 @@ namespace Geekbot.net.Lib UserMentions = message.MentionedUsers.Count, RoleMentions = message.MentionedRoles.Count }, - User = new MessageDto.IdAndName() + User = new MessageDto.IdAndName { Id = message.Author.Id.ToString(), Name = $"{message.Author.Username}#{message.Author.Discriminator}" }, - Guild = new MessageDto.IdAndName() + Guild = new MessageDto.IdAndName { Id = channel.Guild.Id.ToString(), Name = channel.Guild.Name }, - Channel = new MessageDto.IdAndName() + Channel = new MessageDto.IdAndName { Id = channel.Id.ToString(), Name = channel.Name - }, + } }; } diff --git a/Geekbot.net/Lib/TranslationHandler.cs b/Geekbot.net/Lib/TranslationHandler.cs index cdacac8..5324496 100644 --- a/Geekbot.net/Lib/TranslationHandler.cs +++ b/Geekbot.net/Lib/TranslationHandler.cs @@ -4,8 +4,8 @@ using System.IO; using System.Linq; using Discord.Commands; using Discord.WebSocket; -using Serilog; using StackExchange.Redis; +using Utf8Json; namespace Geekbot.net.Lib { @@ -31,7 +31,7 @@ namespace Geekbot.net.Lib try { var translationFile = File.ReadAllText(Path.GetFullPath("./Storage/Translations.json")); - var rawTranslations = Utf8Json.JsonSerializer.Deserialize>>>(translationFile); + var rawTranslations = JsonSerializer.Deserialize>>>(translationFile); var sortedPerLanguage = new Dictionary>>(); foreach (var command in rawTranslations) { @@ -136,7 +136,7 @@ namespace Geekbot.net.Lib try { if (!_supportedLanguages.Contains(language)) return false; - _redis.HashSet($"{guildId}:Settings", new HashEntry[]{ new HashEntry("Language", language), }); + _redis.HashSet($"{guildId}:Settings", new[]{ new HashEntry("Language", language) }); _serverLanguages[guildId] = language; return true; } diff --git a/Geekbot.net/Lib/UserRepository.cs b/Geekbot.net/Lib/UserRepository.cs index b180ef9..28d0541 100644 --- a/Geekbot.net/Lib/UserRepository.cs +++ b/Geekbot.net/Lib/UserRepository.cs @@ -1,10 +1,7 @@ using System; using System.Collections.Generic; -using System.Globalization; -using System.Linq; using System.Threading.Tasks; using Discord.WebSocket; -using Serilog; using StackExchange.Redis; using Utf8Json; @@ -50,7 +47,7 @@ namespace Geekbot.net.Lib private void Store(UserRepositoryUser user) { - _redis.HashSetAsync($"Users:{user.Id.ToString()}", new HashEntry[] + _redis.HashSetAsync($"Users:{user.Id.ToString()}", new[] { new HashEntry("Id", user.Id.ToString()), new HashEntry("Username", user.Username), @@ -58,7 +55,7 @@ namespace Geekbot.net.Lib new HashEntry("AvatarUrl", user.AvatarUrl), new HashEntry("IsBot", user.IsBot), new HashEntry("Joined", user.Joined.ToString()), - new HashEntry("UsedNames", JsonSerializer.Serialize(user.UsedNames)), + new HashEntry("UsedNames", JsonSerializer.Serialize(user.UsedNames)) }); } @@ -67,7 +64,7 @@ namespace Geekbot.net.Lib try { var user = _redis.HashGetAll($"Users:{userId.ToString()}"); - for (int i = 1; i < 11; i++) + for (var i = 1; i < 11; i++) { if (user.Length != 0) break; user = _redis.HashGetAll($"Users:{(userId + (ulong) i).ToString()}"); @@ -88,7 +85,7 @@ namespace Geekbot.net.Lib dto.Discriminator = a.Value.ToString(); break; case "AvatarUrl": - dto.AvatarUrl = (a.Value != "0") ? a.Value.ToString() : null; + dto.AvatarUrl = a.Value != "0" ? a.Value.ToString() : null; break; case "IsBot": dto.IsBot = a.Value == 1; @@ -110,14 +107,14 @@ namespace Geekbot.net.Lib } } - public string getUserSetting(ulong userId, string setting) + public string GetUserSetting(ulong userId, string setting) { return _redis.HashGet($"Users:{userId}", setting); } - public bool saveUserSetting(ulong userId, string setting, string value) + public bool SaveUserSetting(ulong userId, string setting, string value) { - _redis.HashSet($"Users:{userId}", new HashEntry[] + _redis.HashSet($"Users:{userId}", new[] { new HashEntry(setting, value) }); @@ -140,7 +137,7 @@ namespace Geekbot.net.Lib { Task Update(SocketUser user); UserRepositoryUser Get(ulong userId); - string getUserSetting(ulong userId, string setting); - bool saveUserSetting(ulong userId, string setting, string value); + string GetUserSetting(ulong userId, string setting); + bool SaveUserSetting(ulong userId, string setting, string value); } } \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index b805e5f..0a107e9 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -18,16 +18,16 @@ namespace Geekbot.net { internal class Program { - private DiscordSocketClient client; - private CommandService commands; - private IDatabase redis; - private IServiceCollection services; - private IServiceProvider servicesProvider; - private RedisValue token; - private IGeekbotLogger logger; - private IUserRepository userRepository; - private string[] args; - private bool firstStart = false; + private DiscordSocketClient _client; + private CommandService _commands; + private IDatabase _redis; + private IServiceCollection _services; + private IServiceProvider _servicesProvider; + private RedisValue _token; + private IGeekbotLogger _logger; + private IUserRepository _userRepository; + private string[] _args; + private bool _firstStart; private static void Main(string[] args) { @@ -53,23 +53,23 @@ namespace Geekbot.net private async Task MainAsync(string[] args, IGeekbotLogger logger) { - this.logger = logger; - this.args = args; + _logger = logger; + _args = args; logger.Information("Geekbot", "Initing Stuff"); - client = new DiscordSocketClient(new DiscordSocketConfig + _client = new DiscordSocketClient(new DiscordSocketConfig { LogLevel = LogSeverity.Verbose, MessageCacheSize = 1000 }); - client.Log += DiscordLogger; - commands = new CommandService(); + _client.Log += DiscordLogger; + _commands = new CommandService(); try { var redisMultiplexer = ConnectionMultiplexer.Connect("127.0.0.1:6379"); - redis = redisMultiplexer.GetDatabase(6); - logger.Information("Redis", $"Connected to db {redis.Database}"); + _redis = redisMultiplexer.GetDatabase(6); + logger.Information("Redis", $"Connected to db {_redis.Database}"); } catch (Exception e) { @@ -77,40 +77,38 @@ namespace Geekbot.net Environment.Exit(102); } - token = redis.StringGet("discordToken"); - if (token.IsNullOrEmpty) + _token = _redis.StringGet("discordToken"); + if (_token.IsNullOrEmpty) { Console.Write("Your bot Token: "); var newToken = Console.ReadLine(); - redis.StringSet("discordToken", newToken); - redis.StringSet("Game", "Ping Pong"); - token = newToken; - firstStart = true; + _redis.StringSet("discordToken", newToken); + _redis.StringSet("Game", "Ping Pong"); + _token = newToken; + _firstStart = true; } - services = new ServiceCollection(); + _services = new ServiceCollection(); - userRepository = new UserRepository(redis, logger); + _userRepository = new UserRepository(_redis, logger); var fortunes = new FortunesProvider(logger); var mediaProvider = new MediaProvider(logger); - var malClient = new MalClient(redis, logger); + var malClient = new MalClient(_redis, logger); var levelCalc = new LevelCalc(); var emojiConverter = new EmojiConverter(); - var audioUtils = new AudioUtils(); var mtgManaConverter = new MtgManaConverter(); var wikipediaClient = new WikipediaClient(); - services.AddSingleton(redis); - services.AddSingleton(logger); - services.AddSingleton(userRepository); - services.AddSingleton(levelCalc); - services.AddSingleton(emojiConverter); - services.AddSingleton(audioUtils); - services.AddSingleton(fortunes); - services.AddSingleton(mediaProvider); - services.AddSingleton(malClient); - services.AddSingleton(mtgManaConverter); - services.AddSingleton(wikipediaClient); + _services.AddSingleton(_redis); + _services.AddSingleton(logger); + _services.AddSingleton(_userRepository); + _services.AddSingleton(levelCalc); + _services.AddSingleton(emojiConverter); + _services.AddSingleton(fortunes); + _services.AddSingleton(mediaProvider); + _services.AddSingleton(malClient); + _services.AddSingleton(mtgManaConverter); + _services.AddSingleton(wikipediaClient); logger.Information("Geekbot", "Connecting to Discord"); @@ -123,94 +121,94 @@ namespace Geekbot.net { try { - await client.LoginAsync(TokenType.Bot, token); - await client.StartAsync(); - var isConneted = await isConnected(); + await _client.LoginAsync(TokenType.Bot, _token); + await _client.StartAsync(); + var isConneted = await IsConnected(); if (isConneted) { - await client.SetGameAsync(redis.StringGet("Game")); - logger.Information("Geekbot", $"Now Connected as {client.CurrentUser.Username} to {client.Guilds.Count} Servers"); + await _client.SetGameAsync(_redis.StringGet("Game")); + _logger.Information("Geekbot", $"Now Connected as {_client.CurrentUser.Username} to {_client.Guilds.Count} Servers"); - logger.Information("Geekbot", "Registering Stuff"); - var translationHandler = new TranslationHandler(client.Guilds, redis, logger); - var errorHandler = new ErrorHandler(logger, translationHandler, args.Contains("--expose-errors")); - var reactionListener = new ReactionListener(redis); - await commands.AddModulesAsync(Assembly.GetEntryAssembly()); - services.AddSingleton(commands); - services.AddSingleton(errorHandler); - services.AddSingleton(translationHandler); - services.AddSingleton(client); - services.AddSingleton(reactionListener); - servicesProvider = services.BuildServiceProvider(); + _logger.Information("Geekbot", "Registering Stuff"); + var translationHandler = new TranslationHandler(_client.Guilds, _redis, _logger); + var errorHandler = new ErrorHandler(_logger, translationHandler, _args.Contains("--expose-errors")); + var reactionListener = new ReactionListener(_redis); + await _commands.AddModulesAsync(Assembly.GetEntryAssembly()); + _services.AddSingleton(_commands); + _services.AddSingleton(errorHandler); + _services.AddSingleton(translationHandler); + _services.AddSingleton(_client); + _services.AddSingleton(reactionListener); + _servicesProvider = _services.BuildServiceProvider(); - var handlers = new Handlers(client, logger, redis, servicesProvider, commands, userRepository, reactionListener); + var handlers = new Handlers(_client, _logger, _redis, _servicesProvider, _commands, _userRepository, reactionListener); - client.MessageReceived += handlers.RunCommand; - client.MessageReceived += handlers.UpdateStats; - client.MessageDeleted += handlers.MessageDeleted; - client.UserJoined += handlers.UserJoined; - client.UserUpdated += handlers.UserUpdated; - client.UserLeft += handlers.UserLeft; - client.ReactionAdded += handlers.ReactionAdded; - client.ReactionRemoved += handlers.ReactionRemoved; + _client.MessageReceived += handlers.RunCommand; + _client.MessageReceived += handlers.UpdateStats; + _client.MessageDeleted += handlers.MessageDeleted; + _client.UserJoined += handlers.UserJoined; + _client.UserUpdated += handlers.UserUpdated; + _client.UserLeft += handlers.UserLeft; + _client.ReactionAdded += handlers.ReactionAdded; + _client.ReactionRemoved += handlers.ReactionRemoved; - if (firstStart || args.Contains("--reset")) + if (_firstStart || _args.Contains("--reset")) { - logger.Information("Geekbot", "Finishing setup"); + _logger.Information("Geekbot", "Finishing setup"); await FinishSetup(); - logger.Information("Geekbot", "Setup finished"); + _logger.Information("Geekbot", "Setup finished"); } - if (!args.Contains("--disable-api")) + if (!_args.Contains("--disable-api")) { - startWebApi(); + StartWebApi(); } - logger.Information("Geekbot", "Done and ready for use"); + _logger.Information("Geekbot", "Done and ready for use"); } } catch (Exception e) { - logger.Error("Discord", "Could not connect...", e); + _logger.Error("Discord", "Could not connect...", e); Environment.Exit(103); } } - private async Task isConnected() + private async Task IsConnected() { - while (!client.ConnectionState.Equals(ConnectionState.Connected)) + while (!_client.ConnectionState.Equals(ConnectionState.Connected)) await Task.Delay(25); return true; } - private void startWebApi() + private void StartWebApi() { - logger.Information("API", "Starting Webserver"); + _logger.Information("API", "Starting Webserver"); var webApiUrl = new Uri("http://localhost:12995"); new NancyHost(webApiUrl).Start(); - logger.Information("API", $"Webserver now running on {webApiUrl}"); + _logger.Information("API", $"Webserver now running on {webApiUrl}"); } private async Task FinishSetup() { - var appInfo = await client.GetApplicationInfoAsync(); - logger.Information("Setup", $"Just a moment while i setup everything {appInfo.Owner.Username}"); + var appInfo = await _client.GetApplicationInfoAsync(); + _logger.Information("Setup", $"Just a moment while i setup everything {appInfo.Owner.Username}"); try { - redis.StringSet("botOwner", appInfo.Owner.Id); - var req = HttpWebRequest.Create(appInfo.IconUrl); + _redis.StringSet("botOwner", appInfo.Owner.Id); + var req = WebRequest.Create(appInfo.IconUrl); using (var stream = req.GetResponse().GetResponseStream()) { - await client.CurrentUser.ModifyAsync(User => + await _client.CurrentUser.ModifyAsync(user => { - User.Avatar = new Image(stream); - User.Username = appInfo.Name.ToString(); + user.Avatar = new Image(stream); + user.Username = appInfo.Name.ToString(); }); } - logger.Information("Setup", "Everything done, enjoy!"); + _logger.Information("Setup", "Everything done, enjoy!"); } catch (Exception e) { - logger.Warning("Setup", "Oha, it seems like something went wrong while running the setup, geekbot will work never the less though", e); + _logger.Warning("Setup", "Oha, it seems like something went wrong while running the setup, geekbot will work never the less though", e); } return Task.CompletedTask; } @@ -222,19 +220,19 @@ namespace Geekbot.net { case LogSeverity.Verbose: case LogSeverity.Debug: - logger.Debug(message.Source, message.Message); + _logger.Debug(message.Source, message.Message); break; case LogSeverity.Info: - logger.Information(message.Source, message.Message); + _logger.Information(message.Source, message.Message); break; case LogSeverity.Critical: case LogSeverity.Error: case LogSeverity.Warning: if (logMessage.Contains("VOICE_STATE_UPDATE")) break; - logger.Error(message.Source, message.Message, message.Exception); + _logger.Error(message.Source, message.Message, message.Exception); break; default: - logger.Information(message.Source, $"{logMessage} --- {message.Severity}"); + _logger.Information(message.Source, $"{logMessage} --- {message.Severity}"); break; } return Task.CompletedTask; diff --git a/Geekbot.net/WebApi/HelpController.cs b/Geekbot.net/WebApi/HelpController.cs index 6146352..060dea0 100644 --- a/Geekbot.net/WebApi/HelpController.cs +++ b/Geekbot.net/WebApi/HelpController.cs @@ -3,10 +3,9 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Threading.Tasks; -using Discord; using Discord.Commands; -using Nancy; using Geekbot.net.Lib; +using Nancy; namespace Geekbot.net.WebApi { @@ -16,25 +15,18 @@ namespace Geekbot.net.WebApi { Get("/v1/commands", args => { - var commands = getCommands().Result; + var commands = GetCommands().Result; - var commandList = new List(); - foreach (var cmd in commands.Commands) - { - var cmdParamsObj = new List(); - foreach (var cmdParam in cmd.Parameters) - { - var singleParamObj = new CommandParamDto() + var commandList = (from cmd in commands.Commands + let cmdParamsObj = cmd.Parameters.Select(cmdParam => new CommandParamDto { Summary = cmdParam.Summary, - Default = cmdParam?.DefaultValue?.ToString() ?? null, - Type = cmdParam?.Type?.ToString() - }; - cmdParamsObj.Add(singleParamObj); - } - - var param = string.Join(", !", cmd.Aliases); - var cmdObj = new CommandDto() + Default = cmdParam.DefaultValue?.ToString() ?? null, + Type = cmdParam.Type?.ToString() + }) + .ToList() + let param = string.Join(", !", cmd.Aliases) + select new CommandDto { Name = cmd.Name, Summary = cmd.Summary, @@ -42,15 +34,13 @@ namespace Geekbot.net.WebApi IsAdminCommand = (param.Contains("admin")), Aliases = cmd.Aliases.ToArray(), Params = cmdParamsObj - }; - commandList.Add(cmdObj); - } + }).ToList(); return Response.AsJson(commandList); }); } - private async Task getCommands() + private async Task GetCommands() { var commands = new CommandService(); await commands.AddModulesAsync(Assembly.GetEntryAssembly()); diff --git a/Geekbot.net/WebApi/StatusController.cs b/Geekbot.net/WebApi/StatusController.cs index 2fd84f8..b785e07 100644 --- a/Geekbot.net/WebApi/StatusController.cs +++ b/Geekbot.net/WebApi/StatusController.cs @@ -1,5 +1,5 @@ -using Nancy; -using Geekbot.net.Lib; +using Geekbot.net.Lib; +using Nancy; namespace Geekbot.net.WebApi { @@ -9,7 +9,7 @@ namespace Geekbot.net.WebApi { Get("/", args => { - var responseBody = new ApiStatusDto() + var responseBody = new ApiStatusDto { GeekbotVersion = Constants.BotVersion.ToString(), ApiVersion = Constants.ApiVersion.ToString(), diff --git a/Tests/Lib/EmojiConverter.test.cs b/Tests/Lib/EmojiConverter.test.cs index b9e7b4f..4bfcf06 100644 --- a/Tests/Lib/EmojiConverter.test.cs +++ b/Tests/Lib/EmojiConverter.test.cs @@ -1,11 +1,10 @@ using System.Collections.Generic; -using System.Threading.Tasks; using Geekbot.net.Lib; using Xunit; namespace Tests.Lib { - public class EmojiConverter_test + public class EmojiConverterTest { public static IEnumerable NumberToEmojiTestData { @@ -42,11 +41,11 @@ namespace Tests.Lib public void NumberToEmoji(int number, string expectedResult) { var emojiConverter = new EmojiConverter(); - var result = emojiConverter.numberToEmoji(number); + var result = emojiConverter.NumberToEmoji(number); Assert.Equal(result, expectedResult); } - public static IEnumerable textToEmojiTestData + public static IEnumerable TextToEmojiTestData { get { @@ -64,11 +63,11 @@ namespace Tests.Lib } - [Theory, MemberData(nameof(textToEmojiTestData))] + [Theory, MemberData(nameof(TextToEmojiTestData))] public void TextToEmoji(string text, string expectedResult) { var emojiConverter = new EmojiConverter(); - var result = emojiConverter.textToEmoji(text); + var result = emojiConverter.TextToEmoji(text); Assert.Equal(result, expectedResult); } } diff --git a/Tests/Lib/LevelCalc.test.cs b/Tests/Lib/LevelCalc.test.cs index 7c00392..ecca151 100644 --- a/Tests/Lib/LevelCalc.test.cs +++ b/Tests/Lib/LevelCalc.test.cs @@ -1,11 +1,10 @@ using System.Collections.Generic; -using System.Threading.Tasks; using Geekbot.net.Lib; using Xunit; namespace Tests.Lib { - public class LevelCalc_test + public class LevelCalcTest { public static IEnumerable LevelCalcTestData { diff --git a/WikipediaApi/Page/PageCoordinates.cs b/WikipediaApi/Page/PageCoordinates.cs index f86bb09..7fa42ba 100644 --- a/WikipediaApi/Page/PageCoordinates.cs +++ b/WikipediaApi/Page/PageCoordinates.cs @@ -2,7 +2,7 @@ { public class PageCoordinates { - public float lat { get; set; } - public float lon { get; set; } + public float Lat { get; set; } + public float Lon { get; set; } } } \ No newline at end of file diff --git a/WikipediaApi/Page/PagePreview.cs b/WikipediaApi/Page/PagePreview.cs index c1de2d7..8db9dad 100644 --- a/WikipediaApi/Page/PagePreview.cs +++ b/WikipediaApi/Page/PagePreview.cs @@ -1,5 +1,4 @@ using System; -using System.Runtime.Serialization; using Newtonsoft.Json; using Newtonsoft.Json.Converters; @@ -18,7 +17,7 @@ namespace WikipediaApi.Page public string Displaytitle { get; set; } [JsonProperty("namespace")] - public PageNamespace @Namespace { get; set; } + public PageNamespace Namespace { get; set; } [JsonProperty("titles")] public PageTitles Titles { get; set; } diff --git a/WikipediaApi/WikipediaClient.cs b/WikipediaApi/WikipediaClient.cs index 161cbce..6576f3d 100644 --- a/WikipediaApi/WikipediaClient.cs +++ b/WikipediaApi/WikipediaClient.cs @@ -1,5 +1,4 @@ -using System; -using System.Net.Http; +using System.Net.Http; using System.Threading.Tasks; using Newtonsoft.Json; using WikipediaApi.Page; From 276e9bfe8e19a6b5bb4adc4f48e0e2bfeba2bd29 Mon Sep 17 00:00:00 2001 From: runebaas Date: Tue, 1 May 2018 00:19:03 +0200 Subject: [PATCH 032/443] add null checking in !urban --- Geekbot.net/Commands/UrbanDictionary.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Geekbot.net/Commands/UrbanDictionary.cs b/Geekbot.net/Commands/UrbanDictionary.cs index a951cb8..2af43ba 100644 --- a/Geekbot.net/Commands/UrbanDictionary.cs +++ b/Geekbot.net/Commands/UrbanDictionary.cs @@ -49,10 +49,10 @@ namespace Geekbot.net.Commands Url = definition.Permalink }); eb.WithColor(new Color(239, 255, 0)); - eb.Description = definition.Definition; - eb.AddField("Example", definition.Example ?? "(no example given...)"); - eb.AddInlineField("Upvotes", definition.ThumbsUp); - eb.AddInlineField("Downvotes", definition.ThumbsDown); + if (!string.IsNullOrEmpty(definition.Definition)) eb.Description = definition.Definition; + if (!string.IsNullOrEmpty(definition.Example)) eb.AddField("Example", definition.Example ?? "(no example given...)"); + if (!string.IsNullOrEmpty(definition.ThumbsUp)) eb.AddInlineField("Upvotes", definition.ThumbsUp); + if (!string.IsNullOrEmpty(definition.ThumbsDown)) eb.AddInlineField("Downvotes", definition.ThumbsDown); if (definitions.Tags.Length > 0) eb.AddField("Tags", string.Join(", ", definitions.Tags)); await ReplyAsync("", false, eb.Build()); From fa5797cd22b7c08766a062713f461e54a3637a73 Mon Sep 17 00:00:00 2001 From: runebaas Date: Tue, 1 May 2018 02:31:54 +0200 Subject: [PATCH 033/443] use newtonsoft in logger --- Geekbot.net/Lib/GeekbotLogger.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Geekbot.net/Lib/GeekbotLogger.cs b/Geekbot.net/Lib/GeekbotLogger.cs index b5214b0..5ae65d6 100644 --- a/Geekbot.net/Lib/GeekbotLogger.cs +++ b/Geekbot.net/Lib/GeekbotLogger.cs @@ -1,7 +1,7 @@ using System; using System.Threading.Tasks; +using Newtonsoft.Json; using Serilog; -using Utf8Json; namespace Geekbot.net.Lib { @@ -11,7 +11,6 @@ namespace Geekbot.net.Lib public GeekbotLogger() { _serilog = LoggerFactory.CreateLogger(); - //JsonSerializer.SetDefaultResolver(StandardResolver.AllowPrivateExcludeNullSnakeCase); Information("Geekbot", "Using GeekbotLogger"); } @@ -54,7 +53,7 @@ namespace Geekbot.net.Lib StackTrace = stackTrace, Extra = extra }; - return JsonSerializer.ToJsonString(logObject); + return JsonConvert.SerializeObject(logObject); } } From f3156176dd1cc6bc1a95f0a19324e26bced11f77 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Tue, 1 May 2018 16:50:48 +0200 Subject: [PATCH 034/443] More explicit logging configuration --- Geekbot.net/Commands/RandomAnimals.cs | 4 ++-- Geekbot.net/Lib/GeekbotLogger.cs | 19 +++++++++++++------ Geekbot.net/Lib/LoggerFactory.cs | 4 ++-- Geekbot.net/Program.cs | 3 ++- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/Geekbot.net/Commands/RandomAnimals.cs b/Geekbot.net/Commands/RandomAnimals.cs index 2101c33..7829b21 100644 --- a/Geekbot.net/Commands/RandomAnimals.cs +++ b/Geekbot.net/Commands/RandomAnimals.cs @@ -59,7 +59,7 @@ namespace Geekbot.net.Commands [Command("pinguin", RunMode = RunMode.Async)] [Alias("pingu")] [Remarks(CommandCategories.Randomness)] - [Summary("Get a random turtle image")] + [Summary("Get a random pinguin image")] public async Task Pinguin() { await ReplyAsync("", false, Eb(_mediaProvider.GetPinguin())); @@ -67,7 +67,7 @@ namespace Geekbot.net.Commands [Command("fox", RunMode = RunMode.Async)] [Remarks(CommandCategories.Randomness)] - [Summary("Get a random turtle image")] + [Summary("Get a random fox image")] public async Task Fox() { await ReplyAsync("", false, Eb(_mediaProvider.GetFox())); diff --git a/Geekbot.net/Lib/GeekbotLogger.cs b/Geekbot.net/Lib/GeekbotLogger.cs index 5ae65d6..a9fb3a1 100644 --- a/Geekbot.net/Lib/GeekbotLogger.cs +++ b/Geekbot.net/Lib/GeekbotLogger.cs @@ -1,5 +1,4 @@ using System; -using System.Threading.Tasks; using Newtonsoft.Json; using Serilog; @@ -8,9 +7,18 @@ namespace Geekbot.net.Lib public class GeekbotLogger : IGeekbotLogger { private readonly ILogger _serilog; - public GeekbotLogger() + private readonly JsonSerializerSettings _serializerSettings; + private readonly Formatting _jsonFormatting; + + public GeekbotLogger(bool sumologicActive) { - _serilog = LoggerFactory.CreateLogger(); + _serilog = LoggerFactory.CreateLogger(sumologicActive); + _serializerSettings = new JsonSerializerSettings + { + ReferenceLoopHandling = ReferenceLoopHandling.Serialize, + NullValueHandling = NullValueHandling.Include + }; + _jsonFormatting = sumologicActive ? Formatting.None : Formatting.Indented; Information("Geekbot", "Using GeekbotLogger"); } @@ -34,12 +42,11 @@ namespace Geekbot.net.Lib HandleLogObject("Error", source, message, stackTrace, extra); } - private Task HandleLogObject(string type, string source, string message, Exception stackTrace = null, object extra = null) + private void HandleLogObject(string type, string source, string message, Exception stackTrace = null, object extra = null) { var logJson = CreateLogObject(type, source, message, stackTrace, extra); // fuck serilog _serilog.Information(logJson + "}"); - return Task.CompletedTask; } private string CreateLogObject(string type, string source, string message, Exception stackTrace = null, object extra = null) @@ -53,7 +60,7 @@ namespace Geekbot.net.Lib StackTrace = stackTrace, Extra = extra }; - return JsonConvert.SerializeObject(logObject); + return JsonConvert.SerializeObject(logObject, _jsonFormatting, _serializerSettings); } } diff --git a/Geekbot.net/Lib/LoggerFactory.cs b/Geekbot.net/Lib/LoggerFactory.cs index 9bff873..24df1ab 100644 --- a/Geekbot.net/Lib/LoggerFactory.cs +++ b/Geekbot.net/Lib/LoggerFactory.cs @@ -6,11 +6,11 @@ namespace Geekbot.net.Lib { public class LoggerFactory { - public static ILogger CreateLogger() + public static ILogger CreateLogger(bool sumologicActive) { var loggerCreation = new LoggerConfiguration(); var template = "{Message}{NewLine}"; - if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GEEKBOT_SUMO"))) + if (sumologicActive) { Console.WriteLine("Logging Geekbot Logs to Sumologic"); loggerCreation.WriteTo.SumoLogic(Environment.GetEnvironmentVariable("GEEKBOT_SUMO"), diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 0a107e9..10f0dcb 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -39,7 +39,8 @@ namespace Geekbot.net logo.AppendLine(@" \____|_____|_____|_|\_\____/ \___/ |_|"); logo.AppendLine("========================================="); Console.WriteLine(logo.ToString()); - var logger = new GeekbotLogger(); + var sumologicActive = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GEEKBOT_SUMO")); + var logger = new GeekbotLogger(sumologicActive); logger.Information("Geekbot", "Starting..."); try { From ec084a124bae82a0019baa9b0a882411ca0a9033 Mon Sep 17 00:00:00 2001 From: runebaas Date: Tue, 1 May 2018 21:07:27 +0200 Subject: [PATCH 035/443] Only format as json when logging to sumologic --- Geekbot.net/Lib/GeekbotLogger.cs | 44 +++++++++++++++++++------------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/Geekbot.net/Lib/GeekbotLogger.cs b/Geekbot.net/Lib/GeekbotLogger.cs index a9fb3a1..ec88290 100644 --- a/Geekbot.net/Lib/GeekbotLogger.cs +++ b/Geekbot.net/Lib/GeekbotLogger.cs @@ -6,61 +6,69 @@ namespace Geekbot.net.Lib { public class GeekbotLogger : IGeekbotLogger { + private readonly bool _sumologicActive; private readonly ILogger _serilog; private readonly JsonSerializerSettings _serializerSettings; - private readonly Formatting _jsonFormatting; public GeekbotLogger(bool sumologicActive) { + _sumologicActive = sumologicActive; _serilog = LoggerFactory.CreateLogger(sumologicActive); _serializerSettings = new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Serialize, NullValueHandling = NullValueHandling.Include }; - _jsonFormatting = sumologicActive ? Formatting.None : Formatting.Indented; Information("Geekbot", "Using GeekbotLogger"); } public void Debug(string source, string message, object extra = null) { - HandleLogObject("Debug", source, message, null, extra); + _serilog.Debug(CreateLogString("Debug", source, message, null, extra)); } public void Information(string source, string message, object extra = null) { - HandleLogObject("Information", source, message, null, extra); + _serilog.Information(CreateLogString("Information", source, message, null, extra)); } public void Warning(string source, string message, Exception stackTrace = null, object extra = null) { - HandleLogObject("Warning", source, message, stackTrace, extra); + _serilog.Warning(CreateLogString("Warning", source, message, stackTrace, extra)); } public void Error(string source, string message, Exception stackTrace, object extra = null) { - HandleLogObject("Error", source, message, stackTrace, extra); + _serilog.Error(CreateLogString("Error", source, message, stackTrace, extra)); } private void HandleLogObject(string type, string source, string message, Exception stackTrace = null, object extra = null) { - var logJson = CreateLogObject(type, source, message, stackTrace, extra); + var logJson = CreateLogString(type, source, message, stackTrace, extra); // fuck serilog - _serilog.Information(logJson + "}"); + _serilog.Information(logJson, stackTrace + "}"); } - private string CreateLogObject(string type, string source, string message, Exception stackTrace = null, object extra = null) + private string CreateLogString(string type, string source, string message, Exception stackTrace = null, object extra = null) { - var logObject = new GeekbotLoggerObject + if (_sumologicActive) { - Timestamp = DateTime.Now, - Type = type, - Source = source, - Message = message, - StackTrace = stackTrace, - Extra = extra - }; - return JsonConvert.SerializeObject(logObject, _jsonFormatting, _serializerSettings); + var logObject = new GeekbotLoggerObject + { + Timestamp = DateTime.Now, + Type = type, + Source = source, + Message = message, + StackTrace = stackTrace, + Extra = extra + }; + return JsonConvert.SerializeObject(logObject, Formatting.None, _serializerSettings); + } + + if (source != "Message") return $"[{source}] - {message}"; + + var m = (MessageDto) extra; + return $"[{source}] - [{m.Guild.Name} - {m.Channel.Name}] {m.User.Name}: {m.Message.Content}"; } } From d3fcfc8a5c4735b419b5545bacc3416859803d61 Mon Sep 17 00:00:00 2001 From: runebaas Date: Tue, 1 May 2018 22:55:47 +0200 Subject: [PATCH 036/443] Replace Serilog with NLog --- Geekbot.net/Geekbot.net.csproj | 8 ++-- Geekbot.net/Lib/GeekbotLogger.cs | 20 +++------- Geekbot.net/Lib/LoggerFactory.cs | 63 +++++++++++++++++++++++++++----- 3 files changed, 62 insertions(+), 29 deletions(-) diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index c91512f..d943a8e 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -24,17 +24,15 @@ + + - - - - - 1.2.6 + diff --git a/Geekbot.net/Lib/GeekbotLogger.cs b/Geekbot.net/Lib/GeekbotLogger.cs index ec88290..c4a792b 100644 --- a/Geekbot.net/Lib/GeekbotLogger.cs +++ b/Geekbot.net/Lib/GeekbotLogger.cs @@ -1,19 +1,18 @@ using System; using Newtonsoft.Json; -using Serilog; namespace Geekbot.net.Lib { public class GeekbotLogger : IGeekbotLogger { private readonly bool _sumologicActive; - private readonly ILogger _serilog; + private readonly NLog.Logger _logger; private readonly JsonSerializerSettings _serializerSettings; public GeekbotLogger(bool sumologicActive) { _sumologicActive = sumologicActive; - _serilog = LoggerFactory.CreateLogger(sumologicActive); + _logger = LoggerFactory.CreateNLog(sumologicActive); _serializerSettings = new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Serialize, @@ -24,29 +23,22 @@ namespace Geekbot.net.Lib public void Debug(string source, string message, object extra = null) { - _serilog.Debug(CreateLogString("Debug", source, message, null, extra)); + _logger.Debug(CreateLogString("Debug", source, message, null, extra)); } public void Information(string source, string message, object extra = null) { - _serilog.Information(CreateLogString("Information", source, message, null, extra)); + _logger.Info(CreateLogString("Information", source, message, null, extra)); } public void Warning(string source, string message, Exception stackTrace = null, object extra = null) { - _serilog.Warning(CreateLogString("Warning", source, message, stackTrace, extra)); + _logger.Warn(CreateLogString("Warning", source, message, stackTrace, extra)); } public void Error(string source, string message, Exception stackTrace, object extra = null) { - _serilog.Error(CreateLogString("Error", source, message, stackTrace, extra)); - } - - private void HandleLogObject(string type, string source, string message, Exception stackTrace = null, object extra = null) - { - var logJson = CreateLogString(type, source, message, stackTrace, extra); - // fuck serilog - _serilog.Information(logJson, stackTrace + "}"); + _logger.Error(stackTrace, CreateLogString("Error", source, message, stackTrace, extra)); } private string CreateLogString(string type, string source, string message, Exception stackTrace = null, object extra = null) diff --git a/Geekbot.net/Lib/LoggerFactory.cs b/Geekbot.net/Lib/LoggerFactory.cs index 24df1ab..8631c11 100644 --- a/Geekbot.net/Lib/LoggerFactory.cs +++ b/Geekbot.net/Lib/LoggerFactory.cs @@ -1,27 +1,70 @@ using System; -using Serilog; -using Serilog.Sinks.SumoLogic; +using System.Text; +using NLog; +using NLog.Config; +using NLog.Targets; +using SumoLogic.Logging.NLog; namespace Geekbot.net.Lib { public class LoggerFactory { - public static ILogger CreateLogger(bool sumologicActive) + public static Logger CreateNLog(bool sumologicActive) { - var loggerCreation = new LoggerConfiguration(); - var template = "{Message}{NewLine}"; + var config = new LoggingConfiguration(); + if (sumologicActive) { Console.WriteLine("Logging Geekbot Logs to Sumologic"); - loggerCreation.WriteTo.SumoLogic(Environment.GetEnvironmentVariable("GEEKBOT_SUMO"), - outputTemplate: template); + config.LoggingRules.Add( + new LoggingRule("*", LogLevel.Info, LogLevel.Fatal, + new BufferedSumoLogicTarget() + { + Url = Environment.GetEnvironmentVariable("GEEKBOT_SUMO"), + SourceName = "GeekbotLogger", + Layout = "${message}", + UseConsoleLog = false, + MaxQueueSizeBytes = 500000, + FlushingAccuracy = 250, + MaxFlushInterval = 10000, + OptimizeBufferReuse = true, + MessagesPerRequest = 10, + RetryInterval = 5000, + Name = "Geekbot" + }) + ); } else { - loggerCreation.WriteTo.LiterateConsole(outputTemplate: template); - loggerCreation.WriteTo.RollingFile("Logs/geekbot-{Date}.txt", shared: true, outputTemplate: template); + config.LoggingRules.Add( + new LoggingRule("*", LogLevel.Trace, LogLevel.Fatal, + new ColoredConsoleTarget + { + Name = "Console", + Encoding = Encoding.Unicode, + Layout = "[${longdate} ${level:format=FirstCharacter}] ${message} ${exception:format=toString}" + }) + ); + + config.LoggingRules.Add( + new LoggingRule("*", LogLevel.Trace, LogLevel.Fatal, + new FileTarget + { + Name = "File", + Layout = "[${longdate} ${level}] ${message}", + Encoding = Encoding.Unicode, + LineEnding = LineEndingMode.Default, + MaxArchiveFiles = 30, + ArchiveNumbering = ArchiveNumberingMode.Date, + ArchiveEvery = FileArchivePeriod.Day, + ArchiveFileName = "./Logs/Archive/{#####}.log", + FileName = "./Logs/Geekbot.log" + }) + ); } - return loggerCreation.CreateLogger(); + + var loggerConfig = new LogFactory { Configuration = config }; + return loggerConfig.GetCurrentClassLogger(); } } } \ No newline at end of file From 2701f641cf253e0410cdb2f8f7011fc14344260c Mon Sep 17 00:00:00 2001 From: Runebaas Date: Wed, 2 May 2018 20:19:11 +0200 Subject: [PATCH 037/443] Add commandline parser --- Geekbot.net/Geekbot.net.csproj | 1 + Geekbot.net/Lib/GeekbotLogger.cs | 4 ++-- Geekbot.net/Lib/LoggerFactory.cs | 7 ++++--- Geekbot.net/Lib/RunParameters.cs | 23 +++++++++++++++++++++++ Geekbot.net/Program.cs | 22 ++++++++++++++-------- 5 files changed, 44 insertions(+), 13 deletions(-) create mode 100644 Geekbot.net/Lib/RunParameters.cs diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index d943a8e..cf7e5f9 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -11,6 +11,7 @@ NU1701 + 1.0.2 diff --git a/Geekbot.net/Lib/GeekbotLogger.cs b/Geekbot.net/Lib/GeekbotLogger.cs index c4a792b..aae0978 100644 --- a/Geekbot.net/Lib/GeekbotLogger.cs +++ b/Geekbot.net/Lib/GeekbotLogger.cs @@ -9,10 +9,10 @@ namespace Geekbot.net.Lib private readonly NLog.Logger _logger; private readonly JsonSerializerSettings _serializerSettings; - public GeekbotLogger(bool sumologicActive) + public GeekbotLogger(RunParameters runParameters, bool sumologicActive) { _sumologicActive = sumologicActive; - _logger = LoggerFactory.CreateNLog(sumologicActive); + _logger = LoggerFactory.CreateNLog(runParameters, sumologicActive); _serializerSettings = new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Serialize, diff --git a/Geekbot.net/Lib/LoggerFactory.cs b/Geekbot.net/Lib/LoggerFactory.cs index 8631c11..b0b5060 100644 --- a/Geekbot.net/Lib/LoggerFactory.cs +++ b/Geekbot.net/Lib/LoggerFactory.cs @@ -9,7 +9,7 @@ namespace Geekbot.net.Lib { public class LoggerFactory { - public static Logger CreateNLog(bool sumologicActive) + public static Logger CreateNLog(RunParameters runParameters, bool sumologicActive) { var config = new LoggingConfiguration(); @@ -36,8 +36,9 @@ namespace Geekbot.net.Lib } else { + var minLevel = runParameters.Verbose ? LogLevel.Trace : LogLevel.Info; config.LoggingRules.Add( - new LoggingRule("*", LogLevel.Trace, LogLevel.Fatal, + new LoggingRule("*", minLevel, LogLevel.Fatal, new ColoredConsoleTarget { Name = "Console", @@ -47,7 +48,7 @@ namespace Geekbot.net.Lib ); config.LoggingRules.Add( - new LoggingRule("*", LogLevel.Trace, LogLevel.Fatal, + new LoggingRule("*", minLevel, LogLevel.Fatal, new FileTarget { Name = "File", diff --git a/Geekbot.net/Lib/RunParameters.cs b/Geekbot.net/Lib/RunParameters.cs new file mode 100644 index 0000000..0549ac5 --- /dev/null +++ b/Geekbot.net/Lib/RunParameters.cs @@ -0,0 +1,23 @@ +using System; +using CommandLine; + +namespace Geekbot.net.Lib +{ + public class RunParameters + { + [Option('V', "verbose", Default = false, HelpText = "Prints all messages to standard output.")] + public bool Verbose { get; set; } + + [Option('r', "reset", Default = false, HelpText = "Resets the bot")] + public bool Reset { get; set; } + + [Option('j', "log-json", Default = false, HelpText = "Logs messages as json")] + public bool LogJson { get; set; } + + [Option("disable-api", Default = false, HelpText = "Disables the web api")] + public bool DisableApi { get; set; } + + [Option('e', "expose-errors", Default = false, HelpText = "Shows internal errors in the chat")] + public bool ExposeErrors { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 10f0dcb..39e5e8b 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -4,6 +4,7 @@ using System.Net; using System.Reflection; using System.Text; using System.Threading.Tasks; +using CommandLine; using Discord; using Discord.Commands; using Discord.WebSocket; @@ -26,11 +27,16 @@ namespace Geekbot.net private RedisValue _token; private IGeekbotLogger _logger; private IUserRepository _userRepository; - private string[] _args; private bool _firstStart; + private RunParameters _runParameters; private static void Main(string[] args) { + RunParameters runParameters = null; + Parser.Default.ParseArguments(args) + .WithParsed(e => runParameters = e) + .WithNotParsed(_ => Environment.Exit(1)); + var logo = new StringBuilder(); logo.AppendLine(@" ____ _____ _____ _ ______ ___ _____"); logo.AppendLine(@" / ___| ____| ____| |/ / __ ) / _ \\_ _|"); @@ -40,11 +46,11 @@ namespace Geekbot.net logo.AppendLine("========================================="); Console.WriteLine(logo.ToString()); var sumologicActive = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GEEKBOT_SUMO")); - var logger = new GeekbotLogger(sumologicActive); + var logger = new GeekbotLogger(runParameters, sumologicActive); logger.Information("Geekbot", "Starting..."); try { - new Program().MainAsync(args, logger).GetAwaiter().GetResult(); + new Program().MainAsync(runParameters, logger).GetAwaiter().GetResult(); } catch (Exception e) { @@ -52,10 +58,10 @@ namespace Geekbot.net } } - private async Task MainAsync(string[] args, IGeekbotLogger logger) + private async Task MainAsync(RunParameters runParameters, IGeekbotLogger logger) { _logger = logger; - _args = args; + _runParameters = runParameters; logger.Information("Geekbot", "Initing Stuff"); _client = new DiscordSocketClient(new DiscordSocketConfig @@ -132,7 +138,7 @@ namespace Geekbot.net _logger.Information("Geekbot", "Registering Stuff"); var translationHandler = new TranslationHandler(_client.Guilds, _redis, _logger); - var errorHandler = new ErrorHandler(_logger, translationHandler, _args.Contains("--expose-errors")); + var errorHandler = new ErrorHandler(_logger, translationHandler, _runParameters.ExposeErrors); var reactionListener = new ReactionListener(_redis); await _commands.AddModulesAsync(Assembly.GetEntryAssembly()); _services.AddSingleton(_commands); @@ -153,13 +159,13 @@ namespace Geekbot.net _client.ReactionAdded += handlers.ReactionAdded; _client.ReactionRemoved += handlers.ReactionRemoved; - if (_firstStart || _args.Contains("--reset")) + if (_firstStart || _runParameters.Reset) { _logger.Information("Geekbot", "Finishing setup"); await FinishSetup(); _logger.Information("Geekbot", "Setup finished"); } - if (!_args.Contains("--disable-api")) + if (!_runParameters.DisableApi) { StartWebApi(); } From 55e152f4aaae797519e3438419be9b56d7acbea0 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 2 May 2018 23:32:40 +0200 Subject: [PATCH 038/443] Update Readme and add token parameter --- Geekbot.net/Geekbot.net.csproj | 2 +- Geekbot.net/Lib/GeekbotLogger.cs | 6 +++--- Geekbot.net/Lib/RunParameters.cs | 3 +++ Geekbot.net/Program.cs | 3 +-- readme.md | 10 +--------- 5 files changed, 9 insertions(+), 15 deletions(-) diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index cf7e5f9..ba9faed 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -3,7 +3,7 @@ Exe netcoreapp2.0 derp.ico - 1.1.0 + 3.6.0 Pizza and Coffee Studios Pizza and Coffee Studios A Discord bot diff --git a/Geekbot.net/Lib/GeekbotLogger.cs b/Geekbot.net/Lib/GeekbotLogger.cs index aae0978..7093a7c 100644 --- a/Geekbot.net/Lib/GeekbotLogger.cs +++ b/Geekbot.net/Lib/GeekbotLogger.cs @@ -5,13 +5,13 @@ namespace Geekbot.net.Lib { public class GeekbotLogger : IGeekbotLogger { - private readonly bool _sumologicActive; + private readonly bool _logAsJson; private readonly NLog.Logger _logger; private readonly JsonSerializerSettings _serializerSettings; public GeekbotLogger(RunParameters runParameters, bool sumologicActive) { - _sumologicActive = sumologicActive; + _logAsJson = sumologicActive || runParameters.LogJson; _logger = LoggerFactory.CreateNLog(runParameters, sumologicActive); _serializerSettings = new JsonSerializerSettings { @@ -43,7 +43,7 @@ namespace Geekbot.net.Lib private string CreateLogString(string type, string source, string message, Exception stackTrace = null, object extra = null) { - if (_sumologicActive) + if (_logAsJson) { var logObject = new GeekbotLoggerObject { diff --git a/Geekbot.net/Lib/RunParameters.cs b/Geekbot.net/Lib/RunParameters.cs index 0549ac5..35c1a2a 100644 --- a/Geekbot.net/Lib/RunParameters.cs +++ b/Geekbot.net/Lib/RunParameters.cs @@ -19,5 +19,8 @@ namespace Geekbot.net.Lib [Option('e', "expose-errors", Default = false, HelpText = "Shows internal errors in the chat")] public bool ExposeErrors { get; set; } + + [Option("token", Default = null, HelpText = "Set a new bot token")] + public string Token { get; set; } } } \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 39e5e8b..3c0e5eb 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -1,5 +1,4 @@ using System; -using System.Linq; using System.Net; using System.Reflection; using System.Text; @@ -84,7 +83,7 @@ namespace Geekbot.net Environment.Exit(102); } - _token = _redis.StringGet("discordToken"); + _token = runParameters.Token ??_redis.StringGet("discordToken"); if (_token.IsNullOrEmpty) { Console.Write("Your bot Token: "); diff --git a/readme.md b/readme.md index be5ba0a..7d8544f 100644 --- a/readme.md +++ b/readme.md @@ -11,7 +11,6 @@ You can invite Geekbot to your server with [this link](https://discordapp.com/oa * DotNet Core 2 * Redis * Discord.net -* ffmpeg ## Running @@ -24,14 +23,7 @@ Run these commands On your first run geekbot will ask for your bot token, everything else is taken care of. -### Launch Parameters - -| Parameter | Description | -| --- | --- | -| `--verbose` | Show more log information | -| `--disable-api` | Disables the webapi on startup | -| `--reset` | Resets certain parts of the bot | -| `--migrate` | Migrates the database from V3.1 to the new format from V3.2
(make sure to backup before running this) | +For a list of launch options use `dotnet run -h` ## Contributing From e3adf557429effbe2cac6a6dd8af72e4878dabde Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 3 May 2018 00:56:06 +0200 Subject: [PATCH 039/443] Refaction all files into component based folders --- Geekbot.net/Commands/{ => Admin}/Admin.cs | 4 +- Geekbot.net/Commands/{ => Admin}/Mod.cs | 4 +- Geekbot.net/Commands/{ => Admin}/Owner.cs | 5 +- Geekbot.net/Commands/{ => Admin}/Role.cs | 4 +- Geekbot.net/Commands/{ => Admin}/Say.cs | 3 +- Geekbot.net/Commands/{ => Games}/BattleTag.cs | 4 +- Geekbot.net/Commands/{ => Games}/Overwatch.cs | 5 +- Geekbot.net/Commands/{ => Games}/Pokedex.cs | 3 +- Geekbot.net/Commands/{ => Games}/Roll.cs | 4 +- .../{ => Integrations/Google}/Google.cs | 46 +----- .../Google/GoogleKgApiDetailedDto.cs | 9 ++ .../Google/GoogleKgApiElementDto.cs | 8 + .../Google/GoogleKgApiImageDto.cs | 8 + .../Google/GoogleKgApiResponseDto.cs | 9 ++ .../Google/GoogleKgApiResultDto.cs | 10 ++ .../{ => Integrations}/MagicTheGathering.cs | 4 +- .../Commands/{mal.cs => Integrations/Mal.cs} | 4 +- .../UbranDictionary/UrbanDictListItemDto.cs | 12 ++ .../UbranDictionary/UrbanDictResponseDto.cs | 10 ++ .../UbranDictionary}/UrbanDictionary.cs | 22 +-- .../Commands/{ => Integrations}/Wikipedia.cs | 3 +- .../Commands/{ => Integrations}/Youtube.cs | 3 +- .../Commands/{ => Randomness/Cat}/Cat.cs | 110 +++++++------- .../Commands/Randomness/Cat/CatResponseDto.cs | 7 + .../Commands/{ => Randomness}/CheckEm.cs | 143 +++++++++--------- .../Chuck/ChuckNorrisJokeResponseDto.cs | 7 + .../Chuck}/ChuckNorrisJokes.cs | 10 +- .../Randomness/Dad/DadJokeResponseDto.cs | 7 + .../Commands/{ => Randomness/Dad}/DadJokes.cs | 10 +- .../Commands/{ => Randomness/Dog}/Dog.cs | 10 +- .../Commands/Randomness/Dog/DogResponseDto.cs | 7 + .../Commands/{ => Randomness}/EightBall.cs | 3 +- .../Commands/{ => Randomness}/Fortune.cs | 2 +- Geekbot.net/Commands/{ => Randomness}/Gdq.cs | 3 +- .../{ => Randomness}/RandomAnimals.cs | 2 +- Geekbot.net/Commands/{ => Randomness}/Ship.cs | 3 +- Geekbot.net/Commands/{ => Randomness}/Slap.cs | 3 +- Geekbot.net/Commands/{ => User}/GuildInfo.cs | 11 +- Geekbot.net/Commands/{ => User}/Karma.cs | 4 +- Geekbot.net/Commands/{ => User/Rank}/Rank.cs | 19 +-- .../Commands/User/Rank/RankUserPolyfillDto.cs | 9 ++ Geekbot.net/Commands/{ => User}/Stats.cs | 4 +- .../Commands/{ => Utils}/AvatarGetter.cs | 3 +- .../{ => Utils/Changelog}/Changelog.cs | 21 +-- .../Utils/Changelog/CommitAuthorDto.cs | 9 ++ .../Commands/Utils/Changelog/CommitDto.cs | 7 + .../Commands/Utils/Changelog/CommitInfoDto.cs | 8 + Geekbot.net/Commands/{ => Utils}/Choose.cs | 4 +- Geekbot.net/Commands/{ => Utils/Dice}/Dice.cs | 10 +- .../Commands/Utils/Dice/DiceTypeDto.cs | 10 ++ Geekbot.net/Commands/{ => Utils}/Emojify.cs | 4 +- Geekbot.net/Commands/{ => Utils}/Help.cs | 3 +- Geekbot.net/Commands/{ => Utils}/Info.cs | 3 +- Geekbot.net/Commands/{ => Utils}/Ping.cs | 2 +- Geekbot.net/Commands/{ => Utils/Poll}/Poll.cs | 34 ++--- .../Commands/Utils/Poll/PollDataDto.cs | 13 ++ .../Commands/Utils/Poll/PollResultDto.cs | 8 + .../Commands/{ => Utils/Quote}/Quote.cs | 21 +-- .../Commands/Utils/Quote/QuoteObjectDto.cs | 12 ++ Geekbot.net/Commands/Voice.cs | 102 ------------- Geekbot.net/Geekbot.net.csproj | 9 +- Geekbot.net/Handlers.cs | 5 +- Geekbot.net/Lib/Clients/IMalClient.cs | 12 ++ Geekbot.net/Lib/{ => Clients}/MalClient.cs | 10 +- .../Lib/{ => Converters}/EmojiConverter.cs | 8 +- Geekbot.net/Lib/Converters/IEmojiConverter.cs | 8 + .../Lib/Converters/IMtgManaConverter.cs | 7 + .../Lib/Converters/MtgManaConverter.cs | 33 ++++ Geekbot.net/Lib/Converters/MtgManaEmojis.json | 50 ++++++ .../Lib/{ => ErrorHandling}/ErrorHandler.cs | 10 +- .../Lib/ErrorHandling/IErrorHandler.cs | 12 ++ Geekbot.net/Lib/Levels/ILevelCalc.cs | 7 + Geekbot.net/Lib/{ => Levels}/LevelCalc.cs | 7 +- .../Lib/Localization/ITranslationHandler.cs | 14 ++ .../{ => Localization}/TranslationHandler.cs | 14 +- .../Localization}/Translations.json | 0 Geekbot.net/Lib/Logger/DiscordLogger.cs | 42 +++++ Geekbot.net/Lib/{ => Logger}/GeekbotLogger.cs | 25 +-- Geekbot.net/Lib/Logger/IDiscordLogger.cs | 10 ++ Geekbot.net/Lib/Logger/IGeekbotLogger.cs | 13 ++ Geekbot.net/Lib/Logger/LogDto.cs | 14 ++ Geekbot.net/Lib/{ => Logger}/LoggerFactory.cs | 4 +- Geekbot.net/Lib/Logger/MessageDto.cs | 26 ++++ .../Lib/{ => Logger}/SimpleConextConverter.cs | 27 +--- Geekbot.net/Lib/Media/FortunesProvider.cs | 8 +- Geekbot.net/Lib/Media/IFortunesProvider.cs | 7 + Geekbot.net/Lib/Media/IMediaProvider.cs | 14 ++ Geekbot.net/Lib/Media/MediaProvider.cs | 29 ++-- Geekbot.net/Lib/MtgManaConverter.cs | 86 ----------- .../Lib/ReactionListener/IReactionListener.cs | 14 ++ .../ReactionListener.cs | 10 +- .../Lib/UserRepository/IUserRepository.cs | 13 ++ .../{ => UserRepository}/UserRepository.cs | 22 +-- .../UserRepository/UserRepositoryUserDto.cs | 16 ++ Geekbot.net/Program.cs | 40 ++--- Geekbot.net/WebApi/Help/CommandDto.cs | 15 ++ Geekbot.net/WebApi/Help/CommandParamDto.cs | 9 ++ .../WebApi/{ => Help}/HelpController.cs | 23 +-- Geekbot.net/WebApi/Status/ApiStatusDto.cs | 9 ++ .../WebApi/{ => Status}/StatusController.cs | 9 +- Tests/Lib/EmojiConverter.test.cs | 1 + Tests/Lib/LevelCalc.test.cs | 1 + 102 files changed, 816 insertions(+), 709 deletions(-) rename Geekbot.net/Commands/{ => Admin}/Admin.cs (96%) rename Geekbot.net/Commands/{ => Admin}/Mod.cs (94%) rename Geekbot.net/Commands/{ => Admin}/Owner.cs (94%) rename Geekbot.net/Commands/{ => Admin}/Role.cs (98%) rename Geekbot.net/Commands/{ => Admin}/Say.cs (92%) rename Geekbot.net/Commands/{ => Games}/BattleTag.cs (92%) rename Geekbot.net/Commands/{ => Games}/Overwatch.cs (94%) rename Geekbot.net/Commands/{ => Games}/Pokedex.cs (94%) rename Geekbot.net/Commands/{ => Games}/Roll.cs (95%) rename Geekbot.net/Commands/{ => Integrations/Google}/Google.cs (56%) create mode 100644 Geekbot.net/Commands/Integrations/Google/GoogleKgApiDetailedDto.cs create mode 100644 Geekbot.net/Commands/Integrations/Google/GoogleKgApiElementDto.cs create mode 100644 Geekbot.net/Commands/Integrations/Google/GoogleKgApiImageDto.cs create mode 100644 Geekbot.net/Commands/Integrations/Google/GoogleKgApiResponseDto.cs create mode 100644 Geekbot.net/Commands/Integrations/Google/GoogleKgApiResultDto.cs rename Geekbot.net/Commands/{ => Integrations}/MagicTheGathering.cs (94%) rename Geekbot.net/Commands/{mal.cs => Integrations/Mal.cs} (95%) create mode 100644 Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictListItemDto.cs create mode 100644 Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictResponseDto.cs rename Geekbot.net/Commands/{ => Integrations/UbranDictionary}/UrbanDictionary.cs (78%) rename Geekbot.net/Commands/{ => Integrations}/Wikipedia.cs (95%) rename Geekbot.net/Commands/{ => Integrations}/Youtube.cs (92%) rename Geekbot.net/Commands/{ => Randomness/Cat}/Cat.cs (90%) create mode 100644 Geekbot.net/Commands/Randomness/Cat/CatResponseDto.cs rename Geekbot.net/Commands/{ => Randomness}/CheckEm.cs (93%) create mode 100644 Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokeResponseDto.cs rename Geekbot.net/Commands/{ => Randomness/Chuck}/ChuckNorrisJokes.cs (87%) create mode 100644 Geekbot.net/Commands/Randomness/Dad/DadJokeResponseDto.cs rename Geekbot.net/Commands/{ => Randomness/Dad}/DadJokes.cs (87%) rename Geekbot.net/Commands/{ => Randomness/Dog}/Dog.cs (88%) create mode 100644 Geekbot.net/Commands/Randomness/Dog/DogResponseDto.cs rename Geekbot.net/Commands/{ => Randomness}/EightBall.cs (95%) rename Geekbot.net/Commands/{ => Randomness}/Fortune.cs (89%) rename Geekbot.net/Commands/{ => Randomness}/Gdq.cs (89%) rename Geekbot.net/Commands/{ => Randomness}/RandomAnimals.cs (98%) rename Geekbot.net/Commands/{ => Randomness}/Ship.cs (94%) rename Geekbot.net/Commands/{ => Randomness}/Slap.cs (94%) rename Geekbot.net/Commands/{ => User}/GuildInfo.cs (83%) rename Geekbot.net/Commands/{ => User}/Karma.cs (97%) rename Geekbot.net/Commands/{ => User/Rank}/Rank.cs (93%) create mode 100644 Geekbot.net/Commands/User/Rank/RankUserPolyfillDto.cs rename Geekbot.net/Commands/{ => User}/Stats.cs (96%) rename Geekbot.net/Commands/{ => Utils}/AvatarGetter.cs (89%) rename Geekbot.net/Commands/{ => Utils/Changelog}/Changelog.cs (82%) create mode 100644 Geekbot.net/Commands/Utils/Changelog/CommitAuthorDto.cs create mode 100644 Geekbot.net/Commands/Utils/Changelog/CommitDto.cs create mode 100644 Geekbot.net/Commands/Utils/Changelog/CommitInfoDto.cs rename Geekbot.net/Commands/{ => Utils}/Choose.cs (88%) rename Geekbot.net/Commands/{ => Utils/Dice}/Dice.cs (93%) create mode 100644 Geekbot.net/Commands/Utils/Dice/DiceTypeDto.cs rename Geekbot.net/Commands/{ => Utils}/Emojify.cs (89%) rename Geekbot.net/Commands/{ => Utils}/Help.cs (93%) rename Geekbot.net/Commands/{ => Utils}/Info.cs (94%) rename Geekbot.net/Commands/{ => Utils}/Ping.cs (91%) rename Geekbot.net/Commands/{ => Utils/Poll}/Poll.cs (85%) create mode 100644 Geekbot.net/Commands/Utils/Poll/PollDataDto.cs create mode 100644 Geekbot.net/Commands/Utils/Poll/PollResultDto.cs rename Geekbot.net/Commands/{ => Utils/Quote}/Quote.cs (94%) create mode 100644 Geekbot.net/Commands/Utils/Quote/QuoteObjectDto.cs delete mode 100644 Geekbot.net/Commands/Voice.cs create mode 100644 Geekbot.net/Lib/Clients/IMalClient.cs rename Geekbot.net/Lib/{ => Clients}/MalClient.cs (89%) rename Geekbot.net/Lib/{ => Converters}/EmojiConverter.cs (92%) create mode 100644 Geekbot.net/Lib/Converters/IEmojiConverter.cs create mode 100644 Geekbot.net/Lib/Converters/IMtgManaConverter.cs create mode 100644 Geekbot.net/Lib/Converters/MtgManaConverter.cs create mode 100644 Geekbot.net/Lib/Converters/MtgManaEmojis.json rename Geekbot.net/Lib/{ => ErrorHandling}/ErrorHandler.cs (93%) create mode 100644 Geekbot.net/Lib/ErrorHandling/IErrorHandler.cs create mode 100644 Geekbot.net/Lib/Levels/ILevelCalc.cs rename Geekbot.net/Lib/{ => Levels}/LevelCalc.cs (87%) create mode 100644 Geekbot.net/Lib/Localization/ITranslationHandler.cs rename Geekbot.net/Lib/{ => Localization}/TranslationHandler.cs (90%) rename Geekbot.net/{Storage => Lib/Localization}/Translations.json (100%) create mode 100644 Geekbot.net/Lib/Logger/DiscordLogger.cs rename Geekbot.net/Lib/{ => Logger}/GeekbotLogger.cs (75%) create mode 100644 Geekbot.net/Lib/Logger/IDiscordLogger.cs create mode 100644 Geekbot.net/Lib/Logger/IGeekbotLogger.cs create mode 100644 Geekbot.net/Lib/Logger/LogDto.cs rename Geekbot.net/Lib/{ => Logger}/LoggerFactory.cs (95%) create mode 100644 Geekbot.net/Lib/Logger/MessageDto.cs rename Geekbot.net/Lib/{ => Logger}/SimpleConextConverter.cs (75%) create mode 100644 Geekbot.net/Lib/Media/IFortunesProvider.cs create mode 100644 Geekbot.net/Lib/Media/IMediaProvider.cs delete mode 100644 Geekbot.net/Lib/MtgManaConverter.cs create mode 100644 Geekbot.net/Lib/ReactionListener/IReactionListener.cs rename Geekbot.net/Lib/{ => ReactionListener}/ReactionListener.cs (87%) create mode 100644 Geekbot.net/Lib/UserRepository/IUserRepository.cs rename Geekbot.net/Lib/{ => UserRepository}/UserRepository.cs (84%) create mode 100644 Geekbot.net/Lib/UserRepository/UserRepositoryUserDto.cs create mode 100644 Geekbot.net/WebApi/Help/CommandDto.cs create mode 100644 Geekbot.net/WebApi/Help/CommandParamDto.cs rename Geekbot.net/WebApi/{ => Help}/HelpController.cs (70%) create mode 100644 Geekbot.net/WebApi/Status/ApiStatusDto.cs rename Geekbot.net/WebApi/{ => Status}/StatusController.cs (71%) diff --git a/Geekbot.net/Commands/Admin.cs b/Geekbot.net/Commands/Admin/Admin.cs similarity index 96% rename from Geekbot.net/Commands/Admin.cs rename to Geekbot.net/Commands/Admin/Admin.cs index 481eab1..7d30689 100644 --- a/Geekbot.net/Commands/Admin.cs +++ b/Geekbot.net/Commands/Admin/Admin.cs @@ -5,9 +5,11 @@ using Discord; using Discord.Commands; using Discord.WebSocket; using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.Localization; using StackExchange.Redis; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Admin { [Group("admin")] [RequireUserPermission(GuildPermission.Administrator)] diff --git a/Geekbot.net/Commands/Mod.cs b/Geekbot.net/Commands/Admin/Mod.cs similarity index 94% rename from Geekbot.net/Commands/Mod.cs rename to Geekbot.net/Commands/Admin/Mod.cs index e8751f6..2dacee3 100644 --- a/Geekbot.net/Commands/Mod.cs +++ b/Geekbot.net/Commands/Admin/Mod.cs @@ -5,9 +5,11 @@ using Discord; using Discord.Commands; using Discord.WebSocket; using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.UserRepository; using StackExchange.Redis; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Admin { [Group("mod")] [RequireUserPermission(GuildPermission.KickMembers)] diff --git a/Geekbot.net/Commands/Owner.cs b/Geekbot.net/Commands/Admin/Owner.cs similarity index 94% rename from Geekbot.net/Commands/Owner.cs rename to Geekbot.net/Commands/Admin/Owner.cs index b73797f..39ccd8c 100644 --- a/Geekbot.net/Commands/Owner.cs +++ b/Geekbot.net/Commands/Admin/Owner.cs @@ -4,9 +4,12 @@ using Discord; using Discord.Commands; using Discord.WebSocket; using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.Logger; +using Geekbot.net.Lib.UserRepository; using StackExchange.Redis; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Admin { [Group("owner")] [RequireUserPermission(GuildPermission.Administrator)] diff --git a/Geekbot.net/Commands/Role.cs b/Geekbot.net/Commands/Admin/Role.cs similarity index 98% rename from Geekbot.net/Commands/Role.cs rename to Geekbot.net/Commands/Admin/Role.cs index eb67bbf..24d19dd 100644 --- a/Geekbot.net/Commands/Role.cs +++ b/Geekbot.net/Commands/Admin/Role.cs @@ -6,9 +6,11 @@ using Discord; using Discord.Commands; using Discord.Net; using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.ReactionListener; using StackExchange.Redis; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Admin { [Group("role")] public class Role : ModuleBase diff --git a/Geekbot.net/Commands/Say.cs b/Geekbot.net/Commands/Admin/Say.cs similarity index 92% rename from Geekbot.net/Commands/Say.cs rename to Geekbot.net/Commands/Admin/Say.cs index 4c878c4..97c7a0f 100644 --- a/Geekbot.net/Commands/Say.cs +++ b/Geekbot.net/Commands/Admin/Say.cs @@ -3,8 +3,9 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Admin { public class Say : ModuleBase { diff --git a/Geekbot.net/Commands/BattleTag.cs b/Geekbot.net/Commands/Games/BattleTag.cs similarity index 92% rename from Geekbot.net/Commands/BattleTag.cs rename to Geekbot.net/Commands/Games/BattleTag.cs index eb6fca1..88cc0d8 100644 --- a/Geekbot.net/Commands/BattleTag.cs +++ b/Geekbot.net/Commands/Games/BattleTag.cs @@ -2,8 +2,10 @@ 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 +namespace Geekbot.net.Commands.Games { [Group("battletag")] public class BattleTag : ModuleBase diff --git a/Geekbot.net/Commands/Overwatch.cs b/Geekbot.net/Commands/Games/Overwatch.cs similarity index 94% rename from Geekbot.net/Commands/Overwatch.cs rename to Geekbot.net/Commands/Games/Overwatch.cs index 0a39662..73e5858 100644 --- a/Geekbot.net/Commands/Overwatch.cs +++ b/Geekbot.net/Commands/Games/Overwatch.cs @@ -3,11 +3,12 @@ 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; -using StackExchange.Redis; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Games { [Group("ow")] public class Overwatch : ModuleBase diff --git a/Geekbot.net/Commands/Pokedex.cs b/Geekbot.net/Commands/Games/Pokedex.cs similarity index 94% rename from Geekbot.net/Commands/Pokedex.cs rename to Geekbot.net/Commands/Games/Pokedex.cs index fa8746f..57f842a 100644 --- a/Geekbot.net/Commands/Pokedex.cs +++ b/Geekbot.net/Commands/Games/Pokedex.cs @@ -4,9 +4,10 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; using PokeAPI; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Games { public class Pokedex : ModuleBase { diff --git a/Geekbot.net/Commands/Roll.cs b/Geekbot.net/Commands/Games/Roll.cs similarity index 95% rename from Geekbot.net/Commands/Roll.cs rename to Geekbot.net/Commands/Games/Roll.cs index 8d4854c..30697df 100644 --- a/Geekbot.net/Commands/Roll.cs +++ b/Geekbot.net/Commands/Games/Roll.cs @@ -2,9 +2,11 @@ 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 +namespace Geekbot.net.Commands.Games { public class Roll : ModuleBase { diff --git a/Geekbot.net/Commands/Google.cs b/Geekbot.net/Commands/Integrations/Google/Google.cs similarity index 56% rename from Geekbot.net/Commands/Google.cs rename to Geekbot.net/Commands/Integrations/Google/Google.cs index 19d3efb..8e880c4 100644 --- a/Geekbot.net/Commands/Google.cs +++ b/Geekbot.net/Commands/Integrations/Google/Google.cs @@ -1,15 +1,15 @@ using System; -using System.Collections.Generic; using System.Linq; using System.Net; using System.Threading.Tasks; using Discord; using Discord.Commands; using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; using StackExchange.Redis; using Utf8Json; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Integrations.Google { public class Google : ModuleBase { @@ -40,7 +40,7 @@ namespace Geekbot.net.Commands var url = new Uri($"https://kgsearch.googleapis.com/v1/entities:search?languages=en&limit=1&query={searchText}&key={apiKey}"); var responseString = client.DownloadString(url); - var response = JsonSerializer.Deserialize(responseString); + var response = JsonSerializer.Deserialize(responseString); if (!response.ItemListElement.Any()) { @@ -48,13 +48,13 @@ namespace Geekbot.net.Commands return; } - var data = response.ItemListElement.First().Result; + var data = response.ItemListElement.First().ResultDto; var eb = new EmbedBuilder(); eb.Title = data.Name; if(!string.IsNullOrEmpty(data.Description)) eb.WithDescription(data.Description); - if(!string.IsNullOrEmpty(data.DetailedDescription?.Url)) eb.WithUrl(data.DetailedDescription.Url); - if(!string.IsNullOrEmpty(data.DetailedDescription?.ArticleBody)) eb.AddField("Details", data.DetailedDescription.ArticleBody); - if(!string.IsNullOrEmpty(data.Image?.ContentUrl)) eb.WithThumbnailUrl(data.Image.ContentUrl); + if(!string.IsNullOrEmpty(data.DetailedDtoDescription?.Url)) eb.WithUrl(data.DetailedDtoDescription.Url); + if(!string.IsNullOrEmpty(data.DetailedDtoDescription?.ArticleBody)) eb.AddField("Details", data.DetailedDtoDescription.ArticleBody); + if(!string.IsNullOrEmpty(data.ImageDto?.ContentUrl)) eb.WithThumbnailUrl(data.ImageDto.ContentUrl); await ReplyAsync("", false, eb.Build()); } @@ -64,37 +64,5 @@ namespace Geekbot.net.Commands _errorHandler.HandleCommandException(e, Context); } } - - public class GoogleKgApiResponse - { - public List ItemListElement { get; set; } - - public class GoogleKgApiElement - { - public GoogleKgApiResult Result { get; set; } - public double ResultScore { get; set; } - } - - public class GoogleKgApiResult - { - public string Name { get; set; } - public string Description { get; set; } - public GoogleKgApiImage Image { get; set; } - public GoogleKgApiDetailed DetailedDescription { get; set; } - } - - public class GoogleKgApiImage - { - public string ContentUrl { get; set; } - public string Url { get; set; } - } - - public class GoogleKgApiDetailed - { - public string ArticleBody { get; set; } - public string Url { get; set; } - public string License { get; set; } - } - } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/Integrations/Google/GoogleKgApiDetailedDto.cs b/Geekbot.net/Commands/Integrations/Google/GoogleKgApiDetailedDto.cs new file mode 100644 index 0000000..031d1e7 --- /dev/null +++ b/Geekbot.net/Commands/Integrations/Google/GoogleKgApiDetailedDto.cs @@ -0,0 +1,9 @@ +namespace Geekbot.net.Commands.Integrations.Google +{ + public class GoogleKgApiDetailedDto + { + public string ArticleBody { get; set; } + public string Url { get; set; } + public string License { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/Integrations/Google/GoogleKgApiElementDto.cs b/Geekbot.net/Commands/Integrations/Google/GoogleKgApiElementDto.cs new file mode 100644 index 0000000..3c4a130 --- /dev/null +++ b/Geekbot.net/Commands/Integrations/Google/GoogleKgApiElementDto.cs @@ -0,0 +1,8 @@ +namespace Geekbot.net.Commands.Integrations.Google +{ + public class GoogleKgApiElementDto + { + public GoogleKgApiResultDto ResultDto { get; set; } + public double ResultScore { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/Integrations/Google/GoogleKgApiImageDto.cs b/Geekbot.net/Commands/Integrations/Google/GoogleKgApiImageDto.cs new file mode 100644 index 0000000..fe7cdaa --- /dev/null +++ b/Geekbot.net/Commands/Integrations/Google/GoogleKgApiImageDto.cs @@ -0,0 +1,8 @@ +namespace Geekbot.net.Commands.Integrations.Google +{ + public class GoogleKgApiImageDto + { + public string ContentUrl { get; set; } + public string Url { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/Integrations/Google/GoogleKgApiResponseDto.cs b/Geekbot.net/Commands/Integrations/Google/GoogleKgApiResponseDto.cs new file mode 100644 index 0000000..af337db --- /dev/null +++ b/Geekbot.net/Commands/Integrations/Google/GoogleKgApiResponseDto.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace Geekbot.net.Commands.Integrations.Google +{ + public class GoogleKgApiResponseDto + { + public List ItemListElement { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/Integrations/Google/GoogleKgApiResultDto.cs b/Geekbot.net/Commands/Integrations/Google/GoogleKgApiResultDto.cs new file mode 100644 index 0000000..2c348c9 --- /dev/null +++ b/Geekbot.net/Commands/Integrations/Google/GoogleKgApiResultDto.cs @@ -0,0 +1,10 @@ +namespace Geekbot.net.Commands.Integrations.Google +{ + public class GoogleKgApiResultDto + { + public string Name { get; set; } + public string Description { get; set; } + public GoogleKgApiImageDto ImageDto { get; set; } + public GoogleKgApiDetailedDto DetailedDtoDescription { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/MagicTheGathering.cs b/Geekbot.net/Commands/Integrations/MagicTheGathering.cs similarity index 94% rename from Geekbot.net/Commands/MagicTheGathering.cs rename to Geekbot.net/Commands/Integrations/MagicTheGathering.cs index 9c7379f..3074803 100644 --- a/Geekbot.net/Commands/MagicTheGathering.cs +++ b/Geekbot.net/Commands/Integrations/MagicTheGathering.cs @@ -5,9 +5,11 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Geekbot.net.Lib; +using Geekbot.net.Lib.Converters; +using Geekbot.net.Lib.ErrorHandling; using MtgApiManager.Lib.Service; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Integrations { public class Magicthegathering : ModuleBase { diff --git a/Geekbot.net/Commands/mal.cs b/Geekbot.net/Commands/Integrations/Mal.cs similarity index 95% rename from Geekbot.net/Commands/mal.cs rename to Geekbot.net/Commands/Integrations/Mal.cs index 8c16936..d70949a 100644 --- a/Geekbot.net/Commands/mal.cs +++ b/Geekbot.net/Commands/Integrations/Mal.cs @@ -4,8 +4,10 @@ using System.Web; using Discord; using Discord.Commands; using Geekbot.net.Lib; +using Geekbot.net.Lib.Clients; +using Geekbot.net.Lib.ErrorHandling; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Integrations { public class Mal : ModuleBase { diff --git a/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictListItemDto.cs b/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictListItemDto.cs new file mode 100644 index 0000000..e98885b --- /dev/null +++ b/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictListItemDto.cs @@ -0,0 +1,12 @@ +namespace Geekbot.net.Commands.Integrations.UbranDictionary +{ + internal class UrbanListItemDto + { + public string Definition { get; set; } + public string Permalink { get; set; } + public string ThumbsUp { get; set; } + public string Word { get; set; } + public string Example { get; set; } + public string ThumbsDown { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictResponseDto.cs b/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictResponseDto.cs new file mode 100644 index 0000000..2c3e014 --- /dev/null +++ b/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictResponseDto.cs @@ -0,0 +1,10 @@ +using System.Collections.Generic; + +namespace Geekbot.net.Commands.Integrations.UbranDictionary +{ + internal class UrbanResponseDto + { + public string[] Tags { get; set; } + public List List { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/UrbanDictionary.cs b/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs similarity index 78% rename from Geekbot.net/Commands/UrbanDictionary.cs rename to Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs index 2af43ba..44402d0 100644 --- a/Geekbot.net/Commands/UrbanDictionary.cs +++ b/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs @@ -1,14 +1,14 @@ using System; -using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Threading.Tasks; using Discord; using Discord.Commands; using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; using Newtonsoft.Json; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Integrations.UbranDictionary { public class UrbanDictionary : ModuleBase { @@ -33,7 +33,7 @@ namespace Geekbot.net.Commands response.EnsureSuccessStatusCode(); var stringResponse = await response.Content.ReadAsStringAsync(); - var definitions = JsonConvert.DeserializeObject(stringResponse); + var definitions = JsonConvert.DeserializeObject(stringResponse); if (definitions.List.Count == 0) { await ReplyAsync("That word hasn't been defined..."); @@ -63,21 +63,5 @@ namespace Geekbot.net.Commands _errorHandler.HandleCommandException(e, Context); } } - - private class UrbanResponse - { - public string[] Tags { get; set; } - public List List { get; set; } - } - - private class UrbanListItem - { - public string Definition { get; set; } - public string Permalink { get; set; } - public string ThumbsUp { get; set; } - public string Word { get; set; } - public string Example { get; set; } - public string ThumbsDown { get; set; } - } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/Wikipedia.cs b/Geekbot.net/Commands/Integrations/Wikipedia.cs similarity index 95% rename from Geekbot.net/Commands/Wikipedia.cs rename to Geekbot.net/Commands/Integrations/Wikipedia.cs index 05b21a6..68589f6 100644 --- a/Geekbot.net/Commands/Wikipedia.cs +++ b/Geekbot.net/Commands/Integrations/Wikipedia.cs @@ -6,12 +6,13 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; using HtmlAgilityPack; using StackExchange.Redis; using WikipediaApi; using WikipediaApi.Page; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Integrations { public class Wikipedia : ModuleBase { diff --git a/Geekbot.net/Commands/Youtube.cs b/Geekbot.net/Commands/Integrations/Youtube.cs similarity index 92% rename from Geekbot.net/Commands/Youtube.cs rename to Geekbot.net/Commands/Integrations/Youtube.cs index 305052b..f534bcd 100644 --- a/Geekbot.net/Commands/Youtube.cs +++ b/Geekbot.net/Commands/Integrations/Youtube.cs @@ -2,11 +2,12 @@ using System.Threading.Tasks; using Discord.Commands; using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; using Google.Apis.Services; using Google.Apis.YouTube.v3; using StackExchange.Redis; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Integrations { public class Youtube : ModuleBase { diff --git a/Geekbot.net/Commands/Cat.cs b/Geekbot.net/Commands/Randomness/Cat/Cat.cs similarity index 90% rename from Geekbot.net/Commands/Cat.cs rename to Geekbot.net/Commands/Randomness/Cat/Cat.cs index 0545894..88a9ce5 100644 --- a/Geekbot.net/Commands/Cat.cs +++ b/Geekbot.net/Commands/Randomness/Cat/Cat.cs @@ -1,58 +1,54 @@ -using System; -using System.Net.Http; -using System.Threading.Tasks; -using Discord; -using Discord.Commands; -using Geekbot.net.Lib; -using Newtonsoft.Json; - -namespace Geekbot.net.Commands -{ - public class Cat : ModuleBase - { - private readonly IErrorHandler _errorHandler; - - public Cat(IErrorHandler errorHandler) - { - _errorHandler = errorHandler; - } - - [Command("cat", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Randomness)] - [Summary("Return a random image of a cat.")] - public async Task Say() - { - try - { - using (var client = new HttpClient()) - { - try - { - client.BaseAddress = new Uri("https://aws.random.cat"); - var response = await client.GetAsync("/meow"); - response.EnsureSuccessStatusCode(); - - var stringResponse = await response.Content.ReadAsStringAsync(); - var catFile = JsonConvert.DeserializeObject(stringResponse); - var eb = new EmbedBuilder(); - eb.ImageUrl = catFile.File; - await ReplyAsync("", false, eb.Build()); - } - catch - { - await ReplyAsync("Seems like the dog cought the cat (error occured)"); - } - } - } - catch (Exception e) - { - _errorHandler.HandleCommandException(e, Context); - } - } - - private class CatResponse - { - public string File { get; set; } - } - } +using System; +using System.Net.Http; +using System.Threading.Tasks; +using Discord; +using Discord.Commands; +using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; +using Newtonsoft.Json; + +namespace Geekbot.net.Commands.Randomness.Cat +{ + public class Cat : ModuleBase + { + private readonly IErrorHandler _errorHandler; + + public Cat(IErrorHandler errorHandler) + { + _errorHandler = errorHandler; + } + + [Command("cat", RunMode = RunMode.Async)] + [Remarks(CommandCategories.Randomness)] + [Summary("Return a random image of a cat.")] + public async Task Say() + { + try + { + using (var client = new HttpClient()) + { + try + { + client.BaseAddress = new Uri("https://aws.random.cat"); + var response = await client.GetAsync("/meow"); + response.EnsureSuccessStatusCode(); + + var stringResponse = await response.Content.ReadAsStringAsync(); + var catFile = JsonConvert.DeserializeObject(stringResponse); + var eb = new EmbedBuilder(); + eb.ImageUrl = catFile.File; + await ReplyAsync("", false, eb.Build()); + } + catch + { + await ReplyAsync("Seems like the dog cought the cat (error occured)"); + } + } + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + } + } + } } \ No newline at end of file diff --git a/Geekbot.net/Commands/Randomness/Cat/CatResponseDto.cs b/Geekbot.net/Commands/Randomness/Cat/CatResponseDto.cs new file mode 100644 index 0000000..05ebf2b --- /dev/null +++ b/Geekbot.net/Commands/Randomness/Cat/CatResponseDto.cs @@ -0,0 +1,7 @@ +namespace Geekbot.net.Commands.Randomness.Cat +{ + internal class CatResponseDto + { + public string File { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/CheckEm.cs b/Geekbot.net/Commands/Randomness/CheckEm.cs similarity index 93% rename from Geekbot.net/Commands/CheckEm.cs rename to Geekbot.net/Commands/Randomness/CheckEm.cs index 67df682..fc962cc 100644 --- a/Geekbot.net/Commands/CheckEm.cs +++ b/Geekbot.net/Commands/Randomness/CheckEm.cs @@ -1,72 +1,73 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading.Tasks; -using Discord.Commands; -using Geekbot.net.Lib; -using Geekbot.net.Lib.Media; - -namespace Geekbot.net.Commands -{ - public class CheckEm : ModuleBase - { - private readonly IMediaProvider _checkEmImages; - private readonly IErrorHandler _errorHandler; - - public CheckEm(IMediaProvider mediaProvider, IErrorHandler errorHandler) - { - _checkEmImages = mediaProvider; - _errorHandler = errorHandler; - } - - [Command("checkem", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Randomness)] - [Summary("Check for dubs")] - public async Task MuhDubs() - { - try - { - var number = new Random().Next(10000000, 99999999); - var dubtriqua = ""; - - var ns = GetIntArray(number); - if (ns[7] == ns[6]) - { - dubtriqua = "DUBS"; - if (ns[6] == ns[5]) - { - dubtriqua = "TRIPS"; - if (ns[5] == ns[4]) - dubtriqua = "QUADS"; - } - } - - var sb = new StringBuilder(); - sb.AppendLine($"Check em {Context.User.Mention}"); - sb.AppendLine($"**{number}**"); - if (!string.IsNullOrEmpty(dubtriqua)) - sb.AppendLine($":tada: {dubtriqua} :tada:"); - sb.AppendLine(_checkEmImages.GetCheckem()); - - await ReplyAsync(sb.ToString()); - } - catch (Exception e) - { - _errorHandler.HandleCommandException(e, Context); - } - } - - private int[] GetIntArray(int num) - { - var listOfInts = new List(); - while (num > 0) - { - listOfInts.Add(num % 10); - num = num / 10; - } - - listOfInts.Reverse(); - return listOfInts.ToArray(); - } - } +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using Discord.Commands; +using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.Media; + +namespace Geekbot.net.Commands.Randomness +{ + public class CheckEm : ModuleBase + { + private readonly IMediaProvider _checkEmImages; + private readonly IErrorHandler _errorHandler; + + public CheckEm(IMediaProvider mediaProvider, IErrorHandler errorHandler) + { + _checkEmImages = mediaProvider; + _errorHandler = errorHandler; + } + + [Command("checkem", RunMode = RunMode.Async)] + [Remarks(CommandCategories.Randomness)] + [Summary("Check for dubs")] + public async Task MuhDubs() + { + try + { + var number = new Random().Next(10000000, 99999999); + var dubtriqua = ""; + + var ns = GetIntArray(number); + if (ns[7] == ns[6]) + { + dubtriqua = "DUBS"; + if (ns[6] == ns[5]) + { + dubtriqua = "TRIPS"; + if (ns[5] == ns[4]) + dubtriqua = "QUADS"; + } + } + + var sb = new StringBuilder(); + sb.AppendLine($"Check em {Context.User.Mention}"); + sb.AppendLine($"**{number}**"); + if (!string.IsNullOrEmpty(dubtriqua)) + sb.AppendLine($":tada: {dubtriqua} :tada:"); + sb.AppendLine(_checkEmImages.GetCheckem()); + + await ReplyAsync(sb.ToString()); + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + } + } + + private int[] GetIntArray(int num) + { + var listOfInts = new List(); + while (num > 0) + { + listOfInts.Add(num % 10); + num = num / 10; + } + + listOfInts.Reverse(); + return listOfInts.ToArray(); + } + } } \ No newline at end of file diff --git a/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokeResponseDto.cs b/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokeResponseDto.cs new file mode 100644 index 0000000..8d513b8 --- /dev/null +++ b/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokeResponseDto.cs @@ -0,0 +1,7 @@ +namespace Geekbot.net.Commands.Randomness.Chuck +{ + internal class ChuckNorrisJokeResponseDto + { + public string Value { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/ChuckNorrisJokes.cs b/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs similarity index 87% rename from Geekbot.net/Commands/ChuckNorrisJokes.cs rename to Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs index 1c3b009..6ff2255 100644 --- a/Geekbot.net/Commands/ChuckNorrisJokes.cs +++ b/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs @@ -4,9 +4,10 @@ using System.Net.Http.Headers; using System.Threading.Tasks; using Discord.Commands; using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; using Newtonsoft.Json; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Randomness.Chuck { public class ChuckNorrisJokes : ModuleBase { @@ -34,7 +35,7 @@ namespace Geekbot.net.Commands response.EnsureSuccessStatusCode(); var stringResponse = await response.Content.ReadAsStringAsync(); - var data = JsonConvert.DeserializeObject(stringResponse); + var data = JsonConvert.DeserializeObject(stringResponse); await ReplyAsync(data.Value); } catch (HttpRequestException) @@ -48,10 +49,5 @@ namespace Geekbot.net.Commands _errorHandler.HandleCommandException(e, Context); } } - - private class ChuckNorrisJokeResponse - { - public string Value { get; set; } - } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/Randomness/Dad/DadJokeResponseDto.cs b/Geekbot.net/Commands/Randomness/Dad/DadJokeResponseDto.cs new file mode 100644 index 0000000..262eee9 --- /dev/null +++ b/Geekbot.net/Commands/Randomness/Dad/DadJokeResponseDto.cs @@ -0,0 +1,7 @@ +namespace Geekbot.net.Commands.Randomness.Dad +{ + internal class DadJokeResponseDto + { + public string Joke { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/DadJokes.cs b/Geekbot.net/Commands/Randomness/Dad/DadJokes.cs similarity index 87% rename from Geekbot.net/Commands/DadJokes.cs rename to Geekbot.net/Commands/Randomness/Dad/DadJokes.cs index 897c3e0..e35798c 100644 --- a/Geekbot.net/Commands/DadJokes.cs +++ b/Geekbot.net/Commands/Randomness/Dad/DadJokes.cs @@ -4,9 +4,10 @@ using System.Net.Http.Headers; using System.Threading.Tasks; using Discord.Commands; using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; using Newtonsoft.Json; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Randomness.Dad { public class DadJokes : ModuleBase { @@ -34,7 +35,7 @@ namespace Geekbot.net.Commands response.EnsureSuccessStatusCode(); var stringResponse = await response.Content.ReadAsStringAsync(); - var data = JsonConvert.DeserializeObject(stringResponse); + var data = JsonConvert.DeserializeObject(stringResponse); await ReplyAsync(data.Joke); } catch (HttpRequestException) @@ -48,10 +49,5 @@ namespace Geekbot.net.Commands _errorHandler.HandleCommandException(e, Context); } } - - private class DadJokeResponse - { - public string Joke { get; set; } - } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/Dog.cs b/Geekbot.net/Commands/Randomness/Dog/Dog.cs similarity index 88% rename from Geekbot.net/Commands/Dog.cs rename to Geekbot.net/Commands/Randomness/Dog/Dog.cs index 30c0301..08c1a9b 100644 --- a/Geekbot.net/Commands/Dog.cs +++ b/Geekbot.net/Commands/Randomness/Dog/Dog.cs @@ -4,9 +4,10 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; using Newtonsoft.Json; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Randomness.Dog { public class Dog : ModuleBase { @@ -33,7 +34,7 @@ namespace Geekbot.net.Commands response.EnsureSuccessStatusCode(); var stringResponse = await response.Content.ReadAsStringAsync(); - var dogFile = JsonConvert.DeserializeObject(stringResponse); + var dogFile = JsonConvert.DeserializeObject(stringResponse); var eb = new EmbedBuilder(); eb.ImageUrl = dogFile.Url; await ReplyAsync("", false, eb.Build()); @@ -49,10 +50,5 @@ namespace Geekbot.net.Commands _errorHandler.HandleCommandException(e, Context); } } - - private class DogResponse - { - public string Url { get; set; } - } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/Randomness/Dog/DogResponseDto.cs b/Geekbot.net/Commands/Randomness/Dog/DogResponseDto.cs new file mode 100644 index 0000000..1fc1a82 --- /dev/null +++ b/Geekbot.net/Commands/Randomness/Dog/DogResponseDto.cs @@ -0,0 +1,7 @@ +namespace Geekbot.net.Commands.Randomness.Dog +{ + internal class DogResponseDto + { + public string Url { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/EightBall.cs b/Geekbot.net/Commands/Randomness/EightBall.cs similarity index 95% rename from Geekbot.net/Commands/EightBall.cs rename to Geekbot.net/Commands/Randomness/EightBall.cs index 94b01a1..79ba6d1 100644 --- a/Geekbot.net/Commands/EightBall.cs +++ b/Geekbot.net/Commands/Randomness/EightBall.cs @@ -3,8 +3,9 @@ using System.Collections.Generic; using System.Threading.Tasks; using Discord.Commands; using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Randomness { public class EightBall : ModuleBase { diff --git a/Geekbot.net/Commands/Fortune.cs b/Geekbot.net/Commands/Randomness/Fortune.cs similarity index 89% rename from Geekbot.net/Commands/Fortune.cs rename to Geekbot.net/Commands/Randomness/Fortune.cs index 1335139..abc9ce5 100644 --- a/Geekbot.net/Commands/Fortune.cs +++ b/Geekbot.net/Commands/Randomness/Fortune.cs @@ -3,7 +3,7 @@ using Discord.Commands; using Geekbot.net.Lib; using Geekbot.net.Lib.Media; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Randomness { public class Fortune : ModuleBase { diff --git a/Geekbot.net/Commands/Gdq.cs b/Geekbot.net/Commands/Randomness/Gdq.cs similarity index 89% rename from Geekbot.net/Commands/Gdq.cs rename to Geekbot.net/Commands/Randomness/Gdq.cs index fef1c96..3e17451 100644 --- a/Geekbot.net/Commands/Gdq.cs +++ b/Geekbot.net/Commands/Randomness/Gdq.cs @@ -3,8 +3,9 @@ using System.Net; using System.Threading.Tasks; using Discord.Commands; using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Randomness { public class Gdq : ModuleBase { diff --git a/Geekbot.net/Commands/RandomAnimals.cs b/Geekbot.net/Commands/Randomness/RandomAnimals.cs similarity index 98% rename from Geekbot.net/Commands/RandomAnimals.cs rename to Geekbot.net/Commands/Randomness/RandomAnimals.cs index 7829b21..a96aea8 100644 --- a/Geekbot.net/Commands/RandomAnimals.cs +++ b/Geekbot.net/Commands/Randomness/RandomAnimals.cs @@ -4,7 +4,7 @@ using Discord.Commands; using Geekbot.net.Lib; using Geekbot.net.Lib.Media; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Randomness { public class RandomAnimals : ModuleBase { diff --git a/Geekbot.net/Commands/Ship.cs b/Geekbot.net/Commands/Randomness/Ship.cs similarity index 94% rename from Geekbot.net/Commands/Ship.cs rename to Geekbot.net/Commands/Randomness/Ship.cs index d7dfd0e..ffa18b4 100644 --- a/Geekbot.net/Commands/Ship.cs +++ b/Geekbot.net/Commands/Randomness/Ship.cs @@ -3,9 +3,10 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; using StackExchange.Redis; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Randomness { public class Ship : ModuleBase { diff --git a/Geekbot.net/Commands/Slap.cs b/Geekbot.net/Commands/Randomness/Slap.cs similarity index 94% rename from Geekbot.net/Commands/Slap.cs rename to Geekbot.net/Commands/Randomness/Slap.cs index 668d33e..129e89d 100644 --- a/Geekbot.net/Commands/Slap.cs +++ b/Geekbot.net/Commands/Randomness/Slap.cs @@ -4,9 +4,10 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; using StackExchange.Redis; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Randomness { public class Slap : ModuleBase { diff --git a/Geekbot.net/Commands/GuildInfo.cs b/Geekbot.net/Commands/User/GuildInfo.cs similarity index 83% rename from Geekbot.net/Commands/GuildInfo.cs rename to Geekbot.net/Commands/User/GuildInfo.cs index 62e0fd1..6306a76 100644 --- a/Geekbot.net/Commands/GuildInfo.cs +++ b/Geekbot.net/Commands/User/GuildInfo.cs @@ -4,9 +4,11 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.Levels; using StackExchange.Redis; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.User { public class GuildInfo : ModuleBase { @@ -51,12 +53,5 @@ namespace Geekbot.net.Commands _errorHandler.HandleCommandException(e, Context); } } - - public static string FirstCharToUpper(string input) - { - if (string.IsNullOrEmpty(input)) - throw new ArgumentException("ARGH!"); - return input.First().ToString().ToUpper() + input.Substring(1); - } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/Karma.cs b/Geekbot.net/Commands/User/Karma.cs similarity index 97% rename from Geekbot.net/Commands/Karma.cs rename to Geekbot.net/Commands/User/Karma.cs index d379b06..d6244a8 100644 --- a/Geekbot.net/Commands/Karma.cs +++ b/Geekbot.net/Commands/User/Karma.cs @@ -3,9 +3,11 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.Localization; using StackExchange.Redis; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.User { public class Karma : ModuleBase { diff --git a/Geekbot.net/Commands/Rank.cs b/Geekbot.net/Commands/User/Rank/Rank.cs similarity index 93% rename from Geekbot.net/Commands/Rank.cs rename to Geekbot.net/Commands/User/Rank/Rank.cs index 92f78bd..774ac5a 100644 --- a/Geekbot.net/Commands/Rank.cs +++ b/Geekbot.net/Commands/User/Rank/Rank.cs @@ -6,9 +6,13 @@ using System.Threading.Tasks; using Discord.Commands; using Discord.WebSocket; using Geekbot.net.Lib; +using Geekbot.net.Lib.Converters; +using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.Logger; +using Geekbot.net.Lib.UserRepository; using StackExchange.Redis; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.User.Rank { public class Rank : ModuleBase { @@ -69,7 +73,7 @@ namespace Geekbot.net.Commands } if (type == "Messages") sortedList.RemoveAt(0); - var highscoreUsers = new Dictionary(); + var highscoreUsers = new Dictionary(); var listLimiter = 1; var failedToRetrieveUser = false; foreach (var user in sortedList) @@ -80,7 +84,7 @@ namespace Geekbot.net.Commands var guildUser = _userRepository.Get((ulong) user.Name); if (guildUser.Username != null) { - highscoreUsers.Add(new RankUserPolyfill + highscoreUsers.Add(new RankUserPolyfillDto { Username = guildUser.Username, Discriminator = guildUser.Discriminator @@ -88,7 +92,7 @@ namespace Geekbot.net.Commands } else { - highscoreUsers.Add(new RankUserPolyfill + highscoreUsers.Add(new RankUserPolyfillDto { Id = user.Name }, (int) user.Value); @@ -143,11 +147,4 @@ namespace Geekbot.net.Commands } } } - - internal class RankUserPolyfill - { - public string Username { get; set; } - public string Discriminator { get; set; } - public string Id { get; set; } - } } \ No newline at end of file diff --git a/Geekbot.net/Commands/User/Rank/RankUserPolyfillDto.cs b/Geekbot.net/Commands/User/Rank/RankUserPolyfillDto.cs new file mode 100644 index 0000000..f666ffc --- /dev/null +++ b/Geekbot.net/Commands/User/Rank/RankUserPolyfillDto.cs @@ -0,0 +1,9 @@ +namespace Geekbot.net.Commands.User.Rank +{ + internal class RankUserPolyfillDto + { + public string Username { get; set; } + public string Discriminator { get; set; } + public string Id { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/Stats.cs b/Geekbot.net/Commands/User/Stats.cs similarity index 96% rename from Geekbot.net/Commands/Stats.cs rename to Geekbot.net/Commands/User/Stats.cs index 67b76b9..19a6aa8 100644 --- a/Geekbot.net/Commands/Stats.cs +++ b/Geekbot.net/Commands/User/Stats.cs @@ -3,9 +3,11 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.Levels; using StackExchange.Redis; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.User { public class Stats : ModuleBase { diff --git a/Geekbot.net/Commands/AvatarGetter.cs b/Geekbot.net/Commands/Utils/AvatarGetter.cs similarity index 89% rename from Geekbot.net/Commands/AvatarGetter.cs rename to Geekbot.net/Commands/Utils/AvatarGetter.cs index d8a04e9..d42c779 100644 --- a/Geekbot.net/Commands/AvatarGetter.cs +++ b/Geekbot.net/Commands/Utils/AvatarGetter.cs @@ -3,8 +3,9 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Utils { public class AvatarGetter : ModuleBase { diff --git a/Geekbot.net/Commands/Changelog.cs b/Geekbot.net/Commands/Utils/Changelog/Changelog.cs similarity index 82% rename from Geekbot.net/Commands/Changelog.cs rename to Geekbot.net/Commands/Utils/Changelog/Changelog.cs index 70c4c17..f956d7c 100644 --- a/Geekbot.net/Commands/Changelog.cs +++ b/Geekbot.net/Commands/Utils/Changelog/Changelog.cs @@ -8,9 +8,10 @@ using Discord; using Discord.Commands; using Discord.WebSocket; using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; using Newtonsoft.Json; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Utils.Changelog { public class Changelog : ModuleBase { @@ -51,7 +52,7 @@ namespace Geekbot.net.Commands }); var sb = new StringBuilder(); foreach (var commit in commits.Take(10)) - sb.AppendLine($"- {commit.Commit.Message} ({commit.Commit.Author.Date:yyyy-MM-dd})"); + sb.AppendLine($"- {commit.Commit.Message} ({commit.Commit.AuthorDto.Date:yyyy-MM-dd})"); eb.Description = sb.ToString(); eb.WithFooter(new EmbedFooterBuilder { @@ -65,21 +66,5 @@ namespace Geekbot.net.Commands _errorHandler.HandleCommandException(e, Context); } } - - private class CommitDto - { - public CommitInfo Commit { get; set; } - } - - private class CommitInfo - { - public CommitAuthor Author { get; set; } - public string Message { get; set; } - } - - private class CommitAuthor - { - public DateTimeOffset Date { get; set; } - } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/Utils/Changelog/CommitAuthorDto.cs b/Geekbot.net/Commands/Utils/Changelog/CommitAuthorDto.cs new file mode 100644 index 0000000..ad37311 --- /dev/null +++ b/Geekbot.net/Commands/Utils/Changelog/CommitAuthorDto.cs @@ -0,0 +1,9 @@ +using System; + +namespace Geekbot.net.Commands.Utils.Changelog +{ + internal class CommitAuthorDto + { + public DateTimeOffset Date { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/Utils/Changelog/CommitDto.cs b/Geekbot.net/Commands/Utils/Changelog/CommitDto.cs new file mode 100644 index 0000000..d5c5bab --- /dev/null +++ b/Geekbot.net/Commands/Utils/Changelog/CommitDto.cs @@ -0,0 +1,7 @@ +namespace Geekbot.net.Commands.Utils.Changelog +{ + internal class CommitDto + { + public CommitInfoDto Commit { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/Utils/Changelog/CommitInfoDto.cs b/Geekbot.net/Commands/Utils/Changelog/CommitInfoDto.cs new file mode 100644 index 0000000..102d2cd --- /dev/null +++ b/Geekbot.net/Commands/Utils/Changelog/CommitInfoDto.cs @@ -0,0 +1,8 @@ +namespace Geekbot.net.Commands.Utils.Changelog +{ + internal class CommitInfoDto + { + public CommitAuthorDto AuthorDto { get; set; } + public string Message { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/Choose.cs b/Geekbot.net/Commands/Utils/Choose.cs similarity index 88% rename from Geekbot.net/Commands/Choose.cs rename to Geekbot.net/Commands/Utils/Choose.cs index a726741..4d54654 100644 --- a/Geekbot.net/Commands/Choose.cs +++ b/Geekbot.net/Commands/Utils/Choose.cs @@ -2,8 +2,10 @@ using System.Threading.Tasks; using Discord.Commands; using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.Localization; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Utils { public class Choose : ModuleBase { diff --git a/Geekbot.net/Commands/Dice.cs b/Geekbot.net/Commands/Utils/Dice/Dice.cs similarity index 93% rename from Geekbot.net/Commands/Dice.cs rename to Geekbot.net/Commands/Utils/Dice/Dice.cs index 89b3a11..7fc4f84 100644 --- a/Geekbot.net/Commands/Dice.cs +++ b/Geekbot.net/Commands/Utils/Dice/Dice.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; using Discord.Commands; using Geekbot.net.Lib; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Utils.Dice { public class Dice : ModuleBase { @@ -112,12 +112,4 @@ namespace Geekbot.net.Commands return new DiceTypeDto(); } } - - internal class DiceTypeDto - { - public string DiceType { get; set; } - public int Times { get; set; } - public int Sides { get; set; } - public int Mod { get; set; } - } } \ No newline at end of file diff --git a/Geekbot.net/Commands/Utils/Dice/DiceTypeDto.cs b/Geekbot.net/Commands/Utils/Dice/DiceTypeDto.cs new file mode 100644 index 0000000..5c54792 --- /dev/null +++ b/Geekbot.net/Commands/Utils/Dice/DiceTypeDto.cs @@ -0,0 +1,10 @@ +namespace Geekbot.net.Commands.Utils.Dice +{ + internal class DiceTypeDto + { + public string DiceType { get; set; } + public int Times { get; set; } + public int Sides { get; set; } + public int Mod { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/Emojify.cs b/Geekbot.net/Commands/Utils/Emojify.cs similarity index 89% rename from Geekbot.net/Commands/Emojify.cs rename to Geekbot.net/Commands/Utils/Emojify.cs index 739a4dc..57363c2 100644 --- a/Geekbot.net/Commands/Emojify.cs +++ b/Geekbot.net/Commands/Utils/Emojify.cs @@ -2,8 +2,10 @@ using System.Threading.Tasks; using Discord.Commands; using Geekbot.net.Lib; +using Geekbot.net.Lib.Converters; +using Geekbot.net.Lib.ErrorHandling; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Utils { public class Emojify : ModuleBase { diff --git a/Geekbot.net/Commands/Help.cs b/Geekbot.net/Commands/Utils/Help.cs similarity index 93% rename from Geekbot.net/Commands/Help.cs rename to Geekbot.net/Commands/Utils/Help.cs index 1b3e285..be9a747 100644 --- a/Geekbot.net/Commands/Help.cs +++ b/Geekbot.net/Commands/Utils/Help.cs @@ -4,8 +4,9 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Utils { public class Help : ModuleBase { diff --git a/Geekbot.net/Commands/Info.cs b/Geekbot.net/Commands/Utils/Info.cs similarity index 94% rename from Geekbot.net/Commands/Info.cs rename to Geekbot.net/Commands/Utils/Info.cs index e8c35dc..446abfe 100644 --- a/Geekbot.net/Commands/Info.cs +++ b/Geekbot.net/Commands/Utils/Info.cs @@ -6,9 +6,10 @@ using Discord; using Discord.Commands; using Discord.WebSocket; using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; using StackExchange.Redis; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Utils { public class Info : ModuleBase { diff --git a/Geekbot.net/Commands/Ping.cs b/Geekbot.net/Commands/Utils/Ping.cs similarity index 91% rename from Geekbot.net/Commands/Ping.cs rename to Geekbot.net/Commands/Utils/Ping.cs index c41700e..226e9a3 100644 --- a/Geekbot.net/Commands/Ping.cs +++ b/Geekbot.net/Commands/Utils/Ping.cs @@ -2,7 +2,7 @@ using Discord.Commands; using Geekbot.net.Lib; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Utils { public class Ping : ModuleBase { diff --git a/Geekbot.net/Commands/Poll.cs b/Geekbot.net/Commands/Utils/Poll/Poll.cs similarity index 85% rename from Geekbot.net/Commands/Poll.cs rename to Geekbot.net/Commands/Utils/Poll/Poll.cs index cf80835..5d18a0f 100644 --- a/Geekbot.net/Commands/Poll.cs +++ b/Geekbot.net/Commands/Utils/Poll/Poll.cs @@ -6,10 +6,13 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Geekbot.net.Lib; +using Geekbot.net.Lib.Converters; +using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.UserRepository; using Newtonsoft.Json; using StackExchange.Redis; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Utils.Poll { [Group("poll")] public class Poll : ModuleBase @@ -92,7 +95,7 @@ namespace Geekbot.net.Commands pollMessage.AddReactionAsync(new Emoji(_emojiConverter.NumberToEmoji(i))); i++; }); - var poll = new PollData + var poll = new PollDataDto { Creator = Context.User.Id, MessageId = pollMessage.Id, @@ -139,28 +142,28 @@ namespace Geekbot.net.Commands } } - private PollData GetCurrentPoll() + private PollDataDto GetCurrentPoll() { try { var currentPoll = _redis.HashGet($"{Context.Guild.Id}:Polls", Context.Channel.Id); - return JsonConvert.DeserializeObject(currentPoll.ToString()); + return JsonConvert.DeserializeObject(currentPoll.ToString()); } catch { - return new PollData(); + return new PollDataDto(); } } - private async Task> GetPollResults(PollData poll) + private async Task> GetPollResults(PollDataDto poll) { var message = (IUserMessage) await Context.Channel.GetMessageAsync(poll.MessageId); - var results = new List(); + var results = new List(); foreach (var r in message.Reactions) try { var option = int.Parse(r.Key.Name.ToCharArray()[0].ToString()); - var result = new PollResult + var result = new PollResultDto { Option = poll.Options[option - 1], VoteCount = r.Value.ReactionCount @@ -172,20 +175,5 @@ namespace Geekbot.net.Commands results.Sort((x, y) => y.VoteCount.CompareTo(x.VoteCount)); return results; } - - private class PollData - { - public ulong Creator { get; set; } - public ulong MessageId { get; set; } - public bool IsFinshed { get; set; } - public string Question { get; set; } - public List Options { get; set; } - } - - private class PollResult - { - public string Option { get; set; } - public int VoteCount { get; set; } - } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/Utils/Poll/PollDataDto.cs b/Geekbot.net/Commands/Utils/Poll/PollDataDto.cs new file mode 100644 index 0000000..3d21479 --- /dev/null +++ b/Geekbot.net/Commands/Utils/Poll/PollDataDto.cs @@ -0,0 +1,13 @@ +using System.Collections.Generic; + +namespace Geekbot.net.Commands.Utils.Poll +{ + internal class PollDataDto + { + public ulong Creator { get; set; } + public ulong MessageId { get; set; } + public bool IsFinshed { get; set; } + public string Question { get; set; } + public List Options { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/Utils/Poll/PollResultDto.cs b/Geekbot.net/Commands/Utils/Poll/PollResultDto.cs new file mode 100644 index 0000000..b4f14a2 --- /dev/null +++ b/Geekbot.net/Commands/Utils/Poll/PollResultDto.cs @@ -0,0 +1,8 @@ +namespace Geekbot.net.Commands.Utils.Poll +{ + internal class PollResultDto + { + public string Option { get; set; } + public int VoteCount { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/Quote.cs b/Geekbot.net/Commands/Utils/Quote/Quote.cs similarity index 94% rename from Geekbot.net/Commands/Quote.cs rename to Geekbot.net/Commands/Utils/Quote/Quote.cs index 4b4cfc3..f1941ba 100644 --- a/Geekbot.net/Commands/Quote.cs +++ b/Geekbot.net/Commands/Utils/Quote/Quote.cs @@ -4,10 +4,11 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; using Newtonsoft.Json; using StackExchange.Redis; -namespace Geekbot.net.Commands +namespace Geekbot.net.Commands.Utils.Quote { [Group("quote")] public class Quote : ModuleBase @@ -31,7 +32,7 @@ namespace Geekbot.net.Commands var randomQuotes = _redis.SetMembers($"{Context.Guild.Id}:Quotes"); var randomNumber = new Random().Next(randomQuotes.Length - 1); var randomQuote = randomQuotes[randomNumber]; - var quote = JsonConvert.DeserializeObject(randomQuote); + var quote = JsonConvert.DeserializeObject(randomQuote); var embed = QuoteBuilder(quote, randomNumber + 1); await ReplyAsync("", false, embed.Build()); } @@ -159,7 +160,7 @@ namespace Geekbot.net.Commands var success = _redis.SetRemove($"{Context.Guild.Id}:Quotes", quotes[id - 1]); if (success) { - var quote = JsonConvert.DeserializeObject(quotes[id - 1]); + var quote = JsonConvert.DeserializeObject(quotes[id - 1]); var embed = QuoteBuilder(quote); await ReplyAsync($"**Removed #{id}**", false, embed.Build()); } @@ -186,7 +187,7 @@ namespace Geekbot.net.Commands && !msg.Content.ToLower().StartsWith("!")); } - private EmbedBuilder QuoteBuilder(QuoteObject quote, int id = 0) + private EmbedBuilder QuoteBuilder(QuoteObjectDto quote, int id = 0) { var user = Context.Client.GetUserAsync(quote.UserId).Result; var eb = new EmbedBuilder(); @@ -199,7 +200,7 @@ namespace Geekbot.net.Commands return eb; } - private QuoteObject CreateQuoteObject(IMessage message) + private QuoteObjectDto CreateQuoteObject(IMessage message) { string image; try @@ -211,7 +212,7 @@ namespace Geekbot.net.Commands image = null; } - return new QuoteObject + return new QuoteObjectDto { UserId = message.Author.Id, Time = message.Timestamp.DateTime, @@ -220,12 +221,4 @@ namespace Geekbot.net.Commands }; } } - - public class QuoteObject - { - public ulong UserId { get; set; } - public string Quote { get; set; } - public DateTime Time { get; set; } - public string Image { get; set; } - } } \ No newline at end of file diff --git a/Geekbot.net/Commands/Utils/Quote/QuoteObjectDto.cs b/Geekbot.net/Commands/Utils/Quote/QuoteObjectDto.cs new file mode 100644 index 0000000..a37ff76 --- /dev/null +++ b/Geekbot.net/Commands/Utils/Quote/QuoteObjectDto.cs @@ -0,0 +1,12 @@ +using System; + +namespace Geekbot.net.Commands.Utils.Quote +{ + internal class QuoteObjectDto + { + public ulong UserId { get; set; } + public string Quote { get; set; } + public DateTime Time { get; set; } + public string Image { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/Voice.cs b/Geekbot.net/Commands/Voice.cs deleted file mode 100644 index ca65913..0000000 --- a/Geekbot.net/Commands/Voice.cs +++ /dev/null @@ -1,102 +0,0 @@ -//using System; -//using System.IO; -//using System.Threading.Tasks; -//using Discord; -//using Discord.Commands; -//using Geekbot.net.Lib; -// -//namespace Geekbot.net.Commands -//{ -// public class Voice : ModuleBase -// { -// private readonly IAudioUtils _audioUtils; -// private readonly IErrorHandler _errorHandler; -// -// public Voice(IErrorHandler errorHandler, IAudioUtils audioUtils) -// { -// _errorHandler = errorHandler; -// _audioUtils = audioUtils; -// } -// -// [Command("join")] -// public async Task JoinChannel() -// { -// try -// { -// // Get the audio channel -// var channel = (Context.User as IGuildUser)?.VoiceChannel; -// if (channel == null) -// { -// await Context.Channel.SendMessageAsync( -// "User must be in a voice channel, or a voice channel must be passed as an argument."); -// return; -// } -// -// // For the next step with transmitting audio, you would want to pass this Audio Client in to a service. -// var audioClient = await channel.ConnectAsync(); -// _audioUtils.StoreAudioClient(Context.Guild.Id, audioClient); -// await ReplyAsync($"Connected to {channel.Name}"); -// } -// catch (Exception e) -// { -// _errorHandler.HandleCommandException(e, Context); -// } -// } -// -// [Command("disconnect")] -// public async Task DisconnectChannel() -// { -// try -// { -// var audioClient = _audioUtils.GetAudioClient(Context.Guild.Id); -// if (audioClient == null) -// { -// await Context.Channel.SendMessageAsync("I'm not in a voice channel at the moment"); -// return; -// } -// -// await audioClient.StopAsync(); -// await ReplyAsync("Disconnected from channel!"); -// _audioUtils.Cleanup(Context.Guild.Id); -// } -// catch (Exception e) -// { -// _errorHandler.HandleCommandException(e, Context); -// _audioUtils.Cleanup(Context.Guild.Id); -// } -// } -// -// [Command("ytplay")] -// public async Task ytplay(string url) -// { -// try -// { -// if (!url.Contains("youtube")) -// { -// await ReplyAsync("I can only play youtube videos"); -// return; -// } -// var audioClient = _audioUtils.GetAudioClient(Context.Guild.Id); -// if (audioClient == null) -// { -// await ReplyAsync("I'm not in a voice channel at the moment"); -// return; -// } -// -// var message = await Context.Channel.SendMessageAsync("Just a second, i'm still a bit slow at this"); -// var ffmpeg = _audioUtils.CreateStreamFromYoutube(url, Context.Guild.Id); -// var output = ffmpeg.StandardOutput.BaseStream; -// await message.ModifyAsync(msg => msg.Content = "**Playing!** Please note that this feature is experimental"); -// var discord = audioClient.CreatePCMStream(Discord.Audio.AudioApplication.Mixed); -// await output.CopyToAsync(discord); -// await discord.FlushAsync(); -// _audioUtils.Cleanup(Context.Guild.Id); -// } -// catch (Exception e) -// { -// _errorHandler.HandleCommandException(e, Context); -// _audioUtils.Cleanup(Context.Guild.Id); -// } -// } -// } -//} \ No newline at end of file diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index ba9faed..5cba28a 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -63,9 +63,6 @@ PreserveNewest - - PreserveNewest - PreserveNewest @@ -75,6 +72,12 @@ PreserveNewest + + Always + + + Always +
diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index 31608b2..7d1523b 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -4,7 +4,9 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Discord.WebSocket; -using Geekbot.net.Lib; +using Geekbot.net.Lib.Logger; +using Geekbot.net.Lib.ReactionListener; +using Geekbot.net.Lib.UserRepository; using StackExchange.Redis; namespace Geekbot.net @@ -89,7 +91,6 @@ namespace Geekbot.net if (message.Author.IsBot) return Task.CompletedTask; _logger.Information("Message", message.Content, SimpleConextConverter.ConvertSocketMessage(message)); -// _logger.Information($"[Message] {channel.Guild.Name} ({channel.Guild.Id}) - {message.Channel} ({message.Channel.Id}) - {message.Author.Username}#{message.Author.Discriminator} ({message.Author.Id}) - {message.Content}"); } catch (Exception e) { diff --git a/Geekbot.net/Lib/Clients/IMalClient.cs b/Geekbot.net/Lib/Clients/IMalClient.cs new file mode 100644 index 0000000..f59c511 --- /dev/null +++ b/Geekbot.net/Lib/Clients/IMalClient.cs @@ -0,0 +1,12 @@ +using System.Threading.Tasks; +using MyAnimeListSharp.Core; + +namespace Geekbot.net.Lib.Clients +{ + public interface IMalClient + { + bool IsLoggedIn(); + Task GetAnime(string query); + Task GetManga(string query); + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/MalClient.cs b/Geekbot.net/Lib/Clients/MalClient.cs similarity index 89% rename from Geekbot.net/Lib/MalClient.cs rename to Geekbot.net/Lib/Clients/MalClient.cs index dcf4fe3..d8bfe41 100644 --- a/Geekbot.net/Lib/MalClient.cs +++ b/Geekbot.net/Lib/Clients/MalClient.cs @@ -1,10 +1,11 @@ using System.Threading.Tasks; +using Geekbot.net.Lib.Logger; using MyAnimeListSharp.Auth; using MyAnimeListSharp.Core; using MyAnimeListSharp.Facade.Async; using StackExchange.Redis; -namespace Geekbot.net.Lib +namespace Geekbot.net.Lib.Clients { public class MalClient : IMalClient { @@ -66,11 +67,4 @@ namespace Geekbot.net.Lib return response.Entries.Count == 0 ? null : response.Entries[0]; } } - - public interface IMalClient - { - bool IsLoggedIn(); - Task GetAnime(string query); - Task GetManga(string query); - } } \ No newline at end of file diff --git a/Geekbot.net/Lib/EmojiConverter.cs b/Geekbot.net/Lib/Converters/EmojiConverter.cs similarity index 92% rename from Geekbot.net/Lib/EmojiConverter.cs rename to Geekbot.net/Lib/Converters/EmojiConverter.cs index b836485..06f1aa8 100644 --- a/Geekbot.net/Lib/EmojiConverter.cs +++ b/Geekbot.net/Lib/Converters/EmojiConverter.cs @@ -1,7 +1,7 @@ using System.Collections; using System.Text; -namespace Geekbot.net.Lib +namespace Geekbot.net.Lib.Converters { public class EmojiConverter : IEmojiConverter { @@ -90,10 +90,4 @@ namespace Geekbot.net.Lib return returnString.ToString(); } } - - public interface IEmojiConverter - { - string NumberToEmoji(int number); - string TextToEmoji(string text); - } } \ No newline at end of file diff --git a/Geekbot.net/Lib/Converters/IEmojiConverter.cs b/Geekbot.net/Lib/Converters/IEmojiConverter.cs new file mode 100644 index 0000000..b0f666f --- /dev/null +++ b/Geekbot.net/Lib/Converters/IEmojiConverter.cs @@ -0,0 +1,8 @@ +namespace Geekbot.net.Lib.Converters +{ + public interface IEmojiConverter + { + string NumberToEmoji(int number); + string TextToEmoji(string text); + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/Converters/IMtgManaConverter.cs b/Geekbot.net/Lib/Converters/IMtgManaConverter.cs new file mode 100644 index 0000000..d558f09 --- /dev/null +++ b/Geekbot.net/Lib/Converters/IMtgManaConverter.cs @@ -0,0 +1,7 @@ +namespace Geekbot.net.Lib.Converters +{ + public interface IMtgManaConverter + { + string ConvertMana(string mana); + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/Converters/MtgManaConverter.cs b/Geekbot.net/Lib/Converters/MtgManaConverter.cs new file mode 100644 index 0000000..c29e1e0 --- /dev/null +++ b/Geekbot.net/Lib/Converters/MtgManaConverter.cs @@ -0,0 +1,33 @@ +using System.Collections.Generic; +using System.IO; +using System.Text.RegularExpressions; +using Utf8Json; + +namespace Geekbot.net.Lib.Converters +{ + public class MtgManaConverter : IMtgManaConverter + { + private Dictionary _manaDict; + + public MtgManaConverter() + { + // these emotes can be found at https://discord.gg/bz8HyA7 + var mtgEmojis = File.ReadAllText(Path.GetFullPath("./Lib/Converters/MtgManaEmojis.json")); + _manaDict = JsonSerializer.Deserialize>(mtgEmojis); + } + + public string ConvertMana(string mana) + { + var rgx = Regex.Matches(mana, @"(\{(.*?)\})"); + foreach (Match manaTypes in rgx) + { + var m = _manaDict.GetValueOrDefault(manaTypes.Value); + if (!string.IsNullOrEmpty(m)) + { + mana = mana.Replace(manaTypes.Value, m); + } + } + return mana; + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/Converters/MtgManaEmojis.json b/Geekbot.net/Lib/Converters/MtgManaEmojis.json new file mode 100644 index 0000000..8ebe75b --- /dev/null +++ b/Geekbot.net/Lib/Converters/MtgManaEmojis.json @@ -0,0 +1,50 @@ +{ + "{0}": "<:mtg_0:415216130043412482>", + "{1}": "<:mtg_1:415216130253389835>", + "{2}": "<:mtg_2:415216130031091713>", + "{3}": "<:mtg_3:415216130467037194>", + "{4}": "<:mtg_4:415216130026635295>", + "{5}": "<:mtg_5:415216130492203008>", + "{6}": "<:mtg_6:415216130458779658>", + "{7}": "<:mtg_7:415216130190475265>", + "{8}": "<:mtg_8:415216130517630986>", + "{9}": "<:mtg_9:415216130500722689>", + "{10": "<:mtg_10:415216130450391051>", + "{11}": "<:mtg_11:415216130811101185>", + "{12}": "<:mtg_12:415216130525888532>", + "{13}": "<:mtg_13:415216130517631000>", + "{14}": "<:mtg_14:415216130165178370>", + "{15}": "<:mtg_15:415216130576089108>", + "{16}": "<:mtg_16:415216130358247425>", + "{17}": "<:mtg_17:415216130601517056>", + "{18}": "<:mtg_18:415216130462842891>", + "{19}": "<:mtg_19:415216130614099988>", + "{20}": "<:mtg_20:415216130656043038>", + "{W}": "<:mtg_white:415216131515744256>", + "{U}": "<:mtg_blue:415216130521694209>", + "{B}": "<:mtg_black:415216130873884683>", + "{R}": "<:mtg_red:415216131322806272>", + "{G}": "<:mtg_green:415216131180331009>", + "{S}": "<:mtg_s:415216131293446144>", + "{T}": "<:mtg_tap:415258392727257088>", + "{C}": "<:mtg_colorless:415216130706374666>", + "{2/W}": "<:mtg_2w:415216130446065664>", + "{2/U}": "<:mtg_2u:415216130429550592>", + "{2/B}": "<:mtg_2b:415216130160984065>", + "{2/R}": "<:mtg_2r:415216130454716436>", + "{2/G}": "<:mtg_2g:415216130420899840>", + "{W/U}": "<:mtg_wu:415216130970484736>", + "{W/B}": "<:mtg_wb:415216131222011914>", + "{U/R}": "<:mtg_ur:415216130962096128>", + "{U/B}": "<:mtg_ub:415216130865758218>", + "{R/W}": "<:mtg_rw:415216130878210057>", + "{G/W}": "<:mtg_gw:415216130567962646>", + "{G/U}": "<:mtg_gu:415216130739666945>", + "{B/R}": "<:mtg_br:415216130580283394>", + "{B/G}": "<:mtg_bg:415216130781609994>", + "{U/P}": "<:mtg_up:415216130861432842>", + "{R/P}": "<:mtg_rp:415216130597322783>", + "{G/P}": "<:mtg_gp:415216130760769546>", + "{W/P}": "<:mtg_wp:415216131541041172>", + "{B/P}": "<:mtg_bp:415216130664169482>" +} \ No newline at end of file diff --git a/Geekbot.net/Lib/ErrorHandler.cs b/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs similarity index 93% rename from Geekbot.net/Lib/ErrorHandler.cs rename to Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs index 9bf99f4..3d53242 100644 --- a/Geekbot.net/Lib/ErrorHandler.cs +++ b/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs @@ -2,10 +2,12 @@ using System.Net; using Discord.Commands; using Discord.Net; +using Geekbot.net.Lib.Localization; +using Geekbot.net.Lib.Logger; using SharpRaven; using SharpRaven.Data; -namespace Geekbot.net.Lib +namespace Geekbot.net.Lib.ErrorHandling { public class ErrorHandler : IErrorHandler { @@ -90,10 +92,4 @@ namespace Geekbot.net.Lib } - - public interface IErrorHandler - { - void HandleCommandException(Exception e, ICommandContext context, string errorMessage = "def"); - void HandleHttpException(HttpException e, ICommandContext context); - } } \ No newline at end of file diff --git a/Geekbot.net/Lib/ErrorHandling/IErrorHandler.cs b/Geekbot.net/Lib/ErrorHandling/IErrorHandler.cs new file mode 100644 index 0000000..f2d8dac --- /dev/null +++ b/Geekbot.net/Lib/ErrorHandling/IErrorHandler.cs @@ -0,0 +1,12 @@ +using System; +using Discord.Commands; +using Discord.Net; + +namespace Geekbot.net.Lib.ErrorHandling +{ + public interface IErrorHandler + { + void HandleCommandException(Exception e, ICommandContext context, string errorMessage = "def"); + void HandleHttpException(HttpException e, ICommandContext context); + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/Levels/ILevelCalc.cs b/Geekbot.net/Lib/Levels/ILevelCalc.cs new file mode 100644 index 0000000..6353132 --- /dev/null +++ b/Geekbot.net/Lib/Levels/ILevelCalc.cs @@ -0,0 +1,7 @@ +namespace Geekbot.net.Lib.Levels +{ + public interface ILevelCalc + { + int GetLevel(int experience); + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/LevelCalc.cs b/Geekbot.net/Lib/Levels/LevelCalc.cs similarity index 87% rename from Geekbot.net/Lib/LevelCalc.cs rename to Geekbot.net/Lib/Levels/LevelCalc.cs index 75df83d..9ecf803 100644 --- a/Geekbot.net/Lib/LevelCalc.cs +++ b/Geekbot.net/Lib/Levels/LevelCalc.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; -namespace Geekbot.net.Lib +namespace Geekbot.net.Lib.Levels { public class LevelCalc : ILevelCalc { @@ -30,9 +30,4 @@ namespace Geekbot.net.Lib return returnVal; } } - - public interface ILevelCalc - { - int GetLevel(int experience); - } } \ No newline at end of file diff --git a/Geekbot.net/Lib/Localization/ITranslationHandler.cs b/Geekbot.net/Lib/Localization/ITranslationHandler.cs new file mode 100644 index 0000000..3cd9549 --- /dev/null +++ b/Geekbot.net/Lib/Localization/ITranslationHandler.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; +using Discord.Commands; + +namespace Geekbot.net.Lib.Localization +{ + public interface ITranslationHandler + { + string GetString(ulong guildId, string command, string stringName); + Dictionary GetDict(ICommandContext context); + Dictionary GetDict(ICommandContext context, string command); + bool SetLanguage(ulong guildId, string language); + List GetSupportedLanguages(); + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/TranslationHandler.cs b/Geekbot.net/Lib/Localization/TranslationHandler.cs similarity index 90% rename from Geekbot.net/Lib/TranslationHandler.cs rename to Geekbot.net/Lib/Localization/TranslationHandler.cs index 5324496..e57ee8d 100644 --- a/Geekbot.net/Lib/TranslationHandler.cs +++ b/Geekbot.net/Lib/Localization/TranslationHandler.cs @@ -4,10 +4,11 @@ using System.IO; using System.Linq; using Discord.Commands; using Discord.WebSocket; +using Geekbot.net.Lib.Logger; using StackExchange.Redis; using Utf8Json; -namespace Geekbot.net.Lib +namespace Geekbot.net.Lib.Localization { public class TranslationHandler : ITranslationHandler { @@ -30,7 +31,7 @@ namespace Geekbot.net.Lib { try { - var translationFile = File.ReadAllText(Path.GetFullPath("./Storage/Translations.json")); + var translationFile = File.ReadAllText(Path.GetFullPath("./Lib/Localization/Translations.json")); var rawTranslations = JsonSerializer.Deserialize>>>(translationFile); var sortedPerLanguage = new Dictionary>>(); foreach (var command in rawTranslations) @@ -152,13 +153,4 @@ namespace Geekbot.net.Lib return _supportedLanguages; } } - - public interface ITranslationHandler - { - string GetString(ulong guildId, string command, string stringName); - Dictionary GetDict(ICommandContext context); - Dictionary GetDict(ICommandContext context, string command); - bool SetLanguage(ulong guildId, string language); - List GetSupportedLanguages(); - } } \ No newline at end of file diff --git a/Geekbot.net/Storage/Translations.json b/Geekbot.net/Lib/Localization/Translations.json similarity index 100% rename from Geekbot.net/Storage/Translations.json rename to Geekbot.net/Lib/Localization/Translations.json diff --git a/Geekbot.net/Lib/Logger/DiscordLogger.cs b/Geekbot.net/Lib/Logger/DiscordLogger.cs new file mode 100644 index 0000000..fc12201 --- /dev/null +++ b/Geekbot.net/Lib/Logger/DiscordLogger.cs @@ -0,0 +1,42 @@ +using System.Threading.Tasks; +using Discord; + +namespace Geekbot.net.Lib.Logger +{ + public class DiscordLogger : IDiscordLogger + { + private readonly GeekbotLogger _logger; + + public DiscordLogger(GeekbotLogger logger) + { + _logger = logger; + } + + public Task Log(LogMessage message) + { + var logMessage = $"[{message.Source}] {message.Message}"; + switch (message.Severity) + { + case LogSeverity.Verbose: + _logger.Trace(message.Source, message.Message); + break; + case LogSeverity.Debug: + _logger.Debug(message.Source, message.Message); + break; + case LogSeverity.Info: + _logger.Information(message.Source, message.Message); + break; + case LogSeverity.Critical: + case LogSeverity.Error: + case LogSeverity.Warning: + if (logMessage.Contains("VOICE_STATE_UPDATE")) break; + _logger.Error(message.Source, message.Message, message.Exception); + break; + default: + _logger.Information(message.Source, $"{logMessage} --- {message.Severity}"); + break; + } + return Task.CompletedTask; + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/GeekbotLogger.cs b/Geekbot.net/Lib/Logger/GeekbotLogger.cs similarity index 75% rename from Geekbot.net/Lib/GeekbotLogger.cs rename to Geekbot.net/Lib/Logger/GeekbotLogger.cs index 7093a7c..8cefdb4 100644 --- a/Geekbot.net/Lib/GeekbotLogger.cs +++ b/Geekbot.net/Lib/Logger/GeekbotLogger.cs @@ -1,7 +1,7 @@ using System; using Newtonsoft.Json; -namespace Geekbot.net.Lib +namespace Geekbot.net.Lib.Logger { public class GeekbotLogger : IGeekbotLogger { @@ -21,6 +21,11 @@ namespace Geekbot.net.Lib Information("Geekbot", "Using GeekbotLogger"); } + public void Trace(string source, string message, object extra = null) + { + _logger.Trace(CreateLogString("Debug", source, message, null, extra)); + } + public void Debug(string source, string message, object extra = null) { _logger.Debug(CreateLogString("Debug", source, message, null, extra)); @@ -63,22 +68,4 @@ namespace Geekbot.net.Lib return $"[{source}] - [{m.Guild.Name} - {m.Channel.Name}] {m.User.Name}: {m.Message.Content}"; } } - - public class GeekbotLoggerObject - { - public DateTime Timestamp { get; set; } - public string Type { get; set; } - public string Source { get; set; } - public string Message { get; set; } - public Exception StackTrace { get; set; } - public object Extra { get; set; } - } - - public interface IGeekbotLogger - { - void Debug(string source, string message, object extra = null); - void Information(string source, string message, object extra = null); - void Warning(string source, string message, Exception stackTrace = null, object extra = null); - void Error(string source, string message, Exception stackTrace, object extra = null); - } } \ No newline at end of file diff --git a/Geekbot.net/Lib/Logger/IDiscordLogger.cs b/Geekbot.net/Lib/Logger/IDiscordLogger.cs new file mode 100644 index 0000000..fdc8c7f --- /dev/null +++ b/Geekbot.net/Lib/Logger/IDiscordLogger.cs @@ -0,0 +1,10 @@ +using System.Threading.Tasks; +using Discord; + +namespace Geekbot.net.Lib.Logger +{ + public interface IDiscordLogger + { + Task Log(LogMessage message); + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/Logger/IGeekbotLogger.cs b/Geekbot.net/Lib/Logger/IGeekbotLogger.cs new file mode 100644 index 0000000..36d456d --- /dev/null +++ b/Geekbot.net/Lib/Logger/IGeekbotLogger.cs @@ -0,0 +1,13 @@ +using System; + +namespace Geekbot.net.Lib.Logger +{ + public interface IGeekbotLogger + { + void Trace(string source, string message, object extra = null); + void Debug(string source, string message, object extra = null); + void Information(string source, string message, object extra = null); + void Warning(string source, string message, Exception stackTrace = null, object extra = null); + void Error(string source, string message, Exception stackTrace, object extra = null); + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/Logger/LogDto.cs b/Geekbot.net/Lib/Logger/LogDto.cs new file mode 100644 index 0000000..74695ec --- /dev/null +++ b/Geekbot.net/Lib/Logger/LogDto.cs @@ -0,0 +1,14 @@ +using System; + +namespace Geekbot.net.Lib.Logger +{ + public class GeekbotLoggerObject + { + public DateTime Timestamp { get; set; } + public string Type { get; set; } + public string Source { get; set; } + public string Message { get; set; } + public Exception StackTrace { get; set; } + public object Extra { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/LoggerFactory.cs b/Geekbot.net/Lib/Logger/LoggerFactory.cs similarity index 95% rename from Geekbot.net/Lib/LoggerFactory.cs rename to Geekbot.net/Lib/Logger/LoggerFactory.cs index b0b5060..53325a6 100644 --- a/Geekbot.net/Lib/LoggerFactory.cs +++ b/Geekbot.net/Lib/Logger/LoggerFactory.cs @@ -5,11 +5,11 @@ using NLog.Config; using NLog.Targets; using SumoLogic.Logging.NLog; -namespace Geekbot.net.Lib +namespace Geekbot.net.Lib.Logger { public class LoggerFactory { - public static Logger CreateNLog(RunParameters runParameters, bool sumologicActive) + public static NLog.Logger CreateNLog(RunParameters runParameters, bool sumologicActive) { var config = new LoggingConfiguration(); diff --git a/Geekbot.net/Lib/Logger/MessageDto.cs b/Geekbot.net/Lib/Logger/MessageDto.cs new file mode 100644 index 0000000..011acf3 --- /dev/null +++ b/Geekbot.net/Lib/Logger/MessageDto.cs @@ -0,0 +1,26 @@ +namespace Geekbot.net.Lib.Logger +{ + public class MessageDto + { + public MessageContent Message { get; set; } + public IdAndName User { get; set; } + public IdAndName Guild { get; set; } + public IdAndName Channel { get; set; } + + public class MessageContent + { + public string Content { get; set; } + public string Id { get; set; } + public int Attachments { get; set; } + public int ChannelMentions { get; set; } + public int UserMentions { get; set; } + public int RoleMentions { get; set; } + } + + public class IdAndName + { + public string Id { get; set; } + public string Name { get; set; } + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/SimpleConextConverter.cs b/Geekbot.net/Lib/Logger/SimpleConextConverter.cs similarity index 75% rename from Geekbot.net/Lib/SimpleConextConverter.cs rename to Geekbot.net/Lib/Logger/SimpleConextConverter.cs index ad029c7..4df802d 100644 --- a/Geekbot.net/Lib/SimpleConextConverter.cs +++ b/Geekbot.net/Lib/Logger/SimpleConextConverter.cs @@ -1,7 +1,7 @@ using Discord.Commands; using Discord.WebSocket; -namespace Geekbot.net.Lib +namespace Geekbot.net.Lib.Logger { public class SimpleConextConverter { @@ -68,29 +68,4 @@ namespace Geekbot.net.Lib } } - - - public class MessageDto - { - public MessageContent Message { get; set; } - public IdAndName User { get; set; } - public IdAndName Guild { get; set; } - public IdAndName Channel { get; set; } - - public class MessageContent - { - public string Content { get; set; } - public string Id { get; set; } - public int Attachments { get; set; } - public int ChannelMentions { get; set; } - public int UserMentions { get; set; } - public int RoleMentions { get; set; } - } - - public class IdAndName - { - public string Id { get; set; } - public string Name { get; set; } - } - } } \ No newline at end of file diff --git a/Geekbot.net/Lib/Media/FortunesProvider.cs b/Geekbot.net/Lib/Media/FortunesProvider.cs index 5988660..dc97199 100644 --- a/Geekbot.net/Lib/Media/FortunesProvider.cs +++ b/Geekbot.net/Lib/Media/FortunesProvider.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using Geekbot.net.Lib.Logger; namespace Geekbot.net.Lib.Media { @@ -16,7 +17,7 @@ namespace Geekbot.net.Lib.Media var rawFortunes = File.ReadAllText(path); _fortuneArray = rawFortunes.Split("%"); _totalFortunes = _fortuneArray.Length; - logger.Debug("Geekbot", "Loaded {totalFortunes} Fortunes"); + logger.Trace("Geekbot", $"Loaded {_totalFortunes} Fortunes"); } else { @@ -29,9 +30,4 @@ namespace Geekbot.net.Lib.Media return _fortuneArray[new Random().Next(0, _totalFortunes)]; } } - - public interface IFortunesProvider - { - string GetRandomFortune(); - } } \ No newline at end of file diff --git a/Geekbot.net/Lib/Media/IFortunesProvider.cs b/Geekbot.net/Lib/Media/IFortunesProvider.cs new file mode 100644 index 0000000..c82e45c --- /dev/null +++ b/Geekbot.net/Lib/Media/IFortunesProvider.cs @@ -0,0 +1,7 @@ +namespace Geekbot.net.Lib.Media +{ + public interface IFortunesProvider + { + string GetRandomFortune(); + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/Media/IMediaProvider.cs b/Geekbot.net/Lib/Media/IMediaProvider.cs new file mode 100644 index 0000000..79c6c98 --- /dev/null +++ b/Geekbot.net/Lib/Media/IMediaProvider.cs @@ -0,0 +1,14 @@ +namespace Geekbot.net.Lib.Media +{ + public interface IMediaProvider + { + string GetCheckem(); + string GetPanda(); + string GetCrossant(); + string GetSquirrel(); + string GetPumpkin(); + string GetTurtle(); + string GetPinguin(); + string GetFox(); + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/Media/MediaProvider.cs b/Geekbot.net/Lib/Media/MediaProvider.cs index 568f67d..8f28d0d 100644 --- a/Geekbot.net/Lib/Media/MediaProvider.cs +++ b/Geekbot.net/Lib/Media/MediaProvider.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using Geekbot.net.Lib.Logger; namespace Geekbot.net.Lib.Media { @@ -37,56 +38,56 @@ namespace Geekbot.net.Lib.Media { var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/checkEmPics")); _checkemImages = rawLinks.Split("\n"); - _logger.Debug("Geekbot", $"Loaded {_checkemImages.Length} CheckEm Images"); + _logger.Trace("Geekbot", $"Loaded {_checkemImages.Length} CheckEm Images"); } private void LoadPandas() { var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/pandas")); _pandaImages = rawLinks.Split("\n"); - _logger.Debug("Geekbot", $"Loaded {_pandaImages.Length} Panda Images"); + _logger.Trace("Geekbot", $"Loaded {_pandaImages.Length} Panda Images"); } private void BakeCroissants() { var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/croissant")); _croissantImages = rawLinks.Split("\n"); - _logger.Debug("Geekbot", $"Loaded {_croissantImages.Length} Croissant Images"); + _logger.Trace("Geekbot", $"Loaded {_croissantImages.Length} Croissant Images"); } private void LoadSquirrels() { var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/squirrel")); _squirrelImages = rawLinks.Split("\n"); - _logger.Debug("Geekbot", $"Loaded {_squirrelImages.Length} Squirrel Images"); + _logger.Trace("Geekbot", $"Loaded {_squirrelImages.Length} Squirrel Images"); } private void LoadPumpkins() { var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/pumpkin")); _pumpkinImages = rawLinks.Split("\n"); - _logger.Debug("Geekbot", $"Loaded {_pumpkinImages.Length} Pumpkin Images"); + _logger.Trace("Geekbot", $"Loaded {_pumpkinImages.Length} Pumpkin Images"); } private void LoadTurtles() { var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/turtles")); _turtlesImages = rawLinks.Split("\n"); - _logger.Debug("Geekbot", $"Loaded {_turtlesImages.Length} Turtle Images"); + _logger.Trace("Geekbot", $"Loaded {_turtlesImages.Length} Turtle Images"); } private void LoadPinguins() { var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/pinguins")); _pinguinImages = rawLinks.Split("\n"); - _logger.Debug("Geekbot", $"Loaded {_pinguinImages.Length} Pinguin Images"); + _logger.Trace("Geekbot", $"Loaded {_pinguinImages.Length} Pinguin Images"); } private void LoadFoxes() { var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/foxes")); _foxImages = rawLinks.Split("\n"); - _logger.Debug("Geekbot", $"Loaded {_foxImages.Length} Foxes Images"); + _logger.Trace("Geekbot", $"Loaded {_foxImages.Length} Foxes Images"); } public string GetCheckem() @@ -129,16 +130,4 @@ namespace Geekbot.net.Lib.Media return _foxImages[_random.Next(0, _foxImages.Length)]; } } - - public interface IMediaProvider - { - string GetCheckem(); - string GetPanda(); - string GetCrossant(); - string GetSquirrel(); - string GetPumpkin(); - string GetTurtle(); - string GetPinguin(); - string GetFox(); - } } \ No newline at end of file diff --git a/Geekbot.net/Lib/MtgManaConverter.cs b/Geekbot.net/Lib/MtgManaConverter.cs deleted file mode 100644 index 83b6767..0000000 --- a/Geekbot.net/Lib/MtgManaConverter.cs +++ /dev/null @@ -1,86 +0,0 @@ -using System.Collections.Generic; -using System.Text.RegularExpressions; - -namespace Geekbot.net.Lib -{ - public class MtgManaConverter : IMtgManaConverter - { - private Dictionary _manaDict; - - public MtgManaConverter() - { - // these emotes can be found at https://discord.gg/bz8HyA7 - - var manaDict = new Dictionary(); - manaDict.Add("{0}", "<:mtg_0:415216130043412482>"); - manaDict.Add("{1}", "<:mtg_1:415216130253389835>"); - manaDict.Add("{2}", "<:mtg_2:415216130031091713>"); - manaDict.Add("{3}", "<:mtg_3:415216130467037194>"); - manaDict.Add("{4}", "<:mtg_4:415216130026635295>"); - manaDict.Add("{5}", "<:mtg_5:415216130492203008>"); - manaDict.Add("{6}", "<:mtg_6:415216130458779658>"); - manaDict.Add("{7}", "<:mtg_7:415216130190475265>"); - manaDict.Add("{8}", "<:mtg_8:415216130517630986>"); - manaDict.Add("{9}", "<:mtg_9:415216130500722689>"); - manaDict.Add("{10", "<:mtg_10:415216130450391051>"); - manaDict.Add("{11}", "<:mtg_11:415216130811101185>"); - manaDict.Add("{12}", "<:mtg_12:415216130525888532>"); - manaDict.Add("{13}", "<:mtg_13:415216130517631000>"); - manaDict.Add("{14}", "<:mtg_14:415216130165178370>"); - manaDict.Add("{15}", "<:mtg_15:415216130576089108>"); - manaDict.Add("{16}", "<:mtg_16:415216130358247425>"); - manaDict.Add("{17}", "<:mtg_17:415216130601517056>"); - manaDict.Add("{18}", "<:mtg_18:415216130462842891>"); - manaDict.Add("{19}", "<:mtg_19:415216130614099988>"); - manaDict.Add("{20}", "<:mtg_20:415216130656043038>"); - manaDict.Add("{W}", "<:mtg_white:415216131515744256>"); - manaDict.Add("{U}", "<:mtg_blue:415216130521694209>"); - manaDict.Add("{B}", "<:mtg_black:415216130873884683>"); - manaDict.Add("{R}", "<:mtg_red:415216131322806272>"); - manaDict.Add("{G}", "<:mtg_green:415216131180331009>"); - manaDict.Add("{S}", "<:mtg_s:415216131293446144>"); - manaDict.Add("{T}", "<:mtg_tap:415258392727257088>"); - manaDict.Add("{C}", "<:mtg_colorless:415216130706374666>"); - manaDict.Add("{2/W}", "<:mtg_2w:415216130446065664>"); - manaDict.Add("{2/U}", "<:mtg_2u:415216130429550592>"); - manaDict.Add("{2/B}", "<:mtg_2b:415216130160984065>"); - manaDict.Add("{2/R}", "<:mtg_2r:415216130454716436>"); - manaDict.Add("{2/G}", "<:mtg_2g:415216130420899840>"); - manaDict.Add("{W/U}", "<:mtg_wu:415216130970484736>"); - manaDict.Add("{W/B}", "<:mtg_wb:415216131222011914>"); - manaDict.Add("{U/R}", "<:mtg_ur:415216130962096128>"); - manaDict.Add("{U/B}", "<:mtg_ub:415216130865758218>"); - manaDict.Add("{R/W}", "<:mtg_rw:415216130878210057>"); - manaDict.Add("{G/W}", "<:mtg_gw:415216130567962646>"); - manaDict.Add("{G/U}", "<:mtg_gu:415216130739666945>"); - manaDict.Add("{B/R}", "<:mtg_br:415216130580283394>"); - manaDict.Add("{B/G}", "<:mtg_bg:415216130781609994>"); - manaDict.Add("{U/P}", "<:mtg_up:415216130861432842>"); - manaDict.Add("{R/P}", "<:mtg_rp:415216130597322783>"); - manaDict.Add("{G/P}", "<:mtg_gp:415216130760769546>"); - manaDict.Add("{W/P}", "<:mtg_wp:415216131541041172>"); - manaDict.Add("{B/P}", "<:mtg_bp:415216130664169482>"); - - _manaDict = manaDict; - } - - public string ConvertMana(string mana) - { - var rgx = Regex.Matches(mana, @"(\{(.*?)\})"); - foreach (Match manaTypes in rgx) - { - var m = _manaDict.GetValueOrDefault(manaTypes.Value); - if (!string.IsNullOrEmpty(m)) - { - mana = mana.Replace(manaTypes.Value, m); - } - } - return mana; - } - } - - public interface IMtgManaConverter - { - string ConvertMana(string mana); - } -} \ No newline at end of file diff --git a/Geekbot.net/Lib/ReactionListener/IReactionListener.cs b/Geekbot.net/Lib/ReactionListener/IReactionListener.cs new file mode 100644 index 0000000..792a516 --- /dev/null +++ b/Geekbot.net/Lib/ReactionListener/IReactionListener.cs @@ -0,0 +1,14 @@ +using System.Threading.Tasks; +using Discord; +using Discord.WebSocket; + +namespace Geekbot.net.Lib.ReactionListener +{ + public interface IReactionListener + { + bool IsListener(ulong id); + Task AddRoleToListener(string messageId, IEmote emoji, IRole role); + void RemoveRole(ISocketMessageChannel channel, SocketReaction reaction); + void GiveRole(ISocketMessageChannel message, SocketReaction reaction); + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/ReactionListener.cs b/Geekbot.net/Lib/ReactionListener/ReactionListener.cs similarity index 87% rename from Geekbot.net/Lib/ReactionListener.cs rename to Geekbot.net/Lib/ReactionListener/ReactionListener.cs index f837280..950edd1 100644 --- a/Geekbot.net/Lib/ReactionListener.cs +++ b/Geekbot.net/Lib/ReactionListener/ReactionListener.cs @@ -5,7 +5,7 @@ using Discord; using Discord.WebSocket; using StackExchange.Redis; -namespace Geekbot.net.Lib +namespace Geekbot.net.Lib.ReactionListener { public class ReactionListener : IReactionListener { @@ -87,12 +87,4 @@ namespace Geekbot.net.Lib await ((IGuildUser) reaction.User.Value).AddRoleAsync(role); } } - - public interface IReactionListener - { - bool IsListener(ulong id); - Task AddRoleToListener(string messageId, IEmote emoji, IRole role); - void RemoveRole(ISocketMessageChannel channel, SocketReaction reaction); - void GiveRole(ISocketMessageChannel message, SocketReaction reaction); - } } \ No newline at end of file diff --git a/Geekbot.net/Lib/UserRepository/IUserRepository.cs b/Geekbot.net/Lib/UserRepository/IUserRepository.cs new file mode 100644 index 0000000..9466621 --- /dev/null +++ b/Geekbot.net/Lib/UserRepository/IUserRepository.cs @@ -0,0 +1,13 @@ +using System.Threading.Tasks; +using Discord.WebSocket; + +namespace Geekbot.net.Lib.UserRepository +{ + public interface IUserRepository + { + Task Update(SocketUser user); + UserRepositoryUser Get(ulong userId); + string GetUserSetting(ulong userId, string setting); + bool SaveUserSetting(ulong userId, string setting, string value); + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/UserRepository.cs b/Geekbot.net/Lib/UserRepository/UserRepository.cs similarity index 84% rename from Geekbot.net/Lib/UserRepository.cs rename to Geekbot.net/Lib/UserRepository/UserRepository.cs index 28d0541..558732e 100644 --- a/Geekbot.net/Lib/UserRepository.cs +++ b/Geekbot.net/Lib/UserRepository/UserRepository.cs @@ -2,10 +2,11 @@ using System.Collections.Generic; using System.Threading.Tasks; using Discord.WebSocket; +using Geekbot.net.Lib.Logger; using StackExchange.Redis; using Utf8Json; -namespace Geekbot.net.Lib +namespace Geekbot.net.Lib.UserRepository { public class UserRepository : IUserRepository { @@ -121,23 +122,4 @@ namespace Geekbot.net.Lib return true; } } - - public class UserRepositoryUser - { - public ulong Id { get; set; } - public string Username { get; set; } - public string Discriminator { get; set; } - public string AvatarUrl { get; set; } - public bool IsBot { get; set; } - public DateTimeOffset Joined { get; set; } - public List UsedNames { get; set; } - } - - public interface IUserRepository - { - Task Update(SocketUser user); - UserRepositoryUser Get(ulong userId); - string GetUserSetting(ulong userId, string setting); - bool SaveUserSetting(ulong userId, string setting, string value); - } } \ No newline at end of file diff --git a/Geekbot.net/Lib/UserRepository/UserRepositoryUserDto.cs b/Geekbot.net/Lib/UserRepository/UserRepositoryUserDto.cs new file mode 100644 index 0000000..4af719b --- /dev/null +++ b/Geekbot.net/Lib/UserRepository/UserRepositoryUserDto.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; + +namespace Geekbot.net.Lib.UserRepository +{ + public class UserRepositoryUser + { + public ulong Id { get; set; } + public string Username { get; set; } + public string Discriminator { get; set; } + public string AvatarUrl { get; set; } + public bool IsBot { get; set; } + public DateTimeOffset Joined { get; set; } + public List UsedNames { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 3c0e5eb..f5f878a 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -8,7 +8,15 @@ using Discord; using Discord.Commands; using Discord.WebSocket; using Geekbot.net.Lib; +using Geekbot.net.Lib.Clients; +using Geekbot.net.Lib.Converters; +using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.Levels; +using Geekbot.net.Lib.Localization; +using Geekbot.net.Lib.Logger; using Geekbot.net.Lib.Media; +using Geekbot.net.Lib.ReactionListener; +using Geekbot.net.Lib.UserRepository; using Microsoft.Extensions.DependencyInjection; using Nancy.Hosting.Self; using StackExchange.Redis; @@ -24,7 +32,7 @@ namespace Geekbot.net private IServiceCollection _services; private IServiceProvider _servicesProvider; private RedisValue _token; - private IGeekbotLogger _logger; + private GeekbotLogger _logger; private IUserRepository _userRepository; private bool _firstStart; private RunParameters _runParameters; @@ -57,18 +65,19 @@ namespace Geekbot.net } } - private async Task MainAsync(RunParameters runParameters, IGeekbotLogger logger) + private async Task MainAsync(RunParameters runParameters, GeekbotLogger logger) { _logger = logger; _runParameters = runParameters; logger.Information("Geekbot", "Initing Stuff"); + var discordLogger = new DiscordLogger(logger); _client = new DiscordSocketClient(new DiscordSocketConfig { LogLevel = LogSeverity.Verbose, MessageCacheSize = 1000 }); - _client.Log += DiscordLogger; + _client.Log += discordLogger.Log; _commands = new CommandService(); try @@ -218,30 +227,5 @@ namespace Geekbot.net } return Task.CompletedTask; } - - private Task DiscordLogger(LogMessage message) - { - var logMessage = $"[{message.Source}] {message.Message}"; - switch (message.Severity) - { - case LogSeverity.Verbose: - case LogSeverity.Debug: - _logger.Debug(message.Source, message.Message); - break; - case LogSeverity.Info: - _logger.Information(message.Source, message.Message); - break; - case LogSeverity.Critical: - case LogSeverity.Error: - case LogSeverity.Warning: - if (logMessage.Contains("VOICE_STATE_UPDATE")) break; - _logger.Error(message.Source, message.Message, message.Exception); - break; - default: - _logger.Information(message.Source, $"{logMessage} --- {message.Severity}"); - break; - } - return Task.CompletedTask; - } } } \ No newline at end of file diff --git a/Geekbot.net/WebApi/Help/CommandDto.cs b/Geekbot.net/WebApi/Help/CommandDto.cs new file mode 100644 index 0000000..5921fa6 --- /dev/null +++ b/Geekbot.net/WebApi/Help/CommandDto.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; + +namespace Geekbot.net.WebApi.Help +{ + public class CommandDto + { + public string Name { get; set; } + public string Category { get; set; } + public string Summary { get; set; } + public bool IsAdminCommand { get; set; } + public Array Aliases { get; set; } + public List Params { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/WebApi/Help/CommandParamDto.cs b/Geekbot.net/WebApi/Help/CommandParamDto.cs new file mode 100644 index 0000000..f7ce0ea --- /dev/null +++ b/Geekbot.net/WebApi/Help/CommandParamDto.cs @@ -0,0 +1,9 @@ +namespace Geekbot.net.WebApi.Help +{ + public class CommandParamDto + { + public string Summary { get; set; } + public string Default { get; set; } + public string Type { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/WebApi/HelpController.cs b/Geekbot.net/WebApi/Help/HelpController.cs similarity index 70% rename from Geekbot.net/WebApi/HelpController.cs rename to Geekbot.net/WebApi/Help/HelpController.cs index 060dea0..d71d26f 100644 --- a/Geekbot.net/WebApi/HelpController.cs +++ b/Geekbot.net/WebApi/Help/HelpController.cs @@ -1,13 +1,11 @@ -using System; -using System.Collections.Generic; -using System.Linq; +using System.Linq; using System.Reflection; using System.Threading.Tasks; using Discord.Commands; using Geekbot.net.Lib; using Nancy; -namespace Geekbot.net.WebApi +namespace Geekbot.net.WebApi.Help { public class HelpController : NancyModule { @@ -47,21 +45,4 @@ namespace Geekbot.net.WebApi return commands; } } - - public class CommandDto - { - public string Name { get; set; } - public string Category { get; set; } - public string Summary { get; set; } - public bool IsAdminCommand { get; set; } - public Array Aliases { get; set; } - public List Params { get; set; } - } - - public class CommandParamDto - { - public string Summary { get; set; } - public string Default { get; set; } - public string Type { get; set; } - } } \ No newline at end of file diff --git a/Geekbot.net/WebApi/Status/ApiStatusDto.cs b/Geekbot.net/WebApi/Status/ApiStatusDto.cs new file mode 100644 index 0000000..242bf58 --- /dev/null +++ b/Geekbot.net/WebApi/Status/ApiStatusDto.cs @@ -0,0 +1,9 @@ +namespace Geekbot.net.WebApi.Status +{ + public class ApiStatusDto + { + public string GeekbotVersion { get; set; } + public string ApiVersion { get; set; } + public string Status { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/WebApi/StatusController.cs b/Geekbot.net/WebApi/Status/StatusController.cs similarity index 71% rename from Geekbot.net/WebApi/StatusController.cs rename to Geekbot.net/WebApi/Status/StatusController.cs index b785e07..5f995c9 100644 --- a/Geekbot.net/WebApi/StatusController.cs +++ b/Geekbot.net/WebApi/Status/StatusController.cs @@ -1,7 +1,7 @@ using Geekbot.net.Lib; using Nancy; -namespace Geekbot.net.WebApi +namespace Geekbot.net.WebApi.Status { public class StatusController : NancyModule { @@ -19,11 +19,4 @@ namespace Geekbot.net.WebApi }); } } - - public class ApiStatusDto - { - public string GeekbotVersion { get; set; } - public string ApiVersion { get; set; } - public string Status { get; set; } - } } \ No newline at end of file diff --git a/Tests/Lib/EmojiConverter.test.cs b/Tests/Lib/EmojiConverter.test.cs index 4bfcf06..aaaaa8f 100644 --- a/Tests/Lib/EmojiConverter.test.cs +++ b/Tests/Lib/EmojiConverter.test.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using Geekbot.net.Lib; +using Geekbot.net.Lib.Converters; using Xunit; namespace Tests.Lib diff --git a/Tests/Lib/LevelCalc.test.cs b/Tests/Lib/LevelCalc.test.cs index ecca151..4b6b095 100644 --- a/Tests/Lib/LevelCalc.test.cs +++ b/Tests/Lib/LevelCalc.test.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using Geekbot.net.Lib; +using Geekbot.net.Lib.Levels; using Xunit; namespace Tests.Lib From 0828130ae8d7879409cba3794cea5482cbe5bce7 Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 3 May 2018 21:20:49 +0200 Subject: [PATCH 040/443] Fixes --- Geekbot.net/Commands/Integrations/Google/Google.cs | 4 ++-- Geekbot.net/Commands/User/{Rank => Ranking}/Rank.cs | 5 ++--- .../User/{Rank => Ranking}/RankUserPolyfillDto.cs | 2 +- .../Commands/Utils/Changelog/CommitAuthorDto.cs | 2 +- Geekbot.net/Commands/Utils/Changelog/CommitDto.cs | 2 +- .../Commands/Utils/Changelog/CommitInfoDto.cs | 2 +- Geekbot.net/Lib/Logger/GeekbotLogger.cs | 4 ++-- Geekbot.net/Lib/Logger/LoggerFactory.cs | 12 ++++++------ 8 files changed, 16 insertions(+), 17 deletions(-) rename Geekbot.net/Commands/User/{Rank => Ranking}/Rank.cs (95%) rename Geekbot.net/Commands/User/{Rank => Ranking}/RankUserPolyfillDto.cs (77%) diff --git a/Geekbot.net/Commands/Integrations/Google/Google.cs b/Geekbot.net/Commands/Integrations/Google/Google.cs index 8e880c4..631bbe3 100644 --- a/Geekbot.net/Commands/Integrations/Google/Google.cs +++ b/Geekbot.net/Commands/Integrations/Google/Google.cs @@ -6,8 +6,8 @@ using Discord; using Discord.Commands; using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; +using Newtonsoft.Json; using StackExchange.Redis; -using Utf8Json; namespace Geekbot.net.Commands.Integrations.Google { @@ -40,7 +40,7 @@ namespace Geekbot.net.Commands.Integrations.Google var url = new Uri($"https://kgsearch.googleapis.com/v1/entities:search?languages=en&limit=1&query={searchText}&key={apiKey}"); var responseString = client.DownloadString(url); - var response = JsonSerializer.Deserialize(responseString); + var response = JsonConvert.DeserializeObject(responseString); if (!response.ItemListElement.Any()) { diff --git a/Geekbot.net/Commands/User/Rank/Rank.cs b/Geekbot.net/Commands/User/Ranking/Rank.cs similarity index 95% rename from Geekbot.net/Commands/User/Rank/Rank.cs rename to Geekbot.net/Commands/User/Ranking/Rank.cs index 774ac5a..590921e 100644 --- a/Geekbot.net/Commands/User/Rank/Rank.cs +++ b/Geekbot.net/Commands/User/Ranking/Rank.cs @@ -12,7 +12,7 @@ using Geekbot.net.Lib.Logger; using Geekbot.net.Lib.UserRepository; using StackExchange.Redis; -namespace Geekbot.net.Commands.User.Rank +namespace Geekbot.net.Commands.User.Ranking { public class Rank : ModuleBase { @@ -37,8 +37,7 @@ namespace Geekbot.net.Commands.User.Rank [Command("rank", RunMode = RunMode.Async)] [Remarks(CommandCategories.Statistics)] [Summary("get user top 10 in messages or karma")] - public async Task RankCmd([Summary("type")] string typeUnformated = "messages", - [Summary("amount")] int amount = 10) + public async Task RankCmd([Summary("type")] string typeUnformated = "messages", [Summary("amount")] int amount = 10) { try { diff --git a/Geekbot.net/Commands/User/Rank/RankUserPolyfillDto.cs b/Geekbot.net/Commands/User/Ranking/RankUserPolyfillDto.cs similarity index 77% rename from Geekbot.net/Commands/User/Rank/RankUserPolyfillDto.cs rename to Geekbot.net/Commands/User/Ranking/RankUserPolyfillDto.cs index f666ffc..625c326 100644 --- a/Geekbot.net/Commands/User/Rank/RankUserPolyfillDto.cs +++ b/Geekbot.net/Commands/User/Ranking/RankUserPolyfillDto.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Commands.User.Rank +namespace Geekbot.net.Commands.User.Ranking { internal class RankUserPolyfillDto { diff --git a/Geekbot.net/Commands/Utils/Changelog/CommitAuthorDto.cs b/Geekbot.net/Commands/Utils/Changelog/CommitAuthorDto.cs index ad37311..53881d5 100644 --- a/Geekbot.net/Commands/Utils/Changelog/CommitAuthorDto.cs +++ b/Geekbot.net/Commands/Utils/Changelog/CommitAuthorDto.cs @@ -2,7 +2,7 @@ namespace Geekbot.net.Commands.Utils.Changelog { - internal class CommitAuthorDto + public class CommitAuthorDto { public DateTimeOffset Date { get; set; } } diff --git a/Geekbot.net/Commands/Utils/Changelog/CommitDto.cs b/Geekbot.net/Commands/Utils/Changelog/CommitDto.cs index d5c5bab..3379697 100644 --- a/Geekbot.net/Commands/Utils/Changelog/CommitDto.cs +++ b/Geekbot.net/Commands/Utils/Changelog/CommitDto.cs @@ -1,6 +1,6 @@ namespace Geekbot.net.Commands.Utils.Changelog { - internal class CommitDto + public class CommitDto { public CommitInfoDto Commit { get; set; } } diff --git a/Geekbot.net/Commands/Utils/Changelog/CommitInfoDto.cs b/Geekbot.net/Commands/Utils/Changelog/CommitInfoDto.cs index 102d2cd..743ca52 100644 --- a/Geekbot.net/Commands/Utils/Changelog/CommitInfoDto.cs +++ b/Geekbot.net/Commands/Utils/Changelog/CommitInfoDto.cs @@ -1,6 +1,6 @@ namespace Geekbot.net.Commands.Utils.Changelog { - internal class CommitInfoDto + public class CommitInfoDto { public CommitAuthorDto AuthorDto { get; set; } public string Message { get; set; } diff --git a/Geekbot.net/Lib/Logger/GeekbotLogger.cs b/Geekbot.net/Lib/Logger/GeekbotLogger.cs index 8cefdb4..c1ceb38 100644 --- a/Geekbot.net/Lib/Logger/GeekbotLogger.cs +++ b/Geekbot.net/Lib/Logger/GeekbotLogger.cs @@ -23,7 +23,7 @@ namespace Geekbot.net.Lib.Logger public void Trace(string source, string message, object extra = null) { - _logger.Trace(CreateLogString("Debug", source, message, null, extra)); + _logger.Trace(CreateLogString("Trace", source, message, null, extra)); } public void Debug(string source, string message, object extra = null) @@ -65,7 +65,7 @@ namespace Geekbot.net.Lib.Logger if (source != "Message") return $"[{source}] - {message}"; var m = (MessageDto) extra; - return $"[{source}] - [{m.Guild.Name} - {m.Channel.Name}] {m.User.Name}: {m.Message.Content}"; + return $"[{source}] - [{m?.Guild.Name} - {m?.Channel.Name}] {m?.User.Name}: {m?.Message.Content}"; } } } \ No newline at end of file diff --git a/Geekbot.net/Lib/Logger/LoggerFactory.cs b/Geekbot.net/Lib/Logger/LoggerFactory.cs index 53325a6..491197c 100644 --- a/Geekbot.net/Lib/Logger/LoggerFactory.cs +++ b/Geekbot.net/Lib/Logger/LoggerFactory.cs @@ -17,20 +17,20 @@ namespace Geekbot.net.Lib.Logger { Console.WriteLine("Logging Geekbot Logs to Sumologic"); config.LoggingRules.Add( - new LoggingRule("*", LogLevel.Info, LogLevel.Fatal, + new LoggingRule("*", LogLevel.Debug, LogLevel.Fatal, new BufferedSumoLogicTarget() { Url = Environment.GetEnvironmentVariable("GEEKBOT_SUMO"), SourceName = "GeekbotLogger", Layout = "${message}", - UseConsoleLog = false, + UseConsoleLog = true, MaxQueueSizeBytes = 500000, FlushingAccuracy = 250, MaxFlushInterval = 10000, OptimizeBufferReuse = true, MessagesPerRequest = 10, RetryInterval = 5000, - Name = "Geekbot" + Name = "GeekbotBeta" }) ); } @@ -38,11 +38,11 @@ namespace Geekbot.net.Lib.Logger { var minLevel = runParameters.Verbose ? LogLevel.Trace : LogLevel.Info; config.LoggingRules.Add( - new LoggingRule("*", minLevel, LogLevel.Fatal, + new LoggingRule("*", LogLevel.Info, LogLevel.Fatal, new ColoredConsoleTarget { Name = "Console", - Encoding = Encoding.Unicode, + Encoding = Encoding.UTF8, Layout = "[${longdate} ${level:format=FirstCharacter}] ${message} ${exception:format=toString}" }) ); @@ -53,7 +53,7 @@ namespace Geekbot.net.Lib.Logger { Name = "File", Layout = "[${longdate} ${level}] ${message}", - Encoding = Encoding.Unicode, + Encoding = Encoding.UTF8, LineEnding = LineEndingMode.Default, MaxArchiveFiles = 30, ArchiveNumbering = ArchiveNumberingMode.Date, From 8599c815d7743e3817f60793e9555606bea66a61 Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 4 May 2018 00:54:01 +0200 Subject: [PATCH 041/443] Add audio stuff back --- Geekbot.net/Commands/Audio/Voice.cs | 103 +++++++++++++++++++++++++++ Geekbot.net/Lib/Audio/AudioUtils.cs | 96 +++++++++++++++++++++++++ Geekbot.net/Lib/Audio/IAudioUtils.cs | 15 ++++ Geekbot.net/Program.cs | 3 + 4 files changed, 217 insertions(+) create mode 100644 Geekbot.net/Commands/Audio/Voice.cs create mode 100644 Geekbot.net/Lib/Audio/AudioUtils.cs create mode 100644 Geekbot.net/Lib/Audio/IAudioUtils.cs diff --git a/Geekbot.net/Commands/Audio/Voice.cs b/Geekbot.net/Commands/Audio/Voice.cs new file mode 100644 index 0000000..e8f3134 --- /dev/null +++ b/Geekbot.net/Commands/Audio/Voice.cs @@ -0,0 +1,103 @@ +using System; +using System.Threading.Tasks; +using Discord; +using Discord.Commands; +using Geekbot.net.Lib.Audio; +using Geekbot.net.Lib.ErrorHandling; + +namespace Geekbot.net.Commands.Audio +{ + public class Voice : ModuleBase + { + private readonly IAudioUtils _audioUtils; + private readonly IErrorHandler _errorHandler; + + public Voice(IErrorHandler errorHandler, IAudioUtils audioUtils) + { + _errorHandler = errorHandler; + _audioUtils = audioUtils; + } + + [Command("join")] + public async Task JoinChannel() + { + try + { + // Get the audio channel + var channel = (Context.User as IGuildUser)?.VoiceChannel; + if (channel == null) + { + await Context.Channel.SendMessageAsync( + "User must be in a voice channel, or a voice channel must be passed as an argument."); + return; + } + + // For the next step with transmitting audio, you would want to pass this Audio Client in to a service. + var audioClient = await channel.ConnectAsync(); + _audioUtils.StoreAudioClient(Context.Guild.Id, audioClient); + await ReplyAsync($"Connected to {channel.Name}"); + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + } + } + + [Command("disconnect")] + public async Task DisconnectChannel() + { + try + { + var audioClient = _audioUtils.GetAudioClient(Context.Guild.Id); + if (audioClient == null) + { + await Context.Channel.SendMessageAsync("I'm not in a voice channel at the moment"); + return; + } + + await audioClient.StopAsync(); + await ReplyAsync("Disconnected from channel!"); + _audioUtils.Cleanup(Context.Guild.Id); + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + _audioUtils.Cleanup(Context.Guild.Id); + } + } + + [Command("ytplay")] + public async Task ytplay(string url) + { + try + { + if (!url.Contains("youtube")) + { + await ReplyAsync("I can only play youtube videos"); + return; + } + var audioClient = _audioUtils.GetAudioClient(Context.Guild.Id); + if (audioClient == null) + { + await ReplyAsync("I'm not in a voice channel at the moment"); + return; + } + + var message = await Context.Channel.SendMessageAsync("Just a second, i'm still a bit slow at this"); + var ffmpeg = _audioUtils.CreateStreamFromYoutube(url, Context.Guild.Id); + var output = ffmpeg.StandardOutput.BaseStream; + await message.ModifyAsync(msg => msg.Content = "**Playing!** Please note that this feature is experimental"); + var discord = audioClient.CreatePCMStream(Discord.Audio.AudioApplication.Mixed); + await output.CopyToAsync(discord); + await discord.FlushAsync(); + _audioUtils.Cleanup(Context.Guild.Id); + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + _audioUtils.Cleanup(Context.Guild.Id); + } + } + } + +} \ No newline at end of file diff --git a/Geekbot.net/Lib/Audio/AudioUtils.cs b/Geekbot.net/Lib/Audio/AudioUtils.cs new file mode 100644 index 0000000..024019d --- /dev/null +++ b/Geekbot.net/Lib/Audio/AudioUtils.cs @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Net; +using Discord.Audio; + +namespace Geekbot.net.Lib.Audio +{ + public class AudioUtils : IAudioUtils + { + private string _tempFolderPath; + private Dictionary _audioClients; + + public AudioUtils() + { + _audioClients = new Dictionary(); + _tempFolderPath = Path.GetFullPath("./tmp/"); + if (Directory.Exists(_tempFolderPath)) + { + Directory.Delete(_tempFolderPath, true); + } + Directory.CreateDirectory(_tempFolderPath); + } + + public IAudioClient GetAudioClient(ulong guildId) + { + return _audioClients[guildId]; + } + + public void StoreAudioClient(ulong guildId, IAudioClient client) + { + _audioClients[guildId] = client; + } + + public Process CreateStreamFromFile(string path) + { + var ffmpeg = new ProcessStartInfo + { + FileName = "ffmpeg", + Arguments = $"-i {path} -ac 2 -f s16le -ar 48000 pipe:1", + UseShellExecute = false, + RedirectStandardOutput = true, + }; + return Process.Start(ffmpeg); + } + + public Process CreateStreamFromYoutube(string url, ulong guildId) + { + var ytdlMediaUrl = GetYoutubeMediaUrl(url); + DownloadMediaUrl(ytdlMediaUrl, guildId); + return CreateStreamFromFile($"{_tempFolderPath}{guildId}"); + } + + public void Cleanup(ulong guildId) + { + File.Delete($"{_tempFolderPath}{guildId}"); + } + + private string GetYoutubeMediaUrl(string url) + { + var ytdl = new ProcessStartInfo() + { + FileName = "youtube-dl", + Arguments = $"-f bestaudio -g {url}", + UseShellExecute = false, + RedirectStandardOutput = true + }; + var output = Process.Start(ytdl).StandardOutput.ReadToEnd(); + if (string.IsNullOrWhiteSpace(output)) + { + throw new Exception("Could not get Youtube Media URL"); + } + return output; + } + + private void DownloadMediaUrl(string url, ulong guildId) + { + using (var web = new WebClient()) + { + web.DownloadFile(url, $"{_tempFolderPath}{guildId}"); + } +// var ffmpeg = new ProcessStartInfo +// { +// FileName = "ffmpeg", +// Arguments = $"-i \"{_tempFolderPath}{guildId}\" -c:a mp3 -b:a 256k {_tempFolderPath}{guildId}.mp3", +// UseShellExecute = false, +// RedirectStandardOutput = true, +// }; +// Process.Start(ffmpeg).WaitForExit(); +// File.Delete($"{_tempFolderPath}{guildId}"); + return; + } + + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/Audio/IAudioUtils.cs b/Geekbot.net/Lib/Audio/IAudioUtils.cs new file mode 100644 index 0000000..4af1293 --- /dev/null +++ b/Geekbot.net/Lib/Audio/IAudioUtils.cs @@ -0,0 +1,15 @@ +using System.Diagnostics; +using Discord.Audio; + +namespace Geekbot.net.Lib.Audio +{ + public interface IAudioUtils + { + IAudioClient GetAudioClient(ulong guildId); + void StoreAudioClient(ulong guildId, IAudioClient client); + Process CreateStreamFromFile(string path); + Process CreateStreamFromYoutube(string url, ulong guildId); + void Cleanup(ulong guildId); + + } +} \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index f5f878a..8268833 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -8,6 +8,7 @@ using Discord; using Discord.Commands; using Discord.WebSocket; using Geekbot.net.Lib; +using Geekbot.net.Lib.Audio; using Geekbot.net.Lib.Clients; using Geekbot.net.Lib.Converters; using Geekbot.net.Lib.ErrorHandling; @@ -113,6 +114,7 @@ namespace Geekbot.net var emojiConverter = new EmojiConverter(); var mtgManaConverter = new MtgManaConverter(); var wikipediaClient = new WikipediaClient(); + var audioUtils = new AudioUtils(); _services.AddSingleton(_redis); _services.AddSingleton(logger); @@ -124,6 +126,7 @@ namespace Geekbot.net _services.AddSingleton(malClient); _services.AddSingleton(mtgManaConverter); _services.AddSingleton(wikipediaClient); + _services.AddSingleton(audioUtils); logger.Information("Geekbot", "Connecting to Discord"); From 290b85fdb6ff9b480b0922ef684662056be1f0d3 Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 4 May 2018 00:55:32 +0200 Subject: [PATCH 042/443] Fix !changelog, fix bug in logger and typo in !mod namehistory --- Geekbot.net/Commands/Admin/Mod.cs | 2 +- Geekbot.net/Commands/Utils/Changelog/Changelog.cs | 2 +- Geekbot.net/Commands/Utils/Changelog/CommitAuthorDto.cs | 2 ++ Geekbot.net/Commands/Utils/Changelog/CommitInfoDto.cs | 2 +- Geekbot.net/Lib/Logger/GeekbotLogger.cs | 2 +- 5 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Geekbot.net/Commands/Admin/Mod.cs b/Geekbot.net/Commands/Admin/Mod.cs index 2dacee3..6f79157 100644 --- a/Geekbot.net/Commands/Admin/Mod.cs +++ b/Geekbot.net/Commands/Admin/Mod.cs @@ -47,7 +47,7 @@ namespace Geekbot.net.Commands.Admin catch (Exception e) { _errorHandler.HandleCommandException(e, Context, - $"I don't have enough permissions to give {user.Username} that role"); + $"I don't have enough permissions do that"); } } diff --git a/Geekbot.net/Commands/Utils/Changelog/Changelog.cs b/Geekbot.net/Commands/Utils/Changelog/Changelog.cs index f956d7c..f1752f7 100644 --- a/Geekbot.net/Commands/Utils/Changelog/Changelog.cs +++ b/Geekbot.net/Commands/Utils/Changelog/Changelog.cs @@ -52,7 +52,7 @@ namespace Geekbot.net.Commands.Utils.Changelog }); var sb = new StringBuilder(); foreach (var commit in commits.Take(10)) - sb.AppendLine($"- {commit.Commit.Message} ({commit.Commit.AuthorDto.Date:yyyy-MM-dd})"); + sb.AppendLine($"- {commit.Commit.Message} ({commit.Commit.Author.Date:yyyy-MM-dd})"); eb.Description = sb.ToString(); eb.WithFooter(new EmbedFooterBuilder { diff --git a/Geekbot.net/Commands/Utils/Changelog/CommitAuthorDto.cs b/Geekbot.net/Commands/Utils/Changelog/CommitAuthorDto.cs index 53881d5..8debd77 100644 --- a/Geekbot.net/Commands/Utils/Changelog/CommitAuthorDto.cs +++ b/Geekbot.net/Commands/Utils/Changelog/CommitAuthorDto.cs @@ -4,6 +4,8 @@ namespace Geekbot.net.Commands.Utils.Changelog { public class CommitAuthorDto { + public string Name { get; set; } + public string Email { get; set; } public DateTimeOffset Date { get; set; } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/Utils/Changelog/CommitInfoDto.cs b/Geekbot.net/Commands/Utils/Changelog/CommitInfoDto.cs index 743ca52..9008343 100644 --- a/Geekbot.net/Commands/Utils/Changelog/CommitInfoDto.cs +++ b/Geekbot.net/Commands/Utils/Changelog/CommitInfoDto.cs @@ -2,7 +2,7 @@ { public class CommitInfoDto { - public CommitAuthorDto AuthorDto { get; set; } + public CommitAuthorDto Author { get; set; } public string Message { get; set; } } } \ No newline at end of file diff --git a/Geekbot.net/Lib/Logger/GeekbotLogger.cs b/Geekbot.net/Lib/Logger/GeekbotLogger.cs index c1ceb38..45d65f5 100644 --- a/Geekbot.net/Lib/Logger/GeekbotLogger.cs +++ b/Geekbot.net/Lib/Logger/GeekbotLogger.cs @@ -43,7 +43,7 @@ namespace Geekbot.net.Lib.Logger public void Error(string source, string message, Exception stackTrace, object extra = null) { - _logger.Error(stackTrace, CreateLogString("Error", source, message, stackTrace, extra)); + _logger.Error(CreateLogString("Error", source, message, stackTrace, extra)); } private string CreateLogString(string type, string source, string message, Exception stackTrace = null, object extra = null) From 0b7303d576980c6c70e496040ea8af3c4ad0602a Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 4 May 2018 00:59:49 +0200 Subject: [PATCH 043/443] fix !google --- Geekbot.net/Commands/Integrations/Google/Google.cs | 4 ++-- .../Commands/Integrations/Google/GoogleKgApiElementDto.cs | 2 +- .../Commands/Integrations/Google/GoogleKgApiResultDto.cs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Geekbot.net/Commands/Integrations/Google/Google.cs b/Geekbot.net/Commands/Integrations/Google/Google.cs index 631bbe3..7bd3a56 100644 --- a/Geekbot.net/Commands/Integrations/Google/Google.cs +++ b/Geekbot.net/Commands/Integrations/Google/Google.cs @@ -48,13 +48,13 @@ namespace Geekbot.net.Commands.Integrations.Google return; } - var data = response.ItemListElement.First().ResultDto; + var data = response.ItemListElement.First().Result; var eb = new EmbedBuilder(); eb.Title = data.Name; if(!string.IsNullOrEmpty(data.Description)) eb.WithDescription(data.Description); if(!string.IsNullOrEmpty(data.DetailedDtoDescription?.Url)) eb.WithUrl(data.DetailedDtoDescription.Url); if(!string.IsNullOrEmpty(data.DetailedDtoDescription?.ArticleBody)) eb.AddField("Details", data.DetailedDtoDescription.ArticleBody); - if(!string.IsNullOrEmpty(data.ImageDto?.ContentUrl)) eb.WithThumbnailUrl(data.ImageDto.ContentUrl); + if(!string.IsNullOrEmpty(data.Image?.ContentUrl)) eb.WithThumbnailUrl(data.Image.ContentUrl); await ReplyAsync("", false, eb.Build()); } diff --git a/Geekbot.net/Commands/Integrations/Google/GoogleKgApiElementDto.cs b/Geekbot.net/Commands/Integrations/Google/GoogleKgApiElementDto.cs index 3c4a130..a48b184 100644 --- a/Geekbot.net/Commands/Integrations/Google/GoogleKgApiElementDto.cs +++ b/Geekbot.net/Commands/Integrations/Google/GoogleKgApiElementDto.cs @@ -2,7 +2,7 @@ { public class GoogleKgApiElementDto { - public GoogleKgApiResultDto ResultDto { get; set; } + public GoogleKgApiResultDto Result { get; set; } public double ResultScore { get; set; } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/Integrations/Google/GoogleKgApiResultDto.cs b/Geekbot.net/Commands/Integrations/Google/GoogleKgApiResultDto.cs index 2c348c9..465f1d7 100644 --- a/Geekbot.net/Commands/Integrations/Google/GoogleKgApiResultDto.cs +++ b/Geekbot.net/Commands/Integrations/Google/GoogleKgApiResultDto.cs @@ -4,7 +4,7 @@ { public string Name { get; set; } public string Description { get; set; } - public GoogleKgApiImageDto ImageDto { get; set; } + public GoogleKgApiImageDto Image { get; set; } public GoogleKgApiDetailedDto DetailedDtoDescription { get; set; } } } \ No newline at end of file From 7958c87de527d7dbf624412d15a63e2567063feb Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 4 May 2018 01:14:43 +0200 Subject: [PATCH 044/443] Fix every other command that was not working --- Geekbot.net/Program.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 8268833..82016fa 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -116,9 +116,9 @@ namespace Geekbot.net var wikipediaClient = new WikipediaClient(); var audioUtils = new AudioUtils(); - _services.AddSingleton(_redis); - _services.AddSingleton(logger); - _services.AddSingleton(_userRepository); + _services.AddSingleton(_redis); + _services.AddSingleton(_userRepository); + _services.AddSingleton(logger); _services.AddSingleton(levelCalc); _services.AddSingleton(emojiConverter); _services.AddSingleton(fortunes); From 49392703f2a6b539513b62d192c336be2877a63b Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 4 May 2018 23:34:47 +0200 Subject: [PATCH 045/443] Bump version to 3.7 --- Geekbot.net/Commands/Audio/Voice.cs | 10 ++++------ Geekbot.net/Geekbot.net.csproj | 2 +- Geekbot.net/Lib/Constants.cs | 2 +- Geekbot.net/Lib/Logger/LoggerFactory.cs | 2 +- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/Geekbot.net/Commands/Audio/Voice.cs b/Geekbot.net/Commands/Audio/Voice.cs index e8f3134..410d8e2 100644 --- a/Geekbot.net/Commands/Audio/Voice.cs +++ b/Geekbot.net/Commands/Audio/Voice.cs @@ -18,7 +18,7 @@ namespace Geekbot.net.Commands.Audio _audioUtils = audioUtils; } - [Command("join")] +// [Command("join")] public async Task JoinChannel() { try @@ -27,12 +27,10 @@ namespace Geekbot.net.Commands.Audio var channel = (Context.User as IGuildUser)?.VoiceChannel; if (channel == null) { - await Context.Channel.SendMessageAsync( - "User must be in a voice channel, or a voice channel must be passed as an argument."); + await Context.Channel.SendMessageAsync("You must be in a voice channel."); return; } - // For the next step with transmitting audio, you would want to pass this Audio Client in to a service. var audioClient = await channel.ConnectAsync(); _audioUtils.StoreAudioClient(Context.Guild.Id, audioClient); await ReplyAsync($"Connected to {channel.Name}"); @@ -43,7 +41,7 @@ namespace Geekbot.net.Commands.Audio } } - [Command("disconnect")] +// [Command("disconnect")] public async Task DisconnectChannel() { try @@ -66,7 +64,7 @@ namespace Geekbot.net.Commands.Audio } } - [Command("ytplay")] +// [Command("ytplay")] public async Task ytplay(string url) { try diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 5cba28a..861085b 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -3,7 +3,7 @@ Exe netcoreapp2.0 derp.ico - 3.6.0 + 3.7.0 Pizza and Coffee Studios Pizza and Coffee Studios A Discord bot diff --git a/Geekbot.net/Lib/Constants.cs b/Geekbot.net/Lib/Constants.cs index d754b5c..dbba570 100644 --- a/Geekbot.net/Lib/Constants.cs +++ b/Geekbot.net/Lib/Constants.cs @@ -3,7 +3,7 @@ public class Constants { public const string Name = "Geekbot"; - public const double BotVersion = 3.6; + public const double BotVersion = 3.7; public const double ApiVersion = 1; } } \ No newline at end of file diff --git a/Geekbot.net/Lib/Logger/LoggerFactory.cs b/Geekbot.net/Lib/Logger/LoggerFactory.cs index 491197c..d32d80b 100644 --- a/Geekbot.net/Lib/Logger/LoggerFactory.cs +++ b/Geekbot.net/Lib/Logger/LoggerFactory.cs @@ -30,7 +30,7 @@ namespace Geekbot.net.Lib.Logger OptimizeBufferReuse = true, MessagesPerRequest = 10, RetryInterval = 5000, - Name = "GeekbotBeta" + Name = "Geekbot" }) ); } From 0fe273151c61535d54605b6658488f83ef6a117f Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 6 May 2018 00:59:06 +0200 Subject: [PATCH 046/443] Use Owner Attribute instead of manual inline checking --- Geekbot.net/Commands/Admin/Owner.cs | 46 +++++++---------------------- 1 file changed, 11 insertions(+), 35 deletions(-) diff --git a/Geekbot.net/Commands/Admin/Owner.cs b/Geekbot.net/Commands/Admin/Owner.cs index 39ccd8c..4d4f0f2 100644 --- a/Geekbot.net/Commands/Admin/Owner.cs +++ b/Geekbot.net/Commands/Admin/Owner.cs @@ -12,7 +12,7 @@ using StackExchange.Redis; namespace Geekbot.net.Commands.Admin { [Group("owner")] - [RequireUserPermission(GuildPermission.Administrator)] + [RequireOwner] public class Owner : ModuleBase { private readonly DiscordSocketClient _client; @@ -21,8 +21,7 @@ namespace Geekbot.net.Commands.Admin private readonly IDatabase _redis; private readonly IUserRepository _userRepository; - public Owner(IDatabase redis, DiscordSocketClient client, IGeekbotLogger logger, IUserRepository userRepositry, - IErrorHandler errorHandler) + public Owner(IDatabase redis, DiscordSocketClient client, IGeekbotLogger logger, IUserRepository userRepositry, IErrorHandler errorHandler) { _redis = redis; _client = client; @@ -36,14 +35,6 @@ namespace Geekbot.net.Commands.Admin [Summary("Set the youtube api key")] public async Task SetYoutubeKey([Summary("API Key")] string key) { - var botOwner = Context.Guild.GetUserAsync(ulong.Parse(_redis.StringGet("botOwner"))).Result; - if (!Context.User.Id.ToString().Equals(botOwner.Id.ToString())) - { - await ReplyAsync( - $"Sorry, only the botowner can do this ({botOwner.Username}#{botOwner.Discriminator})"); - return; - } - _redis.StringSet("youtubeKey", key); await ReplyAsync("Apikey has been set"); } @@ -53,14 +44,6 @@ namespace Geekbot.net.Commands.Admin [Summary("Set the game that the bot is playing")] public async Task SetGame([Remainder] [Summary("Game")] string key) { - var botOwner = Context.Guild.GetUserAsync(ulong.Parse(_redis.StringGet("botOwner"))).Result; - if (!Context.User.Id.ToString().Equals(botOwner.Id.ToString())) - { - await ReplyAsync( - $"Sorry, only the botowner can do this ({botOwner.Username}#{botOwner.Discriminator})"); - return; - } - _redis.StringSet("Game", key); await _client.SetGameAsync(key); _logger.Information("Geekbot", $"Changed game to {key}"); @@ -72,22 +55,6 @@ namespace Geekbot.net.Commands.Admin [Summary("Populate user cache")] public async Task PopUserRepoCommand() { - try - { - var botOwner = Context.Guild.GetUserAsync(ulong.Parse(_redis.StringGet("botOwner"))).Result; - if (!Context.User.Id.ToString().Equals(botOwner.Id.ToString())) - { - await ReplyAsync( - $"Sorry, only the botowner can do this ({botOwner.Username}#{botOwner.Discriminator})"); - return; - } - } - catch (Exception) - { - await ReplyAsync("Sorry, only the botowner can do this"); - return; - } - var success = 0; var failed = 0; try @@ -114,5 +81,14 @@ namespace Geekbot.net.Commands.Admin "Couldn't complete User Repository, see console for more info"); } } + + [Command("error", RunMode = RunMode.Async)] + [Remarks(CommandCategories.Admin)] + [Summary("Throw an error un purpose")] + public void PurposefulError() + { + var e = new Exception("Error Generated by !owner error"); + _errorHandler.HandleCommandException(e, Context); + } } } \ No newline at end of file From 2b85caeb2979063dae3f225293a615baa916f62c Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 6 May 2018 01:47:13 +0200 Subject: [PATCH 047/443] Use LogSource Enum for logger and improve logging messages --- Geekbot.net/Commands/Admin/Owner.cs | 8 ++-- Geekbot.net/Commands/User/Ranking/Rank.cs | 2 +- Geekbot.net/Handlers.cs | 20 ++++----- Geekbot.net/Lib/Clients/MalClient.cs | 4 +- Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs | 19 +++++--- .../Lib/Localization/TranslationHandler.cs | 12 ++--- Geekbot.net/Lib/Logger/DiscordLogger.cs | 25 ++++++++--- Geekbot.net/Lib/Logger/GeekbotLogger.cs | 19 ++++---- Geekbot.net/Lib/Logger/IGeekbotLogger.cs | 10 ++--- Geekbot.net/Lib/Logger/LogDto.cs | 2 +- Geekbot.net/Lib/Logger/LogSource.cs | 16 +++++++ .../Lib/Logger/SimpleConextConverter.cs | 8 ++-- Geekbot.net/Lib/Media/FortunesProvider.cs | 4 +- Geekbot.net/Lib/Media/MediaProvider.cs | 18 ++++---- .../Lib/UserRepository/UserRepository.cs | 6 +-- Geekbot.net/Program.cs | 44 +++++++------------ 16 files changed, 122 insertions(+), 95 deletions(-) create mode 100644 Geekbot.net/Lib/Logger/LogSource.cs diff --git a/Geekbot.net/Commands/Admin/Owner.cs b/Geekbot.net/Commands/Admin/Owner.cs index 4d4f0f2..2d097e0 100644 --- a/Geekbot.net/Commands/Admin/Owner.cs +++ b/Geekbot.net/Commands/Admin/Owner.cs @@ -46,7 +46,7 @@ namespace Geekbot.net.Commands.Admin { _redis.StringSet("Game", key); await _client.SetGameAsync(key); - _logger.Information("Geekbot", $"Changed game to {key}"); + _logger.Information(LogSource.Geekbot, $"Changed game to {key}"); await ReplyAsync($"Now Playing {key}"); } @@ -59,11 +59,11 @@ namespace Geekbot.net.Commands.Admin var failed = 0; try { - _logger.Warning("UserRepository", "Populating User Repositry"); + _logger.Warning(LogSource.UserRepository, "Populating User Repositry"); await ReplyAsync("Starting Population of User Repository"); foreach (var guild in _client.Guilds) { - _logger.Information("UserRepository", $"Populating users from {guild.Name}"); + _logger.Information(LogSource.UserRepository, $"Populating users from {guild.Name}"); foreach (var user in guild.Users) { var succeded = await _userRepository.Update(user); @@ -71,7 +71,7 @@ namespace Geekbot.net.Commands.Admin } } - _logger.Warning("UserRepository", "Finished Updating User Repositry"); + _logger.Warning(LogSource.UserRepository, "Finished Updating User Repositry"); await ReplyAsync( $"Successfully Populated User Repository with {success} Users in {_client.Guilds.Count} Guilds (Failed: {failed})"); } diff --git a/Geekbot.net/Commands/User/Ranking/Rank.cs b/Geekbot.net/Commands/User/Ranking/Rank.cs index 590921e..08d4d3b 100644 --- a/Geekbot.net/Commands/User/Ranking/Rank.cs +++ b/Geekbot.net/Commands/User/Ranking/Rank.cs @@ -102,7 +102,7 @@ namespace Geekbot.net.Commands.User.Ranking } catch (Exception e) { - _logger.Warning("Geekbot", $"Could not retrieve user {user.Name}", e); + _logger.Warning(LogSource.Geekbot, $"Could not retrieve user {user.Name}", e); } } diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index 7d1523b..f248d5d 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -62,14 +62,14 @@ namespace Geekbot.net message.HasMentionPrefix(_client.CurrentUser, ref argPos))) return Task.CompletedTask; var context = new CommandContext(_client, message); var commandExec = _commands.ExecuteAsync(context, argPos, _servicesProvider); - _logger.Information("Command", + _logger.Information(LogSource.Command, context.Message.Content.Split(" ")[0].Replace("!", ""), SimpleConextConverter.ConvertContext(context)); return Task.CompletedTask; } catch (Exception e) { - _logger.Error("Geekbot", "Failed to run commands", e); + _logger.Error(LogSource.Geekbot, "Failed to Process Message", e); return Task.CompletedTask; } } @@ -81,7 +81,7 @@ namespace Geekbot.net if (message == null) return Task.CompletedTask; if (message.Channel.Name.StartsWith('@')) { - _logger.Information("Message", "DM-Channel - {message.Channel.Name} - {message.Content}"); + _logger.Information(LogSource.Message, $"[DM-Channel] {message.Content}", SimpleConextConverter.ConvertSocketMessage(message)); return Task.CompletedTask; } var channel = (SocketGuildChannel) message.Channel; @@ -90,11 +90,11 @@ namespace Geekbot.net _redis.HashIncrementAsync($"{channel.Guild.Id}:Messages", 0.ToString()); if (message.Author.IsBot) return Task.CompletedTask; - _logger.Information("Message", message.Content, SimpleConextConverter.ConvertSocketMessage(message)); + _logger.Information(LogSource.Message, message.Content, SimpleConextConverter.ConvertSocketMessage(message)); } catch (Exception e) { - _logger.Error("Message", "Could not process message stats", e); + _logger.Error(LogSource.Message, "Could not process message stats", e); } return Task.CompletedTask; } @@ -117,11 +117,11 @@ namespace Geekbot.net } } _userRepository.Update(user); - _logger.Information("Geekbot", $"{user.Username} ({user.Id}) joined {user.Guild.Name} ({user.Guild.Id})"); + _logger.Information(LogSource.Geekbot, $"{user.Username} ({user.Id}) joined {user.Guild.Name} ({user.Guild.Id})"); } catch (Exception e) { - _logger.Error("Geekbot", "Failed to send welcome message", e); + _logger.Error(LogSource.Geekbot, "Failed to send welcome message", e); } return Task.CompletedTask; } @@ -149,9 +149,9 @@ namespace Geekbot.net } catch (Exception e) { - _logger.Error("Geekbot", "Failed to send leave message", e); + _logger.Error(LogSource.Geekbot, "Failed to send leave message", e); } - _logger.Information("Geekbot", $"{user.Username} ({user.Id}) joined {user.Guild.Name} ({user.Guild.Id})"); + _logger.Information(LogSource.Geekbot, $"{user.Username} ({user.Id}) joined {user.Guild.Name} ({user.Guild.Id})"); } // @@ -187,7 +187,7 @@ namespace Geekbot.net } catch (Exception e) { - _logger.Error("Geekbot", "Failed to send delete message...", e); + _logger.Error(LogSource.Geekbot, "Failed to send delete message...", e); } } diff --git a/Geekbot.net/Lib/Clients/MalClient.cs b/Geekbot.net/Lib/Clients/MalClient.cs index d8bfe41..95d7dbd 100644 --- a/Geekbot.net/Lib/Clients/MalClient.cs +++ b/Geekbot.net/Lib/Clients/MalClient.cs @@ -42,10 +42,10 @@ namespace Geekbot.net.Lib.Clients } _animeSearch = new AnimeSearchMethodsAsync(_credentials); _mangaSearch = new MangaSearchMethodsAsync(_credentials); - _logger.Debug("Geekbot", "Logged in to MAL"); + _logger.Debug(LogSource.Geekbot, "Logged in to MAL"); return true; } - _logger.Debug("Geekbot", "No MAL Credentials Set!"); + _logger.Debug(LogSource.Geekbot, "No MAL Credentials Set!"); return false; } diff --git a/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs b/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs index 3d53242..382bdda 100644 --- a/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs +++ b/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs @@ -26,7 +26,7 @@ namespace Geekbot.net.Lib.ErrorHandling if (!string.IsNullOrEmpty(sentryDsn)) { _raven = new RavenClient(sentryDsn); - _logger.Information("Geekbot", $"Command Errors will be logged to Sentry: {sentryDsn}"); + _logger.Information(LogSource.Geekbot, $"Command Errors will be logged to Sentry: {sentryDsn}"); } else { @@ -42,14 +42,21 @@ namespace Geekbot.net.Lib.ErrorHandling var errorObj = SimpleConextConverter.ConvertContext(context); if (e.Message.Contains("50007")) return; if (e.Message.Contains("50013")) return; - _logger.Error("Geekbot", "An error ocured", e, errorObj); + _logger.Error(LogSource.Geekbot, "An error ocured", e, errorObj); if (!string.IsNullOrEmpty(errorMessage)) { if (_errorsInChat) { - var resStackTrace = string.IsNullOrEmpty(e.InnerException?.ToString()) ? e.StackTrace : e.InnerException.ToString(); - var maxLen = Math.Min(resStackTrace.Length, 1850); - context.Channel.SendMessageAsync($"{e.Message}\r\n```\r\n{resStackTrace.Substring(0, maxLen)}\r\n```"); + var resStackTrace = string.IsNullOrEmpty(e.InnerException?.ToString()) ? e.StackTrace : e.InnerException?.ToString(); + if (!string.IsNullOrEmpty(resStackTrace)) + { + var maxLen = Math.Min(resStackTrace.Length, 1850); + context.Channel.SendMessageAsync($"{e.Message}\r\n```\r\n{resStackTrace.Substring(0, maxLen)}\r\n```"); + } + else + { + context.Channel.SendMessageAsync(e.Message); + } } else { @@ -75,7 +82,7 @@ namespace Geekbot.net.Lib.ErrorHandling catch (Exception ex) { context.Channel.SendMessageAsync("Something went really really wrong here"); - _logger.Error("Geekbot", "Errorception", ex); + _logger.Error(LogSource.Geekbot, "Errorception", ex); } } diff --git a/Geekbot.net/Lib/Localization/TranslationHandler.cs b/Geekbot.net/Lib/Localization/TranslationHandler.cs index e57ee8d..e4af1e0 100644 --- a/Geekbot.net/Lib/Localization/TranslationHandler.cs +++ b/Geekbot.net/Lib/Localization/TranslationHandler.cs @@ -22,7 +22,7 @@ namespace Geekbot.net.Lib.Localization { _logger = logger; _redis = redis; - _logger.Information("Geekbot", "Loading Translations"); + _logger.Information(LogSource.Geekbot, "Loading Translations"); LoadTranslations(); LoadServerLanguages(clientGuilds); } @@ -71,7 +71,7 @@ namespace Geekbot.net.Lib.Localization } catch (Exception e) { - _logger.Error("Geekbot", "Failed to load Translations", e); + _logger.Error(LogSource.Geekbot, "Failed to load Translations", e); Environment.Exit(110); } } @@ -100,7 +100,7 @@ namespace Geekbot.net.Lib.Localization translation = _translations[command][stringName]["EN"]; if (string.IsNullOrWhiteSpace(translation)) { - _logger.Warning("Geekbot", $"No translation found for {command} - {stringName}"); + _logger.Warning(LogSource.Geekbot, $"No translation found for {command} - {stringName}"); } return translation; } @@ -114,7 +114,7 @@ namespace Geekbot.net.Lib.Localization } catch (Exception e) { - _logger.Error("Geekbot", "lol nope", e); + _logger.Error(LogSource.Geekbot, "lol nope", e); return new Dictionary(); } } @@ -127,7 +127,7 @@ namespace Geekbot.net.Lib.Localization } catch (Exception e) { - _logger.Error("Geekbot", "lol nope", e); + _logger.Error(LogSource.Geekbot, "lol nope", e); return new Dictionary(); } } @@ -143,7 +143,7 @@ namespace Geekbot.net.Lib.Localization } catch (Exception e) { - _logger.Error("Geekbot", "Error while changing language", e); + _logger.Error(LogSource.Geekbot, "Error while changing language", e); return false; } } diff --git a/Geekbot.net/Lib/Logger/DiscordLogger.cs b/Geekbot.net/Lib/Logger/DiscordLogger.cs index fc12201..2adcd73 100644 --- a/Geekbot.net/Lib/Logger/DiscordLogger.cs +++ b/Geekbot.net/Lib/Logger/DiscordLogger.cs @@ -1,5 +1,7 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using Discord; +using Geekbot.net.Commands.Randomness.Cat; namespace Geekbot.net.Lib.Logger { @@ -14,26 +16,37 @@ namespace Geekbot.net.Lib.Logger public Task Log(LogMessage message) { + LogSource source; + try + { + source = Enum.Parse(message.Source); + } + catch + { + source = LogSource.Discord; + _logger.Warning(LogSource.Geekbot, $"Could not parse {message.Source} to a LogSource Enum"); + } + var logMessage = $"[{message.Source}] {message.Message}"; switch (message.Severity) { case LogSeverity.Verbose: - _logger.Trace(message.Source, message.Message); + _logger.Trace(source, message.Message); break; case LogSeverity.Debug: - _logger.Debug(message.Source, message.Message); + _logger.Debug(source, message.Message); break; case LogSeverity.Info: - _logger.Information(message.Source, message.Message); + _logger.Information(source, message.Message); break; case LogSeverity.Critical: case LogSeverity.Error: case LogSeverity.Warning: if (logMessage.Contains("VOICE_STATE_UPDATE")) break; - _logger.Error(message.Source, message.Message, message.Exception); + _logger.Error(source, message.Message, message.Exception); break; default: - _logger.Information(message.Source, $"{logMessage} --- {message.Severity}"); + _logger.Information(source, $"{logMessage} --- {message.Severity}"); break; } return Task.CompletedTask; diff --git a/Geekbot.net/Lib/Logger/GeekbotLogger.cs b/Geekbot.net/Lib/Logger/GeekbotLogger.cs index 45d65f5..60628af 100644 --- a/Geekbot.net/Lib/Logger/GeekbotLogger.cs +++ b/Geekbot.net/Lib/Logger/GeekbotLogger.cs @@ -18,35 +18,36 @@ namespace Geekbot.net.Lib.Logger ReferenceLoopHandling = ReferenceLoopHandling.Serialize, NullValueHandling = NullValueHandling.Include }; - Information("Geekbot", "Using GeekbotLogger"); + Information(LogSource.Geekbot, "Using GeekbotLogger"); } - public void Trace(string source, string message, object extra = null) + public void Trace(LogSource source, string message, object extra = null) { _logger.Trace(CreateLogString("Trace", source, message, null, extra)); } - public void Debug(string source, string message, object extra = null) + public void Debug(LogSource source, string message, object extra = null) { _logger.Debug(CreateLogString("Debug", source, message, null, extra)); } - public void Information(string source, string message, object extra = null) + public void Information(LogSource source, string message, object extra = null) { _logger.Info(CreateLogString("Information", source, message, null, extra)); } - public void Warning(string source, string message, Exception stackTrace = null, object extra = null) + public void Warning(LogSource source, string message, Exception stackTrace = null, object extra = null) { _logger.Warn(CreateLogString("Warning", source, message, stackTrace, extra)); } - public void Error(string source, string message, Exception stackTrace, object extra = null) + public void Error(LogSource source, string message, Exception stackTrace, object extra = null) { - _logger.Error(CreateLogString("Error", source, message, stackTrace, extra)); + if (_logAsJson) _logger.Error(CreateLogString("Error", source, message, stackTrace, extra)); + else _logger.Error(stackTrace, CreateLogString("Error", source, message, stackTrace, extra)); } - private string CreateLogString(string type, string source, string message, Exception stackTrace = null, object extra = null) + private string CreateLogString(string type, LogSource source, string message, Exception stackTrace = null, object extra = null) { if (_logAsJson) { @@ -62,7 +63,7 @@ namespace Geekbot.net.Lib.Logger return JsonConvert.SerializeObject(logObject, Formatting.None, _serializerSettings); } - if (source != "Message") return $"[{source}] - {message}"; + if (source != LogSource.Message) return $"[{source}] - {message}"; var m = (MessageDto) extra; return $"[{source}] - [{m?.Guild.Name} - {m?.Channel.Name}] {m?.User.Name}: {m?.Message.Content}"; diff --git a/Geekbot.net/Lib/Logger/IGeekbotLogger.cs b/Geekbot.net/Lib/Logger/IGeekbotLogger.cs index 36d456d..1f76cb0 100644 --- a/Geekbot.net/Lib/Logger/IGeekbotLogger.cs +++ b/Geekbot.net/Lib/Logger/IGeekbotLogger.cs @@ -4,10 +4,10 @@ namespace Geekbot.net.Lib.Logger { public interface IGeekbotLogger { - void Trace(string source, string message, object extra = null); - void Debug(string source, string message, object extra = null); - void Information(string source, string message, object extra = null); - void Warning(string source, string message, Exception stackTrace = null, object extra = null); - void Error(string source, string message, Exception stackTrace, object extra = null); + void Trace(LogSource source, string message, object extra = null); + void Debug(LogSource source, string message, object extra = null); + void Information(LogSource source, string message, object extra = null); + void Warning(LogSource source, string message, Exception stackTrace = null, object extra = null); + void Error(LogSource source, string message, Exception stackTrace, object extra = null); } } \ No newline at end of file diff --git a/Geekbot.net/Lib/Logger/LogDto.cs b/Geekbot.net/Lib/Logger/LogDto.cs index 74695ec..8ccfcdf 100644 --- a/Geekbot.net/Lib/Logger/LogDto.cs +++ b/Geekbot.net/Lib/Logger/LogDto.cs @@ -6,7 +6,7 @@ namespace Geekbot.net.Lib.Logger { public DateTime Timestamp { get; set; } public string Type { get; set; } - public string Source { get; set; } + public LogSource Source { get; set; } public string Message { get; set; } public Exception StackTrace { get; set; } public object Extra { get; set; } diff --git a/Geekbot.net/Lib/Logger/LogSource.cs b/Geekbot.net/Lib/Logger/LogSource.cs new file mode 100644 index 0000000..46b902f --- /dev/null +++ b/Geekbot.net/Lib/Logger/LogSource.cs @@ -0,0 +1,16 @@ +namespace Geekbot.net.Lib.Logger +{ + public enum LogSource + { + Geekbot, + Rest, + Gateway, + Discord, + Redis, + Message, + UserRepository, + Command, + Api, + Other + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/Logger/SimpleConextConverter.cs b/Geekbot.net/Lib/Logger/SimpleConextConverter.cs index 4df802d..0845cc1 100644 --- a/Geekbot.net/Lib/Logger/SimpleConextConverter.cs +++ b/Geekbot.net/Lib/Logger/SimpleConextConverter.cs @@ -56,13 +56,13 @@ namespace Geekbot.net.Lib.Logger }, Guild = new MessageDto.IdAndName { - Id = channel.Guild.Id.ToString(), - Name = channel.Guild.Name + Id = channel?.Guild?.Id.ToString(), + Name = channel?.Guild?.Name }, Channel = new MessageDto.IdAndName { - Id = channel.Id.ToString(), - Name = channel.Name + Id = channel?.Id.ToString(), + Name = channel?.Name } }; } diff --git a/Geekbot.net/Lib/Media/FortunesProvider.cs b/Geekbot.net/Lib/Media/FortunesProvider.cs index dc97199..56b5ed6 100644 --- a/Geekbot.net/Lib/Media/FortunesProvider.cs +++ b/Geekbot.net/Lib/Media/FortunesProvider.cs @@ -17,11 +17,11 @@ namespace Geekbot.net.Lib.Media var rawFortunes = File.ReadAllText(path); _fortuneArray = rawFortunes.Split("%"); _totalFortunes = _fortuneArray.Length; - logger.Trace("Geekbot", $"Loaded {_totalFortunes} Fortunes"); + logger.Trace(LogSource.Geekbot, $"Loaded {_totalFortunes} Fortunes"); } else { - logger.Information("Geekbot", $"Fortunes File not found at {path}"); + logger.Information(LogSource.Geekbot, $"Fortunes File not found at {path}"); } } diff --git a/Geekbot.net/Lib/Media/MediaProvider.cs b/Geekbot.net/Lib/Media/MediaProvider.cs index 8f28d0d..7f883ae 100644 --- a/Geekbot.net/Lib/Media/MediaProvider.cs +++ b/Geekbot.net/Lib/Media/MediaProvider.cs @@ -22,7 +22,7 @@ namespace Geekbot.net.Lib.Media _random = new Random(); _logger = logger; - logger.Information("Geekbot", "Loading Media Files"); + logger.Information(LogSource.Geekbot, "Loading Media Files"); LoadCheckem(); LoadPandas(); @@ -38,56 +38,56 @@ namespace Geekbot.net.Lib.Media { var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/checkEmPics")); _checkemImages = rawLinks.Split("\n"); - _logger.Trace("Geekbot", $"Loaded {_checkemImages.Length} CheckEm Images"); + _logger.Trace(LogSource.Geekbot, $"Loaded {_checkemImages.Length} CheckEm Images"); } private void LoadPandas() { var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/pandas")); _pandaImages = rawLinks.Split("\n"); - _logger.Trace("Geekbot", $"Loaded {_pandaImages.Length} Panda Images"); + _logger.Trace(LogSource.Geekbot, $"Loaded {_pandaImages.Length} Panda Images"); } private void BakeCroissants() { var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/croissant")); _croissantImages = rawLinks.Split("\n"); - _logger.Trace("Geekbot", $"Loaded {_croissantImages.Length} Croissant Images"); + _logger.Trace(LogSource.Geekbot, $"Loaded {_croissantImages.Length} Croissant Images"); } private void LoadSquirrels() { var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/squirrel")); _squirrelImages = rawLinks.Split("\n"); - _logger.Trace("Geekbot", $"Loaded {_squirrelImages.Length} Squirrel Images"); + _logger.Trace(LogSource.Geekbot, $"Loaded {_squirrelImages.Length} Squirrel Images"); } private void LoadPumpkins() { var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/pumpkin")); _pumpkinImages = rawLinks.Split("\n"); - _logger.Trace("Geekbot", $"Loaded {_pumpkinImages.Length} Pumpkin Images"); + _logger.Trace(LogSource.Geekbot, $"Loaded {_pumpkinImages.Length} Pumpkin Images"); } private void LoadTurtles() { var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/turtles")); _turtlesImages = rawLinks.Split("\n"); - _logger.Trace("Geekbot", $"Loaded {_turtlesImages.Length} Turtle Images"); + _logger.Trace(LogSource.Geekbot, $"Loaded {_turtlesImages.Length} Turtle Images"); } private void LoadPinguins() { var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/pinguins")); _pinguinImages = rawLinks.Split("\n"); - _logger.Trace("Geekbot", $"Loaded {_pinguinImages.Length} Pinguin Images"); + _logger.Trace(LogSource.Geekbot, $"Loaded {_pinguinImages.Length} Pinguin Images"); } private void LoadFoxes() { var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/foxes")); _foxImages = rawLinks.Split("\n"); - _logger.Trace("Geekbot", $"Loaded {_foxImages.Length} Foxes Images"); + _logger.Trace(LogSource.Geekbot, $"Loaded {_foxImages.Length} Foxes Images"); } public string GetCheckem() diff --git a/Geekbot.net/Lib/UserRepository/UserRepository.cs b/Geekbot.net/Lib/UserRepository/UserRepository.cs index 558732e..3937107 100644 --- a/Geekbot.net/Lib/UserRepository/UserRepository.cs +++ b/Geekbot.net/Lib/UserRepository/UserRepository.cs @@ -36,12 +36,12 @@ namespace Geekbot.net.Lib.UserRepository } Store(savedUser); - _logger.Information("UserRepository", "Updated User", savedUser); + _logger.Information(LogSource.UserRepository, "Updated User", savedUser); return Task.FromResult(true); } catch (Exception e) { - _logger.Warning("UserRepository", $"Failed to update user: {user.Username}#{user.Discriminator} ({user.Id})", e); + _logger.Warning(LogSource.UserRepository, $"Failed to update user: {user.Username}#{user.Discriminator} ({user.Id})", e); return Task.FromResult(false); } } @@ -103,7 +103,7 @@ namespace Geekbot.net.Lib.UserRepository } catch (Exception e) { - _logger.Warning("UserRepository", "Failed to get {userId} from repository", e); + _logger.Warning(LogSource.UserRepository, $"Failed to get {userId} from repository", e); return new UserRepositoryUser(); } } diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 82016fa..bc90b4f 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -55,14 +55,14 @@ namespace Geekbot.net Console.WriteLine(logo.ToString()); var sumologicActive = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GEEKBOT_SUMO")); var logger = new GeekbotLogger(runParameters, sumologicActive); - logger.Information("Geekbot", "Starting..."); + logger.Information(LogSource.Geekbot, "Starting..."); try { new Program().MainAsync(runParameters, logger).GetAwaiter().GetResult(); } catch (Exception e) { - logger.Error("Geekbot", "RIP", e); + logger.Error(LogSource.Geekbot, "RIP", e); } } @@ -70,7 +70,7 @@ namespace Geekbot.net { _logger = logger; _runParameters = runParameters; - logger.Information("Geekbot", "Initing Stuff"); + logger.Information(LogSource.Geekbot, "Initing Stuff"); var discordLogger = new DiscordLogger(logger); _client = new DiscordSocketClient(new DiscordSocketConfig @@ -85,11 +85,11 @@ namespace Geekbot.net { var redisMultiplexer = ConnectionMultiplexer.Connect("127.0.0.1:6379"); _redis = redisMultiplexer.GetDatabase(6); - logger.Information("Redis", $"Connected to db {_redis.Database}"); + logger.Information(LogSource.Redis, $"Connected to db {_redis.Database}"); } catch (Exception e) { - logger.Error("Redis", "Redis Connection Failed", e); + logger.Error(LogSource.Redis, "Redis Connection Failed", e); Environment.Exit(102); } @@ -128,7 +128,7 @@ namespace Geekbot.net _services.AddSingleton(wikipediaClient); _services.AddSingleton(audioUtils); - logger.Information("Geekbot", "Connecting to Discord"); + logger.Information(LogSource.Geekbot, "Connecting to Discord"); await Login(); @@ -145,9 +145,9 @@ namespace Geekbot.net if (isConneted) { await _client.SetGameAsync(_redis.StringGet("Game")); - _logger.Information("Geekbot", $"Now Connected as {_client.CurrentUser.Username} to {_client.Guilds.Count} Servers"); + _logger.Information(LogSource.Geekbot, $"Now Connected as {_client.CurrentUser.Username} to {_client.Guilds.Count} Servers"); - _logger.Information("Geekbot", "Registering Stuff"); + _logger.Information(LogSource.Geekbot, "Registering Stuff"); var translationHandler = new TranslationHandler(_client.Guilds, _redis, _logger); var errorHandler = new ErrorHandler(_logger, translationHandler, _runParameters.ExposeErrors); var reactionListener = new ReactionListener(_redis); @@ -172,21 +172,21 @@ namespace Geekbot.net if (_firstStart || _runParameters.Reset) { - _logger.Information("Geekbot", "Finishing setup"); + _logger.Information(LogSource.Geekbot, "Finishing setup"); await FinishSetup(); - _logger.Information("Geekbot", "Setup finished"); + _logger.Information(LogSource.Geekbot, "Setup finished"); } if (!_runParameters.DisableApi) { StartWebApi(); } - _logger.Information("Geekbot", "Done and ready for use"); + _logger.Information(LogSource.Geekbot, "Done and ready for use"); } } catch (Exception e) { - _logger.Error("Discord", "Could not connect...", e); + _logger.Error(LogSource.Geekbot, "Could not connect...", e); Environment.Exit(103); } } @@ -200,33 +200,23 @@ namespace Geekbot.net private void StartWebApi() { - _logger.Information("API", "Starting Webserver"); + _logger.Information(LogSource.Api, "Starting Webserver"); var webApiUrl = new Uri("http://localhost:12995"); new NancyHost(webApiUrl).Start(); - _logger.Information("API", $"Webserver now running on {webApiUrl}"); + _logger.Information(LogSource.Api, $"Webserver now running on {webApiUrl}"); } private async Task FinishSetup() { - var appInfo = await _client.GetApplicationInfoAsync(); - _logger.Information("Setup", $"Just a moment while i setup everything {appInfo.Owner.Username}"); try { + // ToDo: Set bot avatar + var appInfo = await _client.GetApplicationInfoAsync(); _redis.StringSet("botOwner", appInfo.Owner.Id); - var req = WebRequest.Create(appInfo.IconUrl); - using (var stream = req.GetResponse().GetResponseStream()) - { - await _client.CurrentUser.ModifyAsync(user => - { - user.Avatar = new Image(stream); - user.Username = appInfo.Name.ToString(); - }); - } - _logger.Information("Setup", "Everything done, enjoy!"); } catch (Exception e) { - _logger.Warning("Setup", "Oha, it seems like something went wrong while running the setup, geekbot will work never the less though", e); + _logger.Warning(LogSource.Geekbot, "Setup Failed, couldn't retrieve discord application data", e); } return Task.CompletedTask; } From fe5e2cb80fbe29b1598bff9fc5fb22eb6677c63c Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 6 May 2018 02:00:45 +0200 Subject: [PATCH 048/443] Create Enum for exit codes --- Geekbot.net/Lib/GeekbotExitCode.cs | 19 +++++++++++++++++++ .../Lib/Localization/TranslationHandler.cs | 8 ++++---- Geekbot.net/Program.cs | 8 ++++---- 3 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 Geekbot.net/Lib/GeekbotExitCode.cs diff --git a/Geekbot.net/Lib/GeekbotExitCode.cs b/Geekbot.net/Lib/GeekbotExitCode.cs new file mode 100644 index 0000000..e9e1210 --- /dev/null +++ b/Geekbot.net/Lib/GeekbotExitCode.cs @@ -0,0 +1,19 @@ +namespace Geekbot.net.Lib +{ + public enum GeekbotExitCode : int + { + // General + Clean = 0, + InvalidArguments = 1, + + // Geekbot Internals + TranslationsFailed = 201, + + // Dependent Services + RedisConnectionFailed = 301, + + // Discord Related + CouldNotLogin = 401 + + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/Localization/TranslationHandler.cs b/Geekbot.net/Lib/Localization/TranslationHandler.cs index e4af1e0..550759d 100644 --- a/Geekbot.net/Lib/Localization/TranslationHandler.cs +++ b/Geekbot.net/Lib/Localization/TranslationHandler.cs @@ -72,7 +72,7 @@ namespace Geekbot.net.Lib.Localization catch (Exception e) { _logger.Error(LogSource.Geekbot, "Failed to load Translations", e); - Environment.Exit(110); + Environment.Exit(GeekbotExitCode.TranslationsFailed.GetHashCode()); } } @@ -114,8 +114,8 @@ namespace Geekbot.net.Lib.Localization } catch (Exception e) { - _logger.Error(LogSource.Geekbot, "lol nope", e); - return new Dictionary(); + _logger.Error(LogSource.Geekbot, "No translations for command found", e); + return new Dictionary(); } } @@ -127,7 +127,7 @@ namespace Geekbot.net.Lib.Localization } catch (Exception e) { - _logger.Error(LogSource.Geekbot, "lol nope", e); + _logger.Error(LogSource.Geekbot, "No translations for command found", e); return new Dictionary(); } } diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index bc90b4f..3a35d19 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -43,7 +43,7 @@ namespace Geekbot.net RunParameters runParameters = null; Parser.Default.ParseArguments(args) .WithParsed(e => runParameters = e) - .WithNotParsed(_ => Environment.Exit(1)); + .WithNotParsed(_ => Environment.Exit(GeekbotExitCode.InvalidArguments.GetHashCode())); var logo = new StringBuilder(); logo.AppendLine(@" ____ _____ _____ _ ______ ___ _____"); @@ -90,10 +90,10 @@ namespace Geekbot.net catch (Exception e) { logger.Error(LogSource.Redis, "Redis Connection Failed", e); - Environment.Exit(102); + Environment.Exit(GeekbotExitCode.RedisConnectionFailed.GetHashCode()); } - _token = runParameters.Token ??_redis.StringGet("discordToken"); + _token = runParameters.Token ?? _redis.StringGet("discordToken"); if (_token.IsNullOrEmpty) { Console.Write("Your bot Token: "); @@ -187,7 +187,7 @@ namespace Geekbot.net catch (Exception e) { _logger.Error(LogSource.Geekbot, "Could not connect...", e); - Environment.Exit(103); + Environment.Exit(GeekbotExitCode.CouldNotLogin.GetHashCode()); } } From 7eb62cb267d74da821bd5bbd1a6f6ae9459544d3 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 6 May 2018 02:05:52 +0200 Subject: [PATCH 049/443] Serialize LogSource as string instead of numerical representation --- Geekbot.net/Lib/Logger/LogSource.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Geekbot.net/Lib/Logger/LogSource.cs b/Geekbot.net/Lib/Logger/LogSource.cs index 46b902f..d9a8629 100644 --- a/Geekbot.net/Lib/Logger/LogSource.cs +++ b/Geekbot.net/Lib/Logger/LogSource.cs @@ -1,5 +1,9 @@ -namespace Geekbot.net.Lib.Logger +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; + +namespace Geekbot.net.Lib.Logger { + [JsonConverter(typeof(StringEnumConverter))] public enum LogSource { Geekbot, From 7238e7177d413430aed8b14508278a686b4922a4 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 6 May 2018 02:43:23 +0200 Subject: [PATCH 050/443] Bug fixes in !quote --- Geekbot.net/Commands/Utils/Quote/Quote.cs | 32 +++++++++++++++----- Geekbot.net/Lib/Polyfills/UserPolyfillDto.cs | 31 +++++++++++++++++++ 2 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 Geekbot.net/Lib/Polyfills/UserPolyfillDto.cs diff --git a/Geekbot.net/Commands/Utils/Quote/Quote.cs b/Geekbot.net/Commands/Utils/Quote/Quote.cs index f1941ba..7a25b50 100644 --- a/Geekbot.net/Commands/Utils/Quote/Quote.cs +++ b/Geekbot.net/Commands/Utils/Quote/Quote.cs @@ -5,6 +5,7 @@ using Discord; using Discord.Commands; using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.Polyfills; using Newtonsoft.Json; using StackExchange.Redis; @@ -30,6 +31,11 @@ namespace Geekbot.net.Commands.Utils.Quote try { var randomQuotes = _redis.SetMembers($"{Context.Guild.Id}:Quotes"); + if (!randomQuotes.Any()) + { + await ReplyAsync("This server doesn't seem to have any quotes yet. You can add a quote with `!quote save @user` or `!quote save `"); + return; + } var randomNumber = new Random().Next(randomQuotes.Length - 1); var randomQuote = randomQuotes[randomNumber]; var quote = JsonConvert.DeserializeObject(randomQuote); @@ -62,6 +68,7 @@ namespace Geekbot.net.Commands.Utils.Quote } var lastMessage = await GetLastMessageByUser(user); + if (lastMessage == null) return; var quote = CreateQuoteObject(lastMessage); var quoteStore = JsonConvert.SerializeObject(quote); _redis.SetAdd($"{Context.Guild.Id}:Quotes", quoteStore); @@ -116,6 +123,7 @@ namespace Geekbot.net.Commands.Utils.Quote try { var lastMessage = await GetLastMessageByUser(user); + if (lastMessage == null) return; var quote = CreateQuoteObject(lastMessage); var embed = QuoteBuilder(quote); await ReplyAsync("", false, embed.Build()); @@ -178,18 +186,26 @@ namespace Geekbot.net.Commands.Utils.Quote private async Task GetLastMessageByUser(IUser user) { - var list = Context.Channel.GetMessagesAsync().Flatten(); - await list; - return list.Result - .First(msg => msg.Author.Id == user.Id - && msg.Embeds.Count == 0 - && msg.Id != Context.Message.Id - && !msg.Content.ToLower().StartsWith("!")); + try + { + var list = Context.Channel.GetMessagesAsync().Flatten(); + await list; + return list.Result + .First(msg => msg.Author.Id == user.Id + && msg.Embeds.Count == 0 + && msg.Id != Context.Message.Id + && !msg.Content.ToLower().StartsWith("!")); + } + catch + { + await ReplyAsync($"No quoteable message have been sent by {user.Username} in this channel"); + return null; + } } private EmbedBuilder QuoteBuilder(QuoteObjectDto quote, int id = 0) { - var user = Context.Client.GetUserAsync(quote.UserId).Result; + var user = Context.Client.GetUserAsync(quote.UserId).Result ?? new UserPolyfillDto { Username = "Unknown User" }; var eb = new EmbedBuilder(); eb.WithColor(new Color(143, 167, 232)); eb.Title = id == 0 ? "" : $"#{id} | "; diff --git a/Geekbot.net/Lib/Polyfills/UserPolyfillDto.cs b/Geekbot.net/Lib/Polyfills/UserPolyfillDto.cs new file mode 100644 index 0000000..407ae36 --- /dev/null +++ b/Geekbot.net/Lib/Polyfills/UserPolyfillDto.cs @@ -0,0 +1,31 @@ +using System; +using System.Threading.Tasks; +using Discord; + +namespace Geekbot.net.Lib.Polyfills +{ + internal class UserPolyfillDto : IUser + { + public ulong Id { get; set; } + public DateTimeOffset CreatedAt { get; set; } + public string Mention { get; set; } + public Game? Game { get; set; } + public UserStatus Status { get; set; } + public string AvatarId { get; set; } + public string Discriminator { get; set; } + public ushort DiscriminatorValue { get; set; } + public bool IsBot { get; set; } + public bool IsWebhook { get; set; } + public string Username { get; set; } + + public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128) + { + return "https://discordapp.com/assets/6debd47ed13483642cf09e832ed0bc1b.png"; + } + + public Task GetOrCreateDMChannelAsync(RequestOptions options = null) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file From 59ee6b289acb6101ceb9525c563e77059500281a Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 6 May 2018 03:24:09 +0200 Subject: [PATCH 051/443] Include git sha in binary --- .gitlab-ci.yml | 2 +- Geekbot.net/Commands/Utils/Info.cs | 2 +- Geekbot.net/Geekbot.net.csproj | 2 ++ Geekbot.net/Lib/Constants.cs | 11 +++++++++-- Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs | 2 +- Geekbot.net/WebApi/Status/StatusController.cs | 2 +- 6 files changed, 15 insertions(+), 6 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 3928542..98eb9ba 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -22,7 +22,7 @@ build: script: - dotnet restore - dotnet test Tests - - dotnet publish --configuration Release -o Binaries ./ + - dotnet publish --version-suffix ${CI_COMMIT_SHA:0:8} --configuration Release -o Binaries ./ deploy: stage: deploy diff --git a/Geekbot.net/Commands/Utils/Info.cs b/Geekbot.net/Commands/Utils/Info.cs index 446abfe..de34e89 100644 --- a/Geekbot.net/Commands/Utils/Info.cs +++ b/Geekbot.net/Commands/Utils/Info.cs @@ -37,7 +37,7 @@ namespace Geekbot.net.Commands.Utils eb.WithAuthor(new EmbedAuthorBuilder() .WithIconUrl(_client.CurrentUser.GetAvatarUrl()) - .WithName($"{Constants.Name} V{Constants.BotVersion}")); + .WithName($"{Constants.Name} V{Constants.BotVersion()}")); var botOwner = await Context.Guild.GetUserAsync(ulong.Parse(_redis.StringGet("botOwner"))); var uptime = DateTime.Now.Subtract(Process.GetCurrentProcess().StartTime); diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 861085b..90ff896 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -4,6 +4,8 @@ netcoreapp2.0 derp.ico 3.7.0 + $(VersionSuffix) + $(Version)-$(VersionSuffix) Pizza and Coffee Studios Pizza and Coffee Studios A Discord bot diff --git a/Geekbot.net/Lib/Constants.cs b/Geekbot.net/Lib/Constants.cs index dbba570..240cfce 100644 --- a/Geekbot.net/Lib/Constants.cs +++ b/Geekbot.net/Lib/Constants.cs @@ -1,9 +1,16 @@ -namespace Geekbot.net.Lib +using System.Reflection; + +namespace Geekbot.net.Lib { public class Constants { public const string Name = "Geekbot"; - public const double BotVersion = 3.7; + + public static string BotVersion() + { + return typeof(Program).Assembly.GetCustomAttribute().InformationalVersion; + } + public const double ApiVersion = 1; } } \ No newline at end of file diff --git a/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs b/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs index 382bdda..99d1e8b 100644 --- a/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs +++ b/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs @@ -25,7 +25,7 @@ namespace Geekbot.net.Lib.ErrorHandling var sentryDsn = Environment.GetEnvironmentVariable("SENTRY"); if (!string.IsNullOrEmpty(sentryDsn)) { - _raven = new RavenClient(sentryDsn); + _raven = new RavenClient(sentryDsn) { Release = Constants.BotVersion() }; _logger.Information(LogSource.Geekbot, $"Command Errors will be logged to Sentry: {sentryDsn}"); } else diff --git a/Geekbot.net/WebApi/Status/StatusController.cs b/Geekbot.net/WebApi/Status/StatusController.cs index 5f995c9..05196a2 100644 --- a/Geekbot.net/WebApi/Status/StatusController.cs +++ b/Geekbot.net/WebApi/Status/StatusController.cs @@ -11,7 +11,7 @@ namespace Geekbot.net.WebApi.Status { var responseBody = new ApiStatusDto { - GeekbotVersion = Constants.BotVersion.ToString(), + GeekbotVersion = Constants.BotVersion(), ApiVersion = Constants.ApiVersion.ToString(), Status = "Online" }; From 4d39850373244a966c27f87d5845afa6d2c1d885 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 6 May 2018 23:08:04 +0200 Subject: [PATCH 052/443] A dice can now have 144 sides instead of 120 --- Geekbot.net/Commands/Utils/Dice/Dice.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Geekbot.net/Commands/Utils/Dice/Dice.cs b/Geekbot.net/Commands/Utils/Dice/Dice.cs index 7fc4f84..9c169c7 100644 --- a/Geekbot.net/Commands/Utils/Dice/Dice.cs +++ b/Geekbot.net/Commands/Utils/Dice/Dice.cs @@ -51,9 +51,9 @@ namespace Geekbot.net.Commands.Utils.Dice return; } - if (dices.Any(d => d.Sides > 120)) + if (dices.Any(d => d.Sides > 144)) { - await ReplyAsync("A dice can't have more than 120 sides"); + await ReplyAsync("A dice can't have more than 144 sides"); return; } From d671817b0da09266633395959640779d50f1b7c1 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 6 May 2018 23:28:44 +0200 Subject: [PATCH 053/443] Make constants static and use non buffered sumologic client --- Geekbot.net/Lib/Constants.cs | 2 +- Geekbot.net/Lib/Logger/LoggerFactory.cs | 12 ++++-------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/Geekbot.net/Lib/Constants.cs b/Geekbot.net/Lib/Constants.cs index 240cfce..f80f182 100644 --- a/Geekbot.net/Lib/Constants.cs +++ b/Geekbot.net/Lib/Constants.cs @@ -2,7 +2,7 @@ namespace Geekbot.net.Lib { - public class Constants + public static class Constants { public const string Name = "Geekbot"; diff --git a/Geekbot.net/Lib/Logger/LoggerFactory.cs b/Geekbot.net/Lib/Logger/LoggerFactory.cs index d32d80b..4095d00 100644 --- a/Geekbot.net/Lib/Logger/LoggerFactory.cs +++ b/Geekbot.net/Lib/Logger/LoggerFactory.cs @@ -18,19 +18,15 @@ namespace Geekbot.net.Lib.Logger Console.WriteLine("Logging Geekbot Logs to Sumologic"); config.LoggingRules.Add( new LoggingRule("*", LogLevel.Debug, LogLevel.Fatal, - new BufferedSumoLogicTarget() + new SumoLogicTarget() { Url = Environment.GetEnvironmentVariable("GEEKBOT_SUMO"), SourceName = "GeekbotLogger", Layout = "${message}", - UseConsoleLog = true, - MaxQueueSizeBytes = 500000, - FlushingAccuracy = 250, - MaxFlushInterval = 10000, + UseConsoleLog = false, OptimizeBufferReuse = true, - MessagesPerRequest = 10, - RetryInterval = 5000, - Name = "Geekbot" + Name = "Geekbot", + AppendException = false }) ); } From 83f3c61661077c69a973e877fc03c322f22ad736 Mon Sep 17 00:00:00 2001 From: runebaas Date: Tue, 8 May 2018 00:22:14 +0200 Subject: [PATCH 054/443] Log errors as info when logging to sumologic --- Geekbot.net/Lib/Logger/GeekbotLogger.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Geekbot.net/Lib/Logger/GeekbotLogger.cs b/Geekbot.net/Lib/Logger/GeekbotLogger.cs index 60628af..b2bb677 100644 --- a/Geekbot.net/Lib/Logger/GeekbotLogger.cs +++ b/Geekbot.net/Lib/Logger/GeekbotLogger.cs @@ -28,7 +28,8 @@ namespace Geekbot.net.Lib.Logger public void Debug(LogSource source, string message, object extra = null) { - _logger.Debug(CreateLogString("Debug", source, message, null, extra)); + if (_logAsJson) _logger.Info(CreateLogString("Debug", source, message, null, extra)); + else _logger.Debug(CreateLogString("Debug", source, message, null, extra)); } public void Information(LogSource source, string message, object extra = null) @@ -38,12 +39,13 @@ namespace Geekbot.net.Lib.Logger public void Warning(LogSource source, string message, Exception stackTrace = null, object extra = null) { - _logger.Warn(CreateLogString("Warning", source, message, stackTrace, extra)); + if (_logAsJson) _logger.Info(CreateLogString("Warning", source, message, stackTrace, extra)); + else _logger.Warn(CreateLogString("Warning", source, message, stackTrace, extra)); } public void Error(LogSource source, string message, Exception stackTrace, object extra = null) { - if (_logAsJson) _logger.Error(CreateLogString("Error", source, message, stackTrace, extra)); + if (_logAsJson) _logger.Info(CreateLogString("Error", source, message, stackTrace, extra)); else _logger.Error(stackTrace, CreateLogString("Error", source, message, stackTrace, extra)); } From 510b030fecca6981d78a4fcec948c3bd146ba99a Mon Sep 17 00:00:00 2001 From: Runebaas Date: Wed, 9 May 2018 01:21:39 +0200 Subject: [PATCH 055/443] Begining of implementing the entity framework --- Geekbot.net/Commands/Utils/Quote/Quote.cs | 94 ++++++++++++----------- Geekbot.net/Database/DatabaseContext.cs | 15 ++++ Geekbot.net/Database/QuoteModel.cs | 17 ++++ Geekbot.net/Geekbot.net.csproj | 5 ++ Geekbot.net/Lib/GeekbotExitCode.cs | 1 + Geekbot.net/Lib/RunParameters.cs | 3 + Geekbot.net/Program.cs | 16 ++++ 7 files changed, 108 insertions(+), 43 deletions(-) create mode 100644 Geekbot.net/Database/DatabaseContext.cs create mode 100644 Geekbot.net/Database/QuoteModel.cs diff --git a/Geekbot.net/Commands/Utils/Quote/Quote.cs b/Geekbot.net/Commands/Utils/Quote/Quote.cs index 7a25b50..0310182 100644 --- a/Geekbot.net/Commands/Utils/Quote/Quote.cs +++ b/Geekbot.net/Commands/Utils/Quote/Quote.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; +using Geekbot.net.Database; using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Polyfills; @@ -15,12 +16,14 @@ namespace Geekbot.net.Commands.Utils.Quote public class Quote : ModuleBase { private readonly IErrorHandler _errorHandler; + private readonly DatabaseContext _database; private readonly IDatabase _redis; - public Quote(IDatabase redis, IErrorHandler errorHandler) + public Quote(IDatabase redis, IErrorHandler errorHandler, DatabaseContext database) { _redis = redis; _errorHandler = errorHandler; + _database = database; } [Command] @@ -30,16 +33,18 @@ namespace Geekbot.net.Commands.Utils.Quote { try { - var randomQuotes = _redis.SetMembers($"{Context.Guild.Id}:Quotes"); - if (!randomQuotes.Any()) + var s = _database.Quotes.Where(e => e.GuildId.Equals(Context.Guild.Id)); + var totalQuotes = s.Count(); + + if (totalQuotes > 0) { await ReplyAsync("This server doesn't seem to have any quotes yet. You can add a quote with `!quote save @user` or `!quote save `"); return; } - var randomNumber = new Random().Next(randomQuotes.Length - 1); - var randomQuote = randomQuotes[randomNumber]; - var quote = JsonConvert.DeserializeObject(randomQuote); - var embed = QuoteBuilder(quote, randomNumber + 1); + var quote = s.Where(e => e.Id.Equals(new Random().Next(totalQuotes)))?.FirstOrDefault(); + + + var embed = QuoteBuilder(quote, 2); await ReplyAsync("", false, embed.Build()); } catch (Exception e) @@ -69,9 +74,10 @@ namespace Geekbot.net.Commands.Utils.Quote var lastMessage = await GetLastMessageByUser(user); if (lastMessage == null) return; + var quote = CreateQuoteObject(lastMessage); - var quoteStore = JsonConvert.SerializeObject(quote); - _redis.SetAdd($"{Context.Guild.Id}:Quotes", quoteStore); + await _database.Quotes.AddAsync(quote); + var embed = QuoteBuilder(quote); await ReplyAsync("**Quote Added**", false, embed.Build()); } @@ -103,8 +109,8 @@ namespace Geekbot.net.Commands.Utils.Quote } var quote = CreateQuoteObject(message); - var quoteStore = JsonConvert.SerializeObject(quote); - _redis.SetAdd($"{Context.Guild.Id}:Quotes", quoteStore); + await _database.Quotes.AddAsync(quote); + var embed = QuoteBuilder(quote); await ReplyAsync("**Quote Added**", false, embed.Build()); } @@ -154,35 +160,36 @@ namespace Geekbot.net.Commands.Utils.Quote } } - [Command("remove")] - [RequireUserPermission(GuildPermission.KickMembers)] - [RequireUserPermission(GuildPermission.ManageMessages)] - [RequireUserPermission(GuildPermission.ManageRoles)] - [Remarks(CommandCategories.Quotes)] - [Summary("Remove a quote (required mod permissions)")] - public async Task RemoveQuote([Summary("quoteId")] int id) - { - try - { - var quotes = _redis.SetMembers($"{Context.Guild.Id}:Quotes"); - var success = _redis.SetRemove($"{Context.Guild.Id}:Quotes", quotes[id - 1]); - if (success) - { - var quote = JsonConvert.DeserializeObject(quotes[id - 1]); - var embed = QuoteBuilder(quote); - await ReplyAsync($"**Removed #{id}**", false, embed.Build()); - } - else - { - await ReplyAsync("I couldn't find a quote with that id :disappointed:"); - } - } - catch (Exception e) - { - _errorHandler.HandleCommandException(e, Context, - "I couldn't find a quote with that id :disappointed:"); - } - } + [Command("remove")] + [RequireUserPermission(GuildPermission.KickMembers)] + [RequireUserPermission(GuildPermission.ManageMessages)] + [RequireUserPermission(GuildPermission.ManageRoles)] + [Remarks(CommandCategories.Quotes)] + [Summary("Remove a quote (required mod permissions)")] + public async Task RemoveQuote([Summary("quoteId")] int id) + { + await ReplyAsync("Command currently Disabled"); + // try + // { + // var quotes = _redis.SetMembers($"{Context.Guild.Id}:Quotes"); + // var success = _redis.SetRemove($"{Context.Guild.Id}:Quotes", quotes[id - 1]); + // if (success) + // { + // var quote = JsonConvert.DeserializeObject(quotes[id - 1]); + // var embed = QuoteBuilder(quote); + // await ReplyAsync($"**Removed #{id}**", false, embed.Build()); + // } + // else + // { + // await ReplyAsync("I couldn't find a quote with that id :disappointed:"); + // } + // } + // catch (Exception e) + // { + // _errorHandler.HandleCommandException(e, Context, + // "I couldn't find a quote with that id :disappointed:"); + // } + } private async Task GetLastMessageByUser(IUser user) { @@ -203,7 +210,7 @@ namespace Geekbot.net.Commands.Utils.Quote } } - private EmbedBuilder QuoteBuilder(QuoteObjectDto quote, int id = 0) + private EmbedBuilder QuoteBuilder(QuoteModel quote, int id = 0) { var user = Context.Client.GetUserAsync(quote.UserId).Result ?? new UserPolyfillDto { Username = "Unknown User" }; var eb = new EmbedBuilder(); @@ -216,7 +223,7 @@ namespace Geekbot.net.Commands.Utils.Quote return eb; } - private QuoteObjectDto CreateQuoteObject(IMessage message) + private QuoteModel CreateQuoteObject(IMessage message) { string image; try @@ -228,8 +235,9 @@ namespace Geekbot.net.Commands.Utils.Quote image = null; } - return new QuoteObjectDto + return new QuoteModel() { + GuildId = Context.Guild.Id, UserId = message.Author.Id, Time = message.Timestamp.DateTime, Quote = message.Content, diff --git a/Geekbot.net/Database/DatabaseContext.cs b/Geekbot.net/Database/DatabaseContext.cs new file mode 100644 index 0000000..d2645a1 --- /dev/null +++ b/Geekbot.net/Database/DatabaseContext.cs @@ -0,0 +1,15 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; + +namespace Geekbot.net.Database +{ + public class DatabaseContext : DbContext + { + public DbSet Quotes { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + => optionsBuilder.UseLoggerFactory(new LoggerFactory().AddConsole()) +// .UseInMemoryDatabase(databaseName: "Geekbot"); + .UseMySql(@"Server=localhost;database=geekbot;uid=geekbot;"); + } +} \ No newline at end of file diff --git a/Geekbot.net/Database/QuoteModel.cs b/Geekbot.net/Database/QuoteModel.cs new file mode 100644 index 0000000..ea42e93 --- /dev/null +++ b/Geekbot.net/Database/QuoteModel.cs @@ -0,0 +1,17 @@ +using System; +using System.ComponentModel.DataAnnotations; +using Microsoft.EntityFrameworkCore.Scaffolding.Metadata; + +namespace Geekbot.net.Database +{ + public class QuoteModel + { + [Key] + public int Id { get; set; } + public ulong GuildId { get; set; } + public ulong UserId { get; set; } + public string Quote { get; set; } + public DateTime Time { get; set; } + public string Image { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 90ff896..f8554c9 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -19,6 +19,9 @@
+ + + @@ -31,6 +34,8 @@ + + 1.2.6 diff --git a/Geekbot.net/Lib/GeekbotExitCode.cs b/Geekbot.net/Lib/GeekbotExitCode.cs index e9e1210..d68aa4a 100644 --- a/Geekbot.net/Lib/GeekbotExitCode.cs +++ b/Geekbot.net/Lib/GeekbotExitCode.cs @@ -11,6 +11,7 @@ // Dependent Services RedisConnectionFailed = 301, + DatabaseConnectionFailed = 302, // Discord Related CouldNotLogin = 401 diff --git a/Geekbot.net/Lib/RunParameters.cs b/Geekbot.net/Lib/RunParameters.cs index 35c1a2a..1e1048b 100644 --- a/Geekbot.net/Lib/RunParameters.cs +++ b/Geekbot.net/Lib/RunParameters.cs @@ -22,5 +22,8 @@ namespace Geekbot.net.Lib [Option("token", Default = null, HelpText = "Set a new bot token")] public string Token { get; set; } + + [Option("in-memory", Default = false, HelpText = "Disables the web api")] + public bool InMemory { get; set; } } } \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 3a35d19..f1982c0 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Net; using System.Reflection; using System.Text; @@ -7,6 +8,7 @@ using CommandLine; using Discord; using Discord.Commands; using Discord.WebSocket; +using Geekbot.net.Database; using Geekbot.net.Lib; using Geekbot.net.Lib.Audio; using Geekbot.net.Lib.Clients; @@ -18,6 +20,7 @@ using Geekbot.net.Lib.Logger; using Geekbot.net.Lib.Media; using Geekbot.net.Lib.ReactionListener; using Geekbot.net.Lib.UserRepository; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Nancy.Hosting.Self; using StackExchange.Redis; @@ -104,6 +107,18 @@ namespace Geekbot.net _firstStart = true; } + DatabaseContext database = null; + try + { + database = new DatabaseContext(); + database.Database.EnsureCreated(); + } + catch (Exception e) + { + logger.Error(LogSource.Geekbot, "Could not Connect to datbase", e); + Environment.Exit(GeekbotExitCode.DatabaseConnectionFailed.GetHashCode()); + } + _services = new ServiceCollection(); _userRepository = new UserRepository(_redis, logger); @@ -127,6 +142,7 @@ namespace Geekbot.net _services.AddSingleton(mtgManaConverter); _services.AddSingleton(wikipediaClient); _services.AddSingleton(audioUtils); + _services.AddSingleton(database); logger.Information(LogSource.Geekbot, "Connecting to Discord"); From 177942f7fe08295213625b1beef2d35bf0107c74 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 9 May 2018 02:12:58 +0200 Subject: [PATCH 056/443] Save quotes to the database after adding them --- Geekbot.net/Commands/Utils/Quote/Quote.cs | 65 ++++++++++++----------- Geekbot.net/Database/QuoteModel.cs | 14 ++++- 2 files changed, 46 insertions(+), 33 deletions(-) diff --git a/Geekbot.net/Commands/Utils/Quote/Quote.cs b/Geekbot.net/Commands/Utils/Quote/Quote.cs index 0310182..8186f56 100644 --- a/Geekbot.net/Commands/Utils/Quote/Quote.cs +++ b/Geekbot.net/Commands/Utils/Quote/Quote.cs @@ -33,18 +33,18 @@ namespace Geekbot.net.Commands.Utils.Quote { try { - var s = _database.Quotes.Where(e => e.GuildId.Equals(Context.Guild.Id)); - var totalQuotes = s.Count(); + var s = _database.Quotes.OrderBy(e => e.GuildId).Where(e => e.GuildId.Equals(Context.Guild.Id)).ToList(); - if (totalQuotes > 0) + if (!s.Any()) { await ReplyAsync("This server doesn't seem to have any quotes yet. You can add a quote with `!quote save @user` or `!quote save `"); return; } - var quote = s.Where(e => e.Id.Equals(new Random().Next(totalQuotes)))?.FirstOrDefault(); + + var random = new Random().Next(1, s.Count()); + var quote = s[random - 1]; - - var embed = QuoteBuilder(quote, 2); + var embed = QuoteBuilder(quote); await ReplyAsync("", false, embed.Build()); } catch (Exception e) @@ -76,7 +76,8 @@ namespace Geekbot.net.Commands.Utils.Quote if (lastMessage == null) return; var quote = CreateQuoteObject(lastMessage); - await _database.Quotes.AddAsync(quote); + _database.Quotes.Add(quote); + _database.SaveChanges(); var embed = QuoteBuilder(quote); await ReplyAsync("**Quote Added**", false, embed.Build()); @@ -109,7 +110,8 @@ namespace Geekbot.net.Commands.Utils.Quote } var quote = CreateQuoteObject(message); - await _database.Quotes.AddAsync(quote); + _database.Quotes.Add(quote); + _database.SaveChanges(); var embed = QuoteBuilder(quote); await ReplyAsync("**Quote Added**", false, embed.Build()); @@ -168,27 +170,25 @@ namespace Geekbot.net.Commands.Utils.Quote [Summary("Remove a quote (required mod permissions)")] public async Task RemoveQuote([Summary("quoteId")] int id) { - await ReplyAsync("Command currently Disabled"); - // try - // { - // var quotes = _redis.SetMembers($"{Context.Guild.Id}:Quotes"); - // var success = _redis.SetRemove($"{Context.Guild.Id}:Quotes", quotes[id - 1]); - // if (success) - // { - // var quote = JsonConvert.DeserializeObject(quotes[id - 1]); - // var embed = QuoteBuilder(quote); - // await ReplyAsync($"**Removed #{id}**", false, embed.Build()); - // } - // else - // { - // await ReplyAsync("I couldn't find a quote with that id :disappointed:"); - // } - // } - // catch (Exception e) - // { - // _errorHandler.HandleCommandException(e, Context, - // "I couldn't find a quote with that id :disappointed:"); - // } + try + { + var quote = _database.Quotes.Where(e => e.GuildId == Context.Guild.Id && e.InternalId == id)?.FirstOrDefault(); + if (quote != null) + { + _database.Quotes.Remove(quote); + _database.SaveChanges(); + var embed = QuoteBuilder(quote); + await ReplyAsync($"**Removed #{id}**", false, embed.Build()); + } + else + { + await ReplyAsync("I couldn't find a quote with that id :disappointed:"); + } + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context, "I couldn't find a quote with that id :disappointed:"); + } } private async Task GetLastMessageByUser(IUser user) @@ -210,13 +210,12 @@ namespace Geekbot.net.Commands.Utils.Quote } } - private EmbedBuilder QuoteBuilder(QuoteModel quote, int id = 0) + private EmbedBuilder QuoteBuilder(QuoteModel quote) { var user = Context.Client.GetUserAsync(quote.UserId).Result ?? new UserPolyfillDto { Username = "Unknown User" }; var eb = new EmbedBuilder(); eb.WithColor(new Color(143, 167, 232)); - eb.Title = id == 0 ? "" : $"#{id} | "; - eb.Title += $"{user.Username} @ {quote.Time.Day}.{quote.Time.Month}.{quote.Time.Year}"; + eb.Title = $"#{quote.InternalId} | {user.Username} @ {quote.Time.Day}.{quote.Time.Month}.{quote.Time.Year}"; eb.Description = quote.Quote; eb.ThumbnailUrl = user.GetAvatarUrl(); if (quote.Image != null) eb.ImageUrl = quote.Image; @@ -235,8 +234,10 @@ namespace Geekbot.net.Commands.Utils.Quote image = null; } + var internalId = _database.Quotes.Count(e => e.GuildId == Context.Guild.Id); return new QuoteModel() { + InternalId = internalId, GuildId = Context.Guild.Id, UserId = message.Author.Id, Time = message.Timestamp.DateTime, diff --git a/Geekbot.net/Database/QuoteModel.cs b/Geekbot.net/Database/QuoteModel.cs index ea42e93..37d4a37 100644 --- a/Geekbot.net/Database/QuoteModel.cs +++ b/Geekbot.net/Database/QuoteModel.cs @@ -8,10 +8,22 @@ namespace Geekbot.net.Database { [Key] public int Id { get; set; } + + [Required] + public int InternalId { get; set; } + + [Required] public ulong GuildId { get; set; } + + [Required] public ulong UserId { get; set; } - public string Quote { get; set; } + + [Required] + [DataType(DataType.DateTime)] public DateTime Time { get; set; } + + public string Quote { get; set; } + public string Image { get; set; } } } \ No newline at end of file From 3425896c0b7e4ac27465b15fecff586f00cc47be Mon Sep 17 00:00:00 2001 From: Runebaas Date: Wed, 9 May 2018 18:51:53 +0200 Subject: [PATCH 057/443] Add database Initializer --- Geekbot.net/Database/DatabaseInitializer.cs | 43 +++++++++++++++++++++ Geekbot.net/Database/InMemoryDatabase.cs | 19 +++++++++ Geekbot.net/Database/SqlConnectionString.cs | 16 ++++++++ Geekbot.net/Database/SqlDatabase.cs | 19 +++++++++ Geekbot.net/Program.cs | 12 +----- 5 files changed, 98 insertions(+), 11 deletions(-) create mode 100644 Geekbot.net/Database/DatabaseInitializer.cs create mode 100644 Geekbot.net/Database/InMemoryDatabase.cs create mode 100644 Geekbot.net/Database/SqlConnectionString.cs create mode 100644 Geekbot.net/Database/SqlDatabase.cs diff --git a/Geekbot.net/Database/DatabaseInitializer.cs b/Geekbot.net/Database/DatabaseInitializer.cs new file mode 100644 index 0000000..838eb48 --- /dev/null +++ b/Geekbot.net/Database/DatabaseInitializer.cs @@ -0,0 +1,43 @@ +using System; +using Geekbot.net.Lib; +using Geekbot.net.Lib.Logger; + +namespace Geekbot.net.Database +{ + public class DatabaseInitializer + { + private readonly RunParameters _runParameters; + private readonly GeekbotLogger _logger; + + public DatabaseInitializer(RunParameters runParameters, GeekbotLogger logger) + { + _runParameters = runParameters; + _logger = logger; + } + + public DatabaseContext Initzialize() + { + DatabaseContext database = null; + try + { + if (_runParameters.InMemory) + { + database = new InMemoryDatabase("geekbot"); + } + else + { + database = new SqlDatabase(new SqlConnectionString()); + } + + database.Database.EnsureCreated(); + } + catch (Exception e) + { + _logger.Error(LogSource.Geekbot, "Could not Connect to datbase", e); + Environment.Exit(GeekbotExitCode.DatabaseConnectionFailed.GetHashCode()); + } + + return database; + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Database/InMemoryDatabase.cs b/Geekbot.net/Database/InMemoryDatabase.cs new file mode 100644 index 0000000..cfef816 --- /dev/null +++ b/Geekbot.net/Database/InMemoryDatabase.cs @@ -0,0 +1,19 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; + +namespace Geekbot.net.Database +{ + public class InMemoryDatabase : DatabaseContext + { + private readonly string _name; + + public InMemoryDatabase(string name) + { + _name = name; + } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + => optionsBuilder.UseLoggerFactory(new LoggerFactory().AddConsole()) + .UseInMemoryDatabase(databaseName: _name); + } +} \ No newline at end of file diff --git a/Geekbot.net/Database/SqlConnectionString.cs b/Geekbot.net/Database/SqlConnectionString.cs new file mode 100644 index 0000000..1dcfabb --- /dev/null +++ b/Geekbot.net/Database/SqlConnectionString.cs @@ -0,0 +1,16 @@ +namespace Geekbot.net.Database +{ + public class SqlConnectionString + { + public string Server { get; set; } = "localhost"; + public string Port { get; set; } = "3306"; + public string Database { get; set; } = "geekbot"; + public string Username { get; set; } = "geekbot"; + public string Password { get; set; } = ""; + + public override string ToString() + { + return $"Server={Server};Port={Port};Database={Database};Uid={Username};Pwd={Password};"; + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Database/SqlDatabase.cs b/Geekbot.net/Database/SqlDatabase.cs new file mode 100644 index 0000000..0d9cd8f --- /dev/null +++ b/Geekbot.net/Database/SqlDatabase.cs @@ -0,0 +1,19 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; + +namespace Geekbot.net.Database +{ + public class SqlDatabase : DatabaseContext + { + private readonly SqlConnectionString _connectionString; + + public SqlDatabase(SqlConnectionString connectionString) + { + _connectionString = connectionString; + } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + => optionsBuilder.UseLoggerFactory(new LoggerFactory().AddConsole()) + .UseMySql(_connectionString.ToString()); + } +} \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index f1982c0..4fa763d 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -107,17 +107,7 @@ namespace Geekbot.net _firstStart = true; } - DatabaseContext database = null; - try - { - database = new DatabaseContext(); - database.Database.EnsureCreated(); - } - catch (Exception e) - { - logger.Error(LogSource.Geekbot, "Could not Connect to datbase", e); - Environment.Exit(GeekbotExitCode.DatabaseConnectionFailed.GetHashCode()); - } + var database = new DatabaseInitializer(runParameters, logger).Initzialize(); _services = new ServiceCollection(); From d2f31d07308d3c6c84fb832fb7841f444f451de0 Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 10 May 2018 00:00:51 +0200 Subject: [PATCH 058/443] Use Postgresql, add db run params, npgsql logging adapter and empty models --- Geekbot.net/Commands/Utils/Quote/Quote.cs | 22 +++--- Geekbot.net/Database/DatabaseContext.cs | 16 ++-- Geekbot.net/Database/DatabaseInitializer.cs | 16 +++- Geekbot.net/Database/InMemoryDatabase.cs | 3 +- .../LoggingAdapter/NpgsqlLoggingAdapter.cs | 73 +++++++++++++++++++ .../NpgsqlLoggingProviderAdapter.cs | 20 +++++ .../Database/Models/GuildSettingsModel.cs | 7 ++ Geekbot.net/Database/Models/GuildsModel.cs | 7 ++ Geekbot.net/Database/Models/KarmaModel.cs | 7 ++ .../Database/{ => Models}/QuoteModel.cs | 7 +- .../Database/Models/RoleSelfServiceModel.cs | 7 ++ Geekbot.net/Database/Models/ShipsModel.cs | 7 ++ Geekbot.net/Database/Models/SlapsModel.cs | 7 ++ Geekbot.net/Database/Models/UserModel.cs | 7 ++ Geekbot.net/Database/SqlConnectionString.cs | 12 +-- Geekbot.net/Database/SqlDatabase.cs | 4 +- Geekbot.net/Geekbot.net.csproj | 6 +- Geekbot.net/Lib/Extensions/LongExtensions.cs | 12 +++ Geekbot.net/Lib/Extensions/UlongExtensions.cs | 12 +++ Geekbot.net/Lib/Logger/GeekbotLogger.cs | 10 +++ Geekbot.net/Lib/Logger/IGeekbotLogger.cs | 2 + Geekbot.net/Lib/Logger/LogSource.cs | 1 + Geekbot.net/Lib/Logger/LoggerFactory.cs | 2 +- Geekbot.net/Lib/RunParameters.cs | 24 +++++- 24 files changed, 252 insertions(+), 39 deletions(-) create mode 100644 Geekbot.net/Database/LoggingAdapter/NpgsqlLoggingAdapter.cs create mode 100644 Geekbot.net/Database/LoggingAdapter/NpgsqlLoggingProviderAdapter.cs create mode 100644 Geekbot.net/Database/Models/GuildSettingsModel.cs create mode 100644 Geekbot.net/Database/Models/GuildsModel.cs create mode 100644 Geekbot.net/Database/Models/KarmaModel.cs rename Geekbot.net/Database/{ => Models}/QuoteModel.cs (73%) create mode 100644 Geekbot.net/Database/Models/RoleSelfServiceModel.cs create mode 100644 Geekbot.net/Database/Models/ShipsModel.cs create mode 100644 Geekbot.net/Database/Models/SlapsModel.cs create mode 100644 Geekbot.net/Database/Models/UserModel.cs create mode 100644 Geekbot.net/Lib/Extensions/LongExtensions.cs create mode 100644 Geekbot.net/Lib/Extensions/UlongExtensions.cs diff --git a/Geekbot.net/Commands/Utils/Quote/Quote.cs b/Geekbot.net/Commands/Utils/Quote/Quote.cs index 8186f56..18eab22 100644 --- a/Geekbot.net/Commands/Utils/Quote/Quote.cs +++ b/Geekbot.net/Commands/Utils/Quote/Quote.cs @@ -4,10 +4,11 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Geekbot.net.Database; +using Geekbot.net.Database.Models; using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Polyfills; -using Newtonsoft.Json; using StackExchange.Redis; namespace Geekbot.net.Commands.Utils.Quote @@ -33,7 +34,7 @@ namespace Geekbot.net.Commands.Utils.Quote { try { - var s = _database.Quotes.OrderBy(e => e.GuildId).Where(e => e.GuildId.Equals(Context.Guild.Id)).ToList(); + var s = _database.Quotes.Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong())).ToList(); if (!s.Any()) { @@ -41,8 +42,8 @@ namespace Geekbot.net.Commands.Utils.Quote return; } - var random = new Random().Next(1, s.Count()); - var quote = s[random - 1]; + var random = new Random().Next(s.Count()); + var quote = s[random]; var embed = QuoteBuilder(quote); await ReplyAsync("", false, embed.Build()); @@ -172,7 +173,7 @@ namespace Geekbot.net.Commands.Utils.Quote { try { - var quote = _database.Quotes.Where(e => e.GuildId == Context.Guild.Id && e.InternalId == id)?.FirstOrDefault(); + var quote = _database.Quotes.Where(e => e.GuildId == Context.Guild.Id.AsLong() && e.InternalId == id)?.FirstOrDefault(); if (quote != null) { _database.Quotes.Remove(quote); @@ -212,7 +213,7 @@ namespace Geekbot.net.Commands.Utils.Quote private EmbedBuilder QuoteBuilder(QuoteModel quote) { - var user = Context.Client.GetUserAsync(quote.UserId).Result ?? new UserPolyfillDto { Username = "Unknown User" }; + var user = Context.Client.GetUserAsync(quote.UserId.AsUlong()).Result ?? new UserPolyfillDto { Username = "Unknown User" }; var eb = new EmbedBuilder(); eb.WithColor(new Color(143, 167, 232)); eb.Title = $"#{quote.InternalId} | {user.Username} @ {quote.Time.Day}.{quote.Time.Month}.{quote.Time.Year}"; @@ -234,12 +235,15 @@ namespace Geekbot.net.Commands.Utils.Quote image = null; } - var internalId = _database.Quotes.Count(e => e.GuildId == Context.Guild.Id); + var last = _database.Quotes.Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong())) + .OrderByDescending(e => e.InternalId).FirstOrDefault(); + int internalId = 1; + if (last != null) internalId = last.InternalId + 1; return new QuoteModel() { InternalId = internalId, - GuildId = Context.Guild.Id, - UserId = message.Author.Id, + GuildId = Context.Guild.Id.AsLong(), + UserId = message.Author.Id.AsLong(), Time = message.Timestamp.DateTime, Quote = message.Content, Image = image diff --git a/Geekbot.net/Database/DatabaseContext.cs b/Geekbot.net/Database/DatabaseContext.cs index d2645a1..4426c8d 100644 --- a/Geekbot.net/Database/DatabaseContext.cs +++ b/Geekbot.net/Database/DatabaseContext.cs @@ -1,15 +1,17 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Logging; +using Geekbot.net.Database.Models; +using Microsoft.EntityFrameworkCore; namespace Geekbot.net.Database { public class DatabaseContext : DbContext { public DbSet Quotes { get; set; } - - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - => optionsBuilder.UseLoggerFactory(new LoggerFactory().AddConsole()) -// .UseInMemoryDatabase(databaseName: "Geekbot"); - .UseMySql(@"Server=localhost;database=geekbot;uid=geekbot;"); +// public DbSet Users { get; set; } +// public DbSet Guilds { get; set; } +// public DbSet GuildSettings { get; set; } +// public DbSet Karma { get; set; } +// public DbSet Ships { get; set; } +// public DbSet RoleSelfService { get; set; } +// public DbSet SlapsModels { get; set; } } } \ No newline at end of file diff --git a/Geekbot.net/Database/DatabaseInitializer.cs b/Geekbot.net/Database/DatabaseInitializer.cs index 838eb48..f4bb8f1 100644 --- a/Geekbot.net/Database/DatabaseInitializer.cs +++ b/Geekbot.net/Database/DatabaseInitializer.cs @@ -1,6 +1,8 @@ using System; +using Geekbot.net.Database.LoggingAdapter; using Geekbot.net.Lib; using Geekbot.net.Lib.Logger; +using Npgsql.Logging; namespace Geekbot.net.Database { @@ -26,9 +28,16 @@ namespace Geekbot.net.Database } else { - database = new SqlDatabase(new SqlConnectionString()); + NpgsqlLogManager.Provider = new NpgsqlLoggingProviderAdapter(_logger); + database = new SqlDatabase(new SqlConnectionString + { + Host = _runParameters.DbHost, + Port = _runParameters.DbPort, + Database = _runParameters.DbDatabase, + Username = _runParameters.DbUser, + Password = _runParameters.DbPassword + }); } - database.Database.EnsureCreated(); } catch (Exception e) @@ -36,7 +45,8 @@ namespace Geekbot.net.Database _logger.Error(LogSource.Geekbot, "Could not Connect to datbase", e); Environment.Exit(GeekbotExitCode.DatabaseConnectionFailed.GetHashCode()); } - + + _logger.Information(LogSource.Database, $"Connected with {database.Database.ProviderName}"); return database; } } diff --git a/Geekbot.net/Database/InMemoryDatabase.cs b/Geekbot.net/Database/InMemoryDatabase.cs index cfef816..178c0e3 100644 --- a/Geekbot.net/Database/InMemoryDatabase.cs +++ b/Geekbot.net/Database/InMemoryDatabase.cs @@ -13,7 +13,6 @@ namespace Geekbot.net.Database } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - => optionsBuilder.UseLoggerFactory(new LoggerFactory().AddConsole()) - .UseInMemoryDatabase(databaseName: _name); + => optionsBuilder.UseInMemoryDatabase(databaseName: _name); } } \ No newline at end of file diff --git a/Geekbot.net/Database/LoggingAdapter/NpgsqlLoggingAdapter.cs b/Geekbot.net/Database/LoggingAdapter/NpgsqlLoggingAdapter.cs new file mode 100644 index 0000000..80d60b3 --- /dev/null +++ b/Geekbot.net/Database/LoggingAdapter/NpgsqlLoggingAdapter.cs @@ -0,0 +1,73 @@ +using System; +using Geekbot.net.Lib.Logger; +using Npgsql.Logging; +using LogLevel = NLog.LogLevel; + +namespace Geekbot.net.Database.LoggingAdapter +{ + public class NpgsqlLoggingAdapter : NpgsqlLogger + { + private readonly string _name; + private readonly IGeekbotLogger _geekbotLogger; + + public NpgsqlLoggingAdapter(string name, IGeekbotLogger geekbotLogger) + { + _name = name.Substring(7); + _geekbotLogger = geekbotLogger; + geekbotLogger.Trace(LogSource.Database, $"Loaded Npgsql logging adapter: {name}"); + } + + public override bool IsEnabled(NpgsqlLogLevel level) + { + return !_geekbotLogger.LogAsJson() && _geekbotLogger.GetNLogger().IsEnabled(ToGeekbotLogLevel(level)); + } + + public override void Log(NpgsqlLogLevel level, int connectorId, string msg, Exception exception = null) + { + var nameAndMessage = $"{_name}: {msg}"; + switch (level) + { + case NpgsqlLogLevel.Trace: + _geekbotLogger.Trace(LogSource.Database, nameAndMessage); + break; + case NpgsqlLogLevel.Debug: + _geekbotLogger.Debug(LogSource.Database, nameAndMessage); + break; + case NpgsqlLogLevel.Info: + _geekbotLogger.Information(LogSource.Database, nameAndMessage); + break; + case NpgsqlLogLevel.Warn: + _geekbotLogger.Warning(LogSource.Database, nameAndMessage, exception); + break; + case NpgsqlLogLevel.Error: + case NpgsqlLogLevel.Fatal: + _geekbotLogger.Error(LogSource.Database, nameAndMessage, exception); + break; + default: + _geekbotLogger.Information(LogSource.Database, nameAndMessage); + break; + } + } + + private static LogLevel ToGeekbotLogLevel(NpgsqlLogLevel level) + { + switch (level) + { + case NpgsqlLogLevel.Trace: + return LogLevel.Trace; + case NpgsqlLogLevel.Debug: + return LogLevel.Debug; + case NpgsqlLogLevel.Info: + return LogLevel.Info; + case NpgsqlLogLevel.Warn: + return LogLevel.Warn; + case NpgsqlLogLevel.Error: + return LogLevel.Error; + case NpgsqlLogLevel.Fatal: + return LogLevel.Fatal; + default: + throw new ArgumentOutOfRangeException(nameof(level)); + } + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Database/LoggingAdapter/NpgsqlLoggingProviderAdapter.cs b/Geekbot.net/Database/LoggingAdapter/NpgsqlLoggingProviderAdapter.cs new file mode 100644 index 0000000..0888111 --- /dev/null +++ b/Geekbot.net/Database/LoggingAdapter/NpgsqlLoggingProviderAdapter.cs @@ -0,0 +1,20 @@ +using Geekbot.net.Lib.Logger; +using Npgsql.Logging; + +namespace Geekbot.net.Database.LoggingAdapter +{ + public class NpgsqlLoggingProviderAdapter : INpgsqlLoggingProvider + { + private readonly GeekbotLogger _geekbotLogger; + + public NpgsqlLoggingProviderAdapter(GeekbotLogger geekbotLogger) + { + _geekbotLogger = geekbotLogger; + } + + public NpgsqlLogger CreateLogger(string name) + { + return new NpgsqlLoggingAdapter(name, _geekbotLogger); + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Database/Models/GuildSettingsModel.cs b/Geekbot.net/Database/Models/GuildSettingsModel.cs new file mode 100644 index 0000000..b8ae300 --- /dev/null +++ b/Geekbot.net/Database/Models/GuildSettingsModel.cs @@ -0,0 +1,7 @@ +namespace Geekbot.net.Database.Models +{ + public class GuildSettingsModel + { + + } +} \ No newline at end of file diff --git a/Geekbot.net/Database/Models/GuildsModel.cs b/Geekbot.net/Database/Models/GuildsModel.cs new file mode 100644 index 0000000..0b8ec3c --- /dev/null +++ b/Geekbot.net/Database/Models/GuildsModel.cs @@ -0,0 +1,7 @@ +namespace Geekbot.net.Database.Models +{ + public class GuildsModel + { + + } +} \ No newline at end of file diff --git a/Geekbot.net/Database/Models/KarmaModel.cs b/Geekbot.net/Database/Models/KarmaModel.cs new file mode 100644 index 0000000..d29f313 --- /dev/null +++ b/Geekbot.net/Database/Models/KarmaModel.cs @@ -0,0 +1,7 @@ +namespace Geekbot.net.Database.Models +{ + public class KarmaModel + { + + } +} \ No newline at end of file diff --git a/Geekbot.net/Database/QuoteModel.cs b/Geekbot.net/Database/Models/QuoteModel.cs similarity index 73% rename from Geekbot.net/Database/QuoteModel.cs rename to Geekbot.net/Database/Models/QuoteModel.cs index 37d4a37..98e8b21 100644 --- a/Geekbot.net/Database/QuoteModel.cs +++ b/Geekbot.net/Database/Models/QuoteModel.cs @@ -1,8 +1,7 @@ using System; using System.ComponentModel.DataAnnotations; -using Microsoft.EntityFrameworkCore.Scaffolding.Metadata; -namespace Geekbot.net.Database +namespace Geekbot.net.Database.Models { public class QuoteModel { @@ -13,10 +12,10 @@ namespace Geekbot.net.Database public int InternalId { get; set; } [Required] - public ulong GuildId { get; set; } + public long GuildId { get; set; } [Required] - public ulong UserId { get; set; } + public long UserId { get; set; } [Required] [DataType(DataType.DateTime)] diff --git a/Geekbot.net/Database/Models/RoleSelfServiceModel.cs b/Geekbot.net/Database/Models/RoleSelfServiceModel.cs new file mode 100644 index 0000000..4bb7f01 --- /dev/null +++ b/Geekbot.net/Database/Models/RoleSelfServiceModel.cs @@ -0,0 +1,7 @@ +namespace Geekbot.net.Database.Models +{ + public class RoleSelfServiceModel + { + + } +} \ No newline at end of file diff --git a/Geekbot.net/Database/Models/ShipsModel.cs b/Geekbot.net/Database/Models/ShipsModel.cs new file mode 100644 index 0000000..6eda191 --- /dev/null +++ b/Geekbot.net/Database/Models/ShipsModel.cs @@ -0,0 +1,7 @@ +namespace Geekbot.net.Database.Models +{ + public class ShipsModel + { + + } +} \ No newline at end of file diff --git a/Geekbot.net/Database/Models/SlapsModel.cs b/Geekbot.net/Database/Models/SlapsModel.cs new file mode 100644 index 0000000..80ee199 --- /dev/null +++ b/Geekbot.net/Database/Models/SlapsModel.cs @@ -0,0 +1,7 @@ +namespace Geekbot.net.Database.Models +{ + public class SlapsModel + { + + } +} \ No newline at end of file diff --git a/Geekbot.net/Database/Models/UserModel.cs b/Geekbot.net/Database/Models/UserModel.cs new file mode 100644 index 0000000..d68f178 --- /dev/null +++ b/Geekbot.net/Database/Models/UserModel.cs @@ -0,0 +1,7 @@ +namespace Geekbot.net.Database.Models +{ + public class UserModel + { + + } +} \ No newline at end of file diff --git a/Geekbot.net/Database/SqlConnectionString.cs b/Geekbot.net/Database/SqlConnectionString.cs index 1dcfabb..3228a05 100644 --- a/Geekbot.net/Database/SqlConnectionString.cs +++ b/Geekbot.net/Database/SqlConnectionString.cs @@ -2,15 +2,15 @@ { public class SqlConnectionString { - public string Server { get; set; } = "localhost"; - public string Port { get; set; } = "3306"; - public string Database { get; set; } = "geekbot"; - public string Username { get; set; } = "geekbot"; - public string Password { get; set; } = ""; + public string Host { get; set; } + public string Port { get; set; } + public string Database { get; set; } + public string Username { get; set; } + public string Password { get; set; } public override string ToString() { - return $"Server={Server};Port={Port};Database={Database};Uid={Username};Pwd={Password};"; + return $"Server={Host};Port={Port};Database={Database};Uid={Username};Pwd={Password};"; } } } \ No newline at end of file diff --git a/Geekbot.net/Database/SqlDatabase.cs b/Geekbot.net/Database/SqlDatabase.cs index 0d9cd8f..e6d03d4 100644 --- a/Geekbot.net/Database/SqlDatabase.cs +++ b/Geekbot.net/Database/SqlDatabase.cs @@ -1,5 +1,4 @@ using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Logging; namespace Geekbot.net.Database { @@ -13,7 +12,6 @@ namespace Geekbot.net.Database } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - => optionsBuilder.UseLoggerFactory(new LoggerFactory().AddConsole()) - .UseMySql(_connectionString.ToString()); + => optionsBuilder.UseNpgsql(_connectionString.ToString()); } } \ No newline at end of file diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index f8554c9..8e01709 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -32,15 +32,15 @@ + + - - 1.2.6 - + diff --git a/Geekbot.net/Lib/Extensions/LongExtensions.cs b/Geekbot.net/Lib/Extensions/LongExtensions.cs new file mode 100644 index 0000000..1bcdd9b --- /dev/null +++ b/Geekbot.net/Lib/Extensions/LongExtensions.cs @@ -0,0 +1,12 @@ +using System; + +namespace Geekbot.net.Lib.Extensions +{ + public static class LongExtensions + { + public static ulong AsUlong(this long thing) + { + return Convert.ToUInt64(thing); + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/Extensions/UlongExtensions.cs b/Geekbot.net/Lib/Extensions/UlongExtensions.cs new file mode 100644 index 0000000..8fa2a67 --- /dev/null +++ b/Geekbot.net/Lib/Extensions/UlongExtensions.cs @@ -0,0 +1,12 @@ +using System; + +namespace Geekbot.net.Lib.Extensions +{ + public static class UlongExtensions + { + public static long AsLong(this ulong thing) + { + return Convert.ToInt64(thing); + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/Logger/GeekbotLogger.cs b/Geekbot.net/Lib/Logger/GeekbotLogger.cs index b2bb677..09a3ecb 100644 --- a/Geekbot.net/Lib/Logger/GeekbotLogger.cs +++ b/Geekbot.net/Lib/Logger/GeekbotLogger.cs @@ -49,6 +49,16 @@ namespace Geekbot.net.Lib.Logger else _logger.Error(stackTrace, CreateLogString("Error", source, message, stackTrace, extra)); } + public NLog.Logger GetNLogger() + { + return _logger; + } + + public bool LogAsJson() + { + return _logAsJson; + } + private string CreateLogString(string type, LogSource source, string message, Exception stackTrace = null, object extra = null) { if (_logAsJson) diff --git a/Geekbot.net/Lib/Logger/IGeekbotLogger.cs b/Geekbot.net/Lib/Logger/IGeekbotLogger.cs index 1f76cb0..944524a 100644 --- a/Geekbot.net/Lib/Logger/IGeekbotLogger.cs +++ b/Geekbot.net/Lib/Logger/IGeekbotLogger.cs @@ -9,5 +9,7 @@ namespace Geekbot.net.Lib.Logger void Information(LogSource source, string message, object extra = null); void Warning(LogSource source, string message, Exception stackTrace = null, object extra = null); void Error(LogSource source, string message, Exception stackTrace, object extra = null); + NLog.Logger GetNLogger(); + bool LogAsJson(); } } \ No newline at end of file diff --git a/Geekbot.net/Lib/Logger/LogSource.cs b/Geekbot.net/Lib/Logger/LogSource.cs index d9a8629..cccc930 100644 --- a/Geekbot.net/Lib/Logger/LogSource.cs +++ b/Geekbot.net/Lib/Logger/LogSource.cs @@ -11,6 +11,7 @@ namespace Geekbot.net.Lib.Logger Gateway, Discord, Redis, + Database, Message, UserRepository, Command, diff --git a/Geekbot.net/Lib/Logger/LoggerFactory.cs b/Geekbot.net/Lib/Logger/LoggerFactory.cs index 4095d00..9c5f759 100644 --- a/Geekbot.net/Lib/Logger/LoggerFactory.cs +++ b/Geekbot.net/Lib/Logger/LoggerFactory.cs @@ -34,7 +34,7 @@ namespace Geekbot.net.Lib.Logger { var minLevel = runParameters.Verbose ? LogLevel.Trace : LogLevel.Info; config.LoggingRules.Add( - new LoggingRule("*", LogLevel.Info, LogLevel.Fatal, + new LoggingRule("*", minLevel, LogLevel.Fatal, new ColoredConsoleTarget { Name = "Console", diff --git a/Geekbot.net/Lib/RunParameters.cs b/Geekbot.net/Lib/RunParameters.cs index 1e1048b..5485533 100644 --- a/Geekbot.net/Lib/RunParameters.cs +++ b/Geekbot.net/Lib/RunParameters.cs @@ -5,6 +5,9 @@ namespace Geekbot.net.Lib { public class RunParameters { + /** + * General Parameters + */ [Option('V', "verbose", Default = false, HelpText = "Prints all messages to standard output.")] public bool Verbose { get; set; } @@ -23,7 +26,26 @@ namespace Geekbot.net.Lib [Option("token", Default = null, HelpText = "Set a new bot token")] public string Token { get; set; } - [Option("in-memory", Default = false, HelpText = "Disables the web api")] + /** + * Database Stuff + */ + [Option("in-memory", Default = false, HelpText = "Uses the in-memory database instead of postgresql")] public bool InMemory { get; set; } + + // Postresql connection + [Option("database", Default = false, HelpText = "Select a postgresql database")] + public string DbDatabase { get; set; } = "geekbot"; + + [Option("db-host", Default = false, HelpText = "Set a postgresql host (e.g. 127.0.0.1)")] + public string DbHost { get; set; } = "localhost"; + + [Option("db-port", Default = false, HelpText = "Set a postgresql host (e.g. 5432)")] + public string DbPort { get; set; } = "5432"; + + [Option("db-user", Default = false, HelpText = "Set a postgresql user")] + public string DbUser { get; set; } = "geekbot"; + + [Option("db-password", Default = false, HelpText = "Set a posgresql password")] + public string DbPassword { get; set; } = ""; } } \ No newline at end of file From 15034d63a33dbf00daedf50a2ae2f8fbed9ed27f Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 10 May 2018 02:00:26 +0200 Subject: [PATCH 059/443] Port UserRepository and remove OW stuff --- Geekbot.net/Commands/Admin/Owner.cs | 5 +- Geekbot.net/Commands/Games/BattleTag.cs | 72 ---------- Geekbot.net/Commands/Games/Overwatch.cs | 131 ------------------ Geekbot.net/Commands/User/Ranking/Rank.cs | 2 +- Geekbot.net/Commands/Utils/Quote/Quote.cs | 4 +- Geekbot.net/Database/DatabaseContext.cs | 13 +- Geekbot.net/Database/DatabaseInitializer.cs | 2 + .../Database/Models/GuildSettingsModel.cs | 24 +++- Geekbot.net/Database/Models/GuildsModel.cs | 17 ++- Geekbot.net/Database/Models/KarmaModel.cs | 16 ++- Geekbot.net/Database/Models/ShipsModel.cs | 11 +- Geekbot.net/Database/Models/SlapsModel.cs | 15 +- Geekbot.net/Database/Models/UserModel.cs | 25 +++- .../Database/Models/UserSettingsModel.cs | 15 ++ Geekbot.net/Geekbot.net.csproj | 1 - Geekbot.net/Handlers.cs | 12 +- .../Lib/UserRepository/IUserRepository.cs | 5 +- .../Lib/UserRepository/UserRepository.cs | 116 +++++----------- .../UserRepository/UserRepositoryUserDto.cs | 16 --- Geekbot.net/Program.cs | 2 +- 20 files changed, 176 insertions(+), 328 deletions(-) delete mode 100644 Geekbot.net/Commands/Games/BattleTag.cs delete mode 100644 Geekbot.net/Commands/Games/Overwatch.cs create mode 100644 Geekbot.net/Database/Models/UserSettingsModel.cs delete mode 100644 Geekbot.net/Lib/UserRepository/UserRepositoryUserDto.cs diff --git a/Geekbot.net/Commands/Admin/Owner.cs b/Geekbot.net/Commands/Admin/Owner.cs index 2d097e0..10cbfd7 100644 --- a/Geekbot.net/Commands/Admin/Owner.cs +++ b/Geekbot.net/Commands/Admin/Owner.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Discord.WebSocket; +using Geekbot.net.Database; using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Logger; @@ -17,17 +18,19 @@ namespace Geekbot.net.Commands.Admin { private readonly DiscordSocketClient _client; private readonly IErrorHandler _errorHandler; + private readonly DatabaseContext _database; private readonly IGeekbotLogger _logger; private readonly IDatabase _redis; private readonly IUserRepository _userRepository; - public Owner(IDatabase redis, DiscordSocketClient client, IGeekbotLogger logger, IUserRepository userRepositry, IErrorHandler errorHandler) + public Owner(IDatabase redis, DiscordSocketClient client, IGeekbotLogger logger, IUserRepository userRepositry, IErrorHandler errorHandler, DatabaseContext database) { _redis = redis; _client = client; _logger = logger; _userRepository = userRepositry; _errorHandler = errorHandler; + _database = database; } [Command("youtubekey", RunMode = RunMode.Async)] diff --git a/Geekbot.net/Commands/Games/BattleTag.cs b/Geekbot.net/Commands/Games/BattleTag.cs deleted file mode 100644 index 88cc0d8..0000000 --- a/Geekbot.net/Commands/Games/BattleTag.cs +++ /dev/null @@ -1,72 +0,0 @@ -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; - } - } -} \ No newline at end of file diff --git a/Geekbot.net/Commands/Games/Overwatch.cs b/Geekbot.net/Commands/Games/Overwatch.cs deleted file mode 100644 index 73e5858..0000000 --- a/Geekbot.net/Commands/Games/Overwatch.cs +++ /dev/null @@ -1,131 +0,0 @@ -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 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; - } - } - } -} \ No newline at end of file diff --git a/Geekbot.net/Commands/User/Ranking/Rank.cs b/Geekbot.net/Commands/User/Ranking/Rank.cs index 08d4d3b..e0fbfa0 100644 --- a/Geekbot.net/Commands/User/Ranking/Rank.cs +++ b/Geekbot.net/Commands/User/Ranking/Rank.cs @@ -81,7 +81,7 @@ namespace Geekbot.net.Commands.User.Ranking try { var guildUser = _userRepository.Get((ulong) user.Name); - if (guildUser.Username != null) + if (guildUser?.Username != null) { highscoreUsers.Add(new RankUserPolyfillDto { diff --git a/Geekbot.net/Commands/Utils/Quote/Quote.cs b/Geekbot.net/Commands/Utils/Quote/Quote.cs index 18eab22..71c4089 100644 --- a/Geekbot.net/Commands/Utils/Quote/Quote.cs +++ b/Geekbot.net/Commands/Utils/Quote/Quote.cs @@ -18,11 +18,9 @@ namespace Geekbot.net.Commands.Utils.Quote { private readonly IErrorHandler _errorHandler; private readonly DatabaseContext _database; - private readonly IDatabase _redis; - public Quote(IDatabase redis, IErrorHandler errorHandler, DatabaseContext database) + public Quote(IErrorHandler errorHandler, DatabaseContext database) { - _redis = redis; _errorHandler = errorHandler; _database = database; } diff --git a/Geekbot.net/Database/DatabaseContext.cs b/Geekbot.net/Database/DatabaseContext.cs index 4426c8d..028a4f5 100644 --- a/Geekbot.net/Database/DatabaseContext.cs +++ b/Geekbot.net/Database/DatabaseContext.cs @@ -6,12 +6,13 @@ namespace Geekbot.net.Database public class DatabaseContext : DbContext { public DbSet Quotes { get; set; } -// public DbSet Users { get; set; } -// public DbSet Guilds { get; set; } -// public DbSet GuildSettings { get; set; } -// public DbSet Karma { get; set; } -// public DbSet Ships { get; set; } + public DbSet Users { get; set; } + public DbSet Guilds { get; set; } + public DbSet GuildSettings { get; set; } + public DbSet Karma { get; set; } + public DbSet Ships { get; set; } + public DbSet SlapsModels { get; set; } +// public DbSet UserSettings { get; set; } // public DbSet RoleSelfService { get; set; } -// public DbSet SlapsModels { get; set; } } } \ No newline at end of file diff --git a/Geekbot.net/Database/DatabaseInitializer.cs b/Geekbot.net/Database/DatabaseInitializer.cs index f4bb8f1..83660bb 100644 --- a/Geekbot.net/Database/DatabaseInitializer.cs +++ b/Geekbot.net/Database/DatabaseInitializer.cs @@ -2,6 +2,7 @@ using Geekbot.net.Database.LoggingAdapter; using Geekbot.net.Lib; using Geekbot.net.Lib.Logger; +using Microsoft.EntityFrameworkCore; using Npgsql.Logging; namespace Geekbot.net.Database @@ -39,6 +40,7 @@ namespace Geekbot.net.Database }); } database.Database.EnsureCreated(); + database.Database.Migrate(); } catch (Exception e) { diff --git a/Geekbot.net/Database/Models/GuildSettingsModel.cs b/Geekbot.net/Database/Models/GuildSettingsModel.cs index b8ae300..a71aac1 100644 --- a/Geekbot.net/Database/Models/GuildSettingsModel.cs +++ b/Geekbot.net/Database/Models/GuildSettingsModel.cs @@ -1,7 +1,29 @@ -namespace Geekbot.net.Database.Models +using System.ComponentModel.DataAnnotations; + +namespace Geekbot.net.Database.Models { public class GuildSettingsModel { + [Key] + public int Id { get; set; } + [Required] + public long GuildId { get; set; } + + public bool Ping { get; set; } + + public bool Hui { get; set; } + + public long ModChannel { get; set; } + + public string WelcomeMessage { get; set; } + + public bool ShowDelete { get; set; } + + public bool ShowLeave { get; set; } + + public string WikiLang { get; set; } + + public string Language { get; set; } } } \ No newline at end of file diff --git a/Geekbot.net/Database/Models/GuildsModel.cs b/Geekbot.net/Database/Models/GuildsModel.cs index 0b8ec3c..515231a 100644 --- a/Geekbot.net/Database/Models/GuildsModel.cs +++ b/Geekbot.net/Database/Models/GuildsModel.cs @@ -1,7 +1,22 @@ -namespace Geekbot.net.Database.Models +using System.ComponentModel.DataAnnotations; + +namespace Geekbot.net.Database.Models { public class GuildsModel { + [Key] + public int Id { get; set; } + [Required] + public long GuildId { get; set; } + + [Required] + public string Name { get; set; } + + [Required] + public string Owner { get; set; } + + [Required] + public string IconUrl { get; set; } } } \ No newline at end of file diff --git a/Geekbot.net/Database/Models/KarmaModel.cs b/Geekbot.net/Database/Models/KarmaModel.cs index d29f313..7d5f533 100644 --- a/Geekbot.net/Database/Models/KarmaModel.cs +++ b/Geekbot.net/Database/Models/KarmaModel.cs @@ -1,7 +1,21 @@ -namespace Geekbot.net.Database.Models +using System; +using System.ComponentModel.DataAnnotations; + +namespace Geekbot.net.Database.Models { public class KarmaModel { + [Key] + public int Id { get; set; } + [Required] + public long GuildId { get; set; } + + [Required] + public long UserId { get; set; } + + public int Karma { get; set; } + + public DateTimeOffset TimeOut { get; set; } } } \ No newline at end of file diff --git a/Geekbot.net/Database/Models/ShipsModel.cs b/Geekbot.net/Database/Models/ShipsModel.cs index 6eda191..d8156b0 100644 --- a/Geekbot.net/Database/Models/ShipsModel.cs +++ b/Geekbot.net/Database/Models/ShipsModel.cs @@ -1,7 +1,16 @@ -namespace Geekbot.net.Database.Models +using System.ComponentModel.DataAnnotations; + +namespace Geekbot.net.Database.Models { public class ShipsModel { + [Key] + public int Id { get; set; } + public long FirstUserId { get; set; } + + public long SecondUserId { get; set; } + + public int Strength { get; set; } } } \ No newline at end of file diff --git a/Geekbot.net/Database/Models/SlapsModel.cs b/Geekbot.net/Database/Models/SlapsModel.cs index 80ee199..6c402aa 100644 --- a/Geekbot.net/Database/Models/SlapsModel.cs +++ b/Geekbot.net/Database/Models/SlapsModel.cs @@ -1,7 +1,20 @@ -namespace Geekbot.net.Database.Models +using System.ComponentModel.DataAnnotations; + +namespace Geekbot.net.Database.Models { public class SlapsModel { + [Key] + public int Id { get; set; } + [Required] + public long GuildId { get; set; } + + [Required] + public long UserId { get; set; } + + public int Given { get; set; } + + public int Recieved { get; set; } } } \ No newline at end of file diff --git a/Geekbot.net/Database/Models/UserModel.cs b/Geekbot.net/Database/Models/UserModel.cs index d68f178..0083ada 100644 --- a/Geekbot.net/Database/Models/UserModel.cs +++ b/Geekbot.net/Database/Models/UserModel.cs @@ -1,7 +1,30 @@ -namespace Geekbot.net.Database.Models +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; + +namespace Geekbot.net.Database.Models { public class UserModel { + [Key] + public int Id { get; set; } + [Required] + public long UserId { get; set; } + + [Required] + public string Username { get; set; } + + [Required] + public string Discriminator { get; set; } + + public string AvatarUrl { get; set; } + + [Required] + public bool IsBot { get; set; } + + public DateTimeOffset Joined { get; set; } + + public string[] UsedNames { get; set; } } } \ No newline at end of file diff --git a/Geekbot.net/Database/Models/UserSettingsModel.cs b/Geekbot.net/Database/Models/UserSettingsModel.cs new file mode 100644 index 0000000..659ef1d --- /dev/null +++ b/Geekbot.net/Database/Models/UserSettingsModel.cs @@ -0,0 +1,15 @@ +using System.ComponentModel.DataAnnotations; + +namespace Geekbot.net.Database.Models +{ + public class UserSettingsModel + { + [Key] + public int Id { get; set; } + + [Required] + public long UserId { get; set; } + + // stuff to be added in the future + } +} \ No newline at end of file diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 8e01709..9141772 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -34,7 +34,6 @@ - diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index f248d5d..1515920 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -103,7 +103,7 @@ namespace Geekbot.net // User Stuff // - public Task UserJoined(SocketGuildUser user) + public async Task UserJoined(SocketGuildUser user) { try { @@ -113,23 +113,21 @@ namespace Geekbot.net if (!message.IsNullOrEmpty) { message = message.ToString().Replace("$user", user.Mention); - user.Guild.DefaultChannel.SendMessageAsync(message); + await user.Guild.DefaultChannel.SendMessageAsync(message); } } - _userRepository.Update(user); + await _userRepository.Update(user); _logger.Information(LogSource.Geekbot, $"{user.Username} ({user.Id}) joined {user.Guild.Name} ({user.Guild.Id})"); } catch (Exception e) { _logger.Error(LogSource.Geekbot, "Failed to send welcome message", e); } - return Task.CompletedTask; } - public Task UserUpdated(SocketUser oldUser, SocketUser newUser) + public async Task UserUpdated(SocketUser oldUser, SocketUser newUser) { - _userRepository.Update(newUser); - return Task.CompletedTask; + await _userRepository.Update(newUser); } public async Task UserLeft(SocketGuildUser user) diff --git a/Geekbot.net/Lib/UserRepository/IUserRepository.cs b/Geekbot.net/Lib/UserRepository/IUserRepository.cs index 9466621..b2f9a1f 100644 --- a/Geekbot.net/Lib/UserRepository/IUserRepository.cs +++ b/Geekbot.net/Lib/UserRepository/IUserRepository.cs @@ -1,13 +1,12 @@ using System.Threading.Tasks; using Discord.WebSocket; +using Geekbot.net.Database.Models; namespace Geekbot.net.Lib.UserRepository { public interface IUserRepository { Task Update(SocketUser user); - UserRepositoryUser Get(ulong userId); - string GetUserSetting(ulong userId, string setting); - bool SaveUserSetting(ulong userId, string setting, string value); + UserModel Get(ulong userId); } } \ No newline at end of file diff --git a/Geekbot.net/Lib/UserRepository/UserRepository.cs b/Geekbot.net/Lib/UserRepository/UserRepository.cs index 3937107..0125ee5 100644 --- a/Geekbot.net/Lib/UserRepository/UserRepository.cs +++ b/Geekbot.net/Lib/UserRepository/UserRepository.cs @@ -1,125 +1,81 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using Discord.WebSocket; +using Geekbot.net.Database; +using Geekbot.net.Database.Models; +using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Logger; -using StackExchange.Redis; -using Utf8Json; namespace Geekbot.net.Lib.UserRepository { public class UserRepository : IUserRepository { - private readonly IDatabase _redis; + private readonly DatabaseContext _database; private readonly IGeekbotLogger _logger; - public UserRepository(IDatabase redis, IGeekbotLogger logger) + public UserRepository(DatabaseContext database, IGeekbotLogger logger) { - _redis = redis; + _database = database; _logger = logger; } - public Task Update(SocketUser user) + public async Task Update(SocketUser user) { try { var savedUser = Get(user.Id); - savedUser.Id = user.Id; + var isNew = false; + if (savedUser == null) + { + savedUser = new UserModel(); + isNew = true; + } + savedUser.UserId = user.Id.AsLong(); savedUser.Username = user.Username; savedUser.Discriminator = user.Discriminator; - savedUser.AvatarUrl = user.GetAvatarUrl() ?? "0"; + savedUser.AvatarUrl = user.GetAvatarUrl() ?? ""; savedUser.IsBot = user.IsBot; savedUser.Joined = user.CreatedAt; - if(savedUser.UsedNames == null) savedUser.UsedNames = new List(); + if (savedUser.UsedNames == null) savedUser.UsedNames = Enumerable.Empty().ToArray(); if (!savedUser.UsedNames.Contains(user.Username)) { - savedUser.UsedNames.Add(user.Username); + savedUser.UsedNames = savedUser.UsedNames.Concat(new[] {user.Username}).ToArray(); } - Store(savedUser); - + + if (isNew) + { + await _database.Users.AddAsync(savedUser); + } + else + { + _database.Users.Update(savedUser); + } + + await _database.SaveChangesAsync(); + _logger.Information(LogSource.UserRepository, "Updated User", savedUser); - return Task.FromResult(true); + await Task.Delay(500); + return true; } catch (Exception e) { _logger.Warning(LogSource.UserRepository, $"Failed to update user: {user.Username}#{user.Discriminator} ({user.Id})", e); - return Task.FromResult(false); + return false; } } - private void Store(UserRepositoryUser user) - { - _redis.HashSetAsync($"Users:{user.Id.ToString()}", new[] - { - new HashEntry("Id", user.Id.ToString()), - new HashEntry("Username", user.Username), - new HashEntry("Discriminator", user.Discriminator), - new HashEntry("AvatarUrl", user.AvatarUrl), - new HashEntry("IsBot", user.IsBot), - new HashEntry("Joined", user.Joined.ToString()), - new HashEntry("UsedNames", JsonSerializer.Serialize(user.UsedNames)) - }); - } - - public UserRepositoryUser Get(ulong userId) + public UserModel Get(ulong userId) { try { - var user = _redis.HashGetAll($"Users:{userId.ToString()}"); - for (var i = 1; i < 11; i++) - { - if (user.Length != 0) break; - user = _redis.HashGetAll($"Users:{(userId + (ulong) i).ToString()}"); - - } - var dto = new UserRepositoryUser(); - foreach (var a in user.ToDictionary()) - { - switch (a.Key) - { - case "Id": - dto.Id = ulong.Parse(a.Value); - break; - case "Username": - dto.Username = a.Value.ToString(); - break; - case "Discriminator": - dto.Discriminator = a.Value.ToString(); - break; - case "AvatarUrl": - dto.AvatarUrl = a.Value != "0" ? a.Value.ToString() : null; - break; - case "IsBot": - dto.IsBot = a.Value == 1; - break; - case "Joined": - dto.Joined = DateTimeOffset.Parse(a.Value.ToString()); - break; - case "UsedNames": - dto.UsedNames = JsonSerializer.Deserialize>(a.Value.ToString()) ?? new List(); - break; - } - } - return dto; + return _database.Users.FirstOrDefault(u => u.UserId.Equals(userId.AsLong())); } catch (Exception e) { _logger.Warning(LogSource.UserRepository, $"Failed to get {userId} from repository", e); - return new UserRepositoryUser(); + return null; } } - - public string GetUserSetting(ulong userId, string setting) - { - return _redis.HashGet($"Users:{userId}", setting); - } - - public bool SaveUserSetting(ulong userId, string setting, string value) - { - _redis.HashSet($"Users:{userId}", new[] - { - new HashEntry(setting, value) - }); - return true; - } } } \ No newline at end of file diff --git a/Geekbot.net/Lib/UserRepository/UserRepositoryUserDto.cs b/Geekbot.net/Lib/UserRepository/UserRepositoryUserDto.cs deleted file mode 100644 index 4af719b..0000000 --- a/Geekbot.net/Lib/UserRepository/UserRepositoryUserDto.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Geekbot.net.Lib.UserRepository -{ - public class UserRepositoryUser - { - public ulong Id { get; set; } - public string Username { get; set; } - public string Discriminator { get; set; } - public string AvatarUrl { get; set; } - public bool IsBot { get; set; } - public DateTimeOffset Joined { get; set; } - public List UsedNames { get; set; } - } -} \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 4fa763d..ee05a12 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -111,7 +111,7 @@ namespace Geekbot.net _services = new ServiceCollection(); - _userRepository = new UserRepository(_redis, logger); + _userRepository = new UserRepository(database, logger); var fortunes = new FortunesProvider(logger); var mediaProvider = new MediaProvider(logger); var malClient = new MalClient(_redis, logger); From 5117e7609f6115b287444bafd472b5cd699f8a0b Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 10 May 2018 02:33:10 +0200 Subject: [PATCH 060/443] Port !karma --- Geekbot.net/Commands/User/Karma.cs | 82 +++++++++++++++++++++--------- Geekbot.net/Geekbot.net.csproj | 2 +- 2 files changed, 58 insertions(+), 26 deletions(-) diff --git a/Geekbot.net/Commands/User/Karma.cs b/Geekbot.net/Commands/User/Karma.cs index d6244a8..b63951e 100644 --- a/Geekbot.net/Commands/User/Karma.cs +++ b/Geekbot.net/Commands/User/Karma.cs @@ -1,23 +1,26 @@ using System; +using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; +using Geekbot.net.Database; +using Geekbot.net.Database.Models; using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Localization; -using StackExchange.Redis; namespace Geekbot.net.Commands.User { public class Karma : ModuleBase { private readonly IErrorHandler _errorHandler; - private readonly IDatabase _redis; + private readonly DatabaseContext _database; private readonly ITranslationHandler _translation; - public Karma(IDatabase redis, IErrorHandler errorHandler, ITranslationHandler translation) + public Karma(DatabaseContext database, IErrorHandler errorHandler, ITranslationHandler translation) { - _redis = redis; + _database = database; _errorHandler = errorHandler; _translation = translation; } @@ -30,22 +33,26 @@ namespace Geekbot.net.Commands.User try { var transDict = _translation.GetDict(Context); - var lastKarmaFromRedis = _redis.HashGet($"{Context.Guild.Id}:KarmaTimeout", Context.User.Id.ToString()); - var lastKarma = ConvertToDateTimeOffset(lastKarmaFromRedis.ToString()); + var actor = GetUser(Context.User.Id); if (user.Id == Context.User.Id) { await ReplyAsync(string.Format(transDict["CannotChangeOwn"], Context.User.Username)); } - else if (TimeoutFinished(lastKarma)) + else if (TimeoutFinished(actor.TimeOut)) { await ReplyAsync(string.Format(transDict["WaitUntill"], Context.User.Username, - GetTimeLeft(lastKarma))); + GetTimeLeft(actor.TimeOut))); } else { - var newKarma = _redis.HashIncrement($"{Context.Guild.Id}:Karma", user.Id.ToString()); - _redis.HashSet($"{Context.Guild.Id}:KarmaTimeout", - new[] {new HashEntry(Context.User.Id.ToString(), DateTimeOffset.Now.ToString("u"))}); + var target = GetUser(user.Id); + target.Karma = target.Karma + 1; + SetUser(target); + + actor.TimeOut = DateTimeOffset.Now; + SetUser(actor); + + _database.SaveChanges(); var eb = new EmbedBuilder(); eb.WithAuthor(new EmbedAuthorBuilder() @@ -56,7 +63,7 @@ namespace Geekbot.net.Commands.User eb.Title = transDict["Increased"]; eb.AddInlineField(transDict["By"], Context.User.Username); eb.AddInlineField(transDict["Amount"], "+1"); - eb.AddInlineField(transDict["Current"], newKarma); + eb.AddInlineField(transDict["Current"], target.Karma); await ReplyAsync("", false, eb.Build()); } } @@ -74,22 +81,26 @@ namespace Geekbot.net.Commands.User try { var transDict = _translation.GetDict(Context); - var lastKarmaFromRedis = _redis.HashGet($"{Context.Guild.Id}:KarmaTimeout", Context.User.Id.ToString()); - var lastKarma = ConvertToDateTimeOffset(lastKarmaFromRedis.ToString()); + var actor = GetUser(Context.User.Id); if (user.Id == Context.User.Id) { await ReplyAsync(string.Format(transDict["CannotChangeOwn"], Context.User.Username)); } - else if (TimeoutFinished(lastKarma)) + else if (TimeoutFinished(actor.TimeOut)) { await ReplyAsync(string.Format(transDict["WaitUntill"], Context.User.Username, - GetTimeLeft(lastKarma))); + GetTimeLeft(actor.TimeOut))); } else { - var newKarma = _redis.HashDecrement($"{Context.Guild.Id}:Karma", user.Id.ToString()); - _redis.HashSet($"{Context.Guild.Id}:KarmaTimeout", - new[] {new HashEntry(Context.User.Id.ToString(), DateTimeOffset.Now.ToString())}); + var target = GetUser(user.Id); + target.Karma = target.Karma - 1; + SetUser(target); + + actor.TimeOut = DateTimeOffset.Now; + SetUser(actor); + + _database.SaveChanges(); var eb = new EmbedBuilder(); eb.WithAuthor(new EmbedAuthorBuilder() @@ -100,7 +111,7 @@ namespace Geekbot.net.Commands.User eb.Title = transDict["Decreased"]; eb.AddInlineField(transDict["By"], Context.User.Username); eb.AddInlineField(transDict["Amount"], "-1"); - eb.AddInlineField(transDict["Current"], newKarma); + eb.AddInlineField(transDict["Current"], target.Karma); await ReplyAsync("", false, eb.Build()); } } @@ -110,11 +121,6 @@ namespace Geekbot.net.Commands.User } } - private DateTimeOffset ConvertToDateTimeOffset(string dateTimeOffsetString) - { - return string.IsNullOrEmpty(dateTimeOffsetString) ? DateTimeOffset.Now.Subtract(new TimeSpan(7, 18, 0, 0)) : DateTimeOffset.Parse(dateTimeOffsetString); - } - private bool TimeoutFinished(DateTimeOffset lastKarma) { return lastKarma.AddMinutes(3) > DateTimeOffset.Now; @@ -125,5 +131,31 @@ namespace Geekbot.net.Commands.User var dt = lastKarma.AddMinutes(3).Subtract(DateTimeOffset.Now); return $"{dt.Minutes} Minutes and {dt.Seconds} Seconds"; } + + private KarmaModel GetUser(ulong userId) + { + var user = _database.Karma.FirstOrDefault(u =>u.GuildId.Equals(Context.Guild.Id.AsLong()) && u.UserId.Equals(userId.AsLong())) ?? CreateNewRow(userId); + return user; + } + + private bool SetUser(KarmaModel user) + { + _database.Karma.Update(user); + return true; + } + + private KarmaModel CreateNewRow(ulong userId) + { + var user = new KarmaModel() + { + GuildId = Context.Guild.Id.AsLong(), + UserId = userId.AsLong(), + Karma = 0, + TimeOut = DateTimeOffset.MinValue + }; + var newUser = _database.Karma.Add(user).Entity; + _database.SaveChanges(); + return newUser; + } } } \ No newline at end of file diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 9141772..425eb9f 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -3,7 +3,7 @@ Exe netcoreapp2.0 derp.ico - 3.7.0 + 4.0.0 $(VersionSuffix) $(Version)-$(VersionSuffix) Pizza and Coffee Studios From 54bcd541d3dbe91c9215b0142fd2f7c088cbd559 Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 10 May 2018 03:29:11 +0200 Subject: [PATCH 061/443] Fix run params --- Geekbot.net/Geekbot.net.csproj | 7 ++++++- Geekbot.net/Lib/RunParameters.cs | 22 +++++++++++----------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 425eb9f..33a1663 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -11,6 +11,11 @@ A Discord bot https://github.com/pizzaandcoffee/Geekbot.net NU1701 + git + https://geekbot.pizzaandcoffee.rocks + + + true @@ -88,4 +93,4 @@ - \ No newline at end of file + diff --git a/Geekbot.net/Lib/RunParameters.cs b/Geekbot.net/Lib/RunParameters.cs index 5485533..eb3ca37 100644 --- a/Geekbot.net/Lib/RunParameters.cs +++ b/Geekbot.net/Lib/RunParameters.cs @@ -17,7 +17,7 @@ namespace Geekbot.net.Lib [Option('j', "log-json", Default = false, HelpText = "Logs messages as json")] public bool LogJson { get; set; } - [Option("disable-api", Default = false, HelpText = "Disables the web api")] + [Option('a', "disable-api", Default = false, HelpText = "Disables the web api")] public bool DisableApi { get; set; } [Option('e', "expose-errors", Default = false, HelpText = "Shows internal errors in the chat")] @@ -33,19 +33,19 @@ namespace Geekbot.net.Lib public bool InMemory { get; set; } // Postresql connection - [Option("database", Default = false, HelpText = "Select a postgresql database")] - public string DbDatabase { get; set; } = "geekbot"; + [Option("database", Default = "geekbot", HelpText = "Select a postgresql database")] + public string DbDatabase { get; set; } - [Option("db-host", Default = false, HelpText = "Set a postgresql host (e.g. 127.0.0.1)")] - public string DbHost { get; set; } = "localhost"; + [Option("db-host", Default = "localhost", HelpText = "Set a postgresql host (e.g. 127.0.0.1)")] + public string DbHost { get; set; } - [Option("db-port", Default = false, HelpText = "Set a postgresql host (e.g. 5432)")] - public string DbPort { get; set; } = "5432"; + [Option("db-port", Default = "5432", HelpText = "Set a postgresql host (e.g. 5432)")] + public string DbPort { get; set; } - [Option("db-user", Default = false, HelpText = "Set a postgresql user")] - public string DbUser { get; set; } = "geekbot"; + [Option("db-user", Default = "geekbot", HelpText = "Set a postgresql user")] + public string DbUser { get; set; } - [Option("db-password", Default = false, HelpText = "Set a posgresql password")] - public string DbPassword { get; set; } = ""; + [Option("db-password", Default = "", HelpText = "Set a posgresql password")] + public string DbPassword { get; set; } } } \ No newline at end of file From b54b7cd7869b06c3f1e208fe7e8bcaee8950263a Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 10 May 2018 17:56:43 +0200 Subject: [PATCH 062/443] port !rank --- Geekbot.net/Commands/User/Ranking/Rank.cs | 141 ++++++++++++------ Geekbot.net/Commands/User/Ranking/RankType.cs | 9 ++ ...{RankUserPolyfillDto.cs => RankUserDto.cs} | 2 +- Geekbot.net/Database/DatabaseContext.cs | 2 + Geekbot.net/Database/Models/GuildsModel.cs | 5 +- Geekbot.net/Database/Models/MessagesModel.cs | 18 +++ Geekbot.net/Database/Models/RollsModel.cs | 18 +++ Geekbot.net/Geekbot.net.csproj | 2 +- 8 files changed, 145 insertions(+), 52 deletions(-) create mode 100644 Geekbot.net/Commands/User/Ranking/RankType.cs rename Geekbot.net/Commands/User/Ranking/{RankUserPolyfillDto.cs => RankUserDto.cs} (80%) create mode 100644 Geekbot.net/Database/Models/MessagesModel.cs create mode 100644 Geekbot.net/Database/Models/RollsModel.cs diff --git a/Geekbot.net/Commands/User/Ranking/Rank.cs b/Geekbot.net/Commands/User/Ranking/Rank.cs index e0fbfa0..9b1b32f 100644 --- a/Geekbot.net/Commands/User/Ranking/Rank.cs +++ b/Geekbot.net/Commands/User/Ranking/Rank.cs @@ -5,12 +5,13 @@ using System.Text; using System.Threading.Tasks; using Discord.Commands; using Discord.WebSocket; +using Geekbot.net.Database; using Geekbot.net.Lib; using Geekbot.net.Lib.Converters; using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Logger; using Geekbot.net.Lib.UserRepository; -using StackExchange.Redis; namespace Geekbot.net.Commands.User.Ranking { @@ -19,14 +20,14 @@ namespace Geekbot.net.Commands.User.Ranking private readonly IEmojiConverter _emojiConverter; private readonly IErrorHandler _errorHandler; private readonly IGeekbotLogger _logger; - private readonly IDatabase _redis; + private readonly DatabaseContext _database; private readonly IUserRepository _userRepository; private readonly DiscordSocketClient _client; - public Rank(IDatabase redis, IErrorHandler errorHandler, IGeekbotLogger logger, IUserRepository userRepository, + public Rank(DatabaseContext database, IErrorHandler errorHandler, IGeekbotLogger logger, IUserRepository userRepository, IEmojiConverter emojiConverter, DiscordSocketClient client) { - _redis = redis; + _database = database; _errorHandler = errorHandler; _logger = logger; _userRepository = userRepository; @@ -41,68 +42,76 @@ namespace Geekbot.net.Commands.User.Ranking { try { - var type = typeUnformated.ToCharArray().First().ToString().ToUpper() + typeUnformated.Substring(1); - - if (!type.Equals("Messages") && !type.Equals("Karma") && !type.Equals("Rolls")) + RankType type; + try + { + type = Enum.Parse(typeUnformated.ToLower()); + } + catch { await ReplyAsync("Valid types are '`messages`' '`karma`', '`rolls`'"); return; } + var replyBuilder = new StringBuilder(); - if (amount > 20) { - replyBuilder.AppendLine(":warning: Limiting to 20"); + replyBuilder.AppendLine(":warning: Limiting to 20\n"); amount = 20; } + + Dictionary list; - var messageList = _redis.HashGetAll($"{Context.Guild.Id}:{type}"); - if (messageList.Length == 0) + switch (type) { - await ReplyAsync($"No {type.ToLowerInvariant()} found on this server"); + case RankType.messages: + list = GetMessageList(amount); + break; + case RankType.karma: + list = GetKarmaList(amount); + break; + case RankType.rolls: + list = GetRollsList(amount); + break; + default: + list = new Dictionary(); + break; + } + + if (!list.Any()) + { + await ReplyAsync($"No {type} found on this server"); return; } - var sortedList = messageList.OrderByDescending(e => e.Value).ToList(); - var guildMessages = (int) sortedList.First().Value; - var theBot = sortedList.FirstOrDefault(e => e.Name.ToString().Equals(_client.CurrentUser.Id.ToString())); - if (!string.IsNullOrEmpty(theBot.Name)) - { - sortedList.Remove(theBot); - } - if (type == "Messages") sortedList.RemoveAt(0); - var highscoreUsers = new Dictionary(); - var listLimiter = 1; + var highscoreUsers = new Dictionary(); var failedToRetrieveUser = false; - foreach (var user in sortedList) + foreach (var user in list) { - if (listLimiter > amount) break; try { - var guildUser = _userRepository.Get((ulong) user.Name); + var guildUser = _userRepository.Get(user.Key); if (guildUser?.Username != null) { - highscoreUsers.Add(new RankUserPolyfillDto + highscoreUsers.Add(new RankUserDto { Username = guildUser.Username, Discriminator = guildUser.Discriminator - }, (int) user.Value); + }, user.Value); } else { - highscoreUsers.Add(new RankUserPolyfillDto + highscoreUsers.Add(new RankUserDto { - Id = user.Name - }, (int) user.Value); + Id = user.Key.ToString() + }, user.Value); failedToRetrieveUser = true; } - - listLimiter++; } - catch (Exception e) + catch { - _logger.Warning(LogSource.Geekbot, $"Could not retrieve user {user.Name}", e); + // ignore } } @@ -119,21 +128,7 @@ namespace Geekbot.net.Commands.User.Ranking ? $"**{user.Key.Username}#{user.Key.Discriminator}**" : $"**{user.Key.Id}**"); - switch (type) - { - case "Messages": - var percent = Math.Round((double) (100 * user.Value) / guildMessages, 2); - replyBuilder.Append($" - {percent}% of total - {user.Value} messages"); - break; - case "Karma": - replyBuilder.Append($" - {user.Value} Karma"); - break; - case "Rolls": - replyBuilder.Append($" - {user.Value} Guessed"); - break; - } - - replyBuilder.Append("\n"); + replyBuilder.Append($" - {user.Value} {type}\n"); highscorePlace++; } @@ -145,5 +140,53 @@ namespace Geekbot.net.Commands.User.Ranking _errorHandler.HandleCommandException(e, Context); } } + + private Dictionary GetMessageList(int amount) + { + var data = _database.Messages + .Where(k => k.GuildId.Equals(Context.Guild.Id.AsLong())) + .OrderByDescending(o => o.MessageCount) + .Take(amount); + + var dict = new Dictionary(); + foreach (var user in data) + { + dict.Add(user.UserId.AsUlong(), user.MessageCount); + } + + return dict; + } + + private Dictionary GetKarmaList(int amount) + { + var data = _database.Karma + .Where(k => k.GuildId.Equals(Context.Guild.Id.AsLong())) + .OrderByDescending(o => o.Karma) + .Take(amount); + + var dict = new Dictionary(); + foreach (var user in data) + { + dict.Add(user.UserId.AsUlong(), user.Karma); + } + + return dict; + } + + private Dictionary GetRollsList(int amount) + { + var data = _database.Rolls + .Where(k => k.GuildId.Equals(Context.Guild.Id.AsLong())) + .OrderByDescending(o => o.Rolls) + .Take(amount); + + var dict = new Dictionary(); + foreach (var user in data) + { + dict.Add(user.UserId.AsUlong(), user.Rolls); + } + + return dict; + } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/User/Ranking/RankType.cs b/Geekbot.net/Commands/User/Ranking/RankType.cs new file mode 100644 index 0000000..1e88ef0 --- /dev/null +++ b/Geekbot.net/Commands/User/Ranking/RankType.cs @@ -0,0 +1,9 @@ +namespace Geekbot.net.Commands.User.Ranking +{ + public enum RankType + { + messages, + karma, + rolls + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/User/Ranking/RankUserPolyfillDto.cs b/Geekbot.net/Commands/User/Ranking/RankUserDto.cs similarity index 80% rename from Geekbot.net/Commands/User/Ranking/RankUserPolyfillDto.cs rename to Geekbot.net/Commands/User/Ranking/RankUserDto.cs index 625c326..2ec518e 100644 --- a/Geekbot.net/Commands/User/Ranking/RankUserPolyfillDto.cs +++ b/Geekbot.net/Commands/User/Ranking/RankUserDto.cs @@ -1,6 +1,6 @@ namespace Geekbot.net.Commands.User.Ranking { - internal class RankUserPolyfillDto + internal class RankUserDto { public string Username { get; set; } public string Discriminator { get; set; } diff --git a/Geekbot.net/Database/DatabaseContext.cs b/Geekbot.net/Database/DatabaseContext.cs index 028a4f5..2768e2e 100644 --- a/Geekbot.net/Database/DatabaseContext.cs +++ b/Geekbot.net/Database/DatabaseContext.cs @@ -11,6 +11,8 @@ namespace Geekbot.net.Database public DbSet GuildSettings { get; set; } public DbSet Karma { get; set; } public DbSet Ships { get; set; } + public DbSet Rolls { get; set; } + public DbSet Messages { get; set; } public DbSet SlapsModels { get; set; } // public DbSet UserSettings { get; set; } // public DbSet RoleSelfService { get; set; } diff --git a/Geekbot.net/Database/Models/GuildsModel.cs b/Geekbot.net/Database/Models/GuildsModel.cs index 515231a..371bfd7 100644 --- a/Geekbot.net/Database/Models/GuildsModel.cs +++ b/Geekbot.net/Database/Models/GuildsModel.cs @@ -1,4 +1,5 @@ -using System.ComponentModel.DataAnnotations; +using System; +using System.ComponentModel.DataAnnotations; namespace Geekbot.net.Database.Models { @@ -18,5 +19,7 @@ namespace Geekbot.net.Database.Models [Required] public string IconUrl { get; set; } + + public DateTimeOffset CreatedAt { get; set; } } } \ No newline at end of file diff --git a/Geekbot.net/Database/Models/MessagesModel.cs b/Geekbot.net/Database/Models/MessagesModel.cs new file mode 100644 index 0000000..31332e8 --- /dev/null +++ b/Geekbot.net/Database/Models/MessagesModel.cs @@ -0,0 +1,18 @@ +using System.ComponentModel.DataAnnotations; + +namespace Geekbot.net.Database.Models +{ + public class MessagesModel + { + [Key] + public int Id { get; set; } + + [Required] + public long GuildId { get; set; } + + [Required] + public long UserId { get; set; } + + public int MessageCount { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Database/Models/RollsModel.cs b/Geekbot.net/Database/Models/RollsModel.cs new file mode 100644 index 0000000..de9b82e --- /dev/null +++ b/Geekbot.net/Database/Models/RollsModel.cs @@ -0,0 +1,18 @@ +using System.ComponentModel.DataAnnotations; + +namespace Geekbot.net.Database.Models +{ + public class RollsModel + { + [Key] + public int Id { get; set; } + + [Required] + public long GuildId { get; set; } + + [Required] + public long UserId { get; set; } + + public int Rolls { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 33a1663..ff230b6 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -93,4 +93,4 @@ - + \ No newline at end of file From 9ecc224ae167dd89f308bfa64705f99f37d729b6 Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 10 May 2018 20:12:31 +0200 Subject: [PATCH 063/443] Add ef cli tools --- Geekbot.net/Geekbot.net.csproj | 14 ++++++++++---- Tests/Tests.csproj | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index ff230b6..3c59bc7 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -22,14 +22,16 @@ 1.0.2 - + + - - - + + + + @@ -55,6 +57,10 @@ + + + PreserveNewest diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index c838db0..776acf6 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -5,7 +5,7 @@ NU1701 - + From 32ae82ca8d492943242b1c745fcfa36e0ce913ec Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 11 May 2018 00:36:29 +0200 Subject: [PATCH 064/443] Add simple migration script --- Geekbot.net/Commands/Admin/Owner.cs | 28 +- Geekbot.net/Commands/User/GuildInfo.cs | 13 +- Geekbot.net/Database/DatabaseContext.cs | 2 +- Geekbot.net/Database/Models/GuildsModel.cs | 3 +- Geekbot.net/Database/RedisMigration.cs | 304 +++++++++++++++++++++ Geekbot.net/Geekbot.net.csproj | 3 +- 6 files changed, 341 insertions(+), 12 deletions(-) create mode 100644 Geekbot.net/Database/RedisMigration.cs diff --git a/Geekbot.net/Commands/Admin/Owner.cs b/Geekbot.net/Commands/Admin/Owner.cs index 10cbfd7..a1a98d6 100644 --- a/Geekbot.net/Commands/Admin/Owner.cs +++ b/Geekbot.net/Commands/Admin/Owner.cs @@ -33,6 +33,24 @@ namespace Geekbot.net.Commands.Admin _database = database; } + [Command("migrate", RunMode = RunMode.Async)] + public async Task Migrate() + { + await ReplyAsync("starting migration"); + + try + { + var redisMigration = new RedisMigration(_database, _redis, _logger, _client); + await redisMigration.Migrate(); + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + } + + await ReplyAsync("done"); + } + [Command("youtubekey", RunMode = RunMode.Async)] [Remarks(CommandCategories.Admin)] [Summary("Set the youtube api key")] @@ -90,8 +108,14 @@ namespace Geekbot.net.Commands.Admin [Summary("Throw an error un purpose")] public void PurposefulError() { - var e = new Exception("Error Generated by !owner error"); - _errorHandler.HandleCommandException(e, Context); + try + { + throw new Exception("Error Generated by !owner error"); + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + } } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/User/GuildInfo.cs b/Geekbot.net/Commands/User/GuildInfo.cs index 6306a76..dabe005 100644 --- a/Geekbot.net/Commands/User/GuildInfo.cs +++ b/Geekbot.net/Commands/User/GuildInfo.cs @@ -3,22 +3,23 @@ using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; +using Geekbot.net.Database; using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Levels; -using StackExchange.Redis; namespace Geekbot.net.Commands.User { public class GuildInfo : ModuleBase { private readonly IErrorHandler _errorHandler; + private readonly DatabaseContext _database; private readonly ILevelCalc _levelCalc; - private readonly IDatabase _redis; - public GuildInfo(IDatabase redis, ILevelCalc levelCalc, IErrorHandler errorHandler) + public GuildInfo(DatabaseContext database, ILevelCalc levelCalc, IErrorHandler errorHandler) { - _redis = redis; + _database = database; _levelCalc = levelCalc; _errorHandler = errorHandler; } @@ -39,7 +40,9 @@ namespace Geekbot.net.Commands.User var created = Context.Guild.CreatedAt; var age = Math.Floor((DateTime.Now - created).TotalDays); - var messages = _redis.HashGet($"{Context.Guild.Id}:Messages", 0.ToString()); + var messages = _database.Messages + .Where(e => e.GuildId == Context.Guild.Id.AsLong()) + .Sum(e => e.MessageCount); var level = _levelCalc.GetLevel((int) messages); eb.AddField("Server Age", $"{created.Day}/{created.Month}/{created.Year} ({age} days)"); diff --git a/Geekbot.net/Database/DatabaseContext.cs b/Geekbot.net/Database/DatabaseContext.cs index 2768e2e..78701b8 100644 --- a/Geekbot.net/Database/DatabaseContext.cs +++ b/Geekbot.net/Database/DatabaseContext.cs @@ -13,7 +13,7 @@ namespace Geekbot.net.Database public DbSet Ships { get; set; } public DbSet Rolls { get; set; } public DbSet Messages { get; set; } - public DbSet SlapsModels { get; set; } + public DbSet Slaps { get; set; } // public DbSet UserSettings { get; set; } // public DbSet RoleSelfService { get; set; } } diff --git a/Geekbot.net/Database/Models/GuildsModel.cs b/Geekbot.net/Database/Models/GuildsModel.cs index 371bfd7..b6347a6 100644 --- a/Geekbot.net/Database/Models/GuildsModel.cs +++ b/Geekbot.net/Database/Models/GuildsModel.cs @@ -15,9 +15,8 @@ namespace Geekbot.net.Database.Models public string Name { get; set; } [Required] - public string Owner { get; set; } + public long Owner { get; set; } - [Required] public string IconUrl { get; set; } public DateTimeOffset CreatedAt { get; set; } diff --git a/Geekbot.net/Database/RedisMigration.cs b/Geekbot.net/Database/RedisMigration.cs new file mode 100644 index 0000000..6c8e0d0 --- /dev/null +++ b/Geekbot.net/Database/RedisMigration.cs @@ -0,0 +1,304 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Discord; +using Discord.WebSocket; +using Geekbot.net.Commands.Utils.Quote; +using Geekbot.net.Database.Models; +using Geekbot.net.Lib.Extensions; +using Geekbot.net.Lib.Logger; +using Newtonsoft.Json; +using StackExchange.Redis; + +namespace Geekbot.net.Database +{ + public class RedisMigration + { + private readonly DatabaseContext _database; + private readonly IDatabase _redis; + private readonly IGeekbotLogger _logger; + private readonly DiscordSocketClient _client; + + public RedisMigration(DatabaseContext database, IDatabase redis, IGeekbotLogger logger, DiscordSocketClient client) + { + _database = database; + _redis = redis; + _logger = logger; + _client = client; + } + + public async Task Migrate() + { + _logger.Information(LogSource.Geekbot, "Starting migration process"); + foreach (var guild in _client.Guilds) + { + _logger.Information(LogSource.Geekbot, $"Start Migration for {guild.Name}"); + #region Quotes + /** + * Quotes + */ + try + { + var data = _redis.SetScan($"{guild.Id}:Quotes"); + foreach (var q in data) + { + try + { + var qd = JsonConvert.DeserializeObject(q); + var quote = CreateQuoteObject(guild.Id, qd); + _database.Quotes.Add(quote); + _database.SaveChanges(); + } + catch + { + _logger.Warning(LogSource.Geekbot, $"quote failed: {q}"); + } + } + } + catch + { + _logger.Warning(LogSource.Geekbot, "quote migration failed"); + } + #endregion + + #region Karma + /** + * Karma + */ + try + { + var data = _redis.HashGetAll($"{guild.Id}:Karma"); + foreach (var q in data) + { + try + { + var user = new KarmaModel() + { + GuildId = guild.Id.AsLong(), + UserId = ulong.Parse(q.Name).AsLong(), + Karma = int.Parse(q.Value), + TimeOut = DateTimeOffset.MinValue + }; + _database.Karma.Add(user); + _database.SaveChanges(); + } + catch + { + _logger.Warning(LogSource.Geekbot, $"karma failed for: {q.Name}"); + } + } + } + catch + { + _logger.Warning(LogSource.Geekbot, "karma migration failed"); + } + #endregion + + #region Rolls + /** + * Rolls + */ + try + { + var data = _redis.HashGetAll($"{guild.Id}:Rolls"); + foreach (var q in data) + { + try + { + var user = new RollsModel() + { + GuildId = guild.Id.AsLong(), + UserId = ulong.Parse(q.Name).AsLong(), + Rolls = int.Parse(q.Value) + }; + _database.Rolls.Add(user); + _database.SaveChanges(); + } + catch + { + _logger.Warning(LogSource.Geekbot, $"Rolls failed for: {q.Name}"); + } + } + } + catch + { + _logger.Warning(LogSource.Geekbot, "rolls migration failed"); + } + #endregion + + #region Slaps + /** + * Slaps + */ + try + { + var given = _redis.HashGetAll($"{guild.Id}:SlapsGiven"); + var gotten = _redis.HashGetAll($"{guild.Id}:SlapsGiven"); + foreach (var q in given) + { + try + { + var user = new SlapsModel() + { + GuildId = guild.Id.AsLong(), + UserId = ulong.Parse(q.Name).AsLong(), + Given = int.Parse(q.Value), + Recieved= int.Parse(gotten[int.Parse(q.Name)].Value) + }; + _database.Slaps.Add(user); + _database.SaveChanges(); + } + catch + { + _logger.Warning(LogSource.Geekbot, $"Slaps failed for: {q.Name}"); + } + } + } + catch + { + _logger.Warning(LogSource.Geekbot, "Slaps migration failed"); + } + #endregion + + #region Messages + /** + * Messages + */ + try + { + var data = _redis.HashGetAll($"{guild.Id}:Messages"); + foreach (var q in data) + { + try + { + // drop the guild qounter + if(q.Name.ToString() != "0") continue; + var user = new MessagesModel() + { + GuildId = guild.Id.AsLong(), + UserId = ulong.Parse(q.Name).AsLong(), + MessageCount= int.Parse(q.Value) + }; + _database.Messages.Add(user); + _database.SaveChanges(); + } + catch + { + _logger.Warning(LogSource.Geekbot, $"Messages failed for: {q.Name}"); + } + } + } + catch + { + _logger.Warning(LogSource.Geekbot, "Messages migration failed"); + } + #endregion + + #region Ships + /** + * Ships + */ + try + { + var data = _redis.HashGetAll($"{guild.Id}:Ships"); + var done = new List(); + foreach (var q in data) + { + try + { + if (done.Contains(q.Name)) continue; + var split = q.Name.ToString().Split('-'); + var user = new ShipsModel() + { + FirstUserId = ulong.Parse(split[0]).AsLong(), + SecondUserId = ulong.Parse(split[1]).AsLong(), + Strength = int.Parse(q.Value) + }; + _database.Ships.Add(user); + _database.SaveChanges(); + done.Add(q.Name); + } + catch + { + _logger.Warning(LogSource.Geekbot, $"Ships failed for: {q.Name}"); + } + } + } + catch + { + _logger.Warning(LogSource.Geekbot, "Ships migration failed"); + } + #endregion + + #region Users + /** + * Users + */ + try + { + var data = guild.Users; + foreach (var user in data) + { + try + { + _database.Users.Add(new UserModel() + { + UserId = user.Id.AsLong(), + Username = user.Username, + Discriminator = user.Discriminator, + AvatarUrl = user.GetAvatarUrl(ImageFormat.Auto, 1024), + IsBot = user.IsBot, + Joined = user.CreatedAt, + UsedNames = new [] {user.Username} + }); + _database.SaveChanges(); + } + catch + { + _logger.Warning(LogSource.Geekbot, $"User failed: {user.Username}"); + } + } + } + catch + { + _logger.Warning(LogSource.Geekbot, "User migration failed"); + } + #endregion + + #region Guilds + + _database.Guilds.Add(new GuildsModel + { + CreatedAt = guild.CreatedAt, + GuildId = guild.Id.AsLong(), + IconUrl = guild.IconUrl, + Name = guild.Name, + Owner = guild.Owner.Id.AsLong() + }); + _database.SaveChanges(); + + #endregion + _logger.Information(LogSource.Geekbot, $"Finished Migration for {guild.Name}"); + } + _logger.Information(LogSource.Geekbot, "Finished migration process"); + } + + private QuoteModel CreateQuoteObject(ulong guild, QuoteObjectDto quote) + { + var last = _database.Quotes.Where(e => e.GuildId.Equals(guild.AsLong())) + .OrderByDescending(e => e.InternalId).FirstOrDefault(); + int internalId = 1; + if (last != null) internalId = last.InternalId + 1; + return new QuoteModel() + { + InternalId = internalId, + GuildId = guild.AsLong(), + UserId = quote.UserId.AsLong(), + Time = quote.Time, + Quote = quote.Quote, + Image = quote.Image + }; + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 3c59bc7..27e5dad 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -58,8 +58,7 @@ - + From 3fa4115502455c1174bff2306cf0680a02c392c0 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 13 May 2018 14:14:50 +0200 Subject: [PATCH 065/443] Improve migration Script --- Geekbot.net/Database/DatabaseContext.cs | 6 +++-- Geekbot.net/Database/Models/GlobalsModel.cs | 18 +++++++++++++ Geekbot.net/Database/RedisMigration.cs | 25 ++++++++++--------- Geekbot.net/Lib/Extensions/DbSetExtensions.cs | 17 +++++++++++++ 4 files changed, 52 insertions(+), 14 deletions(-) create mode 100644 Geekbot.net/Database/Models/GlobalsModel.cs create mode 100644 Geekbot.net/Lib/Extensions/DbSetExtensions.cs diff --git a/Geekbot.net/Database/DatabaseContext.cs b/Geekbot.net/Database/DatabaseContext.cs index 78701b8..e4fab28 100644 --- a/Geekbot.net/Database/DatabaseContext.cs +++ b/Geekbot.net/Database/DatabaseContext.cs @@ -14,7 +14,9 @@ namespace Geekbot.net.Database public DbSet Rolls { get; set; } public DbSet Messages { get; set; } public DbSet Slaps { get; set; } -// public DbSet UserSettings { get; set; } -// public DbSet RoleSelfService { get; set; } + public DbSet Globals { get; set; } + + // public DbSet UserSettings { get; set; } + // public DbSet RoleSelfService { get; set; } } } \ No newline at end of file diff --git a/Geekbot.net/Database/Models/GlobalsModel.cs b/Geekbot.net/Database/Models/GlobalsModel.cs new file mode 100644 index 0000000..63261fc --- /dev/null +++ b/Geekbot.net/Database/Models/GlobalsModel.cs @@ -0,0 +1,18 @@ +using System.ComponentModel.DataAnnotations; + +namespace Geekbot.net.Database.Models +{ + public class GlobalsModel + { + [Key] + public int Id { get; set; } + + [Required] + public string Name { get; set; } + + [Required] + public string Value { get; set; } + + public string Meta { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Database/RedisMigration.cs b/Geekbot.net/Database/RedisMigration.cs index 6c8e0d0..1cd73b0 100644 --- a/Geekbot.net/Database/RedisMigration.cs +++ b/Geekbot.net/Database/RedisMigration.cs @@ -8,6 +8,7 @@ using Geekbot.net.Commands.Utils.Quote; using Geekbot.net.Database.Models; using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Logger; +using MtgApiManager.Lib.Model; using Newtonsoft.Json; using StackExchange.Redis; @@ -172,8 +173,6 @@ namespace Geekbot.net.Database { try { - // drop the guild qounter - if(q.Name.ToString() != "0") continue; var user = new MessagesModel() { GuildId = guild.Id.AsLong(), @@ -242,16 +241,18 @@ namespace Geekbot.net.Database { try { - _database.Users.Add(new UserModel() - { - UserId = user.Id.AsLong(), - Username = user.Username, - Discriminator = user.Discriminator, - AvatarUrl = user.GetAvatarUrl(ImageFormat.Auto, 1024), - IsBot = user.IsBot, - Joined = user.CreatedAt, - UsedNames = new [] {user.Username} - }); + var namesSerialized = _redis.HashGet($"User:{user.Id}", "UsedNames").ToString(); + var names = Utf8Json.JsonSerializer.Deserialize(namesSerialized); + _database.Users.AddIfNotExists(new UserModel() + { + UserId = user.Id.AsLong(), + Username = user.Username, + Discriminator = user.Discriminator, + AvatarUrl = user.GetAvatarUrl(ImageFormat.Auto, 1024), + IsBot = user.IsBot, + Joined = user.CreatedAt, + UsedNames = names + }, model => model.UserId.Equals(user.Id.AsLong())); _database.SaveChanges(); } catch diff --git a/Geekbot.net/Lib/Extensions/DbSetExtensions.cs b/Geekbot.net/Lib/Extensions/DbSetExtensions.cs new file mode 100644 index 0000000..c731fc0 --- /dev/null +++ b/Geekbot.net/Lib/Extensions/DbSetExtensions.cs @@ -0,0 +1,17 @@ +using System; +using System.Linq; +using System.Linq.Expressions; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.ChangeTracking; + +namespace Geekbot.net.Lib.Extensions +{ + public static class DbSetExtensions + { + public static EntityEntry AddIfNotExists(this DbSet dbSet, T entity, Expression> predicate = null) where T : class, new() + { + var exists = predicate != null ? dbSet.Any(predicate) : dbSet.Any(); + return !exists ? dbSet.Add(entity) : null; + } + } +} \ No newline at end of file From 37ac7f56a84d98740cbb522aef296ba7a56ab1d5 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 13 May 2018 15:55:57 +0200 Subject: [PATCH 066/443] Remove command categories --- Geekbot.net/Commands/Admin/Admin.cs | 8 -------- Geekbot.net/Commands/Admin/Mod.cs | 2 -- Geekbot.net/Commands/Admin/Owner.cs | 4 ---- Geekbot.net/Commands/Admin/Role.cs | 5 ----- Geekbot.net/Commands/Admin/Say.cs | 1 - Geekbot.net/Commands/Games/Pokedex.cs | 1 - Geekbot.net/Commands/Games/Roll.cs | 1 - .../Commands/Integrations/Google/Google.cs | 1 - .../Commands/Integrations/MagicTheGathering.cs | 1 - Geekbot.net/Commands/Integrations/Mal.cs | 2 -- .../UbranDictionary/UrbanDictionary.cs | 1 - Geekbot.net/Commands/Integrations/Wikipedia.cs | 1 - Geekbot.net/Commands/Integrations/Youtube.cs | 1 - Geekbot.net/Commands/Randomness/Cat/Cat.cs | 1 - Geekbot.net/Commands/Randomness/CheckEm.cs | 1 - .../Commands/Randomness/Chuck/ChuckNorrisJokes.cs | 1 - Geekbot.net/Commands/Randomness/Dad/DadJokes.cs | 1 - Geekbot.net/Commands/Randomness/Dog/Dog.cs | 1 - Geekbot.net/Commands/Randomness/EightBall.cs | 1 - Geekbot.net/Commands/Randomness/Fortune.cs | 1 - Geekbot.net/Commands/Randomness/Gdq.cs | 1 - Geekbot.net/Commands/Randomness/RandomAnimals.cs | 7 ------- Geekbot.net/Commands/Randomness/Ship.cs | 1 - Geekbot.net/Commands/Randomness/Slap.cs | 1 - Geekbot.net/Commands/User/GuildInfo.cs | 1 - Geekbot.net/Commands/User/Karma.cs | 2 -- Geekbot.net/Commands/User/Ranking/Rank.cs | 1 - Geekbot.net/Commands/User/Stats.cs | 1 - Geekbot.net/Commands/Utils/AvatarGetter.cs | 1 - Geekbot.net/Commands/Utils/Changelog/Changelog.cs | 4 +--- Geekbot.net/Commands/Utils/Choose.cs | 1 - Geekbot.net/Commands/Utils/Dice/Dice.cs | 1 - Geekbot.net/Commands/Utils/Emojify.cs | 1 - Geekbot.net/Commands/Utils/Help.cs | 1 - Geekbot.net/Commands/Utils/Info.cs | 2 -- Geekbot.net/Commands/Utils/Ping.cs | 1 - Geekbot.net/Commands/Utils/Poll/Poll.cs | 3 --- Geekbot.net/Commands/Utils/Quote/Quote.cs | 6 ------ Geekbot.net/Lib/CommandCategories.cs | 15 --------------- Geekbot.net/WebApi/Help/HelpController.cs | 1 - 40 files changed, 1 insertion(+), 87 deletions(-) delete mode 100644 Geekbot.net/Lib/CommandCategories.cs diff --git a/Geekbot.net/Commands/Admin/Admin.cs b/Geekbot.net/Commands/Admin/Admin.cs index 7d30689..7b5198d 100644 --- a/Geekbot.net/Commands/Admin/Admin.cs +++ b/Geekbot.net/Commands/Admin/Admin.cs @@ -30,7 +30,6 @@ namespace Geekbot.net.Commands.Admin } [Command("welcome", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Admin)] [Summary("Set a Welcome Message (use '$user' to mention the new joined user).")] public async Task SetWelcomeMessage([Remainder] [Summary("message")] string welcomeMessage) { @@ -40,7 +39,6 @@ namespace Geekbot.net.Commands.Admin } [Command("modchannel", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Admin)] [Summary("Set a channel for moderation purposes")] public async Task SelectModChannel([Summary("#Channel")] ISocketMessageChannel channel) { @@ -61,7 +59,6 @@ namespace Geekbot.net.Commands.Admin } [Command("showleave", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Admin)] [Summary("Notify modchannel when someone leaves")] public async Task ShowLeave([Summary("true/false")] bool enabled) { @@ -88,7 +85,6 @@ namespace Geekbot.net.Commands.Admin } [Command("showdel", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Admin)] [Summary("Notify modchannel when someone deletes a message")] public async Task ShowDelete([Summary("true/false")] bool enabled) { @@ -117,7 +113,6 @@ namespace Geekbot.net.Commands.Admin } [Command("setlang", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Admin)] [Summary("Change the bots language")] public async Task SetLanguage([Summary("language")] string languageRaw) { @@ -142,7 +137,6 @@ namespace Geekbot.net.Commands.Admin } [Command("wiki", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Admin)] [Summary("Change the wikipedia instance (use lang code in xx.wikipedia.org)")] public async Task SetWikiLanguage([Summary("language")] string languageRaw) { @@ -160,7 +154,6 @@ namespace Geekbot.net.Commands.Admin } [Command("lang", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Admin)] [Summary("Change the bots language")] public async Task GetLanguage() { @@ -176,7 +169,6 @@ namespace Geekbot.net.Commands.Admin } [Command("ping", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Admin)] [Summary("Enable the ping reply.")] public async Task TogglePing() { diff --git a/Geekbot.net/Commands/Admin/Mod.cs b/Geekbot.net/Commands/Admin/Mod.cs index 6f79157..b34b328 100644 --- a/Geekbot.net/Commands/Admin/Mod.cs +++ b/Geekbot.net/Commands/Admin/Mod.cs @@ -32,7 +32,6 @@ namespace Geekbot.net.Commands.Admin } [Command("namehistory", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Admin)] [Summary("See past usernames of an user")] public async Task UsernameHistory([Summary("@user")] IUser user) { @@ -52,7 +51,6 @@ namespace Geekbot.net.Commands.Admin } [Command("kick", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Admin)] [Summary("Ban a user")] public async Task Kick([Summary("@user")] IUser userNormal, [Summary("reason")] [Remainder] string reason = "none") diff --git a/Geekbot.net/Commands/Admin/Owner.cs b/Geekbot.net/Commands/Admin/Owner.cs index a1a98d6..2f4e3ab 100644 --- a/Geekbot.net/Commands/Admin/Owner.cs +++ b/Geekbot.net/Commands/Admin/Owner.cs @@ -52,7 +52,6 @@ namespace Geekbot.net.Commands.Admin } [Command("youtubekey", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Admin)] [Summary("Set the youtube api key")] public async Task SetYoutubeKey([Summary("API Key")] string key) { @@ -61,7 +60,6 @@ namespace Geekbot.net.Commands.Admin } [Command("game", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Admin)] [Summary("Set the game that the bot is playing")] public async Task SetGame([Remainder] [Summary("Game")] string key) { @@ -72,7 +70,6 @@ namespace Geekbot.net.Commands.Admin } [Command("popuserrepo", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Admin)] [Summary("Populate user cache")] public async Task PopUserRepoCommand() { @@ -104,7 +101,6 @@ namespace Geekbot.net.Commands.Admin } [Command("error", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Admin)] [Summary("Throw an error un purpose")] public void PurposefulError() { diff --git a/Geekbot.net/Commands/Admin/Role.cs b/Geekbot.net/Commands/Admin/Role.cs index 24d19dd..8624e66 100644 --- a/Geekbot.net/Commands/Admin/Role.cs +++ b/Geekbot.net/Commands/Admin/Role.cs @@ -27,7 +27,6 @@ namespace Geekbot.net.Commands.Admin } [Command(RunMode = RunMode.Async)] - [Remarks(CommandCategories.Helpers)] [Summary("Get a list of all available roles.")] public async Task GetAllRoles() { @@ -53,7 +52,6 @@ namespace Geekbot.net.Commands.Admin } [Command(RunMode = RunMode.Async)] - [Remarks(CommandCategories.Helpers)] [Summary("Get a role by mentioning it.")] public async Task GiveRole([Summary("roleNickname")] string roleNameRaw) { @@ -97,7 +95,6 @@ namespace Geekbot.net.Commands.Admin [RequireUserPermission(GuildPermission.ManageRoles)] [Command("add", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Admin)] [Summary("Add a role to the whitelist.")] public async Task AddRole([Summary("@role")] IRole role, [Summary("alias")] string roleName) { @@ -132,7 +129,6 @@ namespace Geekbot.net.Commands.Admin [RequireUserPermission(GuildPermission.ManageRoles)] [Command("remove", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Admin)] [Summary("Remove a role from the whitelist.")] public async Task RemoveRole([Summary("roleNickname")] string roleName) { @@ -156,7 +152,6 @@ namespace Geekbot.net.Commands.Admin } [RequireUserPermission(GuildPermission.ManageRoles)] - [Remarks(CommandCategories.Admin)] [Summary("Give a role by clicking on an emoji")] [Command("listen", RunMode = RunMode.Async)] public async Task AddListener([Summary("messageID")] string messageId, [Summary("Emoji")] string emoji, [Summary("@role")] IRole role) diff --git a/Geekbot.net/Commands/Admin/Say.cs b/Geekbot.net/Commands/Admin/Say.cs index 97c7a0f..f8708cd 100644 --- a/Geekbot.net/Commands/Admin/Say.cs +++ b/Geekbot.net/Commands/Admin/Say.cs @@ -18,7 +18,6 @@ namespace Geekbot.net.Commands.Admin [RequireUserPermission(GuildPermission.Administrator)] [Command("say", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Admin)] [Summary("Say Something.")] public async Task Echo([Remainder] [Summary("What?")] string echo) { diff --git a/Geekbot.net/Commands/Games/Pokedex.cs b/Geekbot.net/Commands/Games/Pokedex.cs index 57f842a..d4cbc0e 100644 --- a/Geekbot.net/Commands/Games/Pokedex.cs +++ b/Geekbot.net/Commands/Games/Pokedex.cs @@ -19,7 +19,6 @@ namespace Geekbot.net.Commands.Games } [Command("pokedex", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Helpers)] [Summary("A Pokedex Tool")] public async Task GetPokemon([Summary("pokemonName")] string pokemonName) { diff --git a/Geekbot.net/Commands/Games/Roll.cs b/Geekbot.net/Commands/Games/Roll.cs index 30697df..59a3c27 100644 --- a/Geekbot.net/Commands/Games/Roll.cs +++ b/Geekbot.net/Commands/Games/Roll.cs @@ -22,7 +22,6 @@ namespace Geekbot.net.Commands.Games } [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") { diff --git a/Geekbot.net/Commands/Integrations/Google/Google.cs b/Geekbot.net/Commands/Integrations/Google/Google.cs index 7bd3a56..4c86de3 100644 --- a/Geekbot.net/Commands/Integrations/Google/Google.cs +++ b/Geekbot.net/Commands/Integrations/Google/Google.cs @@ -23,7 +23,6 @@ namespace Geekbot.net.Commands.Integrations.Google } [Command("google", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Helpers)] [Summary("Google Something.")] public async Task AskGoogle([Remainder, Summary("SearchText")] string searchText) { diff --git a/Geekbot.net/Commands/Integrations/MagicTheGathering.cs b/Geekbot.net/Commands/Integrations/MagicTheGathering.cs index 3074803..842c9de 100644 --- a/Geekbot.net/Commands/Integrations/MagicTheGathering.cs +++ b/Geekbot.net/Commands/Integrations/MagicTheGathering.cs @@ -23,7 +23,6 @@ namespace Geekbot.net.Commands.Integrations } [Command("mtg", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Games)] [Summary("Find a Magic The Gathering Card.")] public async Task GetCard([Remainder] [Summary("name")] string cardName) { diff --git a/Geekbot.net/Commands/Integrations/Mal.cs b/Geekbot.net/Commands/Integrations/Mal.cs index d70949a..36c6457 100644 --- a/Geekbot.net/Commands/Integrations/Mal.cs +++ b/Geekbot.net/Commands/Integrations/Mal.cs @@ -21,7 +21,6 @@ namespace Geekbot.net.Commands.Integrations } [Command("anime", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Helpers)] [Summary("Show Info about an Anime.")] public async Task SearchAnime([Remainder] [Summary("AnimeName")] string animeName) { @@ -70,7 +69,6 @@ namespace Geekbot.net.Commands.Integrations } [Command("manga", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Helpers)] [Summary("Show Info about a Manga.")] public async Task SearchManga([Remainder] [Summary("MangaName")] string mangaName) { diff --git a/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs b/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs index 44402d0..3ad37f3 100644 --- a/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs +++ b/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs @@ -20,7 +20,6 @@ namespace Geekbot.net.Commands.Integrations.UbranDictionary } [Command("urban", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Helpers)] [Summary("Lookup something on urban dictionary")] public async Task UrbanDefine([Remainder] [Summary("word")] string word) { diff --git a/Geekbot.net/Commands/Integrations/Wikipedia.cs b/Geekbot.net/Commands/Integrations/Wikipedia.cs index 68589f6..e2e8928 100644 --- a/Geekbot.net/Commands/Integrations/Wikipedia.cs +++ b/Geekbot.net/Commands/Integrations/Wikipedia.cs @@ -28,7 +28,6 @@ namespace Geekbot.net.Commands.Integrations } [Command("wiki", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Helpers)] [Summary("Get an article from wikipedia.")] public async Task GetPreview([Remainder] [Summary("Article")] string articleName) { diff --git a/Geekbot.net/Commands/Integrations/Youtube.cs b/Geekbot.net/Commands/Integrations/Youtube.cs index f534bcd..a5d681e 100644 --- a/Geekbot.net/Commands/Integrations/Youtube.cs +++ b/Geekbot.net/Commands/Integrations/Youtube.cs @@ -21,7 +21,6 @@ namespace Geekbot.net.Commands.Integrations } [Command("yt", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Helpers)] [Summary("Search for something on youtube.")] public async Task Yt([Remainder] [Summary("Title")] string searchQuery) { diff --git a/Geekbot.net/Commands/Randomness/Cat/Cat.cs b/Geekbot.net/Commands/Randomness/Cat/Cat.cs index 88a9ce5..a79eb37 100644 --- a/Geekbot.net/Commands/Randomness/Cat/Cat.cs +++ b/Geekbot.net/Commands/Randomness/Cat/Cat.cs @@ -19,7 +19,6 @@ namespace Geekbot.net.Commands.Randomness.Cat } [Command("cat", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Randomness)] [Summary("Return a random image of a cat.")] public async Task Say() { diff --git a/Geekbot.net/Commands/Randomness/CheckEm.cs b/Geekbot.net/Commands/Randomness/CheckEm.cs index fc962cc..325d7cc 100644 --- a/Geekbot.net/Commands/Randomness/CheckEm.cs +++ b/Geekbot.net/Commands/Randomness/CheckEm.cs @@ -21,7 +21,6 @@ namespace Geekbot.net.Commands.Randomness } [Command("checkem", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Randomness)] [Summary("Check for dubs")] public async Task MuhDubs() { diff --git a/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs b/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs index 6ff2255..8e8a369 100644 --- a/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs +++ b/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs @@ -19,7 +19,6 @@ namespace Geekbot.net.Commands.Randomness.Chuck } [Command("chuck", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Randomness)] [Summary("A random chuck norris joke")] public async Task Say() { diff --git a/Geekbot.net/Commands/Randomness/Dad/DadJokes.cs b/Geekbot.net/Commands/Randomness/Dad/DadJokes.cs index e35798c..a14a098 100644 --- a/Geekbot.net/Commands/Randomness/Dad/DadJokes.cs +++ b/Geekbot.net/Commands/Randomness/Dad/DadJokes.cs @@ -19,7 +19,6 @@ namespace Geekbot.net.Commands.Randomness.Dad } [Command("dad", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Randomness)] [Summary("A random dad joke")] public async Task Say() { diff --git a/Geekbot.net/Commands/Randomness/Dog/Dog.cs b/Geekbot.net/Commands/Randomness/Dog/Dog.cs index 08c1a9b..83d7a2e 100644 --- a/Geekbot.net/Commands/Randomness/Dog/Dog.cs +++ b/Geekbot.net/Commands/Randomness/Dog/Dog.cs @@ -19,7 +19,6 @@ namespace Geekbot.net.Commands.Randomness.Dog } [Command("dog", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Randomness)] [Summary("Return a random image of a dog.")] public async Task Say() { diff --git a/Geekbot.net/Commands/Randomness/EightBall.cs b/Geekbot.net/Commands/Randomness/EightBall.cs index 79ba6d1..cf59431 100644 --- a/Geekbot.net/Commands/Randomness/EightBall.cs +++ b/Geekbot.net/Commands/Randomness/EightBall.cs @@ -17,7 +17,6 @@ namespace Geekbot.net.Commands.Randomness } [Command("8ball", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Randomness)] [Summary("Ask 8Ball a Question.")] public async Task Ball([Remainder] [Summary("Question")] string echo) { diff --git a/Geekbot.net/Commands/Randomness/Fortune.cs b/Geekbot.net/Commands/Randomness/Fortune.cs index abc9ce5..498085a 100644 --- a/Geekbot.net/Commands/Randomness/Fortune.cs +++ b/Geekbot.net/Commands/Randomness/Fortune.cs @@ -15,7 +15,6 @@ namespace Geekbot.net.Commands.Randomness } [Command("fortune", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Randomness)] [Summary("Get a random fortune")] public async Task GetAFortune() { diff --git a/Geekbot.net/Commands/Randomness/Gdq.cs b/Geekbot.net/Commands/Randomness/Gdq.cs index 3e17451..8d0472c 100644 --- a/Geekbot.net/Commands/Randomness/Gdq.cs +++ b/Geekbot.net/Commands/Randomness/Gdq.cs @@ -17,7 +17,6 @@ namespace Geekbot.net.Commands.Randomness } [Command("gdq", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Games)] [Summary("Get a quote from the GDQ donation generator.")] public async Task GetQuote() { diff --git a/Geekbot.net/Commands/Randomness/RandomAnimals.cs b/Geekbot.net/Commands/Randomness/RandomAnimals.cs index a96aea8..608b52d 100644 --- a/Geekbot.net/Commands/Randomness/RandomAnimals.cs +++ b/Geekbot.net/Commands/Randomness/RandomAnimals.cs @@ -16,7 +16,6 @@ namespace Geekbot.net.Commands.Randomness } [Command("panda", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Randomness)] [Summary("Get a random panda image")] public async Task Panda() { @@ -25,7 +24,6 @@ namespace Geekbot.net.Commands.Randomness [Command("croissant", RunMode = RunMode.Async)] [Alias("gipfeli")] - [Remarks(CommandCategories.Randomness)] [Summary("Get a random croissant image")] public async Task Croissant() { @@ -33,7 +31,6 @@ namespace Geekbot.net.Commands.Randomness } [Command("pumpkin", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Randomness)] [Summary("Get a random pumpkin image")] public async Task Pumpkin() { @@ -41,7 +38,6 @@ namespace Geekbot.net.Commands.Randomness } [Command("squirrel", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Randomness)] [Summary("Get a random squirrel image")] public async Task Squirrel() { @@ -49,7 +45,6 @@ namespace Geekbot.net.Commands.Randomness } [Command("turtle", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Randomness)] [Summary("Get a random turtle image")] public async Task Turtle() { @@ -58,7 +53,6 @@ namespace Geekbot.net.Commands.Randomness [Command("pinguin", RunMode = RunMode.Async)] [Alias("pingu")] - [Remarks(CommandCategories.Randomness)] [Summary("Get a random pinguin image")] public async Task Pinguin() { @@ -66,7 +60,6 @@ namespace Geekbot.net.Commands.Randomness } [Command("fox", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Randomness)] [Summary("Get a random fox image")] public async Task Fox() { diff --git a/Geekbot.net/Commands/Randomness/Ship.cs b/Geekbot.net/Commands/Randomness/Ship.cs index ffa18b4..735bd51 100644 --- a/Geekbot.net/Commands/Randomness/Ship.cs +++ b/Geekbot.net/Commands/Randomness/Ship.cs @@ -20,7 +20,6 @@ namespace Geekbot.net.Commands.Randomness } [Command("Ship", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Fun)] [Summary("Ask the Shipping meter")] public async Task Command([Summary("@User1")] IUser user1, [Summary("@User2")] IUser user2) { diff --git a/Geekbot.net/Commands/Randomness/Slap.cs b/Geekbot.net/Commands/Randomness/Slap.cs index 129e89d..2cac9d2 100644 --- a/Geekbot.net/Commands/Randomness/Slap.cs +++ b/Geekbot.net/Commands/Randomness/Slap.cs @@ -21,7 +21,6 @@ namespace Geekbot.net.Commands.Randomness } [Command("slap", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Fun)] [Summary("slap someone")] public async Task Slapper([Summary("@user")] IUser user) { diff --git a/Geekbot.net/Commands/User/GuildInfo.cs b/Geekbot.net/Commands/User/GuildInfo.cs index dabe005..c475d99 100644 --- a/Geekbot.net/Commands/User/GuildInfo.cs +++ b/Geekbot.net/Commands/User/GuildInfo.cs @@ -25,7 +25,6 @@ namespace Geekbot.net.Commands.User } [Command("serverstats", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Statistics)] [Summary("Show some info about the bot.")] public async Task GetInfo() { diff --git a/Geekbot.net/Commands/User/Karma.cs b/Geekbot.net/Commands/User/Karma.cs index b63951e..2f427c8 100644 --- a/Geekbot.net/Commands/User/Karma.cs +++ b/Geekbot.net/Commands/User/Karma.cs @@ -26,7 +26,6 @@ namespace Geekbot.net.Commands.User } [Command("good", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Karma)] [Summary("Increase Someones Karma")] public async Task Good([Summary("@someone")] IUser user) { @@ -74,7 +73,6 @@ namespace Geekbot.net.Commands.User } [Command("bad", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Karma)] [Summary("Decrease Someones Karma")] public async Task Bad([Summary("@someone")] IUser user) { diff --git a/Geekbot.net/Commands/User/Ranking/Rank.cs b/Geekbot.net/Commands/User/Ranking/Rank.cs index 9b1b32f..dd1d026 100644 --- a/Geekbot.net/Commands/User/Ranking/Rank.cs +++ b/Geekbot.net/Commands/User/Ranking/Rank.cs @@ -36,7 +36,6 @@ namespace Geekbot.net.Commands.User.Ranking } [Command("rank", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Statistics)] [Summary("get user top 10 in messages or karma")] public async Task RankCmd([Summary("type")] string typeUnformated = "messages", [Summary("amount")] int amount = 10) { diff --git a/Geekbot.net/Commands/User/Stats.cs b/Geekbot.net/Commands/User/Stats.cs index 19a6aa8..93bddef 100644 --- a/Geekbot.net/Commands/User/Stats.cs +++ b/Geekbot.net/Commands/User/Stats.cs @@ -23,7 +23,6 @@ namespace Geekbot.net.Commands.User } [Command("stats", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Statistics)] [Summary("Get information about this user")] public async Task User([Summary("@someone")] IUser user = null) { diff --git a/Geekbot.net/Commands/Utils/AvatarGetter.cs b/Geekbot.net/Commands/Utils/AvatarGetter.cs index d42c779..cf34ca3 100644 --- a/Geekbot.net/Commands/Utils/AvatarGetter.cs +++ b/Geekbot.net/Commands/Utils/AvatarGetter.cs @@ -17,7 +17,6 @@ namespace Geekbot.net.Commands.Utils } [Command("avatar", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Helpers)] [Summary("Get someones avatar")] public async Task GetAvatar([Remainder] [Summary("user")] IUser user = null) { diff --git a/Geekbot.net/Commands/Utils/Changelog/Changelog.cs b/Geekbot.net/Commands/Utils/Changelog/Changelog.cs index f1752f7..c218c66 100644 --- a/Geekbot.net/Commands/Utils/Changelog/Changelog.cs +++ b/Geekbot.net/Commands/Utils/Changelog/Changelog.cs @@ -25,9 +25,7 @@ namespace Geekbot.net.Commands.Utils.Changelog } [Command("changelog", RunMode = RunMode.Async)] - [Alias("updates")] - [Remarks(CommandCategories.Helpers)] - [Summary("Show the latest 5 updates")] + [Summary("Show the latest 10 updates")] public async Task GetChangelog() { try diff --git a/Geekbot.net/Commands/Utils/Choose.cs b/Geekbot.net/Commands/Utils/Choose.cs index 4d54654..2b9ff19 100644 --- a/Geekbot.net/Commands/Utils/Choose.cs +++ b/Geekbot.net/Commands/Utils/Choose.cs @@ -19,7 +19,6 @@ namespace Geekbot.net.Commands.Utils } [Command("choose", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Helpers)] [Summary("Let the bot choose for you, seperate options with a semicolon.")] public async Task Command([Remainder] [Summary("option1;option2")] string choices) diff --git a/Geekbot.net/Commands/Utils/Dice/Dice.cs b/Geekbot.net/Commands/Utils/Dice/Dice.cs index 9c169c7..c8e31f5 100644 --- a/Geekbot.net/Commands/Utils/Dice/Dice.cs +++ b/Geekbot.net/Commands/Utils/Dice/Dice.cs @@ -11,7 +11,6 @@ namespace Geekbot.net.Commands.Utils.Dice public class Dice : ModuleBase { [Command("dice", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Randomness)] [Summary("Roll a dice.")] public async Task RollCommand([Remainder] [Summary("diceType")] string diceType = "1d20") { diff --git a/Geekbot.net/Commands/Utils/Emojify.cs b/Geekbot.net/Commands/Utils/Emojify.cs index 57363c2..07adadb 100644 --- a/Geekbot.net/Commands/Utils/Emojify.cs +++ b/Geekbot.net/Commands/Utils/Emojify.cs @@ -19,7 +19,6 @@ namespace Geekbot.net.Commands.Utils } [Command("emojify", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Helpers)] [Summary("Emojify text")] public async Task Dflt([Remainder] [Summary("text")] string text) { diff --git a/Geekbot.net/Commands/Utils/Help.cs b/Geekbot.net/Commands/Utils/Help.cs index be9a747..4372692 100644 --- a/Geekbot.net/Commands/Utils/Help.cs +++ b/Geekbot.net/Commands/Utils/Help.cs @@ -18,7 +18,6 @@ namespace Geekbot.net.Commands.Utils } [Command("help", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Helpers)] [Summary("List all Commands")] public async Task GetHelp() { diff --git a/Geekbot.net/Commands/Utils/Info.cs b/Geekbot.net/Commands/Utils/Info.cs index de34e89..55615ea 100644 --- a/Geekbot.net/Commands/Utils/Info.cs +++ b/Geekbot.net/Commands/Utils/Info.cs @@ -27,7 +27,6 @@ namespace Geekbot.net.Commands.Utils } [Command("info", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Helpers)] [Summary("Get Information about the bot")] public async Task BotInfo() { @@ -59,7 +58,6 @@ namespace Geekbot.net.Commands.Utils } [Command("uptime", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Helpers)] [Summary("Get the Bot Uptime")] public async Task BotUptime() { diff --git a/Geekbot.net/Commands/Utils/Ping.cs b/Geekbot.net/Commands/Utils/Ping.cs index 226e9a3..653a46f 100644 --- a/Geekbot.net/Commands/Utils/Ping.cs +++ b/Geekbot.net/Commands/Utils/Ping.cs @@ -8,7 +8,6 @@ namespace Geekbot.net.Commands.Utils { [Command("👀", RunMode = RunMode.Async)] [Summary("Look at the bot.")] - [Remarks(CommandCategories.Fun)] public async Task Eyes() { await ReplyAsync("S... Stop looking at me... baka!"); diff --git a/Geekbot.net/Commands/Utils/Poll/Poll.cs b/Geekbot.net/Commands/Utils/Poll/Poll.cs index 5d18a0f..e94e6d4 100644 --- a/Geekbot.net/Commands/Utils/Poll/Poll.cs +++ b/Geekbot.net/Commands/Utils/Poll/Poll.cs @@ -32,7 +32,6 @@ namespace Geekbot.net.Commands.Utils.Poll } [Command(RunMode = RunMode.Async)] - [Remarks(CommandCategories.Helpers)] [Summary("Check status of the current poll")] public async Task Dflt() { @@ -55,7 +54,6 @@ namespace Geekbot.net.Commands.Utils.Poll } [Command("create", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Helpers)] [Summary("Create a poll")] public async Task Create([Remainder] [Summary("question;option1;option2")] string rawPollString) @@ -113,7 +111,6 @@ namespace Geekbot.net.Commands.Utils.Poll } [Command("end", RunMode = RunMode.Async)] - [Remarks(CommandCategories.Helpers)] [Summary("End the current poll")] public async Task End() { diff --git a/Geekbot.net/Commands/Utils/Quote/Quote.cs b/Geekbot.net/Commands/Utils/Quote/Quote.cs index 71c4089..ef0cbc8 100644 --- a/Geekbot.net/Commands/Utils/Quote/Quote.cs +++ b/Geekbot.net/Commands/Utils/Quote/Quote.cs @@ -26,7 +26,6 @@ namespace Geekbot.net.Commands.Utils.Quote } [Command] - [Remarks(CommandCategories.Quotes)] [Summary("Return a random quoute from the database")] public async Task GetRandomQuote() { @@ -53,7 +52,6 @@ namespace Geekbot.net.Commands.Utils.Quote } [Command("save")] - [Remarks(CommandCategories.Quotes)] [Summary("Save a quote from the last sent message by @user")] public async Task SaveQuote([Summary("@user")] IUser user) { @@ -89,7 +87,6 @@ namespace Geekbot.net.Commands.Utils.Quote } [Command("save")] - [Remarks(CommandCategories.Quotes)] [Summary("Save a quote from a message id")] public async Task SaveQuote([Summary("messageId")] ulong messageId) { @@ -123,7 +120,6 @@ namespace Geekbot.net.Commands.Utils.Quote } [Command("make")] - [Remarks(CommandCategories.Quotes)] [Summary("Create a quote from the last sent message by @user")] public async Task ReturnSpecifiedQuote([Summary("@user")] IUser user) { @@ -143,7 +139,6 @@ namespace Geekbot.net.Commands.Utils.Quote } [Command("make")] - [Remarks(CommandCategories.Quotes)] [Summary("Create a quote from a message id")] public async Task ReturnSpecifiedQuote([Summary("messageId")] ulong messageId) { @@ -165,7 +160,6 @@ namespace Geekbot.net.Commands.Utils.Quote [RequireUserPermission(GuildPermission.KickMembers)] [RequireUserPermission(GuildPermission.ManageMessages)] [RequireUserPermission(GuildPermission.ManageRoles)] - [Remarks(CommandCategories.Quotes)] [Summary("Remove a quote (required mod permissions)")] public async Task RemoveQuote([Summary("quoteId")] int id) { diff --git a/Geekbot.net/Lib/CommandCategories.cs b/Geekbot.net/Lib/CommandCategories.cs deleted file mode 100644 index d67123b..0000000 --- a/Geekbot.net/Lib/CommandCategories.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace Geekbot.net.Lib -{ - public static class CommandCategories - { - public const string Randomness = "Randomness"; - public const string Karma = "Karma"; - public const string Quotes = "Quotes"; - public const string Fun = "Fun"; - public const string Statistics = "Statistics"; - public const string Helpers = "Helpers"; - public const string Games = "Games"; - public const string Admin = "Admin"; - public const string Uncategorized = "Uncategorized"; - } -} \ No newline at end of file diff --git a/Geekbot.net/WebApi/Help/HelpController.cs b/Geekbot.net/WebApi/Help/HelpController.cs index d71d26f..015c7d8 100644 --- a/Geekbot.net/WebApi/Help/HelpController.cs +++ b/Geekbot.net/WebApi/Help/HelpController.cs @@ -28,7 +28,6 @@ namespace Geekbot.net.WebApi.Help { Name = cmd.Name, Summary = cmd.Summary, - Category = cmd.Remarks ?? CommandCategories.Uncategorized, IsAdminCommand = (param.Contains("admin")), Aliases = cmd.Aliases.ToArray(), Params = cmdParamsObj From bb8aee1eda8e0923587631555293ca2dffc69446 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 13 May 2018 17:49:13 +0200 Subject: [PATCH 067/443] Port !owner, !admin, !mod, handlers (partial) and add globals to the db --- Geekbot.net/Commands/Admin/Admin.cs | 183 ++++++++++++------ Geekbot.net/Commands/Admin/Mod.cs | 41 +--- Geekbot.net/Commands/Admin/Owner.cs | 11 +- .../Commands/Integrations/Google/Google.cs | 21 +- Geekbot.net/Commands/Integrations/Youtube.cs | 15 +- Geekbot.net/Commands/Utils/Info.cs | 7 +- Geekbot.net/Handlers.cs | 77 ++++---- .../Lib/GlobalSettings/GlobalSettings.cs | 55 ++++++ .../Lib/GlobalSettings/IGlobalSettings.cs | 11 ++ Geekbot.net/Program.cs | 18 +- 10 files changed, 265 insertions(+), 174 deletions(-) create mode 100644 Geekbot.net/Lib/GlobalSettings/GlobalSettings.cs create mode 100644 Geekbot.net/Lib/GlobalSettings/IGlobalSettings.cs diff --git a/Geekbot.net/Commands/Admin/Admin.cs b/Geekbot.net/Commands/Admin/Admin.cs index 7b5198d..f3366a6 100644 --- a/Geekbot.net/Commands/Admin/Admin.cs +++ b/Geekbot.net/Commands/Admin/Admin.cs @@ -1,11 +1,14 @@ using System; +using System.Linq; using System.Text; using System.Threading.Tasks; using Discord; using Discord.Commands; using Discord.WebSocket; -using Geekbot.net.Lib; +using Geekbot.net.Database; +using Geekbot.net.Database.Models; using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Localization; using StackExchange.Redis; @@ -17,13 +20,13 @@ namespace Geekbot.net.Commands.Admin { private readonly DiscordSocketClient _client; private readonly IErrorHandler _errorHandler; - private readonly IDatabase _redis; + private readonly DatabaseContext _database; private readonly ITranslationHandler _translation; - public Admin(IDatabase redis, DiscordSocketClient client, IErrorHandler errorHandler, + public Admin(DatabaseContext database, DiscordSocketClient client, IErrorHandler errorHandler, ITranslationHandler translationHandler) { - _redis = redis; + _database = database; _client = client; _errorHandler = errorHandler; _translation = translationHandler; @@ -33,7 +36,11 @@ namespace Geekbot.net.Commands.Admin [Summary("Set a Welcome Message (use '$user' to mention the new joined user).")] public async Task SetWelcomeMessage([Remainder] [Summary("message")] string welcomeMessage) { - _redis.HashSet($"{Context.Guild.Id}:Settings", new[] {new HashEntry("WelcomeMsg", welcomeMessage)}); + var guild = GetGuildSettings(Context.Guild.Id); + guild.WelcomeMessage = welcomeMessage; + _database.GuildSettings.Update(guild); + _database.SaveChanges(); + var formatedMessage = welcomeMessage.Replace("$user", Context.User.Mention); await ReplyAsync($"Welcome message has been changed\r\nHere is an example of how it would look:\r\n{formatedMessage}"); } @@ -44,13 +51,18 @@ namespace Geekbot.net.Commands.Admin { try { + var m = await channel.SendMessageAsync("verifying..."); + + var guild = GetGuildSettings(Context.Guild.Id); + guild.ModChannel = channel.Id.AsLong(); + _database.GuildSettings.Update(guild); + _database.SaveChanges(); + var sb = new StringBuilder(); sb.AppendLine("Successfully saved mod channel, you can now do the following"); - sb.AppendLine("- `!admin showleave true` - send message to mod channel when someone leaves"); - sb.AppendLine("- `!admin showdel true` - send message to mod channel when someone deletes a message"); - await channel.SendMessageAsync(sb.ToString()); - _redis.HashSet($"{Context.Guild.Id}:Settings", - new[] {new HashEntry("ModChannel", channel.Id.ToString())}); + sb.AppendLine("- `!admin showleave` - send message to mod channel when someone leaves"); + sb.AppendLine("- `!admin showdel` - send message to mod channel when someone deletes a message"); + await m.ModifyAsync(e => e.Content = sb.ToString()); } catch (Exception e) { @@ -59,56 +71,50 @@ namespace Geekbot.net.Commands.Admin } [Command("showleave", RunMode = RunMode.Async)] - [Summary("Notify modchannel when someone leaves")] - public async Task ShowLeave([Summary("true/false")] bool enabled) + [Summary("Toggle - notify modchannel when someone leaves")] + public async Task ShowLeave() { - var modChannelId = ulong.Parse(_redis.HashGet($"{Context.Guild.Id}:Settings", "ModChannel")); try { - var modChannel = (ISocketMessageChannel) _client.GetChannel(modChannelId); - if (enabled) - { - await modChannel.SendMessageAsync("Saved - now sending messages here when someone leaves"); - _redis.HashSet($"{Context.Guild.Id}:Settings", new[] {new HashEntry("ShowLeave", true)}); - } - else - { - await modChannel.SendMessageAsync("Saved - stopping sending messages here when someone leaves"); - _redis.HashSet($"{Context.Guild.Id}:Settings", new[] {new HashEntry("ShowLeave", false)}); - } + var guild = GetGuildSettings(Context.Guild.Id); + var modChannel = await GetModChannel(guild.ModChannel.AsUlong()); + if (modChannel == null) return; + + guild.ShowLeave = !guild.ShowLeave; + _database.GuildSettings.Update(guild); + _database.SaveChanges(); + await modChannel.SendMessageAsync(guild.ShowLeave + ? "Saved - now sending messages here when someone leaves" + : "Saved - stopping sending messages here when someone leaves" + ); } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context, - "Modchannel doesn't seem to exist, please set one with `!admin modchannel [channelId]`"); + _errorHandler.HandleCommandException(e, Context); } } [Command("showdel", RunMode = RunMode.Async)] - [Summary("Notify modchannel when someone deletes a message")] - public async Task ShowDelete([Summary("true/false")] bool enabled) + [Summary("Toggle - notify modchannel when someone deletes a message")] + public async Task ShowDelete() { - var modChannelId = ulong.Parse(_redis.HashGet($"{Context.Guild.Id}:Settings", "ModChannel")); try { - var modChannel = (ISocketMessageChannel) _client.GetChannel(modChannelId); - if (enabled) - { - await modChannel.SendMessageAsync( - "Saved - now sending messages here when someone deletes a message"); - _redis.HashSet($"{Context.Guild.Id}:Settings", new[] {new HashEntry("ShowDelete", true)}); - } - else - { - await modChannel.SendMessageAsync( - "Saved - stopping sending messages here when someone deletes a message"); - _redis.HashSet($"{Context.Guild.Id}:Settings", new[] {new HashEntry("ShowDelete", false)}); - } + var guild = GetGuildSettings(Context.Guild.Id); + var modChannel = await GetModChannel(guild.ModChannel.AsUlong()); + if (modChannel == null) return; + + guild.ShowDelete = !guild.ShowDelete; + _database.GuildSettings.Update(guild); + _database.SaveChanges(); + await modChannel.SendMessageAsync(guild.ShowDelete + ? "Saved - now sending messages here when someone deletes a message" + : "Saved - stopping sending messages here when someone deletes a message" + ); } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context, - "Modchannel doesn't seem to exist, please set one with `!admin modchannel [channelId]`"); + _errorHandler.HandleCommandException(e, Context); } } @@ -122,6 +128,11 @@ namespace Geekbot.net.Commands.Admin var success = _translation.SetLanguage(Context.Guild.Id, language); if (success) { + var guild = GetGuildSettings(Context.Guild.Id); + guild.Language = language; + _database.GuildSettings.Update(guild); + _database.SaveChanges(); + var trans = _translation.GetDict(Context); await ReplyAsync(trans["NewLanguageSet"]); return; @@ -143,8 +154,11 @@ namespace Geekbot.net.Commands.Admin try { var language = languageRaw.ToLower(); - _redis.HashSet($"{Context.Guild.Id}:Settings", new[] {new HashEntry("WikiLang", language) }); - + var guild = GetGuildSettings(Context.Guild.Id); + guild.WikiLang = language; + _database.GuildSettings.Update(guild); + _database.SaveChanges(); + await ReplyAsync($"Now using the {language} wikipedia"); } catch (Exception e) @@ -152,21 +166,6 @@ namespace Geekbot.net.Commands.Admin _errorHandler.HandleCommandException(e, Context); } } - - [Command("lang", RunMode = RunMode.Async)] - [Summary("Change the bots language")] - public async Task GetLanguage() - { - try - { - var trans = _translation.GetDict(Context); - await ReplyAsync(trans["GetLanguage"]); - } - catch (Exception e) - { - _errorHandler.HandleCommandException(e, Context); - } - } [Command("ping", RunMode = RunMode.Async)] [Summary("Enable the ping reply.")] @@ -174,9 +173,11 @@ namespace Geekbot.net.Commands.Admin { try { - bool.TryParse(_redis.HashGet($"{Context.Guild.Id}:Settings", "ping"), out var current); - _redis.HashSet($"{Context.Guild.Id}:Settings", new[] {new HashEntry("ping", current ? "false" : "true") }); - await ReplyAsync(!current ? "i will reply to ping now" : "No more pongs..."); + var guild = GetGuildSettings(Context.Guild.Id); + guild.Ping = !guild.Ping; + _database.GuildSettings.Update(guild); + _database.SaveChanges(); + await ReplyAsync(guild.Ping ? "i will reply to ping now" : "No more pongs..."); } catch (Exception e) { @@ -184,5 +185,59 @@ namespace Geekbot.net.Commands.Admin } } + [Command("hui", RunMode = RunMode.Async)] + [Summary("Enable the ping reply.")] + public async Task ToggleHui() + { + try + { + var guild = GetGuildSettings(Context.Guild.Id); + guild.Hui = !guild.Hui; + _database.GuildSettings.Update(guild); + _database.SaveChanges(); + await ReplyAsync(guild.Hui ? "i will reply to hui now" : "No more hui's..."); + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + } + } + + private GuildSettingsModel GetGuildSettings(ulong guildId) + { + var guild = _database.GuildSettings.FirstOrDefault(g => g.GuildId.Equals(guildId.AsLong())); + if (guild != null) return guild; + Console.WriteLine("Adding non-exist Guild Settings to database"); + _database.GuildSettings.Add(new GuildSettingsModel() + { + GuildId = guildId.AsLong(), + Hui = false, + Ping = false, + Language = "en", + ShowDelete = false, + ShowLeave = false, + WikiLang = "en" + }); + _database.SaveChanges(); + return _database.GuildSettings.FirstOrDefault(g => g.GuildId.Equals(guildId.AsLong())); + } + + private async Task GetModChannel(ulong channelId) + { + try + { + if(channelId == ulong.MinValue) throw new Exception(); + var modChannel = (ISocketMessageChannel) _client.GetChannel(channelId); + if(modChannel == null) throw new Exception(); + return modChannel; + } + catch + { + await ReplyAsync( + "Modchannel doesn't seem to exist, please set one with `!admin modchannel [channelId]`"); + return null; + } + } + } } \ No newline at end of file diff --git a/Geekbot.net/Commands/Admin/Mod.cs b/Geekbot.net/Commands/Admin/Mod.cs index b34b328..2e4ad24 100644 --- a/Geekbot.net/Commands/Admin/Mod.cs +++ b/Geekbot.net/Commands/Admin/Mod.cs @@ -4,10 +4,8 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Discord.WebSocket; -using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.UserRepository; -using StackExchange.Redis; namespace Geekbot.net.Commands.Admin { @@ -19,15 +17,12 @@ namespace Geekbot.net.Commands.Admin { private readonly DiscordSocketClient _client; private readonly IErrorHandler _errorHandler; - private readonly IDatabase _redis; private readonly IUserRepository _userRepository; - public Mod(IUserRepository userRepositry, IErrorHandler errorHandler, IDatabase redis, - DiscordSocketClient client) + public Mod(IUserRepository userRepositry, IErrorHandler errorHandler, DiscordSocketClient client) { _userRepository = userRepositry; _errorHandler = errorHandler; - _redis = redis; _client = client; } @@ -49,39 +44,5 @@ namespace Geekbot.net.Commands.Admin $"I don't have enough permissions do that"); } } - - [Command("kick", RunMode = RunMode.Async)] - [Summary("Ban a user")] - public async Task Kick([Summary("@user")] IUser userNormal, - [Summary("reason")] [Remainder] string reason = "none") - { - try - { - var user = (IGuildUser) userNormal; - if (reason == "none") reason = "No reason provided"; - await user.GetOrCreateDMChannelAsync().Result.SendMessageAsync( - $"You have been kicked from {Context.Guild.Name} for the following reason: \"{reason}\""); - await user.KickAsync(); - try - { - var modChannelId = ulong.Parse(_redis.HashGet($"{Context.Guild.Id}:Settings", "ModChannel")); - var modChannel = (ISocketMessageChannel) _client.GetChannel(modChannelId); - var eb = new EmbedBuilder(); - eb.Title = ":x: User Kicked"; - eb.AddInlineField("User", user.Username); - eb.AddInlineField("By Mod", Context.User.Username); - eb.AddField("Reason", reason); - await modChannel.SendMessageAsync("", false, eb.Build()); - } - catch - { - await ReplyAsync($"{user.Username} was kicked for the following reason: \"{reason}\""); - } - } - catch (Exception e) - { - _errorHandler.HandleCommandException(e, Context, "I don't have enough permissions to kick someone"); - } - } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/Admin/Owner.cs b/Geekbot.net/Commands/Admin/Owner.cs index 2f4e3ab..c02c7fb 100644 --- a/Geekbot.net/Commands/Admin/Owner.cs +++ b/Geekbot.net/Commands/Admin/Owner.cs @@ -1,11 +1,10 @@ using System; using System.Threading.Tasks; -using Discord; using Discord.Commands; using Discord.WebSocket; using Geekbot.net.Database; -using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.GlobalSettings; using Geekbot.net.Lib.Logger; using Geekbot.net.Lib.UserRepository; using StackExchange.Redis; @@ -19,11 +18,12 @@ namespace Geekbot.net.Commands.Admin private readonly DiscordSocketClient _client; private readonly IErrorHandler _errorHandler; private readonly DatabaseContext _database; + private readonly IGlobalSettings _globalSettings; private readonly IGeekbotLogger _logger; private readonly IDatabase _redis; private readonly IUserRepository _userRepository; - public Owner(IDatabase redis, DiscordSocketClient client, IGeekbotLogger logger, IUserRepository userRepositry, IErrorHandler errorHandler, DatabaseContext database) + public Owner(IDatabase redis, DiscordSocketClient client, IGeekbotLogger logger, IUserRepository userRepositry, IErrorHandler errorHandler, DatabaseContext database, IGlobalSettings globalSettings) { _redis = redis; _client = client; @@ -31,6 +31,7 @@ namespace Geekbot.net.Commands.Admin _userRepository = userRepositry; _errorHandler = errorHandler; _database = database; + _globalSettings = globalSettings; } [Command("migrate", RunMode = RunMode.Async)] @@ -55,7 +56,7 @@ namespace Geekbot.net.Commands.Admin [Summary("Set the youtube api key")] public async Task SetYoutubeKey([Summary("API Key")] string key) { - _redis.StringSet("youtubeKey", key); + _globalSettings.SetKey("YoutubeKey", key); await ReplyAsync("Apikey has been set"); } @@ -63,7 +64,7 @@ namespace Geekbot.net.Commands.Admin [Summary("Set the game that the bot is playing")] public async Task SetGame([Remainder] [Summary("Game")] string key) { - _redis.StringSet("Game", key); + _globalSettings.SetKey("Game", key); await _client.SetGameAsync(key); _logger.Information(LogSource.Geekbot, $"Changed game to {key}"); await ReplyAsync($"Now Playing {key}"); diff --git a/Geekbot.net/Commands/Integrations/Google/Google.cs b/Geekbot.net/Commands/Integrations/Google/Google.cs index 4c86de3..1ad7c8e 100644 --- a/Geekbot.net/Commands/Integrations/Google/Google.cs +++ b/Geekbot.net/Commands/Integrations/Google/Google.cs @@ -4,22 +4,21 @@ using System.Net; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.GlobalSettings; using Newtonsoft.Json; -using StackExchange.Redis; namespace Geekbot.net.Commands.Integrations.Google { public class Google : ModuleBase { private readonly IErrorHandler _errorHandler; - private readonly IDatabase _redis; + private readonly IGlobalSettings _globalSettings; - public Google(IErrorHandler errorHandler, IDatabase redis) + public Google(IErrorHandler errorHandler, IGlobalSettings globalSettings) { _errorHandler = errorHandler; - _redis = redis; + _globalSettings = globalSettings; } [Command("google", RunMode = RunMode.Async)] @@ -30,12 +29,12 @@ namespace Geekbot.net.Commands.Integrations.Google { using (var client = new WebClient()) { - var apiKey = _redis.StringGet("googleGraphKey"); - if (!apiKey.HasValue) + var apiKey = _globalSettings.GetKey("GoogleGraphKey"); + if (string.IsNullOrEmpty(apiKey)) { await ReplyAsync("No Google API key has been set, please contact my owner"); return; - } + } var url = new Uri($"https://kgsearch.googleapis.com/v1/entities:search?languages=en&limit=1&query={searchText}&key={apiKey}"); var responseString = client.DownloadString(url); @@ -48,8 +47,10 @@ namespace Geekbot.net.Commands.Integrations.Google } var data = response.ItemListElement.First().Result; - var eb = new EmbedBuilder(); - eb.Title = data.Name; + var eb = new EmbedBuilder + { + Title = data.Name + }; if(!string.IsNullOrEmpty(data.Description)) eb.WithDescription(data.Description); if(!string.IsNullOrEmpty(data.DetailedDtoDescription?.Url)) eb.WithUrl(data.DetailedDtoDescription.Url); if(!string.IsNullOrEmpty(data.DetailedDtoDescription?.ArticleBody)) eb.AddField("Details", data.DetailedDtoDescription.ArticleBody); diff --git a/Geekbot.net/Commands/Integrations/Youtube.cs b/Geekbot.net/Commands/Integrations/Youtube.cs index a5d681e..2e9fce0 100644 --- a/Geekbot.net/Commands/Integrations/Youtube.cs +++ b/Geekbot.net/Commands/Integrations/Youtube.cs @@ -1,22 +1,21 @@ using System; using System.Threading.Tasks; using Discord.Commands; -using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.GlobalSettings; using Google.Apis.Services; using Google.Apis.YouTube.v3; -using StackExchange.Redis; namespace Geekbot.net.Commands.Integrations { public class Youtube : ModuleBase { + private readonly IGlobalSettings _globalSettings; private readonly IErrorHandler _errorHandler; - private readonly IDatabase _redis; - public Youtube(IDatabase redis, IErrorHandler errorHandler) + public Youtube(IGlobalSettings globalSettings, IErrorHandler errorHandler) { - _redis = redis; + _globalSettings = globalSettings; _errorHandler = errorHandler; } @@ -24,8 +23,8 @@ namespace Geekbot.net.Commands.Integrations [Summary("Search for something on youtube.")] public async Task Yt([Remainder] [Summary("Title")] string searchQuery) { - var key = _redis.StringGet("youtubeKey"); - if (key.IsNullOrEmpty) + var key = _globalSettings.GetKey("YoutubeKey"); + if (string.IsNullOrEmpty(key)) { await ReplyAsync("No youtube key set, please tell my senpai to set one"); return; @@ -35,7 +34,7 @@ namespace Geekbot.net.Commands.Integrations { var youtubeService = new YouTubeService(new BaseClientService.Initializer { - ApiKey = key.ToString(), + ApiKey = key, ApplicationName = GetType().ToString() }); diff --git a/Geekbot.net/Commands/Utils/Info.cs b/Geekbot.net/Commands/Utils/Info.cs index 55615ea..e7b104f 100644 --- a/Geekbot.net/Commands/Utils/Info.cs +++ b/Geekbot.net/Commands/Utils/Info.cs @@ -7,7 +7,6 @@ using Discord.Commands; using Discord.WebSocket; using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; -using StackExchange.Redis; namespace Geekbot.net.Commands.Utils { @@ -16,11 +15,9 @@ namespace Geekbot.net.Commands.Utils private readonly DiscordSocketClient _client; private readonly CommandService _commands; private readonly IErrorHandler _errorHandler; - private readonly IDatabase _redis; - public Info(IDatabase redis, IErrorHandler errorHandler, DiscordSocketClient client, CommandService commands) + public Info(IErrorHandler errorHandler, DiscordSocketClient client, CommandService commands) { - _redis = redis; _errorHandler = errorHandler; _client = client; _commands = commands; @@ -37,7 +34,7 @@ namespace Geekbot.net.Commands.Utils eb.WithAuthor(new EmbedAuthorBuilder() .WithIconUrl(_client.CurrentUser.GetAvatarUrl()) .WithName($"{Constants.Name} V{Constants.BotVersion()}")); - var botOwner = await Context.Guild.GetUserAsync(ulong.Parse(_redis.StringGet("botOwner"))); + var botOwner = (await _client.GetApplicationInfoAsync()).Owner; var uptime = DateTime.Now.Subtract(Process.GetCurrentProcess().StartTime); eb.AddInlineField("Bot Name", _client.CurrentUser.Username); diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index 1515920..b901c1f 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -1,9 +1,12 @@ using System; +using System.Linq; using System.Text; using System.Threading.Tasks; using Discord; using Discord.Commands; using Discord.WebSocket; +using Geekbot.net.Database; +using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Logger; using Geekbot.net.Lib.ReactionListener; using Geekbot.net.Lib.UserRepository; @@ -13,6 +16,7 @@ namespace Geekbot.net { public class Handlers { + private readonly DatabaseContext _database; private readonly IDiscordClient _client; private readonly IGeekbotLogger _logger; private readonly IDatabase _redis; @@ -21,8 +25,9 @@ namespace Geekbot.net private readonly IUserRepository _userRepository; private readonly IReactionListener _reactionListener; - public Handlers(IDiscordClient client, IGeekbotLogger logger, IDatabase redis, IServiceProvider servicesProvider, CommandService commands, IUserRepository userRepository, IReactionListener reactionListener) + public Handlers(DatabaseContext database, IDiscordClient client, IGeekbotLogger logger, IDatabase redis, IServiceProvider servicesProvider, CommandService commands, IUserRepository userRepository, IReactionListener reactionListener) { + _database = database; _client = client; _logger = logger; _redis = redis; @@ -46,13 +51,21 @@ namespace Geekbot.net var lowCaseMsg = message.ToString().ToLower(); if (lowCaseMsg.StartsWith("hui")) { - message.Channel.SendMessageAsync("hui!!!"); - return Task.CompletedTask; + var hasPing = _database.GuildSettings.FirstOrDefault(guild => + guild.GuildId.Equals(((SocketGuildChannel) message.Channel).Guild.Id.AsLong())) + ?.Hui ?? false; + if (hasPing) + { + message.Channel.SendMessageAsync("hui!!!"); + return Task.CompletedTask; + } } if (lowCaseMsg.StartsWith("ping ") || lowCaseMsg.Equals("ping")) { - bool.TryParse(_redis.HashGet($"{((SocketGuildChannel) message.Channel).Guild.Id}:Settings", "ping"), out var allowPings); - if (allowPings) + var hasPing = _database.GuildSettings.FirstOrDefault(guild => + guild.GuildId.Equals(((SocketGuildChannel) message.Channel).Guild.Id.AsLong())) + ?.Ping ?? false; + if (hasPing) { message.Channel.SendMessageAsync("pong"); return Task.CompletedTask; @@ -109,10 +122,12 @@ namespace Geekbot.net { if (!user.IsBot) { - var message = _redis.HashGet($"{user.Guild.Id}:Settings", "WelcomeMsg"); - if (!message.IsNullOrEmpty) + var message = _database.GuildSettings.FirstOrDefault(guild => + guild.GuildId.Equals(user.Guild.Id.AsLong())) + ?.WelcomeMessage; + if (!string.IsNullOrEmpty(message)) { - message = message.ToString().Replace("$user", user.Mention); + message = message.Replace("$user", user.Mention); await user.Guild.DefaultChannel.SendMessageAsync(message); } } @@ -134,15 +149,12 @@ namespace Geekbot.net { try { - var sendLeftEnabled = _redis.HashGet($"{user.Guild.Id}:Settings", "ShowLeave"); - if (sendLeftEnabled.ToString() == "1") + var guild = _database.GuildSettings.FirstOrDefault(g => + g.GuildId.Equals(user.Guild.Id.AsLong())); + if (guild?.ShowLeave ?? false) { - var modChannel = ulong.Parse(_redis.HashGet($"{user.Guild.Id}:Settings", "ModChannel")); - if (!string.IsNullOrEmpty(modChannel.ToString())) - { - var modChannelSocket = (ISocketMessageChannel) await _client.GetChannelAsync(modChannel); - await modChannelSocket.SendMessageAsync($"{user.Username}#{user.Discriminator} left the server"); - } + var modChannelSocket = (ISocketMessageChannel) await _client.GetChannelAsync(guild.ModChannel.AsUlong()); + await modChannelSocket.SendMessageAsync($"{user.Username}#{user.Discriminator} left the server"); } } catch (Exception e) @@ -160,27 +172,24 @@ namespace Geekbot.net { try { - var guild = ((IGuildChannel) channel).Guild; - var sendLeftEnabled = _redis.HashGet($"{guild.Id}:Settings", "ShowDelete"); - if (sendLeftEnabled.ToString() == "1") + var guildSocketData = ((IGuildChannel) channel).Guild; + var guild = _database.GuildSettings.FirstOrDefault(g => + g.GuildId.Equals(guildSocketData.Id.AsLong())); + if (guild?.ShowDelete ?? false) { - var modChannel = ulong.Parse(_redis.HashGet($"{guild.Id}:Settings", "ModChannel")); - if (!string.IsNullOrEmpty(modChannel.ToString()) && modChannel != channel.Id) + var modChannelSocket = (ISocketMessageChannel) await _client.GetChannelAsync(guild.ModChannel.AsUlong()); + var sb = new StringBuilder(); + if (message.Value != null) { - var modChannelSocket = (ISocketMessageChannel) await _client.GetChannelAsync(modChannel); - var sb = new StringBuilder(); - if (message.Value != null) - { - sb.AppendLine( - $"The following message from {message.Value.Author.Username}#{message.Value.Author.Discriminator} was deleted in <#{channel.Id}>"); - sb.AppendLine(message.Value.Content); - } - else - { - sb.AppendLine("Someone deleted a message, the message was not cached..."); - } - await modChannelSocket.SendMessageAsync(sb.ToString()); + sb.AppendLine( + $"The following message from {message.Value.Author.Username}#{message.Value.Author.Discriminator} was deleted in <#{channel.Id}>"); + sb.AppendLine(message.Value.Content); } + else + { + sb.AppendLine("Someone deleted a message, the message was not cached..."); + } + await modChannelSocket.SendMessageAsync(sb.ToString()); } } catch (Exception e) diff --git a/Geekbot.net/Lib/GlobalSettings/GlobalSettings.cs b/Geekbot.net/Lib/GlobalSettings/GlobalSettings.cs new file mode 100644 index 0000000..1234b34 --- /dev/null +++ b/Geekbot.net/Lib/GlobalSettings/GlobalSettings.cs @@ -0,0 +1,55 @@ +using System.Linq; +using Geekbot.net.Database; +using Geekbot.net.Database.Models; + +namespace Geekbot.net.Lib.GlobalSettings +{ + public class GlobalSettings : IGlobalSettings + { + private readonly DatabaseContext _database; + + public GlobalSettings(DatabaseContext database) + { + _database = database; + } + + public bool SetKey(string keyName, string value) + { + try + { + var key = GetKeyFull(keyName); + if (key == null) + { + _database.Globals.Add(new GlobalsModel() + { + Name = keyName, + Value = value + }); + _database.SaveChanges(); + return true; + } + + key.Value = value; + _database.Globals.Update(key); + _database.SaveChanges(); + return true; + } + catch + { + return false; + } + } + + public string GetKey(string keyName) + { + var key = _database.Globals.FirstOrDefault(k => k.Name.Equals(keyName)); + return key?.Value ?? string.Empty; + } + + public GlobalsModel GetKeyFull(string keyName) + { + var key = _database.Globals.FirstOrDefault(k => k.Name.Equals(keyName)); + return key; + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/GlobalSettings/IGlobalSettings.cs b/Geekbot.net/Lib/GlobalSettings/IGlobalSettings.cs new file mode 100644 index 0000000..1919b50 --- /dev/null +++ b/Geekbot.net/Lib/GlobalSettings/IGlobalSettings.cs @@ -0,0 +1,11 @@ +using Geekbot.net.Database.Models; + +namespace Geekbot.net.Lib.GlobalSettings +{ + public interface IGlobalSettings + { + bool SetKey(string keyName, string value); + string GetKey(string keyName); + GlobalsModel GetKeyFull(string keyName); + } +} \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index ee05a12..42dc856 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -1,6 +1,4 @@ using System; -using System.Linq; -using System.Net; using System.Reflection; using System.Text; using System.Threading.Tasks; @@ -14,13 +12,13 @@ using Geekbot.net.Lib.Audio; using Geekbot.net.Lib.Clients; using Geekbot.net.Lib.Converters; using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.GlobalSettings; using Geekbot.net.Lib.Levels; using Geekbot.net.Lib.Localization; using Geekbot.net.Lib.Logger; using Geekbot.net.Lib.Media; using Geekbot.net.Lib.ReactionListener; using Geekbot.net.Lib.UserRepository; -using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Nancy.Hosting.Self; using StackExchange.Redis; @@ -33,6 +31,8 @@ namespace Geekbot.net private DiscordSocketClient _client; private CommandService _commands; private IDatabase _redis; + private DatabaseContext _database; + private IGlobalSettings _globalSettings; private IServiceCollection _services; private IServiceProvider _servicesProvider; private RedisValue _token; @@ -107,11 +107,12 @@ namespace Geekbot.net _firstStart = true; } - var database = new DatabaseInitializer(runParameters, logger).Initzialize(); + _database = new DatabaseInitializer(runParameters, logger).Initzialize(); + _globalSettings = new GlobalSettings(_database); _services = new ServiceCollection(); - _userRepository = new UserRepository(database, logger); + _userRepository = new UserRepository(_database, logger); var fortunes = new FortunesProvider(logger); var mediaProvider = new MediaProvider(logger); var malClient = new MalClient(_redis, logger); @@ -132,7 +133,8 @@ namespace Geekbot.net _services.AddSingleton(mtgManaConverter); _services.AddSingleton(wikipediaClient); _services.AddSingleton(audioUtils); - _services.AddSingleton(database); + _services.AddSingleton(_database); + _services.AddSingleton(_globalSettings); logger.Information(LogSource.Geekbot, "Connecting to Discord"); @@ -150,7 +152,7 @@ namespace Geekbot.net var isConneted = await IsConnected(); if (isConneted) { - await _client.SetGameAsync(_redis.StringGet("Game")); + await _client.SetGameAsync(_globalSettings.GetKey("Game")); _logger.Information(LogSource.Geekbot, $"Now Connected as {_client.CurrentUser.Username} to {_client.Guilds.Count} Servers"); _logger.Information(LogSource.Geekbot, "Registering Stuff"); @@ -165,7 +167,7 @@ namespace Geekbot.net _services.AddSingleton(reactionListener); _servicesProvider = _services.BuildServiceProvider(); - var handlers = new Handlers(_client, _logger, _redis, _servicesProvider, _commands, _userRepository, reactionListener); + var handlers = new Handlers(_database, _client, _logger, _redis, _servicesProvider, _commands, _userRepository, reactionListener); _client.MessageReceived += handlers.RunCommand; _client.MessageReceived += handlers.UpdateStats; From a1b5bd1955e50185f20404849c1a5051686b009f Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 13 May 2018 18:47:01 +0200 Subject: [PATCH 068/443] Add more run parameters, remove first launch stuff, store token in globals --- Geekbot.net/Database/RedisMigration.cs | 4 +- .../Lib/ReactionListener/ReactionListener.cs | 18 +++---- Geekbot.net/Lib/RunParameters.cs | 47 ++++++++++++++----- Geekbot.net/Program.cs | 43 ++++------------- 4 files changed, 56 insertions(+), 56 deletions(-) diff --git a/Geekbot.net/Database/RedisMigration.cs b/Geekbot.net/Database/RedisMigration.cs index 1cd73b0..237ce60 100644 --- a/Geekbot.net/Database/RedisMigration.cs +++ b/Geekbot.net/Database/RedisMigration.cs @@ -29,7 +29,7 @@ namespace Geekbot.net.Database _client = client; } - public async Task Migrate() + public Task Migrate() { _logger.Information(LogSource.Geekbot, "Starting migration process"); foreach (var guild in _client.Guilds) @@ -283,6 +283,8 @@ namespace Geekbot.net.Database _logger.Information(LogSource.Geekbot, $"Finished Migration for {guild.Name}"); } _logger.Information(LogSource.Geekbot, "Finished migration process"); + + return Task.CompletedTask;; } private QuoteModel CreateQuoteObject(ulong guild, QuoteObjectDto quote) diff --git a/Geekbot.net/Lib/ReactionListener/ReactionListener.cs b/Geekbot.net/Lib/ReactionListener/ReactionListener.cs index 950edd1..18490db 100644 --- a/Geekbot.net/Lib/ReactionListener/ReactionListener.cs +++ b/Geekbot.net/Lib/ReactionListener/ReactionListener.cs @@ -9,22 +9,22 @@ namespace Geekbot.net.Lib.ReactionListener { public class ReactionListener : IReactionListener { - private readonly IDatabase _database; + private readonly IDatabase _redis; private Dictionary> _listener; - public ReactionListener(IDatabase database) + public ReactionListener(IDatabase redis) { - _database = database; + _redis = redis; LoadListeners(); } private Task LoadListeners() { - var ids = _database.SetMembers("MessageIds"); + var ids = _redis.SetMembers("MessageIds"); _listener = new Dictionary>(); foreach (var id in ids) { - var reactions = _database.HashGetAll($"Messages:{id}"); + var reactions = _redis.HashGetAll($"Messages:{id}"); var messageId = id; var emojiDict = new Dictionary(); foreach (var r in reactions) @@ -54,12 +54,12 @@ namespace Geekbot.net.Lib.ReactionListener public Task AddRoleToListener(string messageId, IEmote emoji, IRole role) { - if (_database.SetMembers("MessageIds").All(e => e.ToString() != messageId)) + if (_redis.SetMembers("MessageIds").All(e => e.ToString() != messageId)) { - _database.SetAdd("MessageIds", messageId); + _redis.SetAdd("MessageIds", messageId); } - _database.HashSet($"Messages:{messageId}", new[] {new HashEntry(emoji.ToString(), role.Id.ToString())}); - _database.SetAdd("MessageIds", messageId); + _redis.HashSet($"Messages:{messageId}", new[] {new HashEntry(emoji.ToString(), role.Id.ToString())}); + _redis.SetAdd("MessageIds", messageId); if (_listener.ContainsKey(messageId)) { _listener[messageId].Add(emoji, role.Id); diff --git a/Geekbot.net/Lib/RunParameters.cs b/Geekbot.net/Lib/RunParameters.cs index eb3ca37..7942b76 100644 --- a/Geekbot.net/Lib/RunParameters.cs +++ b/Geekbot.net/Lib/RunParameters.cs @@ -1,20 +1,17 @@ -using System; -using CommandLine; +using CommandLine; namespace Geekbot.net.Lib { public class RunParameters { - /** - * General Parameters - */ - [Option('V', "verbose", Default = false, HelpText = "Prints all messages to standard output.")] + /************************************ + * General * + ************************************/ + + [Option('V', "verbose", Default = false, HelpText = "Logs everything.")] public bool Verbose { get; set; } - [Option('r', "reset", Default = false, HelpText = "Resets the bot")] - public bool Reset { get; set; } - - [Option('j', "log-json", Default = false, HelpText = "Logs messages as json")] + [Option('j', "log-json", Default = false, HelpText = "Logger outputs json")] public bool LogJson { get; set; } [Option('a', "disable-api", Default = false, HelpText = "Disables the web api")] @@ -26,9 +23,10 @@ namespace Geekbot.net.Lib [Option("token", Default = null, HelpText = "Set a new bot token")] public string Token { get; set; } - /** - * Database Stuff - */ + /************************************ + * Database * + ************************************/ + [Option("in-memory", Default = false, HelpText = "Uses the in-memory database instead of postgresql")] public bool InMemory { get; set; } @@ -47,5 +45,28 @@ namespace Geekbot.net.Lib [Option("db-password", Default = "", HelpText = "Set a posgresql password")] public string DbPassword { get; set; } + + /************************************ + * Redis * + ************************************/ + + [Option("redis-host", Default = "127.0.0.1", HelpText = "Set a redis host")] + public string RedisHost { get; set; } + + [Option("redis-port", Default = "6379", HelpText = "Set a redis port")] + public string RedisPort { get; set; } + + [Option("redis-database", Default = "6", HelpText = "Select a redis database (1-15)")] + public string RedisDatabase { get; set; } + + /************************************ + * WebApi * + ************************************/ + + [Option("api-host", Default = "127.0.0.1", HelpText = "Host on which the WebApi listens")] + public string ApiHost { get; set; } + + [Option("api-port", Default = "12995", HelpText = "Port on which the WebApi listens")] + public string ApiPort { get; set; } } } \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 42dc856..8084127 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -38,7 +38,6 @@ namespace Geekbot.net private RedisValue _token; private GeekbotLogger _logger; private IUserRepository _userRepository; - private bool _firstStart; private RunParameters _runParameters; private static void Main(string[] args) @@ -84,10 +83,13 @@ namespace Geekbot.net _client.Log += discordLogger.Log; _commands = new CommandService(); + _database = new DatabaseInitializer(runParameters, logger).Initzialize(); + _globalSettings = new GlobalSettings(_database); + try { - var redisMultiplexer = ConnectionMultiplexer.Connect("127.0.0.1:6379"); - _redis = redisMultiplexer.GetDatabase(6); + var redisMultiplexer = ConnectionMultiplexer.Connect($"{runParameters.RedisHost}:{runParameters.RedisPort}"); + _redis = redisMultiplexer.GetDatabase(int.Parse(runParameters.RedisDatabase)); logger.Information(LogSource.Redis, $"Connected to db {_redis.Database}"); } catch (Exception e) @@ -96,20 +98,16 @@ namespace Geekbot.net Environment.Exit(GeekbotExitCode.RedisConnectionFailed.GetHashCode()); } - _token = runParameters.Token ?? _redis.StringGet("discordToken"); + _token = runParameters.Token ?? _globalSettings.GetKey("DiscordToken"); if (_token.IsNullOrEmpty) { Console.Write("Your bot Token: "); var newToken = Console.ReadLine(); - _redis.StringSet("discordToken", newToken); - _redis.StringSet("Game", "Ping Pong"); + _globalSettings.SetKey("DiscordToken", newToken); + _globalSettings.SetKey("Game", "Ping Pong"); _token = newToken; - _firstStart = true; } - _database = new DatabaseInitializer(runParameters, logger).Initzialize(); - _globalSettings = new GlobalSettings(_database); - _services = new ServiceCollection(); _userRepository = new UserRepository(_database, logger); @@ -177,13 +175,7 @@ namespace Geekbot.net _client.UserLeft += handlers.UserLeft; _client.ReactionAdded += handlers.ReactionAdded; _client.ReactionRemoved += handlers.ReactionRemoved; - - if (_firstStart || _runParameters.Reset) - { - _logger.Information(LogSource.Geekbot, "Finishing setup"); - await FinishSetup(); - _logger.Information(LogSource.Geekbot, "Setup finished"); - } + if (!_runParameters.DisableApi) { StartWebApi(); @@ -209,24 +201,9 @@ namespace Geekbot.net private void StartWebApi() { _logger.Information(LogSource.Api, "Starting Webserver"); - var webApiUrl = new Uri("http://localhost:12995"); + var webApiUrl = new Uri($"http://{_runParameters.ApiHost}:{_runParameters.ApiPort}"); new NancyHost(webApiUrl).Start(); _logger.Information(LogSource.Api, $"Webserver now running on {webApiUrl}"); } - - private async Task FinishSetup() - { - try - { - // ToDo: Set bot avatar - var appInfo = await _client.GetApplicationInfoAsync(); - _redis.StringSet("botOwner", appInfo.Owner.Id); - } - catch (Exception e) - { - _logger.Warning(LogSource.Geekbot, "Setup Failed, couldn't retrieve discord application data", e); - } - return Task.CompletedTask; - } } } \ No newline at end of file From 08015c61023fb57c37812eb57c34f7ecd7631185 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 13 May 2018 21:06:41 +0200 Subject: [PATCH 069/443] Port !stats, !role, !wiki and !slap, fix messages in rank, add roleselfservicemodel to db --- Geekbot.net/Commands/Admin/Role.cs | 48 +++++++++------- .../Commands/Integrations/Wikipedia.cs | 12 ++-- Geekbot.net/Commands/Randomness/Ship.cs | 37 +++++++----- Geekbot.net/Commands/Randomness/Slap.cs | 57 ++++++++++++++++--- Geekbot.net/Commands/User/Ranking/Rank.cs | 24 +++----- Geekbot.net/Commands/User/Stats.cs | 21 ++++--- Geekbot.net/Database/DatabaseContext.cs | 4 +- .../Database/Models/RoleSelfServiceModel.cs | 12 +++- 8 files changed, 141 insertions(+), 74 deletions(-) diff --git a/Geekbot.net/Commands/Admin/Role.cs b/Geekbot.net/Commands/Admin/Role.cs index 8624e66..a3a649e 100644 --- a/Geekbot.net/Commands/Admin/Role.cs +++ b/Geekbot.net/Commands/Admin/Role.cs @@ -5,24 +5,25 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Discord.Net; -using Geekbot.net.Lib; +using Geekbot.net.Database; +using Geekbot.net.Database.Models; using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.ReactionListener; -using StackExchange.Redis; namespace Geekbot.net.Commands.Admin { [Group("role")] public class Role : ModuleBase { + private readonly DatabaseContext _database; private readonly IErrorHandler _errorHandler; - private readonly IDatabase _redis; private readonly IReactionListener _reactionListener; - public Role(IErrorHandler errorHandler, IDatabase redis, IReactionListener reactionListener) + public Role(DatabaseContext database, IErrorHandler errorHandler, IReactionListener reactionListener) { + _database = database; _errorHandler = errorHandler; - _redis = redis; _reactionListener = reactionListener; } @@ -32,8 +33,8 @@ namespace Geekbot.net.Commands.Admin { try { - var roles = _redis.HashGetAll($"{Context.Guild.Id}:RoleWhitelist"); - if (roles.Length == 0) + var roles = _database.RoleSelfService.Where(g => g.GuildId.Equals(Context.Guild.Id.AsLong())).ToList(); + if (roles.Count == 0) { await ReplyAsync("There are no roles configured for this server"); return; @@ -42,7 +43,7 @@ namespace Geekbot.net.Commands.Admin var sb = new StringBuilder(); sb.AppendLine($"**Self Service Roles on {Context.Guild.Name}**"); sb.AppendLine("To get a role, use `!role name`"); - foreach (var role in roles) sb.AppendLine($"- {role.Name}"); + foreach (var role in roles) sb.AppendLine($"- {role.WhiteListName}"); await ReplyAsync(sb.ToString()); } catch (Exception e) @@ -58,18 +59,19 @@ namespace Geekbot.net.Commands.Admin try { var roleName = roleNameRaw.ToLower(); - if (_redis.HashExists($"{Context.Guild.Id}:RoleWhitelist", roleName)) + var roleFromDb = _database.RoleSelfService.FirstOrDefault(e => + e.GuildId.Equals(Context.Guild.Id.AsLong()) && e.WhiteListName.Equals(roleName)); + if (roleFromDb != null) { var guildUser = (IGuildUser) Context.User; - var roleId = ulong.Parse(_redis.HashGet($"{Context.Guild.Id}:RoleWhitelist", roleName)); - var role = Context.Guild.Roles.First(r => r.Id == roleId); + var role = Context.Guild.Roles.First(r => r.Id == roleFromDb.RoleId.AsUlong()); if (role == null) { await ReplyAsync("That role doesn't seem to exist"); return; } - if (guildUser.RoleIds.Contains(roleId)) + if (guildUser.RoleIds.Contains(role.Id)) { await guildUser.RemoveRoleAsync(role); await ReplyAsync($"Removed you from {role.Name}"); @@ -113,12 +115,17 @@ namespace Geekbot.net.Commands.Admin || role.Permissions.KickMembers) { await ReplyAsync( - "Woah, i don't think you want to add that role to self service as it contains some dangerous permissions"); + "You cannot add that role to self service because it contains one or more dangerous permissions"); return; } - _redis.HashSet($"{Context.Guild.Id}:RoleWhitelist", - new[] {new HashEntry(roleName.ToLower(), role.Id.ToString())}); + _database.RoleSelfService.Add(new RoleSelfServiceModel + { + GuildId = Context.Guild.Id.AsLong(), + RoleId = role.Id.AsLong(), + WhiteListName = roleName + }); + _database.SaveChanges(); await ReplyAsync($"Added {role.Name} to the whitelist"); } catch (Exception e) @@ -134,16 +141,17 @@ namespace Geekbot.net.Commands.Admin { try { - - var success = _redis.HashDelete($"{Context.Guild.Id}:RoleWhitelist", roleName.ToLower()); - if (success) + var roleFromDb = _database.RoleSelfService.FirstOrDefault(e => + e.GuildId.Equals(Context.Guild.Id.AsLong()) && e.WhiteListName.Equals(roleName)); + if (roleFromDb != null) { + _database.RoleSelfService.Remove(roleFromDb); + _database.SaveChanges(); await ReplyAsync($"Removed {roleName} from the whitelist"); return; } - await ReplyAsync("There is not whitelisted role with that name..."); - + await ReplyAsync("There is not whitelisted role with that name"); } catch (Exception e) { diff --git a/Geekbot.net/Commands/Integrations/Wikipedia.cs b/Geekbot.net/Commands/Integrations/Wikipedia.cs index e2e8928..069ba8c 100644 --- a/Geekbot.net/Commands/Integrations/Wikipedia.cs +++ b/Geekbot.net/Commands/Integrations/Wikipedia.cs @@ -5,10 +5,10 @@ using System.Text; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Lib; +using Geekbot.net.Database; using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.Extensions; using HtmlAgilityPack; -using StackExchange.Redis; using WikipediaApi; using WikipediaApi.Page; @@ -18,13 +18,13 @@ namespace Geekbot.net.Commands.Integrations { private readonly IErrorHandler _errorHandler; private readonly IWikipediaClient _wikipediaClient; - private readonly IDatabase _redis; + private readonly DatabaseContext _database; - public Wikipedia(IErrorHandler errorHandler, IWikipediaClient wikipediaClient, IDatabase redis) + public Wikipedia(IErrorHandler errorHandler, IWikipediaClient wikipediaClient, DatabaseContext database) { _errorHandler = errorHandler; _wikipediaClient = wikipediaClient; - _redis = redis; + _database = database; } [Command("wiki", RunMode = RunMode.Async)] @@ -33,7 +33,7 @@ namespace Geekbot.net.Commands.Integrations { try { - var wikiLang = _redis.HashGet($"{Context.Guild.Id}:Settings", "WikiLang").ToString(); + var wikiLang = _database.GuildSettings.FirstOrDefault(g => g.GuildId.Equals(Context.Guild.Id.AsLong()))?.WikiLang; if (string.IsNullOrEmpty(wikiLang)) { wikiLang = "en"; diff --git a/Geekbot.net/Commands/Randomness/Ship.cs b/Geekbot.net/Commands/Randomness/Ship.cs index 735bd51..2e3588d 100644 --- a/Geekbot.net/Commands/Randomness/Ship.cs +++ b/Geekbot.net/Commands/Randomness/Ship.cs @@ -1,21 +1,23 @@ using System; +using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Lib; +using Geekbot.net.Database; +using Geekbot.net.Database.Models; using Geekbot.net.Lib.ErrorHandling; -using StackExchange.Redis; +using Geekbot.net.Lib.Extensions; namespace Geekbot.net.Commands.Randomness { public class Ship : ModuleBase { private readonly IErrorHandler _errorHandler; - private readonly IDatabase _redis; + private readonly DatabaseContext _database; - public Ship(IDatabase redis, IErrorHandler errorHandler) + public Ship(DatabaseContext database, IErrorHandler errorHandler) { - _redis = redis; + _database = database; _errorHandler = errorHandler; } @@ -25,22 +27,29 @@ namespace Geekbot.net.Commands.Randomness { try { - var dbstring = ""; - if (user1.Id > user2.Id) - dbstring = $"{user1.Id}-{user2.Id}"; - else - dbstring = $"{user2.Id}-{user1.Id}"; + var userKeys = user1.Id < user2.Id + ? new Tuple(user1.Id.AsLong(), user2.Id.AsLong()) + : new Tuple(user2.Id.AsLong(), user1.Id.AsLong()); - var dbval = _redis.HashGet($"{Context.Guild.Id}:Ships", dbstring); + var dbval = _database.Ships.FirstOrDefault(s => + s.FirstUserId.Equals(userKeys.Item1) && + s.SecondUserId.Equals(userKeys.Item2)); + var shippingRate = 0; - if (dbval.IsNullOrEmpty) + if (dbval == null) { shippingRate = new Random().Next(1, 100); - _redis.HashSet($"{Context.Guild.Id}:Ships", dbstring, shippingRate); + _database.Ships.Add(new ShipsModel() + { + FirstUserId = userKeys.Item1, + SecondUserId = userKeys.Item2, + Strength = shippingRate + }); + _database.SaveChanges(); } else { - shippingRate = int.Parse(dbval.ToString()); + shippingRate = dbval.Strength; } var reply = ":heartpulse: **Matchmaking** :heartpulse:\r\n"; diff --git a/Geekbot.net/Commands/Randomness/Slap.cs b/Geekbot.net/Commands/Randomness/Slap.cs index 2cac9d2..b37742b 100644 --- a/Geekbot.net/Commands/Randomness/Slap.cs +++ b/Geekbot.net/Commands/Randomness/Slap.cs @@ -1,23 +1,25 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Lib; +using Geekbot.net.Database; +using Geekbot.net.Database.Models; using Geekbot.net.Lib.ErrorHandling; -using StackExchange.Redis; +using Geekbot.net.Lib.Extensions; namespace Geekbot.net.Commands.Randomness { public class Slap : ModuleBase { private readonly IErrorHandler _errorHandler; - private readonly IDatabase _redis; + private readonly DatabaseContext _database; - public Slap(IErrorHandler errorHandler, IDatabase redis) + public Slap(IErrorHandler errorHandler, DatabaseContext database) { _errorHandler = errorHandler; - _redis = redis; + _database = database; } [Command("slap", RunMode = RunMode.Async)] @@ -76,16 +78,53 @@ namespace Geekbot.net.Commands.Randomness "dictionary", "powerless banhammer" }; - - _redis.HashIncrement($"{Context.Guild.Id}:SlapsRecieved", user.Id.ToString()); - _redis.HashIncrement($"{Context.Guild.Id}:SlapsGiven", Context.User.Id.ToString()); - + await ReplyAsync($"{Context.User.Username} slapped {user.Username} with a {things[new Random().Next(things.Count - 1)]}"); + + UpdateRecieved(user.Id); + UpdateGiven(Context.User.Id); + _database.SaveChanges(); } catch (Exception e) { _errorHandler.HandleCommandException(e, Context); } } + + private void UpdateGiven(ulong userId) + { + var user = GetUser(userId); + user.Given++; + _database.Slaps.Update(user); + } + + private void UpdateRecieved(ulong userId) + { + var user = GetUser(userId); + user.Recieved++; + _database.Slaps.Update(user); + } + + private SlapsModel GetUser(ulong userId) + { + var user = _database.Slaps.FirstOrDefault(e => + e.GuildId.Equals(Context.Guild.Id.AsLong()) && + e.UserId.Equals(userId.AsLong()) + ); + + if (user != null) return user; + + _database.Slaps.Add(new SlapsModel + { + GuildId = Context.Guild.Id.AsLong(), + UserId = userId.AsLong(), + Recieved = 0, + Given = 0 + }); + _database.SaveChanges(); + return _database.Slaps.FirstOrDefault(e => + e.GuildId.Equals(Context.Guild.Id.AsLong()) && + e.UserId.Equals(userId.AsLong())); + } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/User/Ranking/Rank.cs b/Geekbot.net/Commands/User/Ranking/Rank.cs index dd1d026..027f41f 100644 --- a/Geekbot.net/Commands/User/Ranking/Rank.cs +++ b/Geekbot.net/Commands/User/Ranking/Rank.cs @@ -6,12 +6,12 @@ using System.Threading.Tasks; using Discord.Commands; using Discord.WebSocket; using Geekbot.net.Database; -using Geekbot.net.Lib; using Geekbot.net.Lib.Converters; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Logger; using Geekbot.net.Lib.UserRepository; +using StackExchange.Redis; namespace Geekbot.net.Commands.User.Ranking { @@ -23,9 +23,10 @@ namespace Geekbot.net.Commands.User.Ranking private readonly DatabaseContext _database; private readonly IUserRepository _userRepository; private readonly DiscordSocketClient _client; + private readonly IDatabase _redis; public Rank(DatabaseContext database, IErrorHandler errorHandler, IGeekbotLogger logger, IUserRepository userRepository, - IEmojiConverter emojiConverter, DiscordSocketClient client) + IEmojiConverter emojiConverter, DiscordSocketClient client, IDatabase redis) { _database = database; _errorHandler = errorHandler; @@ -33,6 +34,7 @@ namespace Geekbot.net.Commands.User.Ranking _userRepository = userRepository; _emojiConverter = emojiConverter; _client = client; + _redis = redis; } [Command("rank", RunMode = RunMode.Async)] @@ -74,8 +76,8 @@ namespace Geekbot.net.Commands.User.Ranking list = GetRollsList(amount); break; default: - list = new Dictionary(); - break; + await ReplyAsync("Valid types are '`messages`' '`karma`', '`rolls`'"); + return; } if (!list.Any()) @@ -142,18 +144,10 @@ namespace Geekbot.net.Commands.User.Ranking private Dictionary GetMessageList(int amount) { - var data = _database.Messages - .Where(k => k.GuildId.Equals(Context.Guild.Id.AsLong())) - .OrderByDescending(o => o.MessageCount) - .Take(amount); - - var dict = new Dictionary(); - foreach (var user in data) - { - dict.Add(user.UserId.AsUlong(), user.MessageCount); - } - return dict; + var data = _redis.HashGetAll($"{Context.Guild.Id}:Messages").ToDictionary().Take(amount + 1); + + return data.Where(user => !user.Key.Equals(0)).ToDictionary(user => ulong.Parse(user.Key), user => int.Parse(user.Value)); } private Dictionary GetKarmaList(int amount) diff --git a/Geekbot.net/Commands/User/Stats.cs b/Geekbot.net/Commands/User/Stats.cs index 93bddef..01d169e 100644 --- a/Geekbot.net/Commands/User/Stats.cs +++ b/Geekbot.net/Commands/User/Stats.cs @@ -1,9 +1,11 @@ using System; +using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Lib; +using Geekbot.net.Database; using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Levels; using StackExchange.Redis; @@ -14,10 +16,12 @@ namespace Geekbot.net.Commands.User private readonly IErrorHandler _errorHandler; private readonly ILevelCalc _levelCalc; private readonly IDatabase _redis; + private readonly DatabaseContext _database; - public Stats(IDatabase redis, IErrorHandler errorHandler, ILevelCalc levelCalc) + public Stats(IDatabase redis, DatabaseContext database, IErrorHandler errorHandler, ILevelCalc levelCalc) { _redis = redis; + _database = database; _errorHandler = errorHandler; _levelCalc = levelCalc; } @@ -47,20 +51,23 @@ namespace Geekbot.net.Commands.User .WithName(userInfo.Username)); eb.WithColor(new Color(221, 255, 119)); - var karma = _redis.HashGet($"{Context.Guild.Id}:Karma", userInfo.Id.ToString()); - var correctRolls = _redis.HashGet($"{Context.Guild.Id}:Rolls", userInfo.Id.ToString()); + var karma = _database.Karma.FirstOrDefault(e => + e.GuildId.Equals(Context.Guild.Id.AsLong()) && + e.UserId.Equals(userInfo.Id.AsLong())); + var correctRolls = _database.Rolls.FirstOrDefault(e => + e.GuildId.Equals(Context.Guild.Id.AsLong()) && + e.UserId.Equals(userInfo.Id.AsLong())); eb.AddInlineField("Discordian Since", $"{createdAt.Day}.{createdAt.Month}.{createdAt.Year} ({age} days)") .AddInlineField("Joined Server", $"{joinedAt.Day}.{joinedAt.Month}.{joinedAt.Year} ({joinedDayAgo} days)") - .AddInlineField("Karma", karma.ToString() ?? "0") + .AddInlineField("Karma", karma?.Karma ?? 0) .AddInlineField("Level", level) .AddInlineField("Messages Sent", messages) .AddInlineField("Server Total", $"{percent}%"); - if (!correctRolls.IsNullOrEmpty) - eb.AddInlineField("Guessed Rolls", correctRolls); + if (correctRolls != null) eb.AddInlineField("Guessed Rolls", correctRolls.Rolls); await ReplyAsync("", false, eb.Build()); } diff --git a/Geekbot.net/Database/DatabaseContext.cs b/Geekbot.net/Database/DatabaseContext.cs index e4fab28..e9de32c 100644 --- a/Geekbot.net/Database/DatabaseContext.cs +++ b/Geekbot.net/Database/DatabaseContext.cs @@ -15,8 +15,8 @@ namespace Geekbot.net.Database public DbSet Messages { get; set; } public DbSet Slaps { get; set; } public DbSet Globals { get; set; } - + public DbSet RoleSelfService { get; set; } + // public DbSet UserSettings { get; set; } - // public DbSet RoleSelfService { get; set; } } } \ No newline at end of file diff --git a/Geekbot.net/Database/Models/RoleSelfServiceModel.cs b/Geekbot.net/Database/Models/RoleSelfServiceModel.cs index 4bb7f01..de8c341 100644 --- a/Geekbot.net/Database/Models/RoleSelfServiceModel.cs +++ b/Geekbot.net/Database/Models/RoleSelfServiceModel.cs @@ -1,7 +1,17 @@ -namespace Geekbot.net.Database.Models +using System.ComponentModel.DataAnnotations; + +namespace Geekbot.net.Database.Models { public class RoleSelfServiceModel { + [Key] + public int Id { get; set; } + [Required] + public long GuildId { get; set; } + + public long RoleId { get; set; } + + public string WhiteListName { get; set; } } } \ No newline at end of file From 4a11ce62077ff2e0b40bb5066fa2653497646648 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 13 May 2018 21:33:48 +0200 Subject: [PATCH 070/443] Add Guild Settings to redis migration --- Geekbot.net/Commands/User/Ranking/Rank.cs | 44 +++++------------- Geekbot.net/Database/RedisMigration.cs | 56 +++++++++++++++++++++++ 2 files changed, 68 insertions(+), 32 deletions(-) diff --git a/Geekbot.net/Commands/User/Ranking/Rank.cs b/Geekbot.net/Commands/User/Ranking/Rank.cs index 027f41f..6e3a1d8 100644 --- a/Geekbot.net/Commands/User/Ranking/Rank.cs +++ b/Geekbot.net/Commands/User/Ranking/Rank.cs @@ -4,12 +4,10 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Discord.Commands; -using Discord.WebSocket; using Geekbot.net.Database; using Geekbot.net.Lib.Converters; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; -using Geekbot.net.Lib.Logger; using Geekbot.net.Lib.UserRepository; using StackExchange.Redis; @@ -19,21 +17,17 @@ namespace Geekbot.net.Commands.User.Ranking { private readonly IEmojiConverter _emojiConverter; private readonly IErrorHandler _errorHandler; - private readonly IGeekbotLogger _logger; private readonly DatabaseContext _database; private readonly IUserRepository _userRepository; - private readonly DiscordSocketClient _client; private readonly IDatabase _redis; - public Rank(DatabaseContext database, IErrorHandler errorHandler, IGeekbotLogger logger, IUserRepository userRepository, - IEmojiConverter emojiConverter, DiscordSocketClient client, IDatabase redis) + public Rank(DatabaseContext database, IErrorHandler errorHandler, IUserRepository userRepository, + IEmojiConverter emojiConverter, IDatabase redis) { _database = database; _errorHandler = errorHandler; - _logger = logger; _userRepository = userRepository; _emojiConverter = emojiConverter; - _client = client; _redis = redis; } @@ -144,42 +138,28 @@ namespace Geekbot.net.Commands.User.Ranking private Dictionary GetMessageList(int amount) { - - var data = _redis.HashGetAll($"{Context.Guild.Id}:Messages").ToDictionary().Take(amount + 1); - - return data.Where(user => !user.Key.Equals(0)).ToDictionary(user => ulong.Parse(user.Key), user => int.Parse(user.Value)); + return _redis + .HashGetAll($"{Context.Guild.Id}:Messages").ToDictionary().Take(amount + 1) + .Where(user => !user.Key.Equals(0)) + .ToDictionary(user => ulong.Parse(user.Key), user => int.Parse(user.Value)); } private Dictionary GetKarmaList(int amount) { - var data = _database.Karma + return _database.Karma .Where(k => k.GuildId.Equals(Context.Guild.Id.AsLong())) .OrderByDescending(o => o.Karma) - .Take(amount); - - var dict = new Dictionary(); - foreach (var user in data) - { - dict.Add(user.UserId.AsUlong(), user.Karma); - } - - return dict; + .Take(amount) + .ToDictionary(key => key.UserId.AsUlong(), key => key.Karma); } private Dictionary GetRollsList(int amount) { - var data = _database.Rolls + return _database.Rolls .Where(k => k.GuildId.Equals(Context.Guild.Id.AsLong())) .OrderByDescending(o => o.Rolls) - .Take(amount); - - var dict = new Dictionary(); - foreach (var user in data) - { - dict.Add(user.UserId.AsUlong(), user.Rolls); - } - - return dict; + .Take(amount) + .ToDictionary(key => key.UserId.AsUlong(), key => key.Rolls); } } } \ No newline at end of file diff --git a/Geekbot.net/Database/RedisMigration.cs b/Geekbot.net/Database/RedisMigration.cs index 237ce60..25a7ee5 100644 --- a/Geekbot.net/Database/RedisMigration.cs +++ b/Geekbot.net/Database/RedisMigration.cs @@ -230,6 +230,62 @@ namespace Geekbot.net.Database } #endregion + #region GuildSettings + /** + * Users + */ + try + { + var data = _redis.HashGetAll($"{guild.Id}:Settings"); + var settings = new GuildSettingsModel() + { + GuildId = guild.Id.AsLong(), + Hui = true + }; + foreach (var setting in data) + { + try + { + switch (setting.Name) + { + case "ShowLeave": + settings.ShowLeave = setting.Value.ToString() == "1"; + break; + case "WikiDel": + settings.ShowDelete = setting.Value.ToString() == "1"; + break; + case "WikiLang": + settings.WikiLang = setting.Value.ToString(); + break; + case "Language": + settings.Language = setting.Value.ToString(); + break; + case "WelcomeMsg": + settings.WelcomeMessage = setting.Value.ToString(); + break; + case "ping": + settings.Ping = bool.Parse(setting.Value.ToString()); + break; + case "ModChannel": + settings.ModChannel = long.Parse(setting.Value); + break; + default: + throw new NotImplementedException(); + } + } + catch + { + _logger.Warning(LogSource.Geekbot, $"Setting failed: {setting.Name} - {guild.Id}"); + } + } + } + catch + { + _logger.Warning(LogSource.Geekbot, "Settings migration failed"); + } + + #endregion + #region Users /** * Users From 3004b192098e6290326454d7ce1af655e5616887 Mon Sep 17 00:00:00 2001 From: runebaas Date: Mon, 14 May 2018 00:41:05 +0200 Subject: [PATCH 071/443] Small tweaks to the web api --- Geekbot.net/Lib/RunParameters.cs | 2 +- Geekbot.net/Program.cs | 4 +++- Geekbot.net/WebApi/Help/HelpController.cs | 7 ++----- Geekbot.net/WebApi/Status/StatusController.cs | 2 +- Geekbot.net/WebApi/WebConfig.cs | 20 +++++++++++++++++-- 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/Geekbot.net/Lib/RunParameters.cs b/Geekbot.net/Lib/RunParameters.cs index 7942b76..7f2dd3d 100644 --- a/Geekbot.net/Lib/RunParameters.cs +++ b/Geekbot.net/Lib/RunParameters.cs @@ -63,7 +63,7 @@ namespace Geekbot.net.Lib * WebApi * ************************************/ - [Option("api-host", Default = "127.0.0.1", HelpText = "Host on which the WebApi listens")] + [Option("api-host", Default = "localhost", HelpText = "Host on which the WebApi listens")] public string ApiHost { get; set; } [Option("api-port", Default = "12995", HelpText = "Port on which the WebApi listens")] diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 8084127..e5b3f59 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -19,6 +19,7 @@ using Geekbot.net.Lib.Logger; using Geekbot.net.Lib.Media; using Geekbot.net.Lib.ReactionListener; using Geekbot.net.Lib.UserRepository; +using Geekbot.net.WebApi; using Microsoft.Extensions.DependencyInjection; using Nancy.Hosting.Self; using StackExchange.Redis; @@ -202,7 +203,8 @@ namespace Geekbot.net { _logger.Information(LogSource.Api, "Starting Webserver"); var webApiUrl = new Uri($"http://{_runParameters.ApiHost}:{_runParameters.ApiPort}"); - new NancyHost(webApiUrl).Start(); + var webConfig = new WebConfig(_logger, _commands); + new NancyHost(webConfig, webApiUrl).Start(); _logger.Information(LogSource.Api, $"Webserver now running on {webApiUrl}"); } } diff --git a/Geekbot.net/WebApi/Help/HelpController.cs b/Geekbot.net/WebApi/Help/HelpController.cs index 015c7d8..8aaed12 100644 --- a/Geekbot.net/WebApi/Help/HelpController.cs +++ b/Geekbot.net/WebApi/Help/HelpController.cs @@ -2,19 +2,16 @@ using System.Reflection; using System.Threading.Tasks; using Discord.Commands; -using Geekbot.net.Lib; using Nancy; namespace Geekbot.net.WebApi.Help { - public class HelpController : NancyModule + public sealed class HelpController : NancyModule { - public HelpController() + public HelpController(CommandService commands) { Get("/v1/commands", args => { - var commands = GetCommands().Result; - var commandList = (from cmd in commands.Commands let cmdParamsObj = cmd.Parameters.Select(cmdParam => new CommandParamDto { diff --git a/Geekbot.net/WebApi/Status/StatusController.cs b/Geekbot.net/WebApi/Status/StatusController.cs index 05196a2..a2c692c 100644 --- a/Geekbot.net/WebApi/Status/StatusController.cs +++ b/Geekbot.net/WebApi/Status/StatusController.cs @@ -3,7 +3,7 @@ using Nancy; namespace Geekbot.net.WebApi.Status { - public class StatusController : NancyModule + public sealed class StatusController : NancyModule { public StatusController() { diff --git a/Geekbot.net/WebApi/WebConfig.cs b/Geekbot.net/WebApi/WebConfig.cs index f0b9ca3..6c5a70c 100644 --- a/Geekbot.net/WebApi/WebConfig.cs +++ b/Geekbot.net/WebApi/WebConfig.cs @@ -1,4 +1,6 @@ using System.Diagnostics; +using Discord.Commands; +using Geekbot.net.Lib.Logger; using Nancy; using Nancy.Bootstrapper; using Nancy.TinyIoc; @@ -7,12 +9,26 @@ namespace Geekbot.net.WebApi { public class WebConfig : DefaultNancyBootstrapper { + private readonly GeekbotLogger _logger; + private readonly CommandService _commands; + + public WebConfig(GeekbotLogger logger, CommandService commands) + { + _logger = logger; + _commands = commands; + } + protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context) { - - //CORS Enable + // Register Dependencies + container.Register(_logger); + container.Register(_commands); + + // Enable CORS pipelines.AfterRequest.AddItemToEndOfPipeline(ctx => { + _logger.Information(LogSource.Api, ctx.Request.Path.ToString()); + ctx.Response.WithHeader("Access-Control-Allow-Origin", "*") .WithHeader("Access-Control-Allow-Methods", "GET") .WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type") From 8c107de92e29e11db7f971725ebfb774741fd798 Mon Sep 17 00:00:00 2001 From: runebaas Date: Mon, 14 May 2018 02:33:49 +0200 Subject: [PATCH 072/443] Replace nancy with kestrel --- Geekbot.net/Geekbot.net.csproj | 6 +- Geekbot.net/Program.cs | 5 +- .../Commands}/CommandDto.cs | 2 +- .../Commands}/CommandParamDto.cs | 2 +- .../Controllers/Commands/HelpController.cs | 41 ++++++++++ .../{ => Controllers}/Status/ApiStatusDto.cs | 2 +- .../Controllers/Status/StatusController.cs | 22 ++++++ Geekbot.net/WebApi/Help/HelpController.cs | 44 ----------- Geekbot.net/WebApi/Logging/AspLogProvider.cs | 29 +++++++ Geekbot.net/WebApi/Logging/AspLogger.cs | 75 +++++++++++++++++++ Geekbot.net/WebApi/Status/StatusController.cs | 22 ------ Geekbot.net/WebApi/WebApiStartup.cs | 50 +++++++++++++ Geekbot.net/WebApi/WebConfig.cs | 39 ---------- 13 files changed, 222 insertions(+), 117 deletions(-) rename Geekbot.net/WebApi/{Help => Controllers/Commands}/CommandDto.cs (85%) rename Geekbot.net/WebApi/{Help => Controllers/Commands}/CommandParamDto.cs (74%) create mode 100644 Geekbot.net/WebApi/Controllers/Commands/HelpController.cs rename Geekbot.net/WebApi/{ => Controllers}/Status/ApiStatusDto.cs (75%) create mode 100644 Geekbot.net/WebApi/Controllers/Status/StatusController.cs delete mode 100644 Geekbot.net/WebApi/Help/HelpController.cs create mode 100644 Geekbot.net/WebApi/Logging/AspLogProvider.cs create mode 100644 Geekbot.net/WebApi/Logging/AspLogger.cs delete mode 100644 Geekbot.net/WebApi/Status/StatusController.cs create mode 100644 Geekbot.net/WebApi/WebApiStartup.cs delete mode 100644 Geekbot.net/WebApi/WebConfig.cs diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 27e5dad..83a37d9 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -24,6 +24,7 @@ + @@ -34,8 +35,6 @@ - - @@ -43,9 +42,6 @@ - - 1.2.6 - diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index e5b3f59..c9b53b3 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -19,9 +19,7 @@ using Geekbot.net.Lib.Logger; using Geekbot.net.Lib.Media; using Geekbot.net.Lib.ReactionListener; using Geekbot.net.Lib.UserRepository; -using Geekbot.net.WebApi; using Microsoft.Extensions.DependencyInjection; -using Nancy.Hosting.Self; using StackExchange.Redis; using WikipediaApi; @@ -203,8 +201,7 @@ namespace Geekbot.net { _logger.Information(LogSource.Api, "Starting Webserver"); var webApiUrl = new Uri($"http://{_runParameters.ApiHost}:{_runParameters.ApiPort}"); - var webConfig = new WebConfig(_logger, _commands); - new NancyHost(webConfig, webApiUrl).Start(); + WebApi.WebApiStartup.StartWebApi(_logger, _runParameters, _commands); _logger.Information(LogSource.Api, $"Webserver now running on {webApiUrl}"); } } diff --git a/Geekbot.net/WebApi/Help/CommandDto.cs b/Geekbot.net/WebApi/Controllers/Commands/CommandDto.cs similarity index 85% rename from Geekbot.net/WebApi/Help/CommandDto.cs rename to Geekbot.net/WebApi/Controllers/Commands/CommandDto.cs index 5921fa6..50f7872 100644 --- a/Geekbot.net/WebApi/Help/CommandDto.cs +++ b/Geekbot.net/WebApi/Controllers/Commands/CommandDto.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; -namespace Geekbot.net.WebApi.Help +namespace Geekbot.net.WebApi.Controllers.Commands { public class CommandDto { diff --git a/Geekbot.net/WebApi/Help/CommandParamDto.cs b/Geekbot.net/WebApi/Controllers/Commands/CommandParamDto.cs similarity index 74% rename from Geekbot.net/WebApi/Help/CommandParamDto.cs rename to Geekbot.net/WebApi/Controllers/Commands/CommandParamDto.cs index f7ce0ea..5f7519d 100644 --- a/Geekbot.net/WebApi/Help/CommandParamDto.cs +++ b/Geekbot.net/WebApi/Controllers/Commands/CommandParamDto.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.WebApi.Help +namespace Geekbot.net.WebApi.Controllers.Commands { public class CommandParamDto { diff --git a/Geekbot.net/WebApi/Controllers/Commands/HelpController.cs b/Geekbot.net/WebApi/Controllers/Commands/HelpController.cs new file mode 100644 index 0000000..9105706 --- /dev/null +++ b/Geekbot.net/WebApi/Controllers/Commands/HelpController.cs @@ -0,0 +1,41 @@ +using System.Linq; +using Discord.Commands; +using Microsoft.AspNetCore.Cors; +using Microsoft.AspNetCore.Mvc; + +namespace Geekbot.net.WebApi.Controllers.Commands +{ + [EnableCors("AllowSpecificOrigin")] + public class HelpController : Controller + { + private readonly CommandService _commands; + + public HelpController(CommandService commands) + { + _commands = commands; + } + + [Route("/v1/commands")] + public IActionResult GetCommands() + { + var commandList = (from cmd in _commands.Commands + let cmdParamsObj = cmd.Parameters.Select(cmdParam => new CommandParamDto + { + Summary = cmdParam.Summary, + Default = cmdParam.DefaultValue?.ToString() ?? null, + Type = cmdParam.Type?.ToString() + }) + .ToList() + let param = string.Join(", !", cmd.Aliases) + select new CommandDto + { + Name = cmd.Name, + Summary = cmd.Summary, + IsAdminCommand = (param.Contains("admin")), + Aliases = cmd.Aliases.ToArray(), + Params = cmdParamsObj + }).ToList(); + return Ok(commandList); + } + } +} \ No newline at end of file diff --git a/Geekbot.net/WebApi/Status/ApiStatusDto.cs b/Geekbot.net/WebApi/Controllers/Status/ApiStatusDto.cs similarity index 75% rename from Geekbot.net/WebApi/Status/ApiStatusDto.cs rename to Geekbot.net/WebApi/Controllers/Status/ApiStatusDto.cs index 242bf58..0d5e6ad 100644 --- a/Geekbot.net/WebApi/Status/ApiStatusDto.cs +++ b/Geekbot.net/WebApi/Controllers/Status/ApiStatusDto.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.WebApi.Status +namespace Geekbot.net.WebApi.Controllers.Status { public class ApiStatusDto { diff --git a/Geekbot.net/WebApi/Controllers/Status/StatusController.cs b/Geekbot.net/WebApi/Controllers/Status/StatusController.cs new file mode 100644 index 0000000..e9af6bb --- /dev/null +++ b/Geekbot.net/WebApi/Controllers/Status/StatusController.cs @@ -0,0 +1,22 @@ +using Geekbot.net.Lib; +using Microsoft.AspNetCore.Cors; +using Microsoft.AspNetCore.Mvc; + +namespace Geekbot.net.WebApi.Controllers.Status +{ + [EnableCors("AllowSpecificOrigin")] + public class StatusController : Controller + { + [Route("/")] + public IActionResult GetCommands() + { + var responseBody = new ApiStatusDto + { + GeekbotVersion = Constants.BotVersion(), + ApiVersion = Constants.ApiVersion.ToString(), + Status = "Online" + }; + return Ok(responseBody); + } + } +} \ No newline at end of file diff --git a/Geekbot.net/WebApi/Help/HelpController.cs b/Geekbot.net/WebApi/Help/HelpController.cs deleted file mode 100644 index 8aaed12..0000000 --- a/Geekbot.net/WebApi/Help/HelpController.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System.Linq; -using System.Reflection; -using System.Threading.Tasks; -using Discord.Commands; -using Nancy; - -namespace Geekbot.net.WebApi.Help -{ - public sealed class HelpController : NancyModule - { - public HelpController(CommandService commands) - { - Get("/v1/commands", args => - { - var commandList = (from cmd in commands.Commands - let cmdParamsObj = cmd.Parameters.Select(cmdParam => new CommandParamDto - { - Summary = cmdParam.Summary, - Default = cmdParam.DefaultValue?.ToString() ?? null, - Type = cmdParam.Type?.ToString() - }) - .ToList() - let param = string.Join(", !", cmd.Aliases) - select new CommandDto - { - Name = cmd.Name, - Summary = cmd.Summary, - IsAdminCommand = (param.Contains("admin")), - Aliases = cmd.Aliases.ToArray(), - Params = cmdParamsObj - }).ToList(); - return Response.AsJson(commandList); - - }); - } - - private async Task GetCommands() - { - var commands = new CommandService(); - await commands.AddModulesAsync(Assembly.GetEntryAssembly()); - return commands; - } - } -} \ No newline at end of file diff --git a/Geekbot.net/WebApi/Logging/AspLogProvider.cs b/Geekbot.net/WebApi/Logging/AspLogProvider.cs new file mode 100644 index 0000000..9a1599a --- /dev/null +++ b/Geekbot.net/WebApi/Logging/AspLogProvider.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Concurrent; +using Geekbot.net.Lib.Logger; +using Microsoft.Extensions.Logging; + +namespace Geekbot.net.WebApi.Logging +{ + public class AspLogProvider : ILoggerProvider + { + private readonly IGeekbotLogger _geekbotLogger; + + private readonly ConcurrentDictionary _loggers = new ConcurrentDictionary(); + + public AspLogProvider(IGeekbotLogger geekbotLogger) + { + _geekbotLogger = geekbotLogger; + } + + public void Dispose() + { + _loggers.Clear(); + } + + public ILogger CreateLogger(string categoryName) + { + return _loggers.GetOrAdd(categoryName, name => new AspLogger(categoryName, _geekbotLogger)); + } + } +} \ No newline at end of file diff --git a/Geekbot.net/WebApi/Logging/AspLogger.cs b/Geekbot.net/WebApi/Logging/AspLogger.cs new file mode 100644 index 0000000..37c9de1 --- /dev/null +++ b/Geekbot.net/WebApi/Logging/AspLogger.cs @@ -0,0 +1,75 @@ +using System; +using Geekbot.net.Lib.Logger; +using Microsoft.Extensions.Logging; + +namespace Geekbot.net.WebApi.Logging +{ + public class AspLogger : ILogger + { + private readonly string _categoryName; + private readonly IGeekbotLogger _geekbotLogger; + + public AspLogger(string categoryName, IGeekbotLogger geekbotLogger) + { + geekbotLogger.Trace(LogSource.Api, $"Adding {categoryName}"); + _categoryName = categoryName; + _geekbotLogger = geekbotLogger; + } + + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) + { + switch (logLevel) + { + case LogLevel.Trace: + _geekbotLogger.Trace(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}"); + break; + case LogLevel.Debug: + _geekbotLogger.Debug(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}"); + break; + case LogLevel.Information: + _geekbotLogger.Information(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}"); + break; + case LogLevel.Warning: + _geekbotLogger.Warning(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}", exception); + break; + case LogLevel.Error: + case LogLevel.Critical: + _geekbotLogger.Error(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}", exception); + break; + default: + throw new ArgumentOutOfRangeException(nameof(logLevel)); + } + } + + public bool IsEnabled(LogLevel logLevel) + { + return !_geekbotLogger.LogAsJson() && _geekbotLogger.GetNLogger().IsEnabled(ToGeekbotLogLevel(logLevel)); + } + + public IDisposable BeginScope(TState state) + { + return null; + } + + private static NLog.LogLevel ToGeekbotLogLevel(LogLevel level) + { + switch (level) + { + case LogLevel.Trace: + return NLog.LogLevel.Trace; + case LogLevel.Debug: + return NLog.LogLevel.Debug; + case LogLevel.Information: + return NLog.LogLevel.Info; + case LogLevel.Warning: + return NLog.LogLevel.Warn; + case LogLevel.Error: + return NLog.LogLevel.Error; + case LogLevel.Critical: + return NLog.LogLevel.Fatal; + default: + throw new ArgumentOutOfRangeException(nameof(level)); + } + } + } +} \ No newline at end of file diff --git a/Geekbot.net/WebApi/Status/StatusController.cs b/Geekbot.net/WebApi/Status/StatusController.cs deleted file mode 100644 index a2c692c..0000000 --- a/Geekbot.net/WebApi/Status/StatusController.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Geekbot.net.Lib; -using Nancy; - -namespace Geekbot.net.WebApi.Status -{ - public sealed class StatusController : NancyModule - { - public StatusController() - { - Get("/", args => - { - var responseBody = new ApiStatusDto - { - GeekbotVersion = Constants.BotVersion(), - ApiVersion = Constants.ApiVersion.ToString(), - Status = "Online" - }; - return Response.AsJson(responseBody); - }); - } - } -} \ No newline at end of file diff --git a/Geekbot.net/WebApi/WebApiStartup.cs b/Geekbot.net/WebApi/WebApiStartup.cs new file mode 100644 index 0000000..675ba1b --- /dev/null +++ b/Geekbot.net/WebApi/WebApiStartup.cs @@ -0,0 +1,50 @@ +using System; +using System.Net; +using System.Reflection; +using Discord.Commands; +using Geekbot.net.Lib; +using Geekbot.net.Lib.Logger; +using Geekbot.net.WebApi.Logging; +using Microsoft.AspNetCore; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +namespace Geekbot.net.WebApi +{ + public class WebApiStartup + { + public static void StartWebApi(IGeekbotLogger logger, RunParameters runParameters, CommandService commandService) + { + WebHost.CreateDefaultBuilder() + .UseKestrel(options => + { + options.Listen(IPAddress.Any, int.Parse(runParameters.ApiPort)); + }) + .ConfigureServices(services => + { + services.AddMvc(); + services.AddSingleton(commandService); + services.AddCors(options => + { + options.AddPolicy("AllowSpecificOrigin", + builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()); + }); + }) + .Configure(app => + { + app.UseMvc(); + app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().Build()); + }) + .ConfigureLogging(logging => + { + logging.ClearProviders(); + logging.SetMinimumLevel(LogLevel.Debug); + logging.AddProvider(new AspLogProvider(logger)); + }) + .UseSetting(WebHostDefaults.ApplicationKey, typeof(Program).GetTypeInfo().Assembly.FullName) + .Build().Run(); + } + } +} \ No newline at end of file diff --git a/Geekbot.net/WebApi/WebConfig.cs b/Geekbot.net/WebApi/WebConfig.cs deleted file mode 100644 index 6c5a70c..0000000 --- a/Geekbot.net/WebApi/WebConfig.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Diagnostics; -using Discord.Commands; -using Geekbot.net.Lib.Logger; -using Nancy; -using Nancy.Bootstrapper; -using Nancy.TinyIoc; - -namespace Geekbot.net.WebApi -{ - public class WebConfig : DefaultNancyBootstrapper - { - private readonly GeekbotLogger _logger; - private readonly CommandService _commands; - - public WebConfig(GeekbotLogger logger, CommandService commands) - { - _logger = logger; - _commands = commands; - } - - protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context) - { - // Register Dependencies - container.Register(_logger); - container.Register(_commands); - - // Enable CORS - pipelines.AfterRequest.AddItemToEndOfPipeline(ctx => - { - _logger.Information(LogSource.Api, ctx.Request.Path.ToString()); - - ctx.Response.WithHeader("Access-Control-Allow-Origin", "*") - .WithHeader("Access-Control-Allow-Methods", "GET") - .WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type") - .WithHeader("Last-Modified", Process.GetCurrentProcess().StartTime.ToString()); - }); - } - } -} \ No newline at end of file From e0d6a97dca2102b8a755edb26ec688acf6296d58 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Mon, 14 May 2018 18:57:07 +0200 Subject: [PATCH 073/443] make database saves async --- Geekbot.net/Commands/Admin/Admin.cs | 16 ++++++++-------- Geekbot.net/Commands/Admin/Role.cs | 4 ++-- Geekbot.net/Commands/Randomness/Ship.cs | 2 +- Geekbot.net/Commands/Randomness/Slap.cs | 2 +- Geekbot.net/Commands/User/Karma.cs | 4 ++-- Geekbot.net/Commands/Utils/Quote/Quote.cs | 6 +++--- Geekbot.net/Database/RedisMigration.cs | 20 +++++++++----------- 7 files changed, 26 insertions(+), 28 deletions(-) diff --git a/Geekbot.net/Commands/Admin/Admin.cs b/Geekbot.net/Commands/Admin/Admin.cs index f3366a6..fc0c231 100644 --- a/Geekbot.net/Commands/Admin/Admin.cs +++ b/Geekbot.net/Commands/Admin/Admin.cs @@ -39,7 +39,7 @@ namespace Geekbot.net.Commands.Admin var guild = GetGuildSettings(Context.Guild.Id); guild.WelcomeMessage = welcomeMessage; _database.GuildSettings.Update(guild); - _database.SaveChanges(); + await _database.SaveChangesAsync(); var formatedMessage = welcomeMessage.Replace("$user", Context.User.Mention); await ReplyAsync($"Welcome message has been changed\r\nHere is an example of how it would look:\r\n{formatedMessage}"); @@ -56,7 +56,7 @@ namespace Geekbot.net.Commands.Admin var guild = GetGuildSettings(Context.Guild.Id); guild.ModChannel = channel.Id.AsLong(); _database.GuildSettings.Update(guild); - _database.SaveChanges(); + await _database.SaveChangesAsync(); var sb = new StringBuilder(); sb.AppendLine("Successfully saved mod channel, you can now do the following"); @@ -82,7 +82,7 @@ namespace Geekbot.net.Commands.Admin guild.ShowLeave = !guild.ShowLeave; _database.GuildSettings.Update(guild); - _database.SaveChanges(); + await _database.SaveChangesAsync(); await modChannel.SendMessageAsync(guild.ShowLeave ? "Saved - now sending messages here when someone leaves" : "Saved - stopping sending messages here when someone leaves" @@ -106,7 +106,7 @@ namespace Geekbot.net.Commands.Admin guild.ShowDelete = !guild.ShowDelete; _database.GuildSettings.Update(guild); - _database.SaveChanges(); + await _database.SaveChangesAsync(); await modChannel.SendMessageAsync(guild.ShowDelete ? "Saved - now sending messages here when someone deletes a message" : "Saved - stopping sending messages here when someone deletes a message" @@ -131,7 +131,7 @@ namespace Geekbot.net.Commands.Admin var guild = GetGuildSettings(Context.Guild.Id); guild.Language = language; _database.GuildSettings.Update(guild); - _database.SaveChanges(); + await _database.SaveChangesAsync(); var trans = _translation.GetDict(Context); await ReplyAsync(trans["NewLanguageSet"]); @@ -157,7 +157,7 @@ namespace Geekbot.net.Commands.Admin var guild = GetGuildSettings(Context.Guild.Id); guild.WikiLang = language; _database.GuildSettings.Update(guild); - _database.SaveChanges(); + await _database.SaveChangesAsync(); await ReplyAsync($"Now using the {language} wikipedia"); } @@ -176,7 +176,7 @@ namespace Geekbot.net.Commands.Admin var guild = GetGuildSettings(Context.Guild.Id); guild.Ping = !guild.Ping; _database.GuildSettings.Update(guild); - _database.SaveChanges(); + await _database.SaveChangesAsync(); await ReplyAsync(guild.Ping ? "i will reply to ping now" : "No more pongs..."); } catch (Exception e) @@ -194,7 +194,7 @@ namespace Geekbot.net.Commands.Admin var guild = GetGuildSettings(Context.Guild.Id); guild.Hui = !guild.Hui; _database.GuildSettings.Update(guild); - _database.SaveChanges(); + await _database.SaveChangesAsync(); await ReplyAsync(guild.Hui ? "i will reply to hui now" : "No more hui's..."); } catch (Exception e) diff --git a/Geekbot.net/Commands/Admin/Role.cs b/Geekbot.net/Commands/Admin/Role.cs index a3a649e..c4d2362 100644 --- a/Geekbot.net/Commands/Admin/Role.cs +++ b/Geekbot.net/Commands/Admin/Role.cs @@ -125,7 +125,7 @@ namespace Geekbot.net.Commands.Admin RoleId = role.Id.AsLong(), WhiteListName = roleName }); - _database.SaveChanges(); + await _database.SaveChangesAsync(); await ReplyAsync($"Added {role.Name} to the whitelist"); } catch (Exception e) @@ -146,7 +146,7 @@ namespace Geekbot.net.Commands.Admin if (roleFromDb != null) { _database.RoleSelfService.Remove(roleFromDb); - _database.SaveChanges(); + await _database.SaveChangesAsync(); await ReplyAsync($"Removed {roleName} from the whitelist"); return; } diff --git a/Geekbot.net/Commands/Randomness/Ship.cs b/Geekbot.net/Commands/Randomness/Ship.cs index 2e3588d..0f33da5 100644 --- a/Geekbot.net/Commands/Randomness/Ship.cs +++ b/Geekbot.net/Commands/Randomness/Ship.cs @@ -45,7 +45,7 @@ namespace Geekbot.net.Commands.Randomness SecondUserId = userKeys.Item2, Strength = shippingRate }); - _database.SaveChanges(); + await _database.SaveChangesAsync(); } else { diff --git a/Geekbot.net/Commands/Randomness/Slap.cs b/Geekbot.net/Commands/Randomness/Slap.cs index b37742b..fea5991 100644 --- a/Geekbot.net/Commands/Randomness/Slap.cs +++ b/Geekbot.net/Commands/Randomness/Slap.cs @@ -83,7 +83,7 @@ namespace Geekbot.net.Commands.Randomness UpdateRecieved(user.Id); UpdateGiven(Context.User.Id); - _database.SaveChanges(); + await _database.SaveChangesAsync(); } catch (Exception e) { diff --git a/Geekbot.net/Commands/User/Karma.cs b/Geekbot.net/Commands/User/Karma.cs index 2f427c8..bf2511f 100644 --- a/Geekbot.net/Commands/User/Karma.cs +++ b/Geekbot.net/Commands/User/Karma.cs @@ -51,7 +51,7 @@ namespace Geekbot.net.Commands.User actor.TimeOut = DateTimeOffset.Now; SetUser(actor); - _database.SaveChanges(); + await _database.SaveChangesAsync(); var eb = new EmbedBuilder(); eb.WithAuthor(new EmbedAuthorBuilder() @@ -98,7 +98,7 @@ namespace Geekbot.net.Commands.User actor.TimeOut = DateTimeOffset.Now; SetUser(actor); - _database.SaveChanges(); + await _database.SaveChangesAsync(); var eb = new EmbedBuilder(); eb.WithAuthor(new EmbedAuthorBuilder() diff --git a/Geekbot.net/Commands/Utils/Quote/Quote.cs b/Geekbot.net/Commands/Utils/Quote/Quote.cs index ef0cbc8..dab31a0 100644 --- a/Geekbot.net/Commands/Utils/Quote/Quote.cs +++ b/Geekbot.net/Commands/Utils/Quote/Quote.cs @@ -74,7 +74,7 @@ namespace Geekbot.net.Commands.Utils.Quote var quote = CreateQuoteObject(lastMessage); _database.Quotes.Add(quote); - _database.SaveChanges(); + await _database.SaveChangesAsync(); var embed = QuoteBuilder(quote); await ReplyAsync("**Quote Added**", false, embed.Build()); @@ -107,7 +107,7 @@ namespace Geekbot.net.Commands.Utils.Quote var quote = CreateQuoteObject(message); _database.Quotes.Add(quote); - _database.SaveChanges(); + await _database.SaveChangesAsync(); var embed = QuoteBuilder(quote); await ReplyAsync("**Quote Added**", false, embed.Build()); @@ -169,7 +169,7 @@ namespace Geekbot.net.Commands.Utils.Quote if (quote != null) { _database.Quotes.Remove(quote); - _database.SaveChanges(); + await _database.SaveChangesAsync(); var embed = QuoteBuilder(quote); await ReplyAsync($"**Removed #{id}**", false, embed.Build()); } diff --git a/Geekbot.net/Database/RedisMigration.cs b/Geekbot.net/Database/RedisMigration.cs index 25a7ee5..c7dd881 100644 --- a/Geekbot.net/Database/RedisMigration.cs +++ b/Geekbot.net/Database/RedisMigration.cs @@ -29,7 +29,7 @@ namespace Geekbot.net.Database _client = client; } - public Task Migrate() + public async Task Migrate() { _logger.Information(LogSource.Geekbot, "Starting migration process"); foreach (var guild in _client.Guilds) @@ -49,7 +49,7 @@ namespace Geekbot.net.Database var qd = JsonConvert.DeserializeObject(q); var quote = CreateQuoteObject(guild.Id, qd); _database.Quotes.Add(quote); - _database.SaveChanges(); + await _database.SaveChangesAsync(); } catch { @@ -82,7 +82,7 @@ namespace Geekbot.net.Database TimeOut = DateTimeOffset.MinValue }; _database.Karma.Add(user); - _database.SaveChanges(); + await _database.SaveChangesAsync(); } catch { @@ -114,7 +114,7 @@ namespace Geekbot.net.Database Rolls = int.Parse(q.Value) }; _database.Rolls.Add(user); - _database.SaveChanges(); + await _database.SaveChangesAsync(); } catch { @@ -148,7 +148,7 @@ namespace Geekbot.net.Database Recieved= int.Parse(gotten[int.Parse(q.Name)].Value) }; _database.Slaps.Add(user); - _database.SaveChanges(); + await _database.SaveChangesAsync(); } catch { @@ -180,7 +180,7 @@ namespace Geekbot.net.Database MessageCount= int.Parse(q.Value) }; _database.Messages.Add(user); - _database.SaveChanges(); + await _database.SaveChangesAsync(); } catch { @@ -215,7 +215,7 @@ namespace Geekbot.net.Database Strength = int.Parse(q.Value) }; _database.Ships.Add(user); - _database.SaveChanges(); + await _database.SaveChangesAsync(); done.Add(q.Name); } catch @@ -309,7 +309,7 @@ namespace Geekbot.net.Database Joined = user.CreatedAt, UsedNames = names }, model => model.UserId.Equals(user.Id.AsLong())); - _database.SaveChanges(); + await _database.SaveChangesAsync(); } catch { @@ -333,14 +333,12 @@ namespace Geekbot.net.Database Name = guild.Name, Owner = guild.Owner.Id.AsLong() }); - _database.SaveChanges(); + await _database.SaveChangesAsync(); #endregion _logger.Information(LogSource.Geekbot, $"Finished Migration for {guild.Name}"); } _logger.Information(LogSource.Geekbot, "Finished migration process"); - - return Task.CompletedTask;; } private QuoteModel CreateQuoteObject(ulong guild, QuoteObjectDto quote) From acb2b25e099b383301115f5cba88c57001ff387a Mon Sep 17 00:00:00 2001 From: runebaas Date: Tue, 15 May 2018 01:18:26 +0200 Subject: [PATCH 074/443] Resharper cleanup and fixes and remove useless asp overhead --- Geekbot.net/Commands/Admin/Admin.cs | 1 - Geekbot.net/Commands/Admin/Say.cs | 1 - Geekbot.net/Commands/Audio/Voice.cs | 2 +- Geekbot.net/Commands/Games/Pokedex.cs | 1 - Geekbot.net/Commands/Games/Roll.cs | 1 - .../Integrations/MagicTheGathering.cs | 1 - Geekbot.net/Commands/Integrations/Mal.cs | 1 - .../UbranDictionary/UrbanDictionary.cs | 1 - Geekbot.net/Commands/Randomness/Cat/Cat.cs | 1 - Geekbot.net/Commands/Randomness/CheckEm.cs | 1 - .../Randomness/Chuck/ChuckNorrisJokes.cs | 1 - .../Commands/Randomness/Dad/DadJokes.cs | 1 - Geekbot.net/Commands/Randomness/Dog/Dog.cs | 1 - Geekbot.net/Commands/Randomness/EightBall.cs | 1 - Geekbot.net/Commands/Randomness/Fortune.cs | 1 - Geekbot.net/Commands/Randomness/Gdq.cs | 1 - .../Commands/Randomness/RandomAnimals.cs | 1 - Geekbot.net/Commands/User/GuildInfo.cs | 3 +- Geekbot.net/Commands/User/Karma.cs | 1 - Geekbot.net/Commands/Utils/AvatarGetter.cs | 1 - .../Commands/Utils/Changelog/Changelog.cs | 1 - Geekbot.net/Commands/Utils/Choose.cs | 1 - Geekbot.net/Commands/Utils/Dice/Dice.cs | 1 - Geekbot.net/Commands/Utils/Emojify.cs | 1 - Geekbot.net/Commands/Utils/Help.cs | 1 - Geekbot.net/Commands/Utils/Ping.cs | 1 - Geekbot.net/Commands/Utils/Poll/Poll.cs | 6 ++-- Geekbot.net/Commands/Utils/Quote/Quote.cs | 2 -- Geekbot.net/Database/InMemoryDatabase.cs | 3 +- Geekbot.net/Database/Models/UserModel.cs | 1 - Geekbot.net/Database/RedisMigration.cs | 1 - Geekbot.net/Geekbot.net.csproj | 6 +++- Geekbot.net/Handlers.cs | 4 ++- Geekbot.net/Lib/Clients/MalClient.cs | 29 +++++++------------ Geekbot.net/Lib/Logger/DiscordLogger.cs | 1 - .../Lib/UserRepository/UserRepository.cs | 1 - Geekbot.net/Program.cs | 19 ++++++------ ...HelpController.cs => CommandController.cs} | 8 ++--- .../Controllers/Status/StatusController.cs | 5 ++-- Geekbot.net/WebApi/Logging/AspLogProvider.cs | 3 +- Geekbot.net/WebApi/Logging/AspLogger.cs | 5 ++++ Geekbot.net/WebApi/WebApiStartup.cs | 10 ++++--- Tests/Lib/EmojiConverter.test.cs | 1 - Tests/Lib/LevelCalc.test.cs | 1 - 44 files changed, 54 insertions(+), 81 deletions(-) rename Geekbot.net/WebApi/Controllers/Commands/{HelpController.cs => CommandController.cs} (83%) diff --git a/Geekbot.net/Commands/Admin/Admin.cs b/Geekbot.net/Commands/Admin/Admin.cs index fc0c231..0925e54 100644 --- a/Geekbot.net/Commands/Admin/Admin.cs +++ b/Geekbot.net/Commands/Admin/Admin.cs @@ -10,7 +10,6 @@ using Geekbot.net.Database.Models; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Localization; -using StackExchange.Redis; namespace Geekbot.net.Commands.Admin { diff --git a/Geekbot.net/Commands/Admin/Say.cs b/Geekbot.net/Commands/Admin/Say.cs index f8708cd..f3db09b 100644 --- a/Geekbot.net/Commands/Admin/Say.cs +++ b/Geekbot.net/Commands/Admin/Say.cs @@ -2,7 +2,6 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; namespace Geekbot.net.Commands.Admin diff --git a/Geekbot.net/Commands/Audio/Voice.cs b/Geekbot.net/Commands/Audio/Voice.cs index 410d8e2..169f298 100644 --- a/Geekbot.net/Commands/Audio/Voice.cs +++ b/Geekbot.net/Commands/Audio/Voice.cs @@ -65,7 +65,7 @@ namespace Geekbot.net.Commands.Audio } // [Command("ytplay")] - public async Task ytplay(string url) + public async Task Ytplay(string url) { try { diff --git a/Geekbot.net/Commands/Games/Pokedex.cs b/Geekbot.net/Commands/Games/Pokedex.cs index d4cbc0e..25f4c88 100644 --- a/Geekbot.net/Commands/Games/Pokedex.cs +++ b/Geekbot.net/Commands/Games/Pokedex.cs @@ -3,7 +3,6 @@ using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; using PokeAPI; diff --git a/Geekbot.net/Commands/Games/Roll.cs b/Geekbot.net/Commands/Games/Roll.cs index 59a3c27..1d2ab4f 100644 --- a/Geekbot.net/Commands/Games/Roll.cs +++ b/Geekbot.net/Commands/Games/Roll.cs @@ -1,7 +1,6 @@ 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; diff --git a/Geekbot.net/Commands/Integrations/MagicTheGathering.cs b/Geekbot.net/Commands/Integrations/MagicTheGathering.cs index 842c9de..968c558 100644 --- a/Geekbot.net/Commands/Integrations/MagicTheGathering.cs +++ b/Geekbot.net/Commands/Integrations/MagicTheGathering.cs @@ -4,7 +4,6 @@ using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Lib; using Geekbot.net.Lib.Converters; using Geekbot.net.Lib.ErrorHandling; using MtgApiManager.Lib.Service; diff --git a/Geekbot.net/Commands/Integrations/Mal.cs b/Geekbot.net/Commands/Integrations/Mal.cs index 36c6457..b617b44 100644 --- a/Geekbot.net/Commands/Integrations/Mal.cs +++ b/Geekbot.net/Commands/Integrations/Mal.cs @@ -3,7 +3,6 @@ using System.Threading.Tasks; using System.Web; using Discord; using Discord.Commands; -using Geekbot.net.Lib; using Geekbot.net.Lib.Clients; using Geekbot.net.Lib.ErrorHandling; diff --git a/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs b/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs index 3ad37f3..36d70b1 100644 --- a/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs +++ b/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs @@ -4,7 +4,6 @@ using System.Net.Http; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; using Newtonsoft.Json; diff --git a/Geekbot.net/Commands/Randomness/Cat/Cat.cs b/Geekbot.net/Commands/Randomness/Cat/Cat.cs index a79eb37..f46ed25 100644 --- a/Geekbot.net/Commands/Randomness/Cat/Cat.cs +++ b/Geekbot.net/Commands/Randomness/Cat/Cat.cs @@ -3,7 +3,6 @@ using System.Net.Http; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; using Newtonsoft.Json; diff --git a/Geekbot.net/Commands/Randomness/CheckEm.cs b/Geekbot.net/Commands/Randomness/CheckEm.cs index 325d7cc..94a895a 100644 --- a/Geekbot.net/Commands/Randomness/CheckEm.cs +++ b/Geekbot.net/Commands/Randomness/CheckEm.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using Discord.Commands; -using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Media; diff --git a/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs b/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs index 8e8a369..b9d906e 100644 --- a/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs +++ b/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs @@ -3,7 +3,6 @@ using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; using Discord.Commands; -using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; using Newtonsoft.Json; diff --git a/Geekbot.net/Commands/Randomness/Dad/DadJokes.cs b/Geekbot.net/Commands/Randomness/Dad/DadJokes.cs index a14a098..3945609 100644 --- a/Geekbot.net/Commands/Randomness/Dad/DadJokes.cs +++ b/Geekbot.net/Commands/Randomness/Dad/DadJokes.cs @@ -3,7 +3,6 @@ using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; using Discord.Commands; -using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; using Newtonsoft.Json; diff --git a/Geekbot.net/Commands/Randomness/Dog/Dog.cs b/Geekbot.net/Commands/Randomness/Dog/Dog.cs index 83d7a2e..6606ef5 100644 --- a/Geekbot.net/Commands/Randomness/Dog/Dog.cs +++ b/Geekbot.net/Commands/Randomness/Dog/Dog.cs @@ -3,7 +3,6 @@ using System.Net.Http; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; using Newtonsoft.Json; diff --git a/Geekbot.net/Commands/Randomness/EightBall.cs b/Geekbot.net/Commands/Randomness/EightBall.cs index cf59431..b6f37d3 100644 --- a/Geekbot.net/Commands/Randomness/EightBall.cs +++ b/Geekbot.net/Commands/Randomness/EightBall.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Threading.Tasks; using Discord.Commands; -using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; namespace Geekbot.net.Commands.Randomness diff --git a/Geekbot.net/Commands/Randomness/Fortune.cs b/Geekbot.net/Commands/Randomness/Fortune.cs index 498085a..d4f6b5f 100644 --- a/Geekbot.net/Commands/Randomness/Fortune.cs +++ b/Geekbot.net/Commands/Randomness/Fortune.cs @@ -1,6 +1,5 @@ using System.Threading.Tasks; using Discord.Commands; -using Geekbot.net.Lib; using Geekbot.net.Lib.Media; namespace Geekbot.net.Commands.Randomness diff --git a/Geekbot.net/Commands/Randomness/Gdq.cs b/Geekbot.net/Commands/Randomness/Gdq.cs index 8d0472c..c723c6f 100644 --- a/Geekbot.net/Commands/Randomness/Gdq.cs +++ b/Geekbot.net/Commands/Randomness/Gdq.cs @@ -2,7 +2,6 @@ using System.Net; using System.Threading.Tasks; using Discord.Commands; -using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; namespace Geekbot.net.Commands.Randomness diff --git a/Geekbot.net/Commands/Randomness/RandomAnimals.cs b/Geekbot.net/Commands/Randomness/RandomAnimals.cs index 608b52d..90138af 100644 --- a/Geekbot.net/Commands/Randomness/RandomAnimals.cs +++ b/Geekbot.net/Commands/Randomness/RandomAnimals.cs @@ -1,7 +1,6 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Lib; using Geekbot.net.Lib.Media; namespace Geekbot.net.Commands.Randomness diff --git a/Geekbot.net/Commands/User/GuildInfo.cs b/Geekbot.net/Commands/User/GuildInfo.cs index c475d99..6f602b1 100644 --- a/Geekbot.net/Commands/User/GuildInfo.cs +++ b/Geekbot.net/Commands/User/GuildInfo.cs @@ -4,7 +4,6 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Geekbot.net.Database; -using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Levels; @@ -42,7 +41,7 @@ namespace Geekbot.net.Commands.User var messages = _database.Messages .Where(e => e.GuildId == Context.Guild.Id.AsLong()) .Sum(e => e.MessageCount); - var level = _levelCalc.GetLevel((int) messages); + var level = _levelCalc.GetLevel(messages); eb.AddField("Server Age", $"{created.Day}/{created.Month}/{created.Year} ({age} days)"); eb.AddInlineField("Level", level) diff --git a/Geekbot.net/Commands/User/Karma.cs b/Geekbot.net/Commands/User/Karma.cs index bf2511f..8a9189d 100644 --- a/Geekbot.net/Commands/User/Karma.cs +++ b/Geekbot.net/Commands/User/Karma.cs @@ -5,7 +5,6 @@ using Discord; using Discord.Commands; using Geekbot.net.Database; using Geekbot.net.Database.Models; -using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Localization; diff --git a/Geekbot.net/Commands/Utils/AvatarGetter.cs b/Geekbot.net/Commands/Utils/AvatarGetter.cs index cf34ca3..3f74770 100644 --- a/Geekbot.net/Commands/Utils/AvatarGetter.cs +++ b/Geekbot.net/Commands/Utils/AvatarGetter.cs @@ -2,7 +2,6 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; namespace Geekbot.net.Commands.Utils diff --git a/Geekbot.net/Commands/Utils/Changelog/Changelog.cs b/Geekbot.net/Commands/Utils/Changelog/Changelog.cs index c218c66..5d47c08 100644 --- a/Geekbot.net/Commands/Utils/Changelog/Changelog.cs +++ b/Geekbot.net/Commands/Utils/Changelog/Changelog.cs @@ -7,7 +7,6 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Discord.WebSocket; -using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; using Newtonsoft.Json; diff --git a/Geekbot.net/Commands/Utils/Choose.cs b/Geekbot.net/Commands/Utils/Choose.cs index 2b9ff19..411802a 100644 --- a/Geekbot.net/Commands/Utils/Choose.cs +++ b/Geekbot.net/Commands/Utils/Choose.cs @@ -1,7 +1,6 @@ using System; using System.Threading.Tasks; using Discord.Commands; -using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Localization; diff --git a/Geekbot.net/Commands/Utils/Dice/Dice.cs b/Geekbot.net/Commands/Utils/Dice/Dice.cs index c8e31f5..7733476 100644 --- a/Geekbot.net/Commands/Utils/Dice/Dice.cs +++ b/Geekbot.net/Commands/Utils/Dice/Dice.cs @@ -4,7 +4,6 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Discord.Commands; -using Geekbot.net.Lib; namespace Geekbot.net.Commands.Utils.Dice { diff --git a/Geekbot.net/Commands/Utils/Emojify.cs b/Geekbot.net/Commands/Utils/Emojify.cs index 07adadb..f0e172e 100644 --- a/Geekbot.net/Commands/Utils/Emojify.cs +++ b/Geekbot.net/Commands/Utils/Emojify.cs @@ -1,7 +1,6 @@ using System; using System.Threading.Tasks; using Discord.Commands; -using Geekbot.net.Lib; using Geekbot.net.Lib.Converters; using Geekbot.net.Lib.ErrorHandling; diff --git a/Geekbot.net/Commands/Utils/Help.cs b/Geekbot.net/Commands/Utils/Help.cs index 4372692..ace5087 100644 --- a/Geekbot.net/Commands/Utils/Help.cs +++ b/Geekbot.net/Commands/Utils/Help.cs @@ -3,7 +3,6 @@ using System.Text; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; namespace Geekbot.net.Commands.Utils diff --git a/Geekbot.net/Commands/Utils/Ping.cs b/Geekbot.net/Commands/Utils/Ping.cs index 653a46f..18cf9cb 100644 --- a/Geekbot.net/Commands/Utils/Ping.cs +++ b/Geekbot.net/Commands/Utils/Ping.cs @@ -1,6 +1,5 @@ using System.Threading.Tasks; using Discord.Commands; -using Geekbot.net.Lib; namespace Geekbot.net.Commands.Utils { diff --git a/Geekbot.net/Commands/Utils/Poll/Poll.cs b/Geekbot.net/Commands/Utils/Poll/Poll.cs index e94e6d4..ba4e49f 100644 --- a/Geekbot.net/Commands/Utils/Poll/Poll.cs +++ b/Geekbot.net/Commands/Utils/Poll/Poll.cs @@ -5,7 +5,6 @@ using System.Text; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Lib; using Geekbot.net.Lib.Converters; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.UserRepository; @@ -167,7 +166,10 @@ namespace Geekbot.net.Commands.Utils.Poll }; results.Add(result); } - catch {} + catch + { + // ignored + } results.Sort((x, y) => y.VoteCount.CompareTo(x.VoteCount)); return results; diff --git a/Geekbot.net/Commands/Utils/Quote/Quote.cs b/Geekbot.net/Commands/Utils/Quote/Quote.cs index dab31a0..949350b 100644 --- a/Geekbot.net/Commands/Utils/Quote/Quote.cs +++ b/Geekbot.net/Commands/Utils/Quote/Quote.cs @@ -5,11 +5,9 @@ using Discord; using Discord.Commands; using Geekbot.net.Database; using Geekbot.net.Database.Models; -using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Polyfills; -using StackExchange.Redis; namespace Geekbot.net.Commands.Utils.Quote { diff --git a/Geekbot.net/Database/InMemoryDatabase.cs b/Geekbot.net/Database/InMemoryDatabase.cs index 178c0e3..2b2cfd0 100644 --- a/Geekbot.net/Database/InMemoryDatabase.cs +++ b/Geekbot.net/Database/InMemoryDatabase.cs @@ -1,5 +1,4 @@ using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Logging; namespace Geekbot.net.Database { @@ -13,6 +12,6 @@ namespace Geekbot.net.Database } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - => optionsBuilder.UseInMemoryDatabase(databaseName: _name); + => optionsBuilder.UseInMemoryDatabase(_name); } } \ No newline at end of file diff --git a/Geekbot.net/Database/Models/UserModel.cs b/Geekbot.net/Database/Models/UserModel.cs index 0083ada..f14ed61 100644 --- a/Geekbot.net/Database/Models/UserModel.cs +++ b/Geekbot.net/Database/Models/UserModel.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.ComponentModel.DataAnnotations; namespace Geekbot.net.Database.Models diff --git a/Geekbot.net/Database/RedisMigration.cs b/Geekbot.net/Database/RedisMigration.cs index c7dd881..005aa69 100644 --- a/Geekbot.net/Database/RedisMigration.cs +++ b/Geekbot.net/Database/RedisMigration.cs @@ -8,7 +8,6 @@ using Geekbot.net.Commands.Utils.Quote; using Geekbot.net.Database.Models; using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Logger; -using MtgApiManager.Lib.Model; using Newtonsoft.Json; using StackExchange.Redis; diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 83a37d9..124dd6d 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -24,7 +24,11 @@ - + + + + + diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index b901c1f..f9fbc62 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -25,7 +25,9 @@ namespace Geekbot.net private readonly IUserRepository _userRepository; private readonly IReactionListener _reactionListener; - public Handlers(DatabaseContext database, IDiscordClient client, IGeekbotLogger logger, IDatabase redis, IServiceProvider servicesProvider, CommandService commands, IUserRepository userRepository, IReactionListener reactionListener) + public Handlers(DatabaseContext database, IDiscordClient client, IGeekbotLogger logger, IDatabase redis, + IServiceProvider servicesProvider, CommandService commands, IUserRepository userRepository, + IReactionListener reactionListener) { _database = database; _client = client; diff --git a/Geekbot.net/Lib/Clients/MalClient.cs b/Geekbot.net/Lib/Clients/MalClient.cs index 95d7dbd..a014cf1 100644 --- a/Geekbot.net/Lib/Clients/MalClient.cs +++ b/Geekbot.net/Lib/Clients/MalClient.cs @@ -1,45 +1,38 @@ using System.Threading.Tasks; +using Geekbot.net.Lib.GlobalSettings; using Geekbot.net.Lib.Logger; using MyAnimeListSharp.Auth; using MyAnimeListSharp.Core; using MyAnimeListSharp.Facade.Async; -using StackExchange.Redis; namespace Geekbot.net.Lib.Clients { public class MalClient : IMalClient { - private readonly IDatabase _redis; + private readonly IGlobalSettings _globalSettings; private readonly IGeekbotLogger _logger; private ICredentialContext _credentials; private AnimeSearchMethodsAsync _animeSearch; private MangaSearchMethodsAsync _mangaSearch; - public MalClient(IDatabase redis, IGeekbotLogger logger) + public MalClient(IGlobalSettings globalSettings, IGeekbotLogger logger) { - _redis = redis; + _globalSettings = globalSettings; _logger = logger; ReloadClient(); } public bool ReloadClient() { - var malCredentials = _redis.HashGetAll("malCredentials"); - if (malCredentials.Length != 0) + var malCredentials = _globalSettings.GetKey("MalCredentials"); + if (!string.IsNullOrEmpty(malCredentials)) { - _credentials = new CredentialContext(); - foreach (var c in malCredentials) + var credSplit = malCredentials.Split('|'); + _credentials = new CredentialContext() { - switch (c.Name) - { - case "Username": - _credentials.UserName = c.Value; - break; - case "Password": - _credentials.Password = c.Value; - break; - } - } + UserName = credSplit[0], + Password = credSplit[1] + }; _animeSearch = new AnimeSearchMethodsAsync(_credentials); _mangaSearch = new MangaSearchMethodsAsync(_credentials); _logger.Debug(LogSource.Geekbot, "Logged in to MAL"); diff --git a/Geekbot.net/Lib/Logger/DiscordLogger.cs b/Geekbot.net/Lib/Logger/DiscordLogger.cs index 2adcd73..688e26b 100644 --- a/Geekbot.net/Lib/Logger/DiscordLogger.cs +++ b/Geekbot.net/Lib/Logger/DiscordLogger.cs @@ -1,7 +1,6 @@ using System; using System.Threading.Tasks; using Discord; -using Geekbot.net.Commands.Randomness.Cat; namespace Geekbot.net.Lib.Logger { diff --git a/Geekbot.net/Lib/UserRepository/UserRepository.cs b/Geekbot.net/Lib/UserRepository/UserRepository.cs index 0125ee5..263a2f3 100644 --- a/Geekbot.net/Lib/UserRepository/UserRepository.cs +++ b/Geekbot.net/Lib/UserRepository/UserRepository.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Discord.WebSocket; diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index c9b53b3..f94a785 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -53,6 +53,7 @@ namespace Geekbot.net logo.AppendLine(@"| |_| | |___| |___| . \| |_) | |_| || |"); logo.AppendLine(@" \____|_____|_____|_|\_\____/ \___/ |_|"); logo.AppendLine("========================================="); + logo.AppendLine($"Version {Constants.BotVersion()}"); Console.WriteLine(logo.ToString()); var sumologicActive = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GEEKBOT_SUMO")); var logger = new GeekbotLogger(runParameters, sumologicActive); @@ -112,7 +113,7 @@ namespace Geekbot.net _userRepository = new UserRepository(_database, logger); var fortunes = new FortunesProvider(logger); var mediaProvider = new MediaProvider(logger); - var malClient = new MalClient(_redis, logger); + var malClient = new MalClient(_globalSettings, logger); var levelCalc = new LevelCalc(); var emojiConverter = new EmojiConverter(); var mtgManaConverter = new MtgManaConverter(); @@ -174,13 +175,12 @@ namespace Geekbot.net _client.UserLeft += handlers.UserLeft; _client.ReactionAdded += handlers.ReactionAdded; _client.ReactionRemoved += handlers.ReactionRemoved; - - if (!_runParameters.DisableApi) - { - StartWebApi(); - } + + var webserver = _runParameters.DisableApi ? Task.Delay(10) : StartWebApi(); _logger.Information(LogSource.Geekbot, "Done and ready for use"); + + await webserver; } } catch (Exception e) @@ -197,12 +197,11 @@ namespace Geekbot.net return true; } - private void StartWebApi() + private Task StartWebApi() { _logger.Information(LogSource.Api, "Starting Webserver"); - var webApiUrl = new Uri($"http://{_runParameters.ApiHost}:{_runParameters.ApiPort}"); - WebApi.WebApiStartup.StartWebApi(_logger, _runParameters, _commands); - _logger.Information(LogSource.Api, $"Webserver now running on {webApiUrl}"); + WebApi.WebApiStartup.StartWebApi(_logger, _runParameters, _commands, _database); + return Task.CompletedTask; } } } \ No newline at end of file diff --git a/Geekbot.net/WebApi/Controllers/Commands/HelpController.cs b/Geekbot.net/WebApi/Controllers/Commands/CommandController.cs similarity index 83% rename from Geekbot.net/WebApi/Controllers/Commands/HelpController.cs rename to Geekbot.net/WebApi/Controllers/Commands/CommandController.cs index 9105706..04caaae 100644 --- a/Geekbot.net/WebApi/Controllers/Commands/HelpController.cs +++ b/Geekbot.net/WebApi/Controllers/Commands/CommandController.cs @@ -6,11 +6,11 @@ using Microsoft.AspNetCore.Mvc; namespace Geekbot.net.WebApi.Controllers.Commands { [EnableCors("AllowSpecificOrigin")] - public class HelpController : Controller + public class CommandController : Controller { private readonly CommandService _commands; - public HelpController(CommandService commands) + public CommandController(CommandService commands) { _commands = commands; } @@ -22,7 +22,7 @@ namespace Geekbot.net.WebApi.Controllers.Commands let cmdParamsObj = cmd.Parameters.Select(cmdParam => new CommandParamDto { Summary = cmdParam.Summary, - Default = cmdParam.DefaultValue?.ToString() ?? null, + Default = cmdParam.DefaultValue?.ToString(), Type = cmdParam.Type?.ToString() }) .ToList() @@ -31,7 +31,7 @@ namespace Geekbot.net.WebApi.Controllers.Commands { Name = cmd.Name, Summary = cmd.Summary, - IsAdminCommand = (param.Contains("admin")), + IsAdminCommand = param.Contains("admin") || param.Contains("owner"), Aliases = cmd.Aliases.ToArray(), Params = cmdParamsObj }).ToList(); diff --git a/Geekbot.net/WebApi/Controllers/Status/StatusController.cs b/Geekbot.net/WebApi/Controllers/Status/StatusController.cs index e9af6bb..c437e64 100644 --- a/Geekbot.net/WebApi/Controllers/Status/StatusController.cs +++ b/Geekbot.net/WebApi/Controllers/Status/StatusController.cs @@ -1,4 +1,5 @@ -using Geekbot.net.Lib; +using System.Globalization; +using Geekbot.net.Lib; using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Mvc; @@ -13,7 +14,7 @@ namespace Geekbot.net.WebApi.Controllers.Status var responseBody = new ApiStatusDto { GeekbotVersion = Constants.BotVersion(), - ApiVersion = Constants.ApiVersion.ToString(), + ApiVersion = Constants.ApiVersion.ToString(CultureInfo.InvariantCulture), Status = "Online" }; return Ok(responseBody); diff --git a/Geekbot.net/WebApi/Logging/AspLogProvider.cs b/Geekbot.net/WebApi/Logging/AspLogProvider.cs index 9a1599a..d35872d 100644 --- a/Geekbot.net/WebApi/Logging/AspLogProvider.cs +++ b/Geekbot.net/WebApi/Logging/AspLogProvider.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Concurrent; +using System.Collections.Concurrent; using Geekbot.net.Lib.Logger; using Microsoft.Extensions.Logging; diff --git a/Geekbot.net/WebApi/Logging/AspLogger.cs b/Geekbot.net/WebApi/Logging/AspLogger.cs index 37c9de1..85dd619 100644 --- a/Geekbot.net/WebApi/Logging/AspLogger.cs +++ b/Geekbot.net/WebApi/Logging/AspLogger.cs @@ -36,6 +36,8 @@ namespace Geekbot.net.WebApi.Logging case LogLevel.Critical: _geekbotLogger.Error(LogSource.Api, $"{eventId.Id} - {_categoryName} - {state}", exception); break; + case LogLevel.None: + break; default: throw new ArgumentOutOfRangeException(nameof(logLevel)); } @@ -67,6 +69,9 @@ namespace Geekbot.net.WebApi.Logging return NLog.LogLevel.Error; case LogLevel.Critical: return NLog.LogLevel.Fatal; + case LogLevel.None: + return NLog.LogLevel.Off; + break; default: throw new ArgumentOutOfRangeException(nameof(level)); } diff --git a/Geekbot.net/WebApi/WebApiStartup.cs b/Geekbot.net/WebApi/WebApiStartup.cs index 675ba1b..e91ff9f 100644 --- a/Geekbot.net/WebApi/WebApiStartup.cs +++ b/Geekbot.net/WebApi/WebApiStartup.cs @@ -1,7 +1,7 @@ -using System; -using System.Net; +using System.Net; using System.Reflection; using Discord.Commands; +using Geekbot.net.Database; using Geekbot.net.Lib; using Geekbot.net.Lib.Logger; using Geekbot.net.WebApi.Logging; @@ -13,9 +13,10 @@ using Microsoft.Extensions.Logging; namespace Geekbot.net.WebApi { - public class WebApiStartup + public static class WebApiStartup { - public static void StartWebApi(IGeekbotLogger logger, RunParameters runParameters, CommandService commandService) + public static void StartWebApi(IGeekbotLogger logger, RunParameters runParameters, CommandService commandService, + DatabaseContext databaseContext) { WebHost.CreateDefaultBuilder() .UseKestrel(options => @@ -26,6 +27,7 @@ namespace Geekbot.net.WebApi { services.AddMvc(); services.AddSingleton(commandService); + services.AddSingleton(databaseContext); services.AddCors(options => { options.AddPolicy("AllowSpecificOrigin", diff --git a/Tests/Lib/EmojiConverter.test.cs b/Tests/Lib/EmojiConverter.test.cs index aaaaa8f..170d521 100644 --- a/Tests/Lib/EmojiConverter.test.cs +++ b/Tests/Lib/EmojiConverter.test.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using Geekbot.net.Lib; using Geekbot.net.Lib.Converters; using Xunit; diff --git a/Tests/Lib/LevelCalc.test.cs b/Tests/Lib/LevelCalc.test.cs index 4b6b095..5ac5fd3 100644 --- a/Tests/Lib/LevelCalc.test.cs +++ b/Tests/Lib/LevelCalc.test.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using Geekbot.net.Lib; using Geekbot.net.Lib.Levels; using Xunit; From 4e4ef3584ee8d7db33ce3816d6bc41da2d52b3b4 Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 17 May 2018 22:06:58 +0200 Subject: [PATCH 075/443] Upgrade to discord.net 2.0 --- Geekbot.net/Commands/Games/Pokedex.cs | 1 + .../Commands/Integrations/MagicTheGathering.cs | 5 +++-- Geekbot.net/Commands/Integrations/Mal.cs | 1 + .../Integrations/UbranDictionary/UrbanDictionary.cs | 1 + Geekbot.net/Commands/Randomness/RandomAnimals.cs | 6 ++---- Geekbot.net/Commands/Utils/Info.cs | 1 + Geekbot.net/Commands/Utils/Poll/Poll.cs | 1 + Geekbot.net/Commands/Utils/Quote/Quote.cs | 11 +++++------ Geekbot.net/Geekbot.net.csproj | 12 ++++++------ Geekbot.net/Lib/Extensions/EmbedBuilderExtensions.cs | 12 ++++++++++++ Geekbot.net/Lib/Polyfills/UserPolyfillDto.cs | 2 +- Tests/Tests.csproj | 2 +- 12 files changed, 35 insertions(+), 20 deletions(-) create mode 100644 Geekbot.net/Lib/Extensions/EmbedBuilderExtensions.cs diff --git a/Geekbot.net/Commands/Games/Pokedex.cs b/Geekbot.net/Commands/Games/Pokedex.cs index 25f4c88..aea442b 100644 --- a/Geekbot.net/Commands/Games/Pokedex.cs +++ b/Geekbot.net/Commands/Games/Pokedex.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.Extensions; using PokeAPI; namespace Geekbot.net.Commands.Games diff --git a/Geekbot.net/Commands/Integrations/MagicTheGathering.cs b/Geekbot.net/Commands/Integrations/MagicTheGathering.cs index 968c558..2284646 100644 --- a/Geekbot.net/Commands/Integrations/MagicTheGathering.cs +++ b/Geekbot.net/Commands/Integrations/MagicTheGathering.cs @@ -6,16 +6,17 @@ using Discord; using Discord.Commands; using Geekbot.net.Lib.Converters; using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.Extensions; using MtgApiManager.Lib.Service; namespace Geekbot.net.Commands.Integrations { - public class Magicthegathering : ModuleBase + public class MagicTheGathering : ModuleBase { private readonly IErrorHandler _errorHandler; private readonly IMtgManaConverter _manaConverter; - public Magicthegathering(IErrorHandler errorHandler, IMtgManaConverter manaConverter) + public MagicTheGathering(IErrorHandler errorHandler, IMtgManaConverter manaConverter) { _errorHandler = errorHandler; _manaConverter = manaConverter; diff --git a/Geekbot.net/Commands/Integrations/Mal.cs b/Geekbot.net/Commands/Integrations/Mal.cs index b617b44..da03ead 100644 --- a/Geekbot.net/Commands/Integrations/Mal.cs +++ b/Geekbot.net/Commands/Integrations/Mal.cs @@ -5,6 +5,7 @@ using Discord; using Discord.Commands; using Geekbot.net.Lib.Clients; using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.Extensions; namespace Geekbot.net.Commands.Integrations { diff --git a/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs b/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs index 36d70b1..928dfe2 100644 --- a/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs +++ b/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.Extensions; using Newtonsoft.Json; namespace Geekbot.net.Commands.Integrations.UbranDictionary diff --git a/Geekbot.net/Commands/Randomness/RandomAnimals.cs b/Geekbot.net/Commands/Randomness/RandomAnimals.cs index 90138af..38061b1 100644 --- a/Geekbot.net/Commands/Randomness/RandomAnimals.cs +++ b/Geekbot.net/Commands/Randomness/RandomAnimals.cs @@ -65,11 +65,9 @@ namespace Geekbot.net.Commands.Randomness await ReplyAsync("", false, Eb(_mediaProvider.GetFox())); } - private EmbedBuilder Eb(string image) + private static Embed Eb(string image) { - var eb = new EmbedBuilder(); - eb.ImageUrl = image; - return eb; + return new EmbedBuilder {ImageUrl = image}.Build(); } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/Utils/Info.cs b/Geekbot.net/Commands/Utils/Info.cs index e7b104f..f0900fa 100644 --- a/Geekbot.net/Commands/Utils/Info.cs +++ b/Geekbot.net/Commands/Utils/Info.cs @@ -7,6 +7,7 @@ using Discord.Commands; using Discord.WebSocket; using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.Extensions; namespace Geekbot.net.Commands.Utils { diff --git a/Geekbot.net/Commands/Utils/Poll/Poll.cs b/Geekbot.net/Commands/Utils/Poll/Poll.cs index ba4e49f..aad6eee 100644 --- a/Geekbot.net/Commands/Utils/Poll/Poll.cs +++ b/Geekbot.net/Commands/Utils/Poll/Poll.cs @@ -7,6 +7,7 @@ using Discord; using Discord.Commands; using Geekbot.net.Lib.Converters; using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.UserRepository; using Newtonsoft.Json; using StackExchange.Redis; diff --git a/Geekbot.net/Commands/Utils/Quote/Quote.cs b/Geekbot.net/Commands/Utils/Quote/Quote.cs index 949350b..6af74d7 100644 --- a/Geekbot.net/Commands/Utils/Quote/Quote.cs +++ b/Geekbot.net/Commands/Utils/Quote/Quote.cs @@ -187,12 +187,11 @@ namespace Geekbot.net.Commands.Utils.Quote try { var list = Context.Channel.GetMessagesAsync().Flatten(); - await list; - return list.Result - .First(msg => msg.Author.Id == user.Id - && msg.Embeds.Count == 0 - && msg.Id != Context.Message.Id - && !msg.Content.ToLower().StartsWith("!")); + return await list.FirstOrDefault(msg => + msg.Author.Id == user.Id && + msg.Embeds.Count == 0 && + msg.Id != Context.Message.Id && + !msg.Content.ToLower().StartsWith("!")); } catch { diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 124dd6d..9b0fac6 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -20,10 +20,10 @@ - 1.0.2 + 2.0.0-beta - - + + @@ -40,12 +40,12 @@ - - + + - + diff --git a/Geekbot.net/Lib/Extensions/EmbedBuilderExtensions.cs b/Geekbot.net/Lib/Extensions/EmbedBuilderExtensions.cs new file mode 100644 index 0000000..246be44 --- /dev/null +++ b/Geekbot.net/Lib/Extensions/EmbedBuilderExtensions.cs @@ -0,0 +1,12 @@ +using Discord; + +namespace Geekbot.net.Lib.Extensions +{ + public static class EmbedBuilderExtensions + { + public static EmbedBuilder AddInlineField(this EmbedBuilder builder, string name, object value) + { + return builder.AddField(new EmbedFieldBuilder().WithIsInline(true).WithName(name).WithValue(value)); + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/Polyfills/UserPolyfillDto.cs b/Geekbot.net/Lib/Polyfills/UserPolyfillDto.cs index 407ae36..568bdd7 100644 --- a/Geekbot.net/Lib/Polyfills/UserPolyfillDto.cs +++ b/Geekbot.net/Lib/Polyfills/UserPolyfillDto.cs @@ -9,7 +9,7 @@ namespace Geekbot.net.Lib.Polyfills public ulong Id { get; set; } public DateTimeOffset CreatedAt { get; set; } public string Mention { get; set; } - public Game? Game { get; set; } + public IActivity Activity { get; } public UserStatus Status { get; set; } public string AvatarId { get; set; } public string Discriminator { get; set; } diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 776acf6..4713b2b 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -5,7 +5,7 @@ NU1701 - + From 6d708627f1f16f809b7faf98885ad001634245b8 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Thu, 17 May 2018 23:55:44 +0200 Subject: [PATCH 076/443] Add Dockerfile --- Dockerfile | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..7594bec --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +FROM microsoft/dotnet:2.1-aspnetcore-runtime + +COPY Geekbot.net/Binaries /app/ + +EXPOSE 12995/tcp +WORKDIR /app +ENTRYPOINT ./run.sh From 74793c8ef76d3f15bbf77b51ce0cc08d49b796ce Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 18 May 2018 00:34:45 +0200 Subject: [PATCH 077/443] Dynamically get discord.net version in !info --- Geekbot.net/Commands/Utils/Info.cs | 10 +++++----- Geekbot.net/Lib/Constants.cs | 5 +++++ Geekbot.net/WebApi/Logging/AspLogger.cs | 1 - 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Geekbot.net/Commands/Utils/Info.cs b/Geekbot.net/Commands/Utils/Info.cs index f0900fa..aca4dcd 100644 --- a/Geekbot.net/Commands/Utils/Info.cs +++ b/Geekbot.net/Commands/Utils/Info.cs @@ -32,19 +32,19 @@ namespace Geekbot.net.Commands.Utils { var eb = new EmbedBuilder(); + var appInfo = await _client.GetApplicationInfoAsync(); + eb.WithAuthor(new EmbedAuthorBuilder() - .WithIconUrl(_client.CurrentUser.GetAvatarUrl()) + .WithIconUrl(appInfo.IconUrl) .WithName($"{Constants.Name} V{Constants.BotVersion()}")); - var botOwner = (await _client.GetApplicationInfoAsync()).Owner; var uptime = DateTime.Now.Subtract(Process.GetCurrentProcess().StartTime); eb.AddInlineField("Bot Name", _client.CurrentUser.Username); - eb.AddInlineField("Bot Owner", $"{botOwner.Username}#{botOwner.Discriminator}"); - eb.AddInlineField("Library", "Discord.NET V1.0.2"); + eb.AddInlineField("Bot Owner", $"{appInfo.Owner.Username}#{appInfo.Owner.Discriminator}"); + eb.AddInlineField("Library", $"Discord.NET {Constants.LibraryVersion()}"); eb.AddInlineField("Uptime", $"{uptime.Days}D {uptime.Hours}H {uptime.Minutes}M {uptime.Seconds}S"); eb.AddInlineField("Servers", Context.Client.GetGuildsAsync().Result.Count); eb.AddInlineField("Total Commands", _commands.Commands.Count()); - eb.AddField("Website", "https://geekbot.pizzaandcoffee.rocks/"); await ReplyAsync("", false, eb.Build()); diff --git a/Geekbot.net/Lib/Constants.cs b/Geekbot.net/Lib/Constants.cs index f80f182..4abee4e 100644 --- a/Geekbot.net/Lib/Constants.cs +++ b/Geekbot.net/Lib/Constants.cs @@ -11,6 +11,11 @@ namespace Geekbot.net.Lib return typeof(Program).Assembly.GetCustomAttribute().InformationalVersion; } + public static string LibraryVersion() + { + return typeof(Discord.WebSocket.DiscordSocketClient).Assembly.GetCustomAttribute().InformationalVersion; + } + public const double ApiVersion = 1; } } \ No newline at end of file diff --git a/Geekbot.net/WebApi/Logging/AspLogger.cs b/Geekbot.net/WebApi/Logging/AspLogger.cs index 85dd619..3316150 100644 --- a/Geekbot.net/WebApi/Logging/AspLogger.cs +++ b/Geekbot.net/WebApi/Logging/AspLogger.cs @@ -71,7 +71,6 @@ namespace Geekbot.net.WebApi.Logging return NLog.LogLevel.Fatal; case LogLevel.None: return NLog.LogLevel.Off; - break; default: throw new ArgumentOutOfRangeException(nameof(level)); } From 9354e5f83e62cdf18c7f6578f608ecba84551f60 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 19 May 2018 10:49:01 +0200 Subject: [PATCH 078/443] Change User Model and Add poll model, port !poll but ended up disabling it --- Geekbot.net/Commands/Admin/Mod.cs | 2 +- Geekbot.net/Commands/Utils/{Poll => }/Poll.cs | 103 +++++++++++------- .../Commands/Utils/Poll/PollDataDto.cs | 13 --- .../Commands/Utils/Poll/PollResultDto.cs | 8 -- Geekbot.net/Database/DatabaseContext.cs | 3 +- Geekbot.net/Database/DatabaseInitializer.cs | 2 +- Geekbot.net/Database/Models/PollModel.cs | 27 +++++ .../Database/Models/PollQuestionModel.cs | 16 +++ Geekbot.net/Database/Models/UserModel.cs | 3 +- .../Database/Models/UserSettingsModel.cs | 15 --- .../Database/Models/UserUsedNamesModel.cs | 15 +++ Geekbot.net/Database/RedisMigration.cs | 16 +-- .../Lib/UserRepository/UserRepository.cs | 7 +- 13 files changed, 138 insertions(+), 92 deletions(-) rename Geekbot.net/Commands/Utils/{Poll => }/Poll.cs (63%) delete mode 100644 Geekbot.net/Commands/Utils/Poll/PollDataDto.cs delete mode 100644 Geekbot.net/Commands/Utils/Poll/PollResultDto.cs create mode 100644 Geekbot.net/Database/Models/PollModel.cs create mode 100644 Geekbot.net/Database/Models/PollQuestionModel.cs delete mode 100644 Geekbot.net/Database/Models/UserSettingsModel.cs create mode 100644 Geekbot.net/Database/Models/UserUsedNamesModel.cs diff --git a/Geekbot.net/Commands/Admin/Mod.cs b/Geekbot.net/Commands/Admin/Mod.cs index 2e4ad24..a200cac 100644 --- a/Geekbot.net/Commands/Admin/Mod.cs +++ b/Geekbot.net/Commands/Admin/Mod.cs @@ -35,7 +35,7 @@ namespace Geekbot.net.Commands.Admin var userRepo = _userRepository.Get(user.Id); var sb = new StringBuilder(); sb.AppendLine($":bust_in_silhouette: {user.Username} has been known as:"); - foreach (var name in userRepo.UsedNames) sb.AppendLine($"- `{name}`"); + foreach (var name in userRepo.UsedNames) sb.AppendLine($"- `{name.Name}`"); await ReplyAsync(sb.ToString()); } catch (Exception e) diff --git a/Geekbot.net/Commands/Utils/Poll/Poll.cs b/Geekbot.net/Commands/Utils/Poll.cs similarity index 63% rename from Geekbot.net/Commands/Utils/Poll/Poll.cs rename to Geekbot.net/Commands/Utils/Poll.cs index aad6eee..585c1fb 100644 --- a/Geekbot.net/Commands/Utils/Poll/Poll.cs +++ b/Geekbot.net/Commands/Utils/Poll.cs @@ -5,28 +5,27 @@ using System.Text; using System.Threading.Tasks; using Discord; using Discord.Commands; +using Geekbot.net.Database; +using Geekbot.net.Database.Models; using Geekbot.net.Lib.Converters; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.UserRepository; -using Newtonsoft.Json; -using StackExchange.Redis; -namespace Geekbot.net.Commands.Utils.Poll +namespace Geekbot.net.Commands.Utils { [Group("poll")] public class Poll : ModuleBase { private readonly IEmojiConverter _emojiConverter; private readonly IErrorHandler _errorHandler; - private readonly IDatabase _redis; + private readonly DatabaseContext _database; private readonly IUserRepository _userRepository; - public Poll(IErrorHandler errorHandler, IDatabase redis, IEmojiConverter emojiConverter, - IUserRepository userRepository) + public Poll(IErrorHandler errorHandler, DatabaseContext database, IEmojiConverter emojiConverter, IUserRepository userRepository) { _errorHandler = errorHandler; - _redis = redis; + _database = database; _emojiConverter = emojiConverter; _userRepository = userRepository; } @@ -38,7 +37,7 @@ namespace Geekbot.net.Commands.Utils.Poll try { var currentPoll = GetCurrentPoll(); - if (currentPoll.Question == null || currentPoll.IsFinshed) + if (currentPoll.Question == null) { await ReplyAsync( "There is no poll in this channel ongoing at the moment\r\nYou can create one with `!poll create question;option1;option2;option3`"); @@ -60,6 +59,9 @@ namespace Geekbot.net.Commands.Utils.Poll { try { + await ReplyAsync("Poll creation currently disabled"); + return; + var currentPoll = GetCurrentPoll(); if (currentPoll.Question != null && !currentPoll.IsFinshed) { @@ -75,34 +77,46 @@ namespace Geekbot.net.Commands.Utils.Poll return; } - var eb = new EmbedBuilder(); - eb.Title = $"Poll by {Context.User.Username}"; var question = pollList[0]; - eb.Description = question; pollList.RemoveAt(0); + + var eb = new EmbedBuilder + { + Title = $"Poll by {Context.User.Username}", + Description = question + }; + + var options = new List(); var i = 1; pollList.ForEach(option => { + options.Add(new PollQuestionModel() + { + OptionId = i, + OptionText = option + }); eb.AddInlineField($"Option {_emojiConverter.NumberToEmoji(i)}", option); i++; }); var pollMessage = await ReplyAsync("", false, eb.Build()); + + var poll = new PollModel() + { + Creator = Context.User.Id.AsLong(), + MessageId = pollMessage.Id.AsLong(), + IsFinshed = false, + Question = question, + Options = options + }; + _database.Polls.Add(poll); + i = 1; pollList.ForEach(option => { pollMessage.AddReactionAsync(new Emoji(_emojiConverter.NumberToEmoji(i))); + Task.Delay(500); i++; }); - var poll = new PollDataDto - { - Creator = Context.User.Id, - MessageId = pollMessage.Id, - IsFinshed = false, - Question = question, - Options = pollList - }; - var pollJson = JsonConvert.SerializeObject(poll); - _redis.HashSet($"{Context.Guild.Id}:Polls", new[] {new HashEntry(Context.Channel.Id, pollJson)}); } catch (Exception e) { @@ -123,15 +137,14 @@ namespace Geekbot.net.Commands.Utils.Poll return; } - var results = await GetPollResults(currentPoll); + currentPoll = await GetPollResults(currentPoll); var sb = new StringBuilder(); sb.AppendLine("**Poll Results**"); sb.AppendLine(currentPoll.Question); - foreach (var result in results) sb.AppendLine($"{result.VoteCount} - {result.Option}"); + foreach (var result in currentPoll.Options) sb.AppendLine($"{result.Votes} - {result.OptionText}"); await ReplyAsync(sb.ToString()); currentPoll.IsFinshed = true; - var pollJson = JsonConvert.SerializeObject(currentPoll); - _redis.HashSet($"{Context.Guild.Id}:Polls", new[] {new HashEntry(Context.Channel.Id, pollJson)}); + _database.Polls.Update(currentPoll); } catch (Exception e) { @@ -139,41 +152,49 @@ namespace Geekbot.net.Commands.Utils.Poll } } - private PollDataDto GetCurrentPoll() + private PollModel GetCurrentPoll() { try { - var currentPoll = _redis.HashGet($"{Context.Guild.Id}:Polls", Context.Channel.Id); - return JsonConvert.DeserializeObject(currentPoll.ToString()); + var currentPoll = _database.Polls.FirstOrDefault(poll => + poll.ChannelId.Equals(Context.Channel.Id.AsLong()) && + poll.GuildId.Equals(Context.Guild.Id.AsLong()) + ); + return currentPoll ?? new PollModel(); + } catch { - return new PollDataDto(); + return new PollModel(); } } - private async Task> GetPollResults(PollDataDto poll) + private async Task GetPollResults(PollModel poll) { - var message = (IUserMessage) await Context.Channel.GetMessageAsync(poll.MessageId); - var results = new List(); + var message = (IUserMessage) await Context.Channel.GetMessageAsync(poll.MessageId.AsUlong()); + + var results = new Dictionary(); foreach (var r in message.Reactions) + { try { - var option = int.Parse(r.Key.Name.ToCharArray()[0].ToString()); - var result = new PollResultDto - { - Option = poll.Options[option - 1], - VoteCount = r.Value.ReactionCount - }; - results.Add(result); + results.Add(r.Key.Name.ToCharArray()[0], r.Value.ReactionCount); } catch { // ignored } + } + + foreach (var q in poll.Options) + { + q.Votes = results.FirstOrDefault(e => e.Key.Equals(q.OptionId)).Value; + } - results.Sort((x, y) => y.VoteCount.CompareTo(x.VoteCount)); - return results; + return poll; + +// var sortedValues = results.OrderBy(e => e.Value); +// return sortedValues; } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/Utils/Poll/PollDataDto.cs b/Geekbot.net/Commands/Utils/Poll/PollDataDto.cs deleted file mode 100644 index 3d21479..0000000 --- a/Geekbot.net/Commands/Utils/Poll/PollDataDto.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System.Collections.Generic; - -namespace Geekbot.net.Commands.Utils.Poll -{ - internal class PollDataDto - { - public ulong Creator { get; set; } - public ulong MessageId { get; set; } - public bool IsFinshed { get; set; } - public string Question { get; set; } - public List Options { get; set; } - } -} \ No newline at end of file diff --git a/Geekbot.net/Commands/Utils/Poll/PollResultDto.cs b/Geekbot.net/Commands/Utils/Poll/PollResultDto.cs deleted file mode 100644 index b4f14a2..0000000 --- a/Geekbot.net/Commands/Utils/Poll/PollResultDto.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Geekbot.net.Commands.Utils.Poll -{ - internal class PollResultDto - { - public string Option { get; set; } - public int VoteCount { get; set; } - } -} \ No newline at end of file diff --git a/Geekbot.net/Database/DatabaseContext.cs b/Geekbot.net/Database/DatabaseContext.cs index e9de32c..ab786b1 100644 --- a/Geekbot.net/Database/DatabaseContext.cs +++ b/Geekbot.net/Database/DatabaseContext.cs @@ -16,7 +16,6 @@ namespace Geekbot.net.Database public DbSet Slaps { get; set; } public DbSet Globals { get; set; } public DbSet RoleSelfService { get; set; } - - // public DbSet UserSettings { get; set; } + public DbSet Polls { get; set; } } } \ No newline at end of file diff --git a/Geekbot.net/Database/DatabaseInitializer.cs b/Geekbot.net/Database/DatabaseInitializer.cs index 83660bb..7ae6cc7 100644 --- a/Geekbot.net/Database/DatabaseInitializer.cs +++ b/Geekbot.net/Database/DatabaseInitializer.cs @@ -40,7 +40,7 @@ namespace Geekbot.net.Database }); } database.Database.EnsureCreated(); - database.Database.Migrate(); + if(!_runParameters.InMemory) database.Database.Migrate(); } catch (Exception e) { diff --git a/Geekbot.net/Database/Models/PollModel.cs b/Geekbot.net/Database/Models/PollModel.cs new file mode 100644 index 0000000..3b12410 --- /dev/null +++ b/Geekbot.net/Database/Models/PollModel.cs @@ -0,0 +1,27 @@ +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; + +namespace Geekbot.net.Database.Models +{ + public class PollModel + { + [Key] + public int Id { get; set; } + + [Required] + public long GuildId { get; set; } + + [Required] + public long ChannelId { get; set; } + + public string Question { get; set; } + + public long Creator { get; set; } + + public long MessageId { get; set; } + + public List Options { get; set; } + + public bool IsFinshed { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Database/Models/PollQuestionModel.cs b/Geekbot.net/Database/Models/PollQuestionModel.cs new file mode 100644 index 0000000..e251fe2 --- /dev/null +++ b/Geekbot.net/Database/Models/PollQuestionModel.cs @@ -0,0 +1,16 @@ +using System.ComponentModel.DataAnnotations; + +namespace Geekbot.net.Database.Models +{ + public class PollQuestionModel + { + [Key] + public int Id { get; set; } + + public int OptionId { get; set; } + + public string OptionText { get; set; } + + public int Votes { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Database/Models/UserModel.cs b/Geekbot.net/Database/Models/UserModel.cs index f14ed61..b9c60ca 100644 --- a/Geekbot.net/Database/Models/UserModel.cs +++ b/Geekbot.net/Database/Models/UserModel.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.ComponentModel.DataAnnotations; namespace Geekbot.net.Database.Models @@ -24,6 +25,6 @@ namespace Geekbot.net.Database.Models public DateTimeOffset Joined { get; set; } - public string[] UsedNames { get; set; } + public List UsedNames { get; set; } } } \ No newline at end of file diff --git a/Geekbot.net/Database/Models/UserSettingsModel.cs b/Geekbot.net/Database/Models/UserSettingsModel.cs deleted file mode 100644 index 659ef1d..0000000 --- a/Geekbot.net/Database/Models/UserSettingsModel.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System.ComponentModel.DataAnnotations; - -namespace Geekbot.net.Database.Models -{ - public class UserSettingsModel - { - [Key] - public int Id { get; set; } - - [Required] - public long UserId { get; set; } - - // stuff to be added in the future - } -} \ No newline at end of file diff --git a/Geekbot.net/Database/Models/UserUsedNamesModel.cs b/Geekbot.net/Database/Models/UserUsedNamesModel.cs new file mode 100644 index 0000000..48ba873 --- /dev/null +++ b/Geekbot.net/Database/Models/UserUsedNamesModel.cs @@ -0,0 +1,15 @@ +using System; +using System.ComponentModel.DataAnnotations; + +namespace Geekbot.net.Database.Models +{ + public class UserUsedNamesModel + { + [Key] + public int Id { get; set; } + + public string Name { get; set; } + + public DateTimeOffset FirstSeen { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Database/RedisMigration.cs b/Geekbot.net/Database/RedisMigration.cs index 005aa69..df2e90b 100644 --- a/Geekbot.net/Database/RedisMigration.cs +++ b/Geekbot.net/Database/RedisMigration.cs @@ -250,7 +250,7 @@ namespace Geekbot.net.Database case "ShowLeave": settings.ShowLeave = setting.Value.ToString() == "1"; break; - case "WikiDel": + case "ShowDelete": settings.ShowDelete = setting.Value.ToString() == "1"; break; case "WikiLang": @@ -272,9 +272,9 @@ namespace Geekbot.net.Database throw new NotImplementedException(); } } - catch + catch (Exception e) { - _logger.Warning(LogSource.Geekbot, $"Setting failed: {setting.Name} - {guild.Id}"); + _logger.Warning(LogSource.Geekbot, $"Setting failed: {setting.Name} - {guild.Id}", e); } } } @@ -297,7 +297,9 @@ namespace Geekbot.net.Database try { var namesSerialized = _redis.HashGet($"User:{user.Id}", "UsedNames").ToString(); - var names = Utf8Json.JsonSerializer.Deserialize(namesSerialized); + var names = namesSerialized != null + ? Utf8Json.JsonSerializer.Deserialize(namesSerialized) + : new string[] {user.Username}; _database.Users.AddIfNotExists(new UserModel() { UserId = user.Id.AsLong(), @@ -306,13 +308,13 @@ namespace Geekbot.net.Database AvatarUrl = user.GetAvatarUrl(ImageFormat.Auto, 1024), IsBot = user.IsBot, Joined = user.CreatedAt, - UsedNames = names + UsedNames = names.Select(name => new UserUsedNamesModel() {Name = name, FirstSeen = DateTimeOffset.Now}).ToList() }, model => model.UserId.Equals(user.Id.AsLong())); await _database.SaveChangesAsync(); } - catch + catch (Exception e) { - _logger.Warning(LogSource.Geekbot, $"User failed: {user.Username}"); + _logger.Warning(LogSource.Geekbot, $"User failed: {user.Username}", e); } } } diff --git a/Geekbot.net/Lib/UserRepository/UserRepository.cs b/Geekbot.net/Lib/UserRepository/UserRepository.cs index 263a2f3..b27a0a2 100644 --- a/Geekbot.net/Lib/UserRepository/UserRepository.cs +++ b/Geekbot.net/Lib/UserRepository/UserRepository.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Discord.WebSocket; @@ -36,10 +37,10 @@ namespace Geekbot.net.Lib.UserRepository savedUser.AvatarUrl = user.GetAvatarUrl() ?? ""; savedUser.IsBot = user.IsBot; savedUser.Joined = user.CreatedAt; - if (savedUser.UsedNames == null) savedUser.UsedNames = Enumerable.Empty().ToArray(); - if (!savedUser.UsedNames.Contains(user.Username)) + if (savedUser.UsedNames == null) savedUser.UsedNames = new List(); + if (!savedUser.UsedNames.Any(e => e.Name.Equals(user.Username))) { - savedUser.UsedNames = savedUser.UsedNames.Concat(new[] {user.Username}).ToArray(); + savedUser.UsedNames.Add(new UserUsedNamesModel { Name = user.Username, FirstSeen = DateTimeOffset.Now }); } if (isNew) From f53258e348988873dfcd30cc2f87990fbd58fe0b Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 23 May 2018 23:50:11 +0200 Subject: [PATCH 079/443] Log errors with stacktrace in migration, skip 10k+ guilds, prevent migration from running twice by accident --- Geekbot.net/Commands/Admin/Owner.cs | 19 ++++- Geekbot.net/Database/RedisMigration.cs | 99 +++++++++++++++----------- Geekbot.net/Handlers.cs | 4 ++ 3 files changed, 78 insertions(+), 44 deletions(-) diff --git a/Geekbot.net/Commands/Admin/Owner.cs b/Geekbot.net/Commands/Admin/Owner.cs index c02c7fb..e4aa2a8 100644 --- a/Geekbot.net/Commands/Admin/Owner.cs +++ b/Geekbot.net/Commands/Admin/Owner.cs @@ -35,14 +35,27 @@ namespace Geekbot.net.Commands.Admin } [Command("migrate", RunMode = RunMode.Async)] - public async Task Migrate() + public async Task Migrate(string force = "") { - await ReplyAsync("starting migration"); - try { + var status = _globalSettings.GetKey("MigrationStatus"); + if (status.Equals("Running")) + { + await ReplyAsync("Migration already running"); + return; + } + if (status.Equals("Done") && !force.Equals("force")) + { + await ReplyAsync("Migration already ran, write `!owner migrate force` to run again"); + return; + } + + await ReplyAsync("starting migration"); + _globalSettings.SetKey("MigrationStatus", "Running"); var redisMigration = new RedisMigration(_database, _redis, _logger, _client); await redisMigration.Migrate(); + _globalSettings.SetKey("MigrationStatus", "Done"); } catch (Exception e) { diff --git a/Geekbot.net/Database/RedisMigration.cs b/Geekbot.net/Database/RedisMigration.cs index df2e90b..2f005e3 100644 --- a/Geekbot.net/Database/RedisMigration.cs +++ b/Geekbot.net/Database/RedisMigration.cs @@ -33,6 +33,12 @@ namespace Geekbot.net.Database _logger.Information(LogSource.Geekbot, "Starting migration process"); foreach (var guild in _client.Guilds) { + if (guild.MemberCount > 10000) + { + _logger.Information(LogSource.Geekbot, $"Skipping {guild.Name} because server size is to big ({guild.MemberCount})"); + break; + } + _logger.Information(LogSource.Geekbot, $"Start Migration for {guild.Name}"); #region Quotes /** @@ -50,15 +56,15 @@ namespace Geekbot.net.Database _database.Quotes.Add(quote); await _database.SaveChangesAsync(); } - catch + catch (Exception e) { - _logger.Warning(LogSource.Geekbot, $"quote failed: {q}"); + _logger.Error(LogSource.Geekbot, $"quote failed: {q}", e); } } } - catch + catch (Exception e) { - _logger.Warning(LogSource.Geekbot, "quote migration failed"); + _logger.Error(LogSource.Geekbot, "quote migration failed", e); } #endregion @@ -83,15 +89,15 @@ namespace Geekbot.net.Database _database.Karma.Add(user); await _database.SaveChangesAsync(); } - catch + catch (Exception e) { - _logger.Warning(LogSource.Geekbot, $"karma failed for: {q.Name}"); + _logger.Error(LogSource.Geekbot, $"karma failed for: {q.Name}", e); } } } - catch + catch (Exception e) { - _logger.Warning(LogSource.Geekbot, "karma migration failed"); + _logger.Error(LogSource.Geekbot, "karma migration failed", e); } #endregion @@ -115,15 +121,15 @@ namespace Geekbot.net.Database _database.Rolls.Add(user); await _database.SaveChangesAsync(); } - catch + catch (Exception e) { - _logger.Warning(LogSource.Geekbot, $"Rolls failed for: {q.Name}"); + _logger.Error(LogSource.Geekbot, $"Rolls failed for: {q.Name}", e); } } } - catch + catch (Exception e) { - _logger.Warning(LogSource.Geekbot, "rolls migration failed"); + _logger.Error(LogSource.Geekbot, "rolls migration failed", e); } #endregion @@ -144,20 +150,20 @@ namespace Geekbot.net.Database GuildId = guild.Id.AsLong(), UserId = ulong.Parse(q.Name).AsLong(), Given = int.Parse(q.Value), - Recieved= int.Parse(gotten[int.Parse(q.Name)].Value) + Recieved= int.Parse(gotten[long.Parse(q.Name)].Value) }; _database.Slaps.Add(user); await _database.SaveChangesAsync(); } - catch + catch (Exception e) { - _logger.Warning(LogSource.Geekbot, $"Slaps failed for: {q.Name}"); + _logger.Error(LogSource.Geekbot, $"Slaps failed for: {q.Name}", e); } } } - catch + catch (Exception e) { - _logger.Warning(LogSource.Geekbot, "Slaps migration failed"); + _logger.Error(LogSource.Geekbot, "Slaps migration failed", e); } #endregion @@ -181,15 +187,15 @@ namespace Geekbot.net.Database _database.Messages.Add(user); await _database.SaveChangesAsync(); } - catch + catch (Exception e) { - _logger.Warning(LogSource.Geekbot, $"Messages failed for: {q.Name}"); + _logger.Error(LogSource.Geekbot, $"Messages failed for: {q.Name}", e); } } } - catch + catch (Exception e) { - _logger.Warning(LogSource.Geekbot, "Messages migration failed"); + _logger.Error(LogSource.Geekbot, "Messages migration failed", e); } #endregion @@ -217,15 +223,15 @@ namespace Geekbot.net.Database await _database.SaveChangesAsync(); done.Add(q.Name); } - catch + catch (Exception e) { - _logger.Warning(LogSource.Geekbot, $"Ships failed for: {q.Name}"); + _logger.Error(LogSource.Geekbot, $"Ships failed for: {q.Name}", e); } } } - catch + catch (Exception e) { - _logger.Warning(LogSource.Geekbot, "Ships migration failed"); + _logger.Error(LogSource.Geekbot, "Ships migration failed", e); } #endregion @@ -268,19 +274,17 @@ namespace Geekbot.net.Database case "ModChannel": settings.ModChannel = long.Parse(setting.Value); break; - default: - throw new NotImplementedException(); } } catch (Exception e) { - _logger.Warning(LogSource.Geekbot, $"Setting failed: {setting.Name} - {guild.Id}", e); + _logger.Error(LogSource.Geekbot, $"Setting failed: {setting.Name} - {guild.Id}", e); } } } - catch + catch (Exception e) { - _logger.Warning(LogSource.Geekbot, "Settings migration failed"); + _logger.Error(LogSource.Geekbot, "Settings migration failed", e); } #endregion @@ -296,6 +300,11 @@ namespace Geekbot.net.Database { try { + if (user.Username == null) + { + await Task.Delay(100); + if (user.Username == null) break; + } var namesSerialized = _redis.HashGet($"User:{user.Id}", "UsedNames").ToString(); var names = namesSerialized != null ? Utf8Json.JsonSerializer.Deserialize(namesSerialized) @@ -314,30 +323,38 @@ namespace Geekbot.net.Database } catch (Exception e) { - _logger.Warning(LogSource.Geekbot, $"User failed: {user.Username}", e); + _logger.Error(LogSource.Geekbot, $"User failed: {user.Username}", e); } } } - catch + catch (Exception e) { - _logger.Warning(LogSource.Geekbot, "User migration failed"); + _logger.Error(LogSource.Geekbot, "User migration failed", e); } #endregion #region Guilds - _database.Guilds.Add(new GuildsModel + try { - CreatedAt = guild.CreatedAt, - GuildId = guild.Id.AsLong(), - IconUrl = guild.IconUrl, - Name = guild.Name, - Owner = guild.Owner.Id.AsLong() - }); - await _database.SaveChangesAsync(); + _database.Guilds.Add(new GuildsModel + { + CreatedAt = guild.CreatedAt, + GuildId = guild.Id.AsLong(), + IconUrl = guild?.IconUrl, + Name = guild.Name, + Owner = guild.Owner.Id.AsLong() + }); + await _database.SaveChangesAsync(); + } + catch (Exception e) + { + _logger.Error(LogSource.Geekbot, $"Guild migration failed: {guild.Name}", e); + } #endregion _logger.Information(LogSource.Geekbot, $"Finished Migration for {guild.Name}"); + await Task.Delay(1000); } _logger.Information(LogSource.Geekbot, "Finished migration process"); } diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index f9fbc62..52dbfc8 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -50,6 +50,10 @@ namespace Geekbot.net if (!(messageParam is SocketUserMessage message)) return Task.CompletedTask; if (message.Author.IsBot) return Task.CompletedTask; var argPos = 0; + + // ToDo: remove +// if (!message.Author.Id.Equals(93061333972455424)) return Task.CompletedTask; + var lowCaseMsg = message.ToString().ToLower(); if (lowCaseMsg.StartsWith("hui")) { From 35f0a5c8f8277df197ca4d110b4e3faf798f212c Mon Sep 17 00:00:00 2001 From: Runebaas Date: Sat, 26 May 2018 02:33:45 +0200 Subject: [PATCH 080/443] Abstract away redis even more with AlmostRedis.cs --- Geekbot.net/Commands/Admin/Owner.cs | 6 +- Geekbot.net/Commands/Games/Roll.cs | 11 +-- Geekbot.net/Commands/User/Ranking/Rank.cs | 7 +- Geekbot.net/Commands/User/Stats.cs | 10 +-- Geekbot.net/Database/RedisMigration.cs | 74 +++++++++---------- Geekbot.net/Handlers.cs | 10 +-- Geekbot.net/Lib/AlmostRedis/AlmostRedis.cs | 44 +++++++++++ Geekbot.net/Lib/AlmostRedis/IAlmostRedis.cs | 13 ++++ .../Lib/Localization/TranslationHandler.cs | 32 ++++++-- Geekbot.net/Program.cs | 24 +++--- 10 files changed, 153 insertions(+), 78 deletions(-) create mode 100644 Geekbot.net/Lib/AlmostRedis/AlmostRedis.cs create mode 100644 Geekbot.net/Lib/AlmostRedis/IAlmostRedis.cs diff --git a/Geekbot.net/Commands/Admin/Owner.cs b/Geekbot.net/Commands/Admin/Owner.cs index e4aa2a8..a05bd8e 100644 --- a/Geekbot.net/Commands/Admin/Owner.cs +++ b/Geekbot.net/Commands/Admin/Owner.cs @@ -3,11 +3,11 @@ using System.Threading.Tasks; using Discord.Commands; using Discord.WebSocket; using Geekbot.net.Database; +using Geekbot.net.Lib.AlmostRedis; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.GlobalSettings; using Geekbot.net.Lib.Logger; using Geekbot.net.Lib.UserRepository; -using StackExchange.Redis; namespace Geekbot.net.Commands.Admin { @@ -20,10 +20,10 @@ namespace Geekbot.net.Commands.Admin private readonly DatabaseContext _database; private readonly IGlobalSettings _globalSettings; private readonly IGeekbotLogger _logger; - private readonly IDatabase _redis; + private readonly IAlmostRedis _redis; private readonly IUserRepository _userRepository; - public Owner(IDatabase redis, DiscordSocketClient client, IGeekbotLogger logger, IUserRepository userRepositry, IErrorHandler errorHandler, DatabaseContext database, IGlobalSettings globalSettings) + public Owner(IAlmostRedis redis, DiscordSocketClient client, IGeekbotLogger logger, IUserRepository userRepositry, IErrorHandler errorHandler, DatabaseContext database, IGlobalSettings globalSettings) { _redis = redis; _client = client; diff --git a/Geekbot.net/Commands/Games/Roll.cs b/Geekbot.net/Commands/Games/Roll.cs index 1d2ab4f..488488f 100644 --- a/Geekbot.net/Commands/Games/Roll.cs +++ b/Geekbot.net/Commands/Games/Roll.cs @@ -1,6 +1,7 @@ using System; using System.Threading.Tasks; using Discord.Commands; +using Geekbot.net.Lib.AlmostRedis; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Localization; using StackExchange.Redis; @@ -10,10 +11,10 @@ namespace Geekbot.net.Commands.Games public class Roll : ModuleBase { private readonly IErrorHandler _errorHandler; - private readonly IDatabase _redis; + private readonly IAlmostRedis _redis; private readonly ITranslationHandler _translation; - public Roll(IDatabase redis, IErrorHandler errorHandler, ITranslationHandler translation) + public Roll(IAlmostRedis redis, IErrorHandler errorHandler, ITranslationHandler translation) { _redis = redis; _translation = translation; @@ -32,20 +33,20 @@ namespace Geekbot.net.Commands.Games var transDict = _translation.GetDict(Context); if (guess <= 100 && guess > 0) { - var prevRoll = _redis.HashGet($"{Context.Guild.Id}:RollsPrevious", Context.Message.Author.Id); + var prevRoll = _redis.Db.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", + _redis.Db.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()); + _redis.Db.HashIncrement($"{Context.Guild.Id}:Rolls", Context.User.Id.ToString()); } } else diff --git a/Geekbot.net/Commands/User/Ranking/Rank.cs b/Geekbot.net/Commands/User/Ranking/Rank.cs index 6e3a1d8..f404b58 100644 --- a/Geekbot.net/Commands/User/Ranking/Rank.cs +++ b/Geekbot.net/Commands/User/Ranking/Rank.cs @@ -5,6 +5,7 @@ using System.Text; using System.Threading.Tasks; using Discord.Commands; using Geekbot.net.Database; +using Geekbot.net.Lib.AlmostRedis; using Geekbot.net.Lib.Converters; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; @@ -19,10 +20,10 @@ namespace Geekbot.net.Commands.User.Ranking private readonly IErrorHandler _errorHandler; private readonly DatabaseContext _database; private readonly IUserRepository _userRepository; - private readonly IDatabase _redis; + private readonly IAlmostRedis _redis; public Rank(DatabaseContext database, IErrorHandler errorHandler, IUserRepository userRepository, - IEmojiConverter emojiConverter, IDatabase redis) + IEmojiConverter emojiConverter, IAlmostRedis redis) { _database = database; _errorHandler = errorHandler; @@ -138,7 +139,7 @@ namespace Geekbot.net.Commands.User.Ranking private Dictionary GetMessageList(int amount) { - return _redis + return _redis.Db .HashGetAll($"{Context.Guild.Id}:Messages").ToDictionary().Take(amount + 1) .Where(user => !user.Key.Equals(0)) .ToDictionary(user => ulong.Parse(user.Key), user => int.Parse(user.Value)); diff --git a/Geekbot.net/Commands/User/Stats.cs b/Geekbot.net/Commands/User/Stats.cs index 01d169e..4a82a89 100644 --- a/Geekbot.net/Commands/User/Stats.cs +++ b/Geekbot.net/Commands/User/Stats.cs @@ -4,10 +4,10 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Geekbot.net.Database; +using Geekbot.net.Lib.AlmostRedis; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Levels; -using StackExchange.Redis; namespace Geekbot.net.Commands.User { @@ -15,10 +15,10 @@ namespace Geekbot.net.Commands.User { private readonly IErrorHandler _errorHandler; private readonly ILevelCalc _levelCalc; - private readonly IDatabase _redis; + private readonly IAlmostRedis _redis; private readonly DatabaseContext _database; - public Stats(IDatabase redis, DatabaseContext database, IErrorHandler errorHandler, ILevelCalc levelCalc) + public Stats(IAlmostRedis redis, DatabaseContext database, IErrorHandler errorHandler, ILevelCalc levelCalc) { _redis = redis; _database = database; @@ -39,8 +39,8 @@ namespace Geekbot.net.Commands.User var age = Math.Floor((DateTime.Now - createdAt).TotalDays); var joinedDayAgo = Math.Floor((DateTime.Now - joinedAt).TotalDays); - var messages = (int) _redis.HashGet($"{Context.Guild.Id}:Messages", userInfo.Id.ToString()); - var guildMessages = (int) _redis.HashGet($"{Context.Guild.Id}:Messages", 0.ToString()); + var messages = (int) _redis.Db.HashGet($"{Context.Guild.Id}:Messages", userInfo.Id.ToString()); + var guildMessages = (int) _redis.Db.HashGet($"{Context.Guild.Id}:Messages", 0.ToString()); var level = _levelCalc.GetLevel(messages); var percent = Math.Round((double) (100 * messages) / guildMessages, 2); diff --git a/Geekbot.net/Database/RedisMigration.cs b/Geekbot.net/Database/RedisMigration.cs index 2f005e3..80965d1 100644 --- a/Geekbot.net/Database/RedisMigration.cs +++ b/Geekbot.net/Database/RedisMigration.cs @@ -6,21 +6,21 @@ using Discord; using Discord.WebSocket; using Geekbot.net.Commands.Utils.Quote; using Geekbot.net.Database.Models; +using Geekbot.net.Lib.AlmostRedis; using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Logger; using Newtonsoft.Json; -using StackExchange.Redis; namespace Geekbot.net.Database { public class RedisMigration { private readonly DatabaseContext _database; - private readonly IDatabase _redis; + private readonly IAlmostRedis _redis; private readonly IGeekbotLogger _logger; private readonly DiscordSocketClient _client; - public RedisMigration(DatabaseContext database, IDatabase redis, IGeekbotLogger logger, DiscordSocketClient client) + public RedisMigration(DatabaseContext database, IAlmostRedis redis, IGeekbotLogger logger, DiscordSocketClient client) { _database = database; _redis = redis; @@ -46,7 +46,7 @@ namespace Geekbot.net.Database */ try { - var data = _redis.SetScan($"{guild.Id}:Quotes"); + var data = _redis.Db.SetScan($"{guild.Id}:Quotes"); foreach (var q in data) { try @@ -74,7 +74,7 @@ namespace Geekbot.net.Database */ try { - var data = _redis.HashGetAll($"{guild.Id}:Karma"); + var data = _redis.Db.HashGetAll($"{guild.Id}:Karma"); foreach (var q in data) { try @@ -107,7 +107,7 @@ namespace Geekbot.net.Database */ try { - var data = _redis.HashGetAll($"{guild.Id}:Rolls"); + var data = _redis.Db.HashGetAll($"{guild.Id}:Rolls"); foreach (var q in data) { try @@ -139,8 +139,8 @@ namespace Geekbot.net.Database */ try { - var given = _redis.HashGetAll($"{guild.Id}:SlapsGiven"); - var gotten = _redis.HashGetAll($"{guild.Id}:SlapsGiven"); + var given = _redis.Db.HashGetAll($"{guild.Id}:SlapsGiven"); + var gotten = _redis.Db.HashGetAll($"{guild.Id}:SlapsGiven"); foreach (var q in given) { try @@ -171,32 +171,32 @@ namespace Geekbot.net.Database /** * Messages */ - try - { - var data = _redis.HashGetAll($"{guild.Id}:Messages"); - foreach (var q in data) - { - try - { - var user = new MessagesModel() - { - GuildId = guild.Id.AsLong(), - UserId = ulong.Parse(q.Name).AsLong(), - MessageCount= int.Parse(q.Value) - }; - _database.Messages.Add(user); - await _database.SaveChangesAsync(); - } - catch (Exception e) - { - _logger.Error(LogSource.Geekbot, $"Messages failed for: {q.Name}", e); - } - } - } - catch (Exception e) - { - _logger.Error(LogSource.Geekbot, "Messages migration failed", e); - } +// try +// { +// var data = _redis.Db.HashGetAll($"{guild.Id}:Messages"); +// foreach (var q in data) +// { +// try +// { +// var user = new MessagesModel() +// { +// GuildId = guild.Id.AsLong(), +// UserId = ulong.Parse(q.Name).AsLong(), +// MessageCount= int.Parse(q.Value) +// }; +// _database.Messages.Add(user); +// await _database.SaveChangesAsync(); +// } +// catch (Exception e) +// { +// _logger.Error(LogSource.Geekbot, $"Messages failed for: {q.Name}", e); +// } +// } +// } +// catch (Exception e) +// { +// _logger.Error(LogSource.Geekbot, "Messages migration failed", e); +// } #endregion #region Ships @@ -205,7 +205,7 @@ namespace Geekbot.net.Database */ try { - var data = _redis.HashGetAll($"{guild.Id}:Ships"); + var data = _redis.Db.HashGetAll($"{guild.Id}:Ships"); var done = new List(); foreach (var q in data) { @@ -241,7 +241,7 @@ namespace Geekbot.net.Database */ try { - var data = _redis.HashGetAll($"{guild.Id}:Settings"); + var data = _redis.Db.HashGetAll($"{guild.Id}:Settings"); var settings = new GuildSettingsModel() { GuildId = guild.Id.AsLong(), @@ -305,7 +305,7 @@ namespace Geekbot.net.Database await Task.Delay(100); if (user.Username == null) break; } - var namesSerialized = _redis.HashGet($"User:{user.Id}", "UsedNames").ToString(); + var namesSerialized = _redis.Db.HashGet($"User:{user.Id}", "UsedNames").ToString(); var names = namesSerialized != null ? Utf8Json.JsonSerializer.Deserialize(namesSerialized) : new string[] {user.Username}; diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index 52dbfc8..0aab944 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -6,11 +6,11 @@ using Discord; using Discord.Commands; using Discord.WebSocket; using Geekbot.net.Database; +using Geekbot.net.Lib.AlmostRedis; using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Logger; using Geekbot.net.Lib.ReactionListener; using Geekbot.net.Lib.UserRepository; -using StackExchange.Redis; namespace Geekbot.net { @@ -19,13 +19,13 @@ namespace Geekbot.net private readonly DatabaseContext _database; private readonly IDiscordClient _client; private readonly IGeekbotLogger _logger; - private readonly IDatabase _redis; + private readonly IAlmostRedis _redis; private readonly IServiceProvider _servicesProvider; private readonly CommandService _commands; private readonly IUserRepository _userRepository; private readonly IReactionListener _reactionListener; - public Handlers(DatabaseContext database, IDiscordClient client, IGeekbotLogger logger, IDatabase redis, + public Handlers(DatabaseContext database, IDiscordClient client, IGeekbotLogger logger, IAlmostRedis redis, IServiceProvider servicesProvider, CommandService commands, IUserRepository userRepository, IReactionListener reactionListener) { @@ -105,8 +105,8 @@ namespace Geekbot.net } var channel = (SocketGuildChannel) message.Channel; - _redis.HashIncrementAsync($"{channel.Guild.Id}:Messages", message.Author.Id.ToString()); - _redis.HashIncrementAsync($"{channel.Guild.Id}:Messages", 0.ToString()); + _redis.Db.HashIncrementAsync($"{channel.Guild.Id}:Messages", message.Author.Id.ToString()); + _redis.Db.HashIncrementAsync($"{channel.Guild.Id}:Messages", 0.ToString()); if (message.Author.IsBot) return Task.CompletedTask; _logger.Information(LogSource.Message, message.Content, SimpleConextConverter.ConvertSocketMessage(message)); diff --git a/Geekbot.net/Lib/AlmostRedis/AlmostRedis.cs b/Geekbot.net/Lib/AlmostRedis/AlmostRedis.cs new file mode 100644 index 0000000..031b266 --- /dev/null +++ b/Geekbot.net/Lib/AlmostRedis/AlmostRedis.cs @@ -0,0 +1,44 @@ +using System.Collections.Generic; +using Geekbot.net.Lib.Logger; +using StackExchange.Redis; + +namespace Geekbot.net.Lib.AlmostRedis +{ + // if anyone ever sees this, please come up with a better fucking name, i'd appriciate it + public class AlmostRedis : IAlmostRedis + { + private readonly GeekbotLogger _logger; + private readonly RunParameters _runParameters; + private IDatabase _database; + private ConnectionMultiplexer _connection; + private IAlmostRedis _almostRedisImplementation; + + public AlmostRedis(GeekbotLogger logger, RunParameters runParameters) + { + _logger = logger; + _runParameters = runParameters; + } + + public void Connect() + { + _connection = ConnectionMultiplexer.Connect($"{_runParameters.RedisHost}:{_runParameters.RedisPort}"); + _database = _connection.GetDatabase(int.Parse(_runParameters.RedisDatabase)); + _logger.Information(LogSource.Redis, $"Connected to Redis on {_connection.Configuration} at {_database.Database}"); + } + + public IDatabase Db + { + get { return _database; } + } + + public ConnectionMultiplexer Connection + { + get { return _connection; } + } + + public IEnumerable GetAllKeys() + { + return _connection.GetServer($"{_runParameters.RedisHost}:{_runParameters.RedisPort}", int.Parse(_runParameters.RedisDatabase)).Keys(); + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/AlmostRedis/IAlmostRedis.cs b/Geekbot.net/Lib/AlmostRedis/IAlmostRedis.cs new file mode 100644 index 0000000..549c49e --- /dev/null +++ b/Geekbot.net/Lib/AlmostRedis/IAlmostRedis.cs @@ -0,0 +1,13 @@ +using System.Collections.Generic; +using StackExchange.Redis; + +namespace Geekbot.net.Lib.AlmostRedis +{ + public interface IAlmostRedis + { + void Connect(); + IDatabase Db { get; } + ConnectionMultiplexer Connection { get; } + IEnumerable GetAllKeys(); + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/Localization/TranslationHandler.cs b/Geekbot.net/Lib/Localization/TranslationHandler.cs index 550759d..afcd439 100644 --- a/Geekbot.net/Lib/Localization/TranslationHandler.cs +++ b/Geekbot.net/Lib/Localization/TranslationHandler.cs @@ -4,24 +4,26 @@ using System.IO; using System.Linq; using Discord.Commands; using Discord.WebSocket; +using Geekbot.net.Database; +using Geekbot.net.Database.Models; +using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Logger; -using StackExchange.Redis; using Utf8Json; namespace Geekbot.net.Lib.Localization { public class TranslationHandler : ITranslationHandler { + private readonly DatabaseContext _database; private readonly IGeekbotLogger _logger; - private readonly IDatabase _redis; private Dictionary>> _translations; private Dictionary _serverLanguages; private List _supportedLanguages; - public TranslationHandler(IReadOnlyCollection clientGuilds, IDatabase redis, IGeekbotLogger logger) + public TranslationHandler(IReadOnlyCollection clientGuilds, DatabaseContext database, IGeekbotLogger logger) { + _database = database; _logger = logger; - _redis = redis; _logger.Information(LogSource.Geekbot, "Loading Translations"); LoadTranslations(); LoadServerLanguages(clientGuilds); @@ -81,14 +83,16 @@ namespace Geekbot.net.Lib.Localization _serverLanguages = new Dictionary(); foreach (var guild in clientGuilds) { - var language = _redis.HashGet($"{guild.Id}:Settings", "Language"); + var language = _database.GuildSettings + .FirstOrDefault(g => g.GuildId.Equals(guild.Id.AsLong())) + ?.Language ?? "EN"; if (string.IsNullOrEmpty(language) || !_supportedLanguages.Contains(language)) { _serverLanguages[guild.Id] = "EN"; } else { - _serverLanguages[guild.Id] = language.ToString(); + _serverLanguages[guild.Id] = language; } } } @@ -137,7 +141,9 @@ namespace Geekbot.net.Lib.Localization try { if (!_supportedLanguages.Contains(language)) return false; - _redis.HashSet($"{guildId}:Settings", new[]{ new HashEntry("Language", language) }); + var guild = GetGuild(guildId); + guild.Language = language; + _database.GuildSettings.Update(guild); _serverLanguages[guildId] = language; return true; } @@ -152,5 +158,17 @@ namespace Geekbot.net.Lib.Localization { return _supportedLanguages; } + + private GuildSettingsModel GetGuild(ulong guildId) + { + var guild = _database.GuildSettings.FirstOrDefault(g => g.GuildId.Equals(guildId.AsLong())); + if (guild != null) return guild; + _database.GuildSettings.Add(new GuildSettingsModel + { + GuildId = guildId.AsLong() + }); + _database.SaveChanges(); + return _database.GuildSettings.FirstOrDefault(g => g.GuildId.Equals(guildId.AsLong())); + } } } \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index f94a785..f818802 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -8,6 +8,7 @@ using Discord.Commands; using Discord.WebSocket; using Geekbot.net.Database; using Geekbot.net.Lib; +using Geekbot.net.Lib.AlmostRedis; using Geekbot.net.Lib.Audio; using Geekbot.net.Lib.Clients; using Geekbot.net.Lib.Converters; @@ -20,7 +21,6 @@ using Geekbot.net.Lib.Media; using Geekbot.net.Lib.ReactionListener; using Geekbot.net.Lib.UserRepository; using Microsoft.Extensions.DependencyInjection; -using StackExchange.Redis; using WikipediaApi; namespace Geekbot.net @@ -29,15 +29,15 @@ namespace Geekbot.net { private DiscordSocketClient _client; private CommandService _commands; - private IDatabase _redis; private DatabaseContext _database; private IGlobalSettings _globalSettings; private IServiceCollection _services; private IServiceProvider _servicesProvider; - private RedisValue _token; + private string _token; private GeekbotLogger _logger; private IUserRepository _userRepository; private RunParameters _runParameters; + private IAlmostRedis _redis; private static void Main(string[] args) { @@ -52,8 +52,7 @@ namespace Geekbot.net logo.AppendLine(@"| | _| _| | _| | ' /| _ \| | | || |"); logo.AppendLine(@"| |_| | |___| |___| . \| |_) | |_| || |"); logo.AppendLine(@" \____|_____|_____|_|\_\____/ \___/ |_|"); - logo.AppendLine("========================================="); - logo.AppendLine($"Version {Constants.BotVersion()}"); + logo.AppendLine($"Version {Constants.BotVersion()} ".PadRight(41, '=')); Console.WriteLine(logo.ToString()); var sumologicActive = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GEEKBOT_SUMO")); var logger = new GeekbotLogger(runParameters, sumologicActive); @@ -88,9 +87,8 @@ namespace Geekbot.net try { - var redisMultiplexer = ConnectionMultiplexer.Connect($"{runParameters.RedisHost}:{runParameters.RedisPort}"); - _redis = redisMultiplexer.GetDatabase(int.Parse(runParameters.RedisDatabase)); - logger.Information(LogSource.Redis, $"Connected to db {_redis.Database}"); + _redis = new AlmostRedis(logger, runParameters); + _redis.Connect(); } catch (Exception e) { @@ -99,7 +97,7 @@ namespace Geekbot.net } _token = runParameters.Token ?? _globalSettings.GetKey("DiscordToken"); - if (_token.IsNullOrEmpty) + if (string.IsNullOrEmpty(_token)) { Console.Write("Your bot Token: "); var newToken = Console.ReadLine(); @@ -120,7 +118,7 @@ namespace Geekbot.net var wikipediaClient = new WikipediaClient(); var audioUtils = new AudioUtils(); - _services.AddSingleton(_redis); + _services.AddSingleton(_redis); _services.AddSingleton(_userRepository); _services.AddSingleton(logger); _services.AddSingleton(levelCalc); @@ -154,9 +152,9 @@ namespace Geekbot.net _logger.Information(LogSource.Geekbot, $"Now Connected as {_client.CurrentUser.Username} to {_client.Guilds.Count} Servers"); _logger.Information(LogSource.Geekbot, "Registering Stuff"); - var translationHandler = new TranslationHandler(_client.Guilds, _redis, _logger); + var translationHandler = new TranslationHandler(_client.Guilds, _database, _logger); var errorHandler = new ErrorHandler(_logger, translationHandler, _runParameters.ExposeErrors); - var reactionListener = new ReactionListener(_redis); + var reactionListener = new ReactionListener(_redis.Db); await _commands.AddModulesAsync(Assembly.GetEntryAssembly()); _services.AddSingleton(_commands); _services.AddSingleton(errorHandler); @@ -185,7 +183,7 @@ namespace Geekbot.net } catch (Exception e) { - _logger.Error(LogSource.Geekbot, "Could not connect...", e); + _logger.Error(LogSource.Geekbot, "Could not connect to Discord", e); Environment.Exit(GeekbotExitCode.CouldNotLogin.GetHashCode()); } } From 80c2bd8500342a6411c7a63746c1af8284c29317 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Sat, 26 May 2018 02:55:18 +0200 Subject: [PATCH 081/443] Lazyload server language setting in translationHandler --- Geekbot.net/Commands/Admin/Admin.cs | 2 +- Geekbot.net/Lib/AlmostRedis/AlmostRedis.cs | 23 +++------- .../Lib/Localization/ITranslationHandler.cs | 2 +- .../Lib/Localization/TranslationHandler.cs | 46 +++++++++++-------- Geekbot.net/Program.cs | 2 +- 5 files changed, 36 insertions(+), 39 deletions(-) diff --git a/Geekbot.net/Commands/Admin/Admin.cs b/Geekbot.net/Commands/Admin/Admin.cs index 0925e54..ba31f3f 100644 --- a/Geekbot.net/Commands/Admin/Admin.cs +++ b/Geekbot.net/Commands/Admin/Admin.cs @@ -138,7 +138,7 @@ namespace Geekbot.net.Commands.Admin } await ReplyAsync( - $"That doesn't seem to be a supported language\r\nSupported Languages are {string.Join(", ", _translation.GetSupportedLanguages())}"); + $"That doesn't seem to be a supported language\r\nSupported Languages are {string.Join(", ", _translation.SupportedLanguages)}"); } catch (Exception e) { diff --git a/Geekbot.net/Lib/AlmostRedis/AlmostRedis.cs b/Geekbot.net/Lib/AlmostRedis/AlmostRedis.cs index 031b266..5e010f9 100644 --- a/Geekbot.net/Lib/AlmostRedis/AlmostRedis.cs +++ b/Geekbot.net/Lib/AlmostRedis/AlmostRedis.cs @@ -9,9 +9,6 @@ namespace Geekbot.net.Lib.AlmostRedis { private readonly GeekbotLogger _logger; private readonly RunParameters _runParameters; - private IDatabase _database; - private ConnectionMultiplexer _connection; - private IAlmostRedis _almostRedisImplementation; public AlmostRedis(GeekbotLogger logger, RunParameters runParameters) { @@ -21,24 +18,18 @@ namespace Geekbot.net.Lib.AlmostRedis public void Connect() { - _connection = ConnectionMultiplexer.Connect($"{_runParameters.RedisHost}:{_runParameters.RedisPort}"); - _database = _connection.GetDatabase(int.Parse(_runParameters.RedisDatabase)); - _logger.Information(LogSource.Redis, $"Connected to Redis on {_connection.Configuration} at {_database.Database}"); + Connection = ConnectionMultiplexer.Connect($"{_runParameters.RedisHost}:{_runParameters.RedisPort}"); + Db = Connection.GetDatabase(int.Parse(_runParameters.RedisDatabase)); + _logger.Information(LogSource.Redis, $"Connected to Redis on {Connection.Configuration} at {Db.Database}"); } - public IDatabase Db - { - get { return _database; } - } + public IDatabase Db { get; private set; } + + public ConnectionMultiplexer Connection { get; private set; } - public ConnectionMultiplexer Connection - { - get { return _connection; } - } - public IEnumerable GetAllKeys() { - return _connection.GetServer($"{_runParameters.RedisHost}:{_runParameters.RedisPort}", int.Parse(_runParameters.RedisDatabase)).Keys(); + return Connection.GetServer($"{_runParameters.RedisHost}:{_runParameters.RedisPort}", int.Parse(_runParameters.RedisDatabase)).Keys(); } } } \ No newline at end of file diff --git a/Geekbot.net/Lib/Localization/ITranslationHandler.cs b/Geekbot.net/Lib/Localization/ITranslationHandler.cs index 3cd9549..df4d707 100644 --- a/Geekbot.net/Lib/Localization/ITranslationHandler.cs +++ b/Geekbot.net/Lib/Localization/ITranslationHandler.cs @@ -9,6 +9,6 @@ namespace Geekbot.net.Lib.Localization Dictionary GetDict(ICommandContext context); Dictionary GetDict(ICommandContext context, string command); bool SetLanguage(ulong guildId, string language); - List GetSupportedLanguages(); + List SupportedLanguages { get; } } } \ No newline at end of file diff --git a/Geekbot.net/Lib/Localization/TranslationHandler.cs b/Geekbot.net/Lib/Localization/TranslationHandler.cs index afcd439..f8e2d88 100644 --- a/Geekbot.net/Lib/Localization/TranslationHandler.cs +++ b/Geekbot.net/Lib/Localization/TranslationHandler.cs @@ -16,17 +16,17 @@ namespace Geekbot.net.Lib.Localization { private readonly DatabaseContext _database; private readonly IGeekbotLogger _logger; + private readonly Dictionary _serverLanguages; private Dictionary>> _translations; - private Dictionary _serverLanguages; private List _supportedLanguages; - public TranslationHandler(IReadOnlyCollection clientGuilds, DatabaseContext database, IGeekbotLogger logger) + public TranslationHandler(DatabaseContext database, IGeekbotLogger logger) { _database = database; _logger = logger; _logger.Information(LogSource.Geekbot, "Loading Translations"); LoadTranslations(); - LoadServerLanguages(clientGuilds); + _serverLanguages = new Dictionary(); } private void LoadTranslations() @@ -78,28 +78,37 @@ namespace Geekbot.net.Lib.Localization } } - private void LoadServerLanguages(IReadOnlyCollection clientGuilds) + private string GetServerLanguage(ulong guildId) { - _serverLanguages = new Dictionary(); - foreach (var guild in clientGuilds) + try { - var language = _database.GuildSettings - .FirstOrDefault(g => g.GuildId.Equals(guild.Id.AsLong())) - ?.Language ?? "EN"; - if (string.IsNullOrEmpty(language) || !_supportedLanguages.Contains(language)) + string lang; + try { - _serverLanguages[guild.Id] = "EN"; + lang = _serverLanguages[guildId]; + if (!string.IsNullOrEmpty(lang)) + { + return lang; + } + throw new Exception(); } - else + catch { - _serverLanguages[guild.Id] = language; + lang = GetGuild(guildId).Language ?? "EN"; + _serverLanguages[guildId] = lang; + return lang; } } + catch (Exception e) + { + _logger.Error(LogSource.Geekbot, "Could not get guild langage", e); + return "EN"; + } } public string GetString(ulong guildId, string command, string stringName) { - var translation = _translations[_serverLanguages[guildId]][command][stringName]; + var translation = _translations[GetServerLanguage(guildId)][command][stringName]; if (!string.IsNullOrWhiteSpace(translation)) return translation; translation = _translations[command][stringName]["EN"]; if (string.IsNullOrWhiteSpace(translation)) @@ -114,7 +123,7 @@ namespace Geekbot.net.Lib.Localization try { var command = context.Message.Content.Split(' ').First().TrimStart('!').ToLower(); - return _translations[_serverLanguages[context.Guild.Id]][command]; + return _translations[GetServerLanguage(context.Guild.Id)][command]; } catch (Exception e) { @@ -127,7 +136,7 @@ namespace Geekbot.net.Lib.Localization { try { - return _translations[_serverLanguages[context.Guild.Id]][command]; + return _translations[GetServerLanguage(context.Guild.Id)][command]; } catch (Exception e) { @@ -154,10 +163,7 @@ namespace Geekbot.net.Lib.Localization } } - public List GetSupportedLanguages() - { - return _supportedLanguages; - } + public List SupportedLanguages => _supportedLanguages; private GuildSettingsModel GetGuild(ulong guildId) { diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index f818802..86a3507 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -152,7 +152,7 @@ namespace Geekbot.net _logger.Information(LogSource.Geekbot, $"Now Connected as {_client.CurrentUser.Username} to {_client.Guilds.Count} Servers"); _logger.Information(LogSource.Geekbot, "Registering Stuff"); - var translationHandler = new TranslationHandler(_client.Guilds, _database, _logger); + var translationHandler = new TranslationHandler(_database, _logger); var errorHandler = new ErrorHandler(_logger, translationHandler, _runParameters.ExposeErrors); var reactionListener = new ReactionListener(_redis.Db); await _commands.AddModulesAsync(Assembly.GetEntryAssembly()); From f438914c19306938121b42f9bd2dafc995b8552b Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 30 May 2018 23:54:00 +0200 Subject: [PATCH 082/443] Upgrade to dotnet 2.1 --- Geekbot.net/Geekbot.net.csproj | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 9b0fac6..336787a 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -22,26 +22,26 @@ 2.0.0-beta - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + - - + + From 8b5e52c8e0ca0d468b0e3525988093ac7abb70dd Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Fri, 1 Jun 2018 18:10:22 +0200 Subject: [PATCH 083/443] Actually upgrade to dotnet 2.1 --- .gitlab-ci.yml | 2 +- Geekbot.net/Geekbot.net.csproj | 8 +++----- Tests/Tests.csproj | 2 +- WikipediaApi/WikipediaApi.csproj | 2 +- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 98eb9ba..b40a5e5 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -9,7 +9,7 @@ before_script: build: stage: build - image: microsoft/dotnet:2.0.3-sdk-stretch + image: microsoft/dotnet:2.1.300-sdk-stretch variables: NUGET_PACKAGES: "${CI_PROJECT_DIR}/.nugetcache" cache: diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 336787a..9019d87 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -1,7 +1,8 @@  Exe - netcoreapp2.0 + netcoreapp2.1 + win-x64;linux-x64 derp.ico 4.0.0 $(VersionSuffix) @@ -42,7 +43,7 @@ - + @@ -57,9 +58,6 @@ - - - PreserveNewest diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 4713b2b..96c170b 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -1,6 +1,6 @@  - netcoreapp2.0 + netcoreapp2.1 false NU1701 diff --git a/WikipediaApi/WikipediaApi.csproj b/WikipediaApi/WikipediaApi.csproj index f182d90..9a86577 100644 --- a/WikipediaApi/WikipediaApi.csproj +++ b/WikipediaApi/WikipediaApi.csproj @@ -1,6 +1,6 @@  - netcoreapp2.0 + netcoreapp2.1 From 926a632641a9908f4d7e5ae0aabec3f850242efb Mon Sep 17 00:00:00 2001 From: Runebaas Date: Sat, 2 Jun 2018 00:16:32 +0200 Subject: [PATCH 084/443] Get guilds from existing redis keys instead of the client --- Geekbot.net/Database/RedisMigration.cs | 22 +++++++++++++++++++++- Geekbot.net/Lib/AlmostRedis/AlmostRedis.cs | 2 +- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Geekbot.net/Database/RedisMigration.cs b/Geekbot.net/Database/RedisMigration.cs index 80965d1..3097a88 100644 --- a/Geekbot.net/Database/RedisMigration.cs +++ b/Geekbot.net/Database/RedisMigration.cs @@ -31,7 +31,27 @@ namespace Geekbot.net.Database public async Task Migrate() { _logger.Information(LogSource.Geekbot, "Starting migration process"); - foreach (var guild in _client.Guilds) + + var keys = _redis.GetAllKeys().Where(e => e.ToString().EndsWith("Messages")); + var guilds = new List(); + + foreach (var key in keys) + { + try + { + var g = _client.GetGuild(ulong.Parse(key.ToString().Split(':').First())); + Console.WriteLine(g.Name); + guilds.Add(g); + } + catch (Exception e) + { + // ignore + } + } + + _logger.Information(LogSource.Geekbot, $"Found {guilds.Count} guilds in redis"); + + foreach (var guild in guilds) { if (guild.MemberCount > 10000) { diff --git a/Geekbot.net/Lib/AlmostRedis/AlmostRedis.cs b/Geekbot.net/Lib/AlmostRedis/AlmostRedis.cs index 5e010f9..c6faf52 100644 --- a/Geekbot.net/Lib/AlmostRedis/AlmostRedis.cs +++ b/Geekbot.net/Lib/AlmostRedis/AlmostRedis.cs @@ -29,7 +29,7 @@ namespace Geekbot.net.Lib.AlmostRedis public IEnumerable GetAllKeys() { - return Connection.GetServer($"{_runParameters.RedisHost}:{_runParameters.RedisPort}", int.Parse(_runParameters.RedisDatabase)).Keys(); + return Connection.GetServer($"{_runParameters.RedisHost}:{_runParameters.RedisPort}").Keys(int.Parse(_runParameters.RedisDatabase)); } } } \ No newline at end of file From 95618b1f8b67af10568a98fff24e3740606cb2e4 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 13 Jun 2018 22:18:57 +0200 Subject: [PATCH 085/443] Make errorhandler and languagehandler async, await all database actions --- Geekbot.net/Commands/Admin/Admin.cs | 40 +++++++++---------- Geekbot.net/Commands/Admin/Mod.cs | 2 +- Geekbot.net/Commands/Admin/Owner.cs | 16 ++++---- Geekbot.net/Commands/Admin/Role.cs | 10 ++--- Geekbot.net/Commands/Admin/Say.cs | 2 +- Geekbot.net/Commands/Audio/Voice.cs | 4 +- Geekbot.net/Commands/Games/Pokedex.cs | 2 +- Geekbot.net/Commands/Games/Roll.cs | 2 +- .../Commands/Integrations/Google/Google.cs | 2 +- .../Integrations/MagicTheGathering.cs | 2 +- Geekbot.net/Commands/Integrations/Mal.cs | 2 +- .../UbranDictionary/UrbanDictionary.cs | 2 +- .../Commands/Integrations/Wikipedia.cs | 2 +- Geekbot.net/Commands/Integrations/Youtube.cs | 2 +- Geekbot.net/Commands/Randomness/Cat/Cat.cs | 2 +- Geekbot.net/Commands/Randomness/CheckEm.cs | 2 +- .../Randomness/Chuck/ChuckNorrisJokes.cs | 2 +- .../Commands/Randomness/Dad/DadJokes.cs | 2 +- Geekbot.net/Commands/Randomness/Dog/Dog.cs | 2 +- Geekbot.net/Commands/Randomness/EightBall.cs | 2 +- Geekbot.net/Commands/Randomness/Gdq.cs | 2 +- Geekbot.net/Commands/Randomness/Ship.cs | 2 +- Geekbot.net/Commands/Randomness/Slap.cs | 16 ++++---- Geekbot.net/Commands/User/GuildInfo.cs | 2 +- Geekbot.net/Commands/User/Karma.cs | 23 +++++------ Geekbot.net/Commands/User/Ranking/Rank.cs | 24 +++++++++-- Geekbot.net/Commands/User/Stats.cs | 13 ++++-- Geekbot.net/Commands/Utils/AvatarGetter.cs | 2 +- .../Commands/Utils/Changelog/Changelog.cs | 2 +- Geekbot.net/Commands/Utils/Choose.cs | 4 +- Geekbot.net/Commands/Utils/Emojify.cs | 2 +- Geekbot.net/Commands/Utils/Help.cs | 2 +- Geekbot.net/Commands/Utils/Info.cs | 4 +- Geekbot.net/Commands/Utils/Poll.cs | 6 +-- Geekbot.net/Commands/Utils/Quote/Quote.cs | 12 +++--- Geekbot.net/Geekbot.net.csproj | 2 +- Geekbot.net/Handlers.cs | 21 ++++++---- Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs | 9 +++-- .../Lib/ErrorHandling/IErrorHandler.cs | 5 ++- .../Lib/Extensions/StringExtensions.cs | 12 ++++++ .../Lib/GlobalSettings/GlobalSettings.cs | 7 ++-- .../Lib/GlobalSettings/IGlobalSettings.cs | 5 ++- Geekbot.net/Lib/Levels/ILevelCalc.cs | 2 +- Geekbot.net/Lib/Levels/LevelCalc.cs | 2 +- .../Lib/Localization/ITranslationHandler.cs | 9 +++-- .../Lib/Localization/TranslationHandler.cs | 26 ++++++------ 46 files changed, 181 insertions(+), 137 deletions(-) create mode 100644 Geekbot.net/Lib/Extensions/StringExtensions.cs diff --git a/Geekbot.net/Commands/Admin/Admin.cs b/Geekbot.net/Commands/Admin/Admin.cs index ba31f3f..fd7bbc6 100644 --- a/Geekbot.net/Commands/Admin/Admin.cs +++ b/Geekbot.net/Commands/Admin/Admin.cs @@ -35,7 +35,7 @@ namespace Geekbot.net.Commands.Admin [Summary("Set a Welcome Message (use '$user' to mention the new joined user).")] public async Task SetWelcomeMessage([Remainder] [Summary("message")] string welcomeMessage) { - var guild = GetGuildSettings(Context.Guild.Id); + var guild = await GetGuildSettings(Context.Guild.Id); guild.WelcomeMessage = welcomeMessage; _database.GuildSettings.Update(guild); await _database.SaveChangesAsync(); @@ -52,7 +52,7 @@ namespace Geekbot.net.Commands.Admin { var m = await channel.SendMessageAsync("verifying..."); - var guild = GetGuildSettings(Context.Guild.Id); + var guild = await GetGuildSettings(Context.Guild.Id); guild.ModChannel = channel.Id.AsLong(); _database.GuildSettings.Update(guild); await _database.SaveChangesAsync(); @@ -65,7 +65,7 @@ namespace Geekbot.net.Commands.Admin } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context, "That channel doesn't seem to be valid"); + await _errorHandler.HandleCommandException(e, Context, "That channel doesn't seem to be valid"); } } @@ -75,7 +75,7 @@ namespace Geekbot.net.Commands.Admin { try { - var guild = GetGuildSettings(Context.Guild.Id); + var guild = await GetGuildSettings(Context.Guild.Id); var modChannel = await GetModChannel(guild.ModChannel.AsUlong()); if (modChannel == null) return; @@ -89,7 +89,7 @@ namespace Geekbot.net.Commands.Admin } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } @@ -99,7 +99,7 @@ namespace Geekbot.net.Commands.Admin { try { - var guild = GetGuildSettings(Context.Guild.Id); + var guild = await GetGuildSettings(Context.Guild.Id); var modChannel = await GetModChannel(guild.ModChannel.AsUlong()); if (modChannel == null) return; @@ -113,7 +113,7 @@ namespace Geekbot.net.Commands.Admin } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } @@ -124,15 +124,15 @@ namespace Geekbot.net.Commands.Admin try { var language = languageRaw.ToUpper(); - var success = _translation.SetLanguage(Context.Guild.Id, language); + var success = await _translation.SetLanguage(Context.Guild.Id, language); if (success) { - var guild = GetGuildSettings(Context.Guild.Id); + var guild = await GetGuildSettings(Context.Guild.Id); guild.Language = language; _database.GuildSettings.Update(guild); await _database.SaveChangesAsync(); - var trans = _translation.GetDict(Context); + var trans = await _translation.GetDict(Context); await ReplyAsync(trans["NewLanguageSet"]); return; } @@ -142,7 +142,7 @@ namespace Geekbot.net.Commands.Admin } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } @@ -153,7 +153,7 @@ namespace Geekbot.net.Commands.Admin try { var language = languageRaw.ToLower(); - var guild = GetGuildSettings(Context.Guild.Id); + var guild = await GetGuildSettings(Context.Guild.Id); guild.WikiLang = language; _database.GuildSettings.Update(guild); await _database.SaveChangesAsync(); @@ -162,7 +162,7 @@ namespace Geekbot.net.Commands.Admin } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } @@ -172,7 +172,7 @@ namespace Geekbot.net.Commands.Admin { try { - var guild = GetGuildSettings(Context.Guild.Id); + var guild = await GetGuildSettings(Context.Guild.Id); guild.Ping = !guild.Ping; _database.GuildSettings.Update(guild); await _database.SaveChangesAsync(); @@ -180,7 +180,7 @@ namespace Geekbot.net.Commands.Admin } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } @@ -190,7 +190,7 @@ namespace Geekbot.net.Commands.Admin { try { - var guild = GetGuildSettings(Context.Guild.Id); + var guild = await GetGuildSettings(Context.Guild.Id); guild.Hui = !guild.Hui; _database.GuildSettings.Update(guild); await _database.SaveChangesAsync(); @@ -198,11 +198,11 @@ namespace Geekbot.net.Commands.Admin } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } - private GuildSettingsModel GetGuildSettings(ulong guildId) + private async Task GetGuildSettings(ulong guildId) { var guild = _database.GuildSettings.FirstOrDefault(g => g.GuildId.Equals(guildId.AsLong())); if (guild != null) return guild; @@ -212,12 +212,12 @@ namespace Geekbot.net.Commands.Admin GuildId = guildId.AsLong(), Hui = false, Ping = false, - Language = "en", + Language = "EN", ShowDelete = false, ShowLeave = false, WikiLang = "en" }); - _database.SaveChanges(); + await _database.SaveChangesAsync(); return _database.GuildSettings.FirstOrDefault(g => g.GuildId.Equals(guildId.AsLong())); } diff --git a/Geekbot.net/Commands/Admin/Mod.cs b/Geekbot.net/Commands/Admin/Mod.cs index a200cac..b014e53 100644 --- a/Geekbot.net/Commands/Admin/Mod.cs +++ b/Geekbot.net/Commands/Admin/Mod.cs @@ -40,7 +40,7 @@ namespace Geekbot.net.Commands.Admin } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context, + await _errorHandler.HandleCommandException(e, Context, $"I don't have enough permissions do that"); } } diff --git a/Geekbot.net/Commands/Admin/Owner.cs b/Geekbot.net/Commands/Admin/Owner.cs index a05bd8e..7cc194b 100644 --- a/Geekbot.net/Commands/Admin/Owner.cs +++ b/Geekbot.net/Commands/Admin/Owner.cs @@ -52,14 +52,14 @@ namespace Geekbot.net.Commands.Admin } await ReplyAsync("starting migration"); - _globalSettings.SetKey("MigrationStatus", "Running"); + await _globalSettings.SetKey("MigrationStatus", "Running"); var redisMigration = new RedisMigration(_database, _redis, _logger, _client); await redisMigration.Migrate(); - _globalSettings.SetKey("MigrationStatus", "Done"); + await _globalSettings.SetKey("MigrationStatus", "Done"); } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } await ReplyAsync("done"); @@ -69,7 +69,7 @@ namespace Geekbot.net.Commands.Admin [Summary("Set the youtube api key")] public async Task SetYoutubeKey([Summary("API Key")] string key) { - _globalSettings.SetKey("YoutubeKey", key); + await _globalSettings.SetKey("YoutubeKey", key); await ReplyAsync("Apikey has been set"); } @@ -77,7 +77,7 @@ namespace Geekbot.net.Commands.Admin [Summary("Set the game that the bot is playing")] public async Task SetGame([Remainder] [Summary("Game")] string key) { - _globalSettings.SetKey("Game", key); + await _globalSettings.SetKey("Game", key); await _client.SetGameAsync(key); _logger.Information(LogSource.Geekbot, $"Changed game to {key}"); await ReplyAsync($"Now Playing {key}"); @@ -109,14 +109,14 @@ namespace Geekbot.net.Commands.Admin } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context, + await _errorHandler.HandleCommandException(e, Context, "Couldn't complete User Repository, see console for more info"); } } [Command("error", RunMode = RunMode.Async)] [Summary("Throw an error un purpose")] - public void PurposefulError() + public async Task PurposefulError() { try { @@ -124,7 +124,7 @@ namespace Geekbot.net.Commands.Admin } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } } diff --git a/Geekbot.net/Commands/Admin/Role.cs b/Geekbot.net/Commands/Admin/Role.cs index c4d2362..a76c9d5 100644 --- a/Geekbot.net/Commands/Admin/Role.cs +++ b/Geekbot.net/Commands/Admin/Role.cs @@ -48,7 +48,7 @@ namespace Geekbot.net.Commands.Admin } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } @@ -87,11 +87,11 @@ namespace Geekbot.net.Commands.Admin } catch (HttpException e) { - _errorHandler.HandleHttpException(e, Context); + await _errorHandler.HandleHttpException(e, Context); } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } @@ -130,7 +130,7 @@ namespace Geekbot.net.Commands.Admin } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } @@ -155,7 +155,7 @@ namespace Geekbot.net.Commands.Admin } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } diff --git a/Geekbot.net/Commands/Admin/Say.cs b/Geekbot.net/Commands/Admin/Say.cs index f3db09b..910d700 100644 --- a/Geekbot.net/Commands/Admin/Say.cs +++ b/Geekbot.net/Commands/Admin/Say.cs @@ -27,7 +27,7 @@ namespace Geekbot.net.Commands.Admin } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } } diff --git a/Geekbot.net/Commands/Audio/Voice.cs b/Geekbot.net/Commands/Audio/Voice.cs index 169f298..aa406cc 100644 --- a/Geekbot.net/Commands/Audio/Voice.cs +++ b/Geekbot.net/Commands/Audio/Voice.cs @@ -59,7 +59,7 @@ namespace Geekbot.net.Commands.Audio } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); _audioUtils.Cleanup(Context.Guild.Id); } } @@ -92,7 +92,7 @@ namespace Geekbot.net.Commands.Audio } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); _audioUtils.Cleanup(Context.Guild.Id); } } diff --git a/Geekbot.net/Commands/Games/Pokedex.cs b/Geekbot.net/Commands/Games/Pokedex.cs index aea442b..fbfb568 100644 --- a/Geekbot.net/Commands/Games/Pokedex.cs +++ b/Geekbot.net/Commands/Games/Pokedex.cs @@ -41,7 +41,7 @@ namespace Geekbot.net.Commands.Games } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } diff --git a/Geekbot.net/Commands/Games/Roll.cs b/Geekbot.net/Commands/Games/Roll.cs index 488488f..e8d2d56 100644 --- a/Geekbot.net/Commands/Games/Roll.cs +++ b/Geekbot.net/Commands/Games/Roll.cs @@ -30,7 +30,7 @@ namespace Geekbot.net.Commands.Games var number = new Random().Next(1, 100); var guess = 1000; int.TryParse(stuff, out guess); - var transDict = _translation.GetDict(Context); + var transDict = await _translation.GetDict(Context); if (guess <= 100 && guess > 0) { var prevRoll = _redis.Db.HashGet($"{Context.Guild.Id}:RollsPrevious", Context.Message.Author.Id); diff --git a/Geekbot.net/Commands/Integrations/Google/Google.cs b/Geekbot.net/Commands/Integrations/Google/Google.cs index 1ad7c8e..6ff926d 100644 --- a/Geekbot.net/Commands/Integrations/Google/Google.cs +++ b/Geekbot.net/Commands/Integrations/Google/Google.cs @@ -61,7 +61,7 @@ namespace Geekbot.net.Commands.Integrations.Google } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } } diff --git a/Geekbot.net/Commands/Integrations/MagicTheGathering.cs b/Geekbot.net/Commands/Integrations/MagicTheGathering.cs index 2284646..2d3e7c6 100644 --- a/Geekbot.net/Commands/Integrations/MagicTheGathering.cs +++ b/Geekbot.net/Commands/Integrations/MagicTheGathering.cs @@ -64,7 +64,7 @@ namespace Geekbot.net.Commands.Integrations } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } diff --git a/Geekbot.net/Commands/Integrations/Mal.cs b/Geekbot.net/Commands/Integrations/Mal.cs index da03ead..7c79861 100644 --- a/Geekbot.net/Commands/Integrations/Mal.cs +++ b/Geekbot.net/Commands/Integrations/Mal.cs @@ -64,7 +64,7 @@ namespace Geekbot.net.Commands.Integrations } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } diff --git a/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs b/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs index 928dfe2..25ab732 100644 --- a/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs +++ b/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs @@ -59,7 +59,7 @@ namespace Geekbot.net.Commands.Integrations.UbranDictionary } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } } diff --git a/Geekbot.net/Commands/Integrations/Wikipedia.cs b/Geekbot.net/Commands/Integrations/Wikipedia.cs index 069ba8c..e1f6092 100644 --- a/Geekbot.net/Commands/Integrations/Wikipedia.cs +++ b/Geekbot.net/Commands/Integrations/Wikipedia.cs @@ -87,7 +87,7 @@ namespace Geekbot.net.Commands.Integrations } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } diff --git a/Geekbot.net/Commands/Integrations/Youtube.cs b/Geekbot.net/Commands/Integrations/Youtube.cs index 2e9fce0..ed630ee 100644 --- a/Geekbot.net/Commands/Integrations/Youtube.cs +++ b/Geekbot.net/Commands/Integrations/Youtube.cs @@ -51,7 +51,7 @@ namespace Geekbot.net.Commands.Integrations } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } } diff --git a/Geekbot.net/Commands/Randomness/Cat/Cat.cs b/Geekbot.net/Commands/Randomness/Cat/Cat.cs index f46ed25..4d43a4c 100644 --- a/Geekbot.net/Commands/Randomness/Cat/Cat.cs +++ b/Geekbot.net/Commands/Randomness/Cat/Cat.cs @@ -45,7 +45,7 @@ namespace Geekbot.net.Commands.Randomness.Cat } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } } diff --git a/Geekbot.net/Commands/Randomness/CheckEm.cs b/Geekbot.net/Commands/Randomness/CheckEm.cs index 94a895a..f8191ad 100644 --- a/Geekbot.net/Commands/Randomness/CheckEm.cs +++ b/Geekbot.net/Commands/Randomness/CheckEm.cs @@ -51,7 +51,7 @@ namespace Geekbot.net.Commands.Randomness } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } diff --git a/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs b/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs index b9d906e..07ea41f 100644 --- a/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs +++ b/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs @@ -44,7 +44,7 @@ namespace Geekbot.net.Commands.Randomness.Chuck } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } } diff --git a/Geekbot.net/Commands/Randomness/Dad/DadJokes.cs b/Geekbot.net/Commands/Randomness/Dad/DadJokes.cs index 3945609..2c51781 100644 --- a/Geekbot.net/Commands/Randomness/Dad/DadJokes.cs +++ b/Geekbot.net/Commands/Randomness/Dad/DadJokes.cs @@ -44,7 +44,7 @@ namespace Geekbot.net.Commands.Randomness.Dad } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } } diff --git a/Geekbot.net/Commands/Randomness/Dog/Dog.cs b/Geekbot.net/Commands/Randomness/Dog/Dog.cs index 6606ef5..176cbad 100644 --- a/Geekbot.net/Commands/Randomness/Dog/Dog.cs +++ b/Geekbot.net/Commands/Randomness/Dog/Dog.cs @@ -45,7 +45,7 @@ namespace Geekbot.net.Commands.Randomness.Dog } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } } diff --git a/Geekbot.net/Commands/Randomness/EightBall.cs b/Geekbot.net/Commands/Randomness/EightBall.cs index b6f37d3..7b3bb1f 100644 --- a/Geekbot.net/Commands/Randomness/EightBall.cs +++ b/Geekbot.net/Commands/Randomness/EightBall.cs @@ -50,7 +50,7 @@ namespace Geekbot.net.Commands.Randomness } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } } diff --git a/Geekbot.net/Commands/Randomness/Gdq.cs b/Geekbot.net/Commands/Randomness/Gdq.cs index c723c6f..2685b33 100644 --- a/Geekbot.net/Commands/Randomness/Gdq.cs +++ b/Geekbot.net/Commands/Randomness/Gdq.cs @@ -31,7 +31,7 @@ namespace Geekbot.net.Commands.Randomness } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } } diff --git a/Geekbot.net/Commands/Randomness/Ship.cs b/Geekbot.net/Commands/Randomness/Ship.cs index 0f33da5..45956b6 100644 --- a/Geekbot.net/Commands/Randomness/Ship.cs +++ b/Geekbot.net/Commands/Randomness/Ship.cs @@ -59,7 +59,7 @@ namespace Geekbot.net.Commands.Randomness } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } diff --git a/Geekbot.net/Commands/Randomness/Slap.cs b/Geekbot.net/Commands/Randomness/Slap.cs index fea5991..412b20c 100644 --- a/Geekbot.net/Commands/Randomness/Slap.cs +++ b/Geekbot.net/Commands/Randomness/Slap.cs @@ -81,8 +81,8 @@ namespace Geekbot.net.Commands.Randomness await ReplyAsync($"{Context.User.Username} slapped {user.Username} with a {things[new Random().Next(things.Count - 1)]}"); - UpdateRecieved(user.Id); - UpdateGiven(Context.User.Id); + await UpdateRecieved(user.Id); + await UpdateGiven(Context.User.Id); await _database.SaveChangesAsync(); } catch (Exception e) @@ -91,21 +91,21 @@ namespace Geekbot.net.Commands.Randomness } } - private void UpdateGiven(ulong userId) + private async Task UpdateGiven(ulong userId) { - var user = GetUser(userId); + var user = await GetUser(userId); user.Given++; _database.Slaps.Update(user); } - private void UpdateRecieved(ulong userId) + private async Task UpdateRecieved(ulong userId) { - var user = GetUser(userId); + var user = await GetUser(userId); user.Recieved++; _database.Slaps.Update(user); } - private SlapsModel GetUser(ulong userId) + private async Task GetUser(ulong userId) { var user = _database.Slaps.FirstOrDefault(e => e.GuildId.Equals(Context.Guild.Id.AsLong()) && @@ -121,7 +121,7 @@ namespace Geekbot.net.Commands.Randomness Recieved = 0, Given = 0 }); - _database.SaveChanges(); + await _database.SaveChangesAsync(); return _database.Slaps.FirstOrDefault(e => e.GuildId.Equals(Context.Guild.Id.AsLong()) && e.UserId.Equals(userId.AsLong())); diff --git a/Geekbot.net/Commands/User/GuildInfo.cs b/Geekbot.net/Commands/User/GuildInfo.cs index 6f602b1..342f80f 100644 --- a/Geekbot.net/Commands/User/GuildInfo.cs +++ b/Geekbot.net/Commands/User/GuildInfo.cs @@ -51,7 +51,7 @@ namespace Geekbot.net.Commands.User } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } } diff --git a/Geekbot.net/Commands/User/Karma.cs b/Geekbot.net/Commands/User/Karma.cs index 8a9189d..beb1334 100644 --- a/Geekbot.net/Commands/User/Karma.cs +++ b/Geekbot.net/Commands/User/Karma.cs @@ -30,8 +30,8 @@ namespace Geekbot.net.Commands.User { try { - var transDict = _translation.GetDict(Context); - var actor = GetUser(Context.User.Id); + var transDict = await _translation.GetDict(Context); + var actor = await GetUser(Context.User.Id); if (user.Id == Context.User.Id) { await ReplyAsync(string.Format(transDict["CannotChangeOwn"], Context.User.Username)); @@ -43,7 +43,7 @@ namespace Geekbot.net.Commands.User } else { - var target = GetUser(user.Id); + var target = await GetUser(user.Id); target.Karma = target.Karma + 1; SetUser(target); @@ -77,8 +77,8 @@ namespace Geekbot.net.Commands.User { try { - var transDict = _translation.GetDict(Context); - var actor = GetUser(Context.User.Id); + var transDict = await _translation.GetDict(Context); + var actor = await GetUser(Context.User.Id); if (user.Id == Context.User.Id) { await ReplyAsync(string.Format(transDict["CannotChangeOwn"], Context.User.Username)); @@ -90,7 +90,7 @@ namespace Geekbot.net.Commands.User } else { - var target = GetUser(user.Id); + var target = await GetUser(user.Id); target.Karma = target.Karma - 1; SetUser(target); @@ -129,19 +129,18 @@ namespace Geekbot.net.Commands.User return $"{dt.Minutes} Minutes and {dt.Seconds} Seconds"; } - private KarmaModel GetUser(ulong userId) + private async Task GetUser(ulong userId) { - var user = _database.Karma.FirstOrDefault(u =>u.GuildId.Equals(Context.Guild.Id.AsLong()) && u.UserId.Equals(userId.AsLong())) ?? CreateNewRow(userId); + var user = _database.Karma.FirstOrDefault(u =>u.GuildId.Equals(Context.Guild.Id.AsLong()) && u.UserId.Equals(userId.AsLong())) ?? await CreateNewRow(userId); return user; } - private bool SetUser(KarmaModel user) + private void SetUser(KarmaModel user) { _database.Karma.Update(user); - return true; } - private KarmaModel CreateNewRow(ulong userId) + private async Task CreateNewRow(ulong userId) { var user = new KarmaModel() { @@ -151,7 +150,7 @@ namespace Geekbot.net.Commands.User TimeOut = DateTimeOffset.MinValue }; var newUser = _database.Karma.Add(user).Entity; - _database.SaveChanges(); + await _database.SaveChangesAsync(); return newUser; } } diff --git a/Geekbot.net/Commands/User/Ranking/Rank.cs b/Geekbot.net/Commands/User/Ranking/Rank.cs index f404b58..a2e9891 100644 --- a/Geekbot.net/Commands/User/Ranking/Rank.cs +++ b/Geekbot.net/Commands/User/Ranking/Rank.cs @@ -81,6 +81,15 @@ namespace Geekbot.net.Commands.User.Ranking return; } + int guildMessages = 0; + if (type == RankType.messages) + { + guildMessages = _database.Messages + .Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong())) + .Select(e => e.MessageCount) + .Sum(); + } + var highscoreUsers = new Dictionary(); var failedToRetrieveUser = false; foreach (var user in list) @@ -112,7 +121,7 @@ namespace Geekbot.net.Commands.User.Ranking } if (failedToRetrieveUser) replyBuilder.AppendLine(":warning: Couldn't get all userdata\n"); - replyBuilder.AppendLine($":bar_chart: **{type} Highscore for {Context.Guild.Name}**"); + replyBuilder.AppendLine($":bar_chart: **{type.ToString().CapitalizeFirst()} Highscore for {Context.Guild.Name}**"); var highscorePlace = 1; foreach (var user in highscoreUsers) { @@ -123,8 +132,10 @@ namespace Geekbot.net.Commands.User.Ranking replyBuilder.Append(user.Key.Username != null ? $"**{user.Key.Username}#{user.Key.Discriminator}**" : $"**{user.Key.Id}**"); - - replyBuilder.Append($" - {user.Value} {type}\n"); + + replyBuilder.Append(type == RankType.messages + ? $" - {user.Value} {type} - {Math.Round((double) (100 * user.Value) / guildMessages, digits: 2)}%\n" + : $" - {user.Value} {type}\n"); highscorePlace++; } @@ -133,12 +144,17 @@ namespace Geekbot.net.Commands.User.Ranking } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } private Dictionary GetMessageList(int amount) { +// return _database.Messages +// .Where(k => k.GuildId.Equals(Context.Guild.Id.AsLong())) +// .OrderByDescending(o => o.MessageCount) +// .Take(amount) +// .ToDictionary(key => key.UserId.AsUlong(), key => key.MessageCount); return _redis.Db .HashGetAll($"{Context.Guild.Id}:Messages").ToDictionary().Take(amount + 1) .Where(user => !user.Key.Equals(0)) diff --git a/Geekbot.net/Commands/User/Stats.cs b/Geekbot.net/Commands/User/Stats.cs index 4a82a89..990b229 100644 --- a/Geekbot.net/Commands/User/Stats.cs +++ b/Geekbot.net/Commands/User/Stats.cs @@ -39,11 +39,18 @@ namespace Geekbot.net.Commands.User var age = Math.Floor((DateTime.Now - createdAt).TotalDays); var joinedDayAgo = Math.Floor((DateTime.Now - joinedAt).TotalDays); - var messages = (int) _redis.Db.HashGet($"{Context.Guild.Id}:Messages", userInfo.Id.ToString()); - var guildMessages = (int) _redis.Db.HashGet($"{Context.Guild.Id}:Messages", 0.ToString()); + var messages = _database.Messages.FirstOrDefault(e => + e.GuildId.Equals(Context.Guild.Id.AsLong()) && + e.UserId.Equals(userInfo.Id.AsLong()))?.MessageCount; + + var guildMessages = _database.Messages + .Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong())) + .Select(e => e.MessageCount) + .Sum(); + var level = _levelCalc.GetLevel(messages); - var percent = Math.Round((double) (100 * messages) / guildMessages, 2); + var percent = Math.Round((double) (100 * messages) / guildMessages, digits: 2); var eb = new EmbedBuilder(); eb.WithAuthor(new EmbedAuthorBuilder() diff --git a/Geekbot.net/Commands/Utils/AvatarGetter.cs b/Geekbot.net/Commands/Utils/AvatarGetter.cs index 3f74770..e329ad4 100644 --- a/Geekbot.net/Commands/Utils/AvatarGetter.cs +++ b/Geekbot.net/Commands/Utils/AvatarGetter.cs @@ -27,7 +27,7 @@ namespace Geekbot.net.Commands.Utils } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } } diff --git a/Geekbot.net/Commands/Utils/Changelog/Changelog.cs b/Geekbot.net/Commands/Utils/Changelog/Changelog.cs index 5d47c08..adcc9a3 100644 --- a/Geekbot.net/Commands/Utils/Changelog/Changelog.cs +++ b/Geekbot.net/Commands/Utils/Changelog/Changelog.cs @@ -60,7 +60,7 @@ namespace Geekbot.net.Commands.Utils.Changelog } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } } diff --git a/Geekbot.net/Commands/Utils/Choose.cs b/Geekbot.net/Commands/Utils/Choose.cs index 411802a..ee2bf0d 100644 --- a/Geekbot.net/Commands/Utils/Choose.cs +++ b/Geekbot.net/Commands/Utils/Choose.cs @@ -24,14 +24,14 @@ namespace Geekbot.net.Commands.Utils { try { - var transDict = _translation.GetDict(Context); + var transDict = await _translation.GetDict(Context); var choicesArray = choices.Split(';'); var choice = new Random().Next(choicesArray.Length); await ReplyAsync(string.Format(transDict["Choice"], choicesArray[choice])); } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } } diff --git a/Geekbot.net/Commands/Utils/Emojify.cs b/Geekbot.net/Commands/Utils/Emojify.cs index f0e172e..5355aff 100644 --- a/Geekbot.net/Commands/Utils/Emojify.cs +++ b/Geekbot.net/Commands/Utils/Emojify.cs @@ -35,7 +35,7 @@ namespace Geekbot.net.Commands.Utils } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } } diff --git a/Geekbot.net/Commands/Utils/Help.cs b/Geekbot.net/Commands/Utils/Help.cs index ace5087..a6f0084 100644 --- a/Geekbot.net/Commands/Utils/Help.cs +++ b/Geekbot.net/Commands/Utils/Help.cs @@ -32,7 +32,7 @@ namespace Geekbot.net.Commands.Utils } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } } diff --git a/Geekbot.net/Commands/Utils/Info.cs b/Geekbot.net/Commands/Utils/Info.cs index aca4dcd..b1d5403 100644 --- a/Geekbot.net/Commands/Utils/Info.cs +++ b/Geekbot.net/Commands/Utils/Info.cs @@ -51,7 +51,7 @@ namespace Geekbot.net.Commands.Utils } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } @@ -66,7 +66,7 @@ namespace Geekbot.net.Commands.Utils } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } } diff --git a/Geekbot.net/Commands/Utils/Poll.cs b/Geekbot.net/Commands/Utils/Poll.cs index 585c1fb..c5a0119 100644 --- a/Geekbot.net/Commands/Utils/Poll.cs +++ b/Geekbot.net/Commands/Utils/Poll.cs @@ -48,7 +48,7 @@ namespace Geekbot.net.Commands.Utils } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } @@ -120,7 +120,7 @@ namespace Geekbot.net.Commands.Utils } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } @@ -148,7 +148,7 @@ namespace Geekbot.net.Commands.Utils } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } diff --git a/Geekbot.net/Commands/Utils/Quote/Quote.cs b/Geekbot.net/Commands/Utils/Quote/Quote.cs index 6af74d7..9a1d072 100644 --- a/Geekbot.net/Commands/Utils/Quote/Quote.cs +++ b/Geekbot.net/Commands/Utils/Quote/Quote.cs @@ -45,7 +45,7 @@ namespace Geekbot.net.Commands.Utils.Quote } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context, "Whoops, seems like the quote was to edgy to return"); + await _errorHandler.HandleCommandException(e, Context, "Whoops, seems like the quote was to edgy to return"); } } @@ -79,7 +79,7 @@ namespace Geekbot.net.Commands.Utils.Quote } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context, + await _errorHandler.HandleCommandException(e, Context, "I counldn't find a quote from that user :disappointed:"); } } @@ -112,7 +112,7 @@ namespace Geekbot.net.Commands.Utils.Quote } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context, + await _errorHandler.HandleCommandException(e, Context, "I couldn't find a message with that id :disappointed:"); } } @@ -131,7 +131,7 @@ namespace Geekbot.net.Commands.Utils.Quote } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context, + await _errorHandler.HandleCommandException(e, Context, "I counldn't find a quote from that user :disappointed:"); } } @@ -149,7 +149,7 @@ namespace Geekbot.net.Commands.Utils.Quote } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context, + await _errorHandler.HandleCommandException(e, Context, "I couldn't find a message with that id :disappointed:"); } } @@ -178,7 +178,7 @@ namespace Geekbot.net.Commands.Utils.Quote } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context, "I couldn't find a quote with that id :disappointed:"); + await _errorHandler.HandleCommandException(e, Context, "I couldn't find a quote with that id :disappointed:"); } } diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 9019d87..6e857bf 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -44,7 +44,7 @@ - + diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index 0aab944..0331ca9 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -11,6 +11,7 @@ using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Logger; using Geekbot.net.Lib.ReactionListener; using Geekbot.net.Lib.UserRepository; +using Microsoft.EntityFrameworkCore; namespace Geekbot.net { @@ -93,29 +94,33 @@ namespace Geekbot.net } } - public Task UpdateStats(SocketMessage message) + public async Task UpdateStats(SocketMessage message) { try { - if (message == null) return Task.CompletedTask; + if (message == null) return; if (message.Channel.Name.StartsWith('@')) { _logger.Information(LogSource.Message, $"[DM-Channel] {message.Content}", SimpleConextConverter.ConvertSocketMessage(message)); - return Task.CompletedTask; + return; } var channel = (SocketGuildChannel) message.Channel; + +// await _database.Database.ExecuteSqlCommandAsync("UPDATE \"Messages\" " + +// $"SET \"MessageCount\" = \"MessageCount\" + {1} " + +// $"WHERE \"GuildId\" = '{channel.Guild.Id.AsLong()}' " + +// $"AND \"UserId\" = '{message.Author.Id.AsLong()}'"); +// + await _redis.Db.HashIncrementAsync($"{channel.Guild.Id}:Messages", message.Author.Id.ToString()); + await _redis.Db.HashIncrementAsync($"{channel.Guild.Id}:Messages", 0.ToString()); - _redis.Db.HashIncrementAsync($"{channel.Guild.Id}:Messages", message.Author.Id.ToString()); - _redis.Db.HashIncrementAsync($"{channel.Guild.Id}:Messages", 0.ToString()); - - if (message.Author.IsBot) return Task.CompletedTask; + if (message.Author.IsBot) return; _logger.Information(LogSource.Message, message.Content, SimpleConextConverter.ConvertSocketMessage(message)); } catch (Exception e) { _logger.Error(LogSource.Message, "Could not process message stats", e); } - return Task.CompletedTask; } // diff --git a/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs b/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs index 99d1e8b..9556b84 100644 --- a/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs +++ b/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs @@ -1,5 +1,6 @@ using System; using System.Net; +using System.Threading.Tasks; using Discord.Commands; using Discord.Net; using Geekbot.net.Lib.Localization; @@ -34,11 +35,11 @@ namespace Geekbot.net.Lib.ErrorHandling } } - public void HandleCommandException(Exception e, ICommandContext context, string errorMessage = "def") + public async Task HandleCommandException(Exception e, ICommandContext context, string errorMessage = "def") { try { - var errorString = errorMessage == "def" ? _translation.GetString(context.Guild.Id, "errorHandler", "SomethingWentWrong") : errorMessage; + var errorString = errorMessage == "def" ? await _translation.GetString(context.Guild.Id, "errorHandler", "SomethingWentWrong") : errorMessage; var errorObj = SimpleConextConverter.ConvertContext(context); if (e.Message.Contains("50007")) return; if (e.Message.Contains("50013")) return; @@ -86,9 +87,9 @@ namespace Geekbot.net.Lib.ErrorHandling } } - public async void HandleHttpException(HttpException e, ICommandContext context) + public async Task HandleHttpException(HttpException e, ICommandContext context) { - var errorStrings = _translation.GetDict(context, "httpErrors"); + var errorStrings = await _translation.GetDict(context, "httpErrors"); switch(e.HttpCode) { case HttpStatusCode.Forbidden: diff --git a/Geekbot.net/Lib/ErrorHandling/IErrorHandler.cs b/Geekbot.net/Lib/ErrorHandling/IErrorHandler.cs index f2d8dac..05e0d8e 100644 --- a/Geekbot.net/Lib/ErrorHandling/IErrorHandler.cs +++ b/Geekbot.net/Lib/ErrorHandling/IErrorHandler.cs @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using Discord.Commands; using Discord.Net; @@ -6,7 +7,7 @@ namespace Geekbot.net.Lib.ErrorHandling { public interface IErrorHandler { - void HandleCommandException(Exception e, ICommandContext context, string errorMessage = "def"); - void HandleHttpException(HttpException e, ICommandContext context); + Task HandleCommandException(Exception e, ICommandContext context, string errorMessage = "def"); + Task HandleHttpException(HttpException e, ICommandContext context); } } \ No newline at end of file diff --git a/Geekbot.net/Lib/Extensions/StringExtensions.cs b/Geekbot.net/Lib/Extensions/StringExtensions.cs new file mode 100644 index 0000000..9ec1dfe --- /dev/null +++ b/Geekbot.net/Lib/Extensions/StringExtensions.cs @@ -0,0 +1,12 @@ +using System.Linq; + +namespace Geekbot.net.Lib.Extensions +{ + public static class StringExtensions + { + public static string CapitalizeFirst(this string source) + { + return source.First().ToString().ToUpper() + source.Substring(1); + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/GlobalSettings/GlobalSettings.cs b/Geekbot.net/Lib/GlobalSettings/GlobalSettings.cs index 1234b34..8a1d0e4 100644 --- a/Geekbot.net/Lib/GlobalSettings/GlobalSettings.cs +++ b/Geekbot.net/Lib/GlobalSettings/GlobalSettings.cs @@ -1,4 +1,5 @@ using System.Linq; +using System.Threading.Tasks; using Geekbot.net.Database; using Geekbot.net.Database.Models; @@ -13,7 +14,7 @@ namespace Geekbot.net.Lib.GlobalSettings _database = database; } - public bool SetKey(string keyName, string value) + public async Task SetKey(string keyName, string value) { try { @@ -25,13 +26,13 @@ namespace Geekbot.net.Lib.GlobalSettings Name = keyName, Value = value }); - _database.SaveChanges(); + await _database.SaveChangesAsync(); return true; } key.Value = value; _database.Globals.Update(key); - _database.SaveChanges(); + await _database.SaveChangesAsync(); return true; } catch diff --git a/Geekbot.net/Lib/GlobalSettings/IGlobalSettings.cs b/Geekbot.net/Lib/GlobalSettings/IGlobalSettings.cs index 1919b50..4833215 100644 --- a/Geekbot.net/Lib/GlobalSettings/IGlobalSettings.cs +++ b/Geekbot.net/Lib/GlobalSettings/IGlobalSettings.cs @@ -1,10 +1,11 @@ -using Geekbot.net.Database.Models; +using System.Threading.Tasks; +using Geekbot.net.Database.Models; namespace Geekbot.net.Lib.GlobalSettings { public interface IGlobalSettings { - bool SetKey(string keyName, string value); + Task SetKey(string keyName, string value); string GetKey(string keyName); GlobalsModel GetKeyFull(string keyName); } diff --git a/Geekbot.net/Lib/Levels/ILevelCalc.cs b/Geekbot.net/Lib/Levels/ILevelCalc.cs index 6353132..cc488e7 100644 --- a/Geekbot.net/Lib/Levels/ILevelCalc.cs +++ b/Geekbot.net/Lib/Levels/ILevelCalc.cs @@ -2,6 +2,6 @@ { public interface ILevelCalc { - int GetLevel(int experience); + int GetLevel(int? experience); } } \ No newline at end of file diff --git a/Geekbot.net/Lib/Levels/LevelCalc.cs b/Geekbot.net/Lib/Levels/LevelCalc.cs index 9ecf803..f5b6b80 100644 --- a/Geekbot.net/Lib/Levels/LevelCalc.cs +++ b/Geekbot.net/Lib/Levels/LevelCalc.cs @@ -19,7 +19,7 @@ namespace Geekbot.net.Lib.Levels _levels = levels.ToArray(); } - public int GetLevel(int messages) + public int GetLevel(int? messages) { var returnVal = 1; foreach (var level in _levels) diff --git a/Geekbot.net/Lib/Localization/ITranslationHandler.cs b/Geekbot.net/Lib/Localization/ITranslationHandler.cs index df4d707..2124d4e 100644 --- a/Geekbot.net/Lib/Localization/ITranslationHandler.cs +++ b/Geekbot.net/Lib/Localization/ITranslationHandler.cs @@ -1,14 +1,15 @@ using System.Collections.Generic; +using System.Threading.Tasks; using Discord.Commands; namespace Geekbot.net.Lib.Localization { public interface ITranslationHandler { - string GetString(ulong guildId, string command, string stringName); - Dictionary GetDict(ICommandContext context); - Dictionary GetDict(ICommandContext context, string command); - bool SetLanguage(ulong guildId, string language); + Task GetString(ulong guildId, string command, string stringName); + Task> GetDict(ICommandContext context); + Task> GetDict(ICommandContext context, string command); + Task SetLanguage(ulong guildId, string language); List SupportedLanguages { get; } } } \ No newline at end of file diff --git a/Geekbot.net/Lib/Localization/TranslationHandler.cs b/Geekbot.net/Lib/Localization/TranslationHandler.cs index f8e2d88..88e518e 100644 --- a/Geekbot.net/Lib/Localization/TranslationHandler.cs +++ b/Geekbot.net/Lib/Localization/TranslationHandler.cs @@ -2,8 +2,8 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Threading.Tasks; using Discord.Commands; -using Discord.WebSocket; using Geekbot.net.Database; using Geekbot.net.Database.Models; using Geekbot.net.Lib.Extensions; @@ -78,7 +78,7 @@ namespace Geekbot.net.Lib.Localization } } - private string GetServerLanguage(ulong guildId) + private async Task GetServerLanguage(ulong guildId) { try { @@ -94,7 +94,7 @@ namespace Geekbot.net.Lib.Localization } catch { - lang = GetGuild(guildId).Language ?? "EN"; + lang = (await GetGuild(guildId)).Language ?? "EN"; _serverLanguages[guildId] = lang; return lang; } @@ -106,9 +106,9 @@ namespace Geekbot.net.Lib.Localization } } - public string GetString(ulong guildId, string command, string stringName) + public async Task GetString(ulong guildId, string command, string stringName) { - var translation = _translations[GetServerLanguage(guildId)][command][stringName]; + var translation = _translations[await GetServerLanguage(guildId)][command][stringName]; if (!string.IsNullOrWhiteSpace(translation)) return translation; translation = _translations[command][stringName]["EN"]; if (string.IsNullOrWhiteSpace(translation)) @@ -118,12 +118,12 @@ namespace Geekbot.net.Lib.Localization return translation; } - public Dictionary GetDict(ICommandContext context) + public async Task> GetDict(ICommandContext context) { try { var command = context.Message.Content.Split(' ').First().TrimStart('!').ToLower(); - return _translations[GetServerLanguage(context.Guild.Id)][command]; + return _translations[await GetServerLanguage(context.Guild.Id)][command]; } catch (Exception e) { @@ -132,11 +132,11 @@ namespace Geekbot.net.Lib.Localization } } - public Dictionary GetDict(ICommandContext context, string command) + public async Task> GetDict(ICommandContext context, string command) { try { - return _translations[GetServerLanguage(context.Guild.Id)][command]; + return _translations[await GetServerLanguage(context.Guild.Id)][command]; } catch (Exception e) { @@ -145,12 +145,12 @@ namespace Geekbot.net.Lib.Localization } } - public bool SetLanguage(ulong guildId, string language) + public async Task SetLanguage(ulong guildId, string language) { try { if (!_supportedLanguages.Contains(language)) return false; - var guild = GetGuild(guildId); + var guild = await GetGuild(guildId); guild.Language = language; _database.GuildSettings.Update(guild); _serverLanguages[guildId] = language; @@ -165,7 +165,7 @@ namespace Geekbot.net.Lib.Localization public List SupportedLanguages => _supportedLanguages; - private GuildSettingsModel GetGuild(ulong guildId) + private async Task GetGuild(ulong guildId) { var guild = _database.GuildSettings.FirstOrDefault(g => g.GuildId.Equals(guildId.AsLong())); if (guild != null) return guild; @@ -173,7 +173,7 @@ namespace Geekbot.net.Lib.Localization { GuildId = guildId.AsLong() }); - _database.SaveChanges(); + await _database.SaveChangesAsync(); return _database.GuildSettings.FirstOrDefault(g => g.GuildId.Equals(guildId.AsLong())); } } From 18b3436d66aff23814b16f39c3efa39ef42140d3 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 28 Jul 2018 16:31:18 +0200 Subject: [PATCH 086/443] add more awaits and update !roll --- Geekbot.net.sln.DotSettings | 11 ++++ Geekbot.net/Commands/Audio/Voice.cs | 2 +- Geekbot.net/Commands/Games/Roll.cs | 17 ++++--- Geekbot.net/Commands/Integrations/Mal.cs | 2 +- Geekbot.net/Commands/Randomness/Slap.cs | 2 +- Geekbot.net/Commands/User/Karma.cs | 4 +- Geekbot.net/Commands/User/Stats.cs | 2 +- Geekbot.net/Geekbot.net.csproj | 2 +- Geekbot.net/Handlers.cs | 51 +++++++++---------- Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs | 8 +-- Geekbot.net/Program.cs | 4 +- Tests/Tests.csproj | 2 +- 12 files changed, 60 insertions(+), 47 deletions(-) create mode 100644 Geekbot.net.sln.DotSettings diff --git a/Geekbot.net.sln.DotSettings b/Geekbot.net.sln.DotSettings new file mode 100644 index 0000000..7fa04b3 --- /dev/null +++ b/Geekbot.net.sln.DotSettings @@ -0,0 +1,11 @@ + + NEVER + 200 + True + True + True + True + True + True + True + True \ No newline at end of file diff --git a/Geekbot.net/Commands/Audio/Voice.cs b/Geekbot.net/Commands/Audio/Voice.cs index aa406cc..b12d6e2 100644 --- a/Geekbot.net/Commands/Audio/Voice.cs +++ b/Geekbot.net/Commands/Audio/Voice.cs @@ -37,7 +37,7 @@ namespace Geekbot.net.Commands.Audio } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } diff --git a/Geekbot.net/Commands/Games/Roll.cs b/Geekbot.net/Commands/Games/Roll.cs index e8d2d56..c629ed8 100644 --- a/Geekbot.net/Commands/Games/Roll.cs +++ b/Geekbot.net/Commands/Games/Roll.cs @@ -33,15 +33,18 @@ namespace Geekbot.net.Commands.Games var transDict = await _translation.GetDict(Context); if (guess <= 100 && guess > 0) { - var prevRoll = _redis.Db.HashGet($"{Context.Guild.Id}:RollsPrevious", Context.Message.Author.Id); - if (!prevRoll.IsNullOrEmpty && prevRoll.ToString() == guess.ToString()) + var prevRoll = _redis.Db.HashGet($"{Context.Guild.Id}:RollsPrevious2", Context.Message.Author.Id).ToString()?.Split('|'); + if (prevRoll?.Length == 2) { - await ReplyAsync(string.Format(transDict["NoPrevGuess"], Context.Message.Author.Mention)); - return; + if (prevRoll[0] == guess.ToString() && DateTime.Parse(prevRoll[1]) > DateTime.Now.AddDays(-1)) + { + await ReplyAsync(string.Format(transDict["NoPrevGuess"], Context.Message.Author.Mention)); + return; + } } - _redis.Db.HashSet($"{Context.Guild.Id}:RollsPrevious", - new[] {new HashEntry(Context.Message.Author.Id, guess)}); + _redis.Db.HashSet($"{Context.Guild.Id}:RollsPrevious2", new[] {new HashEntry(Context.Message.Author.Id, $"{guess}|{DateTime.Now}")}); + await ReplyAsync(string.Format(transDict["Rolled"], Context.Message.Author.Mention, number, guess)); if (guess == number) { @@ -56,7 +59,7 @@ namespace Geekbot.net.Commands.Games } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } } diff --git a/Geekbot.net/Commands/Integrations/Mal.cs b/Geekbot.net/Commands/Integrations/Mal.cs index 7c79861..88f500e 100644 --- a/Geekbot.net/Commands/Integrations/Mal.cs +++ b/Geekbot.net/Commands/Integrations/Mal.cs @@ -112,7 +112,7 @@ namespace Geekbot.net.Commands.Integrations } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } } diff --git a/Geekbot.net/Commands/Randomness/Slap.cs b/Geekbot.net/Commands/Randomness/Slap.cs index 412b20c..13cd22a 100644 --- a/Geekbot.net/Commands/Randomness/Slap.cs +++ b/Geekbot.net/Commands/Randomness/Slap.cs @@ -87,7 +87,7 @@ namespace Geekbot.net.Commands.Randomness } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } diff --git a/Geekbot.net/Commands/User/Karma.cs b/Geekbot.net/Commands/User/Karma.cs index beb1334..39b05be 100644 --- a/Geekbot.net/Commands/User/Karma.cs +++ b/Geekbot.net/Commands/User/Karma.cs @@ -67,7 +67,7 @@ namespace Geekbot.net.Commands.User } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } @@ -114,7 +114,7 @@ namespace Geekbot.net.Commands.User } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } diff --git a/Geekbot.net/Commands/User/Stats.cs b/Geekbot.net/Commands/User/Stats.cs index 990b229..4d4d800 100644 --- a/Geekbot.net/Commands/User/Stats.cs +++ b/Geekbot.net/Commands/User/Stats.cs @@ -80,7 +80,7 @@ namespace Geekbot.net.Commands.User } catch (Exception e) { - _errorHandler.HandleCommandException(e, Context); + await _errorHandler.HandleCommandException(e, Context); } } } diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 6e857bf..68a1870 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -38,7 +38,7 @@ - + diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index 0331ca9..9811432 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -25,8 +25,8 @@ namespace Geekbot.net private readonly CommandService _commands; private readonly IUserRepository _userRepository; private readonly IReactionListener _reactionListener; - - public Handlers(DatabaseContext database, IDiscordClient client, IGeekbotLogger logger, IAlmostRedis redis, + + public Handlers(DatabaseContext database, IDiscordClient client, IGeekbotLogger logger, IAlmostRedis redis, IServiceProvider servicesProvider, CommandService commands, IUserRepository userRepository, IReactionListener reactionListener) { @@ -39,11 +39,11 @@ namespace Geekbot.net _userRepository = userRepository; _reactionListener = reactionListener; } - + // // Incoming Messages // - + public Task RunCommand(SocketMessage messageParam) { try @@ -51,33 +51,31 @@ namespace Geekbot.net if (!(messageParam is SocketUserMessage message)) return Task.CompletedTask; if (message.Author.IsBot) return Task.CompletedTask; var argPos = 0; - + // ToDo: remove // if (!message.Author.Id.Equals(93061333972455424)) return Task.CompletedTask; - + var lowCaseMsg = message.ToString().ToLower(); if (lowCaseMsg.StartsWith("hui")) { - var hasPing = _database.GuildSettings.FirstOrDefault(guild => - guild.GuildId.Equals(((SocketGuildChannel) message.Channel).Guild.Id.AsLong())) - ?.Hui ?? false; + var hasPing = _database.GuildSettings.FirstOrDefault(guild => guild.GuildId.Equals(((SocketGuildChannel) message.Channel).Guild.Id.AsLong()))?.Hui ?? false; if (hasPing) { message.Channel.SendMessageAsync("hui!!!"); return Task.CompletedTask; } } + if (lowCaseMsg.StartsWith("ping ") || lowCaseMsg.Equals("ping")) { - var hasPing = _database.GuildSettings.FirstOrDefault(guild => - guild.GuildId.Equals(((SocketGuildChannel) message.Channel).Guild.Id.AsLong())) - ?.Ping ?? false; + var hasPing = _database.GuildSettings.FirstOrDefault(guild => guild.GuildId.Equals(((SocketGuildChannel) message.Channel).Guild.Id.AsLong()))?.Ping ?? false; if (hasPing) { message.Channel.SendMessageAsync("pong"); - return Task.CompletedTask; + return Task.CompletedTask; } } + if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos))) return Task.CompletedTask; var context = new CommandContext(_client, message); @@ -104,8 +102,9 @@ namespace Geekbot.net _logger.Information(LogSource.Message, $"[DM-Channel] {message.Content}", SimpleConextConverter.ConvertSocketMessage(message)); return; } + var channel = (SocketGuildChannel) message.Channel; - + // await _database.Database.ExecuteSqlCommandAsync("UPDATE \"Messages\" " + // $"SET \"MessageCount\" = \"MessageCount\" + {1} " + // $"WHERE \"GuildId\" = '{channel.Guild.Id.AsLong()}' " + @@ -122,26 +121,25 @@ namespace Geekbot.net _logger.Error(LogSource.Message, "Could not process message stats", e); } } - + // // User Stuff // - + public async Task UserJoined(SocketGuildUser user) { try { if (!user.IsBot) { - var message = _database.GuildSettings.FirstOrDefault(guild => - guild.GuildId.Equals(user.Guild.Id.AsLong())) - ?.WelcomeMessage; + var message = _database.GuildSettings.FirstOrDefault(guild => guild.GuildId.Equals(user.Guild.Id.AsLong()))?.WelcomeMessage; if (!string.IsNullOrEmpty(message)) { message = message.Replace("$user", user.Mention); await user.Guild.DefaultChannel.SendMessageAsync(message); } } + await _userRepository.Update(user); _logger.Information(LogSource.Geekbot, $"{user.Username} ({user.Id}) joined {user.Guild.Name} ({user.Guild.Id})"); } @@ -172,13 +170,14 @@ namespace Geekbot.net { _logger.Error(LogSource.Geekbot, "Failed to send leave message", e); } + _logger.Information(LogSource.Geekbot, $"{user.Username} ({user.Id}) joined {user.Guild.Name} ({user.Guild.Id})"); } - + // // Message Stuff // - + public async Task MessageDeleted(Cacheable message, ISocketMessageChannel channel) { try @@ -192,14 +191,14 @@ namespace Geekbot.net var sb = new StringBuilder(); if (message.Value != null) { - sb.AppendLine( - $"The following message from {message.Value.Author.Username}#{message.Value.Author.Discriminator} was deleted in <#{channel.Id}>"); + sb.AppendLine($"The following message from {message.Value.Author.Username}#{message.Value.Author.Discriminator} was deleted in <#{channel.Id}>"); sb.AppendLine(message.Value.Content); } else { sb.AppendLine("Someone deleted a message, the message was not cached..."); } + await modChannelSocket.SendMessageAsync(sb.ToString()); } } @@ -208,11 +207,11 @@ namespace Geekbot.net _logger.Error(LogSource.Geekbot, "Failed to send delete message...", e); } } - + // // Reactions // - + public Task ReactionAdded(Cacheable cacheable, ISocketMessageChannel socketMessageChannel, SocketReaction reaction) { if (reaction.User.Value.IsBot) return Task.CompletedTask; @@ -229,4 +228,4 @@ namespace Geekbot.net return Task.CompletedTask; } } -} +} \ No newline at end of file diff --git a/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs b/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs index 9556b84..e9ff92c 100644 --- a/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs +++ b/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs @@ -52,16 +52,16 @@ namespace Geekbot.net.Lib.ErrorHandling if (!string.IsNullOrEmpty(resStackTrace)) { var maxLen = Math.Min(resStackTrace.Length, 1850); - context.Channel.SendMessageAsync($"{e.Message}\r\n```\r\n{resStackTrace.Substring(0, maxLen)}\r\n```"); + await context.Channel.SendMessageAsync($"{e.Message}\r\n```\r\n{resStackTrace.Substring(0, maxLen)}\r\n```"); } else { - context.Channel.SendMessageAsync(e.Message); + await context.Channel.SendMessageAsync(e.Message); } } else { - context.Channel.SendMessageAsync(errorString); + await context.Channel.SendMessageAsync(errorString); } } @@ -82,7 +82,7 @@ namespace Geekbot.net.Lib.ErrorHandling } catch (Exception ex) { - context.Channel.SendMessageAsync("Something went really really wrong here"); + await context.Channel.SendMessageAsync("Something went really really wrong here"); _logger.Error(LogSource.Geekbot, "Errorception", ex); } } diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 86a3507..3be93c5 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -101,8 +101,8 @@ namespace Geekbot.net { Console.Write("Your bot Token: "); var newToken = Console.ReadLine(); - _globalSettings.SetKey("DiscordToken", newToken); - _globalSettings.SetKey("Game", "Ping Pong"); + await _globalSettings.SetKey("DiscordToken", newToken); + await _globalSettings.SetKey("Game", "Ping Pong"); _token = newToken; } diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 96c170b..58fbfc6 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -7,7 +7,7 @@ - + From 28052a5ba9b5a88ea90028c055878bfde785e29b Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 28 Jul 2018 19:12:50 +0200 Subject: [PATCH 087/443] Update logsource in redis migration --- Geekbot.net/Database/RedisMigration.cs | 94 +++++++++++++------------- Geekbot.net/Lib/Logger/LogSource.cs | 1 + 2 files changed, 48 insertions(+), 47 deletions(-) diff --git a/Geekbot.net/Database/RedisMigration.cs b/Geekbot.net/Database/RedisMigration.cs index 3097a88..07b13b4 100644 --- a/Geekbot.net/Database/RedisMigration.cs +++ b/Geekbot.net/Database/RedisMigration.cs @@ -30,7 +30,7 @@ namespace Geekbot.net.Database public async Task Migrate() { - _logger.Information(LogSource.Geekbot, "Starting migration process"); + _logger.Information(LogSource.Migration, "Starting migration process"); var keys = _redis.GetAllKeys().Where(e => e.ToString().EndsWith("Messages")); var guilds = new List(); @@ -49,17 +49,17 @@ namespace Geekbot.net.Database } } - _logger.Information(LogSource.Geekbot, $"Found {guilds.Count} guilds in redis"); + _logger.Information(LogSource.Migration, $"Found {guilds.Count} guilds in redis"); foreach (var guild in guilds) { if (guild.MemberCount > 10000) { - _logger.Information(LogSource.Geekbot, $"Skipping {guild.Name} because server size is to big ({guild.MemberCount})"); + _logger.Information(LogSource.Migration, $"Skipping {guild.Name} because server size is to big ({guild.MemberCount})"); break; } - _logger.Information(LogSource.Geekbot, $"Start Migration for {guild.Name}"); + _logger.Information(LogSource.Migration, $"Start Migration for {guild.Name}"); #region Quotes /** * Quotes @@ -78,13 +78,13 @@ namespace Geekbot.net.Database } catch (Exception e) { - _logger.Error(LogSource.Geekbot, $"quote failed: {q}", e); + _logger.Error(LogSource.Migration, $"quote failed: {q}", e); } } } catch (Exception e) { - _logger.Error(LogSource.Geekbot, "quote migration failed", e); + _logger.Error(LogSource.Migration, "quote migration failed", e); } #endregion @@ -111,13 +111,13 @@ namespace Geekbot.net.Database } catch (Exception e) { - _logger.Error(LogSource.Geekbot, $"karma failed for: {q.Name}", e); + _logger.Error(LogSource.Migration, $"karma failed for: {q.Name}", e); } } } catch (Exception e) { - _logger.Error(LogSource.Geekbot, "karma migration failed", e); + _logger.Error(LogSource.Migration, "karma migration failed", e); } #endregion @@ -143,13 +143,13 @@ namespace Geekbot.net.Database } catch (Exception e) { - _logger.Error(LogSource.Geekbot, $"Rolls failed for: {q.Name}", e); + _logger.Error(LogSource.Migration, $"Rolls failed for: {q.Name}", e); } } } catch (Exception e) { - _logger.Error(LogSource.Geekbot, "rolls migration failed", e); + _logger.Error(LogSource.Migration, "rolls migration failed", e); } #endregion @@ -177,13 +177,13 @@ namespace Geekbot.net.Database } catch (Exception e) { - _logger.Error(LogSource.Geekbot, $"Slaps failed for: {q.Name}", e); + _logger.Error(LogSource.Migration, $"Slaps failed for: {q.Name}", e); } } } catch (Exception e) { - _logger.Error(LogSource.Geekbot, "Slaps migration failed", e); + _logger.Error(LogSource.Migration, "Slaps migration failed", e); } #endregion @@ -191,32 +191,32 @@ namespace Geekbot.net.Database /** * Messages */ -// try -// { -// var data = _redis.Db.HashGetAll($"{guild.Id}:Messages"); -// foreach (var q in data) -// { -// try -// { -// var user = new MessagesModel() -// { -// GuildId = guild.Id.AsLong(), -// UserId = ulong.Parse(q.Name).AsLong(), -// MessageCount= int.Parse(q.Value) -// }; -// _database.Messages.Add(user); -// await _database.SaveChangesAsync(); -// } -// catch (Exception e) -// { -// _logger.Error(LogSource.Geekbot, $"Messages failed for: {q.Name}", e); -// } -// } -// } -// catch (Exception e) -// { -// _logger.Error(LogSource.Geekbot, "Messages migration failed", e); -// } + /*try + { + var data = _redis.Db.HashGetAll($"{guild.Id}:Messages"); + foreach (var q in data) + { + try + { + var user = new MessagesModel() + { + GuildId = guild.Id.AsLong(), + UserId = ulong.Parse(q.Name).AsLong(), + MessageCount= int.Parse(q.Value) + }; + _database.Messages.Add(user); + await _database.SaveChangesAsync(); + } + catch (Exception e) + { + _logger.Error(LogSource.Migration, $"Messages failed for: {q.Name}", e); + } + } + } + catch (Exception e) + { + _logger.Error(LogSource.Migration, "Messages migration failed", e); + }*/ #endregion #region Ships @@ -245,13 +245,13 @@ namespace Geekbot.net.Database } catch (Exception e) { - _logger.Error(LogSource.Geekbot, $"Ships failed for: {q.Name}", e); + _logger.Error(LogSource.Migration, $"Ships failed for: {q.Name}", e); } } } catch (Exception e) { - _logger.Error(LogSource.Geekbot, "Ships migration failed", e); + _logger.Error(LogSource.Migration, "Ships migration failed", e); } #endregion @@ -298,13 +298,13 @@ namespace Geekbot.net.Database } catch (Exception e) { - _logger.Error(LogSource.Geekbot, $"Setting failed: {setting.Name} - {guild.Id}", e); + _logger.Error(LogSource.Migration, $"Setting failed: {setting.Name} - {guild.Id}", e); } } } catch (Exception e) { - _logger.Error(LogSource.Geekbot, "Settings migration failed", e); + _logger.Error(LogSource.Migration, "Settings migration failed", e); } #endregion @@ -343,13 +343,13 @@ namespace Geekbot.net.Database } catch (Exception e) { - _logger.Error(LogSource.Geekbot, $"User failed: {user.Username}", e); + _logger.Error(LogSource.Migration, $"User failed: {user.Username}", e); } } } catch (Exception e) { - _logger.Error(LogSource.Geekbot, "User migration failed", e); + _logger.Error(LogSource.Migration, "User migration failed", e); } #endregion @@ -369,14 +369,14 @@ namespace Geekbot.net.Database } catch (Exception e) { - _logger.Error(LogSource.Geekbot, $"Guild migration failed: {guild.Name}", e); + _logger.Error(LogSource.Migration, $"Guild migration failed: {guild.Name}", e); } #endregion - _logger.Information(LogSource.Geekbot, $"Finished Migration for {guild.Name}"); + _logger.Information(LogSource.Migration, $"Finished Migration for {guild.Name}"); await Task.Delay(1000); } - _logger.Information(LogSource.Geekbot, "Finished migration process"); + _logger.Information(LogSource.Migration, "Finished migration process"); } private QuoteModel CreateQuoteObject(ulong guild, QuoteObjectDto quote) diff --git a/Geekbot.net/Lib/Logger/LogSource.cs b/Geekbot.net/Lib/Logger/LogSource.cs index cccc930..42c1877 100644 --- a/Geekbot.net/Lib/Logger/LogSource.cs +++ b/Geekbot.net/Lib/Logger/LogSource.cs @@ -16,6 +16,7 @@ namespace Geekbot.net.Lib.Logger UserRepository, Command, Api, + Migration, Other } } \ No newline at end of file From 5b56c1822fcec4f3acdf0582e5033521b9ac4661 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 25 Aug 2018 21:30:33 +0200 Subject: [PATCH 088/443] Updates to language handler --- .../Lib/Localization/TranslationHandler.cs | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/Geekbot.net/Lib/Localization/TranslationHandler.cs b/Geekbot.net/Lib/Localization/TranslationHandler.cs index 88e518e..b1f6e53 100644 --- a/Geekbot.net/Lib/Localization/TranslationHandler.cs +++ b/Geekbot.net/Lib/Localization/TranslationHandler.cs @@ -18,8 +18,7 @@ namespace Geekbot.net.Lib.Localization private readonly IGeekbotLogger _logger; private readonly Dictionary _serverLanguages; private Dictionary>> _translations; - private List _supportedLanguages; - + public TranslationHandler(DatabaseContext database, IGeekbotLogger logger) { _database = database; @@ -45,30 +44,27 @@ namespace Geekbot.net.Lib.Localization if (!sortedPerLanguage.ContainsKey(lang.Key)) { var commandDict = new Dictionary>(); - var strDict = new Dictionary(); - strDict.Add(str.Key, lang.Value); + var strDict = new Dictionary {{str.Key, lang.Value}}; commandDict.Add(command.Key, strDict); sortedPerLanguage.Add(lang.Key, commandDict); + break; } if (!sortedPerLanguage[lang.Key].ContainsKey(command.Key)) { - var strDict = new Dictionary(); - strDict.Add(str.Key, lang.Value); + var strDict = new Dictionary {{str.Key, lang.Value}}; sortedPerLanguage[lang.Key].Add(command.Key, strDict); + break; } - if (!sortedPerLanguage[lang.Key][command.Key].ContainsKey(str.Key)) - { - sortedPerLanguage[lang.Key][command.Key].Add(str.Key, lang.Value); - } + sortedPerLanguage[lang.Key][command.Key].Add(str.Key, lang.Value); } } } _translations = sortedPerLanguage; - _supportedLanguages = new List(); + SupportedLanguages = new List(); foreach (var lang in sortedPerLanguage) { - _supportedLanguages.Add(lang.Key); + SupportedLanguages.Add(lang.Key); } } catch (Exception e) @@ -101,7 +97,7 @@ namespace Geekbot.net.Lib.Localization } catch (Exception e) { - _logger.Error(LogSource.Geekbot, "Could not get guild langage", e); + _logger.Error(LogSource.Geekbot, "Could not get guild language", e); return "EN"; } } @@ -149,7 +145,7 @@ namespace Geekbot.net.Lib.Localization { try { - if (!_supportedLanguages.Contains(language)) return false; + if (!SupportedLanguages.Contains(language)) return false; var guild = await GetGuild(guildId); guild.Language = language; _database.GuildSettings.Update(guild); @@ -163,7 +159,7 @@ namespace Geekbot.net.Lib.Localization } } - public List SupportedLanguages => _supportedLanguages; + public List SupportedLanguages { get; private set; } private async Task GetGuild(ulong guildId) { From 8a62f94e01e7530d8247fbe683843c48f19cb6ad Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 25 Aug 2018 22:40:02 +0200 Subject: [PATCH 089/443] Revert !stats us pull message count from redis --- Geekbot.net/Commands/User/Stats.cs | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/Geekbot.net/Commands/User/Stats.cs b/Geekbot.net/Commands/User/Stats.cs index 4d4d800..baacb36 100644 --- a/Geekbot.net/Commands/User/Stats.cs +++ b/Geekbot.net/Commands/User/Stats.cs @@ -39,18 +39,11 @@ namespace Geekbot.net.Commands.User var age = Math.Floor((DateTime.Now - createdAt).TotalDays); var joinedDayAgo = Math.Floor((DateTime.Now - joinedAt).TotalDays); - var messages = _database.Messages.FirstOrDefault(e => - e.GuildId.Equals(Context.Guild.Id.AsLong()) && - e.UserId.Equals(userInfo.Id.AsLong()))?.MessageCount; - - var guildMessages = _database.Messages - .Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong())) - .Select(e => e.MessageCount) - .Sum(); - + var messages = (int) _redis.Db.HashGet($"{Context.Guild.Id}:Messages", userInfo.Id.ToString()); + var guildMessages = (int) _redis.Db.HashGet($"{Context.Guild.Id}:Messages", 0.ToString()); var level = _levelCalc.GetLevel(messages); - var percent = Math.Round((double) (100 * messages) / guildMessages, digits: 2); + var percent = Math.Round((double) (100 * messages) / guildMessages, 2); var eb = new EmbedBuilder(); eb.WithAuthor(new EmbedAuthorBuilder() From e18a512a9b85feb10872de9da04e6bb8e795bfae Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 25 Aug 2018 23:26:46 +0200 Subject: [PATCH 090/443] Update Migration Script --- Geekbot.net/Database/RedisMigration.cs | 17 +++++++---------- Geekbot.net/Handlers.cs | 3 +-- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/Geekbot.net/Database/RedisMigration.cs b/Geekbot.net/Database/RedisMigration.cs index 07b13b4..92ee555 100644 --- a/Geekbot.net/Database/RedisMigration.cs +++ b/Geekbot.net/Database/RedisMigration.cs @@ -33,7 +33,7 @@ namespace Geekbot.net.Database _logger.Information(LogSource.Migration, "Starting migration process"); var keys = _redis.GetAllKeys().Where(e => e.ToString().EndsWith("Messages")); - var guilds = new List(); + var allGuilds = new List(); foreach (var key in keys) { @@ -41,7 +41,7 @@ namespace Geekbot.net.Database { var g = _client.GetGuild(ulong.Parse(key.ToString().Split(':').First())); Console.WriteLine(g.Name); - guilds.Add(g); + allGuilds.Add(g); } catch (Exception e) { @@ -49,16 +49,12 @@ namespace Geekbot.net.Database } } - _logger.Information(LogSource.Migration, $"Found {guilds.Count} guilds in redis"); + _logger.Information(LogSource.Migration, $"Found {allGuilds.Count} guilds in redis"); + var guilds = allGuilds.FindAll(e => e.MemberCount < 10000); + foreach (var guild in guilds) { - if (guild.MemberCount > 10000) - { - _logger.Information(LogSource.Migration, $"Skipping {guild.Name} because server size is to big ({guild.MemberCount})"); - break; - } - _logger.Information(LogSource.Migration, $"Start Migration for {guild.Name}"); #region Quotes /** @@ -315,7 +311,7 @@ namespace Geekbot.net.Database */ try { - var data = guild.Users; + var data = guild.Users.ToList().FindAll(e => !e.IsBot); foreach (var user in data) { try @@ -325,6 +321,7 @@ namespace Geekbot.net.Database await Task.Delay(100); if (user.Username == null) break; } + var namesSerialized = _redis.Db.HashGet($"User:{user.Id}", "UsedNames").ToString(); var names = namesSerialized != null ? Utf8Json.JsonSerializer.Deserialize(namesSerialized) diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index 9811432..64b3a15 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -183,8 +183,7 @@ namespace Geekbot.net try { var guildSocketData = ((IGuildChannel) channel).Guild; - var guild = _database.GuildSettings.FirstOrDefault(g => - g.GuildId.Equals(guildSocketData.Id.AsLong())); + var guild = _database.GuildSettings.FirstOrDefault(g => g.GuildId.Equals(guildSocketData.Id.AsLong())); if (guild?.ShowDelete ?? false) { var modChannelSocket = (ISocketMessageChannel) await _client.GetChannelAsync(guild.ModChannel.AsUlong()); From 954de7f8db700a697a9f1baeb0a2297091c82696 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 25 Aug 2018 23:27:44 +0200 Subject: [PATCH 091/443] Revert "Updates to language handler" This reverts commit 5b56c18 --- .../Lib/Localization/TranslationHandler.cs | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/Geekbot.net/Lib/Localization/TranslationHandler.cs b/Geekbot.net/Lib/Localization/TranslationHandler.cs index b1f6e53..88e518e 100644 --- a/Geekbot.net/Lib/Localization/TranslationHandler.cs +++ b/Geekbot.net/Lib/Localization/TranslationHandler.cs @@ -18,7 +18,8 @@ namespace Geekbot.net.Lib.Localization private readonly IGeekbotLogger _logger; private readonly Dictionary _serverLanguages; private Dictionary>> _translations; - + private List _supportedLanguages; + public TranslationHandler(DatabaseContext database, IGeekbotLogger logger) { _database = database; @@ -44,27 +45,30 @@ namespace Geekbot.net.Lib.Localization if (!sortedPerLanguage.ContainsKey(lang.Key)) { var commandDict = new Dictionary>(); - var strDict = new Dictionary {{str.Key, lang.Value}}; + var strDict = new Dictionary(); + strDict.Add(str.Key, lang.Value); commandDict.Add(command.Key, strDict); sortedPerLanguage.Add(lang.Key, commandDict); - break; } if (!sortedPerLanguage[lang.Key].ContainsKey(command.Key)) { - var strDict = new Dictionary {{str.Key, lang.Value}}; + var strDict = new Dictionary(); + strDict.Add(str.Key, lang.Value); sortedPerLanguage[lang.Key].Add(command.Key, strDict); - break; } - sortedPerLanguage[lang.Key][command.Key].Add(str.Key, lang.Value); + if (!sortedPerLanguage[lang.Key][command.Key].ContainsKey(str.Key)) + { + sortedPerLanguage[lang.Key][command.Key].Add(str.Key, lang.Value); + } } } } _translations = sortedPerLanguage; - SupportedLanguages = new List(); + _supportedLanguages = new List(); foreach (var lang in sortedPerLanguage) { - SupportedLanguages.Add(lang.Key); + _supportedLanguages.Add(lang.Key); } } catch (Exception e) @@ -97,7 +101,7 @@ namespace Geekbot.net.Lib.Localization } catch (Exception e) { - _logger.Error(LogSource.Geekbot, "Could not get guild language", e); + _logger.Error(LogSource.Geekbot, "Could not get guild langage", e); return "EN"; } } @@ -145,7 +149,7 @@ namespace Geekbot.net.Lib.Localization { try { - if (!SupportedLanguages.Contains(language)) return false; + if (!_supportedLanguages.Contains(language)) return false; var guild = await GetGuild(guildId); guild.Language = language; _database.GuildSettings.Update(guild); @@ -159,7 +163,7 @@ namespace Geekbot.net.Lib.Localization } } - public List SupportedLanguages { get; private set; } + public List SupportedLanguages => _supportedLanguages; private async Task GetGuild(ulong guildId) { From adb1614177254ba56862ba57967241d1606d0bac Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 26 Aug 2018 00:10:19 +0200 Subject: [PATCH 092/443] Update Deployment Script --- .gitlab-ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b40a5e5..243ea77 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -41,8 +41,8 @@ deploy: - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null - chmod 700 ~/.ssh script: - - rsync -rav -e "ssh -p 65432" ./Geekbot.net/Binaries/* www-data@31.220.42.224:$DEPPATH - - ssh -p 65432 www-data@31.220.42.224 "sudo systemctl restart geekbot.service" + - rsync -rav -e "ssh -p 65432" ./Geekbot.net/Binaries/* geekbot@172.104.156.220:$DEPPATH + - ssh -p 65432 geekbot@172.104.156.220 "sudo systemctl restart geekbot.service" mirror: stage: deploy From cbe88a1721e20515052c10bad925363d5a6aa053 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 26 Aug 2018 00:31:59 +0200 Subject: [PATCH 093/443] Fix !rank --- Geekbot.net/Commands/User/Ranking/Rank.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Geekbot.net/Commands/User/Ranking/Rank.cs b/Geekbot.net/Commands/User/Ranking/Rank.cs index a2e9891..50ccd51 100644 --- a/Geekbot.net/Commands/User/Ranking/Rank.cs +++ b/Geekbot.net/Commands/User/Ranking/Rank.cs @@ -84,10 +84,11 @@ namespace Geekbot.net.Commands.User.Ranking int guildMessages = 0; if (type == RankType.messages) { - guildMessages = _database.Messages - .Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong())) - .Select(e => e.MessageCount) - .Sum(); +// guildMessages = _database.Messages +// .Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong())) +// .Select(e => e.MessageCount) +// .Sum(); + guildMessages = (int) _redis.Db.HashGet($"{Context.Guild.Id}:Messages", 0.ToString()); } var highscoreUsers = new Dictionary(); @@ -158,6 +159,7 @@ namespace Geekbot.net.Commands.User.Ranking return _redis.Db .HashGetAll($"{Context.Guild.Id}:Messages").ToDictionary().Take(amount + 1) .Where(user => !user.Key.Equals(0)) + .OrderByDescending(s => s.Value) .ToDictionary(user => ulong.Parse(user.Key), user => int.Parse(user.Value)); } From 707be4abe20d8933dac4fbe326fb125648d99a23 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 26 Aug 2018 12:33:04 +0200 Subject: [PATCH 094/443] Trying to fix !rank again --- Geekbot.net/Commands/User/Ranking/Rank.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Geekbot.net/Commands/User/Ranking/Rank.cs b/Geekbot.net/Commands/User/Ranking/Rank.cs index 50ccd51..8e40f06 100644 --- a/Geekbot.net/Commands/User/Ranking/Rank.cs +++ b/Geekbot.net/Commands/User/Ranking/Rank.cs @@ -157,9 +157,10 @@ namespace Geekbot.net.Commands.User.Ranking // .Take(amount) // .ToDictionary(key => key.UserId.AsUlong(), key => key.MessageCount); return _redis.Db - .HashGetAll($"{Context.Guild.Id}:Messages").ToDictionary().Take(amount + 1) + .HashGetAll($"{Context.Guild.Id}:Messages") .Where(user => !user.Key.Equals(0)) .OrderByDescending(s => s.Value) + .Take(amount) .ToDictionary(user => ulong.Parse(user.Key), user => int.Parse(user.Value)); } From 9dda829cf9824180d096008018aa73c53a79fe79 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 26 Aug 2018 12:43:18 +0200 Subject: [PATCH 095/443] remove nuget caching during CI --- .gitlab-ci.yml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 243ea77..cd67b30 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -10,11 +10,6 @@ before_script: build: stage: build image: microsoft/dotnet:2.1.300-sdk-stretch - variables: - NUGET_PACKAGES: "${CI_PROJECT_DIR}/.nugetcache" - cache: - paths: - - .nugetcache artifacts: expire_in: 1h paths: From 00da991dae13abe693897fd652cf14278265e766 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 26 Aug 2018 17:39:01 +0200 Subject: [PATCH 096/443] Update !rank error message --- Geekbot.net/Commands/User/Ranking/Rank.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Geekbot.net/Commands/User/Ranking/Rank.cs b/Geekbot.net/Commands/User/Ranking/Rank.cs index 8e40f06..ce74de3 100644 --- a/Geekbot.net/Commands/User/Ranking/Rank.cs +++ b/Geekbot.net/Commands/User/Ranking/Rank.cs @@ -121,7 +121,7 @@ namespace Geekbot.net.Commands.User.Ranking } } - if (failedToRetrieveUser) replyBuilder.AppendLine(":warning: Couldn't get all userdata\n"); + if (failedToRetrieveUser) replyBuilder.AppendLine(":warning: I couldn't find all usernames. Maybe they left the server?\n"); replyBuilder.AppendLine($":bar_chart: **{type.ToString().CapitalizeFirst()} Highscore for {Context.Guild.Name}**"); var highscorePlace = 1; foreach (var user in highscoreUsers) From 74619a4434051eb319fc76e41dc8f7e082b14007 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 26 Aug 2018 17:54:26 +0200 Subject: [PATCH 097/443] Solve casting issue --- Geekbot.net/Handlers.cs | 2 +- Geekbot.net/Lib/Logger/SimpleConextConverter.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index 64b3a15..0e57d55 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -99,7 +99,7 @@ namespace Geekbot.net if (message == null) return; if (message.Channel.Name.StartsWith('@')) { - _logger.Information(LogSource.Message, $"[DM-Channel] {message.Content}", SimpleConextConverter.ConvertSocketMessage(message)); + _logger.Information(LogSource.Message, $"[DM-Channel] {message.Content}", SimpleConextConverter.ConvertSocketMessage(message, true)); return; } diff --git a/Geekbot.net/Lib/Logger/SimpleConextConverter.cs b/Geekbot.net/Lib/Logger/SimpleConextConverter.cs index 0845cc1..5659896 100644 --- a/Geekbot.net/Lib/Logger/SimpleConextConverter.cs +++ b/Geekbot.net/Lib/Logger/SimpleConextConverter.cs @@ -35,9 +35,9 @@ namespace Geekbot.net.Lib.Logger } }; } - public static MessageDto ConvertSocketMessage(SocketMessage message) + public static MessageDto ConvertSocketMessage(SocketMessage message, bool isPrivate = false) { - var channel = (SocketGuildChannel) message.Channel; + SocketGuildChannel channel = isPrivate ? null : (SocketGuildChannel) message.Channel; return new MessageDto { Message = new MessageDto.MessageContent From d1e9992a8c73e497428226f4c7636969b00a57f2 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 26 Aug 2018 18:05:50 +0200 Subject: [PATCH 098/443] Add default values to guildsettings model --- .../Database/Models/GuildSettingsModel.cs | 28 +++++++++---------- Geekbot.net/Handlers.cs | 2 +- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/Geekbot.net/Database/Models/GuildSettingsModel.cs b/Geekbot.net/Database/Models/GuildSettingsModel.cs index a71aac1..6bc98b1 100644 --- a/Geekbot.net/Database/Models/GuildSettingsModel.cs +++ b/Geekbot.net/Database/Models/GuildSettingsModel.cs @@ -9,21 +9,21 @@ namespace Geekbot.net.Database.Models [Required] public long GuildId { get; set; } - - public bool Ping { get; set; } - - public bool Hui { get; set; } - - public long ModChannel { get; set; } + + public bool Ping { get; set; } = false; + + public bool Hui { get; set; } = false; + + public long ModChannel { get; set; } = 0; public string WelcomeMessage { get; set; } - - public bool ShowDelete { get; set; } - - public bool ShowLeave { get; set; } - - public string WikiLang { get; set; } - - public string Language { get; set; } + + public bool ShowDelete { get; set; } = false; + + public bool ShowLeave { get; set; } = false; + + public string WikiLang { get; set; } = "en"; + + public string Language { get; set; } = "EN"; } } \ No newline at end of file diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index 0e57d55..65a04f4 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -184,7 +184,7 @@ namespace Geekbot.net { var guildSocketData = ((IGuildChannel) channel).Guild; var guild = _database.GuildSettings.FirstOrDefault(g => g.GuildId.Equals(guildSocketData.Id.AsLong())); - if (guild?.ShowDelete ?? false) + if ((guild?.ShowDelete ?? false) && guild?.ModChannel != 0) { var modChannelSocket = (ISocketMessageChannel) await _client.GetChannelAsync(guild.ModChannel.AsUlong()); var sb = new StringBuilder(); From b0758eb119d0356da7b8e43ba806d02374201d01 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 29 Aug 2018 21:16:01 +0200 Subject: [PATCH 099/443] Create multiple database contexts instead of one --- Geekbot.net/Commands/User/Ranking/Rank.cs | 4 +-- Geekbot.net/Commands/User/Stats.cs | 7 +++++ Geekbot.net/Database/DatabaseInitializer.cs | 6 ++-- Geekbot.net/Handlers.cs | 32 ++++++++++++++------- Geekbot.net/Program.cs | 22 ++++++++------ 5 files changed, 47 insertions(+), 24 deletions(-) diff --git a/Geekbot.net/Commands/User/Ranking/Rank.cs b/Geekbot.net/Commands/User/Ranking/Rank.cs index ce74de3..1bb9f95 100644 --- a/Geekbot.net/Commands/User/Ranking/Rank.cs +++ b/Geekbot.net/Commands/User/Ranking/Rank.cs @@ -158,10 +158,10 @@ namespace Geekbot.net.Commands.User.Ranking // .ToDictionary(key => key.UserId.AsUlong(), key => key.MessageCount); return _redis.Db .HashGetAll($"{Context.Guild.Id}:Messages") - .Where(user => !user.Key.Equals(0)) + .Where(user => !user.Name.Equals(0)) .OrderByDescending(s => s.Value) .Take(amount) - .ToDictionary(user => ulong.Parse(user.Key), user => int.Parse(user.Value)); + .ToDictionary(user => ulong.Parse(user.Name), user => int.Parse(user.Value)); } private Dictionary GetKarmaList(int amount) diff --git a/Geekbot.net/Commands/User/Stats.cs b/Geekbot.net/Commands/User/Stats.cs index baacb36..4ec2148 100644 --- a/Geekbot.net/Commands/User/Stats.cs +++ b/Geekbot.net/Commands/User/Stats.cs @@ -39,6 +39,13 @@ namespace Geekbot.net.Commands.User var age = Math.Floor((DateTime.Now - createdAt).TotalDays); var joinedDayAgo = Math.Floor((DateTime.Now - joinedAt).TotalDays); +// var messages = _database.Messages +// .First(e => e.GuildId.Equals(Context.Guild.Id.AsLong()) && e.UserId.Equals(Context.User.Id.AsLong())) +// .MessageCount; +// var guildMessages = _database.Messages +// .Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong())) +// .Select(e => e.MessageCount) +// .Sum(); var messages = (int) _redis.Db.HashGet($"{Context.Guild.Id}:Messages", userInfo.Id.ToString()); var guildMessages = (int) _redis.Db.HashGet($"{Context.Guild.Id}:Messages", 0.ToString()); var level = _levelCalc.GetLevel(messages); diff --git a/Geekbot.net/Database/DatabaseInitializer.cs b/Geekbot.net/Database/DatabaseInitializer.cs index 7ae6cc7..7eff2c6 100644 --- a/Geekbot.net/Database/DatabaseInitializer.cs +++ b/Geekbot.net/Database/DatabaseInitializer.cs @@ -16,9 +16,10 @@ namespace Geekbot.net.Database { _runParameters = runParameters; _logger = logger; + NpgsqlLogManager.Provider = new NpgsqlLoggingProviderAdapter(_logger); } - public DatabaseContext Initzialize() + public DatabaseContext Initialize() { DatabaseContext database = null; try @@ -29,7 +30,6 @@ namespace Geekbot.net.Database } else { - NpgsqlLogManager.Provider = new NpgsqlLoggingProviderAdapter(_logger); database = new SqlDatabase(new SqlConnectionString { Host = _runParameters.DbHost, @@ -39,8 +39,6 @@ namespace Geekbot.net.Database Password = _runParameters.DbPassword }); } - database.Database.EnsureCreated(); - if(!_runParameters.InMemory) database.Database.Migrate(); } catch (Exception e) { diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index 65a04f4..d2e065d 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -6,6 +6,7 @@ using Discord; using Discord.Commands; using Discord.WebSocket; using Geekbot.net.Database; +using Geekbot.net.Database.Models; using Geekbot.net.Lib.AlmostRedis; using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Logger; @@ -25,12 +26,14 @@ namespace Geekbot.net private readonly CommandService _commands; private readonly IUserRepository _userRepository; private readonly IReactionListener _reactionListener; + private readonly DatabaseContext _messageCounterDatabaseContext; - public Handlers(DatabaseContext database, IDiscordClient client, IGeekbotLogger logger, IAlmostRedis redis, + public Handlers(DatabaseInitializer databaseInitializer, IDiscordClient client, IGeekbotLogger logger, IAlmostRedis redis, IServiceProvider servicesProvider, CommandService commands, IUserRepository userRepository, IReactionListener reactionListener) { - _database = database; + _database = databaseInitializer.Initialize(); + _messageCounterDatabaseContext = databaseInitializer.Initialize(); _client = client; _logger = logger; _redis = redis; @@ -52,9 +55,6 @@ namespace Geekbot.net if (message.Author.IsBot) return Task.CompletedTask; var argPos = 0; - // ToDo: remove -// if (!message.Author.Id.Equals(93061333972455424)) return Task.CompletedTask; - var lowCaseMsg = message.ToString().ToLower(); if (lowCaseMsg.StartsWith("hui")) { @@ -105,11 +105,23 @@ namespace Geekbot.net var channel = (SocketGuildChannel) message.Channel; -// await _database.Database.ExecuteSqlCommandAsync("UPDATE \"Messages\" " + -// $"SET \"MessageCount\" = \"MessageCount\" + {1} " + -// $"WHERE \"GuildId\" = '{channel.Guild.Id.AsLong()}' " + -// $"AND \"UserId\" = '{message.Author.Id.AsLong()}'"); -// +// var rowId = await _messageCounterDatabaseContext.Database.ExecuteSqlCommandAsync( +// "UPDATE \"Messages\" SET \"MessageCount\" = \"MessageCount\" + 1 WHERE \"GuildId\" = {0} AND \"UserId\" = {1}", +// channel.Guild.Id.AsLong(), +// message.Author.Id.AsLong() +// ); +// +// if (rowId == 0) +// { +// _messageCounterDatabaseContext.Messages.Add(new MessagesModel +// { +// UserId = message.Author.Id.AsLong(), +// GuildId = channel.Guild.Id.AsLong(), +// MessageCount = 1 +// }); +// _messageCounterDatabaseContext.SaveChanges(); +// } + await _redis.Db.HashIncrementAsync($"{channel.Guild.Id}:Messages", message.Author.Id.ToString()); await _redis.Db.HashIncrementAsync($"{channel.Guild.Id}:Messages", 0.ToString()); diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 3be93c5..875821f 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -7,6 +7,7 @@ using Discord; using Discord.Commands; using Discord.WebSocket; using Geekbot.net.Database; +using Geekbot.net.Database.LoggingAdapter; using Geekbot.net.Lib; using Geekbot.net.Lib.AlmostRedis; using Geekbot.net.Lib.Audio; @@ -20,6 +21,7 @@ using Geekbot.net.Lib.Logger; using Geekbot.net.Lib.Media; using Geekbot.net.Lib.ReactionListener; using Geekbot.net.Lib.UserRepository; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using WikipediaApi; @@ -29,7 +31,7 @@ namespace Geekbot.net { private DiscordSocketClient _client; private CommandService _commands; - private DatabaseContext _database; + private DatabaseInitializer _databaseInitializer; private IGlobalSettings _globalSettings; private IServiceCollection _services; private IServiceProvider _servicesProvider; @@ -82,8 +84,12 @@ namespace Geekbot.net _client.Log += discordLogger.Log; _commands = new CommandService(); - _database = new DatabaseInitializer(runParameters, logger).Initzialize(); - _globalSettings = new GlobalSettings(_database); + _databaseInitializer = new DatabaseInitializer(runParameters, logger); + var database = _databaseInitializer.Initialize(); + database.Database.EnsureCreated(); + if(!_runParameters.InMemory) database.Database.Migrate(); + + _globalSettings = new GlobalSettings(database); try { @@ -108,7 +114,7 @@ namespace Geekbot.net _services = new ServiceCollection(); - _userRepository = new UserRepository(_database, logger); + _userRepository = new UserRepository(_databaseInitializer.Initialize(), logger); var fortunes = new FortunesProvider(logger); var mediaProvider = new MediaProvider(logger); var malClient = new MalClient(_globalSettings, logger); @@ -129,8 +135,8 @@ namespace Geekbot.net _services.AddSingleton(mtgManaConverter); _services.AddSingleton(wikipediaClient); _services.AddSingleton(audioUtils); - _services.AddSingleton(_database); _services.AddSingleton(_globalSettings); + _services.AddTransient((e) => _databaseInitializer.Initialize()); logger.Information(LogSource.Geekbot, "Connecting to Discord"); @@ -152,7 +158,7 @@ namespace Geekbot.net _logger.Information(LogSource.Geekbot, $"Now Connected as {_client.CurrentUser.Username} to {_client.Guilds.Count} Servers"); _logger.Information(LogSource.Geekbot, "Registering Stuff"); - var translationHandler = new TranslationHandler(_database, _logger); + var translationHandler = new TranslationHandler(_databaseInitializer.Initialize(), _logger); var errorHandler = new ErrorHandler(_logger, translationHandler, _runParameters.ExposeErrors); var reactionListener = new ReactionListener(_redis.Db); await _commands.AddModulesAsync(Assembly.GetEntryAssembly()); @@ -163,7 +169,7 @@ namespace Geekbot.net _services.AddSingleton(reactionListener); _servicesProvider = _services.BuildServiceProvider(); - var handlers = new Handlers(_database, _client, _logger, _redis, _servicesProvider, _commands, _userRepository, reactionListener); + var handlers = new Handlers(_databaseInitializer, _client, _logger, _redis, _servicesProvider, _commands, _userRepository, reactionListener); _client.MessageReceived += handlers.RunCommand; _client.MessageReceived += handlers.UpdateStats; @@ -198,7 +204,7 @@ namespace Geekbot.net private Task StartWebApi() { _logger.Information(LogSource.Api, "Starting Webserver"); - WebApi.WebApiStartup.StartWebApi(_logger, _runParameters, _commands, _database); + WebApi.WebApiStartup.StartWebApi(_logger, _runParameters, _commands, _databaseInitializer.Initialize()); return Task.CompletedTask; } } From 5db3187f0d9db435188fb9e6c6eadf4bbd986e2c Mon Sep 17 00:00:00 2001 From: Daan Date: Tue, 28 Aug 2018 20:32:24 +0000 Subject: [PATCH 100/443] Change deployment IP --- .gitlab-ci.yml | 96 +++++++++++++++++++++++++------------------------- 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index cd67b30..93afe34 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,49 +1,49 @@ -stages: - - build - - deploy - -before_script: - - set -e - - set -u - - set -o pipefail - -build: - stage: build - image: microsoft/dotnet:2.1.300-sdk-stretch - artifacts: - expire_in: 1h - paths: - - Geekbot.net/Binaries/ - script: - - dotnet restore - - dotnet test Tests - - dotnet publish --version-suffix ${CI_COMMIT_SHA:0:8} --configuration Release -o Binaries ./ - -deploy: - stage: deploy - image: instrumentisto/rsync-ssh - only: - - master - dependencies: - - build - environment: - name: Production - url: https://discordapp.com/oauth2/authorize?client_id=171249478546882561&scope=bot&permissions=1416834054 - before_script: - - eval $(ssh-agent -s) - - mkdir -p ~/.ssh - - '[[ -f /.dockerenv ]] && echo -e "Host *\n StrictHostKeyChecking no" > ~/.ssh/config' - - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null - - chmod 700 ~/.ssh - script: - - rsync -rav -e "ssh -p 65432" ./Geekbot.net/Binaries/* geekbot@172.104.156.220:$DEPPATH - - ssh -p 65432 geekbot@172.104.156.220 "sudo systemctl restart geekbot.service" - -mirror: - stage: deploy - image: bravissimolabs/alpine-git:latest - only: - - master - script: - - git push https://runebaas:$TOKEN@github.com/pizzaandcoffee/Geekbot.net.git origin/master:master -f +stages: + - build + - deploy + +before_script: + - set -e + - set -u + - set -o pipefail + +build: + stage: build + image: microsoft/dotnet:2.1.300-sdk-stretch + artifacts: + expire_in: 1h + paths: + - Geekbot.net/Binaries/ + script: + - dotnet restore + - dotnet test Tests + - dotnet publish --version-suffix ${CI_COMMIT_SHA:0:8} --configuration Release -o Binaries ./ + +deploy: + stage: deploy + image: instrumentisto/rsync-ssh + only: + - master + dependencies: + - build + environment: + name: Production + url: https://discordapp.com/oauth2/authorize?client_id=171249478546882561&scope=bot&permissions=1416834054 + before_script: + - eval $(ssh-agent -s) + - mkdir -p ~/.ssh + - '[[ -f /.dockerenv ]] && echo -e "Host *\n StrictHostKeyChecking no" > ~/.ssh/config' + - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null + - chmod 700 ~/.ssh + script: + - rsync -rav -e "ssh -p 65432" ./Geekbot.net/Binaries/* geekbot@192.168.156.175:$DEPPATH + - ssh -p 65432 geekbot@192.168.156.175 "sudo systemctl restart geekbot.service" + +mirror: + stage: deploy + image: bravissimolabs/alpine-git:latest + only: + - master + script: + - git push https://runebaas:$TOKEN@github.com/pizzaandcoffee/Geekbot.net.git origin/master:master -f \ No newline at end of file From 0e06e8544751bf96dbbb65b7b8c0ffa45db24b67 Mon Sep 17 00:00:00 2001 From: Date: Wed, 29 Aug 2018 22:04:27 +0200 Subject: [PATCH 101/443] Change the discord.net release branch --- Geekbot.net/Geekbot.net.csproj | 2 +- Geekbot.net/Lib/Polyfills/UserPolyfillDto.cs | 5 +++++ Geekbot.net/Program.cs | 5 ++--- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 68a1870..46c1eff 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -21,7 +21,7 @@ - 2.0.0-beta + 2.0.0-beta2-00984 diff --git a/Geekbot.net/Lib/Polyfills/UserPolyfillDto.cs b/Geekbot.net/Lib/Polyfills/UserPolyfillDto.cs index 568bdd7..6d81581 100644 --- a/Geekbot.net/Lib/Polyfills/UserPolyfillDto.cs +++ b/Geekbot.net/Lib/Polyfills/UserPolyfillDto.cs @@ -23,6 +23,11 @@ namespace Geekbot.net.Lib.Polyfills return "https://discordapp.com/assets/6debd47ed13483642cf09e832ed0bc1b.png"; } + public string GetDefaultAvatarUrl() + { + throw new NotImplementedException(); + } + public Task GetOrCreateDMChannelAsync(RequestOptions options = null) { throw new NotImplementedException(); diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 875821f..e2f81b2 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -161,14 +161,13 @@ namespace Geekbot.net var translationHandler = new TranslationHandler(_databaseInitializer.Initialize(), _logger); var errorHandler = new ErrorHandler(_logger, translationHandler, _runParameters.ExposeErrors); var reactionListener = new ReactionListener(_redis.Db); - await _commands.AddModulesAsync(Assembly.GetEntryAssembly()); - _services.AddSingleton(_commands); _services.AddSingleton(errorHandler); _services.AddSingleton(translationHandler); _services.AddSingleton(_client); _services.AddSingleton(reactionListener); _servicesProvider = _services.BuildServiceProvider(); - + await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _servicesProvider); + var handlers = new Handlers(_databaseInitializer, _client, _logger, _redis, _servicesProvider, _commands, _userRepository, reactionListener); _client.MessageReceived += handlers.RunCommand; From 0d0f0550a5937d29402419c001e3adf0ddded170 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 29 Aug 2018 22:20:04 +0200 Subject: [PATCH 102/443] Add source to dotnet restore in ci file --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 93afe34..c5b5573 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -15,7 +15,7 @@ build: paths: - Geekbot.net/Binaries/ script: - - dotnet restore + - dotnet restore -s https://api.nuget.org/v3/index.json -s https://www.myget.org/F/discord-net/api/v3/index.json - dotnet test Tests - dotnet publish --version-suffix ${CI_COMMIT_SHA:0:8} --configuration Release -o Binaries ./ From 449e1e0adceff9fe72379c88634e65adb78bd537 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 29 Aug 2018 22:49:13 +0200 Subject: [PATCH 103/443] Update images in ci file --- .gitlab-ci.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index c5b5573..1a55164 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -9,7 +9,7 @@ before_script: build: stage: build - image: microsoft/dotnet:2.1.300-sdk-stretch + image: microsoft/dotnet:2.1.401-sdk-stretch artifacts: expire_in: 1h paths: @@ -21,7 +21,7 @@ build: deploy: stage: deploy - image: instrumentisto/rsync-ssh + image: runebaas/rsync-ssh-git only: - master dependencies: @@ -41,7 +41,7 @@ deploy: mirror: stage: deploy - image: bravissimolabs/alpine-git:latest + image: runebaas/rsync-ssh-git only: - master script: From 6a163366ea50b1fcf76024a035e57096c4855c60 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 29 Aug 2018 23:01:10 +0200 Subject: [PATCH 104/443] Start counting messages in postgres --- Geekbot.net/Handlers.cs | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index d2e065d..6dd53d0 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -105,23 +105,24 @@ namespace Geekbot.net var channel = (SocketGuildChannel) message.Channel; -// var rowId = await _messageCounterDatabaseContext.Database.ExecuteSqlCommandAsync( -// "UPDATE \"Messages\" SET \"MessageCount\" = \"MessageCount\" + 1 WHERE \"GuildId\" = {0} AND \"UserId\" = {1}", -// channel.Guild.Id.AsLong(), -// message.Author.Id.AsLong() -// ); -// -// if (rowId == 0) -// { -// _messageCounterDatabaseContext.Messages.Add(new MessagesModel -// { -// UserId = message.Author.Id.AsLong(), -// GuildId = channel.Guild.Id.AsLong(), -// MessageCount = 1 -// }); -// _messageCounterDatabaseContext.SaveChanges(); -// } - + // just testing, redis will remain the source of truth for now + var rowId = await _messageCounterDatabaseContext.Database.ExecuteSqlCommandAsync( + "UPDATE \"Messages\" SET \"MessageCount\" = \"MessageCount\" + 1 WHERE \"GuildId\" = {0} AND \"UserId\" = {1}", + channel.Guild.Id.AsLong(), + message.Author.Id.AsLong() + ); + + if (rowId == 0) + { + _messageCounterDatabaseContext.Messages.Add(new MessagesModel + { + UserId = message.Author.Id.AsLong(), + GuildId = channel.Guild.Id.AsLong(), + MessageCount = 1 + }); + _messageCounterDatabaseContext.SaveChanges(); + } + await _redis.Db.HashIncrementAsync($"{channel.Guild.Id}:Messages", message.Author.Id.ToString()); await _redis.Db.HashIncrementAsync($"{channel.Guild.Id}:Messages", 0.ToString()); From 5a4e710ef9e443091ab95a6e43b912f69cc3d44f Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 2 Sep 2018 21:58:16 +0200 Subject: [PATCH 105/443] NRE fixes in simpleMessageConverter, !urban and errorHandler --- .../Integrations/UbranDictionary/UrbanDictionary.cs | 2 +- Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs | 2 +- Geekbot.net/Lib/Logger/SimpleConextConverter.cs | 12 ++++++------ 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs b/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs index 25ab732..4f29f10 100644 --- a/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs +++ b/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs @@ -52,7 +52,7 @@ namespace Geekbot.net.Commands.Integrations.UbranDictionary if (!string.IsNullOrEmpty(definition.Example)) eb.AddField("Example", definition.Example ?? "(no example given...)"); if (!string.IsNullOrEmpty(definition.ThumbsUp)) eb.AddInlineField("Upvotes", definition.ThumbsUp); if (!string.IsNullOrEmpty(definition.ThumbsDown)) eb.AddInlineField("Downvotes", definition.ThumbsDown); - if (definitions.Tags.Length > 0) eb.AddField("Tags", string.Join(", ", definitions.Tags)); + if (definitions.Tags?.Length > 0) eb.AddField("Tags", string.Join(", ", definitions.Tags)); await ReplyAsync("", false, eb.Build()); } diff --git a/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs b/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs index e9ff92c..8ebee2e 100644 --- a/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs +++ b/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs @@ -39,7 +39,7 @@ namespace Geekbot.net.Lib.ErrorHandling { try { - var errorString = errorMessage == "def" ? await _translation.GetString(context.Guild.Id, "errorHandler", "SomethingWentWrong") : errorMessage; + var errorString = errorMessage == "def" ? await _translation.GetString(context.Guild?.Id ?? 0, "errorHandler", "SomethingWentWrong") : errorMessage; var errorObj = SimpleConextConverter.ConvertContext(context); if (e.Message.Contains("50007")) return; if (e.Message.Contains("50013")) return; diff --git a/Geekbot.net/Lib/Logger/SimpleConextConverter.cs b/Geekbot.net/Lib/Logger/SimpleConextConverter.cs index 5659896..8b8b0ed 100644 --- a/Geekbot.net/Lib/Logger/SimpleConextConverter.cs +++ b/Geekbot.net/Lib/Logger/SimpleConextConverter.cs @@ -25,13 +25,13 @@ namespace Geekbot.net.Lib.Logger }, Guild = new MessageDto.IdAndName { - Id = context.Guild.Id.ToString(), - Name = context.Guild.Name + Id = context.Guild?.Id.ToString(), + Name = context.Guild?.Name }, Channel = new MessageDto.IdAndName { - Id = context.Channel.Id.ToString(), - Name = context.Channel.Name + Id = context.Channel?.Id.ToString() ?? context.User.Id.ToString(), + Name = context.Channel?.Name ?? "DM-Channel" } }; } @@ -61,8 +61,8 @@ namespace Geekbot.net.Lib.Logger }, Channel = new MessageDto.IdAndName { - Id = channel?.Id.ToString(), - Name = channel?.Name + Id = channel?.Id.ToString() ?? message.Author.Id.ToString(), + Name = channel?.Name ?? "DM-Channel" } }; } From 0d8c82f8aa501ec7df2552b2e0bcea74614967a4 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 2 Sep 2018 22:42:13 +0200 Subject: [PATCH 106/443] Add Caching to GlobalSettings --- .../Lib/GlobalSettings/GlobalSettings.cs | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/Geekbot.net/Lib/GlobalSettings/GlobalSettings.cs b/Geekbot.net/Lib/GlobalSettings/GlobalSettings.cs index 8a1d0e4..a53bc98 100644 --- a/Geekbot.net/Lib/GlobalSettings/GlobalSettings.cs +++ b/Geekbot.net/Lib/GlobalSettings/GlobalSettings.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using Geekbot.net.Database; using Geekbot.net.Database.Models; @@ -8,10 +9,12 @@ namespace Geekbot.net.Lib.GlobalSettings public class GlobalSettings : IGlobalSettings { private readonly DatabaseContext _database; + private Dictionary _cache; public GlobalSettings(DatabaseContext database) { _database = database; + _cache = new Dictionary(); } public async Task SetKey(string keyName, string value) @@ -29,9 +32,9 @@ namespace Geekbot.net.Lib.GlobalSettings await _database.SaveChangesAsync(); return true; } - key.Value = value; _database.Globals.Update(key); + _cache[keyName] = value; await _database.SaveChangesAsync(); return true; } @@ -43,8 +46,17 @@ namespace Geekbot.net.Lib.GlobalSettings public string GetKey(string keyName) { - var key = _database.Globals.FirstOrDefault(k => k.Name.Equals(keyName)); - return key?.Value ?? string.Empty; + var keyValue = ""; + if (string.IsNullOrEmpty(_cache[keyName])) + { + keyValue = _database.Globals.FirstOrDefault(k => k.Name.Equals(keyName))?.Value ?? string.Empty; + _cache[keyName] = keyValue; + } + else + { + keyValue = _cache[keyName]; + } + return keyValue ; } public GlobalsModel GetKeyFull(string keyName) From d4c0899ba9be9e0945f8c8de228877e26551b193 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 2 Sep 2018 23:59:26 +0200 Subject: [PATCH 107/443] Fix dict lookup in globalSettings.cs --- Geekbot.net/Lib/GlobalSettings/GlobalSettings.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Geekbot.net/Lib/GlobalSettings/GlobalSettings.cs b/Geekbot.net/Lib/GlobalSettings/GlobalSettings.cs index a53bc98..685a1e5 100644 --- a/Geekbot.net/Lib/GlobalSettings/GlobalSettings.cs +++ b/Geekbot.net/Lib/GlobalSettings/GlobalSettings.cs @@ -9,7 +9,7 @@ namespace Geekbot.net.Lib.GlobalSettings public class GlobalSettings : IGlobalSettings { private readonly DatabaseContext _database; - private Dictionary _cache; + private readonly Dictionary _cache; public GlobalSettings(DatabaseContext database) { @@ -47,7 +47,7 @@ namespace Geekbot.net.Lib.GlobalSettings public string GetKey(string keyName) { var keyValue = ""; - if (string.IsNullOrEmpty(_cache[keyName])) + if (string.IsNullOrEmpty(_cache.GetValueOrDefault(keyName))) { keyValue = _database.Globals.FirstOrDefault(k => k.Name.Equals(keyName))?.Value ?? string.Empty; _cache[keyName] = keyValue; From 6d3fc46e3450cb95ea81aa2ccb649b26b660715b Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 2 Sep 2018 23:59:41 +0200 Subject: [PATCH 108/443] Add Callback Endpoint to API --- Geekbot.net/Program.cs | 2 +- .../Callback/CallbackController.cs | 55 +++++++++++++++++++ .../Callback/CallbackTokenResponseDto.cs | 11 ++++ Geekbot.net/WebApi/WebApiStartup.cs | 6 +- 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 Geekbot.net/WebApi/Controllers/Callback/CallbackController.cs create mode 100644 Geekbot.net/WebApi/Controllers/Callback/CallbackTokenResponseDto.cs diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index e2f81b2..f2a6291 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -203,7 +203,7 @@ namespace Geekbot.net private Task StartWebApi() { _logger.Information(LogSource.Api, "Starting Webserver"); - WebApi.WebApiStartup.StartWebApi(_logger, _runParameters, _commands, _databaseInitializer.Initialize()); + WebApi.WebApiStartup.StartWebApi(_logger, _runParameters, _commands, _databaseInitializer.Initialize(), _client, _globalSettings); return Task.CompletedTask; } } diff --git a/Geekbot.net/WebApi/Controllers/Callback/CallbackController.cs b/Geekbot.net/WebApi/Controllers/Callback/CallbackController.cs new file mode 100644 index 0000000..58bdb83 --- /dev/null +++ b/Geekbot.net/WebApi/Controllers/Callback/CallbackController.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Threading.Tasks; +using Discord.WebSocket; +using Geekbot.net.Lib.GlobalSettings; +using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json; + +namespace Geekbot.net.WebApi.Controllers.Callback +{ + public class CallbackController : Controller + { + private readonly DiscordSocketClient _client; + private readonly IGlobalSettings _globalSettings; + + public CallbackController(DiscordSocketClient client, IGlobalSettings globalSettings) + { + _client = client; + _globalSettings = globalSettings; + } + + [Route("/callback")] + public async Task DoCallback([FromQuery] string code) + { + var token = ""; + using (var client = new HttpClient()) + { + client.BaseAddress = new Uri("https://discordapp.com"); + var appInfo = await _client.GetApplicationInfoAsync(); + var accessToken = _globalSettings.GetKey("OAuthToken"); + var callbackUrl = _globalSettings.GetKey("OAuthCallbackUrl"); + + var form = new Dictionary(); + form.Add("client_id", appInfo.Id.ToString()); + form.Add("client_secret", accessToken); + form.Add("grant_type", "authorization_code"); + form.Add("code", code); + form.Add("scope", "identify email guilds"); + form.Add("redirect_uri", callbackUrl); + + client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded")); + var result = await client.PostAsync("/api/oauth2/token", new FormUrlEncodedContent(form)); + result.EnsureSuccessStatusCode(); + + var stringResponse = await result.Content.ReadAsStringAsync(); + var responseData = JsonConvert.DeserializeObject(stringResponse); + token = responseData.access_token; + } + + return new RedirectResult($"https://geekbot.pizzaandcoffee.rocks/login?token={token}", false); + } + } +} \ No newline at end of file diff --git a/Geekbot.net/WebApi/Controllers/Callback/CallbackTokenResponseDto.cs b/Geekbot.net/WebApi/Controllers/Callback/CallbackTokenResponseDto.cs new file mode 100644 index 0000000..3c81592 --- /dev/null +++ b/Geekbot.net/WebApi/Controllers/Callback/CallbackTokenResponseDto.cs @@ -0,0 +1,11 @@ +namespace Geekbot.net.WebApi.Controllers.Callback +{ + public class CallbackTokenResponseDto + { + public string access_token { get; set; } + public string token_type { get; set; } + public int expires_in { get; set; } + public string refresh_token { get; set; } + public string scope { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/WebApi/WebApiStartup.cs b/Geekbot.net/WebApi/WebApiStartup.cs index e91ff9f..e93a8d5 100644 --- a/Geekbot.net/WebApi/WebApiStartup.cs +++ b/Geekbot.net/WebApi/WebApiStartup.cs @@ -1,8 +1,10 @@ using System.Net; using System.Reflection; using Discord.Commands; +using Discord.WebSocket; using Geekbot.net.Database; using Geekbot.net.Lib; +using Geekbot.net.Lib.GlobalSettings; using Geekbot.net.Lib.Logger; using Geekbot.net.WebApi.Logging; using Microsoft.AspNetCore; @@ -16,7 +18,7 @@ namespace Geekbot.net.WebApi public static class WebApiStartup { public static void StartWebApi(IGeekbotLogger logger, RunParameters runParameters, CommandService commandService, - DatabaseContext databaseContext) + DatabaseContext databaseContext, DiscordSocketClient client, IGlobalSettings globalSettings) { WebHost.CreateDefaultBuilder() .UseKestrel(options => @@ -28,6 +30,8 @@ namespace Geekbot.net.WebApi services.AddMvc(); services.AddSingleton(commandService); services.AddSingleton(databaseContext); + services.AddSingleton(client); + services.AddSingleton(globalSettings); services.AddCors(options => { options.AddPolicy("AllowSpecificOrigin", From a8c91c03723ee78f83a287d5c658c130203c8702 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 5 Sep 2018 21:13:43 +0200 Subject: [PATCH 109/443] Hide owner commands from command endpoint --- Geekbot.net/WebApi/Controllers/Commands/CommandController.cs | 4 ++-- Geekbot.net/WebApi/Controllers/Commands/CommandDto.cs | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Geekbot.net/WebApi/Controllers/Commands/CommandController.cs b/Geekbot.net/WebApi/Controllers/Commands/CommandController.cs index 04caaae..74f6e3c 100644 --- a/Geekbot.net/WebApi/Controllers/Commands/CommandController.cs +++ b/Geekbot.net/WebApi/Controllers/Commands/CommandController.cs @@ -32,10 +32,10 @@ namespace Geekbot.net.WebApi.Controllers.Commands Name = cmd.Name, Summary = cmd.Summary, IsAdminCommand = param.Contains("admin") || param.Contains("owner"), - Aliases = cmd.Aliases.ToArray(), + Aliases = cmd.Aliases.ToList(), Params = cmdParamsObj }).ToList(); - return Ok(commandList); + return Ok(commandList.FindAll(e => !e.Aliases[0].StartsWith("owner"))); } } } \ No newline at end of file diff --git a/Geekbot.net/WebApi/Controllers/Commands/CommandDto.cs b/Geekbot.net/WebApi/Controllers/Commands/CommandDto.cs index 50f7872..68c4e74 100644 --- a/Geekbot.net/WebApi/Controllers/Commands/CommandDto.cs +++ b/Geekbot.net/WebApi/Controllers/Commands/CommandDto.cs @@ -6,10 +6,9 @@ namespace Geekbot.net.WebApi.Controllers.Commands public class CommandDto { public string Name { get; set; } - public string Category { get; set; } public string Summary { get; set; } public bool IsAdminCommand { get; set; } - public Array Aliases { get; set; } + public List Aliases { get; set; } public List Params { get; set; } } } \ No newline at end of file From fce6be56e29422263c7a113236e0f5f4f1158914 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 5 Sep 2018 21:15:13 +0200 Subject: [PATCH 110/443] Add Message migration script --- .../Commands/Admin/Owner/MigrationMethods.cs | 8 ++ .../Commands/Admin/{ => Owner}/Owner.cs | 49 ++++++++----- Geekbot.net/Commands/User/Ranking/Rank.cs | 20 ++--- Geekbot.net/Commands/User/Stats.cs | 18 ++--- Geekbot.net/Database/MessageMigration.cs | 73 +++++++++++++++++++ 5 files changed, 132 insertions(+), 36 deletions(-) create mode 100644 Geekbot.net/Commands/Admin/Owner/MigrationMethods.cs rename Geekbot.net/Commands/Admin/{ => Owner}/Owner.cs (68%) create mode 100644 Geekbot.net/Database/MessageMigration.cs diff --git a/Geekbot.net/Commands/Admin/Owner/MigrationMethods.cs b/Geekbot.net/Commands/Admin/Owner/MigrationMethods.cs new file mode 100644 index 0000000..6f50257 --- /dev/null +++ b/Geekbot.net/Commands/Admin/Owner/MigrationMethods.cs @@ -0,0 +1,8 @@ +namespace Geekbot.net.Commands.Admin.Owner +{ + public enum MigrationMethods + { + redis, + messages + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/Admin/Owner.cs b/Geekbot.net/Commands/Admin/Owner/Owner.cs similarity index 68% rename from Geekbot.net/Commands/Admin/Owner.cs rename to Geekbot.net/Commands/Admin/Owner/Owner.cs index 7cc194b..b02ee47 100644 --- a/Geekbot.net/Commands/Admin/Owner.cs +++ b/Geekbot.net/Commands/Admin/Owner/Owner.cs @@ -9,7 +9,7 @@ using Geekbot.net.Lib.GlobalSettings; using Geekbot.net.Lib.Logger; using Geekbot.net.Lib.UserRepository; -namespace Geekbot.net.Commands.Admin +namespace Geekbot.net.Commands.Admin.Owner { [Group("owner")] [RequireOwner] @@ -35,27 +35,42 @@ namespace Geekbot.net.Commands.Admin } [Command("migrate", RunMode = RunMode.Async)] - public async Task Migrate(string force = "") + public async Task Migrate(MigrationMethods method, string force = "") { try { - var status = _globalSettings.GetKey("MigrationStatus"); - if (status.Equals("Running")) + switch (method) { - await ReplyAsync("Migration already running"); - return; - } - if (status.Equals("Done") && !force.Equals("force")) - { - await ReplyAsync("Migration already ran, write `!owner migrate force` to run again"); - return; - } + case MigrationMethods.redis: + var status = _globalSettings.GetKey("MigrationStatus"); + if (status.Equals("Running")) + { + await ReplyAsync("Migration already running"); + return; + } + if (status.Equals("Done") && !force.Equals("force")) + { + await ReplyAsync("Migration already ran, write `!owner migrate redis force` to run again"); + return; + } - await ReplyAsync("starting migration"); - await _globalSettings.SetKey("MigrationStatus", "Running"); - var redisMigration = new RedisMigration(_database, _redis, _logger, _client); - await redisMigration.Migrate(); - await _globalSettings.SetKey("MigrationStatus", "Done"); + await ReplyAsync("starting migration"); + await _globalSettings.SetKey("MigrationStatus", "Running"); + var redisMigration = new RedisMigration(_database, _redis, _logger, _client); + await redisMigration.Migrate(); + await _globalSettings.SetKey("MigrationStatus", "Done"); + break; + + case MigrationMethods.messages: + await ReplyAsync("Migrating Messages to postgres..."); + var messageMigration = new MessageMigration(_database, _redis, _logger); + await messageMigration.Migrate(); + break; + + default: + await ReplyAsync("No Migration Method specified..."); + break; + } } catch (Exception e) { diff --git a/Geekbot.net/Commands/User/Ranking/Rank.cs b/Geekbot.net/Commands/User/Ranking/Rank.cs index 1bb9f95..08fd384 100644 --- a/Geekbot.net/Commands/User/Ranking/Rank.cs +++ b/Geekbot.net/Commands/User/Ranking/Rank.cs @@ -151,17 +151,17 @@ namespace Geekbot.net.Commands.User.Ranking private Dictionary GetMessageList(int amount) { -// return _database.Messages -// .Where(k => k.GuildId.Equals(Context.Guild.Id.AsLong())) -// .OrderByDescending(o => o.MessageCount) -// .Take(amount) -// .ToDictionary(key => key.UserId.AsUlong(), key => key.MessageCount); - return _redis.Db - .HashGetAll($"{Context.Guild.Id}:Messages") - .Where(user => !user.Name.Equals(0)) - .OrderByDescending(s => s.Value) + return _database.Messages + .Where(k => k.GuildId.Equals(Context.Guild.Id.AsLong())) + .OrderByDescending(o => o.MessageCount) .Take(amount) - .ToDictionary(user => ulong.Parse(user.Name), user => int.Parse(user.Value)); + .ToDictionary(key => key.UserId.AsUlong(), key => key.MessageCount); +// return _redis.Db +// .HashGetAll($"{Context.Guild.Id}:Messages") +// .Where(user => !user.Name.Equals(0)) +// .OrderByDescending(s => s.Value) +// .Take(amount) +// .ToDictionary(user => ulong.Parse(user.Name), user => int.Parse(user.Value)); } private Dictionary GetKarmaList(int amount) diff --git a/Geekbot.net/Commands/User/Stats.cs b/Geekbot.net/Commands/User/Stats.cs index 4ec2148..4468b21 100644 --- a/Geekbot.net/Commands/User/Stats.cs +++ b/Geekbot.net/Commands/User/Stats.cs @@ -39,15 +39,15 @@ namespace Geekbot.net.Commands.User var age = Math.Floor((DateTime.Now - createdAt).TotalDays); var joinedDayAgo = Math.Floor((DateTime.Now - joinedAt).TotalDays); -// var messages = _database.Messages -// .First(e => e.GuildId.Equals(Context.Guild.Id.AsLong()) && e.UserId.Equals(Context.User.Id.AsLong())) -// .MessageCount; -// var guildMessages = _database.Messages -// .Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong())) -// .Select(e => e.MessageCount) -// .Sum(); - var messages = (int) _redis.Db.HashGet($"{Context.Guild.Id}:Messages", userInfo.Id.ToString()); - var guildMessages = (int) _redis.Db.HashGet($"{Context.Guild.Id}:Messages", 0.ToString()); + var messages = _database.Messages + .First(e => e.GuildId.Equals(Context.Guild.Id.AsLong()) && e.UserId.Equals(Context.User.Id.AsLong())) + .MessageCount; + var guildMessages = _database.Messages + .Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong())) + .Select(e => e.MessageCount) + .Sum(); +// var messages = (int) _redis.Db.HashGet($"{Context.Guild.Id}:Messages", userInfo.Id.ToString()); +// var guildMessages = (int) _redis.Db.HashGet($"{Context.Guild.Id}:Messages", 0.ToString()); var level = _levelCalc.GetLevel(messages); var percent = Math.Round((double) (100 * messages) / guildMessages, 2); diff --git a/Geekbot.net/Database/MessageMigration.cs b/Geekbot.net/Database/MessageMigration.cs new file mode 100644 index 0000000..ed07072 --- /dev/null +++ b/Geekbot.net/Database/MessageMigration.cs @@ -0,0 +1,73 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using Geekbot.net.Database.Models; +using Geekbot.net.Lib.AlmostRedis; +using Geekbot.net.Lib.Extensions; +using Geekbot.net.Lib.Logger; + +namespace Geekbot.net.Database +{ + public class MessageMigration + { + private readonly DatabaseContext _database; + private readonly IAlmostRedis _redis; + private readonly IGeekbotLogger _logger; + + public MessageMigration(DatabaseContext database, IAlmostRedis redis, IGeekbotLogger logger) + { + _database = database; + _redis = redis; + _logger = logger; + } + + public async Task Migrate() + { + _logger.Warning(LogSource.Migration, "Starting message migration"); + try + { + var messageKeys = _redis.GetAllKeys().Where(e => e.ToString().EndsWith("Messages")); + foreach (var keyName in messageKeys) + { + try + { + var guildId = ulong.Parse(keyName.ToString().Split(':').FirstOrDefault()); + var guildUsers = _redis.Db.HashGetAll(keyName); + foreach (var user in guildUsers) + { + try + { + var userId = ulong.Parse(user.Name); + if (userId != 0) + { + var userMessages = int.Parse(user.Value); + _database.Messages.Add(new MessagesModel + { + UserId = userId.AsLong(), + GuildId = guildId.AsLong(), + MessageCount = userMessages + }); + } + } + catch (Exception e) + { + _logger.Error(LogSource.Migration, $"Failed to add record for a user in {guildId}", e); + } + } + + await _database.SaveChangesAsync(); + } + catch (Exception e) + { + _logger.Error(LogSource.Migration, "Failed to determinate guild", e); + } + } + _logger.Warning(LogSource.Migration, "Successfully finished message migration"); + } + catch (Exception e) + { + _logger.Error(LogSource.Migration, "Message migration failed", e); + } + } + } +} \ No newline at end of file From f0814ba6853b539d2c33e877b953dc0a426c7e4c Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 5 Sep 2018 21:15:46 +0200 Subject: [PATCH 111/443] Temporally disable message counting --- Geekbot.net/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index f2a6291..2c2e6ce 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -171,7 +171,7 @@ namespace Geekbot.net var handlers = new Handlers(_databaseInitializer, _client, _logger, _redis, _servicesProvider, _commands, _userRepository, reactionListener); _client.MessageReceived += handlers.RunCommand; - _client.MessageReceived += handlers.UpdateStats; +// _client.MessageReceived += handlers.UpdateStats; _client.MessageDeleted += handlers.MessageDeleted; _client.UserJoined += handlers.UserJoined; _client.UserUpdated += handlers.UserUpdated; From a5c70859a432431d4246525f27d8bcc809ef32e1 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 5 Sep 2018 21:22:56 +0200 Subject: [PATCH 112/443] Reenable Stats Counting --- Geekbot.net/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 2c2e6ce..f2a6291 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -171,7 +171,7 @@ namespace Geekbot.net var handlers = new Handlers(_databaseInitializer, _client, _logger, _redis, _servicesProvider, _commands, _userRepository, reactionListener); _client.MessageReceived += handlers.RunCommand; -// _client.MessageReceived += handlers.UpdateStats; + _client.MessageReceived += handlers.UpdateStats; _client.MessageDeleted += handlers.MessageDeleted; _client.UserJoined += handlers.UserJoined; _client.UserUpdated += handlers.UserUpdated; From 99245b9ead0add5fd861216e6c2a5d5925f4984d Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 5 Sep 2018 22:55:45 +0200 Subject: [PATCH 113/443] Refactor !rank, add HighscoreManager and add /v1/highscore to the api --- Geekbot.net/Commands/User/Ranking/Rank.cs | 112 +++--------------- Geekbot.net/Commands/User/Ranking/RankType.cs | 9 -- Geekbot.net/Database/RedisMigration.cs | 2 +- .../Highscores/HighscoreListEmptyException.cs | 13 ++ .../Lib/Highscores/HighscoreManager.cs | 106 +++++++++++++++++ Geekbot.net/Lib/Highscores/HighscoreTypes.cs | 9 ++ .../Highscores/HighscoreUserDto.cs} | 5 +- .../Lib/Highscores/IHighscoreManager.cs | 12 ++ Geekbot.net/Lib/Logger/LogSource.cs | 1 + Geekbot.net/Program.cs | 7 +- Geekbot.net/WebApi/ApiError.cs | 7 ++ .../Highscores/HighscoreController.cs | 56 +++++++++ .../HighscoreControllerPostBodyDto.cs | 16 +++ .../HighscoreControllerReponseBody.cs | 11 ++ Geekbot.net/WebApi/WebApiStartup.cs | 4 +- 15 files changed, 261 insertions(+), 109 deletions(-) delete mode 100644 Geekbot.net/Commands/User/Ranking/RankType.cs create mode 100644 Geekbot.net/Lib/Highscores/HighscoreListEmptyException.cs create mode 100644 Geekbot.net/Lib/Highscores/HighscoreManager.cs create mode 100644 Geekbot.net/Lib/Highscores/HighscoreTypes.cs rename Geekbot.net/{Commands/User/Ranking/RankUserDto.cs => Lib/Highscores/HighscoreUserDto.cs} (54%) create mode 100644 Geekbot.net/Lib/Highscores/IHighscoreManager.cs create mode 100644 Geekbot.net/WebApi/ApiError.cs create mode 100644 Geekbot.net/WebApi/Controllers/Highscores/HighscoreController.cs create mode 100644 Geekbot.net/WebApi/Controllers/Highscores/HighscoreControllerPostBodyDto.cs create mode 100644 Geekbot.net/WebApi/Controllers/Highscores/HighscoreControllerReponseBody.cs diff --git a/Geekbot.net/Commands/User/Ranking/Rank.cs b/Geekbot.net/Commands/User/Ranking/Rank.cs index 08fd384..9c37a4a 100644 --- a/Geekbot.net/Commands/User/Ranking/Rank.cs +++ b/Geekbot.net/Commands/User/Ranking/Rank.cs @@ -5,31 +5,30 @@ using System.Text; using System.Threading.Tasks; using Discord.Commands; using Geekbot.net.Database; -using Geekbot.net.Lib.AlmostRedis; using Geekbot.net.Lib.Converters; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; +using Geekbot.net.Lib.Highscores; using Geekbot.net.Lib.UserRepository; -using StackExchange.Redis; namespace Geekbot.net.Commands.User.Ranking { public class Rank : ModuleBase { private readonly IEmojiConverter _emojiConverter; + private readonly IHighscoreManager _highscoreManager; private readonly IErrorHandler _errorHandler; private readonly DatabaseContext _database; private readonly IUserRepository _userRepository; - private readonly IAlmostRedis _redis; public Rank(DatabaseContext database, IErrorHandler errorHandler, IUserRepository userRepository, - IEmojiConverter emojiConverter, IAlmostRedis redis) + IEmojiConverter emojiConverter, IHighscoreManager highscoreManager) { _database = database; _errorHandler = errorHandler; _userRepository = userRepository; _emojiConverter = emojiConverter; - _redis = redis; + _highscoreManager = highscoreManager; } [Command("rank", RunMode = RunMode.Async)] @@ -38,10 +37,10 @@ namespace Geekbot.net.Commands.User.Ranking { try { - RankType type; + HighscoreTypes type; try { - type = Enum.Parse(typeUnformated.ToLower()); + type = Enum.Parse(typeUnformated.ToLower()); } catch { @@ -49,7 +48,6 @@ namespace Geekbot.net.Commands.User.Ranking return; } - var replyBuilder = new StringBuilder(); if (amount > 20) { @@ -57,69 +55,28 @@ namespace Geekbot.net.Commands.User.Ranking amount = 20; } - Dictionary list; - - switch (type) + var guildId = Context.Guild.Id; + Dictionary highscoreUsers; + try { - case RankType.messages: - list = GetMessageList(amount); - break; - case RankType.karma: - list = GetKarmaList(amount); - break; - case RankType.rolls: - list = GetRollsList(amount); - break; - default: - await ReplyAsync("Valid types are '`messages`' '`karma`', '`rolls`'"); - return; + highscoreUsers = _highscoreManager.GetHighscoresWithUserData(type, guildId, amount); } - - if (!list.Any()) + catch (HighscoreListEmptyException) { await ReplyAsync($"No {type} found on this server"); return; } int guildMessages = 0; - if (type == RankType.messages) + if (type == HighscoreTypes.messages) { -// guildMessages = _database.Messages -// .Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong())) -// .Select(e => e.MessageCount) -// .Sum(); - guildMessages = (int) _redis.Db.HashGet($"{Context.Guild.Id}:Messages", 0.ToString()); + guildMessages = _database.Messages + .Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong())) + .Select(e => e.MessageCount) + .Sum(); } - var highscoreUsers = new Dictionary(); - var failedToRetrieveUser = false; - foreach (var user in list) - { - try - { - var guildUser = _userRepository.Get(user.Key); - if (guildUser?.Username != null) - { - highscoreUsers.Add(new RankUserDto - { - Username = guildUser.Username, - Discriminator = guildUser.Discriminator - }, user.Value); - } - else - { - highscoreUsers.Add(new RankUserDto - { - Id = user.Key.ToString() - }, user.Value); - failedToRetrieveUser = true; - } - } - catch - { - // ignore - } - } + var failedToRetrieveUser = highscoreUsers.Any(e => string.IsNullOrEmpty(e.Key.Username)); if (failedToRetrieveUser) replyBuilder.AppendLine(":warning: I couldn't find all usernames. Maybe they left the server?\n"); replyBuilder.AppendLine($":bar_chart: **{type.ToString().CapitalizeFirst()} Highscore for {Context.Guild.Name}**"); @@ -134,7 +91,7 @@ namespace Geekbot.net.Commands.User.Ranking ? $"**{user.Key.Username}#{user.Key.Discriminator}**" : $"**{user.Key.Id}**"); - replyBuilder.Append(type == RankType.messages + replyBuilder.Append(type == HighscoreTypes.messages ? $" - {user.Value} {type} - {Math.Round((double) (100 * user.Value) / guildMessages, digits: 2)}%\n" : $" - {user.Value} {type}\n"); @@ -148,38 +105,5 @@ namespace Geekbot.net.Commands.User.Ranking await _errorHandler.HandleCommandException(e, Context); } } - - private Dictionary GetMessageList(int amount) - { - return _database.Messages - .Where(k => k.GuildId.Equals(Context.Guild.Id.AsLong())) - .OrderByDescending(o => o.MessageCount) - .Take(amount) - .ToDictionary(key => key.UserId.AsUlong(), key => key.MessageCount); -// return _redis.Db -// .HashGetAll($"{Context.Guild.Id}:Messages") -// .Where(user => !user.Name.Equals(0)) -// .OrderByDescending(s => s.Value) -// .Take(amount) -// .ToDictionary(user => ulong.Parse(user.Name), user => int.Parse(user.Value)); - } - - private Dictionary GetKarmaList(int amount) - { - return _database.Karma - .Where(k => k.GuildId.Equals(Context.Guild.Id.AsLong())) - .OrderByDescending(o => o.Karma) - .Take(amount) - .ToDictionary(key => key.UserId.AsUlong(), key => key.Karma); - } - - private Dictionary GetRollsList(int amount) - { - return _database.Rolls - .Where(k => k.GuildId.Equals(Context.Guild.Id.AsLong())) - .OrderByDescending(o => o.Rolls) - .Take(amount) - .ToDictionary(key => key.UserId.AsUlong(), key => key.Rolls); - } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/User/Ranking/RankType.cs b/Geekbot.net/Commands/User/Ranking/RankType.cs deleted file mode 100644 index 1e88ef0..0000000 --- a/Geekbot.net/Commands/User/Ranking/RankType.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace Geekbot.net.Commands.User.Ranking -{ - public enum RankType - { - messages, - karma, - rolls - } -} \ No newline at end of file diff --git a/Geekbot.net/Database/RedisMigration.cs b/Geekbot.net/Database/RedisMigration.cs index 92ee555..74d97d2 100644 --- a/Geekbot.net/Database/RedisMigration.cs +++ b/Geekbot.net/Database/RedisMigration.cs @@ -43,7 +43,7 @@ namespace Geekbot.net.Database Console.WriteLine(g.Name); allGuilds.Add(g); } - catch (Exception e) + catch (Exception) { // ignore } diff --git a/Geekbot.net/Lib/Highscores/HighscoreListEmptyException.cs b/Geekbot.net/Lib/Highscores/HighscoreListEmptyException.cs new file mode 100644 index 0000000..8c05ca9 --- /dev/null +++ b/Geekbot.net/Lib/Highscores/HighscoreListEmptyException.cs @@ -0,0 +1,13 @@ +using System; + +namespace Geekbot.net.Lib.Highscores +{ + public class HighscoreListEmptyException : Exception + { + public HighscoreListEmptyException() {} + + public HighscoreListEmptyException(string message) : base(message) {} + + public HighscoreListEmptyException(string message, Exception inner) : base(message, inner) {} + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/Highscores/HighscoreManager.cs b/Geekbot.net/Lib/Highscores/HighscoreManager.cs new file mode 100644 index 0000000..c684254 --- /dev/null +++ b/Geekbot.net/Lib/Highscores/HighscoreManager.cs @@ -0,0 +1,106 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Geekbot.net.Database; +using Geekbot.net.Lib.Extensions; +using Geekbot.net.Lib.Logger; +using Geekbot.net.Lib.UserRepository; + +namespace Geekbot.net.Lib.Highscores +{ + public class HighscoreManager : IHighscoreManager + { + private readonly DatabaseContext _database; + private readonly IUserRepository _userRepository; + + public HighscoreManager(DatabaseContext databaseContext, IUserRepository userRepository) + { + _database = databaseContext; + _userRepository = userRepository; + + } + + public Dictionary GetHighscoresWithUserData(HighscoreTypes type, ulong guildId, int amount) + { + Dictionary list; + switch (type) + { + case HighscoreTypes.messages: + list = GetMessageList(guildId, amount); + break; + case HighscoreTypes.karma: + list = GetKarmaList(guildId, amount); + break; + case HighscoreTypes.rolls: + list = GetRollsList(guildId, amount); + break; + default: + list = new Dictionary(); + break; + } + + if (!list.Any()) + { + throw new HighscoreListEmptyException($"No {type} found for guild {guildId}"); + } + + var highscoreUsers = new Dictionary(); + foreach (var user in list) + { + try + { + var guildUser = _userRepository.Get(user.Key); + if (guildUser?.Username != null) + { + highscoreUsers.Add(new HighscoreUserDto + { + Username = guildUser.Username, + Discriminator = guildUser.Discriminator, + Avatar = guildUser.AvatarUrl + }, user.Value); + } + else + { + highscoreUsers.Add(new HighscoreUserDto + { + Id = user.Key.ToString() + }, user.Value); + } + } + catch + { + // ignore + } + } + + return highscoreUsers; + } + + public Dictionary GetMessageList(ulong guildId, int amount) + { + return _database.Messages + .Where(k => k.GuildId.Equals(guildId.AsLong())) + .OrderByDescending(o => o.MessageCount) + .Take(amount) + .ToDictionary(key => key.UserId.AsUlong(), key => key.MessageCount); + } + + public Dictionary GetKarmaList(ulong guildId, int amount) + { + return _database.Karma + .Where(k => k.GuildId.Equals(guildId.AsLong())) + .OrderByDescending(o => o.Karma) + .Take(amount) + .ToDictionary(key => key.UserId.AsUlong(), key => key.Karma); + } + + public Dictionary GetRollsList(ulong guildId, int amount) + { + return _database.Rolls + .Where(k => k.GuildId.Equals(guildId.AsLong())) + .OrderByDescending(o => o.Rolls) + .Take(amount) + .ToDictionary(key => key.UserId.AsUlong(), key => key.Rolls); + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/Highscores/HighscoreTypes.cs b/Geekbot.net/Lib/Highscores/HighscoreTypes.cs new file mode 100644 index 0000000..492d08d --- /dev/null +++ b/Geekbot.net/Lib/Highscores/HighscoreTypes.cs @@ -0,0 +1,9 @@ +namespace Geekbot.net.Lib.Highscores +{ + public enum HighscoreTypes + { + messages, + karma, + rolls + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/User/Ranking/RankUserDto.cs b/Geekbot.net/Lib/Highscores/HighscoreUserDto.cs similarity index 54% rename from Geekbot.net/Commands/User/Ranking/RankUserDto.cs rename to Geekbot.net/Lib/Highscores/HighscoreUserDto.cs index 2ec518e..7abb352 100644 --- a/Geekbot.net/Commands/User/Ranking/RankUserDto.cs +++ b/Geekbot.net/Lib/Highscores/HighscoreUserDto.cs @@ -1,8 +1,9 @@ -namespace Geekbot.net.Commands.User.Ranking +namespace Geekbot.net.Lib.Highscores { - internal class RankUserDto + public class HighscoreUserDto { public string Username { get; set; } + public string Avatar { get; set; } public string Discriminator { get; set; } public string Id { get; set; } } diff --git a/Geekbot.net/Lib/Highscores/IHighscoreManager.cs b/Geekbot.net/Lib/Highscores/IHighscoreManager.cs new file mode 100644 index 0000000..a09b07e --- /dev/null +++ b/Geekbot.net/Lib/Highscores/IHighscoreManager.cs @@ -0,0 +1,12 @@ +using System.Collections.Generic; + +namespace Geekbot.net.Lib.Highscores +{ + public interface IHighscoreManager + { + Dictionary GetHighscoresWithUserData(HighscoreTypes type, ulong guildId, int amount); + Dictionary GetMessageList(ulong guildId, int amount); + Dictionary GetKarmaList(ulong guildId, int amount); + Dictionary GetRollsList(ulong guildId, int amount); + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/Logger/LogSource.cs b/Geekbot.net/Lib/Logger/LogSource.cs index 42c1877..7cd92ff 100644 --- a/Geekbot.net/Lib/Logger/LogSource.cs +++ b/Geekbot.net/Lib/Logger/LogSource.cs @@ -17,6 +17,7 @@ namespace Geekbot.net.Lib.Logger Command, Api, Migration, + HighscoreManager, Other } } \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index f2a6291..9af8879 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -7,7 +7,6 @@ using Discord; using Discord.Commands; using Discord.WebSocket; using Geekbot.net.Database; -using Geekbot.net.Database.LoggingAdapter; using Geekbot.net.Lib; using Geekbot.net.Lib.AlmostRedis; using Geekbot.net.Lib.Audio; @@ -15,6 +14,7 @@ using Geekbot.net.Lib.Clients; using Geekbot.net.Lib.Converters; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.GlobalSettings; +using Geekbot.net.Lib.Highscores; using Geekbot.net.Lib.Levels; using Geekbot.net.Lib.Localization; using Geekbot.net.Lib.Logger; @@ -40,6 +40,7 @@ namespace Geekbot.net private IUserRepository _userRepository; private RunParameters _runParameters; private IAlmostRedis _redis; + private IHighscoreManager _highscoreManager; private static void Main(string[] args) { @@ -123,6 +124,7 @@ namespace Geekbot.net var mtgManaConverter = new MtgManaConverter(); var wikipediaClient = new WikipediaClient(); var audioUtils = new AudioUtils(); + _highscoreManager = new HighscoreManager(_databaseInitializer.Initialize(), _userRepository); _services.AddSingleton(_redis); _services.AddSingleton(_userRepository); @@ -135,6 +137,7 @@ namespace Geekbot.net _services.AddSingleton(mtgManaConverter); _services.AddSingleton(wikipediaClient); _services.AddSingleton(audioUtils); + _services.AddSingleton(_highscoreManager); _services.AddSingleton(_globalSettings); _services.AddTransient((e) => _databaseInitializer.Initialize()); @@ -203,7 +206,7 @@ namespace Geekbot.net private Task StartWebApi() { _logger.Information(LogSource.Api, "Starting Webserver"); - WebApi.WebApiStartup.StartWebApi(_logger, _runParameters, _commands, _databaseInitializer.Initialize(), _client, _globalSettings); + WebApi.WebApiStartup.StartWebApi(_logger, _runParameters, _commands, _databaseInitializer.Initialize(), _client, _globalSettings, _highscoreManager); return Task.CompletedTask; } } diff --git a/Geekbot.net/WebApi/ApiError.cs b/Geekbot.net/WebApi/ApiError.cs new file mode 100644 index 0000000..182518e --- /dev/null +++ b/Geekbot.net/WebApi/ApiError.cs @@ -0,0 +1,7 @@ +namespace Geekbot.net.WebApi +{ + public class ApiError + { + public string Message { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/WebApi/Controllers/Highscores/HighscoreController.cs b/Geekbot.net/WebApi/Controllers/Highscores/HighscoreController.cs new file mode 100644 index 0000000..3cccb0e --- /dev/null +++ b/Geekbot.net/WebApi/Controllers/Highscores/HighscoreController.cs @@ -0,0 +1,56 @@ +using System.Collections.Generic; +using Geekbot.net.Lib.Highscores; +using Microsoft.AspNetCore.Cors; +using Microsoft.AspNetCore.Mvc; + +namespace Geekbot.net.WebApi.Controllers.Highscores +{ + [EnableCors("AllowSpecificOrigin")] + public class HighscoreController : Controller + { + private readonly IHighscoreManager _highscoreManager; + + public HighscoreController(IHighscoreManager highscoreManager) + { + _highscoreManager = highscoreManager; + } + + [HttpPost] + [Route("/v1/highscore")] + public IActionResult GetHighscores([FromBody] HighscoreControllerPostBodyDto body) + { + if (!ModelState.IsValid || body == null) + { + var error = new SerializableError(ModelState); + return BadRequest(error); + } + + Dictionary list; + try + { + list = _highscoreManager.GetHighscoresWithUserData(body.Type, body.GuildId, body.Amount); + } + catch (HighscoreListEmptyException) + { + return NotFound(new ApiError + { + Message = $"No {body.Type} found on this server" + }); + } + + var response = new List(); + var counter = 1; + foreach (var item in list) + { + response.Add(new HighscoreControllerReponseBody + { + count = item.Value, + rank = counter, + user = item.Key + }); + counter++; + } + return Ok(response); + } + } +} \ No newline at end of file diff --git a/Geekbot.net/WebApi/Controllers/Highscores/HighscoreControllerPostBodyDto.cs b/Geekbot.net/WebApi/Controllers/Highscores/HighscoreControllerPostBodyDto.cs new file mode 100644 index 0000000..22da3c7 --- /dev/null +++ b/Geekbot.net/WebApi/Controllers/Highscores/HighscoreControllerPostBodyDto.cs @@ -0,0 +1,16 @@ +using System.ComponentModel.DataAnnotations; +using Geekbot.net.Lib.Highscores; + +namespace Geekbot.net.WebApi.Controllers.Highscores +{ + public class HighscoreControllerPostBodyDto + { + [Required] + public ulong GuildId { get; set; } + + public HighscoreTypes Type { get; set; } = HighscoreTypes.messages; + + [Range(1, 150)] + public int Amount { get; set; } = 50; + } +} \ No newline at end of file diff --git a/Geekbot.net/WebApi/Controllers/Highscores/HighscoreControllerReponseBody.cs b/Geekbot.net/WebApi/Controllers/Highscores/HighscoreControllerReponseBody.cs new file mode 100644 index 0000000..0e59880 --- /dev/null +++ b/Geekbot.net/WebApi/Controllers/Highscores/HighscoreControllerReponseBody.cs @@ -0,0 +1,11 @@ +using Geekbot.net.Lib.Highscores; + +namespace Geekbot.net.WebApi.Controllers.Highscores +{ + public class HighscoreControllerReponseBody + { + public int rank { get; set; } + public HighscoreUserDto user { get; set; } + public int count { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/WebApi/WebApiStartup.cs b/Geekbot.net/WebApi/WebApiStartup.cs index e93a8d5..2459362 100644 --- a/Geekbot.net/WebApi/WebApiStartup.cs +++ b/Geekbot.net/WebApi/WebApiStartup.cs @@ -5,6 +5,7 @@ using Discord.WebSocket; using Geekbot.net.Database; using Geekbot.net.Lib; using Geekbot.net.Lib.GlobalSettings; +using Geekbot.net.Lib.Highscores; using Geekbot.net.Lib.Logger; using Geekbot.net.WebApi.Logging; using Microsoft.AspNetCore; @@ -18,7 +19,7 @@ namespace Geekbot.net.WebApi public static class WebApiStartup { public static void StartWebApi(IGeekbotLogger logger, RunParameters runParameters, CommandService commandService, - DatabaseContext databaseContext, DiscordSocketClient client, IGlobalSettings globalSettings) + DatabaseContext databaseContext, DiscordSocketClient client, IGlobalSettings globalSettings, IHighscoreManager highscoreManager) { WebHost.CreateDefaultBuilder() .UseKestrel(options => @@ -32,6 +33,7 @@ namespace Geekbot.net.WebApi services.AddSingleton(databaseContext); services.AddSingleton(client); services.AddSingleton(globalSettings); + services.AddSingleton(highscoreManager); services.AddCors(options => { options.AddPolicy("AllowSpecificOrigin", From 863fcd5e9df78d2a19c5031e0899a919203edf03 Mon Sep 17 00:00:00 2001 From: runebaas Date: Tue, 11 Sep 2018 00:13:54 +0200 Subject: [PATCH 114/443] Add bugsnag --- Geekbot.net/Geekbot.net.csproj | 2 + Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs | 83 +++++++++++++++---- Geekbot.net/Program.cs | 2 +- 3 files changed, 70 insertions(+), 17 deletions(-) diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 46c1eff..052199d 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -19,6 +19,8 @@ true + + 2.0.0-beta2-00984 diff --git a/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs b/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs index 8ebee2e..7d5deec 100644 --- a/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs +++ b/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs @@ -1,12 +1,17 @@ using System; +using System.Collections.Generic; using System.Net; using System.Threading.Tasks; +using Bugsnag; +using Bugsnag.Payload; using Discord.Commands; using Discord.Net; +using Geekbot.net.Lib.GlobalSettings; using Geekbot.net.Lib.Localization; using Geekbot.net.Lib.Logger; using SharpRaven; using SharpRaven.Data; +using Exception = System.Exception; namespace Geekbot.net.Lib.ErrorHandling { @@ -16,8 +21,9 @@ namespace Geekbot.net.Lib.ErrorHandling private readonly ITranslationHandler _translation; private readonly IRavenClient _raven; private readonly bool _errorsInChat; + private readonly IClient _bugsnag; - public ErrorHandler(IGeekbotLogger logger, ITranslationHandler translation, bool errorsInChat) + public ErrorHandler(IGeekbotLogger logger, ITranslationHandler translation, IGlobalSettings globalSettings, bool errorsInChat) { _logger = logger; _translation = translation; @@ -33,6 +39,21 @@ namespace Geekbot.net.Lib.ErrorHandling { _raven = null; } + + var bugsnagApiKey = globalSettings.GetKey("BugsnagApiKey"); + if (!string.IsNullOrEmpty(bugsnagApiKey)) + { + _bugsnag = new Bugsnag.Client(new Bugsnag.Configuration + { + ApiKey = bugsnagApiKey, + AppVersion = Constants.BotVersion() + }); + _logger.Information(LogSource.Geekbot, "Command Errors will be logged to Bugsnag"); + } + else + { + _bugsnag = null; + } } public async Task HandleCommandException(Exception e, ICommandContext context, string errorMessage = "def") @@ -65,20 +86,8 @@ namespace Geekbot.net.Lib.ErrorHandling } } - - if (_raven == null) return; - - var sentryEvent = new SentryEvent(e) - { - Tags = - { - ["discord_server"] = errorObj.Guild.Name, - ["discord_user"] = errorObj.User.Name - }, - Message = errorObj.Message.Content, - Extra = errorObj - }; - _raven.Capture(sentryEvent); + + ReportExternal(e, errorObj); } catch (Exception ex) { @@ -98,6 +107,48 @@ namespace Geekbot.net.Lib.ErrorHandling } } - + private void ReportExternal(Exception e, MessageDto errorObj) + { + if (_raven != null) + { + var sentryEvent = new SentryEvent(e) + { + Tags = + { + ["discord_server"] = errorObj.Guild.Name, + ["discord_user"] = errorObj.User.Name + }, + Message = errorObj.Message.Content, + Extra = errorObj + }; + _raven.Capture(sentryEvent); + } + + _bugsnag?.Notify(e, (report) => + { + report.Event.Metadata.Add("Discord Location", new Dictionary + { + {"Guild Name", errorObj.Guild.Name}, + {"Guild Id", errorObj.Guild.Id}, + {"Channel Name", errorObj.Channel.Name}, + {"Channel Id", errorObj.Channel.Id} + }); + report.Event.Metadata.Add("Message Info", new Dictionary + { + {"Content", errorObj.Message.Content}, + {"Id", errorObj.Message.Id}, + {"Attachments", errorObj.Message.Attachments.ToString()}, + {"ChannelMentions", errorObj.Message.ChannelMentions.ToString()}, + {"UserMentions", errorObj.Message.UserMentions.ToString()}, + {"RoleMentions", errorObj.Message.RoleMentions.ToString()}, + }); + report.Event.Severity = Severity.Error; + report.Event.User = new User + { + Id = errorObj.User.Id, + Name = errorObj.User.Name + }; + }); + } } } \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 9af8879..e2cff4f 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -162,7 +162,7 @@ namespace Geekbot.net _logger.Information(LogSource.Geekbot, "Registering Stuff"); var translationHandler = new TranslationHandler(_databaseInitializer.Initialize(), _logger); - var errorHandler = new ErrorHandler(_logger, translationHandler, _runParameters.ExposeErrors); + var errorHandler = new ErrorHandler(_logger, translationHandler, _globalSettings, _runParameters.ExposeErrors); var reactionListener = new ReactionListener(_redis.Db); _services.AddSingleton(errorHandler); _services.AddSingleton(translationHandler); From 458268bd41e0db5a49ef96a73aa602c0470b2c61 Mon Sep 17 00:00:00 2001 From: runebaas Date: Mon, 8 Oct 2018 22:15:07 +0200 Subject: [PATCH 115/443] Fix bug in stats where the requestor would always see his own message count --- Geekbot.net/Commands/User/Stats.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Geekbot.net/Commands/User/Stats.cs b/Geekbot.net/Commands/User/Stats.cs index 4468b21..a230456 100644 --- a/Geekbot.net/Commands/User/Stats.cs +++ b/Geekbot.net/Commands/User/Stats.cs @@ -40,7 +40,7 @@ namespace Geekbot.net.Commands.User var joinedDayAgo = Math.Floor((DateTime.Now - joinedAt).TotalDays); var messages = _database.Messages - .First(e => e.GuildId.Equals(Context.Guild.Id.AsLong()) && e.UserId.Equals(Context.User.Id.AsLong())) + .First(e => e.GuildId.Equals(Context.Guild.Id.AsLong()) && e.UserId.Equals(userInfo.Id.AsLong())) .MessageCount; var guildMessages = _database.Messages .Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong())) From e8546f37f2040991b7cc6e9ebaff8dd17a194876 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Tue, 30 Oct 2018 19:53:50 +0900 Subject: [PATCH 116/443] Disable the say command --- Geekbot.net/Commands/Admin/Say.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Geekbot.net/Commands/Admin/Say.cs b/Geekbot.net/Commands/Admin/Say.cs index 910d700..2a6f80e 100644 --- a/Geekbot.net/Commands/Admin/Say.cs +++ b/Geekbot.net/Commands/Admin/Say.cs @@ -22,8 +22,7 @@ namespace Geekbot.net.Commands.Admin { try { - await Context.Message.DeleteAsync(); - await ReplyAsync(echo); + await Context.User.SendMessageAsync("the `!say` command has been disabled since 2018/10/30"); } catch (Exception e) { From 26da438efeb462126e1c4ac12eb3f41d51bb6392 Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 29 Nov 2018 18:14:22 +0100 Subject: [PATCH 117/443] Fix namehistory command --- Geekbot.net/Commands/Admin/Mod.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Geekbot.net/Commands/Admin/Mod.cs b/Geekbot.net/Commands/Admin/Mod.cs index b014e53..e1653e9 100644 --- a/Geekbot.net/Commands/Admin/Mod.cs +++ b/Geekbot.net/Commands/Admin/Mod.cs @@ -33,10 +33,17 @@ namespace Geekbot.net.Commands.Admin try { var userRepo = _userRepository.Get(user.Id); - var sb = new StringBuilder(); - sb.AppendLine($":bust_in_silhouette: {user.Username} has been known as:"); - foreach (var name in userRepo.UsedNames) sb.AppendLine($"- `{name.Name}`"); - await ReplyAsync(sb.ToString()); + if (userRepo != null && userRepo.UsedNames != null) + { + var sb = new StringBuilder(); + sb.AppendLine($":bust_in_silhouette: {user.Username} has been known as:"); + foreach (var name in userRepo.UsedNames) sb.AppendLine($"- `{name.Name}`"); + await ReplyAsync(sb.ToString()); + } + else + { + await ReplyAsync($"No name changes found for {user.Username}"); + } } catch (Exception e) { From 2c6b61b7e6f5f444974b5bce54646dfa93d810ef Mon Sep 17 00:00:00 2001 From: Runebaas Date: Sun, 27 Jan 2019 22:08:49 +0100 Subject: [PATCH 118/443] Add parameter to enable db logging, disabled by default --- Geekbot.net.sln.DotSettings | 1 + Geekbot.net/Database/DatabaseInitializer.cs | 10 +++++++--- .../Database/LoggingAdapter/NpgsqlLoggingAdapter.cs | 7 +++++-- .../LoggingAdapter/NpgsqlLoggingProviderAdapter.cs | 9 ++++++--- Geekbot.net/Lib/RunParameters.cs | 4 ++++ 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/Geekbot.net.sln.DotSettings b/Geekbot.net.sln.DotSettings index 7fa04b3..6d439b8 100644 --- a/Geekbot.net.sln.DotSettings +++ b/Geekbot.net.sln.DotSettings @@ -5,6 +5,7 @@ True True True + True True True True diff --git a/Geekbot.net/Database/DatabaseInitializer.cs b/Geekbot.net/Database/DatabaseInitializer.cs index 7eff2c6..6adff44 100644 --- a/Geekbot.net/Database/DatabaseInitializer.cs +++ b/Geekbot.net/Database/DatabaseInitializer.cs @@ -16,7 +16,7 @@ namespace Geekbot.net.Database { _runParameters = runParameters; _logger = logger; - NpgsqlLogManager.Provider = new NpgsqlLoggingProviderAdapter(_logger); + NpgsqlLogManager.Provider = new NpgsqlLoggingProviderAdapter(logger, runParameters); } public DatabaseContext Initialize() @@ -45,8 +45,12 @@ namespace Geekbot.net.Database _logger.Error(LogSource.Geekbot, "Could not Connect to datbase", e); Environment.Exit(GeekbotExitCode.DatabaseConnectionFailed.GetHashCode()); } - - _logger.Information(LogSource.Database, $"Connected with {database.Database.ProviderName}"); + + if (_runParameters.DbLogging) + { + _logger.Information(LogSource.Database, $"Connected with {database.Database.ProviderName}"); + } + return database; } } diff --git a/Geekbot.net/Database/LoggingAdapter/NpgsqlLoggingAdapter.cs b/Geekbot.net/Database/LoggingAdapter/NpgsqlLoggingAdapter.cs index 80d60b3..e8850cd 100644 --- a/Geekbot.net/Database/LoggingAdapter/NpgsqlLoggingAdapter.cs +++ b/Geekbot.net/Database/LoggingAdapter/NpgsqlLoggingAdapter.cs @@ -1,4 +1,5 @@ using System; +using Geekbot.net.Lib; using Geekbot.net.Lib.Logger; using Npgsql.Logging; using LogLevel = NLog.LogLevel; @@ -9,17 +10,19 @@ namespace Geekbot.net.Database.LoggingAdapter { private readonly string _name; private readonly IGeekbotLogger _geekbotLogger; + private readonly RunParameters _runParameters; - public NpgsqlLoggingAdapter(string name, IGeekbotLogger geekbotLogger) + public NpgsqlLoggingAdapter(string name, IGeekbotLogger geekbotLogger, RunParameters runParameters) { _name = name.Substring(7); _geekbotLogger = geekbotLogger; + _runParameters = runParameters; geekbotLogger.Trace(LogSource.Database, $"Loaded Npgsql logging adapter: {name}"); } public override bool IsEnabled(NpgsqlLogLevel level) { - return !_geekbotLogger.LogAsJson() && _geekbotLogger.GetNLogger().IsEnabled(ToGeekbotLogLevel(level)); + return (_runParameters.DbLogging && _geekbotLogger.GetNLogger().IsEnabled(ToGeekbotLogLevel(level))); } public override void Log(NpgsqlLogLevel level, int connectorId, string msg, Exception exception = null) diff --git a/Geekbot.net/Database/LoggingAdapter/NpgsqlLoggingProviderAdapter.cs b/Geekbot.net/Database/LoggingAdapter/NpgsqlLoggingProviderAdapter.cs index 0888111..efe82a3 100644 --- a/Geekbot.net/Database/LoggingAdapter/NpgsqlLoggingProviderAdapter.cs +++ b/Geekbot.net/Database/LoggingAdapter/NpgsqlLoggingProviderAdapter.cs @@ -1,4 +1,5 @@ -using Geekbot.net.Lib.Logger; +using Geekbot.net.Lib; +using Geekbot.net.Lib.Logger; using Npgsql.Logging; namespace Geekbot.net.Database.LoggingAdapter @@ -6,15 +7,17 @@ namespace Geekbot.net.Database.LoggingAdapter public class NpgsqlLoggingProviderAdapter : INpgsqlLoggingProvider { private readonly GeekbotLogger _geekbotLogger; + private readonly RunParameters _runParameters; - public NpgsqlLoggingProviderAdapter(GeekbotLogger geekbotLogger) + public NpgsqlLoggingProviderAdapter(GeekbotLogger geekbotLogger, RunParameters runParameters) { _geekbotLogger = geekbotLogger; + _runParameters = runParameters; } public NpgsqlLogger CreateLogger(string name) { - return new NpgsqlLoggingAdapter(name, _geekbotLogger); + return new NpgsqlLoggingAdapter(name, _geekbotLogger, _runParameters); } } } \ No newline at end of file diff --git a/Geekbot.net/Lib/RunParameters.cs b/Geekbot.net/Lib/RunParameters.cs index 7f2dd3d..444a60f 100644 --- a/Geekbot.net/Lib/RunParameters.cs +++ b/Geekbot.net/Lib/RunParameters.cs @@ -45,6 +45,10 @@ namespace Geekbot.net.Lib [Option("db-password", Default = "", HelpText = "Set a posgresql password")] public string DbPassword { get; set; } + + // Logging + [Option("db-logging", Default = false, HelpText = "Enable database logging")] + public bool DbLogging { get; set; } /************************************ * Redis * From 8059f033049692a3d45c38f5148ede87dbfa8cdb Mon Sep 17 00:00:00 2001 From: Runebaas Date: Sun, 27 Jan 2019 22:32:13 +0100 Subject: [PATCH 119/443] Remove !say completly --- Geekbot.net/Commands/Admin/Say.cs | 33 ------------------------------- 1 file changed, 33 deletions(-) delete mode 100644 Geekbot.net/Commands/Admin/Say.cs diff --git a/Geekbot.net/Commands/Admin/Say.cs b/Geekbot.net/Commands/Admin/Say.cs deleted file mode 100644 index 2a6f80e..0000000 --- a/Geekbot.net/Commands/Admin/Say.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using System.Threading.Tasks; -using Discord; -using Discord.Commands; -using Geekbot.net.Lib.ErrorHandling; - -namespace Geekbot.net.Commands.Admin -{ - public class Say : ModuleBase - { - private readonly IErrorHandler _errorHandler; - - public Say(IErrorHandler errorHandler) - { - _errorHandler = errorHandler; - } - - [RequireUserPermission(GuildPermission.Administrator)] - [Command("say", RunMode = RunMode.Async)] - [Summary("Say Something.")] - public async Task Echo([Remainder] [Summary("What?")] string echo) - { - try - { - await Context.User.SendMessageAsync("the `!say` command has been disabled since 2018/10/30"); - } - catch (Exception e) - { - await _errorHandler.HandleCommandException(e, Context); - } - } - } -} \ No newline at end of file From 4de8447c97158bd5a10a34a636b282cd575c9faa Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 13 Feb 2019 00:50:31 +0100 Subject: [PATCH 120/443] Fix roll command to write successful writes to the database instead of redis --- Geekbot.net/Commands/Games/Roll.cs | 31 +++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/Geekbot.net/Commands/Games/Roll.cs b/Geekbot.net/Commands/Games/Roll.cs index c629ed8..8db295c 100644 --- a/Geekbot.net/Commands/Games/Roll.cs +++ b/Geekbot.net/Commands/Games/Roll.cs @@ -1,8 +1,12 @@ using System; +using System.Linq; using System.Threading.Tasks; using Discord.Commands; +using Geekbot.net.Database; +using Geekbot.net.Database.Models; using Geekbot.net.Lib.AlmostRedis; using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Localization; using StackExchange.Redis; @@ -13,11 +17,13 @@ namespace Geekbot.net.Commands.Games private readonly IErrorHandler _errorHandler; private readonly IAlmostRedis _redis; private readonly ITranslationHandler _translation; + private readonly DatabaseContext _database; - public Roll(IAlmostRedis redis, IErrorHandler errorHandler, ITranslationHandler translation) + public Roll(IAlmostRedis redis, IErrorHandler errorHandler, ITranslationHandler translation, DatabaseContext database) { _redis = redis; _translation = translation; + _database = database; _errorHandler = errorHandler; } @@ -50,6 +56,10 @@ namespace Geekbot.net.Commands.Games { await ReplyAsync(string.Format(transDict["Gratz"], Context.Message.Author)); _redis.Db.HashIncrement($"{Context.Guild.Id}:Rolls", Context.User.Id.ToString()); + var user = await GetUser(Context.User.Id); + user.Rolls += 1; + _database.Rolls.Update(user); + await _database.SaveChangesAsync(); } } else @@ -62,5 +72,24 @@ namespace Geekbot.net.Commands.Games await _errorHandler.HandleCommandException(e, Context); } } + + private async Task GetUser(ulong userId) + { + var user = _database.Rolls.FirstOrDefault(u =>u.GuildId.Equals(Context.Guild.Id.AsLong()) && u.UserId.Equals(userId.AsLong())) ?? await CreateNewRow(userId); + return user; + } + + private async Task CreateNewRow(ulong userId) + { + var user = new RollsModel() + { + GuildId = Context.Guild.Id.AsLong(), + UserId = userId.AsLong(), + Rolls = 0 + }; + var newUser = _database.Rolls.Add(user).Entity; + await _database.SaveChangesAsync(); + return newUser; + } } } \ No newline at end of file From 4e4f2e88d5e4c6dead62bb4cf2ef710695a90d34 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 13 Feb 2019 01:06:05 +0100 Subject: [PATCH 121/443] Fix deployment --- .gitlab-ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 1a55164..1ba3063 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -36,8 +36,8 @@ deploy: - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null - chmod 700 ~/.ssh script: - - rsync -rav -e "ssh -p 65432" ./Geekbot.net/Binaries/* geekbot@192.168.156.175:$DEPPATH - - ssh -p 65432 geekbot@192.168.156.175 "sudo systemctl restart geekbot.service" + - rsync -rav -e "ssh -p 65432" ./Geekbot.net/Binaries/* geekbot@$DEPIP:$DEPPATH + - ssh -p 65432 geekbot@$DEPIP "sudo systemctl restart geekbot.service" mirror: stage: deploy From aa059b5558a49e992289e6c1593165e89a1b15c1 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Sat, 2 Mar 2019 00:12:35 -0500 Subject: [PATCH 122/443] Update Discord.net --- Geekbot.net/Geekbot.net.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 052199d..554950c 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -23,7 +23,7 @@ - 2.0.0-beta2-00984 + 2.0.1 From ff15e21b5af76878ae5da79d426b326d65b9bd9e Mon Sep 17 00:00:00 2001 From: Runebaas Date: Sat, 2 Mar 2019 00:32:59 -0500 Subject: [PATCH 123/443] Upgrade Gatherer Client and fix bug where card legalities could be an empty collection and throwing and exception as a result --- Geekbot.net/Commands/Integrations/MagicTheGathering.cs | 2 +- Geekbot.net/Geekbot.net.csproj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Geekbot.net/Commands/Integrations/MagicTheGathering.cs b/Geekbot.net/Commands/Integrations/MagicTheGathering.cs index 2d3e7c6..bb95eb7 100644 --- a/Geekbot.net/Commands/Integrations/MagicTheGathering.cs +++ b/Geekbot.net/Commands/Integrations/MagicTheGathering.cs @@ -57,7 +57,7 @@ namespace Geekbot.net.Commands.Integrations if (!string.IsNullOrEmpty(card.ManaCost)) eb.AddInlineField("Cost", _manaConverter.ConvertMana(card.ManaCost)); if (!string.IsNullOrEmpty(card.Rarity)) eb.AddInlineField("Rarity", card.Rarity); - if (card.Legalities != null) + if (card.Legalities != null && card.Legalities.Count > 0) eb.AddField("Legality", string.Join(", ", card.Legalities.Select(e => e.Format))); await ReplyAsync("", false, eb.Build()); diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 554950c..b0a92d9 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -40,7 +40,7 @@ - + From 16f70d52ced95596ab72531ddb0d8193e9113d23 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Sat, 2 Mar 2019 00:48:16 -0500 Subject: [PATCH 124/443] Some Upgrades --- Geekbot.net/Geekbot.net.csproj | 16 ++++++++-------- Tests/Tests.csproj | 4 ++-- WikipediaApi/WikipediaApi.csproj | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index b0a92d9..0ed34e8 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -21,12 +21,12 @@ - + 2.0.1 - - + + @@ -42,14 +42,14 @@ - - - + + + - + - + diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 58fbfc6..094bf84 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -6,8 +6,8 @@ - - + + diff --git a/WikipediaApi/WikipediaApi.csproj b/WikipediaApi/WikipediaApi.csproj index 9a86577..8198ca2 100644 --- a/WikipediaApi/WikipediaApi.csproj +++ b/WikipediaApi/WikipediaApi.csproj @@ -3,6 +3,6 @@ netcoreapp2.1 - + \ No newline at end of file From 2b90358ca52ba06a1e16963400bf4b56dac5fc70 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Sat, 2 Mar 2019 00:50:54 -0500 Subject: [PATCH 125/443] Remove Bugsnag --- Geekbot.net/Geekbot.net.csproj | 2 - Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs | 67 +++---------------- 2 files changed, 10 insertions(+), 59 deletions(-) diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 0ed34e8..9e98f9b 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -19,8 +19,6 @@ true - - 2.0.1 diff --git a/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs b/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs index 7d5deec..c6e1f8d 100644 --- a/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs +++ b/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs @@ -1,9 +1,6 @@ using System; -using System.Collections.Generic; using System.Net; using System.Threading.Tasks; -using Bugsnag; -using Bugsnag.Payload; using Discord.Commands; using Discord.Net; using Geekbot.net.Lib.GlobalSettings; @@ -21,7 +18,6 @@ namespace Geekbot.net.Lib.ErrorHandling private readonly ITranslationHandler _translation; private readonly IRavenClient _raven; private readonly bool _errorsInChat; - private readonly IClient _bugsnag; public ErrorHandler(IGeekbotLogger logger, ITranslationHandler translation, IGlobalSettings globalSettings, bool errorsInChat) { @@ -39,21 +35,6 @@ namespace Geekbot.net.Lib.ErrorHandling { _raven = null; } - - var bugsnagApiKey = globalSettings.GetKey("BugsnagApiKey"); - if (!string.IsNullOrEmpty(bugsnagApiKey)) - { - _bugsnag = new Bugsnag.Client(new Bugsnag.Configuration - { - ApiKey = bugsnagApiKey, - AppVersion = Constants.BotVersion() - }); - _logger.Information(LogSource.Geekbot, "Command Errors will be logged to Bugsnag"); - } - else - { - _bugsnag = null; - } } public async Task HandleCommandException(Exception e, ICommandContext context, string errorMessage = "def") @@ -109,46 +90,18 @@ namespace Geekbot.net.Lib.ErrorHandling private void ReportExternal(Exception e, MessageDto errorObj) { - if (_raven != null) + if (_raven == null) return; + var sentryEvent = new SentryEvent(e) { - var sentryEvent = new SentryEvent(e) + Tags = { - Tags = - { - ["discord_server"] = errorObj.Guild.Name, - ["discord_user"] = errorObj.User.Name - }, - Message = errorObj.Message.Content, - Extra = errorObj - }; - _raven.Capture(sentryEvent); - } - - _bugsnag?.Notify(e, (report) => - { - report.Event.Metadata.Add("Discord Location", new Dictionary - { - {"Guild Name", errorObj.Guild.Name}, - {"Guild Id", errorObj.Guild.Id}, - {"Channel Name", errorObj.Channel.Name}, - {"Channel Id", errorObj.Channel.Id} - }); - report.Event.Metadata.Add("Message Info", new Dictionary - { - {"Content", errorObj.Message.Content}, - {"Id", errorObj.Message.Id}, - {"Attachments", errorObj.Message.Attachments.ToString()}, - {"ChannelMentions", errorObj.Message.ChannelMentions.ToString()}, - {"UserMentions", errorObj.Message.UserMentions.ToString()}, - {"RoleMentions", errorObj.Message.RoleMentions.ToString()}, - }); - report.Event.Severity = Severity.Error; - report.Event.User = new User - { - Id = errorObj.User.Id, - Name = errorObj.User.Name - }; - }); + ["discord_server"] = errorObj.Guild.Name, + ["discord_user"] = errorObj.User.Name + }, + Message = errorObj.Message.Content, + Extra = errorObj + }; + _raven.Capture(sentryEvent); } } } \ No newline at end of file From 35a4f4a122bd35be32d9d3a5c706c38df5d9c9da Mon Sep 17 00:00:00 2001 From: Runebaas Date: Sat, 2 Mar 2019 01:21:42 -0500 Subject: [PATCH 126/443] Limit MTG Page Size to 1 card --- Geekbot.net/Commands/Integrations/MagicTheGathering.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Geekbot.net/Commands/Integrations/MagicTheGathering.cs b/Geekbot.net/Commands/Integrations/MagicTheGathering.cs index bb95eb7..9ccab39 100644 --- a/Geekbot.net/Commands/Integrations/MagicTheGathering.cs +++ b/Geekbot.net/Commands/Integrations/MagicTheGathering.cs @@ -29,7 +29,11 @@ namespace Geekbot.net.Commands.Integrations try { var service = new CardService(); - var result = service.Where(x => x.Name, cardName); + var result = service + .Where(x => x.Name, cardName) + // fewer cards less risk of deserialization problems, don't need more than one anyways... + // ToDo: fix the deserialization issue in card[n].foreignNames[] + .Where(x => x.PageSize, 1); var card = result.All().Value.FirstOrDefault(); if (card == null) From 809adee77854a12bdfd481f73e8c48ede28e6517 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Sat, 2 Mar 2019 01:57:43 -0500 Subject: [PATCH 127/443] Add Sentry Release Reporting (attempt 1) --- .gitlab-ci.yml | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 1ba3063..9f8432a 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,5 +1,6 @@ stages: - build + - ops - deploy before_script: @@ -19,6 +20,17 @@ build: - dotnet test Tests - dotnet publish --version-suffix ${CI_COMMIT_SHA:0:8} --configuration Release -o Binaries ./ +sentry: + stage: ops + image: getsentry/sentry-cli + only: + - master + dependencies: + - build + script: + - sentry-cli releases new -p geekbot 4.0.0-${CI_COMMIT_SHA:0:8} + - sentry-cli releases set-commits --auto 4.0.0-${CI_COMMIT_SHA:0:8} + deploy: stage: deploy image: runebaas/rsync-ssh-git @@ -26,6 +38,7 @@ deploy: - master dependencies: - build + - sentry environment: name: Production url: https://discordapp.com/oauth2/authorize?client_id=171249478546882561&scope=bot&permissions=1416834054 @@ -44,6 +57,8 @@ mirror: image: runebaas/rsync-ssh-git only: - master + dependencies: + - build + - sentry script: - git push https://runebaas:$TOKEN@github.com/pizzaandcoffee/Geekbot.net.git origin/master:master -f - \ No newline at end of file From 3a6dd8ea1000eb6b50c54b557d8aebf232b12cc4 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Sat, 2 Mar 2019 02:06:46 -0500 Subject: [PATCH 128/443] Add Sentry Release Reporting (attempt 2) --- .gitlab-ci.yml | 7 +++++-- Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 9f8432a..42eaca6 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -23,13 +23,16 @@ build: sentry: stage: ops image: getsentry/sentry-cli + variables: + VERSION: 4.0.0-${CI_COMMIT_SHA:0:8} only: - master dependencies: - build script: - - sentry-cli releases new -p geekbot 4.0.0-${CI_COMMIT_SHA:0:8} - - sentry-cli releases set-commits --auto 4.0.0-${CI_COMMIT_SHA:0:8} + - sentry-cli releases new -p geekbot $VERSION + - sentry-cli releases set-commits --auto $VERSION + - sentry-cli releases deploys $VERSION new -e Production deploy: stage: deploy diff --git a/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs b/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs index c6e1f8d..5323ee6 100644 --- a/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs +++ b/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs @@ -28,7 +28,7 @@ namespace Geekbot.net.Lib.ErrorHandling var sentryDsn = Environment.GetEnvironmentVariable("SENTRY"); if (!string.IsNullOrEmpty(sentryDsn)) { - _raven = new RavenClient(sentryDsn) { Release = Constants.BotVersion() }; + _raven = new RavenClient(sentryDsn) { Release = Constants.BotVersion(), Environment = "Production" }; _logger.Information(LogSource.Geekbot, $"Command Errors will be logged to Sentry: {sentryDsn}"); } else From 3f083b520d3d676cd5856cff2de7729c94d00753 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Sat, 2 Mar 2019 02:12:03 -0500 Subject: [PATCH 129/443] Add Sentry Release Reporting (attempt 3) --- .gitlab-ci.yml | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 42eaca6..542c761 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -23,16 +23,14 @@ build: sentry: stage: ops image: getsentry/sentry-cli - variables: - VERSION: 4.0.0-${CI_COMMIT_SHA:0:8} only: - master dependencies: - build script: - - sentry-cli releases new -p geekbot $VERSION - - sentry-cli releases set-commits --auto $VERSION - - sentry-cli releases deploys $VERSION new -e Production + - sentry-cli releases new -p geekbot 4.0.0-${CI_COMMIT_SHA:0:8} + - sentry-cli releases set-commits --auto 4.0.0-${CI_COMMIT_SHA:0:8} + - sentry-cli releases deploys 4.0.0-${CI_COMMIT_SHA:0:8} new -e Production deploy: stage: deploy From a1e0c35dd4b887919a2216f8d1e4ce2ed7da49eb Mon Sep 17 00:00:00 2001 From: Runebaas Date: Sun, 3 Mar 2019 20:27:50 -0500 Subject: [PATCH 130/443] Upgrade to DotNet Core 2.2 --- Geekbot.net/Geekbot.net.csproj | 31 +++++++++++++------------ Geekbot.net/Lib/Logger/LoggerFactory.cs | 3 +-- Tests/Tests.csproj | 4 ++-- WikipediaApi/WikipediaApi.csproj | 2 +- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 9e98f9b..9ea7546 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -1,12 +1,13 @@  Exe - netcoreapp2.1 + netcoreapp2.2 win-x64;linux-x64 derp.ico 4.0.0 $(VersionSuffix) $(Version)-$(VersionSuffix) + $(Version)-DEV Pizza and Coffee Studios Pizza and Coffee Studios A Discord bot @@ -25,30 +26,30 @@ - + - - - - - - - - - - - + + + + + + + + + + + - + - + 4.3.0 diff --git a/Geekbot.net/Lib/Logger/LoggerFactory.cs b/Geekbot.net/Lib/Logger/LoggerFactory.cs index 9c5f759..b07bcda 100644 --- a/Geekbot.net/Lib/Logger/LoggerFactory.cs +++ b/Geekbot.net/Lib/Logger/LoggerFactory.cs @@ -25,8 +25,7 @@ namespace Geekbot.net.Lib.Logger Layout = "${message}", UseConsoleLog = false, OptimizeBufferReuse = true, - Name = "Geekbot", - AppendException = false + Name = "Geekbot" }) ); } diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 094bf84..1e26252 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -1,11 +1,11 @@  - netcoreapp2.1 + netcoreapp2.2 false NU1701 - + diff --git a/WikipediaApi/WikipediaApi.csproj b/WikipediaApi/WikipediaApi.csproj index 8198ca2..79206c2 100644 --- a/WikipediaApi/WikipediaApi.csproj +++ b/WikipediaApi/WikipediaApi.csproj @@ -1,6 +1,6 @@  - netcoreapp2.1 + netcoreapp2.2 From dbd36a62d31b371962e139cd14903cda9af29de6 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Sun, 3 Mar 2019 20:38:45 -0500 Subject: [PATCH 131/443] Disable Stats Updater when using the InMemory database --- Geekbot.net/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index e2cff4f..7b00055 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -174,13 +174,13 @@ namespace Geekbot.net var handlers = new Handlers(_databaseInitializer, _client, _logger, _redis, _servicesProvider, _commands, _userRepository, reactionListener); _client.MessageReceived += handlers.RunCommand; - _client.MessageReceived += handlers.UpdateStats; _client.MessageDeleted += handlers.MessageDeleted; _client.UserJoined += handlers.UserJoined; _client.UserUpdated += handlers.UserUpdated; _client.UserLeft += handlers.UserLeft; _client.ReactionAdded += handlers.ReactionAdded; _client.ReactionRemoved += handlers.ReactionRemoved; + if (!_runParameters.InMemory) _client.MessageReceived += handlers.UpdateStats; var webserver = _runParameters.DisableApi ? Task.Delay(10) : StartWebApi(); From f4897f7f2ee9aced537a91cc7e580cb0c6f3ca18 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Sun, 3 Mar 2019 20:39:31 -0500 Subject: [PATCH 132/443] Comment out unused code in Poll.cs --- Geekbot.net/Commands/Utils/Poll.cs | 110 ++++++++++++++--------------- 1 file changed, 55 insertions(+), 55 deletions(-) diff --git a/Geekbot.net/Commands/Utils/Poll.cs b/Geekbot.net/Commands/Utils/Poll.cs index c5a0119..598e78c 100644 --- a/Geekbot.net/Commands/Utils/Poll.cs +++ b/Geekbot.net/Commands/Utils/Poll.cs @@ -62,61 +62,61 @@ namespace Geekbot.net.Commands.Utils await ReplyAsync("Poll creation currently disabled"); return; - var currentPoll = GetCurrentPoll(); - if (currentPoll.Question != null && !currentPoll.IsFinshed) - { - await ReplyAsync("You have not finished you last poll yet. To finish it use `!poll end`"); - return; - } - - var pollList = rawPollString.Split(';').ToList(); - if (pollList.Count <= 2) - { - await ReplyAsync( - "You need a question with atleast 2 options, a valid creation would look like this `question;option1;option2`"); - return; - } - - var question = pollList[0]; - pollList.RemoveAt(0); - - var eb = new EmbedBuilder - { - Title = $"Poll by {Context.User.Username}", - Description = question - }; - - var options = new List(); - var i = 1; - pollList.ForEach(option => - { - options.Add(new PollQuestionModel() - { - OptionId = i, - OptionText = option - }); - eb.AddInlineField($"Option {_emojiConverter.NumberToEmoji(i)}", option); - i++; - }); - var pollMessage = await ReplyAsync("", false, eb.Build()); - - var poll = new PollModel() - { - Creator = Context.User.Id.AsLong(), - MessageId = pollMessage.Id.AsLong(), - IsFinshed = false, - Question = question, - Options = options - }; - _database.Polls.Add(poll); - - i = 1; - pollList.ForEach(option => - { - pollMessage.AddReactionAsync(new Emoji(_emojiConverter.NumberToEmoji(i))); - Task.Delay(500); - i++; - }); +// var currentPoll = GetCurrentPoll(); +// if (currentPoll.Question != null && !currentPoll.IsFinshed) +// { +// await ReplyAsync("You have not finished you last poll yet. To finish it use `!poll end`"); +// return; +// } +// +// var pollList = rawPollString.Split(';').ToList(); +// if (pollList.Count <= 2) +// { +// await ReplyAsync( +// "You need a question with atleast 2 options, a valid creation would look like this `question;option1;option2`"); +// return; +// } +// +// var question = pollList[0]; +// pollList.RemoveAt(0); +// +// var eb = new EmbedBuilder +// { +// Title = $"Poll by {Context.User.Username}", +// Description = question +// }; +// +// var options = new List(); +// var i = 1; +// pollList.ForEach(option => +// { +// options.Add(new PollQuestionModel() +// { +// OptionId = i, +// OptionText = option +// }); +// eb.AddInlineField($"Option {_emojiConverter.NumberToEmoji(i)}", option); +// i++; +// }); +// var pollMessage = await ReplyAsync("", false, eb.Build()); +// +// var poll = new PollModel() +// { +// Creator = Context.User.Id.AsLong(), +// MessageId = pollMessage.Id.AsLong(), +// IsFinshed = false, +// Question = question, +// Options = options +// }; +// _database.Polls.Add(poll); +// +// i = 1; +// pollList.ForEach(option => +// { +// pollMessage.AddReactionAsync(new Emoji(_emojiConverter.NumberToEmoji(i))); +// Task.Delay(500); +// i++; +// }); } catch (Exception e) { From c1ff11ca34663245a4f10f74280ec50b0f7393d0 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Sun, 3 Mar 2019 21:01:47 -0500 Subject: [PATCH 133/443] Fix bug in TranslationsHandler where language lookup would fail if user messaged the bot directly --- .../Lib/Localization/TranslationHandler.cs | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/Geekbot.net/Lib/Localization/TranslationHandler.cs b/Geekbot.net/Lib/Localization/TranslationHandler.cs index 88e518e..a1b650b 100644 --- a/Geekbot.net/Lib/Localization/TranslationHandler.cs +++ b/Geekbot.net/Lib/Localization/TranslationHandler.cs @@ -18,8 +18,7 @@ namespace Geekbot.net.Lib.Localization private readonly IGeekbotLogger _logger; private readonly Dictionary _serverLanguages; private Dictionary>> _translations; - private List _supportedLanguages; - + public TranslationHandler(DatabaseContext database, IGeekbotLogger logger) { _database = database; @@ -45,8 +44,10 @@ namespace Geekbot.net.Lib.Localization if (!sortedPerLanguage.ContainsKey(lang.Key)) { var commandDict = new Dictionary>(); - var strDict = new Dictionary(); - strDict.Add(str.Key, lang.Value); + var strDict = new Dictionary + { + {str.Key, lang.Value} + }; commandDict.Add(command.Key, strDict); sortedPerLanguage.Add(lang.Key, commandDict); } @@ -65,10 +66,10 @@ namespace Geekbot.net.Lib.Localization } _translations = sortedPerLanguage; - _supportedLanguages = new List(); + SupportedLanguages = new List(); foreach (var lang in sortedPerLanguage) { - _supportedLanguages.Add(lang.Key); + SupportedLanguages.Add(lang.Key); } } catch (Exception e) @@ -101,7 +102,7 @@ namespace Geekbot.net.Lib.Localization } catch (Exception e) { - _logger.Error(LogSource.Geekbot, "Could not get guild langage", e); + _logger.Error(LogSource.Geekbot, "Could not get guild language", e); return "EN"; } } @@ -123,7 +124,8 @@ namespace Geekbot.net.Lib.Localization try { var command = context.Message.Content.Split(' ').First().TrimStart('!').ToLower(); - return _translations[await GetServerLanguage(context.Guild.Id)][command]; + var serverLanguage = await GetServerLanguage(context.Guild?.Id ?? 0); + return _translations[serverLanguage][command]; } catch (Exception e) { @@ -136,7 +138,8 @@ namespace Geekbot.net.Lib.Localization { try { - return _translations[await GetServerLanguage(context.Guild.Id)][command]; + var serverLanguage = await GetServerLanguage(context.Guild?.Id ?? 0); + return _translations[serverLanguage][command]; } catch (Exception e) { @@ -149,7 +152,7 @@ namespace Geekbot.net.Lib.Localization { try { - if (!_supportedLanguages.Contains(language)) return false; + if (!SupportedLanguages.Contains(language)) return false; var guild = await GetGuild(guildId); guild.Language = language; _database.GuildSettings.Update(guild); @@ -163,7 +166,7 @@ namespace Geekbot.net.Lib.Localization } } - public List SupportedLanguages => _supportedLanguages; + public List SupportedLanguages { get; private set; } private async Task GetGuild(ulong guildId) { From 7dbb2ab89f86473766746613dcab5ad3e0b53d5b Mon Sep 17 00:00:00 2001 From: Runebaas Date: Sun, 3 Mar 2019 21:14:17 -0500 Subject: [PATCH 134/443] Tell the user if the mal api refuses to answer --- Geekbot.net/Commands/Integrations/Mal.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Geekbot.net/Commands/Integrations/Mal.cs b/Geekbot.net/Commands/Integrations/Mal.cs index 88f500e..1ad50bb 100644 --- a/Geekbot.net/Commands/Integrations/Mal.cs +++ b/Geekbot.net/Commands/Integrations/Mal.cs @@ -1,6 +1,7 @@ using System; using System.Threading.Tasks; using System.Web; +using System.Xml; using Discord; using Discord.Commands; using Geekbot.net.Lib.Clients; @@ -62,6 +63,10 @@ namespace Geekbot.net.Commands.Integrations "Unfortunally i'm not connected to MyAnimeList.net, please tell my senpai to connect me"); } } + catch (XmlException e) + { + await _errorHandler.HandleCommandException(e, Context, "The MyAnimeList.net API refused to answer"); + } catch (Exception e) { await _errorHandler.HandleCommandException(e, Context); @@ -110,6 +115,10 @@ namespace Geekbot.net.Commands.Integrations "Unfortunally i'm not connected to MyAnimeList.net, please tell my senpai to connect me"); } } + catch (XmlException e) + { + await _errorHandler.HandleCommandException(e, Context, "The MyAnimeList.net API refused to answer"); + } catch (Exception e) { await _errorHandler.HandleCommandException(e, Context); From 0ae9dcce67d40620f85e648ee2311ce163a8b8e6 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Mon, 4 Mar 2019 15:41:09 -0500 Subject: [PATCH 135/443] Update build image for gitlab-ci to match dotnet 2.2 --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 542c761..a348503 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -10,7 +10,7 @@ before_script: build: stage: build - image: microsoft/dotnet:2.1.401-sdk-stretch + image: mcr.microsoft.com/dotnet/core/sdk:2.2 artifacts: expire_in: 1h paths: From 3d493fa5315931f518cb9569fcf3d20050d676b5 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Sun, 17 Mar 2019 17:52:07 +0100 Subject: [PATCH 136/443] Add kanye command --- .../Commands/Randomness/Kanye/Kanye.cs | 53 +++++++++++++++++++ .../Randomness/Kanye/KanyeResponseDto.cs | 8 +++ 2 files changed, 61 insertions(+) create mode 100644 Geekbot.net/Commands/Randomness/Kanye/Kanye.cs create mode 100644 Geekbot.net/Commands/Randomness/Kanye/KanyeResponseDto.cs diff --git a/Geekbot.net/Commands/Randomness/Kanye/Kanye.cs b/Geekbot.net/Commands/Randomness/Kanye/Kanye.cs new file mode 100644 index 0000000..42a6ca7 --- /dev/null +++ b/Geekbot.net/Commands/Randomness/Kanye/Kanye.cs @@ -0,0 +1,53 @@ +using System; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Threading.Tasks; +using Discord.Commands; +using Geekbot.net.Commands.Randomness.Dad; +using Geekbot.net.Lib.ErrorHandling; +using Microsoft.AspNetCore.Hosting.Internal; +using Newtonsoft.Json; + +namespace Geekbot.net.Commands.Randomness.Kanye +{ + public class Kanye : ModuleBase + { + private readonly IErrorHandler _errorHandler; + + public Kanye(IErrorHandler errorHandler) + { + _errorHandler = errorHandler; + } + + [Command("kanye", RunMode = RunMode.Async)] + [Summary("A random kayne west quote")] + public async Task Say() + { + try + { + using (var client = new HttpClient()) + { + try + { + client.DefaultRequestHeaders.Accept.Clear(); + client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json")); + var response = await client.GetAsync("https://api.kanye.rest/"); + response.EnsureSuccessStatusCode(); + + var stringResponse = await response.Content.ReadAsStringAsync(); + var data = JsonConvert.DeserializeObject(stringResponse); + await ReplyAsync(data.Quote); + } + catch (HttpRequestException) + { + await ReplyAsync("Api down..."); + } + } + } + catch (Exception e) + { + await _errorHandler.HandleCommandException(e, Context); + } + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/Randomness/Kanye/KanyeResponseDto.cs b/Geekbot.net/Commands/Randomness/Kanye/KanyeResponseDto.cs new file mode 100644 index 0000000..ff74f37 --- /dev/null +++ b/Geekbot.net/Commands/Randomness/Kanye/KanyeResponseDto.cs @@ -0,0 +1,8 @@ +namespace Geekbot.net.Commands.Randomness.Kanye +{ + public class KanyeResponseDto + { + public string Id { get; set; } + public string Quote { get; set; } + } +} \ No newline at end of file From 1e98b44cb7392b76eb9a75cd0c5c5c37bdf41f58 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Sun, 17 Mar 2019 18:32:56 +0100 Subject: [PATCH 137/443] Add Attribute to disable commands in DMs --- .../DisableInDirectMessageAttribute.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Geekbot.net/Lib/CommandPreconditions/DisableInDirectMessageAttribute.cs diff --git a/Geekbot.net/Lib/CommandPreconditions/DisableInDirectMessageAttribute.cs b/Geekbot.net/Lib/CommandPreconditions/DisableInDirectMessageAttribute.cs new file mode 100644 index 0000000..110b69b --- /dev/null +++ b/Geekbot.net/Lib/CommandPreconditions/DisableInDirectMessageAttribute.cs @@ -0,0 +1,16 @@ +using System; +using System.Threading.Tasks; +using Discord.Commands; + +namespace Geekbot.net.Lib.CommandPreconditions +{ + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)] + public class DisableInDirectMessageAttribute : PreconditionAttribute + { + public override Task CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services) + { + var result = context.Guild.Id != 0 ? PreconditionResult.FromSuccess() : PreconditionResult.FromError("Command unavailable in Direct Messaging"); + return Task.FromResult(result); + } + } +} \ No newline at end of file From de408dbfd999c11c5daadc85df708a32f60ddb22 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Sun, 17 Mar 2019 18:33:18 +0100 Subject: [PATCH 138/443] Apply DM Disable attribute to some commands --- Geekbot.net/Commands/Admin/Admin.cs | 2 ++ Geekbot.net/Commands/Admin/Mod.cs | 2 ++ Geekbot.net/Commands/Admin/Role.cs | 2 ++ Geekbot.net/Commands/User/GuildInfo.cs | 2 ++ Geekbot.net/Commands/User/Karma.cs | 2 ++ Geekbot.net/Commands/User/Ranking/Rank.cs | 2 ++ Geekbot.net/Commands/User/Stats.cs | 2 ++ Geekbot.net/Commands/Utils/Poll.cs | 2 ++ Geekbot.net/Commands/Utils/Quote/Quote.cs | 2 ++ 9 files changed, 18 insertions(+) diff --git a/Geekbot.net/Commands/Admin/Admin.cs b/Geekbot.net/Commands/Admin/Admin.cs index fd7bbc6..f60e5df 100644 --- a/Geekbot.net/Commands/Admin/Admin.cs +++ b/Geekbot.net/Commands/Admin/Admin.cs @@ -7,6 +7,7 @@ using Discord.Commands; using Discord.WebSocket; using Geekbot.net.Database; using Geekbot.net.Database.Models; +using Geekbot.net.Lib.CommandPreconditions; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Localization; @@ -15,6 +16,7 @@ namespace Geekbot.net.Commands.Admin { [Group("admin")] [RequireUserPermission(GuildPermission.Administrator)] + [DisableInDirectMessage] public class Admin : ModuleBase { private readonly DiscordSocketClient _client; diff --git a/Geekbot.net/Commands/Admin/Mod.cs b/Geekbot.net/Commands/Admin/Mod.cs index e1653e9..6c6be5a 100644 --- a/Geekbot.net/Commands/Admin/Mod.cs +++ b/Geekbot.net/Commands/Admin/Mod.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Discord.WebSocket; +using Geekbot.net.Lib.CommandPreconditions; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.UserRepository; @@ -13,6 +14,7 @@ namespace Geekbot.net.Commands.Admin [RequireUserPermission(GuildPermission.KickMembers)] [RequireUserPermission(GuildPermission.ManageMessages)] [RequireUserPermission(GuildPermission.ManageRoles)] + [DisableInDirectMessage] public class Mod : ModuleBase { private readonly DiscordSocketClient _client; diff --git a/Geekbot.net/Commands/Admin/Role.cs b/Geekbot.net/Commands/Admin/Role.cs index a76c9d5..aa767d1 100644 --- a/Geekbot.net/Commands/Admin/Role.cs +++ b/Geekbot.net/Commands/Admin/Role.cs @@ -7,6 +7,7 @@ using Discord.Commands; using Discord.Net; using Geekbot.net.Database; using Geekbot.net.Database.Models; +using Geekbot.net.Lib.CommandPreconditions; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.ReactionListener; @@ -14,6 +15,7 @@ using Geekbot.net.Lib.ReactionListener; namespace Geekbot.net.Commands.Admin { [Group("role")] + [DisableInDirectMessage] public class Role : ModuleBase { private readonly DatabaseContext _database; diff --git a/Geekbot.net/Commands/User/GuildInfo.cs b/Geekbot.net/Commands/User/GuildInfo.cs index 342f80f..6c8b941 100644 --- a/Geekbot.net/Commands/User/GuildInfo.cs +++ b/Geekbot.net/Commands/User/GuildInfo.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Geekbot.net.Database; +using Geekbot.net.Lib.CommandPreconditions; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Levels; @@ -25,6 +26,7 @@ namespace Geekbot.net.Commands.User [Command("serverstats", RunMode = RunMode.Async)] [Summary("Show some info about the bot.")] + [DisableInDirectMessage] public async Task GetInfo() { try diff --git a/Geekbot.net/Commands/User/Karma.cs b/Geekbot.net/Commands/User/Karma.cs index 39b05be..e2a4739 100644 --- a/Geekbot.net/Commands/User/Karma.cs +++ b/Geekbot.net/Commands/User/Karma.cs @@ -5,12 +5,14 @@ using Discord; using Discord.Commands; using Geekbot.net.Database; using Geekbot.net.Database.Models; +using Geekbot.net.Lib.CommandPreconditions; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Localization; namespace Geekbot.net.Commands.User { + [DisableInDirectMessage] public class Karma : ModuleBase { private readonly IErrorHandler _errorHandler; diff --git a/Geekbot.net/Commands/User/Ranking/Rank.cs b/Geekbot.net/Commands/User/Ranking/Rank.cs index 9c37a4a..9a08d98 100644 --- a/Geekbot.net/Commands/User/Ranking/Rank.cs +++ b/Geekbot.net/Commands/User/Ranking/Rank.cs @@ -5,6 +5,7 @@ using System.Text; using System.Threading.Tasks; using Discord.Commands; using Geekbot.net.Database; +using Geekbot.net.Lib.CommandPreconditions; using Geekbot.net.Lib.Converters; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; @@ -33,6 +34,7 @@ namespace Geekbot.net.Commands.User.Ranking [Command("rank", RunMode = RunMode.Async)] [Summary("get user top 10 in messages or karma")] + [DisableInDirectMessage] public async Task RankCmd([Summary("type")] string typeUnformated = "messages", [Summary("amount")] int amount = 10) { try diff --git a/Geekbot.net/Commands/User/Stats.cs b/Geekbot.net/Commands/User/Stats.cs index a230456..4145d03 100644 --- a/Geekbot.net/Commands/User/Stats.cs +++ b/Geekbot.net/Commands/User/Stats.cs @@ -5,6 +5,7 @@ using Discord; using Discord.Commands; using Geekbot.net.Database; using Geekbot.net.Lib.AlmostRedis; +using Geekbot.net.Lib.CommandPreconditions; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Levels; @@ -28,6 +29,7 @@ namespace Geekbot.net.Commands.User [Command("stats", RunMode = RunMode.Async)] [Summary("Get information about this user")] + [DisableInDirectMessage] public async Task User([Summary("@someone")] IUser user = null) { try diff --git a/Geekbot.net/Commands/Utils/Poll.cs b/Geekbot.net/Commands/Utils/Poll.cs index 598e78c..617d6e2 100644 --- a/Geekbot.net/Commands/Utils/Poll.cs +++ b/Geekbot.net/Commands/Utils/Poll.cs @@ -7,6 +7,7 @@ using Discord; using Discord.Commands; using Geekbot.net.Database; using Geekbot.net.Database.Models; +using Geekbot.net.Lib.CommandPreconditions; using Geekbot.net.Lib.Converters; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; @@ -15,6 +16,7 @@ using Geekbot.net.Lib.UserRepository; namespace Geekbot.net.Commands.Utils { [Group("poll")] + [DisableInDirectMessage] public class Poll : ModuleBase { private readonly IEmojiConverter _emojiConverter; diff --git a/Geekbot.net/Commands/Utils/Quote/Quote.cs b/Geekbot.net/Commands/Utils/Quote/Quote.cs index 9a1d072..e2456e7 100644 --- a/Geekbot.net/Commands/Utils/Quote/Quote.cs +++ b/Geekbot.net/Commands/Utils/Quote/Quote.cs @@ -5,6 +5,7 @@ using Discord; using Discord.Commands; using Geekbot.net.Database; using Geekbot.net.Database.Models; +using Geekbot.net.Lib.CommandPreconditions; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Polyfills; @@ -12,6 +13,7 @@ using Geekbot.net.Lib.Polyfills; namespace Geekbot.net.Commands.Utils.Quote { [Group("quote")] + [DisableInDirectMessage] public class Quote : ModuleBase { private readonly IErrorHandler _errorHandler; From 99ece47925a80086638c6eae70c4c05229818a03 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Sun, 17 Mar 2019 18:38:33 +0100 Subject: [PATCH 139/443] Release 4.1.0 --- .gitlab-ci.yml | 6 +++--- Geekbot.net/Geekbot.net.csproj | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a348503..8c7a530 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -28,9 +28,9 @@ sentry: dependencies: - build script: - - sentry-cli releases new -p geekbot 4.0.0-${CI_COMMIT_SHA:0:8} - - sentry-cli releases set-commits --auto 4.0.0-${CI_COMMIT_SHA:0:8} - - sentry-cli releases deploys 4.0.0-${CI_COMMIT_SHA:0:8} new -e Production + - sentry-cli releases new -p geekbot 4.1.0-${CI_COMMIT_SHA:0:8} + - sentry-cli releases set-commits --auto 4.1.0-${CI_COMMIT_SHA:0:8} + - sentry-cli releases deploys 4.1.0-${CI_COMMIT_SHA:0:8} new -e Production deploy: stage: deploy diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 9ea7546..c0bdca2 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -4,7 +4,7 @@ netcoreapp2.2 win-x64;linux-x64 derp.ico - 4.0.0 + 4.1.0 $(VersionSuffix) $(Version)-$(VersionSuffix) $(Version)-DEV From 8f7aa097632771a8c0932fa24e41f23e6b97dd45 Mon Sep 17 00:00:00 2001 From: Daan Date: Thu, 11 Apr 2019 09:33:03 +0000 Subject: [PATCH 140/443] Add SAST to the build pipeline --- .gitlab-ci.yml | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 8c7a530..9149e9c 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,5 +1,6 @@ stages: - build + - security - ops - deploy @@ -20,6 +21,36 @@ build: - dotnet test Tests - dotnet publish --version-suffix ${CI_COMMIT_SHA:0:8} --configuration Release -o Binaries ./ +sast: + stage: security + image: docker:stable + variables: + DOCKER_DRIVER: overlay2 + allow_failure: true + services: + - docker:stable-dind + script: + - export SAST_VERSION=${SP_VERSION:-$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')} + - | + docker run \ + --env SAST_ANALYZER_IMAGES \ + --env SAST_ANALYZER_IMAGE_PREFIX \ + --env SAST_ANALYZER_IMAGE_TAG \ + --env SAST_DEFAULT_ANALYZERS \ + --env SAST_BRAKEMAN_LEVEL \ + --env SAST_GOSEC_LEVEL \ + --env SAST_FLAWFINDER_LEVEL \ + --env SAST_DOCKER_CLIENT_NEGOTIATION_TIMEOUT \ + --env SAST_PULL_ANALYZER_IMAGE_TIMEOUT \ + --env SAST_RUN_ANALYZER_TIMEOUT \ + --volume "$PWD:/code" \ + --volume /var/run/docker.sock:/var/run/docker.sock \ + "registry.gitlab.com/gitlab-org/security-products/sast:$SAST_VERSION" /app/bin/run /code + dependencies: [] + artifacts: + reports: + sast: gl-sast-report.json + sentry: stage: ops image: getsentry/sentry-cli From d761e0bf10795336f3eac1a81b50d0affba8af41 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 1 May 2019 18:22:44 +0200 Subject: [PATCH 141/443] Add some dabbing --- Geekbot.net/Commands/Randomness/RandomAnimals.cs | 7 +++++++ Geekbot.net/Lib/Media/IMediaProvider.cs | 1 + Geekbot.net/Lib/Media/MediaProvider.cs | 14 ++++++++++++++ Geekbot.net/Storage/dab | 8 ++++++++ 4 files changed, 30 insertions(+) create mode 100644 Geekbot.net/Storage/dab diff --git a/Geekbot.net/Commands/Randomness/RandomAnimals.cs b/Geekbot.net/Commands/Randomness/RandomAnimals.cs index 38061b1..8562908 100644 --- a/Geekbot.net/Commands/Randomness/RandomAnimals.cs +++ b/Geekbot.net/Commands/Randomness/RandomAnimals.cs @@ -64,6 +64,13 @@ namespace Geekbot.net.Commands.Randomness { await ReplyAsync("", false, Eb(_mediaProvider.GetFox())); } + + [Command("dab", RunMode = RunMode.Async)] + [Summary("Get a random dab image")] + public async Task Dab() + { + await ReplyAsync("", false, Eb(_mediaProvider.GetDab())); + } private static Embed Eb(string image) { diff --git a/Geekbot.net/Lib/Media/IMediaProvider.cs b/Geekbot.net/Lib/Media/IMediaProvider.cs index 79c6c98..a646cea 100644 --- a/Geekbot.net/Lib/Media/IMediaProvider.cs +++ b/Geekbot.net/Lib/Media/IMediaProvider.cs @@ -10,5 +10,6 @@ string GetTurtle(); string GetPinguin(); string GetFox(); + string GetDab(); } } \ No newline at end of file diff --git a/Geekbot.net/Lib/Media/MediaProvider.cs b/Geekbot.net/Lib/Media/MediaProvider.cs index 7f883ae..c88b696 100644 --- a/Geekbot.net/Lib/Media/MediaProvider.cs +++ b/Geekbot.net/Lib/Media/MediaProvider.cs @@ -16,6 +16,7 @@ namespace Geekbot.net.Lib.Media private string[] _turtlesImages; private string[] _pinguinImages; private string[] _foxImages; + private string[] _dabImages; public MediaProvider(IGeekbotLogger logger) { @@ -32,6 +33,7 @@ namespace Geekbot.net.Lib.Media LoadTurtles(); LoadPinguins(); LoadFoxes(); + LoadDab(); } private void LoadCheckem() @@ -90,6 +92,13 @@ namespace Geekbot.net.Lib.Media _logger.Trace(LogSource.Geekbot, $"Loaded {_foxImages.Length} Foxes Images"); } + private void LoadDab() + { + var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/dab")); + _dabImages = rawLinks.Split("\n"); + _logger.Trace(LogSource.Geekbot, $"Loaded {_dabImages.Length} Dab Images"); + } + public string GetCheckem() { return _checkemImages[_random.Next(0, _checkemImages.Length)]; @@ -129,5 +138,10 @@ namespace Geekbot.net.Lib.Media { return _foxImages[_random.Next(0, _foxImages.Length)]; } + + public string GetDab() + { + return _dabImages[_random.Next(0, _dabImages.Length)]; + } } } \ No newline at end of file diff --git a/Geekbot.net/Storage/dab b/Geekbot.net/Storage/dab new file mode 100644 index 0000000..cba5a5c --- /dev/null +++ b/Geekbot.net/Storage/dab @@ -0,0 +1,8 @@ +https://pre00.deviantart.net/dcde/th/pre/i/2018/247/8/1/dabbing_pug_cute_dab_dance_by_manekibb-dcm2lvd.png +https://banner2.kisspng.com/20180625/xfv/kisspng-squidward-tentacles-dab-desktop-wallpaper-dab-emoji-5b31a97a839bf2.6353972915299813065391.jpg +https://djbooth.net/.image/t_share/MTUzNDg2MDIzOTU4NzM0NzA1/life-death-of-dab-dancejpg.jpg +https://res.cloudinary.com/teepublic/image/private/s--gfsWHvaH--/t_Preview/b_rgb:262c3a,c_limit,f_jpg,h_630,q_90,w_630/v1493209189/production/designs/1524888_1.jpg +https://i1.wp.com/fortniteskins.net/wp-content/uploads/2018/04/dab-skin-1.png?quality=90&strip=all&ssl=1 +https://i.pinimg.com/originals/12/d4/0a/12d40a8fc66b7a7ea8b9044ee0303974.png +https://images.bewakoof.com/t540/dab-penguin-boyfriend-t-shirt-women-s-printed-boyfriend-t-shirts-196918-1538034349.jpg +https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSKH1FLh5L3pOR2tamx-5OBqm_W2FFl8F7gteTAs2vMowwJ1Y32 \ No newline at end of file From 38fc6c7f93e6717bd945e3381e73cceec80b4214 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 1 May 2019 18:34:42 +0200 Subject: [PATCH 142/443] Copy dab file... --- Geekbot.net/Geekbot.net.csproj | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index c0bdca2..fcf7b95 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -93,6 +93,9 @@ Always + + PreserveNewest + From b01094f4c26b4c8f5eb05d2ec3368d4e8a1bc5a1 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 1 May 2019 18:38:02 +0200 Subject: [PATCH 143/443] Remove SAST from pipeline --- .gitlab-ci.yml | 31 ------------------------------- 1 file changed, 31 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 9149e9c..8c7a530 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,6 +1,5 @@ stages: - build - - security - ops - deploy @@ -21,36 +20,6 @@ build: - dotnet test Tests - dotnet publish --version-suffix ${CI_COMMIT_SHA:0:8} --configuration Release -o Binaries ./ -sast: - stage: security - image: docker:stable - variables: - DOCKER_DRIVER: overlay2 - allow_failure: true - services: - - docker:stable-dind - script: - - export SAST_VERSION=${SP_VERSION:-$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')} - - | - docker run \ - --env SAST_ANALYZER_IMAGES \ - --env SAST_ANALYZER_IMAGE_PREFIX \ - --env SAST_ANALYZER_IMAGE_TAG \ - --env SAST_DEFAULT_ANALYZERS \ - --env SAST_BRAKEMAN_LEVEL \ - --env SAST_GOSEC_LEVEL \ - --env SAST_FLAWFINDER_LEVEL \ - --env SAST_DOCKER_CLIENT_NEGOTIATION_TIMEOUT \ - --env SAST_PULL_ANALYZER_IMAGE_TIMEOUT \ - --env SAST_RUN_ANALYZER_TIMEOUT \ - --volume "$PWD:/code" \ - --volume /var/run/docker.sock:/var/run/docker.sock \ - "registry.gitlab.com/gitlab-org/security-products/sast:$SAST_VERSION" /app/bin/run /code - dependencies: [] - artifacts: - reports: - sast: gl-sast-report.json - sentry: stage: ops image: getsentry/sentry-cli From 119f4586c7932fa78618f4343d45256e64a8f11d Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 10 May 2019 20:51:13 +0200 Subject: [PATCH 144/443] Add Some Cookies --- Geekbot.net/Commands/Rpg/Cookies.cs | 95 +++++++++++++++++++++ Geekbot.net/Database/DatabaseContext.cs | 1 + Geekbot.net/Database/Models/CookiesModel.cs | 21 +++++ 3 files changed, 117 insertions(+) create mode 100644 Geekbot.net/Commands/Rpg/Cookies.cs create mode 100644 Geekbot.net/Database/Models/CookiesModel.cs diff --git a/Geekbot.net/Commands/Rpg/Cookies.cs b/Geekbot.net/Commands/Rpg/Cookies.cs new file mode 100644 index 0000000..c96d456 --- /dev/null +++ b/Geekbot.net/Commands/Rpg/Cookies.cs @@ -0,0 +1,95 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using Discord.Commands; +using Geekbot.net.Database; +using Geekbot.net.Database.Models; +using Geekbot.net.Lib.CommandPreconditions; +using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.Extensions; +using Geekbot.net.Lib.Localization; + +namespace Geekbot.net.Commands.Rpg +{ + [DisableInDirectMessage] + [Group("cookie")] + public class Cookies : ModuleBase + { + private readonly DatabaseContext _database; + private readonly IErrorHandler _errorHandler; + private readonly ITranslationHandler _translation; + + public Cookies(DatabaseContext database, IErrorHandler errorHandler, ITranslationHandler translation) + { + _database = database; + _errorHandler = errorHandler; + _translation = translation; + } + + [Command("get", RunMode = RunMode.Async)] + [Summary("Get a cookie every 24 hours")] + public async Task GetCookies() + { + try + { + var actor = await GetUser(Context.User.Id); + if (actor.LastPayout.Value.AddHours(24) > DateTimeOffset.Now) + { + await ReplyAsync($"You already got cookies in the last 24 hours, wait until {actor.LastPayout.Value.AddHours(24):HH:mm:ss} for more cookies"); + return; + } + actor.Cookies += 10; + actor.LastPayout = DateTimeOffset.Now; + await SetUser(actor); + await ReplyAsync($"You got 10 cookies, there are now {actor.Cookies} cookies in you cookie jar"); + + } + catch (Exception e) + { + await _errorHandler.HandleCommandException(e, Context); + } + } + + [Command("jar", RunMode = RunMode.Async)] + [Summary("Look at your cookie jar")] + public async Task PeekIntoCookieJar() + { + try + { + var actor = await GetUser(Context.User.Id); + await ReplyAsync($"There are {actor.Cookies} cookies in you cookie jar"); + + } + catch (Exception e) + { + await _errorHandler.HandleCommandException(e, Context); + } + } + + private async Task GetUser(ulong userId) + { + var user = _database.Cookies.FirstOrDefault(u =>u.GuildId.Equals(Context.Guild.Id.AsLong()) && u.UserId.Equals(userId.AsLong())) ?? await CreateNewRow(userId); + return user; + } + + private async Task SetUser(CookiesModel user) + { + _database.Cookies.Update(user); + await _database.SaveChangesAsync(); + } + + private async Task CreateNewRow(ulong userId) + { + var user = new CookiesModel() + { + GuildId = Context.Guild.Id.AsLong(), + UserId = userId.AsLong(), + Cookies = 0, + LastPayout = DateTimeOffset.MinValue + }; + var newUser = _database.Cookies.Add(user).Entity; + await _database.SaveChangesAsync(); + return newUser; + } + } +} diff --git a/Geekbot.net/Database/DatabaseContext.cs b/Geekbot.net/Database/DatabaseContext.cs index ab786b1..cbfa30d 100644 --- a/Geekbot.net/Database/DatabaseContext.cs +++ b/Geekbot.net/Database/DatabaseContext.cs @@ -17,5 +17,6 @@ namespace Geekbot.net.Database public DbSet Globals { get; set; } public DbSet RoleSelfService { get; set; } public DbSet Polls { get; set; } + public DbSet Cookies { get; set; } } } \ No newline at end of file diff --git a/Geekbot.net/Database/Models/CookiesModel.cs b/Geekbot.net/Database/Models/CookiesModel.cs new file mode 100644 index 0000000..228f3b0 --- /dev/null +++ b/Geekbot.net/Database/Models/CookiesModel.cs @@ -0,0 +1,21 @@ +using System; +using System.ComponentModel.DataAnnotations; + +namespace Geekbot.net.Database.Models +{ + public class CookiesModel + { + [Key] + public int Id { get; set; } + + [Required] + public long GuildId { get; set; } + + [Required] + public long UserId { get; set; } + + public int Cookies { get; set; } = 0; + + public DateTimeOffset? LastPayout { get; set; } + } +} \ No newline at end of file From efaaf594f0b2b048747576f2e67528c8514134b7 Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 10 May 2019 21:10:49 +0200 Subject: [PATCH 145/443] Make sure that stats works with the inmemory database and add cookies --- Geekbot.net/Commands/User/Stats.cs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/Geekbot.net/Commands/User/Stats.cs b/Geekbot.net/Commands/User/Stats.cs index 4145d03..6ddf7d4 100644 --- a/Geekbot.net/Commands/User/Stats.cs +++ b/Geekbot.net/Commands/User/Stats.cs @@ -42,17 +42,20 @@ namespace Geekbot.net.Commands.User var joinedDayAgo = Math.Floor((DateTime.Now - joinedAt).TotalDays); var messages = _database.Messages - .First(e => e.GuildId.Equals(Context.Guild.Id.AsLong()) && e.UserId.Equals(userInfo.Id.AsLong())) - .MessageCount; + ?.FirstOrDefault(e => e.GuildId.Equals(Context.Guild.Id.AsLong()) && e.UserId.Equals(userInfo.Id.AsLong())) + ?.MessageCount ?? 0; var guildMessages = _database.Messages - .Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong())) - .Select(e => e.MessageCount) - .Sum(); -// var messages = (int) _redis.Db.HashGet($"{Context.Guild.Id}:Messages", userInfo.Id.ToString()); -// var guildMessages = (int) _redis.Db.HashGet($"{Context.Guild.Id}:Messages", 0.ToString()); + ?.Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong())) + .Select(e => e.MessageCount) + .Sum() ?? 0; + var level = _levelCalc.GetLevel(messages); - var percent = Math.Round((double) (100 * messages) / guildMessages, 2); + var percent = Math.Round((double) (100 * messages) / guildMessages, digits: 2); + + var cookies = _database.Cookies + ?.FirstOrDefault(e => e.GuildId.Equals(Context.Guild.Id.AsLong()) && e.UserId.Equals(userInfo.Id.AsLong())) + ?.Cookies ?? 0; var eb = new EmbedBuilder(); eb.WithAuthor(new EmbedAuthorBuilder() @@ -77,6 +80,7 @@ namespace Geekbot.net.Commands.User .AddInlineField("Server Total", $"{percent}%"); if (correctRolls != null) eb.AddInlineField("Guessed Rolls", correctRolls.Rolls); + if (cookies > 0) eb.AddInlineField("Cookies", cookies); await ReplyAsync("", false, eb.Build()); } From d3038b90a8f5a7717699bbddfb1dfc00107c8d13 Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 10 May 2019 21:37:52 +0200 Subject: [PATCH 146/443] Add Cookies to the highscore --- Geekbot.net/Commands/User/Ranking/Rank.cs | 2 +- Geekbot.net/Lib/Highscores/HighscoreManager.cs | 12 ++++++++++++ Geekbot.net/Lib/Highscores/HighscoreTypes.cs | 3 ++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Geekbot.net/Commands/User/Ranking/Rank.cs b/Geekbot.net/Commands/User/Ranking/Rank.cs index 9a08d98..8a79fa6 100644 --- a/Geekbot.net/Commands/User/Ranking/Rank.cs +++ b/Geekbot.net/Commands/User/Ranking/Rank.cs @@ -46,7 +46,7 @@ namespace Geekbot.net.Commands.User.Ranking } catch { - await ReplyAsync("Valid types are '`messages`' '`karma`', '`rolls`'"); + await ReplyAsync("Valid types are '`messages`' '`karma`', '`rolls`' and '`cookies`'"); return; } diff --git a/Geekbot.net/Lib/Highscores/HighscoreManager.cs b/Geekbot.net/Lib/Highscores/HighscoreManager.cs index c684254..f3b3a18 100644 --- a/Geekbot.net/Lib/Highscores/HighscoreManager.cs +++ b/Geekbot.net/Lib/Highscores/HighscoreManager.cs @@ -34,6 +34,9 @@ namespace Geekbot.net.Lib.Highscores case HighscoreTypes.rolls: list = GetRollsList(guildId, amount); break; + case HighscoreTypes.cookies: + list = GetCookiesList(guildId, amount); + break; default: list = new Dictionary(); break; @@ -102,5 +105,14 @@ namespace Geekbot.net.Lib.Highscores .Take(amount) .ToDictionary(key => key.UserId.AsUlong(), key => key.Rolls); } + + public Dictionary GetCookiesList(ulong guildId, int amount) + { + return _database.Cookies + .Where(k => k.GuildId.Equals(guildId.AsLong())) + .OrderByDescending(o => o.Cookies) + .Take(amount) + .ToDictionary(key => key.UserId.AsUlong(), key => key.Cookies); + } } } \ No newline at end of file diff --git a/Geekbot.net/Lib/Highscores/HighscoreTypes.cs b/Geekbot.net/Lib/Highscores/HighscoreTypes.cs index 492d08d..adf5698 100644 --- a/Geekbot.net/Lib/Highscores/HighscoreTypes.cs +++ b/Geekbot.net/Lib/Highscores/HighscoreTypes.cs @@ -4,6 +4,7 @@ { messages, karma, - rolls + rolls, + cookies } } \ No newline at end of file From ba4de946dcd90f6da16f5bd7733a2f643e21b36e Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 10 May 2019 22:06:58 +0200 Subject: [PATCH 147/443] Add translations for cookies/guetzli --- Geekbot.net/Commands/Rpg/Cookies.cs | 9 +++++---- Geekbot.net/Lib/Localization/Translations.json | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Geekbot.net/Commands/Rpg/Cookies.cs b/Geekbot.net/Commands/Rpg/Cookies.cs index c96d456..f55fa6c 100644 --- a/Geekbot.net/Commands/Rpg/Cookies.cs +++ b/Geekbot.net/Commands/Rpg/Cookies.cs @@ -32,16 +32,17 @@ namespace Geekbot.net.Commands.Rpg { try { + var transDict = await _translation.GetDict(Context); var actor = await GetUser(Context.User.Id); if (actor.LastPayout.Value.AddHours(24) > DateTimeOffset.Now) { - await ReplyAsync($"You already got cookies in the last 24 hours, wait until {actor.LastPayout.Value.AddHours(24):HH:mm:ss} for more cookies"); + await ReplyAsync(string.Format(transDict["WaitForMoreCookies"], actor.LastPayout.Value.AddHours(24).ToString("HH:mm:ss"))); return; } actor.Cookies += 10; actor.LastPayout = DateTimeOffset.Now; await SetUser(actor); - await ReplyAsync($"You got 10 cookies, there are now {actor.Cookies} cookies in you cookie jar"); + await ReplyAsync(string.Format(transDict["GetCookies"], 10, actor.Cookies)); } catch (Exception e) @@ -56,9 +57,9 @@ namespace Geekbot.net.Commands.Rpg { try { + var transDict = await _translation.GetDict(Context); var actor = await GetUser(Context.User.Id); - await ReplyAsync($"There are {actor.Cookies} cookies in you cookie jar"); - + await ReplyAsync(string.Format(transDict["InYourJar"], actor.Cookies)); } catch (Exception e) { diff --git a/Geekbot.net/Lib/Localization/Translations.json b/Geekbot.net/Lib/Localization/Translations.json index 2d3414e..41865a1 100644 --- a/Geekbot.net/Lib/Localization/Translations.json +++ b/Geekbot.net/Lib/Localization/Translations.json @@ -96,5 +96,19 @@ "EN": ":red_circle: {0}, you can't guess the same number again", "CHDE": ":red_circle: {0}, du chasch nid nomol es gliche rate" } + }, + "cookie": { + "GetCookies": { + "EN": "You got {0} cookies, there are now {1} cookies in you cookie jar", + "CHDE": "Du häsch {0} guetzli becho, du häsch jetzt {1} guetzli ih dr büchse" + }, + "WaitForMoreCookies": { + "EN": "You already got cookies in the last 24 hours, wait until {0} for more cookies", + "CHDE": "Du hesch scho guetzli becho ih de letzti 24 stund, wart no bis {0}" + }, + "InYourJar": { + "EN": "There are {0} cookies in you cookie jar", + "CHDE": "Es hät {0} guetzli ih dineri büchs" + } } } \ No newline at end of file From fd75fef9739d4eb593c4e396117d1cb5e585315c Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 10 May 2019 22:30:48 +0200 Subject: [PATCH 148/443] Add the ability for users to give cookies away --- Geekbot.net/Commands/Rpg/Cookies.cs | 32 +++++++++++++++++++ .../Lib/Localization/Translations.json | 8 +++++ 2 files changed, 40 insertions(+) diff --git a/Geekbot.net/Commands/Rpg/Cookies.cs b/Geekbot.net/Commands/Rpg/Cookies.cs index f55fa6c..21077bf 100644 --- a/Geekbot.net/Commands/Rpg/Cookies.cs +++ b/Geekbot.net/Commands/Rpg/Cookies.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using System.Threading.Tasks; +using Discord; using Discord.Commands; using Geekbot.net.Database; using Geekbot.net.Database.Models; @@ -67,6 +68,37 @@ namespace Geekbot.net.Commands.Rpg } } + [Command("give", RunMode = RunMode.Async)] + [Summary("Give cookies to someone")] + public async Task PeekIntoCookieJar([Summary("User")] IUser user, [Summary("amount")] int amount) + { + try + { + var transDict = await _translation.GetDict(Context); + var giver = await GetUser(Context.User.Id); + + if (giver.Cookies < amount) + { + await ReplyAsync(string.Format(transDict["NotEnoughToGive"])); + return; + } + + var taker = await GetUser(user.Id); + + giver.Cookies -= amount; + taker.Cookies += amount; + + await SetUser(giver); + await SetUser(taker); + + await ReplyAsync(string.Format(transDict["Given"], amount, user.Username)); + } + catch (Exception e) + { + await _errorHandler.HandleCommandException(e, Context); + } + } + private async Task GetUser(ulong userId) { var user = _database.Cookies.FirstOrDefault(u =>u.GuildId.Equals(Context.Guild.Id.AsLong()) && u.UserId.Equals(userId.AsLong())) ?? await CreateNewRow(userId); diff --git a/Geekbot.net/Lib/Localization/Translations.json b/Geekbot.net/Lib/Localization/Translations.json index 41865a1..191c4ff 100644 --- a/Geekbot.net/Lib/Localization/Translations.json +++ b/Geekbot.net/Lib/Localization/Translations.json @@ -109,6 +109,14 @@ "InYourJar": { "EN": "There are {0} cookies in you cookie jar", "CHDE": "Es hät {0} guetzli ih dineri büchs" + }, + "Given": { + "EN": "You gave {0} cookies to {1}", + "CHDE": "Du hesch {1} {0} guetzli geh" + }, + "NotEnoughToGive": { + "EN": "You don't have enough cookies", + "CHDE": "Du hesch nid gnueg guetzli" } } } \ No newline at end of file From fe08eee04966450b1c2e1b9ba9cd8c66a3482498 Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 10 May 2019 22:42:53 +0200 Subject: [PATCH 149/443] Let users eat their cookies --- Geekbot.net/Commands/Rpg/Cookies.cs | 30 ++++++++++++++++++- .../Lib/Localization/Translations.json | 8 +++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/Geekbot.net/Commands/Rpg/Cookies.cs b/Geekbot.net/Commands/Rpg/Cookies.cs index 21077bf..3e3e98c 100644 --- a/Geekbot.net/Commands/Rpg/Cookies.cs +++ b/Geekbot.net/Commands/Rpg/Cookies.cs @@ -70,7 +70,7 @@ namespace Geekbot.net.Commands.Rpg [Command("give", RunMode = RunMode.Async)] [Summary("Give cookies to someone")] - public async Task PeekIntoCookieJar([Summary("User")] IUser user, [Summary("amount")] int amount) + public async Task GiveACookie([Summary("User")] IUser user, [Summary("amount")] int amount) { try { @@ -99,6 +99,34 @@ namespace Geekbot.net.Commands.Rpg } } + [Command("eat", RunMode = RunMode.Async)] + [Summary("Eat a cookie")] + public async Task EatACookie() + { + try + { + var transDict = await _translation.GetDict(Context); + var actor = await GetUser(Context.User.Id); + + if (actor.Cookies < 5) + { + await ReplyAsync(string.Format(transDict["NotEnoughCookiesToEat"])); + return; + } + + var amount = new Random().Next(1, 5); + actor.Cookies -= amount; + + await SetUser(actor); + + await ReplyAsync(string.Format(transDict["AteCookies"], amount, actor.Cookies)); + } + catch (Exception e) + { + await _errorHandler.HandleCommandException(e, Context); + } + } + private async Task GetUser(ulong userId) { var user = _database.Cookies.FirstOrDefault(u =>u.GuildId.Equals(Context.Guild.Id.AsLong()) && u.UserId.Equals(userId.AsLong())) ?? await CreateNewRow(userId); diff --git a/Geekbot.net/Lib/Localization/Translations.json b/Geekbot.net/Lib/Localization/Translations.json index 191c4ff..7747016 100644 --- a/Geekbot.net/Lib/Localization/Translations.json +++ b/Geekbot.net/Lib/Localization/Translations.json @@ -117,6 +117,14 @@ "NotEnoughToGive": { "EN": "You don't have enough cookies", "CHDE": "Du hesch nid gnueg guetzli" + }, + "NotEnoughCookiesToEat": { + "EN": "Your cookie jar looks almost empty, you should probably not eat a cookie", + "CHDE": "Du hesch chuum no guetzli ih dineri büchs, du sötsch warschinli keini esse" + }, + "AteCookies": { + "EN": "You ate {0} cookies, you've only got {1} cookies left", + "CHDE": "Du hesch {0} guetzli gesse und hesch jezt no {1} übrig" } } } \ No newline at end of file From 4143180b42fe54b3e11a1821a6c3fdc98e7c5492 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 11 May 2019 01:18:22 +0200 Subject: [PATCH 150/443] Add a better random number generator --- Geekbot.net/Commands/Games/Roll.cs | 7 ++- Geekbot.net/Commands/Randomness/Ship.cs | 7 ++- Geekbot.net/Commands/Rpg/Cookies.cs | 7 ++- Geekbot.net/Commands/Utils/Dice/Dice.cs | 10 +++- Geekbot.net/Commands/Utils/Quote/Quote.cs | 7 ++- .../IRandomNumberGenerator.cs | 7 +++ .../RandomNumberGenerator.cs | 46 +++++++++++++++++++ Geekbot.net/Program.cs | 3 ++ 8 files changed, 85 insertions(+), 9 deletions(-) create mode 100644 Geekbot.net/Lib/RandomNumberGenerator/IRandomNumberGenerator.cs create mode 100644 Geekbot.net/Lib/RandomNumberGenerator/RandomNumberGenerator.cs diff --git a/Geekbot.net/Commands/Games/Roll.cs b/Geekbot.net/Commands/Games/Roll.cs index 8db295c..ac9279a 100644 --- a/Geekbot.net/Commands/Games/Roll.cs +++ b/Geekbot.net/Commands/Games/Roll.cs @@ -8,6 +8,7 @@ using Geekbot.net.Lib.AlmostRedis; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Localization; +using Geekbot.net.Lib.RandomNumberGenerator; using StackExchange.Redis; namespace Geekbot.net.Commands.Games @@ -18,12 +19,14 @@ namespace Geekbot.net.Commands.Games private readonly IAlmostRedis _redis; private readonly ITranslationHandler _translation; private readonly DatabaseContext _database; + private readonly IRandomNumberGenerator _randomNumberGenerator; - public Roll(IAlmostRedis redis, IErrorHandler errorHandler, ITranslationHandler translation, DatabaseContext database) + public Roll(IAlmostRedis redis, IErrorHandler errorHandler, ITranslationHandler translation, DatabaseContext database, IRandomNumberGenerator randomNumberGenerator) { _redis = redis; _translation = translation; _database = database; + _randomNumberGenerator = randomNumberGenerator; _errorHandler = errorHandler; } @@ -33,7 +36,7 @@ namespace Geekbot.net.Commands.Games { try { - var number = new Random().Next(1, 100); + var number = _randomNumberGenerator.Next(1, 100); var guess = 1000; int.TryParse(stuff, out guess); var transDict = await _translation.GetDict(Context); diff --git a/Geekbot.net/Commands/Randomness/Ship.cs b/Geekbot.net/Commands/Randomness/Ship.cs index 45956b6..3d876ef 100644 --- a/Geekbot.net/Commands/Randomness/Ship.cs +++ b/Geekbot.net/Commands/Randomness/Ship.cs @@ -7,18 +7,21 @@ using Geekbot.net.Database; using Geekbot.net.Database.Models; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; +using Geekbot.net.Lib.RandomNumberGenerator; namespace Geekbot.net.Commands.Randomness { public class Ship : ModuleBase { private readonly IErrorHandler _errorHandler; + private readonly IRandomNumberGenerator _randomNumberGenerator; private readonly DatabaseContext _database; - public Ship(DatabaseContext database, IErrorHandler errorHandler) + public Ship(DatabaseContext database, IErrorHandler errorHandler, IRandomNumberGenerator randomNumberGenerator) { _database = database; _errorHandler = errorHandler; + _randomNumberGenerator = randomNumberGenerator; } [Command("Ship", RunMode = RunMode.Async)] @@ -38,7 +41,7 @@ namespace Geekbot.net.Commands.Randomness var shippingRate = 0; if (dbval == null) { - shippingRate = new Random().Next(1, 100); + shippingRate = _randomNumberGenerator.Next(1, 100); _database.Ships.Add(new ShipsModel() { FirstUserId = userKeys.Item1, diff --git a/Geekbot.net/Commands/Rpg/Cookies.cs b/Geekbot.net/Commands/Rpg/Cookies.cs index 3e3e98c..594dd5e 100644 --- a/Geekbot.net/Commands/Rpg/Cookies.cs +++ b/Geekbot.net/Commands/Rpg/Cookies.cs @@ -9,6 +9,7 @@ using Geekbot.net.Lib.CommandPreconditions; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Localization; +using Geekbot.net.Lib.RandomNumberGenerator; namespace Geekbot.net.Commands.Rpg { @@ -19,12 +20,14 @@ namespace Geekbot.net.Commands.Rpg private readonly DatabaseContext _database; private readonly IErrorHandler _errorHandler; private readonly ITranslationHandler _translation; + private readonly IRandomNumberGenerator _randomNumberGenerator; - public Cookies(DatabaseContext database, IErrorHandler errorHandler, ITranslationHandler translation) + public Cookies(DatabaseContext database, IErrorHandler errorHandler, ITranslationHandler translation , IRandomNumberGenerator randomNumberGenerator) { _database = database; _errorHandler = errorHandler; _translation = translation; + _randomNumberGenerator = randomNumberGenerator; } [Command("get", RunMode = RunMode.Async)] @@ -114,7 +117,7 @@ namespace Geekbot.net.Commands.Rpg return; } - var amount = new Random().Next(1, 5); + var amount = _randomNumberGenerator.Next(1, 5); actor.Cookies -= amount; await SetUser(actor); diff --git a/Geekbot.net/Commands/Utils/Dice/Dice.cs b/Geekbot.net/Commands/Utils/Dice/Dice.cs index 7733476..8b23fe6 100644 --- a/Geekbot.net/Commands/Utils/Dice/Dice.cs +++ b/Geekbot.net/Commands/Utils/Dice/Dice.cs @@ -4,11 +4,19 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Discord.Commands; +using Geekbot.net.Lib.RandomNumberGenerator; namespace Geekbot.net.Commands.Utils.Dice { public class Dice : ModuleBase { + private readonly IRandomNumberGenerator _randomNumberGenerator; + + public Dice(IRandomNumberGenerator randomNumberGenerator) + { + _randomNumberGenerator = randomNumberGenerator; + } + [Command("dice", RunMode = RunMode.Async)] [Summary("Roll a dice.")] public async Task RollCommand([Remainder] [Summary("diceType")] string diceType = "1d20") @@ -66,7 +74,7 @@ namespace Geekbot.net.Commands.Utils.Dice var results = new List(); for (var i = 0; i < dice.Times; i++) { - var roll = new Random().Next(1, dice.Sides); + var roll = _randomNumberGenerator.Next(1, dice.Sides); total += roll; results.Add(roll); if (roll == dice.Sides) extraText = "**Critical Hit!**"; diff --git a/Geekbot.net/Commands/Utils/Quote/Quote.cs b/Geekbot.net/Commands/Utils/Quote/Quote.cs index e2456e7..a6f3ec9 100644 --- a/Geekbot.net/Commands/Utils/Quote/Quote.cs +++ b/Geekbot.net/Commands/Utils/Quote/Quote.cs @@ -9,6 +9,7 @@ using Geekbot.net.Lib.CommandPreconditions; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Polyfills; +using Geekbot.net.Lib.RandomNumberGenerator; namespace Geekbot.net.Commands.Utils.Quote { @@ -18,11 +19,13 @@ namespace Geekbot.net.Commands.Utils.Quote { private readonly IErrorHandler _errorHandler; private readonly DatabaseContext _database; + private readonly IRandomNumberGenerator _randomNumberGenerator; - public Quote(IErrorHandler errorHandler, DatabaseContext database) + public Quote(IErrorHandler errorHandler, DatabaseContext database, IRandomNumberGenerator randomNumberGenerator) { _errorHandler = errorHandler; _database = database; + _randomNumberGenerator = randomNumberGenerator; } [Command] @@ -39,7 +42,7 @@ namespace Geekbot.net.Commands.Utils.Quote return; } - var random = new Random().Next(s.Count()); + var random = _randomNumberGenerator.Next(0, s.Count()); var quote = s[random]; var embed = QuoteBuilder(quote); diff --git a/Geekbot.net/Lib/RandomNumberGenerator/IRandomNumberGenerator.cs b/Geekbot.net/Lib/RandomNumberGenerator/IRandomNumberGenerator.cs new file mode 100644 index 0000000..32c6285 --- /dev/null +++ b/Geekbot.net/Lib/RandomNumberGenerator/IRandomNumberGenerator.cs @@ -0,0 +1,7 @@ +namespace Geekbot.net.Lib.RandomNumberGenerator +{ + public interface IRandomNumberGenerator + { + int Next(int minValue, int maxExclusiveValue); + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/RandomNumberGenerator/RandomNumberGenerator.cs b/Geekbot.net/Lib/RandomNumberGenerator/RandomNumberGenerator.cs new file mode 100644 index 0000000..79b4620 --- /dev/null +++ b/Geekbot.net/Lib/RandomNumberGenerator/RandomNumberGenerator.cs @@ -0,0 +1,46 @@ +using System; +using System.Security.Cryptography; + +namespace Geekbot.net.Lib.RandomNumberGenerator +{ + public class RandomNumberGenerator : IRandomNumberGenerator + { + readonly RNGCryptoServiceProvider csp; + + public RandomNumberGenerator() + { + csp = new RNGCryptoServiceProvider(); + } + + public int Next(int minValue, int maxExclusiveValue) + { + if (minValue >= maxExclusiveValue) + { + throw new ArgumentOutOfRangeException("minValue must be lower than maxExclusiveValue"); + } + + var diff = (long)maxExclusiveValue - minValue; + var upperBound = uint.MaxValue / diff * diff; + + uint ui; + do + { + ui = GetRandomUInt(); + } while (ui >= upperBound); + return (int)(minValue + (ui % diff)); + } + + private uint GetRandomUInt() + { + var randomBytes = GenerateRandomBytes(sizeof(uint)); + return BitConverter.ToUInt32(randomBytes, 0); + } + + private byte[] GenerateRandomBytes(int bytesNumber) + { + var buffer = new byte[bytesNumber]; + csp.GetBytes(buffer); + return buffer; + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 7b00055..43c16d1 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -19,6 +19,7 @@ using Geekbot.net.Lib.Levels; using Geekbot.net.Lib.Localization; using Geekbot.net.Lib.Logger; using Geekbot.net.Lib.Media; +using Geekbot.net.Lib.RandomNumberGenerator; using Geekbot.net.Lib.ReactionListener; using Geekbot.net.Lib.UserRepository; using Microsoft.EntityFrameworkCore; @@ -124,6 +125,7 @@ namespace Geekbot.net var mtgManaConverter = new MtgManaConverter(); var wikipediaClient = new WikipediaClient(); var audioUtils = new AudioUtils(); + var randomNumberGenerator = new RandomNumberGenerator(); _highscoreManager = new HighscoreManager(_databaseInitializer.Initialize(), _userRepository); _services.AddSingleton(_redis); @@ -137,6 +139,7 @@ namespace Geekbot.net _services.AddSingleton(mtgManaConverter); _services.AddSingleton(wikipediaClient); _services.AddSingleton(audioUtils); + _services.AddSingleton(randomNumberGenerator); _services.AddSingleton(_highscoreManager); _services.AddSingleton(_globalSettings); _services.AddTransient((e) => _databaseInitializer.Initialize()); From 8effc42f92a7f36163de306e5e30387f084e7e78 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 12 May 2019 00:17:34 +0200 Subject: [PATCH 151/443] Rewrite huge parts of the localization class to support plurals and localized date time formating, also created a TranslationGuildContext class for use in commands --- Geekbot.net/Commands/Rpg/Cookies.cs | 23 +-- .../Lib/Localization/ITranslationHandler.cs | 2 + .../Localization/TranslationGuildContext.cs | 90 ++++++++++++ .../Lib/Localization/TranslationHandler.cs | 41 ++++-- .../Lib/Localization/Translations.json | 134 ++++++++++-------- 5 files changed, 211 insertions(+), 79 deletions(-) create mode 100644 Geekbot.net/Lib/Localization/TranslationGuildContext.cs diff --git a/Geekbot.net/Commands/Rpg/Cookies.cs b/Geekbot.net/Commands/Rpg/Cookies.cs index 594dd5e..6d527ef 100644 --- a/Geekbot.net/Commands/Rpg/Cookies.cs +++ b/Geekbot.net/Commands/Rpg/Cookies.cs @@ -36,17 +36,18 @@ namespace Geekbot.net.Commands.Rpg { try { - var transDict = await _translation.GetDict(Context); + var transContext = await _translation.GetGuildContext(Context); var actor = await GetUser(Context.User.Id); if (actor.LastPayout.Value.AddHours(24) > DateTimeOffset.Now) { - await ReplyAsync(string.Format(transDict["WaitForMoreCookies"], actor.LastPayout.Value.AddHours(24).ToString("HH:mm:ss"))); + var formatedWaitTime = transContext.FormatDateTimeAsRemaining(actor.LastPayout.Value.AddHours(24)); + await ReplyAsync(transContext.GetString("WaitForMoreCookies", formatedWaitTime)); return; } actor.Cookies += 10; actor.LastPayout = DateTimeOffset.Now; await SetUser(actor); - await ReplyAsync(string.Format(transDict["GetCookies"], 10, actor.Cookies)); + await ReplyAsync(transContext.GetString("GetCookies", 10, actor.Cookies)); } catch (Exception e) @@ -61,9 +62,9 @@ namespace Geekbot.net.Commands.Rpg { try { - var transDict = await _translation.GetDict(Context); + var transContext = await _translation.GetGuildContext(Context); var actor = await GetUser(Context.User.Id); - await ReplyAsync(string.Format(transDict["InYourJar"], actor.Cookies)); + await ReplyAsync(transContext.GetString("InYourJar", actor.Cookies)); } catch (Exception e) { @@ -77,12 +78,12 @@ namespace Geekbot.net.Commands.Rpg { try { - var transDict = await _translation.GetDict(Context); + var transContext = await _translation.GetGuildContext(Context); var giver = await GetUser(Context.User.Id); if (giver.Cookies < amount) { - await ReplyAsync(string.Format(transDict["NotEnoughToGive"])); + await ReplyAsync(transContext.GetString("NotEnoughToGive")); return; } @@ -94,7 +95,7 @@ namespace Geekbot.net.Commands.Rpg await SetUser(giver); await SetUser(taker); - await ReplyAsync(string.Format(transDict["Given"], amount, user.Username)); + await ReplyAsync(transContext.GetString("Given", amount, user.Username)); } catch (Exception e) { @@ -108,12 +109,12 @@ namespace Geekbot.net.Commands.Rpg { try { - var transDict = await _translation.GetDict(Context); + var transContext = await _translation.GetGuildContext(Context); var actor = await GetUser(Context.User.Id); if (actor.Cookies < 5) { - await ReplyAsync(string.Format(transDict["NotEnoughCookiesToEat"])); + await ReplyAsync(transContext.GetString("NotEnoughCookiesToEat")); return; } @@ -122,7 +123,7 @@ namespace Geekbot.net.Commands.Rpg await SetUser(actor); - await ReplyAsync(string.Format(transDict["AteCookies"], amount, actor.Cookies)); + await ReplyAsync(transContext.GetString("AteCookies", amount, actor.Cookies)); } catch (Exception e) { diff --git a/Geekbot.net/Lib/Localization/ITranslationHandler.cs b/Geekbot.net/Lib/Localization/ITranslationHandler.cs index 2124d4e..7a46e56 100644 --- a/Geekbot.net/Lib/Localization/ITranslationHandler.cs +++ b/Geekbot.net/Lib/Localization/ITranslationHandler.cs @@ -7,8 +7,10 @@ namespace Geekbot.net.Lib.Localization public interface ITranslationHandler { Task GetString(ulong guildId, string command, string stringName); + List GetStrings(string language, string command, string stringName); Task> GetDict(ICommandContext context); Task> GetDict(ICommandContext context, string command); + Task GetGuildContext(ICommandContext context); Task SetLanguage(ulong guildId, string language); List SupportedLanguages { get; } } diff --git a/Geekbot.net/Lib/Localization/TranslationGuildContext.cs b/Geekbot.net/Lib/Localization/TranslationGuildContext.cs new file mode 100644 index 0000000..037ef3a --- /dev/null +++ b/Geekbot.net/Lib/Localization/TranslationGuildContext.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Geekbot.net.Lib.Localization +{ + public class TranslationGuildContext + { + public ITranslationHandler TranslationHandler { get; } + public string Language { get; } + public Dictionary Dict { get; } + + public TranslationGuildContext(ITranslationHandler translationHandler, string language, Dictionary dict) + { + TranslationHandler = translationHandler; + Language = language; + Dict = dict; + } + + public string GetString(string stringToFormat, params object[] args) + { + return string.Format(Dict[stringToFormat] ?? "", args); + } + + public string FormatDateTimeAsRemaining(DateTimeOffset dateTime) + { + var remaining = dateTime - DateTimeOffset.Now; + const string formattable = "{0} {1}"; + var sb = new StringBuilder(); + + if (remaining.Days > 0) + { + var s = GetTimeString(TimeTypes.Days); + sb.AppendFormat(formattable, remaining.Days, GetSingOrPlur(remaining.Days, s)); + } + + if (remaining.Hours > 0) + { + if (sb.Length > 0) sb.Append(", "); + var s = GetTimeString(TimeTypes.Hours); + sb.AppendFormat(formattable, remaining.Hours, GetSingOrPlur(remaining.Hours, s)); + } + + if (remaining.Minutes > 0) + { + if (sb.Length > 0) sb.Append(", "); + var s = GetTimeString(TimeTypes.Minutes); + sb.AppendFormat(formattable, remaining.Minutes, GetSingOrPlur(remaining.Minutes, s)); + } + + if (remaining.Seconds > 0) + { + if (sb.Length > 0) + { + var and = TranslationHandler.GetStrings(Language, "dateTime", "And").First(); + sb.AppendFormat(" {0} ", and); + } + var s = GetTimeString(TimeTypes.Seconds); + sb.AppendFormat(formattable, remaining.Seconds, GetSingOrPlur(remaining.Seconds, s)); + } + + return sb.ToString().Trim(); + } + + public Task SetLanguage(ulong guildId, string language) + { + return TranslationHandler.SetLanguage(guildId, language); + } + + private List GetTimeString(TimeTypes type) + { + return TranslationHandler.GetStrings(Language, "dateTime", type.ToString()); + } + + private string GetSingOrPlur(int number, List versions) + { + return number == 1 ? versions[0] : versions[1]; + } + + private enum TimeTypes + { + Days, + Hours, + Minutes, + Seconds + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/Localization/TranslationHandler.cs b/Geekbot.net/Lib/Localization/TranslationHandler.cs index a1b650b..b61c93c 100644 --- a/Geekbot.net/Lib/Localization/TranslationHandler.cs +++ b/Geekbot.net/Lib/Localization/TranslationHandler.cs @@ -17,7 +17,7 @@ namespace Geekbot.net.Lib.Localization private readonly DatabaseContext _database; private readonly IGeekbotLogger _logger; private readonly Dictionary _serverLanguages; - private Dictionary>> _translations; + private Dictionary>>> _translations; public TranslationHandler(DatabaseContext database, IGeekbotLogger logger) { @@ -33,8 +33,8 @@ namespace Geekbot.net.Lib.Localization try { var translationFile = File.ReadAllText(Path.GetFullPath("./Lib/Localization/Translations.json")); - var rawTranslations = JsonSerializer.Deserialize>>>(translationFile); - var sortedPerLanguage = new Dictionary>>(); + var rawTranslations = JsonSerializer.Deserialize>>>>(translationFile); + var sortedPerLanguage = new Dictionary>>>(); foreach (var command in rawTranslations) { foreach (var str in command.Value) @@ -43,8 +43,8 @@ namespace Geekbot.net.Lib.Localization { if (!sortedPerLanguage.ContainsKey(lang.Key)) { - var commandDict = new Dictionary>(); - var strDict = new Dictionary + var commandDict = new Dictionary>>(); + var strDict = new Dictionary> { {str.Key, lang.Value} }; @@ -53,8 +53,10 @@ namespace Geekbot.net.Lib.Localization } if (!sortedPerLanguage[lang.Key].ContainsKey(command.Key)) { - var strDict = new Dictionary(); - strDict.Add(str.Key, lang.Value); + var strDict = new Dictionary> + { + {str.Key, lang.Value} + }; sortedPerLanguage[lang.Key].Add(command.Key, strDict); } if (!sortedPerLanguage[lang.Key][command.Key].ContainsKey(str.Key)) @@ -109,10 +111,16 @@ namespace Geekbot.net.Lib.Localization public async Task GetString(ulong guildId, string command, string stringName) { - var translation = _translations[await GetServerLanguage(guildId)][command][stringName]; - if (!string.IsNullOrWhiteSpace(translation)) return translation; + var serverLang = await GetServerLanguage(guildId); + return GetStrings(serverLang, command, stringName).First(); + } + + public List GetStrings(string language, string command, string stringName) + { + var translation = _translations[language][command][stringName]; + if (!string.IsNullOrWhiteSpace(translation.First())) return translation; translation = _translations[command][stringName]["EN"]; - if (string.IsNullOrWhiteSpace(translation)) + if (string.IsNullOrWhiteSpace(translation.First())) { _logger.Warning(LogSource.Geekbot, $"No translation found for {command} - {stringName}"); } @@ -125,7 +133,8 @@ namespace Geekbot.net.Lib.Localization { var command = context.Message.Content.Split(' ').First().TrimStart('!').ToLower(); var serverLanguage = await GetServerLanguage(context.Guild?.Id ?? 0); - return _translations[serverLanguage][command]; + return _translations[serverLanguage][command] + .ToDictionary(dict => dict.Key, dict => dict.Value.First()); } catch (Exception e) { @@ -133,13 +142,21 @@ namespace Geekbot.net.Lib.Localization return new Dictionary(); } } + + public async Task GetGuildContext(ICommandContext context) + { + var dict = await GetDict(context); + var language = await GetServerLanguage(context.Guild?.Id ?? 0); + return new TranslationGuildContext(this, language, dict); + } public async Task> GetDict(ICommandContext context, string command) { try { var serverLanguage = await GetServerLanguage(context.Guild?.Id ?? 0); - return _translations[serverLanguage][command]; + return _translations[serverLanguage][command] + .ToDictionary(dict => dict.Key, dict => dict.Value.First()); } catch (Exception e) { diff --git a/Geekbot.net/Lib/Localization/Translations.json b/Geekbot.net/Lib/Localization/Translations.json index 7747016..356deb5 100644 --- a/Geekbot.net/Lib/Localization/Translations.json +++ b/Geekbot.net/Lib/Localization/Translations.json @@ -1,130 +1,152 @@ { + "dateTime": { + "Days": { + "EN": ["day", "days"], + "CHDE": ["tag", "täg"] + }, + "Hours": { + "EN": ["hour", "hours"], + "CHDE": ["stund", "stunde"] + }, + "Minutes": { + "EN": ["minute", "minutes"], + "CHDE": ["minute", "minute"] + }, + "Seconds": { + "EN": ["second", "seconds"], + "CHDE": ["sekunde", "sekunde"] + }, + "And": { + "EN": ["and"], + "CHDE": ["und"] + } + }, "admin": { "NewLanguageSet": { - "EN": "I will reply in english from now on", - "CHDE": "I werd ab jetzt uf schwiizerdüütsch antworte, äuuä" + "EN": ["I will reply in english from now on"], + "CHDE": ["I werd ab jetzt uf schwiizerdüütsch antworte, äuuä"] }, "GetLanguage": { - "EN": "I'm talking english", - "CHDE": "I red schwiizerdüütsch" + "EN": ["I'm talking english"], + "CHDE": ["I red schwiizerdüütsch"] } }, "errorHandler": { "SomethingWentWrong": { - "EN": "Something went wrong :confused:", - "CHDE": "Öppis isch schief gange :confused:" + "EN": ["Something went wrong :confused:"], + "CHDE": ["Öppis isch schief gange :confused:"] } }, "httpErrors": { "403": { - "EN": "Seems like i don't have enough permission to that :confused:", - "CHDE": "Gseht danach us das ich nid gnueg recht han zum das mache :confused:" + "EN": ["Seems like i don't have enough permission to that :confused:"], + "CHDE": ["Gseht danach us das ich nid gnueg recht han zum das mache :confused:"] } }, "choose": { "Choice": { - "EN": "I Choose **{0}**", - "CHDE": "I nimme **{0}**" + "EN": ["I Choose **{0}**"], + "CHDE": ["I nimme **{0}**"] } }, "good": { "CannotChangeOwn": { - "EN": "Sorry {0}, but you can't give yourself karma", - "CHDE": "Sorry {0}, aber du chasch dr selber kei karma geh" + "EN": ["Sorry {0}, but you can't give yourself karma"], + "CHDE": ["Sorry {0}, aber du chasch dr selber kei karma geh"] }, "WaitUntill": { - "EN": "Sorry {0}, but you have to wait {1} before you can give karma again...", - "CHDE": "Sorry {0}, aber du musch no {1} warte bisch d wieder karma chasch geh..." + "EN": ["Sorry {0}, but you have to wait {1} before you can give karma again..."], + "CHDE": ["Sorry {0}, aber du musch no {1} warte bisch d wieder karma chasch geh..."] }, "Increased": { - "EN": "Karma gained", - "CHDE": "Karma becho" + "EN": ["Karma gained"], + "CHDE": ["Karma becho"] }, "By": { - "EN": "By", - "CHDE": "Vo" + "EN": ["By"], + "CHDE": ["Vo"] }, "Amount": { - "EN": "Amount", - "CHDE": "Mengi" + "EN": ["Amount"], + "CHDE": ["Mengi"] }, "Current": { - "EN": "Current", - "CHDE": "Jetzt" + "EN": ["Current"], + "CHDE": ["Jetzt"] } }, "bad": { "CannotChangeOwn": { - "EN": "Sorry {0}, but you can't lower your own karma", - "CHDE": "Sorry {0}, aber du chasch dr din eigete karma nid weg neh" + "EN": ["Sorry {0}, but you can't lower your own karma"], + "CHDE": ["Sorry {0}, aber du chasch dr din eigete karma nid weg neh"] }, "WaitUntill": { - "EN": "Sorry {0}, but you have to wait {1} before you can lower karma again...", - "CHDE": "Sorry {0}, aber du musch no {1} warte bisch d wieder karma chasch senke..." + "EN": ["Sorry {0}, but you have to wait {1} before you can lower karma again..."], + "CHDE": ["Sorry {0}, aber du musch no {1} warte bisch d wieder karma chasch senke..."] }, "Decreased": { - "EN": "Karma lowered", - "CHDE": "Karma gsenkt" + "EN": ["Karma lowered"], + "CHDE": ["Karma gsenkt"] }, "By": { - "EN": "By", - "CHDE": "Vo" + "EN": ["By"], + "CHDE": ["Vo"] }, "Amount": { - "EN": "Amount", - "CHDE": "Mengi" + "EN": ["Amount"], + "CHDE": ["Mengi"] }, "Current": { - "EN": "Current", - "CHDE": "Jetzt" + "EN": ["Current"], + "CHDE": ["Jetzt"] } }, "roll": { "Rolled": { - "EN": "{0}, you rolled {1}, your guess was {2}", - "CHDE": "{0}, du hesch {1} grollt und hesch {2} grate" + "EN": ["{0}, you rolled {1}, your guess was {2}"], + "CHDE": ["{0}, du hesch {1} grollt und hesch {2} grate"] }, "Gratz": { - "EN": "Congratulations {0}, your guess was correct!", - "CHDE": "Gratuliere {0}, du hesch richtig grate!" + "EN": ["Congratulations {0}, your guess was correct!"], + "CHDE": ["Gratuliere {0}, du hesch richtig grate!"] }, "RolledNoGuess": { - "EN": "{0}, you rolled {1}", - "CHDE": "{0}, du hesch {1} grollt" + "EN": ["{0}, you rolled {1}"], + "CHDE": ["{0}, du hesch {1} grollt"] }, "NoPrevGuess": { - "EN": ":red_circle: {0}, you can't guess the same number again", - "CHDE": ":red_circle: {0}, du chasch nid nomol es gliche rate" + "EN": [":red_circle: {0}, you can't guess the same number again"], + "CHDE": [":red_circle: {0}, du chasch nid nomol es gliche rate"] } }, "cookie": { "GetCookies": { - "EN": "You got {0} cookies, there are now {1} cookies in you cookie jar", - "CHDE": "Du häsch {0} guetzli becho, du häsch jetzt {1} guetzli ih dr büchse" + "EN": ["You got {0} cookies, there are now {1} cookies in you cookie jar"], + "CHDE": ["Du häsch {0} guetzli becho, du häsch jetzt {1} guetzli ih dr büchse"] }, "WaitForMoreCookies": { - "EN": "You already got cookies in the last 24 hours, wait until {0} for more cookies", - "CHDE": "Du hesch scho guetzli becho ih de letzti 24 stund, wart no bis {0}" + "EN": ["You already got cookies in the last 24 hours, you can have more cookies in {0}"], + "CHDE": ["Du hesch scho guetzli becho ih de letzti 24 stund, du chasch meh ha in {0}"] }, "InYourJar": { - "EN": "There are {0} cookies in you cookie jar", - "CHDE": "Es hät {0} guetzli ih dineri büchs" + "EN": ["There are {0} cookies in you cookie jar"], + "CHDE": ["Es hät {0} guetzli ih dineri büchs"] }, "Given": { - "EN": "You gave {0} cookies to {1}", - "CHDE": "Du hesch {1} {0} guetzli geh" + "EN": ["You gave {0} cookies to {1}"], + "CHDE": ["Du hesch {1} {0} guetzli geh"] }, "NotEnoughToGive": { - "EN": "You don't have enough cookies", - "CHDE": "Du hesch nid gnueg guetzli" + "EN": ["You don't have enough cookies"], + "CHDE": ["Du hesch nid gnueg guetzli"] }, "NotEnoughCookiesToEat": { - "EN": "Your cookie jar looks almost empty, you should probably not eat a cookie", - "CHDE": "Du hesch chuum no guetzli ih dineri büchs, du sötsch warschinli keini esse" + "EN": ["Your cookie jar looks almost empty, you should probably not eat a cookie"], + "CHDE": ["Du hesch chuum no guetzli ih dineri büchs, du sötsch warschinli keini esse"] }, "AteCookies": { - "EN": "You ate {0} cookies, you've only got {1} cookies left", - "CHDE": "Du hesch {0} guetzli gesse und hesch jezt no {1} übrig" + "EN": ["You ate {0} cookies, you've only got {1} cookies left"], + "CHDE": ["Du hesch {0} guetzli gesse und hesch jezt no {1} übrig"] } } } \ No newline at end of file From 0f0c0684c62c2e385fd941d122bef7d81406d63e Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 12 May 2019 00:32:07 +0200 Subject: [PATCH 152/443] Convert karma command to use new GuildContext for translations --- Geekbot.net/Commands/User/Karma.cs | 38 +++++++++++++----------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/Geekbot.net/Commands/User/Karma.cs b/Geekbot.net/Commands/User/Karma.cs index e2a4739..ed221ff 100644 --- a/Geekbot.net/Commands/User/Karma.cs +++ b/Geekbot.net/Commands/User/Karma.cs @@ -32,16 +32,16 @@ namespace Geekbot.net.Commands.User { try { - var transDict = await _translation.GetDict(Context); + var transContext = await _translation.GetGuildContext(Context); var actor = await GetUser(Context.User.Id); if (user.Id == Context.User.Id) { - await ReplyAsync(string.Format(transDict["CannotChangeOwn"], Context.User.Username)); + await ReplyAsync(transContext.GetString("CannotChangeOwn", Context.User.Username)); } else if (TimeoutFinished(actor.TimeOut)) { - await ReplyAsync(string.Format(transDict["WaitUntill"], Context.User.Username, - GetTimeLeft(actor.TimeOut))); + var formatedWaitTime = transContext.FormatDateTimeAsRemaining(actor.TimeOut.AddMinutes(3)); + await ReplyAsync(transContext.GetString("WaitUntill", Context.User.Username, formatedWaitTime)); } else { @@ -60,10 +60,10 @@ namespace Geekbot.net.Commands.User .WithName(user.Username)); eb.WithColor(new Color(138, 219, 146)); - eb.Title = transDict["Increased"]; - eb.AddInlineField(transDict["By"], Context.User.Username); - eb.AddInlineField(transDict["Amount"], "+1"); - eb.AddInlineField(transDict["Current"], target.Karma); + eb.Title = transContext.GetString("Increased"); + eb.AddInlineField(transContext.GetString("By"), Context.User.Username); + eb.AddInlineField(transContext.GetString("Amount"), "+1"); + eb.AddInlineField(transContext.GetString("Current"), target.Karma); await ReplyAsync("", false, eb.Build()); } } @@ -79,16 +79,16 @@ namespace Geekbot.net.Commands.User { try { - var transDict = await _translation.GetDict(Context); + var transContext = await _translation.GetGuildContext(Context); var actor = await GetUser(Context.User.Id); if (user.Id == Context.User.Id) { - await ReplyAsync(string.Format(transDict["CannotChangeOwn"], Context.User.Username)); + await ReplyAsync(transContext.GetString("CannotChangeOwn", Context.User.Username)); } else if (TimeoutFinished(actor.TimeOut)) { - await ReplyAsync(string.Format(transDict["WaitUntill"], Context.User.Username, - GetTimeLeft(actor.TimeOut))); + var formatedWaitTime = transContext.FormatDateTimeAsRemaining(actor.TimeOut.AddMinutes(3)); + await ReplyAsync(transContext.GetString("WaitUntill", Context.User.Username, formatedWaitTime)); } else { @@ -107,10 +107,10 @@ namespace Geekbot.net.Commands.User .WithName(user.Username)); eb.WithColor(new Color(138, 219, 146)); - eb.Title = transDict["Decreased"]; - eb.AddInlineField(transDict["By"], Context.User.Username); - eb.AddInlineField(transDict["Amount"], "-1"); - eb.AddInlineField(transDict["Current"], target.Karma); + eb.Title = transContext.GetString("Decreased"); + eb.AddInlineField(transContext.GetString("By"), Context.User.Username); + eb.AddInlineField(transContext.GetString("Amount"), "-1"); + eb.AddInlineField(transContext.GetString("Current"), target.Karma); await ReplyAsync("", false, eb.Build()); } } @@ -125,12 +125,6 @@ namespace Geekbot.net.Commands.User return lastKarma.AddMinutes(3) > DateTimeOffset.Now; } - private string GetTimeLeft(DateTimeOffset lastKarma) - { - var dt = lastKarma.AddMinutes(3).Subtract(DateTimeOffset.Now); - return $"{dt.Minutes} Minutes and {dt.Seconds} Seconds"; - } - private async Task GetUser(ulong userId) { var user = _database.Karma.FirstOrDefault(u =>u.GuildId.Equals(Context.Guild.Id.AsLong()) && u.UserId.Equals(userId.AsLong())) ?? await CreateNewRow(userId); From 67add36e51070a7e90c451c770f9c4eeb6820922 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 12 May 2019 00:44:25 +0200 Subject: [PATCH 153/443] Remove all in-command usages of GetDict for translations --- Geekbot.net/Commands/Admin/Admin.cs | 4 ++-- Geekbot.net/Commands/Games/Roll.cs | 10 +++++----- Geekbot.net/Commands/Utils/Choose.cs | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Geekbot.net/Commands/Admin/Admin.cs b/Geekbot.net/Commands/Admin/Admin.cs index f60e5df..ba31e54 100644 --- a/Geekbot.net/Commands/Admin/Admin.cs +++ b/Geekbot.net/Commands/Admin/Admin.cs @@ -134,8 +134,8 @@ namespace Geekbot.net.Commands.Admin _database.GuildSettings.Update(guild); await _database.SaveChangesAsync(); - var trans = await _translation.GetDict(Context); - await ReplyAsync(trans["NewLanguageSet"]); + var transContext = await _translation.GetGuildContext(Context); + await ReplyAsync(transContext.GetString("NewLanguageSet")); return; } diff --git a/Geekbot.net/Commands/Games/Roll.cs b/Geekbot.net/Commands/Games/Roll.cs index ac9279a..4fbb24e 100644 --- a/Geekbot.net/Commands/Games/Roll.cs +++ b/Geekbot.net/Commands/Games/Roll.cs @@ -39,7 +39,7 @@ namespace Geekbot.net.Commands.Games var number = _randomNumberGenerator.Next(1, 100); var guess = 1000; int.TryParse(stuff, out guess); - var transDict = await _translation.GetDict(Context); + var transContext = await _translation.GetGuildContext(Context); if (guess <= 100 && guess > 0) { var prevRoll = _redis.Db.HashGet($"{Context.Guild.Id}:RollsPrevious2", Context.Message.Author.Id).ToString()?.Split('|'); @@ -47,17 +47,17 @@ namespace Geekbot.net.Commands.Games { if (prevRoll[0] == guess.ToString() && DateTime.Parse(prevRoll[1]) > DateTime.Now.AddDays(-1)) { - await ReplyAsync(string.Format(transDict["NoPrevGuess"], Context.Message.Author.Mention)); + await ReplyAsync(transContext.GetString("NoPrevGuess", Context.Message.Author.Mention)); return; } } _redis.Db.HashSet($"{Context.Guild.Id}:RollsPrevious2", new[] {new HashEntry(Context.Message.Author.Id, $"{guess}|{DateTime.Now}")}); - await ReplyAsync(string.Format(transDict["Rolled"], Context.Message.Author.Mention, number, guess)); + await ReplyAsync(transContext.GetString("Rolled", Context.Message.Author.Mention, number, guess)); if (guess == number) { - await ReplyAsync(string.Format(transDict["Gratz"], Context.Message.Author)); + await ReplyAsync(transContext.GetString("Gratz", Context.Message.Author)); _redis.Db.HashIncrement($"{Context.Guild.Id}:Rolls", Context.User.Id.ToString()); var user = await GetUser(Context.User.Id); user.Rolls += 1; @@ -67,7 +67,7 @@ namespace Geekbot.net.Commands.Games } else { - await ReplyAsync(string.Format(transDict["RolledNoGuess"], Context.Message.Author.Mention, number)); + await ReplyAsync(transContext.GetString("RolledNoGuess", Context.Message.Author.Mention, number)); } } catch (Exception e) diff --git a/Geekbot.net/Commands/Utils/Choose.cs b/Geekbot.net/Commands/Utils/Choose.cs index ee2bf0d..7060755 100644 --- a/Geekbot.net/Commands/Utils/Choose.cs +++ b/Geekbot.net/Commands/Utils/Choose.cs @@ -24,10 +24,10 @@ namespace Geekbot.net.Commands.Utils { try { - var transDict = await _translation.GetDict(Context); + var transContext = await _translation.GetGuildContext(Context); var choicesArray = choices.Split(';'); var choice = new Random().Next(choicesArray.Length); - await ReplyAsync(string.Format(transDict["Choice"], choicesArray[choice])); + await ReplyAsync(transContext.GetString("Choice", choicesArray[choice])); } catch (Exception e) { From b309f155bec37802d23ebaf6fb3f94ef48a78f2a Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 12 May 2019 00:56:22 +0200 Subject: [PATCH 154/443] Make GetDict private in the TranslationHandler --- Geekbot.net/Lib/Localization/ITranslationHandler.cs | 1 - Geekbot.net/Lib/Localization/TranslationHandler.cs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/Geekbot.net/Lib/Localization/ITranslationHandler.cs b/Geekbot.net/Lib/Localization/ITranslationHandler.cs index 7a46e56..bb70046 100644 --- a/Geekbot.net/Lib/Localization/ITranslationHandler.cs +++ b/Geekbot.net/Lib/Localization/ITranslationHandler.cs @@ -8,7 +8,6 @@ namespace Geekbot.net.Lib.Localization { Task GetString(ulong guildId, string command, string stringName); List GetStrings(string language, string command, string stringName); - Task> GetDict(ICommandContext context); Task> GetDict(ICommandContext context, string command); Task GetGuildContext(ICommandContext context); Task SetLanguage(ulong guildId, string language); diff --git a/Geekbot.net/Lib/Localization/TranslationHandler.cs b/Geekbot.net/Lib/Localization/TranslationHandler.cs index b61c93c..f298e2d 100644 --- a/Geekbot.net/Lib/Localization/TranslationHandler.cs +++ b/Geekbot.net/Lib/Localization/TranslationHandler.cs @@ -127,7 +127,7 @@ namespace Geekbot.net.Lib.Localization return translation; } - public async Task> GetDict(ICommandContext context) + private async Task> GetDict(ICommandContext context) { try { From 495288b88700c56cf07ab756e4db9a755c68ee37 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 12 May 2019 01:07:22 +0200 Subject: [PATCH 155/443] Pass full translation dictionary to TranslationGuildContext and decide there whether to use singular or plural --- Geekbot.net/Commands/Utils/Choose.cs | 2 +- Geekbot.net/Lib/Localization/TranslationGuildContext.cs | 6 +++--- Geekbot.net/Lib/Localization/TranslationHandler.cs | 7 +++---- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/Geekbot.net/Commands/Utils/Choose.cs b/Geekbot.net/Commands/Utils/Choose.cs index 7060755..62069bd 100644 --- a/Geekbot.net/Commands/Utils/Choose.cs +++ b/Geekbot.net/Commands/Utils/Choose.cs @@ -27,7 +27,7 @@ namespace Geekbot.net.Commands.Utils var transContext = await _translation.GetGuildContext(Context); var choicesArray = choices.Split(';'); var choice = new Random().Next(choicesArray.Length); - await ReplyAsync(transContext.GetString("Choice", choicesArray[choice])); + await ReplyAsync(transContext.GetString("Choice", choicesArray[choice].Trim())); } catch (Exception e) { diff --git a/Geekbot.net/Lib/Localization/TranslationGuildContext.cs b/Geekbot.net/Lib/Localization/TranslationGuildContext.cs index 037ef3a..3f6f923 100644 --- a/Geekbot.net/Lib/Localization/TranslationGuildContext.cs +++ b/Geekbot.net/Lib/Localization/TranslationGuildContext.cs @@ -10,9 +10,9 @@ namespace Geekbot.net.Lib.Localization { public ITranslationHandler TranslationHandler { get; } public string Language { get; } - public Dictionary Dict { get; } + public Dictionary> Dict { get; } - public TranslationGuildContext(ITranslationHandler translationHandler, string language, Dictionary dict) + public TranslationGuildContext(ITranslationHandler translationHandler, string language, Dictionary> dict) { TranslationHandler = translationHandler; Language = language; @@ -21,7 +21,7 @@ namespace Geekbot.net.Lib.Localization public string GetString(string stringToFormat, params object[] args) { - return string.Format(Dict[stringToFormat] ?? "", args); + return string.Format(Dict[stringToFormat].First() ?? "", args); } public string FormatDateTimeAsRemaining(DateTimeOffset dateTime) diff --git a/Geekbot.net/Lib/Localization/TranslationHandler.cs b/Geekbot.net/Lib/Localization/TranslationHandler.cs index f298e2d..8100478 100644 --- a/Geekbot.net/Lib/Localization/TranslationHandler.cs +++ b/Geekbot.net/Lib/Localization/TranslationHandler.cs @@ -127,19 +127,18 @@ namespace Geekbot.net.Lib.Localization return translation; } - private async Task> GetDict(ICommandContext context) + private async Task>> GetDict(ICommandContext context) { try { var command = context.Message.Content.Split(' ').First().TrimStart('!').ToLower(); var serverLanguage = await GetServerLanguage(context.Guild?.Id ?? 0); - return _translations[serverLanguage][command] - .ToDictionary(dict => dict.Key, dict => dict.Value.First()); + return _translations[serverLanguage][command]; } catch (Exception e) { _logger.Error(LogSource.Geekbot, "No translations for command found", e); - return new Dictionary(); + return new Dictionary>(); } } From f9269b877cf4ae3e44dd2244f08de10dea0c9c1f Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 12 May 2019 01:38:56 +0200 Subject: [PATCH 156/443] Use typed TestData for unit tests --- Tests/Lib/Converters/EmojiConverter.test.cs | 94 +++++++++++++++++++++ Tests/Lib/EmojiConverter.test.cs | 74 ---------------- Tests/Lib/LevelCalc.test.cs | 48 ----------- Tests/Lib/Levels/LevelCalc.test.cs | 60 +++++++++++++ Tests/TestData.cs | 21 +++++ Tests/Tests.csproj | 1 + 6 files changed, 176 insertions(+), 122 deletions(-) create mode 100644 Tests/Lib/Converters/EmojiConverter.test.cs delete mode 100644 Tests/Lib/EmojiConverter.test.cs delete mode 100644 Tests/Lib/LevelCalc.test.cs create mode 100644 Tests/Lib/Levels/LevelCalc.test.cs create mode 100644 Tests/TestData.cs diff --git a/Tests/Lib/Converters/EmojiConverter.test.cs b/Tests/Lib/Converters/EmojiConverter.test.cs new file mode 100644 index 0000000..ad37824 --- /dev/null +++ b/Tests/Lib/Converters/EmojiConverter.test.cs @@ -0,0 +1,94 @@ +using Geekbot.net.Lib.Converters; +using Xunit; + +namespace Tests.Lib.Converters +{ + public class EmojiConverterTest + { + public class NumberToEmojiTestDto + { + public int Number { get; set; } + public string Expected { get; set; } + } + + public static TestData NumberToEmojiTestData => + new TestData + { + { + "2", + new NumberToEmojiTestDto + { + Number = 2, + Expected = ":two:" + } + }, + { + "10", + new NumberToEmojiTestDto + { + Number = 10, + Expected = "🔟" + } + }, + { + "15", + new NumberToEmojiTestDto + { + Number = 15, + Expected = ":one::five:" + } + }, + { + "null", + new NumberToEmojiTestDto + { + Number = 0, + Expected = ":zero:" + } + } + }; + + [Theory, MemberData(nameof(NumberToEmojiTestData))] + public void NumberToEmoji(string testName, NumberToEmojiTestDto testData) + { + var emojiConverter = new EmojiConverter(); + var result = emojiConverter.NumberToEmoji(testData.Number); + Assert.Equal(result, testData.Expected); + } + + public class TextToEmojiTestDto + { + public string Text { get; set; } + public string Expected { get; set; } + } + + public static TestData TextToEmojiTestData => + new TestData + { + { + "Test", + new TextToEmojiTestDto + { + Text = "test", + Expected = ":regional_indicator_t: :regional_indicator_e: :regional_indicator_s: :regional_indicator_t: " + } + }, + { + "Best3+?", + new TextToEmojiTestDto + { + Text = "Best3+?", + Expected = ":b: :regional_indicator_e: :regional_indicator_s: :regional_indicator_t: :three: :heavy_plus_sign: :question: " + } + } + }; + + [Theory, MemberData(nameof(TextToEmojiTestData))] + public void TextToEmoji(string testName, TextToEmojiTestDto testData) + { + var emojiConverter = new EmojiConverter(); + var result = emojiConverter.TextToEmoji(testData.Text); + Assert.Equal(result, testData.Expected); + } + } +} \ No newline at end of file diff --git a/Tests/Lib/EmojiConverter.test.cs b/Tests/Lib/EmojiConverter.test.cs deleted file mode 100644 index 170d521..0000000 --- a/Tests/Lib/EmojiConverter.test.cs +++ /dev/null @@ -1,74 +0,0 @@ -using System.Collections.Generic; -using Geekbot.net.Lib.Converters; -using Xunit; - -namespace Tests.Lib -{ - public class EmojiConverterTest - { - public static IEnumerable NumberToEmojiTestData - { - get - { - yield return new object[] - { - 2, - ":two:" - }; - - yield return new object[] - { - 10, - "🔟" - }; - - yield return new object[] - { - 15, - ":one::five:" - }; - - yield return new object[] - { - null, - ":zero:" - }; - } - } - - - [Theory, MemberData(nameof(NumberToEmojiTestData))] - public void NumberToEmoji(int number, string expectedResult) - { - var emojiConverter = new EmojiConverter(); - var result = emojiConverter.NumberToEmoji(number); - Assert.Equal(result, expectedResult); - } - - public static IEnumerable TextToEmojiTestData - { - get - { - yield return new object[] - { - "test", - ":regional_indicator_t: :regional_indicator_e: :regional_indicator_s: :regional_indicator_t: " - }; - yield return new object[] - { - "Best3+?", - ":b: :regional_indicator_e: :regional_indicator_s: :regional_indicator_t: :three: :heavy_plus_sign: :question: " - }; - } - } - - - [Theory, MemberData(nameof(TextToEmojiTestData))] - public void TextToEmoji(string text, string expectedResult) - { - var emojiConverter = new EmojiConverter(); - var result = emojiConverter.TextToEmoji(text); - Assert.Equal(result, expectedResult); - } - } -} \ No newline at end of file diff --git a/Tests/Lib/LevelCalc.test.cs b/Tests/Lib/LevelCalc.test.cs deleted file mode 100644 index 5ac5fd3..0000000 --- a/Tests/Lib/LevelCalc.test.cs +++ /dev/null @@ -1,48 +0,0 @@ -using System.Collections.Generic; -using Geekbot.net.Lib.Levels; -using Xunit; - -namespace Tests.Lib -{ - public class LevelCalcTest - { - public static IEnumerable LevelCalcTestData - { - get - { - yield return new object[] - { - 500, - 13 - }; - - yield return new object[] - { - 41659, - 55 - }; - - yield return new object[] - { - 0, - 1 - }; - - yield return new object[] - { - 4000000, - 101 - }; - } - } - - - [Theory, MemberData(nameof(LevelCalcTestData))] - public void GetLevel(int messages, int expectedResult) - { - var levelCalc = new LevelCalc(); - var result = levelCalc.GetLevel(messages); - Assert.Equal(result, expectedResult); - } - } -} \ No newline at end of file diff --git a/Tests/Lib/Levels/LevelCalc.test.cs b/Tests/Lib/Levels/LevelCalc.test.cs new file mode 100644 index 0000000..9b4b97c --- /dev/null +++ b/Tests/Lib/Levels/LevelCalc.test.cs @@ -0,0 +1,60 @@ +using System.Collections.Generic; +using Geekbot.net.Lib.Levels; +using Xunit; + +namespace Tests.Lib.Levels +{ + public class LevelCalcTest + { + public class LevelCalcTestDto + { + public int Messages { get; set; } + public int ExpectedLevel { get; set; } + } + + public static TestData LevelCalcTestData => + new TestData() + { + { + "500", + new LevelCalcTestDto + { + Messages = 500, + ExpectedLevel = 13 + } + }, + { + "41659", + new LevelCalcTestDto + { + Messages = 41659, + ExpectedLevel = 55 + } + }, + { + "0", + new LevelCalcTestDto + { + Messages = 0, + ExpectedLevel = 1 + } + }, + { + "4000000", + new LevelCalcTestDto + { + Messages = 4000000, + ExpectedLevel = 101 + } + } + }; + + [Theory, MemberData(nameof(LevelCalcTestData))] + public void GetLevel(string testName, LevelCalcTestDto testData) + { + var levelCalc = new LevelCalc(); + var result = levelCalc.GetLevel(testData.Messages); + Assert.Equal(result, testData.ExpectedLevel); + } + } +} \ No newline at end of file diff --git a/Tests/TestData.cs b/Tests/TestData.cs new file mode 100644 index 0000000..6c61f0b --- /dev/null +++ b/Tests/TestData.cs @@ -0,0 +1,21 @@ +using Xunit; + +namespace Tests +{ + /// + /// Represents a set of data for a theory. Data can be added to the data set using the collection initializer syntax. + /// + /// + public class TestData : TheoryData + { + /// + /// Adds a theory to the Test + /// + /// Name of the Test + /// Data used in the Test + public void Add(string testName, T testObject) + { + AddRow(testName, testObject); + } + } +} \ No newline at end of file diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 1e26252..108b314 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -3,6 +3,7 @@ netcoreapp2.2 false NU1701 + xUnit1026 From 7d7d393a3d72c86d7dd856c22578a2e2b7e8abc4 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 12 May 2019 01:59:55 +0200 Subject: [PATCH 157/443] Add tests for FormatDateTimeAsRemaining --- .../TranslationGuildContext.test.cs | 71 +++++++++++++++++++ Tests/Tests.csproj | 1 + 2 files changed, 72 insertions(+) create mode 100644 Tests/Lib/Localization/TranslationGuildContext.test.cs diff --git a/Tests/Lib/Localization/TranslationGuildContext.test.cs b/Tests/Lib/Localization/TranslationGuildContext.test.cs new file mode 100644 index 0000000..90f1446 --- /dev/null +++ b/Tests/Lib/Localization/TranslationGuildContext.test.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using Geekbot.net.Lib.Localization; +using Moq; +using Xunit; + +namespace Tests.Lib.Localization +{ + public class TranslationGuildContext_test + { + public class FormatDateTimeAsRemainingTestDto + { + public DateTimeOffset DateTime { get; set; } + public string Expected { get; set; } + } + + public static TestData FormatDateTimeAsRemainingData => + new TestData + { + { + "Wait for days", + new FormatDateTimeAsRemainingTestDto + { + DateTime = DateTimeOffset.Now.AddDays(5), + Expected = "4 days, 23 hours, 59 minutes and 59 seconds" + } + }, + { + "Wait for minutes", + new FormatDateTimeAsRemainingTestDto + { + DateTime = DateTimeOffset.Now.AddMinutes(5), + Expected = "4 minutes and 59 seconds" + } + }, + { + "Wait for seconds", + new FormatDateTimeAsRemainingTestDto + { + DateTime = DateTimeOffset.Now.AddSeconds(5), + Expected = "4 seconds" + } + } + }; + + [Theory, MemberData(nameof(FormatDateTimeAsRemainingData))] + public void FormatDateTimeAsRemaining(string testName, FormatDateTimeAsRemainingTestDto testData) + { + var translationHandlerMock = new Mock(MockBehavior.Loose); + translationHandlerMock + .Setup(thm => thm.GetStrings("EN", "dateTime", "Days")) + .Returns(new List {{"day"}, {"days"}}); + translationHandlerMock + .Setup(thm => thm.GetStrings("EN", "dateTime", "Hours")) + .Returns(new List {{"hour"}, {"hours"}}); + translationHandlerMock + .Setup(thm => thm.GetStrings("EN", "dateTime", "Minutes")) + .Returns(new List {{"minute"}, {"minutes"}}); + translationHandlerMock + .Setup(thm => thm.GetStrings("EN", "dateTime", "Seconds")) + .Returns(new List {{"second"}, {"seconds"}}); + translationHandlerMock + .Setup(thm => thm.GetStrings("EN", "dateTime", "And")) + .Returns(new List {{"and"}}); + + var context = new TranslationGuildContext(translationHandlerMock.Object, "EN", new Dictionary>()); + var result = context.FormatDateTimeAsRemaining(testData.DateTime); + Assert.Equal(result, testData.Expected); + } + } +} \ No newline at end of file diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 108b314..771a9d4 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -7,6 +7,7 @@ + From a86a33635cf960c0a355ff362ad5cd0b46368db0 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 12 May 2019 12:25:42 +0200 Subject: [PATCH 158/443] Rename cookie command to cookies --- Geekbot.net/Commands/Rpg/Cookies.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Geekbot.net/Commands/Rpg/Cookies.cs b/Geekbot.net/Commands/Rpg/Cookies.cs index 6d527ef..e7192c6 100644 --- a/Geekbot.net/Commands/Rpg/Cookies.cs +++ b/Geekbot.net/Commands/Rpg/Cookies.cs @@ -14,7 +14,7 @@ using Geekbot.net.Lib.RandomNumberGenerator; namespace Geekbot.net.Commands.Rpg { [DisableInDirectMessage] - [Group("cookie")] + [Group("cookies")] public class Cookies : ModuleBase { private readonly DatabaseContext _database; From 4b67bb51d1f1a6fcac1b011b70eb39c9f2d5f48a Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 12 May 2019 12:31:31 +0200 Subject: [PATCH 159/443] Reflect cookies command change in translations --- Geekbot.net/Lib/Localization/Translations.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Geekbot.net/Lib/Localization/Translations.json b/Geekbot.net/Lib/Localization/Translations.json index 356deb5..617106d 100644 --- a/Geekbot.net/Lib/Localization/Translations.json +++ b/Geekbot.net/Lib/Localization/Translations.json @@ -119,7 +119,7 @@ "CHDE": [":red_circle: {0}, du chasch nid nomol es gliche rate"] } }, - "cookie": { + "cookies": { "GetCookies": { "EN": ["You got {0} cookies, there are now {1} cookies in you cookie jar"], "CHDE": ["Du häsch {0} guetzli becho, du häsch jetzt {1} guetzli ih dr büchse"] From 68a62ab4ec4f21bf98d0443ba742e5e2230fec53 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 12 May 2019 13:47:15 +0200 Subject: [PATCH 160/443] Add translations for the role command --- Geekbot.net/Commands/Admin/Role.cs | 34 ++++++++------- .../Lib/Localization/Translations.json | 42 +++++++++++++++++++ 2 files changed, 62 insertions(+), 14 deletions(-) diff --git a/Geekbot.net/Commands/Admin/Role.cs b/Geekbot.net/Commands/Admin/Role.cs index aa767d1..47d7246 100644 --- a/Geekbot.net/Commands/Admin/Role.cs +++ b/Geekbot.net/Commands/Admin/Role.cs @@ -10,6 +10,7 @@ using Geekbot.net.Database.Models; using Geekbot.net.Lib.CommandPreconditions; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; +using Geekbot.net.Lib.Localization; using Geekbot.net.Lib.ReactionListener; namespace Geekbot.net.Commands.Admin @@ -21,12 +22,14 @@ namespace Geekbot.net.Commands.Admin private readonly DatabaseContext _database; private readonly IErrorHandler _errorHandler; private readonly IReactionListener _reactionListener; + private readonly TranslationHandler _translationHandler; - public Role(DatabaseContext database, IErrorHandler errorHandler, IReactionListener reactionListener) + public Role(DatabaseContext database, IErrorHandler errorHandler, IReactionListener reactionListener, TranslationHandler translationHandler) { _database = database; _errorHandler = errorHandler; _reactionListener = reactionListener; + _translationHandler = translationHandler; } [Command(RunMode = RunMode.Async)] @@ -35,16 +38,17 @@ namespace Geekbot.net.Commands.Admin { try { + var transContext = await _translationHandler.GetGuildContext(Context); var roles = _database.RoleSelfService.Where(g => g.GuildId.Equals(Context.Guild.Id.AsLong())).ToList(); if (roles.Count == 0) { - await ReplyAsync("There are no roles configured for this server"); + await ReplyAsync(transContext.GetString("NoRolesConfigured")); return; } var sb = new StringBuilder(); - sb.AppendLine($"**Self Service Roles on {Context.Guild.Name}**"); - sb.AppendLine("To get a role, use `!role name`"); + sb.AppendLine(transContext.GetString("ListHeader", Context.Guild.Name)); + sb.AppendLine(transContext.GetString("ListInstruction")); foreach (var role in roles) sb.AppendLine($"- {role.WhiteListName}"); await ReplyAsync(sb.ToString()); } @@ -60,6 +64,7 @@ namespace Geekbot.net.Commands.Admin { try { + var transContext = await _translationHandler.GetGuildContext(Context); var roleName = roleNameRaw.ToLower(); var roleFromDb = _database.RoleSelfService.FirstOrDefault(e => e.GuildId.Equals(Context.Guild.Id.AsLong()) && e.WhiteListName.Equals(roleName)); @@ -69,23 +74,23 @@ namespace Geekbot.net.Commands.Admin var role = Context.Guild.Roles.First(r => r.Id == roleFromDb.RoleId.AsUlong()); if (role == null) { - await ReplyAsync("That role doesn't seem to exist"); + await ReplyAsync(transContext.GetString("RoleNotFound")); return; } if (guildUser.RoleIds.Contains(role.Id)) { await guildUser.RemoveRoleAsync(role); - await ReplyAsync($"Removed you from {role.Name}"); + await ReplyAsync(transContext.GetString("RemovedUserFromRole", role.Name)); return; } await guildUser.AddRoleAsync(role); - await ReplyAsync($"Added you to {role.Name}"); + await ReplyAsync(transContext.GetString("AddedUserFromRole", role.Name)); return; } - await ReplyAsync("That role doesn't seem to exist"); + await ReplyAsync(transContext.GetString("RoleNotFound")); } catch (HttpException e) { @@ -104,9 +109,10 @@ namespace Geekbot.net.Commands.Admin { try { + var transContext = await _translationHandler.GetGuildContext(Context); if (role.IsManaged) { - await ReplyAsync("You can't add a role that is managed by discord"); + await ReplyAsync(transContext.GetString("CannotAddManagedRole")); return; } @@ -116,8 +122,7 @@ namespace Geekbot.net.Commands.Admin || role.Permissions.BanMembers || role.Permissions.KickMembers) { - await ReplyAsync( - "You cannot add that role to self service because it contains one or more dangerous permissions"); + await ReplyAsync(transContext.GetString("CannotAddDangerousRole")); return; } @@ -128,7 +133,7 @@ namespace Geekbot.net.Commands.Admin WhiteListName = roleName }); await _database.SaveChangesAsync(); - await ReplyAsync($"Added {role.Name} to the whitelist"); + await ReplyAsync(transContext.GetString("CannotAddDangerousRole", role.Name)); } catch (Exception e) { @@ -143,17 +148,18 @@ namespace Geekbot.net.Commands.Admin { try { + var transContext = await _translationHandler.GetGuildContext(Context); var roleFromDb = _database.RoleSelfService.FirstOrDefault(e => e.GuildId.Equals(Context.Guild.Id.AsLong()) && e.WhiteListName.Equals(roleName)); if (roleFromDb != null) { _database.RoleSelfService.Remove(roleFromDb); await _database.SaveChangesAsync(); - await ReplyAsync($"Removed {roleName} from the whitelist"); + await ReplyAsync(transContext.GetString("RemovedRoleFromWhitelist", roleName)); return; } - await ReplyAsync("There is not whitelisted role with that name"); + await ReplyAsync(transContext.GetString("RoleNotFound")); } catch (Exception e) { diff --git a/Geekbot.net/Lib/Localization/Translations.json b/Geekbot.net/Lib/Localization/Translations.json index 617106d..6a864fc 100644 --- a/Geekbot.net/Lib/Localization/Translations.json +++ b/Geekbot.net/Lib/Localization/Translations.json @@ -148,5 +148,47 @@ "EN": ["You ate {0} cookies, you've only got {1} cookies left"], "CHDE": ["Du hesch {0} guetzli gesse und hesch jezt no {1} übrig"] } + }, + "role": { + "NoRolesConfigured": { + "EN": ["There are no roles configured for this server"], + "CHDE": ["Es sind kei rolle für dä server konfiguriert"] + }, + "ListHeader": { + "EN": ["**Self Service Roles on {0}**"], + "CHDE": ["**Self Service Rollene uf {0}**"] + }, + "ListInstruction": { + "EN": ["To get a role, use `!role [name]`"], + "CHDE": ["Zum ä rolle becho, schriib `!role [name]`"] + }, + "RoleNotFound": { + "EN": ["That role doesn't exist or is not on the whitelist"], + "CHDE": ["Die rolle gids nid or isch nid uf dr whitelist"] + }, + "RemovedUserFromRole": { + "EN": ["Removed you from {0}"], + "CHDE": ["Han di entfernt vo {0}"] + }, + "AddedUserFromRole": { + "EN": ["Added you to {0}"], + "CHDE": ["Han di hinzue gfüegt zu {0}"] + }, + "CannotAddManagedRole": { + "EN": ["You can't add a role that is managed by discord"], + "CHDE": ["Du chasch kei rolle hinzuefüge wo verwalted wird vo discord"] + }, + "CannotAddDangerousRole": { + "EN": ["You cannot add that role to self service because it contains one or more dangerous permissions"], + "CHDE": ["Du chasch die rolle nid hinzuefüge will er ein oder mehreri gföhrlichi berechtigunge het"] + }, + "AddedRoleToWhitelist": { + "EN": ["Added {0} to the whitelist"], + "CHDE": ["{0} isch zur whitelist hinzuegfüegt"] + }, + "RemovedRoleFromWhitelist": { + "EN": ["Removed {0} from the whitelist"], + "CHDE": ["{0} isch vo dr whitelist glöscht"] + } } } \ No newline at end of file From c6271cbaa0de233cf0fcc1f923268e4e463e06b0 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 12 May 2019 13:48:37 +0200 Subject: [PATCH 161/443] Remove AudioUtils --- Geekbot.net/Commands/Audio/Voice.cs | 101 --------------------------- Geekbot.net/Lib/Audio/AudioUtils.cs | 96 ------------------------- Geekbot.net/Lib/Audio/IAudioUtils.cs | 15 ---- Geekbot.net/Program.cs | 3 - 4 files changed, 215 deletions(-) delete mode 100644 Geekbot.net/Commands/Audio/Voice.cs delete mode 100644 Geekbot.net/Lib/Audio/AudioUtils.cs delete mode 100644 Geekbot.net/Lib/Audio/IAudioUtils.cs diff --git a/Geekbot.net/Commands/Audio/Voice.cs b/Geekbot.net/Commands/Audio/Voice.cs deleted file mode 100644 index b12d6e2..0000000 --- a/Geekbot.net/Commands/Audio/Voice.cs +++ /dev/null @@ -1,101 +0,0 @@ -using System; -using System.Threading.Tasks; -using Discord; -using Discord.Commands; -using Geekbot.net.Lib.Audio; -using Geekbot.net.Lib.ErrorHandling; - -namespace Geekbot.net.Commands.Audio -{ - public class Voice : ModuleBase - { - private readonly IAudioUtils _audioUtils; - private readonly IErrorHandler _errorHandler; - - public Voice(IErrorHandler errorHandler, IAudioUtils audioUtils) - { - _errorHandler = errorHandler; - _audioUtils = audioUtils; - } - -// [Command("join")] - public async Task JoinChannel() - { - try - { - // Get the audio channel - var channel = (Context.User as IGuildUser)?.VoiceChannel; - if (channel == null) - { - await Context.Channel.SendMessageAsync("You must be in a voice channel."); - return; - } - - var audioClient = await channel.ConnectAsync(); - _audioUtils.StoreAudioClient(Context.Guild.Id, audioClient); - await ReplyAsync($"Connected to {channel.Name}"); - } - catch (Exception e) - { - await _errorHandler.HandleCommandException(e, Context); - } - } - -// [Command("disconnect")] - public async Task DisconnectChannel() - { - try - { - var audioClient = _audioUtils.GetAudioClient(Context.Guild.Id); - if (audioClient == null) - { - await Context.Channel.SendMessageAsync("I'm not in a voice channel at the moment"); - return; - } - - await audioClient.StopAsync(); - await ReplyAsync("Disconnected from channel!"); - _audioUtils.Cleanup(Context.Guild.Id); - } - catch (Exception e) - { - await _errorHandler.HandleCommandException(e, Context); - _audioUtils.Cleanup(Context.Guild.Id); - } - } - -// [Command("ytplay")] - public async Task Ytplay(string url) - { - try - { - if (!url.Contains("youtube")) - { - await ReplyAsync("I can only play youtube videos"); - return; - } - var audioClient = _audioUtils.GetAudioClient(Context.Guild.Id); - if (audioClient == null) - { - await ReplyAsync("I'm not in a voice channel at the moment"); - return; - } - - var message = await Context.Channel.SendMessageAsync("Just a second, i'm still a bit slow at this"); - var ffmpeg = _audioUtils.CreateStreamFromYoutube(url, Context.Guild.Id); - var output = ffmpeg.StandardOutput.BaseStream; - await message.ModifyAsync(msg => msg.Content = "**Playing!** Please note that this feature is experimental"); - var discord = audioClient.CreatePCMStream(Discord.Audio.AudioApplication.Mixed); - await output.CopyToAsync(discord); - await discord.FlushAsync(); - _audioUtils.Cleanup(Context.Guild.Id); - } - catch (Exception e) - { - await _errorHandler.HandleCommandException(e, Context); - _audioUtils.Cleanup(Context.Guild.Id); - } - } - } - -} \ No newline at end of file diff --git a/Geekbot.net/Lib/Audio/AudioUtils.cs b/Geekbot.net/Lib/Audio/AudioUtils.cs deleted file mode 100644 index 024019d..0000000 --- a/Geekbot.net/Lib/Audio/AudioUtils.cs +++ /dev/null @@ -1,96 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Net; -using Discord.Audio; - -namespace Geekbot.net.Lib.Audio -{ - public class AudioUtils : IAudioUtils - { - private string _tempFolderPath; - private Dictionary _audioClients; - - public AudioUtils() - { - _audioClients = new Dictionary(); - _tempFolderPath = Path.GetFullPath("./tmp/"); - if (Directory.Exists(_tempFolderPath)) - { - Directory.Delete(_tempFolderPath, true); - } - Directory.CreateDirectory(_tempFolderPath); - } - - public IAudioClient GetAudioClient(ulong guildId) - { - return _audioClients[guildId]; - } - - public void StoreAudioClient(ulong guildId, IAudioClient client) - { - _audioClients[guildId] = client; - } - - public Process CreateStreamFromFile(string path) - { - var ffmpeg = new ProcessStartInfo - { - FileName = "ffmpeg", - Arguments = $"-i {path} -ac 2 -f s16le -ar 48000 pipe:1", - UseShellExecute = false, - RedirectStandardOutput = true, - }; - return Process.Start(ffmpeg); - } - - public Process CreateStreamFromYoutube(string url, ulong guildId) - { - var ytdlMediaUrl = GetYoutubeMediaUrl(url); - DownloadMediaUrl(ytdlMediaUrl, guildId); - return CreateStreamFromFile($"{_tempFolderPath}{guildId}"); - } - - public void Cleanup(ulong guildId) - { - File.Delete($"{_tempFolderPath}{guildId}"); - } - - private string GetYoutubeMediaUrl(string url) - { - var ytdl = new ProcessStartInfo() - { - FileName = "youtube-dl", - Arguments = $"-f bestaudio -g {url}", - UseShellExecute = false, - RedirectStandardOutput = true - }; - var output = Process.Start(ytdl).StandardOutput.ReadToEnd(); - if (string.IsNullOrWhiteSpace(output)) - { - throw new Exception("Could not get Youtube Media URL"); - } - return output; - } - - private void DownloadMediaUrl(string url, ulong guildId) - { - using (var web = new WebClient()) - { - web.DownloadFile(url, $"{_tempFolderPath}{guildId}"); - } -// var ffmpeg = new ProcessStartInfo -// { -// FileName = "ffmpeg", -// Arguments = $"-i \"{_tempFolderPath}{guildId}\" -c:a mp3 -b:a 256k {_tempFolderPath}{guildId}.mp3", -// UseShellExecute = false, -// RedirectStandardOutput = true, -// }; -// Process.Start(ffmpeg).WaitForExit(); -// File.Delete($"{_tempFolderPath}{guildId}"); - return; - } - - } -} \ No newline at end of file diff --git a/Geekbot.net/Lib/Audio/IAudioUtils.cs b/Geekbot.net/Lib/Audio/IAudioUtils.cs deleted file mode 100644 index 4af1293..0000000 --- a/Geekbot.net/Lib/Audio/IAudioUtils.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System.Diagnostics; -using Discord.Audio; - -namespace Geekbot.net.Lib.Audio -{ - public interface IAudioUtils - { - IAudioClient GetAudioClient(ulong guildId); - void StoreAudioClient(ulong guildId, IAudioClient client); - Process CreateStreamFromFile(string path); - Process CreateStreamFromYoutube(string url, ulong guildId); - void Cleanup(ulong guildId); - - } -} \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 43c16d1..27274d2 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -9,7 +9,6 @@ using Discord.WebSocket; using Geekbot.net.Database; using Geekbot.net.Lib; using Geekbot.net.Lib.AlmostRedis; -using Geekbot.net.Lib.Audio; using Geekbot.net.Lib.Clients; using Geekbot.net.Lib.Converters; using Geekbot.net.Lib.ErrorHandling; @@ -124,7 +123,6 @@ namespace Geekbot.net var emojiConverter = new EmojiConverter(); var mtgManaConverter = new MtgManaConverter(); var wikipediaClient = new WikipediaClient(); - var audioUtils = new AudioUtils(); var randomNumberGenerator = new RandomNumberGenerator(); _highscoreManager = new HighscoreManager(_databaseInitializer.Initialize(), _userRepository); @@ -138,7 +136,6 @@ namespace Geekbot.net _services.AddSingleton(malClient); _services.AddSingleton(mtgManaConverter); _services.AddSingleton(wikipediaClient); - _services.AddSingleton(audioUtils); _services.AddSingleton(randomNumberGenerator); _services.AddSingleton(_highscoreManager); _services.AddSingleton(_globalSettings); From 03d1607d646dcb3e02576c24626e637169d3e1a7 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 12 May 2019 13:49:01 +0200 Subject: [PATCH 162/443] Remove Poll command --- Geekbot.net/Commands/Utils/Poll.cs | 202 ----------------------------- 1 file changed, 202 deletions(-) delete mode 100644 Geekbot.net/Commands/Utils/Poll.cs diff --git a/Geekbot.net/Commands/Utils/Poll.cs b/Geekbot.net/Commands/Utils/Poll.cs deleted file mode 100644 index 617d6e2..0000000 --- a/Geekbot.net/Commands/Utils/Poll.cs +++ /dev/null @@ -1,202 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Discord; -using Discord.Commands; -using Geekbot.net.Database; -using Geekbot.net.Database.Models; -using Geekbot.net.Lib.CommandPreconditions; -using Geekbot.net.Lib.Converters; -using Geekbot.net.Lib.ErrorHandling; -using Geekbot.net.Lib.Extensions; -using Geekbot.net.Lib.UserRepository; - -namespace Geekbot.net.Commands.Utils -{ - [Group("poll")] - [DisableInDirectMessage] - public class Poll : ModuleBase - { - private readonly IEmojiConverter _emojiConverter; - private readonly IErrorHandler _errorHandler; - private readonly DatabaseContext _database; - private readonly IUserRepository _userRepository; - - public Poll(IErrorHandler errorHandler, DatabaseContext database, IEmojiConverter emojiConverter, IUserRepository userRepository) - { - _errorHandler = errorHandler; - _database = database; - _emojiConverter = emojiConverter; - _userRepository = userRepository; - } - - [Command(RunMode = RunMode.Async)] - [Summary("Check status of the current poll")] - public async Task Dflt() - { - try - { - var currentPoll = GetCurrentPoll(); - if (currentPoll.Question == null) - { - await ReplyAsync( - "There is no poll in this channel ongoing at the moment\r\nYou can create one with `!poll create question;option1;option2;option3`"); - return; - } - - await ReplyAsync("There is a poll running at the moment"); - } - catch (Exception e) - { - await _errorHandler.HandleCommandException(e, Context); - } - } - - [Command("create", RunMode = RunMode.Async)] - [Summary("Create a poll")] - public async Task Create([Remainder] [Summary("question;option1;option2")] - string rawPollString) - { - try - { - await ReplyAsync("Poll creation currently disabled"); - return; - -// var currentPoll = GetCurrentPoll(); -// if (currentPoll.Question != null && !currentPoll.IsFinshed) -// { -// await ReplyAsync("You have not finished you last poll yet. To finish it use `!poll end`"); -// return; -// } -// -// var pollList = rawPollString.Split(';').ToList(); -// if (pollList.Count <= 2) -// { -// await ReplyAsync( -// "You need a question with atleast 2 options, a valid creation would look like this `question;option1;option2`"); -// return; -// } -// -// var question = pollList[0]; -// pollList.RemoveAt(0); -// -// var eb = new EmbedBuilder -// { -// Title = $"Poll by {Context.User.Username}", -// Description = question -// }; -// -// var options = new List(); -// var i = 1; -// pollList.ForEach(option => -// { -// options.Add(new PollQuestionModel() -// { -// OptionId = i, -// OptionText = option -// }); -// eb.AddInlineField($"Option {_emojiConverter.NumberToEmoji(i)}", option); -// i++; -// }); -// var pollMessage = await ReplyAsync("", false, eb.Build()); -// -// var poll = new PollModel() -// { -// Creator = Context.User.Id.AsLong(), -// MessageId = pollMessage.Id.AsLong(), -// IsFinshed = false, -// Question = question, -// Options = options -// }; -// _database.Polls.Add(poll); -// -// i = 1; -// pollList.ForEach(option => -// { -// pollMessage.AddReactionAsync(new Emoji(_emojiConverter.NumberToEmoji(i))); -// Task.Delay(500); -// i++; -// }); - } - catch (Exception e) - { - await _errorHandler.HandleCommandException(e, Context); - } - } - - [Command("end", RunMode = RunMode.Async)] - [Summary("End the current poll")] - public async Task End() - { - try - { - var currentPoll = GetCurrentPoll(); - if (currentPoll.Question == null || currentPoll.IsFinshed) - { - await ReplyAsync("There is no ongoing poll at the moment"); - return; - } - - currentPoll = await GetPollResults(currentPoll); - var sb = new StringBuilder(); - sb.AppendLine("**Poll Results**"); - sb.AppendLine(currentPoll.Question); - foreach (var result in currentPoll.Options) sb.AppendLine($"{result.Votes} - {result.OptionText}"); - await ReplyAsync(sb.ToString()); - currentPoll.IsFinshed = true; - _database.Polls.Update(currentPoll); - } - catch (Exception e) - { - await _errorHandler.HandleCommandException(e, Context); - } - } - - private PollModel GetCurrentPoll() - { - try - { - var currentPoll = _database.Polls.FirstOrDefault(poll => - poll.ChannelId.Equals(Context.Channel.Id.AsLong()) && - poll.GuildId.Equals(Context.Guild.Id.AsLong()) - ); - return currentPoll ?? new PollModel(); - - } - catch - { - return new PollModel(); - } - } - - private async Task GetPollResults(PollModel poll) - { - var message = (IUserMessage) await Context.Channel.GetMessageAsync(poll.MessageId.AsUlong()); - - var results = new Dictionary(); - foreach (var r in message.Reactions) - { - try - { - results.Add(r.Key.Name.ToCharArray()[0], r.Value.ReactionCount); - } - catch - { - // ignored - } - } - - foreach (var q in poll.Options) - { - q.Votes = results.FirstOrDefault(e => e.Key.Equals(q.OptionId)).Value; - } - - return poll; - -// var sortedValues = results.OrderBy(e => e.Value); -// return sortedValues; - } - } -} \ No newline at end of file From e5742165d1236b0dfa3acb4f09f1259c6b380da6 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 12 May 2019 15:29:22 +0200 Subject: [PATCH 163/443] Parially translate quote --- Geekbot.net/Commands/Admin/Role.cs | 4 +-- Geekbot.net/Commands/Utils/Quote/Quote.cs | 27 ++++++++++++------- .../Lib/Localization/Translations.json | 26 ++++++++++++++++++ 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/Geekbot.net/Commands/Admin/Role.cs b/Geekbot.net/Commands/Admin/Role.cs index 47d7246..f0aa7bd 100644 --- a/Geekbot.net/Commands/Admin/Role.cs +++ b/Geekbot.net/Commands/Admin/Role.cs @@ -22,9 +22,9 @@ namespace Geekbot.net.Commands.Admin private readonly DatabaseContext _database; private readonly IErrorHandler _errorHandler; private readonly IReactionListener _reactionListener; - private readonly TranslationHandler _translationHandler; + private readonly ITranslationHandler _translationHandler; - public Role(DatabaseContext database, IErrorHandler errorHandler, IReactionListener reactionListener, TranslationHandler translationHandler) + public Role(DatabaseContext database, IErrorHandler errorHandler, IReactionListener reactionListener, ITranslationHandler translationHandler) { _database = database; _errorHandler = errorHandler; diff --git a/Geekbot.net/Commands/Utils/Quote/Quote.cs b/Geekbot.net/Commands/Utils/Quote/Quote.cs index a6f3ec9..84a3786 100644 --- a/Geekbot.net/Commands/Utils/Quote/Quote.cs +++ b/Geekbot.net/Commands/Utils/Quote/Quote.cs @@ -8,6 +8,7 @@ using Geekbot.net.Database.Models; using Geekbot.net.Lib.CommandPreconditions; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; +using Geekbot.net.Lib.Localization; using Geekbot.net.Lib.Polyfills; using Geekbot.net.Lib.RandomNumberGenerator; @@ -20,12 +21,14 @@ namespace Geekbot.net.Commands.Utils.Quote private readonly IErrorHandler _errorHandler; private readonly DatabaseContext _database; private readonly IRandomNumberGenerator _randomNumberGenerator; + private readonly ITranslationHandler _translationHandler; - public Quote(IErrorHandler errorHandler, DatabaseContext database, IRandomNumberGenerator randomNumberGenerator) + public Quote(IErrorHandler errorHandler, DatabaseContext database, IRandomNumberGenerator randomNumberGenerator, ITranslationHandler translationHandler) { _errorHandler = errorHandler; _database = database; _randomNumberGenerator = randomNumberGenerator; + _translationHandler = translationHandler; } [Command] @@ -38,7 +41,8 @@ namespace Geekbot.net.Commands.Utils.Quote if (!s.Any()) { - await ReplyAsync("This server doesn't seem to have any quotes yet. You can add a quote with `!quote save @user` or `!quote save `"); + var transContext = await _translationHandler.GetGuildContext(Context); + await ReplyAsync(transContext.GetString("NoQuotesFound")); return; } @@ -60,15 +64,16 @@ namespace Geekbot.net.Commands.Utils.Quote { try { + var transContext = await _translationHandler.GetGuildContext(Context); if (user.Id == Context.Message.Author.Id) { - await ReplyAsync("You can't save your own quotes..."); + await ReplyAsync(transContext.GetString("CannotSaveOwnQuotes")); return; } if (user.IsBot) { - await ReplyAsync("You can't save quotes by a bot..."); + await ReplyAsync(transContext.GetString("CannotQuoteBots")); return; } @@ -80,7 +85,7 @@ namespace Geekbot.net.Commands.Utils.Quote await _database.SaveChangesAsync(); var embed = QuoteBuilder(quote); - await ReplyAsync("**Quote Added**", false, embed.Build()); + await ReplyAsync(transContext.GetString("QuoteAdded"), false, embed.Build()); } catch (Exception e) { @@ -95,16 +100,17 @@ namespace Geekbot.net.Commands.Utils.Quote { try { + var transContext = await _translationHandler.GetGuildContext(Context); var message = await Context.Channel.GetMessageAsync(messageId); if (message.Author.Id == Context.Message.Author.Id) { - await ReplyAsync("You can't save your own quotes..."); + await ReplyAsync(transContext.GetString("CannotSaveOwnQuotes")); return; } if (message.Author.IsBot) { - await ReplyAsync("You can't save quotes by a bot..."); + await ReplyAsync(transContext.GetString("CannotQuoteBots")); return; } @@ -113,7 +119,7 @@ namespace Geekbot.net.Commands.Utils.Quote await _database.SaveChangesAsync(); var embed = QuoteBuilder(quote); - await ReplyAsync("**Quote Added**", false, embed.Build()); + await ReplyAsync(transContext.GetString("QuoteAdded"), false, embed.Build()); } catch (Exception e) { @@ -168,17 +174,18 @@ namespace Geekbot.net.Commands.Utils.Quote { try { + var transContext = await _translationHandler.GetGuildContext(Context); var quote = _database.Quotes.Where(e => e.GuildId == Context.Guild.Id.AsLong() && e.InternalId == id)?.FirstOrDefault(); if (quote != null) { _database.Quotes.Remove(quote); await _database.SaveChangesAsync(); var embed = QuoteBuilder(quote); - await ReplyAsync($"**Removed #{id}**", false, embed.Build()); + await ReplyAsync(transContext.GetString("Removed", id), false, embed.Build()); } else { - await ReplyAsync("I couldn't find a quote with that id :disappointed:"); + await ReplyAsync(transContext.GetString("NotFoundWithId")); } } catch (Exception e) diff --git a/Geekbot.net/Lib/Localization/Translations.json b/Geekbot.net/Lib/Localization/Translations.json index 6a864fc..689085e 100644 --- a/Geekbot.net/Lib/Localization/Translations.json +++ b/Geekbot.net/Lib/Localization/Translations.json @@ -190,5 +190,31 @@ "EN": ["Removed {0} from the whitelist"], "CHDE": ["{0} isch vo dr whitelist glöscht"] } + }, + "quote": { + "NoQuotesFound": { + "EN": ["This server doesn't seem to have any quotes yet. You can add a quote with `!quote save @user` or `!quote save `"], + "CHDE": ["Dä server het no kei quotes. Du chasch quotes hinzuefüege mit `!quote save @user` oder `!quote save `"] + }, + "CannotSaveOwnQuotes": { + "EN": ["You can't save your own quotes..."], + "CHDE": ["Du chasch kei quotes vo dir selber speichere..."] + }, + "CannotQuoteBots": { + "EN": ["You can't save quotes by a bot..."], + "CHDE": ["Du chasch kei quotes vomne bot speichere..."] + }, + "QuoteAdded": { + "EN": ["**Quote Added**"], + "CHDE": ["**Quote hinzugfüegt**"] + }, + "Removed": { + "EN": ["**Removed #{0}**"], + "CHDE": ["**#{0} glöscht**"] + }, + "NotFoundWithId": { + "EN": ["I couldn't find a quote with that ID :disappointed:"], + "CHDE": ["Ich chan kei quote finde mit därri ID :disappointed:"] + } } } \ No newline at end of file From e0e76d7c27a80346ef34aedb3e62b4d48678035b Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 12 May 2019 15:40:28 +0200 Subject: [PATCH 164/443] Use YAML for translations --- Geekbot.net/Geekbot.net.csproj | 4 +- .../Lib/Localization/TranslationHandler.cs | 14 +- .../Lib/Localization/Translations.json | 220 --------------- Geekbot.net/Lib/Localization/Translations.yml | 265 ++++++++++++++++++ 4 files changed, 278 insertions(+), 225 deletions(-) delete mode 100644 Geekbot.net/Lib/Localization/Translations.json create mode 100644 Geekbot.net/Lib/Localization/Translations.yml diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index fcf7b95..405c17e 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -58,6 +58,7 @@ 4.3.0 + @@ -87,9 +88,6 @@ PreserveNewest - - Always - Always diff --git a/Geekbot.net/Lib/Localization/TranslationHandler.cs b/Geekbot.net/Lib/Localization/TranslationHandler.cs index 8100478..51c516c 100644 --- a/Geekbot.net/Lib/Localization/TranslationHandler.cs +++ b/Geekbot.net/Lib/Localization/TranslationHandler.cs @@ -9,6 +9,8 @@ using Geekbot.net.Database.Models; using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Logger; using Utf8Json; +using YamlDotNet.RepresentationModel; +using YamlDotNet.Serialization; namespace Geekbot.net.Lib.Localization { @@ -32,8 +34,15 @@ namespace Geekbot.net.Lib.Localization { try { - var translationFile = File.ReadAllText(Path.GetFullPath("./Lib/Localization/Translations.json")); - var rawTranslations = JsonSerializer.Deserialize>>>>(translationFile); + // Read the file + var translationFile = File.ReadAllText(Path.GetFullPath("./Lib/Localization/Translations.yml")); + + // Deserialize + var input = new StringReader(translationFile); + var deserializer = new DeserializerBuilder().Build(); + var rawTranslations = deserializer.Deserialize>>>>(input); + + // Sort var sortedPerLanguage = new Dictionary>>>(); foreach (var command in rawTranslations) { @@ -68,6 +77,7 @@ namespace Geekbot.net.Lib.Localization } _translations = sortedPerLanguage; + // Find Languages SupportedLanguages = new List(); foreach (var lang in sortedPerLanguage) { diff --git a/Geekbot.net/Lib/Localization/Translations.json b/Geekbot.net/Lib/Localization/Translations.json deleted file mode 100644 index 689085e..0000000 --- a/Geekbot.net/Lib/Localization/Translations.json +++ /dev/null @@ -1,220 +0,0 @@ -{ - "dateTime": { - "Days": { - "EN": ["day", "days"], - "CHDE": ["tag", "täg"] - }, - "Hours": { - "EN": ["hour", "hours"], - "CHDE": ["stund", "stunde"] - }, - "Minutes": { - "EN": ["minute", "minutes"], - "CHDE": ["minute", "minute"] - }, - "Seconds": { - "EN": ["second", "seconds"], - "CHDE": ["sekunde", "sekunde"] - }, - "And": { - "EN": ["and"], - "CHDE": ["und"] - } - }, - "admin": { - "NewLanguageSet": { - "EN": ["I will reply in english from now on"], - "CHDE": ["I werd ab jetzt uf schwiizerdüütsch antworte, äuuä"] - }, - "GetLanguage": { - "EN": ["I'm talking english"], - "CHDE": ["I red schwiizerdüütsch"] - } - }, - "errorHandler": { - "SomethingWentWrong": { - "EN": ["Something went wrong :confused:"], - "CHDE": ["Öppis isch schief gange :confused:"] - } - }, - "httpErrors": { - "403": { - "EN": ["Seems like i don't have enough permission to that :confused:"], - "CHDE": ["Gseht danach us das ich nid gnueg recht han zum das mache :confused:"] - } - }, - "choose": { - "Choice": { - "EN": ["I Choose **{0}**"], - "CHDE": ["I nimme **{0}**"] - } - }, - "good": { - "CannotChangeOwn": { - "EN": ["Sorry {0}, but you can't give yourself karma"], - "CHDE": ["Sorry {0}, aber du chasch dr selber kei karma geh"] - }, - "WaitUntill": { - "EN": ["Sorry {0}, but you have to wait {1} before you can give karma again..."], - "CHDE": ["Sorry {0}, aber du musch no {1} warte bisch d wieder karma chasch geh..."] - }, - "Increased": { - "EN": ["Karma gained"], - "CHDE": ["Karma becho"] - }, - "By": { - "EN": ["By"], - "CHDE": ["Vo"] - }, - "Amount": { - "EN": ["Amount"], - "CHDE": ["Mengi"] - }, - "Current": { - "EN": ["Current"], - "CHDE": ["Jetzt"] - } - }, - "bad": { - "CannotChangeOwn": { - "EN": ["Sorry {0}, but you can't lower your own karma"], - "CHDE": ["Sorry {0}, aber du chasch dr din eigete karma nid weg neh"] - }, - "WaitUntill": { - "EN": ["Sorry {0}, but you have to wait {1} before you can lower karma again..."], - "CHDE": ["Sorry {0}, aber du musch no {1} warte bisch d wieder karma chasch senke..."] - }, - "Decreased": { - "EN": ["Karma lowered"], - "CHDE": ["Karma gsenkt"] - }, - "By": { - "EN": ["By"], - "CHDE": ["Vo"] - }, - "Amount": { - "EN": ["Amount"], - "CHDE": ["Mengi"] - }, - "Current": { - "EN": ["Current"], - "CHDE": ["Jetzt"] - } - }, - "roll": { - "Rolled": { - "EN": ["{0}, you rolled {1}, your guess was {2}"], - "CHDE": ["{0}, du hesch {1} grollt und hesch {2} grate"] - }, - "Gratz": { - "EN": ["Congratulations {0}, your guess was correct!"], - "CHDE": ["Gratuliere {0}, du hesch richtig grate!"] - }, - "RolledNoGuess": { - "EN": ["{0}, you rolled {1}"], - "CHDE": ["{0}, du hesch {1} grollt"] - }, - "NoPrevGuess": { - "EN": [":red_circle: {0}, you can't guess the same number again"], - "CHDE": [":red_circle: {0}, du chasch nid nomol es gliche rate"] - } - }, - "cookies": { - "GetCookies": { - "EN": ["You got {0} cookies, there are now {1} cookies in you cookie jar"], - "CHDE": ["Du häsch {0} guetzli becho, du häsch jetzt {1} guetzli ih dr büchse"] - }, - "WaitForMoreCookies": { - "EN": ["You already got cookies in the last 24 hours, you can have more cookies in {0}"], - "CHDE": ["Du hesch scho guetzli becho ih de letzti 24 stund, du chasch meh ha in {0}"] - }, - "InYourJar": { - "EN": ["There are {0} cookies in you cookie jar"], - "CHDE": ["Es hät {0} guetzli ih dineri büchs"] - }, - "Given": { - "EN": ["You gave {0} cookies to {1}"], - "CHDE": ["Du hesch {1} {0} guetzli geh"] - }, - "NotEnoughToGive": { - "EN": ["You don't have enough cookies"], - "CHDE": ["Du hesch nid gnueg guetzli"] - }, - "NotEnoughCookiesToEat": { - "EN": ["Your cookie jar looks almost empty, you should probably not eat a cookie"], - "CHDE": ["Du hesch chuum no guetzli ih dineri büchs, du sötsch warschinli keini esse"] - }, - "AteCookies": { - "EN": ["You ate {0} cookies, you've only got {1} cookies left"], - "CHDE": ["Du hesch {0} guetzli gesse und hesch jezt no {1} übrig"] - } - }, - "role": { - "NoRolesConfigured": { - "EN": ["There are no roles configured for this server"], - "CHDE": ["Es sind kei rolle für dä server konfiguriert"] - }, - "ListHeader": { - "EN": ["**Self Service Roles on {0}**"], - "CHDE": ["**Self Service Rollene uf {0}**"] - }, - "ListInstruction": { - "EN": ["To get a role, use `!role [name]`"], - "CHDE": ["Zum ä rolle becho, schriib `!role [name]`"] - }, - "RoleNotFound": { - "EN": ["That role doesn't exist or is not on the whitelist"], - "CHDE": ["Die rolle gids nid or isch nid uf dr whitelist"] - }, - "RemovedUserFromRole": { - "EN": ["Removed you from {0}"], - "CHDE": ["Han di entfernt vo {0}"] - }, - "AddedUserFromRole": { - "EN": ["Added you to {0}"], - "CHDE": ["Han di hinzue gfüegt zu {0}"] - }, - "CannotAddManagedRole": { - "EN": ["You can't add a role that is managed by discord"], - "CHDE": ["Du chasch kei rolle hinzuefüge wo verwalted wird vo discord"] - }, - "CannotAddDangerousRole": { - "EN": ["You cannot add that role to self service because it contains one or more dangerous permissions"], - "CHDE": ["Du chasch die rolle nid hinzuefüge will er ein oder mehreri gföhrlichi berechtigunge het"] - }, - "AddedRoleToWhitelist": { - "EN": ["Added {0} to the whitelist"], - "CHDE": ["{0} isch zur whitelist hinzuegfüegt"] - }, - "RemovedRoleFromWhitelist": { - "EN": ["Removed {0} from the whitelist"], - "CHDE": ["{0} isch vo dr whitelist glöscht"] - } - }, - "quote": { - "NoQuotesFound": { - "EN": ["This server doesn't seem to have any quotes yet. You can add a quote with `!quote save @user` or `!quote save `"], - "CHDE": ["Dä server het no kei quotes. Du chasch quotes hinzuefüege mit `!quote save @user` oder `!quote save `"] - }, - "CannotSaveOwnQuotes": { - "EN": ["You can't save your own quotes..."], - "CHDE": ["Du chasch kei quotes vo dir selber speichere..."] - }, - "CannotQuoteBots": { - "EN": ["You can't save quotes by a bot..."], - "CHDE": ["Du chasch kei quotes vomne bot speichere..."] - }, - "QuoteAdded": { - "EN": ["**Quote Added**"], - "CHDE": ["**Quote hinzugfüegt**"] - }, - "Removed": { - "EN": ["**Removed #{0}**"], - "CHDE": ["**#{0} glöscht**"] - }, - "NotFoundWithId": { - "EN": ["I couldn't find a quote with that ID :disappointed:"], - "CHDE": ["Ich chan kei quote finde mit därri ID :disappointed:"] - } - } -} \ No newline at end of file diff --git a/Geekbot.net/Lib/Localization/Translations.yml b/Geekbot.net/Lib/Localization/Translations.yml new file mode 100644 index 0000000..02e13f4 --- /dev/null +++ b/Geekbot.net/Lib/Localization/Translations.yml @@ -0,0 +1,265 @@ +--- +dateTime: + Days: + EN: + - "day" + - "days" + CHDE: + - "tag" + - "täg" + Hours: + EN: + - "hour" + - "hours" + CHDE: + - "stund" + - "stunde" + Minutes: + EN: + - "minute" + - "minutes" + CHDE: + - "minute" + - "minute" + Seconds: + EN: + - "second" + - "seconds" + CHDE: + - "sekunde" + - "sekunde" + And: + EN: + - "and" + CHDE: + - "und" +admin: + NewLanguageSet: + EN: + - "I will reply in english from now on" + CHDE: + - "I werd ab jetzt uf schwiizerdüütsch antworte, äuuä" + GetLanguage: + EN: + - "I'm talking english" + CHDE: + - "I red schwiizerdüütsch" +errorHandler: + SomethingWentWrong: + EN: + - "Something went wrong :confused:" + CHDE: + - "Öppis isch schief gange :confused:" +httpErrors: + 403: + EN: + - "Seems like i don't have enough permission to that :confused:" + CHDE: + - "Gseht danach us das ich nid gnueg recht han zum das mache :confused:" +choose: + Choice: + EN: + - "I Choose **{0}**" + CHDE: + - "I nimme **{0}**" +good: + CannotChangeOwn: + EN: + - "Sorry {0}, but you can't give yourself karma" + CHDE: + - "Sorry {0}, aber du chasch dr selber kei karma geh" + WaitUntill: + EN: + - "Sorry {0}, but you have to wait {1} before you can give karma again..." + CHDE: + - "Sorry {0}, aber du musch no {1} warte bisch d wieder karma chasch geh..." + Increased: + EN: + - "Karma gained" + CHDE: + - "Karma becho" + By: + EN: + - "By" + CHDE: + - "Vo" + Amount: + EN: + - "Amount" + CHDE: + - "Mengi" + Current: + EN: + - "Current" + CHDE: + - "Jetzt" +bad: + CannotChangeOwn: + EN: + - "Sorry {0}, but you can't lower your own karma" + CHDE: + - "Sorry {0}, aber du chasch dr din eigete karma nid weg neh" + WaitUntill: + EN: + - "Sorry {0}, but you have to wait {1} before you can lower karma again..." + CHDE: + - "Sorry {0}, aber du musch no {1} warte bisch d wieder karma chasch senke..." + Decreased: + EN: + - "Karma lowered" + CHDE: + - "Karma gsenkt" + By: + EN: + - "By" + CHDE: + - "Vo" + Amount: + EN: + - "Amount" + CHDE: + - "Mengi" + Current: + EN: + - "Current" + CHDE: + - "Jetzt" +roll: + Rolled: + EN: + - "{0}, you rolled {1}, your guess was {2}" + CHDE: + - "{0}, du hesch {1} grollt und hesch {2} grate" + Gratz: + EN: + - "Congratulations {0}, your guess was correct!" + CHDE: + - "Gratuliere {0}, du hesch richtig grate!" + RolledNoGuess: + EN: + - "{0}, you rolled {1}" + CHDE: + - "{0}, du hesch {1} grollt" + NoPrevGuess: + EN: + - ":red_circle: {0}, you can't guess the same number again" + CHDE: + - ":red_circle: {0}, du chasch nid nomol es gliche rate" +cookies: + GetCookies: + EN: + - "You got {0} cookies, there are now {1} cookies in you cookie jar" + CHDE: + - "Du häsch {0} guetzli becho, du häsch jetzt {1} guetzli ih dr büchse" + WaitForMoreCookies: + EN: + - "You already got cookies in the last 24 hours, you can have more cookies in {0}" + CHDE: + - "Du hesch scho guetzli becho ih de letzti 24 stund, du chasch meh ha in {0}" + InYourJar: + EN: + - "There are {0} cookies in you cookie jar" + CHDE: + - "Es hät {0} guetzli ih dineri büchs" + Given: + EN: + - "You gave {0} cookies to {1}" + CHDE: + - "Du hesch {1} {0} guetzli geh" + NotEnoughToGive: + EN: + - "You don't have enough cookies" + CHDE: + - "Du hesch nid gnueg guetzli" + NotEnoughCookiesToEat: + EN: + - "Your cookie jar looks almost empty, you should probably not eat a cookie" + CHDE: + - "Du hesch chuum no guetzli ih dineri büchs, du sötsch warschinli keini esse" + AteCookies: + EN: + - "You ate {0} cookies, you've only got {1} cookies left" + CHDE: + - "Du hesch {0} guetzli gesse und hesch jezt no {1} übrig" +role: + NoRolesConfigured: + EN: + - "There are no roles configured for this server" + CHDE: + - "Es sind kei rolle für dä server konfiguriert" + ListHeader: + EN: + - "**Self Service Roles on {0}**" + CHDE: + - "**Self Service Rollene uf {0}**" + ListInstruction: + EN: + - "To get a role, use `!role [name]`" + CHDE: + - "Zum ä rolle becho, schriib `!role [name]`" + RoleNotFound: + EN: + - "That role doesn't exist or is not on the whitelist" + CHDE: + - "Die rolle gids nid or isch nid uf dr whitelist" + RemovedUserFromRole: + EN: + - "Removed you from {0}" + CHDE: + - "Han di entfernt vo {0}" + AddedUserFromRole: + EN: + - "Added you to {0}" + CHDE: + - "Han di hinzue gfüegt zu {0}" + CannotAddManagedRole: + EN: + - "You can't add a role that is managed by discord" + CHDE: + - "Du chasch kei rolle hinzuefüge wo verwalted wird vo discord" + CannotAddDangerousRole: + EN: + - "You cannot add that role to self service because it contains one or more dangerous permissions" + CHDE: + - "Du chasch die rolle nid hinzuefüge will er ein oder mehreri gföhrlichi berechtigunge het" + AddedRoleToWhitelist: + EN: + - "Added {0} to the whitelist" + CHDE: + - "{0} isch zur whitelist hinzuegfüegt" + RemovedRoleFromWhitelist: + EN: + - "Removed {0} from the whitelist" + CHDE: + - "{0} isch vo dr whitelist glöscht" +quote: + NoQuotesFound: + EN: + - "This server doesn't seem to have any quotes yet. You can add a quote with `!quote save @user` or `!quote save `" + CHDE: + - "Dä server het no kei quotes. Du chasch quotes hinzuefüege mit `!quote save @user` oder `!quote save `" + CannotSaveOwnQuotes: + EN: + - "You can't save your own quotes..." + CHDE: + - "Du chasch kei quotes vo dir selber speichere..." + CannotQuoteBots: + EN: + - "You can't save quotes by a bot..." + CHDE: + - "Du chasch kei quotes vomne bot speichere..." + QuoteAdded: + EN: + - "**Quote Added**" + CHDE: + - "**Quote hinzugfüegt**" + Removed: + EN: + - "**Removed #{0}**" + CHDE: + - "**#{0} glöscht**" + NotFoundWithId: + EN: + - "I couldn't find a quote with that ID :disappointed:" + CHDE: + - "Ich chan kei quote finde mit därri ID :disappointed:" \ No newline at end of file From 53f894676c88370b281c7f4abb258034db76e159 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 12 May 2019 15:49:00 +0200 Subject: [PATCH 165/443] Revert back to using strings in stead of arrays in the translations --- .../Lib/Localization/ITranslationHandler.cs | 2 +- .../Localization/TranslationGuildContext.cs | 15 +- .../Lib/Localization/TranslationHandler.cs | 27 +- Geekbot.net/Lib/Localization/Translations.yml | 302 ++++++------------ 4 files changed, 120 insertions(+), 226 deletions(-) diff --git a/Geekbot.net/Lib/Localization/ITranslationHandler.cs b/Geekbot.net/Lib/Localization/ITranslationHandler.cs index bb70046..9cd0680 100644 --- a/Geekbot.net/Lib/Localization/ITranslationHandler.cs +++ b/Geekbot.net/Lib/Localization/ITranslationHandler.cs @@ -7,7 +7,7 @@ namespace Geekbot.net.Lib.Localization public interface ITranslationHandler { Task GetString(ulong guildId, string command, string stringName); - List GetStrings(string language, string command, string stringName); + string GetString(string language, string command, string stringName); Task> GetDict(ICommandContext context, string command); Task GetGuildContext(ICommandContext context); Task SetLanguage(ulong guildId, string language); diff --git a/Geekbot.net/Lib/Localization/TranslationGuildContext.cs b/Geekbot.net/Lib/Localization/TranslationGuildContext.cs index 3f6f923..6181c04 100644 --- a/Geekbot.net/Lib/Localization/TranslationGuildContext.cs +++ b/Geekbot.net/Lib/Localization/TranslationGuildContext.cs @@ -10,9 +10,9 @@ namespace Geekbot.net.Lib.Localization { public ITranslationHandler TranslationHandler { get; } public string Language { get; } - public Dictionary> Dict { get; } + public Dictionary Dict { get; } - public TranslationGuildContext(ITranslationHandler translationHandler, string language, Dictionary> dict) + public TranslationGuildContext(ITranslationHandler translationHandler, string language, Dictionary dict) { TranslationHandler = translationHandler; Language = language; @@ -21,7 +21,7 @@ namespace Geekbot.net.Lib.Localization public string GetString(string stringToFormat, params object[] args) { - return string.Format(Dict[stringToFormat].First() ?? "", args); + return string.Format(Dict[stringToFormat] ?? "", args); } public string FormatDateTimeAsRemaining(DateTimeOffset dateTime) @@ -54,7 +54,7 @@ namespace Geekbot.net.Lib.Localization { if (sb.Length > 0) { - var and = TranslationHandler.GetStrings(Language, "dateTime", "And").First(); + var and = TranslationHandler.GetString(Language, "dateTime", "And"); sb.AppendFormat(" {0} ", and); } var s = GetTimeString(TimeTypes.Seconds); @@ -69,13 +69,14 @@ namespace Geekbot.net.Lib.Localization return TranslationHandler.SetLanguage(guildId, language); } - private List GetTimeString(TimeTypes type) + private string GetTimeString(TimeTypes type) { - return TranslationHandler.GetStrings(Language, "dateTime", type.ToString()); + return TranslationHandler.GetString(Language, "dateTime", type.ToString()); } - private string GetSingOrPlur(int number, List versions) + private string GetSingOrPlur(int number, string rawString) { + var versions = rawString.Split('|'); return number == 1 ? versions[0] : versions[1]; } diff --git a/Geekbot.net/Lib/Localization/TranslationHandler.cs b/Geekbot.net/Lib/Localization/TranslationHandler.cs index 51c516c..3d1ee3a 100644 --- a/Geekbot.net/Lib/Localization/TranslationHandler.cs +++ b/Geekbot.net/Lib/Localization/TranslationHandler.cs @@ -19,7 +19,7 @@ namespace Geekbot.net.Lib.Localization private readonly DatabaseContext _database; private readonly IGeekbotLogger _logger; private readonly Dictionary _serverLanguages; - private Dictionary>>> _translations; + private Dictionary>> _translations; public TranslationHandler(DatabaseContext database, IGeekbotLogger logger) { @@ -40,10 +40,10 @@ namespace Geekbot.net.Lib.Localization // Deserialize var input = new StringReader(translationFile); var deserializer = new DeserializerBuilder().Build(); - var rawTranslations = deserializer.Deserialize>>>>(input); + var rawTranslations = deserializer.Deserialize>>>(input); // Sort - var sortedPerLanguage = new Dictionary>>>(); + var sortedPerLanguage = new Dictionary>>(); foreach (var command in rawTranslations) { foreach (var str in command.Value) @@ -52,8 +52,8 @@ namespace Geekbot.net.Lib.Localization { if (!sortedPerLanguage.ContainsKey(lang.Key)) { - var commandDict = new Dictionary>>(); - var strDict = new Dictionary> + var commandDict = new Dictionary>(); + var strDict = new Dictionary { {str.Key, lang.Value} }; @@ -62,7 +62,7 @@ namespace Geekbot.net.Lib.Localization } if (!sortedPerLanguage[lang.Key].ContainsKey(command.Key)) { - var strDict = new Dictionary> + var strDict = new Dictionary { {str.Key, lang.Value} }; @@ -122,22 +122,22 @@ namespace Geekbot.net.Lib.Localization public async Task GetString(ulong guildId, string command, string stringName) { var serverLang = await GetServerLanguage(guildId); - return GetStrings(serverLang, command, stringName).First(); + return GetString(serverLang, command, stringName); } - public List GetStrings(string language, string command, string stringName) + public string GetString(string language, string command, string stringName) { var translation = _translations[language][command][stringName]; - if (!string.IsNullOrWhiteSpace(translation.First())) return translation; + if (!string.IsNullOrWhiteSpace(translation)) return translation; translation = _translations[command][stringName]["EN"]; - if (string.IsNullOrWhiteSpace(translation.First())) + if (string.IsNullOrWhiteSpace(translation)) { _logger.Warning(LogSource.Geekbot, $"No translation found for {command} - {stringName}"); } return translation; } - private async Task>> GetDict(ICommandContext context) + private async Task> GetDict(ICommandContext context) { try { @@ -148,7 +148,7 @@ namespace Geekbot.net.Lib.Localization catch (Exception e) { _logger.Error(LogSource.Geekbot, "No translations for command found", e); - return new Dictionary>(); + return new Dictionary(); } } @@ -164,8 +164,7 @@ namespace Geekbot.net.Lib.Localization try { var serverLanguage = await GetServerLanguage(context.Guild?.Id ?? 0); - return _translations[serverLanguage][command] - .ToDictionary(dict => dict.Key, dict => dict.Value.First()); + return _translations[serverLanguage][command]; } catch (Exception e) { diff --git a/Geekbot.net/Lib/Localization/Translations.yml b/Geekbot.net/Lib/Localization/Translations.yml index 02e13f4..6b8d1cd 100644 --- a/Geekbot.net/Lib/Localization/Translations.yml +++ b/Geekbot.net/Lib/Localization/Translations.yml @@ -1,265 +1,159 @@ --- dateTime: Days: - EN: - - "day" - - "days" - CHDE: - - "tag" - - "täg" + EN: "day|days" + CHDE: "tag|täg" Hours: - EN: - - "hour" - - "hours" - CHDE: - - "stund" - - "stunde" + EN: "hour|hours" + CHDE: "stund|stunde" Minutes: - EN: - - "minute" - - "minutes" - CHDE: - - "minute" - - "minute" + EN: "minute|minutes" + CHDE: "minute|minute" Seconds: - EN: - - "second" - - "seconds" - CHDE: - - "sekunde" - - "sekunde" + EN: "second|seconds" + CHDE: "sekunde|sekunde" And: - EN: - - "and" - CHDE: - - "und" + EN: "and" + CHDE: "und" admin: NewLanguageSet: - EN: - - "I will reply in english from now on" - CHDE: - - "I werd ab jetzt uf schwiizerdüütsch antworte, äuuä" + EN: "I will reply in english from now on" + CHDE: "I werd ab jetzt uf schwiizerdüütsch antworte, äuuä" GetLanguage: - EN: - - "I'm talking english" - CHDE: - - "I red schwiizerdüütsch" + EN: "I'm talking english" + CHDE: "I red schwiizerdüütsch" errorHandler: SomethingWentWrong: - EN: - - "Something went wrong :confused:" - CHDE: - - "Öppis isch schief gange :confused:" + EN: "Something went wrong :confused:" + CHDE: "Öppis isch schief gange :confused:" httpErrors: 403: - EN: - - "Seems like i don't have enough permission to that :confused:" - CHDE: - - "Gseht danach us das ich nid gnueg recht han zum das mache :confused:" + EN: "Seems like i don't have enough permission to that :confused:" + CHDE: "Gseht danach us das ich nid gnueg recht han zum das mache :confused:" choose: Choice: - EN: - - "I Choose **{0}**" - CHDE: - - "I nimme **{0}**" + EN: "I Choose **{0}**" + CHDE: "I nimme **{0}**" good: CannotChangeOwn: - EN: - - "Sorry {0}, but you can't give yourself karma" - CHDE: - - "Sorry {0}, aber du chasch dr selber kei karma geh" + EN: "Sorry {0}, but you can't give yourself karma" + CHDE: "Sorry {0}, aber du chasch dr selber kei karma geh" WaitUntill: - EN: - - "Sorry {0}, but you have to wait {1} before you can give karma again..." - CHDE: - - "Sorry {0}, aber du musch no {1} warte bisch d wieder karma chasch geh..." + EN: "Sorry {0}, but you have to wait {1} before you can give karma again..." + CHDE: "Sorry {0}, aber du musch no {1} warte bisch d wieder karma chasch geh..." Increased: - EN: - - "Karma gained" - CHDE: - - "Karma becho" + EN: "Karma gained" + CHDE: "Karma becho" By: - EN: - - "By" - CHDE: - - "Vo" + EN: "By" + CHDE: "Vo" Amount: - EN: - - "Amount" - CHDE: - - "Mengi" + EN: "Amount" + CHDE: "Mengi" Current: - EN: - - "Current" - CHDE: - - "Jetzt" + EN: "Current" + CHDE: "Jetzt" bad: CannotChangeOwn: - EN: - - "Sorry {0}, but you can't lower your own karma" - CHDE: - - "Sorry {0}, aber du chasch dr din eigete karma nid weg neh" + EN: "Sorry {0}, but you can't lower your own karma" + CHDE: "Sorry {0}, aber du chasch dr din eigete karma nid weg neh" WaitUntill: - EN: - - "Sorry {0}, but you have to wait {1} before you can lower karma again..." - CHDE: - - "Sorry {0}, aber du musch no {1} warte bisch d wieder karma chasch senke..." + EN: "Sorry {0}, but you have to wait {1} before you can lower karma again..." + CHDE: "Sorry {0}, aber du musch no {1} warte bisch d wieder karma chasch senke..." Decreased: - EN: - - "Karma lowered" - CHDE: - - "Karma gsenkt" + EN: "Karma lowered" + CHDE: "Karma gsenkt" By: - EN: - - "By" - CHDE: - - "Vo" + EN: "By" + CHDE: "Vo" Amount: - EN: - - "Amount" - CHDE: - - "Mengi" + EN: "Amount" + CHDE: "Mengi" Current: - EN: - - "Current" - CHDE: - - "Jetzt" + EN: "Current" + CHDE: "Jetzt" roll: Rolled: - EN: - - "{0}, you rolled {1}, your guess was {2}" - CHDE: - - "{0}, du hesch {1} grollt und hesch {2} grate" + EN: "{0}, you rolled {1}, your guess was {2}" + CHDE: "{0}, du hesch {1} grollt und hesch {2} grate" Gratz: - EN: - - "Congratulations {0}, your guess was correct!" - CHDE: - - "Gratuliere {0}, du hesch richtig grate!" + EN: "Congratulations {0}, your guess was correct!" + CHDE: "Gratuliere {0}, du hesch richtig grate!" RolledNoGuess: - EN: - - "{0}, you rolled {1}" - CHDE: - - "{0}, du hesch {1} grollt" + EN: "{0}, you rolled {1}" + CHDE: "{0}, du hesch {1} grollt" NoPrevGuess: - EN: - - ":red_circle: {0}, you can't guess the same number again" - CHDE: - - ":red_circle: {0}, du chasch nid nomol es gliche rate" + EN: ":red_circle: {0}, you can't guess the same number again" + CHDE: ":red_circle: {0}, du chasch nid nomol es gliche rate" cookies: GetCookies: - EN: - - "You got {0} cookies, there are now {1} cookies in you cookie jar" - CHDE: - - "Du häsch {0} guetzli becho, du häsch jetzt {1} guetzli ih dr büchse" + EN: "You got {0} cookies, there are now {1} cookies in you cookie jar" + CHDE: "Du häsch {0} guetzli becho, du häsch jetzt {1} guetzli ih dr büchse" WaitForMoreCookies: - EN: - - "You already got cookies in the last 24 hours, you can have more cookies in {0}" - CHDE: - - "Du hesch scho guetzli becho ih de letzti 24 stund, du chasch meh ha in {0}" + EN: "You already got cookies in the last 24 hours, you can have more cookies in {0}" + CHDE: "Du hesch scho guetzli becho ih de letzti 24 stund, du chasch meh ha in {0}" InYourJar: - EN: - - "There are {0} cookies in you cookie jar" - CHDE: - - "Es hät {0} guetzli ih dineri büchs" + EN: "There are {0} cookies in you cookie jar" + CHDE: "Es hät {0} guetzli ih dineri büchs" Given: - EN: - - "You gave {0} cookies to {1}" - CHDE: - - "Du hesch {1} {0} guetzli geh" + EN: "You gave {0} cookies to {1}" + CHDE: "Du hesch {1} {0} guetzli geh" NotEnoughToGive: - EN: - - "You don't have enough cookies" - CHDE: - - "Du hesch nid gnueg guetzli" + EN: "You don't have enough cookies" + CHDE: "Du hesch nid gnueg guetzli" NotEnoughCookiesToEat: - EN: - - "Your cookie jar looks almost empty, you should probably not eat a cookie" - CHDE: - - "Du hesch chuum no guetzli ih dineri büchs, du sötsch warschinli keini esse" + EN: "Your cookie jar looks almost empty, you should probably not eat a cookie" + CHDE: "Du hesch chuum no guetzli ih dineri büchs, du sötsch warschinli keini esse" AteCookies: - EN: - - "You ate {0} cookies, you've only got {1} cookies left" - CHDE: - - "Du hesch {0} guetzli gesse und hesch jezt no {1} übrig" + EN: "You ate {0} cookies, you've only got {1} cookies left" + CHDE: "Du hesch {0} guetzli gesse und hesch jezt no {1} übrig" role: NoRolesConfigured: - EN: - - "There are no roles configured for this server" - CHDE: - - "Es sind kei rolle für dä server konfiguriert" + EN: "There are no roles configured for this server" + CHDE: "Es sind kei rolle für dä server konfiguriert" ListHeader: - EN: - - "**Self Service Roles on {0}**" - CHDE: - - "**Self Service Rollene uf {0}**" + EN: "**Self Service Roles on {0}**" + CHDE: "**Self Service Rollene uf {0}**" ListInstruction: - EN: - - "To get a role, use `!role [name]`" - CHDE: - - "Zum ä rolle becho, schriib `!role [name]`" + EN: "To get a role, use `!role [name]`" + CHDE: "Zum ä rolle becho, schriib `!role [name]`" RoleNotFound: - EN: - - "That role doesn't exist or is not on the whitelist" - CHDE: - - "Die rolle gids nid or isch nid uf dr whitelist" + EN: "That role doesn't exist or is not on the whitelist" + CHDE: "Die rolle gids nid or isch nid uf dr whitelist" RemovedUserFromRole: - EN: - - "Removed you from {0}" - CHDE: - - "Han di entfernt vo {0}" + EN: "Removed you from {0}" + CHDE: "Han di entfernt vo {0}" AddedUserFromRole: - EN: - - "Added you to {0}" - CHDE: - - "Han di hinzue gfüegt zu {0}" + EN: "Added you to {0}" + CHDE: "Han di hinzue gfüegt zu {0}" CannotAddManagedRole: - EN: - - "You can't add a role that is managed by discord" - CHDE: - - "Du chasch kei rolle hinzuefüge wo verwalted wird vo discord" + EN: "You can't add a role that is managed by discord" + CHDE: "Du chasch kei rolle hinzuefüge wo verwalted wird vo discord" CannotAddDangerousRole: - EN: - - "You cannot add that role to self service because it contains one or more dangerous permissions" - CHDE: - - "Du chasch die rolle nid hinzuefüge will er ein oder mehreri gföhrlichi berechtigunge het" + EN: "You cannot add that role to self service because it contains one or more dangerous permissions" + CHDE: "Du chasch die rolle nid hinzuefüge will er ein oder mehreri gföhrlichi berechtigunge het" AddedRoleToWhitelist: - EN: - - "Added {0} to the whitelist" - CHDE: - - "{0} isch zur whitelist hinzuegfüegt" + EN: "Added {0} to the whitelist" + CHDE: "{0} isch zur whitelist hinzuegfüegt" RemovedRoleFromWhitelist: - EN: - - "Removed {0} from the whitelist" - CHDE: - - "{0} isch vo dr whitelist glöscht" + EN: "Removed {0} from the whitelist" + CHDE: "{0} isch vo dr whitelist glöscht" quote: NoQuotesFound: - EN: - - "This server doesn't seem to have any quotes yet. You can add a quote with `!quote save @user` or `!quote save `" - CHDE: - - "Dä server het no kei quotes. Du chasch quotes hinzuefüege mit `!quote save @user` oder `!quote save `" + EN: "This server doesn't seem to have any quotes yet. You can add a quote with `!quote save @user` or `!quote save `" + CHDE: "Dä server het no kei quotes. Du chasch quotes hinzuefüege mit `!quote save @user` oder `!quote save `" CannotSaveOwnQuotes: - EN: - - "You can't save your own quotes..." - CHDE: - - "Du chasch kei quotes vo dir selber speichere..." + EN: "You can't save your own quotes..." + CHDE: "Du chasch kei quotes vo dir selber speichere..." CannotQuoteBots: - EN: - - "You can't save quotes by a bot..." - CHDE: - - "Du chasch kei quotes vomne bot speichere..." + EN: "You can't save quotes by a bot..." + CHDE: "Du chasch kei quotes vomne bot speichere..." QuoteAdded: - EN: - - "**Quote Added**" - CHDE: - - "**Quote hinzugfüegt**" + EN: "**Quote Added**" + CHDE: "**Quote hinzugfüegt**" Removed: - EN: - - "**Removed #{0}**" - CHDE: - - "**#{0} glöscht**" + EN: "**Removed #{0}**" + CHDE: "**#{0} glöscht**" NotFoundWithId: - EN: - - "I couldn't find a quote with that ID :disappointed:" - CHDE: - - "Ich chan kei quote finde mit därri ID :disappointed:" \ No newline at end of file + EN: "I couldn't find a quote with that ID :disappointed:" + CHDE: "Ich chan kei quote finde mit därri ID :disappointed:" \ No newline at end of file From 5efb146d18568448c1d05076ba8feba7d8d65d96 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 12 May 2019 15:57:58 +0200 Subject: [PATCH 166/443] Fix Unit tests --- Geekbot.net/Geekbot.net.csproj | 3 +++ .../TranslationGuildContext.test.cs | 22 +++++++++---------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 405c17e..9c5c5c7 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -94,6 +94,9 @@ PreserveNewest + + Always + diff --git a/Tests/Lib/Localization/TranslationGuildContext.test.cs b/Tests/Lib/Localization/TranslationGuildContext.test.cs index 90f1446..aeea0e1 100644 --- a/Tests/Lib/Localization/TranslationGuildContext.test.cs +++ b/Tests/Lib/Localization/TranslationGuildContext.test.cs @@ -48,22 +48,22 @@ namespace Tests.Lib.Localization { var translationHandlerMock = new Mock(MockBehavior.Loose); translationHandlerMock - .Setup(thm => thm.GetStrings("EN", "dateTime", "Days")) - .Returns(new List {{"day"}, {"days"}}); + .Setup(thm => thm.GetString("EN", "dateTime", "Days")) + .Returns("day|days"); translationHandlerMock - .Setup(thm => thm.GetStrings("EN", "dateTime", "Hours")) - .Returns(new List {{"hour"}, {"hours"}}); + .Setup(thm => thm.GetString("EN", "dateTime", "Hours")) + .Returns("hour|hours"); translationHandlerMock - .Setup(thm => thm.GetStrings("EN", "dateTime", "Minutes")) - .Returns(new List {{"minute"}, {"minutes"}}); + .Setup(thm => thm.GetString("EN", "dateTime", "Minutes")) + .Returns("minute|minutes"); translationHandlerMock - .Setup(thm => thm.GetStrings("EN", "dateTime", "Seconds")) - .Returns(new List {{"second"}, {"seconds"}}); + .Setup(thm => thm.GetString("EN", "dateTime", "Seconds")) + .Returns("second|seconds"); translationHandlerMock - .Setup(thm => thm.GetStrings("EN", "dateTime", "And")) - .Returns(new List {{"and"}}); + .Setup(thm => thm.GetString("EN", "dateTime", "And")) + .Returns("and"); - var context = new TranslationGuildContext(translationHandlerMock.Object, "EN", new Dictionary>()); + var context = new TranslationGuildContext(translationHandlerMock.Object, "EN", new Dictionary()); var result = context.FormatDateTimeAsRemaining(testData.DateTime); Assert.Equal(result, testData.Expected); } From 92c2d173eb64c9b373f97d384aa322d47bf3420b Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 12 May 2019 16:15:42 +0200 Subject: [PATCH 167/443] make HighscoreManager transient to solve a bug where the information reported would be wrong --- Geekbot.net/Program.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 27274d2..d04dcbb 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -40,7 +40,6 @@ namespace Geekbot.net private IUserRepository _userRepository; private RunParameters _runParameters; private IAlmostRedis _redis; - private IHighscoreManager _highscoreManager; private static void Main(string[] args) { @@ -124,7 +123,6 @@ namespace Geekbot.net var mtgManaConverter = new MtgManaConverter(); var wikipediaClient = new WikipediaClient(); var randomNumberGenerator = new RandomNumberGenerator(); - _highscoreManager = new HighscoreManager(_databaseInitializer.Initialize(), _userRepository); _services.AddSingleton(_redis); _services.AddSingleton(_userRepository); @@ -137,8 +135,8 @@ namespace Geekbot.net _services.AddSingleton(mtgManaConverter); _services.AddSingleton(wikipediaClient); _services.AddSingleton(randomNumberGenerator); - _services.AddSingleton(_highscoreManager); _services.AddSingleton(_globalSettings); + _services.AddTransient((e) => new HighscoreManager(_databaseInitializer.Initialize(), _userRepository)); _services.AddTransient((e) => _databaseInitializer.Initialize()); logger.Information(LogSource.Geekbot, "Connecting to Discord"); @@ -206,7 +204,8 @@ namespace Geekbot.net private Task StartWebApi() { _logger.Information(LogSource.Api, "Starting Webserver"); - WebApi.WebApiStartup.StartWebApi(_logger, _runParameters, _commands, _databaseInitializer.Initialize(), _client, _globalSettings, _highscoreManager); + var highscoreManager = new HighscoreManager(_databaseInitializer.Initialize(), _userRepository); + WebApi.WebApiStartup.StartWebApi(_logger, _runParameters, _commands, _databaseInitializer.Initialize(), _client, _globalSettings, highscoreManager); return Task.CompletedTask; } } From 5109cc03a444c5e145e62e062077fd958a21edb7 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 12 May 2019 16:38:43 +0200 Subject: [PATCH 168/443] Translate rank command --- Geekbot.net/Commands/Rpg/Cookies.cs | 2 +- Geekbot.net/Commands/User/Ranking/Rank.cs | 16 ++++++++++------ Geekbot.net/Lib/Localization/Translations.yml | 18 +++++++++++++++++- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/Geekbot.net/Commands/Rpg/Cookies.cs b/Geekbot.net/Commands/Rpg/Cookies.cs index e7192c6..6e4ff54 100644 --- a/Geekbot.net/Commands/Rpg/Cookies.cs +++ b/Geekbot.net/Commands/Rpg/Cookies.cs @@ -74,7 +74,7 @@ namespace Geekbot.net.Commands.Rpg [Command("give", RunMode = RunMode.Async)] [Summary("Give cookies to someone")] - public async Task GiveACookie([Summary("User")] IUser user, [Summary("amount")] int amount) + public async Task GiveACookie([Summary("User")] IUser user, [Summary("amount")] int amount = 1) { try { diff --git a/Geekbot.net/Commands/User/Ranking/Rank.cs b/Geekbot.net/Commands/User/Ranking/Rank.cs index 8a79fa6..fdc8011 100644 --- a/Geekbot.net/Commands/User/Ranking/Rank.cs +++ b/Geekbot.net/Commands/User/Ranking/Rank.cs @@ -10,6 +10,7 @@ using Geekbot.net.Lib.Converters; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Highscores; +using Geekbot.net.Lib.Localization; using Geekbot.net.Lib.UserRepository; namespace Geekbot.net.Commands.User.Ranking @@ -18,18 +19,20 @@ namespace Geekbot.net.Commands.User.Ranking { private readonly IEmojiConverter _emojiConverter; private readonly IHighscoreManager _highscoreManager; + private readonly ITranslationHandler _translationHandler; private readonly IErrorHandler _errorHandler; private readonly DatabaseContext _database; private readonly IUserRepository _userRepository; public Rank(DatabaseContext database, IErrorHandler errorHandler, IUserRepository userRepository, - IEmojiConverter emojiConverter, IHighscoreManager highscoreManager) + IEmojiConverter emojiConverter, IHighscoreManager highscoreManager, ITranslationHandler translationHandler) { _database = database; _errorHandler = errorHandler; _userRepository = userRepository; _emojiConverter = emojiConverter; _highscoreManager = highscoreManager; + _translationHandler = translationHandler; } [Command("rank", RunMode = RunMode.Async)] @@ -39,6 +42,7 @@ namespace Geekbot.net.Commands.User.Ranking { try { + var transContext = await _translationHandler.GetGuildContext(Context); HighscoreTypes type; try { @@ -46,14 +50,14 @@ namespace Geekbot.net.Commands.User.Ranking } catch { - await ReplyAsync("Valid types are '`messages`' '`karma`', '`rolls`' and '`cookies`'"); + await ReplyAsync(transContext.GetString("InvalidType")); return; } var replyBuilder = new StringBuilder(); if (amount > 20) { - replyBuilder.AppendLine(":warning: Limiting to 20\n"); + await ReplyAsync(transContext.GetString("LimitingTo20Warning")); amount = 20; } @@ -65,7 +69,7 @@ namespace Geekbot.net.Commands.User.Ranking } catch (HighscoreListEmptyException) { - await ReplyAsync($"No {type} found on this server"); + await ReplyAsync(transContext.GetString("NoTypeFoundForServer", type)); return; } @@ -80,8 +84,8 @@ namespace Geekbot.net.Commands.User.Ranking var failedToRetrieveUser = highscoreUsers.Any(e => string.IsNullOrEmpty(e.Key.Username)); - if (failedToRetrieveUser) replyBuilder.AppendLine(":warning: I couldn't find all usernames. Maybe they left the server?\n"); - replyBuilder.AppendLine($":bar_chart: **{type.ToString().CapitalizeFirst()} Highscore for {Context.Guild.Name}**"); + if (failedToRetrieveUser) replyBuilder.AppendLine(transContext.GetString("FailedToResolveAllUsernames")); + replyBuilder.AppendLine(transContext.GetString("HighscoresFor", type.ToString().CapitalizeFirst(), Context.Guild.Name)); var highscorePlace = 1; foreach (var user in highscoreUsers) { diff --git a/Geekbot.net/Lib/Localization/Translations.yml b/Geekbot.net/Lib/Localization/Translations.yml index 6b8d1cd..04835a2 100644 --- a/Geekbot.net/Lib/Localization/Translations.yml +++ b/Geekbot.net/Lib/Localization/Translations.yml @@ -156,4 +156,20 @@ quote: CHDE: "**#{0} glöscht**" NotFoundWithId: EN: "I couldn't find a quote with that ID :disappointed:" - CHDE: "Ich chan kei quote finde mit därri ID :disappointed:" \ No newline at end of file + CHDE: "Ich chan kei quote finde mit därri ID :disappointed:" +rank: + InvalidType: + EN: "Valid types are '`messages`' '`karma`', '`rolls`' and '`cookies`'" + CHDE: "Gültigi paramenter sind '`messages`' '`karma`', '`rolls`' und '`cookies`'" + LimitingTo20Warning: + EN: ":warning: Limiting to 20\n" + CHDE: ":warning: Limitiert uf 20\n" + NoTypeFoundForServer: + EN: "No {0} found on this server" + CHDE: "Kei {0} gfunde für dä server" + FailedToResolveAllUsernames: + EN: ":warning: I couldn't find all usernames. Maybe they left the server?\n" + CHDE: ":warning: Ich han nid alli benutzername gfunde. villiicht hend sie de server verlah?\n" + HighscoresFor: + EN: ":bar_chart: **{0} Highscore for {1}**" + CHDE: ":bar_chart: **{0} Highscore für {1}**" \ No newline at end of file From 3e3cbc257e1e60e6d0da731e39ddc9e383146055 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 12 May 2019 17:29:07 +0200 Subject: [PATCH 169/443] Fix bug where Enum.Parse would always pass if a number was given as argument in rank --- Geekbot.net/Commands/User/Ranking/Rank.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Geekbot.net/Commands/User/Ranking/Rank.cs b/Geekbot.net/Commands/User/Ranking/Rank.cs index fdc8011..0ad21d5 100644 --- a/Geekbot.net/Commands/User/Ranking/Rank.cs +++ b/Geekbot.net/Commands/User/Ranking/Rank.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -46,7 +46,8 @@ namespace Geekbot.net.Commands.User.Ranking HighscoreTypes type; try { - type = Enum.Parse(typeUnformated.ToLower()); + type = Enum.Parse(typeUnformated, true); + if (!Enum.IsDefined(typeof(HighscoreTypes), type)) throw new Exception(); } catch { From 4833ccd65ac268a33f7f7e1304428af48b3543dc Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 12 May 2019 17:29:52 +0200 Subject: [PATCH 170/443] Use SortedDictionary for Highscores instead of a simple Dictionary --- Geekbot.net/Commands/User/Ranking/Rank.cs | 4 ++-- Geekbot.net/Lib/Highscores/HighscoreManager.cs | 4 ++-- Geekbot.net/Lib/Highscores/IHighscoreManager.cs | 2 +- .../WebApi/Controllers/Highscores/HighscoreController.cs | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Geekbot.net/Commands/User/Ranking/Rank.cs b/Geekbot.net/Commands/User/Ranking/Rank.cs index 0ad21d5..1cd45e8 100644 --- a/Geekbot.net/Commands/User/Ranking/Rank.cs +++ b/Geekbot.net/Commands/User/Ranking/Rank.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -63,7 +63,7 @@ namespace Geekbot.net.Commands.User.Ranking } var guildId = Context.Guild.Id; - Dictionary highscoreUsers; + SortedDictionary highscoreUsers; try { highscoreUsers = _highscoreManager.GetHighscoresWithUserData(type, guildId, amount); diff --git a/Geekbot.net/Lib/Highscores/HighscoreManager.cs b/Geekbot.net/Lib/Highscores/HighscoreManager.cs index f3b3a18..02a0b69 100644 --- a/Geekbot.net/Lib/Highscores/HighscoreManager.cs +++ b/Geekbot.net/Lib/Highscores/HighscoreManager.cs @@ -20,7 +20,7 @@ namespace Geekbot.net.Lib.Highscores } - public Dictionary GetHighscoresWithUserData(HighscoreTypes type, ulong guildId, int amount) + public SortedDictionary GetHighscoresWithUserData(HighscoreTypes type, ulong guildId, int amount) { Dictionary list; switch (type) @@ -47,7 +47,7 @@ namespace Geekbot.net.Lib.Highscores throw new HighscoreListEmptyException($"No {type} found for guild {guildId}"); } - var highscoreUsers = new Dictionary(); + var highscoreUsers = new SortedDictionary(); foreach (var user in list) { try diff --git a/Geekbot.net/Lib/Highscores/IHighscoreManager.cs b/Geekbot.net/Lib/Highscores/IHighscoreManager.cs index a09b07e..aca3513 100644 --- a/Geekbot.net/Lib/Highscores/IHighscoreManager.cs +++ b/Geekbot.net/Lib/Highscores/IHighscoreManager.cs @@ -4,7 +4,7 @@ namespace Geekbot.net.Lib.Highscores { public interface IHighscoreManager { - Dictionary GetHighscoresWithUserData(HighscoreTypes type, ulong guildId, int amount); + SortedDictionary GetHighscoresWithUserData(HighscoreTypes type, ulong guildId, int amount); Dictionary GetMessageList(ulong guildId, int amount); Dictionary GetKarmaList(ulong guildId, int amount); Dictionary GetRollsList(ulong guildId, int amount); diff --git a/Geekbot.net/WebApi/Controllers/Highscores/HighscoreController.cs b/Geekbot.net/WebApi/Controllers/Highscores/HighscoreController.cs index 3cccb0e..2b03260 100644 --- a/Geekbot.net/WebApi/Controllers/Highscores/HighscoreController.cs +++ b/Geekbot.net/WebApi/Controllers/Highscores/HighscoreController.cs @@ -25,7 +25,7 @@ namespace Geekbot.net.WebApi.Controllers.Highscores return BadRequest(error); } - Dictionary list; + SortedDictionary list; try { list = _highscoreManager.GetHighscoresWithUserData(body.Type, body.GuildId, body.Amount); From aef50aa2dee1b68452c07ea54b4b02b9dfa135f1 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 12 May 2019 17:57:03 +0200 Subject: [PATCH 171/443] Revert "Use SortedDictionary for Highscores instead of a simple Dictionary" This reverts commit 4833ccd6 --- Geekbot.net/Commands/User/Ranking/Rank.cs | 4 ++-- Geekbot.net/Lib/Highscores/HighscoreManager.cs | 4 ++-- Geekbot.net/Lib/Highscores/IHighscoreManager.cs | 2 +- .../WebApi/Controllers/Highscores/HighscoreController.cs | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Geekbot.net/Commands/User/Ranking/Rank.cs b/Geekbot.net/Commands/User/Ranking/Rank.cs index 1cd45e8..0ad21d5 100644 --- a/Geekbot.net/Commands/User/Ranking/Rank.cs +++ b/Geekbot.net/Commands/User/Ranking/Rank.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -63,7 +63,7 @@ namespace Geekbot.net.Commands.User.Ranking } var guildId = Context.Guild.Id; - SortedDictionary highscoreUsers; + Dictionary highscoreUsers; try { highscoreUsers = _highscoreManager.GetHighscoresWithUserData(type, guildId, amount); diff --git a/Geekbot.net/Lib/Highscores/HighscoreManager.cs b/Geekbot.net/Lib/Highscores/HighscoreManager.cs index 02a0b69..f3b3a18 100644 --- a/Geekbot.net/Lib/Highscores/HighscoreManager.cs +++ b/Geekbot.net/Lib/Highscores/HighscoreManager.cs @@ -20,7 +20,7 @@ namespace Geekbot.net.Lib.Highscores } - public SortedDictionary GetHighscoresWithUserData(HighscoreTypes type, ulong guildId, int amount) + public Dictionary GetHighscoresWithUserData(HighscoreTypes type, ulong guildId, int amount) { Dictionary list; switch (type) @@ -47,7 +47,7 @@ namespace Geekbot.net.Lib.Highscores throw new HighscoreListEmptyException($"No {type} found for guild {guildId}"); } - var highscoreUsers = new SortedDictionary(); + var highscoreUsers = new Dictionary(); foreach (var user in list) { try diff --git a/Geekbot.net/Lib/Highscores/IHighscoreManager.cs b/Geekbot.net/Lib/Highscores/IHighscoreManager.cs index aca3513..a09b07e 100644 --- a/Geekbot.net/Lib/Highscores/IHighscoreManager.cs +++ b/Geekbot.net/Lib/Highscores/IHighscoreManager.cs @@ -4,7 +4,7 @@ namespace Geekbot.net.Lib.Highscores { public interface IHighscoreManager { - SortedDictionary GetHighscoresWithUserData(HighscoreTypes type, ulong guildId, int amount); + Dictionary GetHighscoresWithUserData(HighscoreTypes type, ulong guildId, int amount); Dictionary GetMessageList(ulong guildId, int amount); Dictionary GetKarmaList(ulong guildId, int amount); Dictionary GetRollsList(ulong guildId, int amount); diff --git a/Geekbot.net/WebApi/Controllers/Highscores/HighscoreController.cs b/Geekbot.net/WebApi/Controllers/Highscores/HighscoreController.cs index 2b03260..3cccb0e 100644 --- a/Geekbot.net/WebApi/Controllers/Highscores/HighscoreController.cs +++ b/Geekbot.net/WebApi/Controllers/Highscores/HighscoreController.cs @@ -25,7 +25,7 @@ namespace Geekbot.net.WebApi.Controllers.Highscores return BadRequest(error); } - SortedDictionary list; + Dictionary list; try { list = _highscoreManager.GetHighscoresWithUserData(body.Type, body.GuildId, body.Amount); From f5cd0ffcc8ba0fd5e6a3c2c6a873ae0032d69b03 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 12 May 2019 22:05:35 +0200 Subject: [PATCH 172/443] add unit test for making sure all translations keys exist --- Tests/Lib/Localization/Translations.test.cs | 44 +++++++++++++++++++++ Tests/Tests.csproj | 2 + 2 files changed, 46 insertions(+) create mode 100644 Tests/Lib/Localization/Translations.test.cs diff --git a/Tests/Lib/Localization/Translations.test.cs b/Tests/Lib/Localization/Translations.test.cs new file mode 100644 index 0000000..fc43091 --- /dev/null +++ b/Tests/Lib/Localization/Translations.test.cs @@ -0,0 +1,44 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +using FluentAssertions; +using Xunit; +using YamlDotNet.Serialization; + +namespace Tests.Lib.Localization +{ + public class Translations_test + { + [Fact] + public void TranslationsYamlIsValid() + { + // Read the file + var translationFile = File.ReadAllText(Path.GetFullPath("./../../../../Geekbot.net/Lib/Localization/Translations.yml")); + + // Deserialize + var input = new StringReader(translationFile); + var deserializer = new DeserializerBuilder().Build(); + var rawTranslations = deserializer.Deserialize>>>(input); + + // These languages must be supported + var supportedLanguages = new List + { + "EN", + "CHDE" + }; + + // Iterate every single key to make sure it's populated + foreach (var command in rawTranslations) + { + foreach (var str in command.Value) + { + str.Value.Select(e => e.Key).ToList().Should().BeEquivalentTo(supportedLanguages, str.Key); + foreach (var lang in str.Value) + { + lang.Value.Should().NotBeNullOrEmpty($"{command.Key} / {str.Key} / {lang.Key}"); + } + } + } + } + } +} \ No newline at end of file diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 771a9d4..1c07b9c 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -6,11 +6,13 @@ xUnit1026 + + From 5f3d7f8a737223faa9f1b1cc5c69adba75e8d32d Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 15 May 2019 21:41:29 +0200 Subject: [PATCH 173/443] Add bdcc (Benedict Cumberbatch Name Generator) --- .../BenedictCumberbatchNameGenerator.cs | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Geekbot.net/Commands/Randomness/BenedictCumberbatchNameGenerator.cs diff --git a/Geekbot.net/Commands/Randomness/BenedictCumberbatchNameGenerator.cs b/Geekbot.net/Commands/Randomness/BenedictCumberbatchNameGenerator.cs new file mode 100644 index 0000000..699d550 --- /dev/null +++ b/Geekbot.net/Commands/Randomness/BenedictCumberbatchNameGenerator.cs @@ -0,0 +1,54 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Discord.Commands; +using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.RandomNumberGenerator; + +namespace Geekbot.net.Commands.Randomness +{ + public class BenedictCumberbatchNameGenerator : ModuleBase + { + private readonly IErrorHandler _errorHandler; + private readonly IRandomNumberGenerator _randomNumberGenerator; + + public BenedictCumberbatchNameGenerator(IErrorHandler errorHandler, IRandomNumberGenerator randomNumberGenerator) + { + _errorHandler = errorHandler; + _randomNumberGenerator = randomNumberGenerator; + } + + [Command("bdcc", RunMode = RunMode.Async)] + [Summary("Benedict Cumberbatch Name Generator")] + public async Task GetQuote() + { + var firstnameList = new List + { + "Bumblebee", "Bandersnatch", "Broccoli", "Rinkydink", "Bombadil", "Boilerdang", "Bandicoot", "Fragglerock", "Muffintop", "Congleton", "Blubberdick", "Buffalo", "Benadryl", + "Butterfree", "Burberry", "Whippersnatch", "Buttermilk", "Beezlebub", "Budapest", "Boilerdang", "Blubberwhale", "Bumberstump", "Bulbasaur", "Cogglesnatch", "Liverswort", "Bodybuild", + "Johnnycash", "Bendydick", "Burgerking", "Bonaparte", "Bunsenburner", "Billiardball", "Bukkake", "Baseballmitt", "Blubberbutt", "Baseballbat", "Rumblesack", "Barister", "Danglerack", + "Rinkydink", "Bombadil", "Honkytonk", "Billyray", "Bumbleshack", "Snorkeldink", "Beetlejuice", "Bedlington", "Bandicoot", "Boobytrap", "Blenderdick", "Bentobox", "Pallettown", + "Wimbledon", "Buttercup", "Blasphemy", "Syphilis", "Snorkeldink", "Brandenburg", "Barbituate", "Snozzlebert", "Tiddleywomp", "Bouillabaisse", "Wellington", "Benetton", "Bendandsnap", + "Timothy", "Brewery", "Bentobox", "Brandybuck", "Benjamin", "Buckminster", "Bourgeoisie", "Bakery", "Oscarbait", "Buckyball", "Bourgeoisie", "Burlington", "Buckingham", "Barnoldswick", + "Bumblesniff", "Butercup", "Bubblebath", "Fiddlestick", "Bulbasaur", "Bumblebee", "Bettyboop", "Botany", "Cadbury", "Brendadirk", "Buckingham", "Barnabus", "Barnacle", "Billybong", + "Botany", "Benddadick", "Benderchick" + }; + + var lastnameList = new List + { + "Coddleswort", "Crumplesack", "Curdlesnoot", "Calldispatch", "Humperdinck", "Rivendell", "Cuttlefish", "Lingerie", "Vegemite", "Ampersand", "Cumberbund", "Candycrush", "Clombyclomp", + "Cragglethatch", "Nottinghill", "Cabbagepatch", "Camouflage", "Creamsicle", "Curdlemilk", "Upperclass", "Frumblesnatch", "Crumplehorn", "Talisman", "Candlestick", "Chesterfield", + "Bumbersplat", "Scratchnsniff", "Snugglesnatch", "Charizard", "Carrotstick", "Cumbercooch", "Crackerjack", "Crucifix", "Cuckatoo", "Cockletit", "Collywog", "Capncrunch", "Covergirl", + "Cumbersnatch", "Countryside", "Coggleswort", "Splishnsplash", "Copperwire", "Animorph", "Curdledmilk", "Cheddarcheese", "Cottagecheese", "Crumplehorn", "Snickersbar", "Banglesnatch", + "Stinkyrash", "Cameltoe", "Chickenbroth", "Concubine", "Candygram", "Moldyspore", "Chuckecheese", "Cankersore", "Crimpysnitch", "Wafflesmack", "Chowderpants", "Toodlesnoot", + "Clavichord", "Cuckooclock", "Oxfordshire", "Cumbersome", "Chickenstrips", "Battleship", "Commonwealth", "Cunningsnatch", "Custardbath", "Kryptonite", "Curdlesnoot", "Cummerbund", + "Coochyrash", "Crackerdong", "Crackerdong", "Curdledong", "Crackersprout", "Crumplebutt", "Colonist", "Coochierash", "Anglerfish", "Cumbersniff", "Charmander", "Scratch-n-sniff", + "Cumberbitch", "Pumpkinpatch", "Cramplesnutch", "Lumberjack", "Bonaparte", "Cul-de-sac", "Cankersore", "Cucumbercatch", "Contradict" + }; + + var lastname = lastnameList[_randomNumberGenerator.Next(0, lastnameList.Count - 1)]; + var firstname = firstnameList[_randomNumberGenerator.Next(0, firstnameList.Count - 1)]; + + await ReplyAsync($"{firstname} {lastname}"); + } + } +} \ No newline at end of file From 761ed7302c0e36924c902d6ebc1db7d1e710997e Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 15 May 2019 22:18:02 +0200 Subject: [PATCH 174/443] Wrap bdcc in a try catch block --- .../BenedictCumberbatchNameGenerator.cs | 65 ++++++++++++------- 1 file changed, 40 insertions(+), 25 deletions(-) diff --git a/Geekbot.net/Commands/Randomness/BenedictCumberbatchNameGenerator.cs b/Geekbot.net/Commands/Randomness/BenedictCumberbatchNameGenerator.cs index 699d550..eee5763 100644 --- a/Geekbot.net/Commands/Randomness/BenedictCumberbatchNameGenerator.cs +++ b/Geekbot.net/Commands/Randomness/BenedictCumberbatchNameGenerator.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Threading.Tasks; using Discord.Commands; @@ -21,34 +22,48 @@ namespace Geekbot.net.Commands.Randomness [Summary("Benedict Cumberbatch Name Generator")] public async Task GetQuote() { - var firstnameList = new List + try { - "Bumblebee", "Bandersnatch", "Broccoli", "Rinkydink", "Bombadil", "Boilerdang", "Bandicoot", "Fragglerock", "Muffintop", "Congleton", "Blubberdick", "Buffalo", "Benadryl", - "Butterfree", "Burberry", "Whippersnatch", "Buttermilk", "Beezlebub", "Budapest", "Boilerdang", "Blubberwhale", "Bumberstump", "Bulbasaur", "Cogglesnatch", "Liverswort", "Bodybuild", - "Johnnycash", "Bendydick", "Burgerking", "Bonaparte", "Bunsenburner", "Billiardball", "Bukkake", "Baseballmitt", "Blubberbutt", "Baseballbat", "Rumblesack", "Barister", "Danglerack", - "Rinkydink", "Bombadil", "Honkytonk", "Billyray", "Bumbleshack", "Snorkeldink", "Beetlejuice", "Bedlington", "Bandicoot", "Boobytrap", "Blenderdick", "Bentobox", "Pallettown", - "Wimbledon", "Buttercup", "Blasphemy", "Syphilis", "Snorkeldink", "Brandenburg", "Barbituate", "Snozzlebert", "Tiddleywomp", "Bouillabaisse", "Wellington", "Benetton", "Bendandsnap", - "Timothy", "Brewery", "Bentobox", "Brandybuck", "Benjamin", "Buckminster", "Bourgeoisie", "Bakery", "Oscarbait", "Buckyball", "Bourgeoisie", "Burlington", "Buckingham", "Barnoldswick", - "Bumblesniff", "Butercup", "Bubblebath", "Fiddlestick", "Bulbasaur", "Bumblebee", "Bettyboop", "Botany", "Cadbury", "Brendadirk", "Buckingham", "Barnabus", "Barnacle", "Billybong", - "Botany", "Benddadick", "Benderchick" - }; + var firstnameList = new List + { + "Bumblebee", "Bandersnatch", "Broccoli", "Rinkydink", "Bombadil", "Boilerdang", "Bandicoot", "Fragglerock", "Muffintop", "Congleton", "Blubberdick", "Buffalo", "Benadryl", + "Butterfree", "Burberry", "Whippersnatch", "Buttermilk", "Beezlebub", "Budapest", "Boilerdang", "Blubberwhale", "Bumberstump", "Bulbasaur", "Cogglesnatch", "Liverswort", + "Bodybuild", + "Johnnycash", "Bendydick", "Burgerking", "Bonaparte", "Bunsenburner", "Billiardball", "Bukkake", "Baseballmitt", "Blubberbutt", "Baseballbat", "Rumblesack", "Barister", + "Danglerack", + "Rinkydink", "Bombadil", "Honkytonk", "Billyray", "Bumbleshack", "Snorkeldink", "Beetlejuice", "Bedlington", "Bandicoot", "Boobytrap", "Blenderdick", "Bentobox", "Pallettown", + "Wimbledon", "Buttercup", "Blasphemy", "Syphilis", "Snorkeldink", "Brandenburg", "Barbituate", "Snozzlebert", "Tiddleywomp", "Bouillabaisse", "Wellington", "Benetton", + "Bendandsnap", + "Timothy", "Brewery", "Bentobox", "Brandybuck", "Benjamin", "Buckminster", "Bourgeoisie", "Bakery", "Oscarbait", "Buckyball", "Bourgeoisie", "Burlington", "Buckingham", + "Barnoldswick", + "Bumblesniff", "Butercup", "Bubblebath", "Fiddlestick", "Bulbasaur", "Bumblebee", "Bettyboop", "Botany", "Cadbury", "Brendadirk", "Buckingham", "Barnabus", "Barnacle", "Billybong", + "Botany", "Benddadick", "Benderchick" + }; - var lastnameList = new List + var lastnameList = new List + { + "Coddleswort", "Crumplesack", "Curdlesnoot", "Calldispatch", "Humperdinck", "Rivendell", "Cuttlefish", "Lingerie", "Vegemite", "Ampersand", "Cumberbund", "Candycrush", + "Clombyclomp", + "Cragglethatch", "Nottinghill", "Cabbagepatch", "Camouflage", "Creamsicle", "Curdlemilk", "Upperclass", "Frumblesnatch", "Crumplehorn", "Talisman", "Candlestick", "Chesterfield", + "Bumbersplat", "Scratchnsniff", "Snugglesnatch", "Charizard", "Carrotstick", "Cumbercooch", "Crackerjack", "Crucifix", "Cuckatoo", "Cockletit", "Collywog", "Capncrunch", + "Covergirl", + "Cumbersnatch", "Countryside", "Coggleswort", "Splishnsplash", "Copperwire", "Animorph", "Curdledmilk", "Cheddarcheese", "Cottagecheese", "Crumplehorn", "Snickersbar", + "Banglesnatch", + "Stinkyrash", "Cameltoe", "Chickenbroth", "Concubine", "Candygram", "Moldyspore", "Chuckecheese", "Cankersore", "Crimpysnitch", "Wafflesmack", "Chowderpants", "Toodlesnoot", + "Clavichord", "Cuckooclock", "Oxfordshire", "Cumbersome", "Chickenstrips", "Battleship", "Commonwealth", "Cunningsnatch", "Custardbath", "Kryptonite", "Curdlesnoot", "Cummerbund", + "Coochyrash", "Crackerdong", "Crackerdong", "Curdledong", "Crackersprout", "Crumplebutt", "Colonist", "Coochierash", "Anglerfish", "Cumbersniff", "Charmander", "Scratch-n-sniff", + "Cumberbitch", "Pumpkinpatch", "Cramplesnutch", "Lumberjack", "Bonaparte", "Cul-de-sac", "Cankersore", "Cucumbercatch", "Contradict" + }; + + var lastname = lastnameList[_randomNumberGenerator.Next(0, lastnameList.Count - 1)]; + var firstname = firstnameList[_randomNumberGenerator.Next(0, firstnameList.Count - 1)]; + + await ReplyAsync($"{firstname} {lastname}"); + } + catch (Exception e) { - "Coddleswort", "Crumplesack", "Curdlesnoot", "Calldispatch", "Humperdinck", "Rivendell", "Cuttlefish", "Lingerie", "Vegemite", "Ampersand", "Cumberbund", "Candycrush", "Clombyclomp", - "Cragglethatch", "Nottinghill", "Cabbagepatch", "Camouflage", "Creamsicle", "Curdlemilk", "Upperclass", "Frumblesnatch", "Crumplehorn", "Talisman", "Candlestick", "Chesterfield", - "Bumbersplat", "Scratchnsniff", "Snugglesnatch", "Charizard", "Carrotstick", "Cumbercooch", "Crackerjack", "Crucifix", "Cuckatoo", "Cockletit", "Collywog", "Capncrunch", "Covergirl", - "Cumbersnatch", "Countryside", "Coggleswort", "Splishnsplash", "Copperwire", "Animorph", "Curdledmilk", "Cheddarcheese", "Cottagecheese", "Crumplehorn", "Snickersbar", "Banglesnatch", - "Stinkyrash", "Cameltoe", "Chickenbroth", "Concubine", "Candygram", "Moldyspore", "Chuckecheese", "Cankersore", "Crimpysnitch", "Wafflesmack", "Chowderpants", "Toodlesnoot", - "Clavichord", "Cuckooclock", "Oxfordshire", "Cumbersome", "Chickenstrips", "Battleship", "Commonwealth", "Cunningsnatch", "Custardbath", "Kryptonite", "Curdlesnoot", "Cummerbund", - "Coochyrash", "Crackerdong", "Crackerdong", "Curdledong", "Crackersprout", "Crumplebutt", "Colonist", "Coochierash", "Anglerfish", "Cumbersniff", "Charmander", "Scratch-n-sniff", - "Cumberbitch", "Pumpkinpatch", "Cramplesnutch", "Lumberjack", "Bonaparte", "Cul-de-sac", "Cankersore", "Cucumbercatch", "Contradict" - }; - - var lastname = lastnameList[_randomNumberGenerator.Next(0, lastnameList.Count - 1)]; - var firstname = firstnameList[_randomNumberGenerator.Next(0, firstnameList.Count - 1)]; - - await ReplyAsync($"{firstname} {lastname}"); + await _errorHandler.HandleCommandException(e, Context); + } } } } \ No newline at end of file From cf5cb2ea869138ca3fee699c904a579f93a41076 Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 16 May 2019 21:54:13 +0200 Subject: [PATCH 175/443] Reformat bdcc --- .../BenedictCumberbatchNameGenerator.cs | 33 ++++++++----------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/Geekbot.net/Commands/Randomness/BenedictCumberbatchNameGenerator.cs b/Geekbot.net/Commands/Randomness/BenedictCumberbatchNameGenerator.cs index eee5763..501f891 100644 --- a/Geekbot.net/Commands/Randomness/BenedictCumberbatchNameGenerator.cs +++ b/Geekbot.net/Commands/Randomness/BenedictCumberbatchNameGenerator.cs @@ -28,31 +28,24 @@ namespace Geekbot.net.Commands.Randomness { "Bumblebee", "Bandersnatch", "Broccoli", "Rinkydink", "Bombadil", "Boilerdang", "Bandicoot", "Fragglerock", "Muffintop", "Congleton", "Blubberdick", "Buffalo", "Benadryl", "Butterfree", "Burberry", "Whippersnatch", "Buttermilk", "Beezlebub", "Budapest", "Boilerdang", "Blubberwhale", "Bumberstump", "Bulbasaur", "Cogglesnatch", "Liverswort", - "Bodybuild", - "Johnnycash", "Bendydick", "Burgerking", "Bonaparte", "Bunsenburner", "Billiardball", "Bukkake", "Baseballmitt", "Blubberbutt", "Baseballbat", "Rumblesack", "Barister", - "Danglerack", - "Rinkydink", "Bombadil", "Honkytonk", "Billyray", "Bumbleshack", "Snorkeldink", "Beetlejuice", "Bedlington", "Bandicoot", "Boobytrap", "Blenderdick", "Bentobox", "Pallettown", - "Wimbledon", "Buttercup", "Blasphemy", "Syphilis", "Snorkeldink", "Brandenburg", "Barbituate", "Snozzlebert", "Tiddleywomp", "Bouillabaisse", "Wellington", "Benetton", - "Bendandsnap", - "Timothy", "Brewery", "Bentobox", "Brandybuck", "Benjamin", "Buckminster", "Bourgeoisie", "Bakery", "Oscarbait", "Buckyball", "Bourgeoisie", "Burlington", "Buckingham", - "Barnoldswick", - "Bumblesniff", "Butercup", "Bubblebath", "Fiddlestick", "Bulbasaur", "Bumblebee", "Bettyboop", "Botany", "Cadbury", "Brendadirk", "Buckingham", "Barnabus", "Barnacle", "Billybong", - "Botany", "Benddadick", "Benderchick" + "Bodybuild", "Johnnycash", "Bendydick", "Burgerking", "Bonaparte", "Bunsenburner", "Billiardball", "Bukkake", "Baseballmitt", "Blubberbutt", "Baseballbat", "Rumblesack", + "Barister", "Danglerack", "Rinkydink", "Bombadil", "Honkytonk", "Billyray", "Bumbleshack", "Snorkeldink", "Beetlejuice", "Bedlington", "Bandicoot", "Boobytrap", "Blenderdick", + "Bentobox", "Pallettown", "Wimbledon", "Buttercup", "Blasphemy", "Syphilis", "Snorkeldink", "Brandenburg", "Barbituate", "Snozzlebert", "Tiddleywomp", "Bouillabaisse", + "Wellington", "Benetton", "Bendandsnap", "Timothy", "Brewery", "Bentobox", "Brandybuck", "Benjamin", "Buckminster", "Bourgeoisie", "Bakery", "Oscarbait", "Buckyball", + "Bourgeoisie", "Burlington", "Buckingham", "Barnoldswick", "Bumblesniff", "Butercup", "Bubblebath", "Fiddlestick", "Bulbasaur", "Bumblebee", "Bettyboop", "Botany", "Cadbury", + "Brendadirk", "Buckingham", "Barnabus", "Barnacle", "Billybong", "Botany", "Benddadick", "Benderchick" }; var lastnameList = new List { "Coddleswort", "Crumplesack", "Curdlesnoot", "Calldispatch", "Humperdinck", "Rivendell", "Cuttlefish", "Lingerie", "Vegemite", "Ampersand", "Cumberbund", "Candycrush", - "Clombyclomp", - "Cragglethatch", "Nottinghill", "Cabbagepatch", "Camouflage", "Creamsicle", "Curdlemilk", "Upperclass", "Frumblesnatch", "Crumplehorn", "Talisman", "Candlestick", "Chesterfield", - "Bumbersplat", "Scratchnsniff", "Snugglesnatch", "Charizard", "Carrotstick", "Cumbercooch", "Crackerjack", "Crucifix", "Cuckatoo", "Cockletit", "Collywog", "Capncrunch", - "Covergirl", - "Cumbersnatch", "Countryside", "Coggleswort", "Splishnsplash", "Copperwire", "Animorph", "Curdledmilk", "Cheddarcheese", "Cottagecheese", "Crumplehorn", "Snickersbar", - "Banglesnatch", - "Stinkyrash", "Cameltoe", "Chickenbroth", "Concubine", "Candygram", "Moldyspore", "Chuckecheese", "Cankersore", "Crimpysnitch", "Wafflesmack", "Chowderpants", "Toodlesnoot", - "Clavichord", "Cuckooclock", "Oxfordshire", "Cumbersome", "Chickenstrips", "Battleship", "Commonwealth", "Cunningsnatch", "Custardbath", "Kryptonite", "Curdlesnoot", "Cummerbund", - "Coochyrash", "Crackerdong", "Crackerdong", "Curdledong", "Crackersprout", "Crumplebutt", "Colonist", "Coochierash", "Anglerfish", "Cumbersniff", "Charmander", "Scratch-n-sniff", - "Cumberbitch", "Pumpkinpatch", "Cramplesnutch", "Lumberjack", "Bonaparte", "Cul-de-sac", "Cankersore", "Cucumbercatch", "Contradict" + "Clombyclomp", "Cragglethatch", "Nottinghill", "Cabbagepatch", "Camouflage", "Creamsicle", "Curdlemilk", "Upperclass", "Frumblesnatch", "Crumplehorn", "Talisman", "Candlestick", + "Chesterfield", "Bumbersplat", "Scratchnsniff", "Snugglesnatch", "Charizard", "Carrotstick", "Cumbercooch", "Crackerjack", "Crucifix", "Cuckatoo", "Cockletit", "Collywog", + "Capncrunch", "Covergirl", "Cumbersnatch", "Countryside", "Coggleswort", "Splishnsplash", "Copperwire", "Animorph", "Curdledmilk", "Cheddarcheese", "Cottagecheese", "Crumplehorn", + "Snickersbar", "Banglesnatch", "Stinkyrash", "Cameltoe", "Chickenbroth", "Concubine", "Candygram", "Moldyspore", "Chuckecheese", "Cankersore", "Crimpysnitch", "Wafflesmack", + "Chowderpants", "Toodlesnoot", "Clavichord", "Cuckooclock", "Oxfordshire", "Cumbersome", "Chickenstrips", "Battleship", "Commonwealth", "Cunningsnatch", "Custardbath", + "Kryptonite", "Curdlesnoot", "Cummerbund", "Coochyrash", "Crackerdong", "Crackerdong", "Curdledong", "Crackersprout", "Crumplebutt", "Colonist", "Coochierash", "Anglerfish", + "Cumbersniff", "Charmander", "Scratch-n-sniff", "Cumberbitch", "Pumpkinpatch", "Cramplesnutch", "Lumberjack", "Bonaparte", "Cul-de-sac", "Cankersore", "Cucumbercatch", "Contradict" }; var lastname = lastnameList[_randomNumberGenerator.Next(0, lastnameList.Count - 1)]; From 0e1084ccad2d6b4de3c4d06e40139d405944786f Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 16 May 2019 22:00:28 +0200 Subject: [PATCH 176/443] Remove Database migration scripts --- .../Commands/Admin/Owner/MigrationMethods.cs | 8 - Geekbot.net/Commands/Admin/Owner/Owner.cs | 57 +-- Geekbot.net/Database/MessageMigration.cs | 73 ---- Geekbot.net/Database/RedisMigration.cs | 396 ------------------ 4 files changed, 3 insertions(+), 531 deletions(-) delete mode 100644 Geekbot.net/Commands/Admin/Owner/MigrationMethods.cs delete mode 100644 Geekbot.net/Database/MessageMigration.cs delete mode 100644 Geekbot.net/Database/RedisMigration.cs diff --git a/Geekbot.net/Commands/Admin/Owner/MigrationMethods.cs b/Geekbot.net/Commands/Admin/Owner/MigrationMethods.cs deleted file mode 100644 index 6f50257..0000000 --- a/Geekbot.net/Commands/Admin/Owner/MigrationMethods.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Geekbot.net.Commands.Admin.Owner -{ - public enum MigrationMethods - { - redis, - messages - } -} \ No newline at end of file diff --git a/Geekbot.net/Commands/Admin/Owner/Owner.cs b/Geekbot.net/Commands/Admin/Owner/Owner.cs index b02ee47..6263e46 100644 --- a/Geekbot.net/Commands/Admin/Owner/Owner.cs +++ b/Geekbot.net/Commands/Admin/Owner/Owner.cs @@ -1,9 +1,8 @@ -using System; +using System; using System.Threading.Tasks; +using Discord; using Discord.Commands; using Discord.WebSocket; -using Geekbot.net.Database; -using Geekbot.net.Lib.AlmostRedis; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.GlobalSettings; using Geekbot.net.Lib.Logger; @@ -17,69 +16,19 @@ namespace Geekbot.net.Commands.Admin.Owner { private readonly DiscordSocketClient _client; private readonly IErrorHandler _errorHandler; - private readonly DatabaseContext _database; private readonly IGlobalSettings _globalSettings; private readonly IGeekbotLogger _logger; - private readonly IAlmostRedis _redis; private readonly IUserRepository _userRepository; - public Owner(IAlmostRedis redis, DiscordSocketClient client, IGeekbotLogger logger, IUserRepository userRepositry, IErrorHandler errorHandler, DatabaseContext database, IGlobalSettings globalSettings) + public Owner(DiscordSocketClient client, IGeekbotLogger logger, IUserRepository userRepositry, IErrorHandler errorHandler, IGlobalSettings globalSettings) { - _redis = redis; _client = client; _logger = logger; _userRepository = userRepositry; _errorHandler = errorHandler; - _database = database; _globalSettings = globalSettings; } - [Command("migrate", RunMode = RunMode.Async)] - public async Task Migrate(MigrationMethods method, string force = "") - { - try - { - switch (method) - { - case MigrationMethods.redis: - var status = _globalSettings.GetKey("MigrationStatus"); - if (status.Equals("Running")) - { - await ReplyAsync("Migration already running"); - return; - } - if (status.Equals("Done") && !force.Equals("force")) - { - await ReplyAsync("Migration already ran, write `!owner migrate redis force` to run again"); - return; - } - - await ReplyAsync("starting migration"); - await _globalSettings.SetKey("MigrationStatus", "Running"); - var redisMigration = new RedisMigration(_database, _redis, _logger, _client); - await redisMigration.Migrate(); - await _globalSettings.SetKey("MigrationStatus", "Done"); - break; - - case MigrationMethods.messages: - await ReplyAsync("Migrating Messages to postgres..."); - var messageMigration = new MessageMigration(_database, _redis, _logger); - await messageMigration.Migrate(); - break; - - default: - await ReplyAsync("No Migration Method specified..."); - break; - } - } - catch (Exception e) - { - await _errorHandler.HandleCommandException(e, Context); - } - - await ReplyAsync("done"); - } - [Command("youtubekey", RunMode = RunMode.Async)] [Summary("Set the youtube api key")] public async Task SetYoutubeKey([Summary("API Key")] string key) diff --git a/Geekbot.net/Database/MessageMigration.cs b/Geekbot.net/Database/MessageMigration.cs deleted file mode 100644 index ed07072..0000000 --- a/Geekbot.net/Database/MessageMigration.cs +++ /dev/null @@ -1,73 +0,0 @@ -using System; -using System.Linq; -using System.Threading.Tasks; -using Geekbot.net.Database.Models; -using Geekbot.net.Lib.AlmostRedis; -using Geekbot.net.Lib.Extensions; -using Geekbot.net.Lib.Logger; - -namespace Geekbot.net.Database -{ - public class MessageMigration - { - private readonly DatabaseContext _database; - private readonly IAlmostRedis _redis; - private readonly IGeekbotLogger _logger; - - public MessageMigration(DatabaseContext database, IAlmostRedis redis, IGeekbotLogger logger) - { - _database = database; - _redis = redis; - _logger = logger; - } - - public async Task Migrate() - { - _logger.Warning(LogSource.Migration, "Starting message migration"); - try - { - var messageKeys = _redis.GetAllKeys().Where(e => e.ToString().EndsWith("Messages")); - foreach (var keyName in messageKeys) - { - try - { - var guildId = ulong.Parse(keyName.ToString().Split(':').FirstOrDefault()); - var guildUsers = _redis.Db.HashGetAll(keyName); - foreach (var user in guildUsers) - { - try - { - var userId = ulong.Parse(user.Name); - if (userId != 0) - { - var userMessages = int.Parse(user.Value); - _database.Messages.Add(new MessagesModel - { - UserId = userId.AsLong(), - GuildId = guildId.AsLong(), - MessageCount = userMessages - }); - } - } - catch (Exception e) - { - _logger.Error(LogSource.Migration, $"Failed to add record for a user in {guildId}", e); - } - } - - await _database.SaveChangesAsync(); - } - catch (Exception e) - { - _logger.Error(LogSource.Migration, "Failed to determinate guild", e); - } - } - _logger.Warning(LogSource.Migration, "Successfully finished message migration"); - } - catch (Exception e) - { - _logger.Error(LogSource.Migration, "Message migration failed", e); - } - } - } -} \ No newline at end of file diff --git a/Geekbot.net/Database/RedisMigration.cs b/Geekbot.net/Database/RedisMigration.cs deleted file mode 100644 index 74d97d2..0000000 --- a/Geekbot.net/Database/RedisMigration.cs +++ /dev/null @@ -1,396 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Discord; -using Discord.WebSocket; -using Geekbot.net.Commands.Utils.Quote; -using Geekbot.net.Database.Models; -using Geekbot.net.Lib.AlmostRedis; -using Geekbot.net.Lib.Extensions; -using Geekbot.net.Lib.Logger; -using Newtonsoft.Json; - -namespace Geekbot.net.Database -{ - public class RedisMigration - { - private readonly DatabaseContext _database; - private readonly IAlmostRedis _redis; - private readonly IGeekbotLogger _logger; - private readonly DiscordSocketClient _client; - - public RedisMigration(DatabaseContext database, IAlmostRedis redis, IGeekbotLogger logger, DiscordSocketClient client) - { - _database = database; - _redis = redis; - _logger = logger; - _client = client; - } - - public async Task Migrate() - { - _logger.Information(LogSource.Migration, "Starting migration process"); - - var keys = _redis.GetAllKeys().Where(e => e.ToString().EndsWith("Messages")); - var allGuilds = new List(); - - foreach (var key in keys) - { - try - { - var g = _client.GetGuild(ulong.Parse(key.ToString().Split(':').First())); - Console.WriteLine(g.Name); - allGuilds.Add(g); - } - catch (Exception) - { - // ignore - } - } - - _logger.Information(LogSource.Migration, $"Found {allGuilds.Count} guilds in redis"); - - var guilds = allGuilds.FindAll(e => e.MemberCount < 10000); - - foreach (var guild in guilds) - { - _logger.Information(LogSource.Migration, $"Start Migration for {guild.Name}"); - #region Quotes - /** - * Quotes - */ - try - { - var data = _redis.Db.SetScan($"{guild.Id}:Quotes"); - foreach (var q in data) - { - try - { - var qd = JsonConvert.DeserializeObject(q); - var quote = CreateQuoteObject(guild.Id, qd); - _database.Quotes.Add(quote); - await _database.SaveChangesAsync(); - } - catch (Exception e) - { - _logger.Error(LogSource.Migration, $"quote failed: {q}", e); - } - } - } - catch (Exception e) - { - _logger.Error(LogSource.Migration, "quote migration failed", e); - } - #endregion - - #region Karma - /** - * Karma - */ - try - { - var data = _redis.Db.HashGetAll($"{guild.Id}:Karma"); - foreach (var q in data) - { - try - { - var user = new KarmaModel() - { - GuildId = guild.Id.AsLong(), - UserId = ulong.Parse(q.Name).AsLong(), - Karma = int.Parse(q.Value), - TimeOut = DateTimeOffset.MinValue - }; - _database.Karma.Add(user); - await _database.SaveChangesAsync(); - } - catch (Exception e) - { - _logger.Error(LogSource.Migration, $"karma failed for: {q.Name}", e); - } - } - } - catch (Exception e) - { - _logger.Error(LogSource.Migration, "karma migration failed", e); - } - #endregion - - #region Rolls - /** - * Rolls - */ - try - { - var data = _redis.Db.HashGetAll($"{guild.Id}:Rolls"); - foreach (var q in data) - { - try - { - var user = new RollsModel() - { - GuildId = guild.Id.AsLong(), - UserId = ulong.Parse(q.Name).AsLong(), - Rolls = int.Parse(q.Value) - }; - _database.Rolls.Add(user); - await _database.SaveChangesAsync(); - } - catch (Exception e) - { - _logger.Error(LogSource.Migration, $"Rolls failed for: {q.Name}", e); - } - } - } - catch (Exception e) - { - _logger.Error(LogSource.Migration, "rolls migration failed", e); - } - #endregion - - #region Slaps - /** - * Slaps - */ - try - { - var given = _redis.Db.HashGetAll($"{guild.Id}:SlapsGiven"); - var gotten = _redis.Db.HashGetAll($"{guild.Id}:SlapsGiven"); - foreach (var q in given) - { - try - { - var user = new SlapsModel() - { - GuildId = guild.Id.AsLong(), - UserId = ulong.Parse(q.Name).AsLong(), - Given = int.Parse(q.Value), - Recieved= int.Parse(gotten[long.Parse(q.Name)].Value) - }; - _database.Slaps.Add(user); - await _database.SaveChangesAsync(); - } - catch (Exception e) - { - _logger.Error(LogSource.Migration, $"Slaps failed for: {q.Name}", e); - } - } - } - catch (Exception e) - { - _logger.Error(LogSource.Migration, "Slaps migration failed", e); - } - #endregion - - #region Messages - /** - * Messages - */ - /*try - { - var data = _redis.Db.HashGetAll($"{guild.Id}:Messages"); - foreach (var q in data) - { - try - { - var user = new MessagesModel() - { - GuildId = guild.Id.AsLong(), - UserId = ulong.Parse(q.Name).AsLong(), - MessageCount= int.Parse(q.Value) - }; - _database.Messages.Add(user); - await _database.SaveChangesAsync(); - } - catch (Exception e) - { - _logger.Error(LogSource.Migration, $"Messages failed for: {q.Name}", e); - } - } - } - catch (Exception e) - { - _logger.Error(LogSource.Migration, "Messages migration failed", e); - }*/ - #endregion - - #region Ships - /** - * Ships - */ - try - { - var data = _redis.Db.HashGetAll($"{guild.Id}:Ships"); - var done = new List(); - foreach (var q in data) - { - try - { - if (done.Contains(q.Name)) continue; - var split = q.Name.ToString().Split('-'); - var user = new ShipsModel() - { - FirstUserId = ulong.Parse(split[0]).AsLong(), - SecondUserId = ulong.Parse(split[1]).AsLong(), - Strength = int.Parse(q.Value) - }; - _database.Ships.Add(user); - await _database.SaveChangesAsync(); - done.Add(q.Name); - } - catch (Exception e) - { - _logger.Error(LogSource.Migration, $"Ships failed for: {q.Name}", e); - } - } - } - catch (Exception e) - { - _logger.Error(LogSource.Migration, "Ships migration failed", e); - } - #endregion - - #region GuildSettings - /** - * Users - */ - try - { - var data = _redis.Db.HashGetAll($"{guild.Id}:Settings"); - var settings = new GuildSettingsModel() - { - GuildId = guild.Id.AsLong(), - Hui = true - }; - foreach (var setting in data) - { - try - { - switch (setting.Name) - { - case "ShowLeave": - settings.ShowLeave = setting.Value.ToString() == "1"; - break; - case "ShowDelete": - settings.ShowDelete = setting.Value.ToString() == "1"; - break; - case "WikiLang": - settings.WikiLang = setting.Value.ToString(); - break; - case "Language": - settings.Language = setting.Value.ToString(); - break; - case "WelcomeMsg": - settings.WelcomeMessage = setting.Value.ToString(); - break; - case "ping": - settings.Ping = bool.Parse(setting.Value.ToString()); - break; - case "ModChannel": - settings.ModChannel = long.Parse(setting.Value); - break; - } - } - catch (Exception e) - { - _logger.Error(LogSource.Migration, $"Setting failed: {setting.Name} - {guild.Id}", e); - } - } - } - catch (Exception e) - { - _logger.Error(LogSource.Migration, "Settings migration failed", e); - } - - #endregion - - #region Users - /** - * Users - */ - try - { - var data = guild.Users.ToList().FindAll(e => !e.IsBot); - foreach (var user in data) - { - try - { - if (user.Username == null) - { - await Task.Delay(100); - if (user.Username == null) break; - } - - var namesSerialized = _redis.Db.HashGet($"User:{user.Id}", "UsedNames").ToString(); - var names = namesSerialized != null - ? Utf8Json.JsonSerializer.Deserialize(namesSerialized) - : new string[] {user.Username}; - _database.Users.AddIfNotExists(new UserModel() - { - UserId = user.Id.AsLong(), - Username = user.Username, - Discriminator = user.Discriminator, - AvatarUrl = user.GetAvatarUrl(ImageFormat.Auto, 1024), - IsBot = user.IsBot, - Joined = user.CreatedAt, - UsedNames = names.Select(name => new UserUsedNamesModel() {Name = name, FirstSeen = DateTimeOffset.Now}).ToList() - }, model => model.UserId.Equals(user.Id.AsLong())); - await _database.SaveChangesAsync(); - } - catch (Exception e) - { - _logger.Error(LogSource.Migration, $"User failed: {user.Username}", e); - } - } - } - catch (Exception e) - { - _logger.Error(LogSource.Migration, "User migration failed", e); - } - #endregion - - #region Guilds - - try - { - _database.Guilds.Add(new GuildsModel - { - CreatedAt = guild.CreatedAt, - GuildId = guild.Id.AsLong(), - IconUrl = guild?.IconUrl, - Name = guild.Name, - Owner = guild.Owner.Id.AsLong() - }); - await _database.SaveChangesAsync(); - } - catch (Exception e) - { - _logger.Error(LogSource.Migration, $"Guild migration failed: {guild.Name}", e); - } - - #endregion - _logger.Information(LogSource.Migration, $"Finished Migration for {guild.Name}"); - await Task.Delay(1000); - } - _logger.Information(LogSource.Migration, "Finished migration process"); - } - - private QuoteModel CreateQuoteObject(ulong guild, QuoteObjectDto quote) - { - var last = _database.Quotes.Where(e => e.GuildId.Equals(guild.AsLong())) - .OrderByDescending(e => e.InternalId).FirstOrDefault(); - int internalId = 1; - if (last != null) internalId = last.InternalId + 1; - return new QuoteModel() - { - InternalId = internalId, - GuildId = guild.AsLong(), - UserId = quote.UserId.AsLong(), - Time = quote.Time, - Quote = quote.Quote, - Image = quote.Image - }; - } - } -} \ No newline at end of file From 03343918b0fe392baad639dbae30d78e68eee8df Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 16 May 2019 22:01:35 +0200 Subject: [PATCH 177/443] Add a refresh user command to the owner commands --- Geekbot.net/Commands/Admin/Owner/Owner.cs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Geekbot.net/Commands/Admin/Owner/Owner.cs b/Geekbot.net/Commands/Admin/Owner/Owner.cs index 6263e46..661e5a3 100644 --- a/Geekbot.net/Commands/Admin/Owner/Owner.cs +++ b/Geekbot.net/Commands/Admin/Owner/Owner.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Threading.Tasks; using Discord; using Discord.Commands; @@ -78,6 +78,21 @@ namespace Geekbot.net.Commands.Admin.Owner } } + [Command("refreshuser", RunMode = RunMode.Async)] + [Summary("Refresh a user in the user cache")] + public async Task PopUserRepoCommand([Summary("@user")] IUser user) + { + try + { + await _userRepository.Update(user as SocketUser); + await ReplyAsync($"Refreshed: {user.Username}#{user.Discriminator}"); + } + catch (Exception e) + { + await _errorHandler.HandleCommandException(e, Context); + } + } + [Command("error", RunMode = RunMode.Async)] [Summary("Throw an error un purpose")] public async Task PurposefulError() From e4fd828a132ca0b9b195e6144eab738480207196 Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 16 May 2019 22:10:56 +0200 Subject: [PATCH 178/443] Make the command documentation slightly more consistent across most all commands --- Geekbot.net/Commands/Admin/Admin.cs | 2 +- Geekbot.net/Commands/Admin/Mod.cs | 2 +- Geekbot.net/Commands/Admin/Owner/Owner.cs | 4 ++-- Geekbot.net/Commands/Admin/Role.cs | 6 +++--- Geekbot.net/Commands/Games/Pokedex.cs | 2 +- Geekbot.net/Commands/Games/Roll.cs | 2 +- Geekbot.net/Commands/Integrations/Google/Google.cs | 2 +- Geekbot.net/Commands/Integrations/MagicTheGathering.cs | 2 +- Geekbot.net/Commands/Integrations/Mal.cs | 4 ++-- Geekbot.net/Commands/Integrations/Wikipedia.cs | 2 +- Geekbot.net/Commands/Integrations/Youtube.cs | 2 +- Geekbot.net/Commands/Randomness/EightBall.cs | 2 +- Geekbot.net/Commands/Randomness/Ship.cs | 2 +- Geekbot.net/Commands/Randomness/Slap.cs | 2 +- Geekbot.net/Commands/Rpg/Cookies.cs | 2 +- Geekbot.net/Commands/Utils/AvatarGetter.cs | 2 +- Geekbot.net/Commands/Utils/Dice/Dice.cs | 2 +- Geekbot.net/Commands/Utils/Quote/Quote.cs | 10 +++++----- 18 files changed, 26 insertions(+), 26 deletions(-) diff --git a/Geekbot.net/Commands/Admin/Admin.cs b/Geekbot.net/Commands/Admin/Admin.cs index ba31e54..e2e1f86 100644 --- a/Geekbot.net/Commands/Admin/Admin.cs +++ b/Geekbot.net/Commands/Admin/Admin.cs @@ -35,7 +35,7 @@ namespace Geekbot.net.Commands.Admin [Command("welcome", RunMode = RunMode.Async)] [Summary("Set a Welcome Message (use '$user' to mention the new joined user).")] - public async Task SetWelcomeMessage([Remainder] [Summary("message")] string welcomeMessage) + public async Task SetWelcomeMessage([Remainder, Summary("message")] string welcomeMessage) { var guild = await GetGuildSettings(Context.Guild.Id); guild.WelcomeMessage = welcomeMessage; diff --git a/Geekbot.net/Commands/Admin/Mod.cs b/Geekbot.net/Commands/Admin/Mod.cs index 6c6be5a..1188504 100644 --- a/Geekbot.net/Commands/Admin/Mod.cs +++ b/Geekbot.net/Commands/Admin/Mod.cs @@ -30,7 +30,7 @@ namespace Geekbot.net.Commands.Admin [Command("namehistory", RunMode = RunMode.Async)] [Summary("See past usernames of an user")] - public async Task UsernameHistory([Summary("@user")] IUser user) + public async Task UsernameHistory([Summary("@someone")] IUser user) { try { diff --git a/Geekbot.net/Commands/Admin/Owner/Owner.cs b/Geekbot.net/Commands/Admin/Owner/Owner.cs index 661e5a3..74e0e8e 100644 --- a/Geekbot.net/Commands/Admin/Owner/Owner.cs +++ b/Geekbot.net/Commands/Admin/Owner/Owner.cs @@ -31,7 +31,7 @@ namespace Geekbot.net.Commands.Admin.Owner [Command("youtubekey", RunMode = RunMode.Async)] [Summary("Set the youtube api key")] - public async Task SetYoutubeKey([Summary("API Key")] string key) + public async Task SetYoutubeKey([Summary("API-Key")] string key) { await _globalSettings.SetKey("YoutubeKey", key); await ReplyAsync("Apikey has been set"); @@ -80,7 +80,7 @@ namespace Geekbot.net.Commands.Admin.Owner [Command("refreshuser", RunMode = RunMode.Async)] [Summary("Refresh a user in the user cache")] - public async Task PopUserRepoCommand([Summary("@user")] IUser user) + public async Task PopUserRepoCommand([Summary("@someone")] IUser user) { try { diff --git a/Geekbot.net/Commands/Admin/Role.cs b/Geekbot.net/Commands/Admin/Role.cs index f0aa7bd..85b7160 100644 --- a/Geekbot.net/Commands/Admin/Role.cs +++ b/Geekbot.net/Commands/Admin/Role.cs @@ -60,7 +60,7 @@ namespace Geekbot.net.Commands.Admin [Command(RunMode = RunMode.Async)] [Summary("Get a role by mentioning it.")] - public async Task GiveRole([Summary("roleNickname")] string roleNameRaw) + public async Task GiveRole([Summary("role-nickname")] string roleNameRaw) { try { @@ -144,7 +144,7 @@ namespace Geekbot.net.Commands.Admin [RequireUserPermission(GuildPermission.ManageRoles)] [Command("remove", RunMode = RunMode.Async)] [Summary("Remove a role from the whitelist.")] - public async Task RemoveRole([Summary("roleNickname")] string roleName) + public async Task RemoveRole([Summary("role-nickname")] string roleName) { try { @@ -170,7 +170,7 @@ namespace Geekbot.net.Commands.Admin [RequireUserPermission(GuildPermission.ManageRoles)] [Summary("Give a role by clicking on an emoji")] [Command("listen", RunMode = RunMode.Async)] - public async Task AddListener([Summary("messageID")] string messageId, [Summary("Emoji")] string emoji, [Summary("@role")] IRole role) + public async Task AddListener([Summary("message-ID")] string messageId, [Summary("Emoji")] string emoji, [Summary("@role")] IRole role) { try { diff --git a/Geekbot.net/Commands/Games/Pokedex.cs b/Geekbot.net/Commands/Games/Pokedex.cs index fbfb568..a0d2ae0 100644 --- a/Geekbot.net/Commands/Games/Pokedex.cs +++ b/Geekbot.net/Commands/Games/Pokedex.cs @@ -20,7 +20,7 @@ namespace Geekbot.net.Commands.Games [Command("pokedex", RunMode = RunMode.Async)] [Summary("A Pokedex Tool")] - public async Task GetPokemon([Summary("pokemonName")] string pokemonName) + public async Task GetPokemon([Summary("pokemon-name")] string pokemonName) { try { diff --git a/Geekbot.net/Commands/Games/Roll.cs b/Geekbot.net/Commands/Games/Roll.cs index 4fbb24e..dfecb13 100644 --- a/Geekbot.net/Commands/Games/Roll.cs +++ b/Geekbot.net/Commands/Games/Roll.cs @@ -32,7 +32,7 @@ namespace Geekbot.net.Commands.Games [Command("roll", RunMode = RunMode.Async)] [Summary("Guess which number the bot will roll (1-100")] - public async Task RollCommand([Remainder] [Summary("guess")] string stuff = "noGuess") + public async Task RollCommand([Remainder] [Summary("guess")] string stuff = null) { try { diff --git a/Geekbot.net/Commands/Integrations/Google/Google.cs b/Geekbot.net/Commands/Integrations/Google/Google.cs index 6ff926d..db059b4 100644 --- a/Geekbot.net/Commands/Integrations/Google/Google.cs +++ b/Geekbot.net/Commands/Integrations/Google/Google.cs @@ -23,7 +23,7 @@ namespace Geekbot.net.Commands.Integrations.Google [Command("google", RunMode = RunMode.Async)] [Summary("Google Something.")] - public async Task AskGoogle([Remainder, Summary("SearchText")] string searchText) + public async Task AskGoogle([Remainder, Summary("search-text")] string searchText) { try { diff --git a/Geekbot.net/Commands/Integrations/MagicTheGathering.cs b/Geekbot.net/Commands/Integrations/MagicTheGathering.cs index 9ccab39..ecf09cd 100644 --- a/Geekbot.net/Commands/Integrations/MagicTheGathering.cs +++ b/Geekbot.net/Commands/Integrations/MagicTheGathering.cs @@ -24,7 +24,7 @@ namespace Geekbot.net.Commands.Integrations [Command("mtg", RunMode = RunMode.Async)] [Summary("Find a Magic The Gathering Card.")] - public async Task GetCard([Remainder] [Summary("name")] string cardName) + public async Task GetCard([Remainder] [Summary("card-name")] string cardName) { try { diff --git a/Geekbot.net/Commands/Integrations/Mal.cs b/Geekbot.net/Commands/Integrations/Mal.cs index 1ad50bb..ecc91bf 100644 --- a/Geekbot.net/Commands/Integrations/Mal.cs +++ b/Geekbot.net/Commands/Integrations/Mal.cs @@ -23,7 +23,7 @@ namespace Geekbot.net.Commands.Integrations [Command("anime", RunMode = RunMode.Async)] [Summary("Show Info about an Anime.")] - public async Task SearchAnime([Remainder] [Summary("AnimeName")] string animeName) + public async Task SearchAnime([Remainder] [Summary("anime-name")] string animeName) { try { @@ -75,7 +75,7 @@ namespace Geekbot.net.Commands.Integrations [Command("manga", RunMode = RunMode.Async)] [Summary("Show Info about a Manga.")] - public async Task SearchManga([Remainder] [Summary("MangaName")] string mangaName) + public async Task SearchManga([Remainder] [Summary("manga-name")] string mangaName) { try { diff --git a/Geekbot.net/Commands/Integrations/Wikipedia.cs b/Geekbot.net/Commands/Integrations/Wikipedia.cs index e1f6092..9124670 100644 --- a/Geekbot.net/Commands/Integrations/Wikipedia.cs +++ b/Geekbot.net/Commands/Integrations/Wikipedia.cs @@ -29,7 +29,7 @@ namespace Geekbot.net.Commands.Integrations [Command("wiki", RunMode = RunMode.Async)] [Summary("Get an article from wikipedia.")] - public async Task GetPreview([Remainder] [Summary("Article")] string articleName) + public async Task GetPreview([Remainder] [Summary("article")] string articleName) { try { diff --git a/Geekbot.net/Commands/Integrations/Youtube.cs b/Geekbot.net/Commands/Integrations/Youtube.cs index ed630ee..1672bab 100644 --- a/Geekbot.net/Commands/Integrations/Youtube.cs +++ b/Geekbot.net/Commands/Integrations/Youtube.cs @@ -21,7 +21,7 @@ namespace Geekbot.net.Commands.Integrations [Command("yt", RunMode = RunMode.Async)] [Summary("Search for something on youtube.")] - public async Task Yt([Remainder] [Summary("Title")] string searchQuery) + public async Task Yt([Remainder] [Summary("title")] string searchQuery) { var key = _globalSettings.GetKey("YoutubeKey"); if (string.IsNullOrEmpty(key)) diff --git a/Geekbot.net/Commands/Randomness/EightBall.cs b/Geekbot.net/Commands/Randomness/EightBall.cs index 7b3bb1f..d07451d 100644 --- a/Geekbot.net/Commands/Randomness/EightBall.cs +++ b/Geekbot.net/Commands/Randomness/EightBall.cs @@ -17,7 +17,7 @@ namespace Geekbot.net.Commands.Randomness [Command("8ball", RunMode = RunMode.Async)] [Summary("Ask 8Ball a Question.")] - public async Task Ball([Remainder] [Summary("Question")] string echo) + public async Task Ball([Remainder] [Summary("question")] string echo) { try { diff --git a/Geekbot.net/Commands/Randomness/Ship.cs b/Geekbot.net/Commands/Randomness/Ship.cs index 3d876ef..2571891 100644 --- a/Geekbot.net/Commands/Randomness/Ship.cs +++ b/Geekbot.net/Commands/Randomness/Ship.cs @@ -26,7 +26,7 @@ namespace Geekbot.net.Commands.Randomness [Command("Ship", RunMode = RunMode.Async)] [Summary("Ask the Shipping meter")] - public async Task Command([Summary("@User1")] IUser user1, [Summary("@User2")] IUser user2) + public async Task Command([Summary("@user1")] IUser user1, [Summary("@user2")] IUser user2) { try { diff --git a/Geekbot.net/Commands/Randomness/Slap.cs b/Geekbot.net/Commands/Randomness/Slap.cs index 13cd22a..20e83c3 100644 --- a/Geekbot.net/Commands/Randomness/Slap.cs +++ b/Geekbot.net/Commands/Randomness/Slap.cs @@ -24,7 +24,7 @@ namespace Geekbot.net.Commands.Randomness [Command("slap", RunMode = RunMode.Async)] [Summary("slap someone")] - public async Task Slapper([Summary("@user")] IUser user) + public async Task Slapper([Summary("@someone")] IUser user) { try { diff --git a/Geekbot.net/Commands/Rpg/Cookies.cs b/Geekbot.net/Commands/Rpg/Cookies.cs index 6e4ff54..f22eccf 100644 --- a/Geekbot.net/Commands/Rpg/Cookies.cs +++ b/Geekbot.net/Commands/Rpg/Cookies.cs @@ -74,7 +74,7 @@ namespace Geekbot.net.Commands.Rpg [Command("give", RunMode = RunMode.Async)] [Summary("Give cookies to someone")] - public async Task GiveACookie([Summary("User")] IUser user, [Summary("amount")] int amount = 1) + public async Task GiveACookie([Summary("@someone")] IUser user, [Summary("amount")] int amount = 1) { try { diff --git a/Geekbot.net/Commands/Utils/AvatarGetter.cs b/Geekbot.net/Commands/Utils/AvatarGetter.cs index e329ad4..142b3e3 100644 --- a/Geekbot.net/Commands/Utils/AvatarGetter.cs +++ b/Geekbot.net/Commands/Utils/AvatarGetter.cs @@ -17,7 +17,7 @@ namespace Geekbot.net.Commands.Utils [Command("avatar", RunMode = RunMode.Async)] [Summary("Get someones avatar")] - public async Task GetAvatar([Remainder] [Summary("user")] IUser user = null) + public async Task GetAvatar([Remainder, Summary("@someone")] IUser user = null) { try { diff --git a/Geekbot.net/Commands/Utils/Dice/Dice.cs b/Geekbot.net/Commands/Utils/Dice/Dice.cs index 8b23fe6..6b95903 100644 --- a/Geekbot.net/Commands/Utils/Dice/Dice.cs +++ b/Geekbot.net/Commands/Utils/Dice/Dice.cs @@ -19,7 +19,7 @@ namespace Geekbot.net.Commands.Utils.Dice [Command("dice", RunMode = RunMode.Async)] [Summary("Roll a dice.")] - public async Task RollCommand([Remainder] [Summary("diceType")] string diceType = "1d20") + public async Task RollCommand([Remainder] [Summary("dice-type")] string diceType = "1d20") { var splitedDices = diceType.Split("+"); var dices = new List(); diff --git a/Geekbot.net/Commands/Utils/Quote/Quote.cs b/Geekbot.net/Commands/Utils/Quote/Quote.cs index 84a3786..f06c6ab 100644 --- a/Geekbot.net/Commands/Utils/Quote/Quote.cs +++ b/Geekbot.net/Commands/Utils/Quote/Quote.cs @@ -60,7 +60,7 @@ namespace Geekbot.net.Commands.Utils.Quote [Command("save")] [Summary("Save a quote from the last sent message by @user")] - public async Task SaveQuote([Summary("@user")] IUser user) + public async Task SaveQuote([Summary("@someone")] IUser user) { try { @@ -96,7 +96,7 @@ namespace Geekbot.net.Commands.Utils.Quote [Command("save")] [Summary("Save a quote from a message id")] - public async Task SaveQuote([Summary("messageId")] ulong messageId) + public async Task SaveQuote([Summary("message-ID")] ulong messageId) { try { @@ -130,7 +130,7 @@ namespace Geekbot.net.Commands.Utils.Quote [Command("make")] [Summary("Create a quote from the last sent message by @user")] - public async Task ReturnSpecifiedQuote([Summary("@user")] IUser user) + public async Task ReturnSpecifiedQuote([Summary("@someone")] IUser user) { try { @@ -149,7 +149,7 @@ namespace Geekbot.net.Commands.Utils.Quote [Command("make")] [Summary("Create a quote from a message id")] - public async Task ReturnSpecifiedQuote([Summary("messageId")] ulong messageId) + public async Task ReturnSpecifiedQuote([Summary("message-ID")] ulong messageId) { try { @@ -170,7 +170,7 @@ namespace Geekbot.net.Commands.Utils.Quote [RequireUserPermission(GuildPermission.ManageMessages)] [RequireUserPermission(GuildPermission.ManageRoles)] [Summary("Remove a quote (required mod permissions)")] - public async Task RemoveQuote([Summary("quoteId")] int id) + public async Task RemoveQuote([Summary("quote-ID")] int id) { try { From 8f861ee0bcbb52251a7394b65f320764752d1c44 Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 16 May 2019 22:17:33 +0200 Subject: [PATCH 179/443] overload owner refreshuser with a user-id as parameter --- Geekbot.net/Commands/Admin/Owner/Owner.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Geekbot.net/Commands/Admin/Owner/Owner.cs b/Geekbot.net/Commands/Admin/Owner/Owner.cs index 74e0e8e..996d848 100644 --- a/Geekbot.net/Commands/Admin/Owner/Owner.cs +++ b/Geekbot.net/Commands/Admin/Owner/Owner.cs @@ -92,6 +92,22 @@ namespace Geekbot.net.Commands.Admin.Owner await _errorHandler.HandleCommandException(e, Context); } } + + [Command("refreshuser", RunMode = RunMode.Async)] + [Summary("Refresh a user in the user cache")] + public async Task PopUserRepoCommand([Summary("user-id")] ulong userId) + { + try + { + var user = _client.GetUser(userId); + await _userRepository.Update(user); + await ReplyAsync($"Refreshed: {user.Username}#{user.Discriminator}"); + } + catch (Exception e) + { + await _errorHandler.HandleCommandException(e, Context); + } + } [Command("error", RunMode = RunMode.Async)] [Summary("Throw an error un purpose")] From 57698c8ce63b5aa10fed87cba1918e9fa1321f9b Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 16 May 2019 22:40:11 +0200 Subject: [PATCH 180/443] Fix mtg issue and update json.net --- Geekbot.net/Commands/Integrations/MagicTheGathering.cs | 1 - Geekbot.net/Geekbot.net.csproj | 4 ++-- WikipediaApi/WikipediaApi.csproj | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Geekbot.net/Commands/Integrations/MagicTheGathering.cs b/Geekbot.net/Commands/Integrations/MagicTheGathering.cs index ecf09cd..ef16fee 100644 --- a/Geekbot.net/Commands/Integrations/MagicTheGathering.cs +++ b/Geekbot.net/Commands/Integrations/MagicTheGathering.cs @@ -32,7 +32,6 @@ namespace Geekbot.net.Commands.Integrations var result = service .Where(x => x.Name, cardName) // fewer cards less risk of deserialization problems, don't need more than one anyways... - // ToDo: fix the deserialization issue in card[n].foreignNames[] .Where(x => x.PageSize, 1); var card = result.All().Value.FirstOrDefault(); diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 9c5c5c7..db8cf08 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -39,9 +39,9 @@ - + - + diff --git a/WikipediaApi/WikipediaApi.csproj b/WikipediaApi/WikipediaApi.csproj index 79206c2..3e82c68 100644 --- a/WikipediaApi/WikipediaApi.csproj +++ b/WikipediaApi/WikipediaApi.csproj @@ -3,6 +3,6 @@ netcoreapp2.2 - + \ No newline at end of file From 8822e65e6a8fcb8b51b495c90d9f901dc4fa1951 Mon Sep 17 00:00:00 2001 From: runebaas Date: Tue, 21 May 2019 19:42:04 +0200 Subject: [PATCH 181/443] Upgrade Discord.net to V2.1 --- Geekbot.net/Geekbot.net.csproj | 2 +- Geekbot.net/Program.cs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index db8cf08..4749374 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -22,7 +22,7 @@ - 2.0.1 + 2.1.0 diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index d04dcbb..b7251a8 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -79,7 +79,8 @@ namespace Geekbot.net _client = new DiscordSocketClient(new DiscordSocketConfig { LogLevel = LogSeverity.Verbose, - MessageCacheSize = 1000 + MessageCacheSize = 1000, + ExclusiveBulkDelete = true }); _client.Log += discordLogger.Log; _commands = new CommandService(); From ced287e492d970f2dcb151a008916df093f4d79d Mon Sep 17 00:00:00 2001 From: runebaas Date: Tue, 21 May 2019 20:50:50 +0200 Subject: [PATCH 182/443] Allow a server admin to specify a channel for the welcome message --- Geekbot.net/Commands/Admin/Admin.cs | 25 +++++++++++++- .../Database/Models/GuildSettingsModel.cs | 6 ++-- Geekbot.net/Handlers.cs | 33 +++++++++++++++---- 3 files changed, 55 insertions(+), 9 deletions(-) diff --git a/Geekbot.net/Commands/Admin/Admin.cs b/Geekbot.net/Commands/Admin/Admin.cs index e2e1f86..15b4df1 100644 --- a/Geekbot.net/Commands/Admin/Admin.cs +++ b/Geekbot.net/Commands/Admin/Admin.cs @@ -46,6 +46,29 @@ namespace Geekbot.net.Commands.Admin await ReplyAsync($"Welcome message has been changed\r\nHere is an example of how it would look:\r\n{formatedMessage}"); } + [Command("welcomechannel", RunMode = RunMode.Async)] + [Summary("Set a channel for the welcome messages (by default it uses the top most channel)")] + public async Task SelectWelcomeChannel([Summary("#Channel")] ISocketMessageChannel channel) + { + try + { + var m = await channel.SendMessageAsync("..."); + + var guild = await GetGuildSettings(Context.Guild.Id); + guild.WelcomeChannel = channel.Id.AsLong(); + _database.GuildSettings.Update(guild); + await _database.SaveChangesAsync(); + + await m.DeleteAsync(); + + await ReplyAsync("Successfully saved the welcome channel"); + } + catch (Exception e) + { + await _errorHandler.HandleCommandException(e, Context, "That channel doesn't seem to exist or i don't have write permissions"); + } + } + [Command("modchannel", RunMode = RunMode.Async)] [Summary("Set a channel for moderation purposes")] public async Task SelectModChannel([Summary("#Channel")] ISocketMessageChannel channel) @@ -67,7 +90,7 @@ namespace Geekbot.net.Commands.Admin } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context, "That channel doesn't seem to be valid"); + await _errorHandler.HandleCommandException(e, Context, "That channel doesn't seem to exist or i don't have write permissions"); } } diff --git a/Geekbot.net/Database/Models/GuildSettingsModel.cs b/Geekbot.net/Database/Models/GuildSettingsModel.cs index 6bc98b1..ebb6dc4 100644 --- a/Geekbot.net/Database/Models/GuildSettingsModel.cs +++ b/Geekbot.net/Database/Models/GuildSettingsModel.cs @@ -6,7 +6,7 @@ namespace Geekbot.net.Database.Models { [Key] public int Id { get; set; } - + [Required] public long GuildId { get; set; } @@ -15,9 +15,11 @@ namespace Geekbot.net.Database.Models public bool Hui { get; set; } = false; public long ModChannel { get; set; } = 0; - + public string WelcomeMessage { get; set; } + public long WelcomeChannel { get; set; } + public bool ShowDelete { get; set; } = false; public bool ShowLeave { get; set; } = false; diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index 6dd53d0..a254c14 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -4,6 +4,7 @@ using System.Text; using System.Threading.Tasks; using Discord; using Discord.Commands; +using Discord.Rest; using Discord.WebSocket; using Geekbot.net.Database; using Geekbot.net.Database.Models; @@ -143,18 +144,38 @@ namespace Geekbot.net { try { + var userRepoUpdate = _userRepository.Update(user); + _logger.Information(LogSource.Geekbot, $"{user.Username} ({user.Id}) joined {user.Guild.Name} ({user.Guild.Id})"); + if (!user.IsBot) { - var message = _database.GuildSettings.FirstOrDefault(guild => guild.GuildId.Equals(user.Guild.Id.AsLong()))?.WelcomeMessage; - if (!string.IsNullOrEmpty(message)) + var guildSettings = _database.GuildSettings.FirstOrDefault(guild => guild.GuildId == user.Guild.Id.AsLong()); + var message = guildSettings?.WelcomeMessage; + if (string.IsNullOrEmpty(message)) return; + message = message.Replace("$user", user.Mention); + + var fallbackSender = new Func>(() => user.Guild.DefaultChannel.SendMessageAsync(message)); + if (guildSettings.WelcomeChannel != 0) { - message = message.Replace("$user", user.Mention); - await user.Guild.DefaultChannel.SendMessageAsync(message); + try + { + var target = await _client.GetChannelAsync(guildSettings.WelcomeChannel.AsUlong()); + var channel = target as ISocketMessageChannel; + await channel.SendMessageAsync(message); + } + catch (Exception e) + { + _logger.Error(LogSource.Geekbot, "Failed to send welcome message to user defined welcome channel", e); + await fallbackSender(); + } + } + else + { + await fallbackSender(); } } - await _userRepository.Update(user); - _logger.Information(LogSource.Geekbot, $"{user.Username} ({user.Id}) joined {user.Guild.Name} ({user.Guild.Id})"); + await userRepoUpdate; } catch (Exception e) { From 288c976674afe692af29e27b1f8f19962e68e4d5 Mon Sep 17 00:00:00 2001 From: runebaas Date: Tue, 28 May 2019 20:32:15 +0200 Subject: [PATCH 183/443] rename bdcc to bdcb --- .../Commands/Randomness/BenedictCumberbatchNameGenerator.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Geekbot.net/Commands/Randomness/BenedictCumberbatchNameGenerator.cs b/Geekbot.net/Commands/Randomness/BenedictCumberbatchNameGenerator.cs index 501f891..838dff2 100644 --- a/Geekbot.net/Commands/Randomness/BenedictCumberbatchNameGenerator.cs +++ b/Geekbot.net/Commands/Randomness/BenedictCumberbatchNameGenerator.cs @@ -18,7 +18,7 @@ namespace Geekbot.net.Commands.Randomness _randomNumberGenerator = randomNumberGenerator; } - [Command("bdcc", RunMode = RunMode.Async)] + [Command("bdcb", RunMode = RunMode.Async)] [Summary("Benedict Cumberbatch Name Generator")] public async Task GetQuote() { @@ -59,4 +59,4 @@ namespace Geekbot.net.Commands.Randomness } } } -} \ No newline at end of file +} From 8fadff4092d2c92f495555c677ce2963e2f2f5d0 Mon Sep 17 00:00:00 2001 From: runebaas Date: Tue, 4 Jun 2019 23:00:16 +0200 Subject: [PATCH 184/443] Cookies timeout is now at midnight instead of every 24h --- Geekbot.net/Commands/Rpg/Cookies.cs | 4 ++-- Geekbot.net/Lib/Localization/Translations.yml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Geekbot.net/Commands/Rpg/Cookies.cs b/Geekbot.net/Commands/Rpg/Cookies.cs index f22eccf..3c41044 100644 --- a/Geekbot.net/Commands/Rpg/Cookies.cs +++ b/Geekbot.net/Commands/Rpg/Cookies.cs @@ -38,9 +38,9 @@ namespace Geekbot.net.Commands.Rpg { var transContext = await _translation.GetGuildContext(Context); var actor = await GetUser(Context.User.Id); - if (actor.LastPayout.Value.AddHours(24) > DateTimeOffset.Now) + if (actor.LastPayout.Value.AddDays(1).Date > DateTime.Now.Date) { - var formatedWaitTime = transContext.FormatDateTimeAsRemaining(actor.LastPayout.Value.AddHours(24)); + var formatedWaitTime = transContext.FormatDateTimeAsRemaining(DateTimeOffset.Now.AddDays(1).Date); await ReplyAsync(transContext.GetString("WaitForMoreCookies", formatedWaitTime)); return; } diff --git a/Geekbot.net/Lib/Localization/Translations.yml b/Geekbot.net/Lib/Localization/Translations.yml index 04835a2..4bf4d14 100644 --- a/Geekbot.net/Lib/Localization/Translations.yml +++ b/Geekbot.net/Lib/Localization/Translations.yml @@ -90,8 +90,8 @@ cookies: EN: "You got {0} cookies, there are now {1} cookies in you cookie jar" CHDE: "Du häsch {0} guetzli becho, du häsch jetzt {1} guetzli ih dr büchse" WaitForMoreCookies: - EN: "You already got cookies in the last 24 hours, you can have more cookies in {0}" - CHDE: "Du hesch scho guetzli becho ih de letzti 24 stund, du chasch meh ha in {0}" + EN: "You already got cookies today, you can have more cookies in {0}" + CHDE: "Du hesch scho guetzli becho hüt, du chasch meh ha in {0}" InYourJar: EN: "There are {0} cookies in you cookie jar" CHDE: "Es hät {0} guetzli ih dineri büchs" From ac43d087b175686081b8e29d1e0f86e008b09e8c Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 21 Jul 2019 13:00:02 +0200 Subject: [PATCH 185/443] Add the !cookie alias and allow yaml alliases in the translations file --- Geekbot.net/Commands/Rpg/Cookies.cs | 1 + Geekbot.net/Lib/Localization/TranslationHandler.cs | 6 +++--- Geekbot.net/Lib/Localization/Translations.yml | 5 ++++- Tests/Lib/Localization/Translations.test.cs | 4 +++- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Geekbot.net/Commands/Rpg/Cookies.cs b/Geekbot.net/Commands/Rpg/Cookies.cs index 3c41044..b1a2d7d 100644 --- a/Geekbot.net/Commands/Rpg/Cookies.cs +++ b/Geekbot.net/Commands/Rpg/Cookies.cs @@ -15,6 +15,7 @@ namespace Geekbot.net.Commands.Rpg { [DisableInDirectMessage] [Group("cookies")] + [Alias("cookie")] public class Cookies : ModuleBase { private readonly DatabaseContext _database; diff --git a/Geekbot.net/Lib/Localization/TranslationHandler.cs b/Geekbot.net/Lib/Localization/TranslationHandler.cs index 3d1ee3a..a7a1a12 100644 --- a/Geekbot.net/Lib/Localization/TranslationHandler.cs +++ b/Geekbot.net/Lib/Localization/TranslationHandler.cs @@ -8,8 +8,7 @@ using Geekbot.net.Database; using Geekbot.net.Database.Models; using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Logger; -using Utf8Json; -using YamlDotNet.RepresentationModel; +using YamlDotNet.Core; using YamlDotNet.Serialization; namespace Geekbot.net.Lib.Localization @@ -39,8 +38,9 @@ namespace Geekbot.net.Lib.Localization // Deserialize var input = new StringReader(translationFile); + var mergingParser = new MergingParser(new Parser(input)); var deserializer = new DeserializerBuilder().Build(); - var rawTranslations = deserializer.Deserialize>>>(input); + var rawTranslations = deserializer.Deserialize>>>(mergingParser); // Sort var sortedPerLanguage = new Dictionary>>(); diff --git a/Geekbot.net/Lib/Localization/Translations.yml b/Geekbot.net/Lib/Localization/Translations.yml index 4bf4d14..730ea40 100644 --- a/Geekbot.net/Lib/Localization/Translations.yml +++ b/Geekbot.net/Lib/Localization/Translations.yml @@ -85,7 +85,7 @@ roll: NoPrevGuess: EN: ":red_circle: {0}, you can't guess the same number again" CHDE: ":red_circle: {0}, du chasch nid nomol es gliche rate" -cookies: +cookies: &cookiesAlias GetCookies: EN: "You got {0} cookies, there are now {1} cookies in you cookie jar" CHDE: "Du häsch {0} guetzli becho, du häsch jetzt {1} guetzli ih dr büchse" @@ -107,6 +107,9 @@ cookies: AteCookies: EN: "You ate {0} cookies, you've only got {1} cookies left" CHDE: "Du hesch {0} guetzli gesse und hesch jezt no {1} übrig" +cookie: + # because command aliases are to hard to deal with... + <<: *cookiesAlias role: NoRolesConfigured: EN: "There are no roles configured for this server" diff --git a/Tests/Lib/Localization/Translations.test.cs b/Tests/Lib/Localization/Translations.test.cs index fc43091..736ed83 100644 --- a/Tests/Lib/Localization/Translations.test.cs +++ b/Tests/Lib/Localization/Translations.test.cs @@ -3,6 +3,7 @@ using System.IO; using System.Linq; using FluentAssertions; using Xunit; +using YamlDotNet.Core; using YamlDotNet.Serialization; namespace Tests.Lib.Localization @@ -17,8 +18,9 @@ namespace Tests.Lib.Localization // Deserialize var input = new StringReader(translationFile); + var mergingParser = new MergingParser(new Parser(input)); var deserializer = new DeserializerBuilder().Build(); - var rawTranslations = deserializer.Deserialize>>>(input); + var rawTranslations = deserializer.Deserialize>>>(mergingParser); // These languages must be supported var supportedLanguages = new List From 7a250f664215d3a36739469b30ced674e32546f4 Mon Sep 17 00:00:00 2001 From: runebaas Date: Tue, 30 Jul 2019 00:42:08 +0200 Subject: [PATCH 186/443] Ignore commands on certain serves --- Geekbot.net/Handlers.cs | 28 +++++++++++++++++++++++++--- Geekbot.net/Program.cs | 3 ++- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index a254c14..d16750b 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -28,10 +29,12 @@ namespace Geekbot.net private readonly IUserRepository _userRepository; private readonly IReactionListener _reactionListener; private readonly DatabaseContext _messageCounterDatabaseContext; + private readonly RestApplication _applicationInfo; + private readonly List _ignoredServers; public Handlers(DatabaseInitializer databaseInitializer, IDiscordClient client, IGeekbotLogger logger, IAlmostRedis redis, IServiceProvider servicesProvider, CommandService commands, IUserRepository userRepository, - IReactionListener reactionListener) + IReactionListener reactionListener, RestApplication applicationInfo) { _database = databaseInitializer.Initialize(); _messageCounterDatabaseContext = databaseInitializer.Initialize(); @@ -42,6 +45,14 @@ namespace Geekbot.net _commands = commands; _userRepository = userRepository; _reactionListener = reactionListener; + _applicationInfo = applicationInfo; + // ToDo: create a clean solution for this... + _ignoredServers = new List() + { + 228623803201224704, // SwitzerLAN + 169844523181015040, // EEvent + 248531441548263425 // MYI + }; } // @@ -56,10 +67,21 @@ namespace Geekbot.net if (message.Author.IsBot) return Task.CompletedTask; var argPos = 0; + var guildId = ((SocketGuildChannel) message.Channel).Guild.Id; + // Some guilds only wanted very specific functionally without any of the commands, a quick hack that solves that short term... + // ToDo: cleanup + if (_ignoredServers.Contains(guildId)) + { + if (message.Author.Id != _applicationInfo.Owner.Id) + { + return Task.CompletedTask; + } + } + var lowCaseMsg = message.ToString().ToLower(); if (lowCaseMsg.StartsWith("hui")) { - var hasPing = _database.GuildSettings.FirstOrDefault(guild => guild.GuildId.Equals(((SocketGuildChannel) message.Channel).Guild.Id.AsLong()))?.Hui ?? false; + var hasPing = _database.GuildSettings.FirstOrDefault(guild => guild.GuildId.Equals(guildId.AsLong()))?.Hui ?? false; if (hasPing) { message.Channel.SendMessageAsync("hui!!!"); @@ -69,7 +91,7 @@ namespace Geekbot.net if (lowCaseMsg.StartsWith("ping ") || lowCaseMsg.Equals("ping")) { - var hasPing = _database.GuildSettings.FirstOrDefault(guild => guild.GuildId.Equals(((SocketGuildChannel) message.Channel).Guild.Id.AsLong()))?.Ping ?? false; + var hasPing = _database.GuildSettings.FirstOrDefault(guild => guild.GuildId.Equals(guildId.AsLong()))?.Ping ?? false; if (hasPing) { message.Channel.SendMessageAsync("pong"); diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index b7251a8..72ec6d5 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -156,6 +156,7 @@ namespace Geekbot.net var isConneted = await IsConnected(); if (isConneted) { + var applicationInfo = await _client.GetApplicationInfoAsync(); await _client.SetGameAsync(_globalSettings.GetKey("Game")); _logger.Information(LogSource.Geekbot, $"Now Connected as {_client.CurrentUser.Username} to {_client.Guilds.Count} Servers"); @@ -170,7 +171,7 @@ namespace Geekbot.net _servicesProvider = _services.BuildServiceProvider(); await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _servicesProvider); - var handlers = new Handlers(_databaseInitializer, _client, _logger, _redis, _servicesProvider, _commands, _userRepository, reactionListener); + var handlers = new Handlers(_databaseInitializer, _client, _logger, _redis, _servicesProvider, _commands, _userRepository, reactionListener, applicationInfo); _client.MessageReceived += handlers.RunCommand; _client.MessageDeleted += handlers.MessageDeleted; From fe4a78b7433fc096472353831159033317e6eace Mon Sep 17 00:00:00 2001 From: runebaas Date: Tue, 30 Jul 2019 00:47:32 +0200 Subject: [PATCH 187/443] Allow failure on sentry during deployment --- .gitlab-ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 8c7a530..fc60d1b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -23,6 +23,7 @@ build: sentry: stage: ops image: getsentry/sentry-cli + allow_failure: true only: - master dependencies: From 1bfd7c7a129fc675f19a317bb3554e3d5b19d80b Mon Sep 17 00:00:00 2001 From: runebaas Date: Mon, 5 Aug 2019 09:14:58 +0200 Subject: [PATCH 188/443] Ignore the discord bots server aswell --- Geekbot.net/Handlers.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index d16750b..09f95be 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -51,7 +51,8 @@ namespace Geekbot.net { 228623803201224704, // SwitzerLAN 169844523181015040, // EEvent - 248531441548263425 // MYI + 248531441548263425, // MYI + 110373943822540800 // Discord Bots }; } From 143722eccf3b775dc2eaf0a0d5ea4b90c218c916 Mon Sep 17 00:00:00 2001 From: runebaas Date: Mon, 5 Aug 2019 09:18:16 +0200 Subject: [PATCH 189/443] Stop using redis for counting messages --- Geekbot.net/Handlers.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index 09f95be..4671e07 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -129,7 +129,6 @@ namespace Geekbot.net var channel = (SocketGuildChannel) message.Channel; - // just testing, redis will remain the source of truth for now var rowId = await _messageCounterDatabaseContext.Database.ExecuteSqlCommandAsync( "UPDATE \"Messages\" SET \"MessageCount\" = \"MessageCount\" + 1 WHERE \"GuildId\" = {0} AND \"UserId\" = {1}", channel.Guild.Id.AsLong(), @@ -147,9 +146,6 @@ namespace Geekbot.net _messageCounterDatabaseContext.SaveChanges(); } - await _redis.Db.HashIncrementAsync($"{channel.Guild.Id}:Messages", message.Author.Id.ToString()); - await _redis.Db.HashIncrementAsync($"{channel.Guild.Id}:Messages", 0.ToString()); - if (message.Author.IsBot) return; _logger.Information(LogSource.Message, message.Content, SimpleConextConverter.ConvertSocketMessage(message)); } From f1387f824e34e99b8fd017767d620be7563f1736 Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 19 Sep 2019 13:40:02 +0200 Subject: [PATCH 190/443] Remove the google command --- .../Commands/Integrations/Google/Google.cs | 68 ------------------- .../Google/GoogleKgApiDetailedDto.cs | 9 --- .../Google/GoogleKgApiElementDto.cs | 8 --- .../Google/GoogleKgApiImageDto.cs | 8 --- .../Google/GoogleKgApiResponseDto.cs | 9 --- .../Google/GoogleKgApiResultDto.cs | 10 --- 6 files changed, 112 deletions(-) delete mode 100644 Geekbot.net/Commands/Integrations/Google/Google.cs delete mode 100644 Geekbot.net/Commands/Integrations/Google/GoogleKgApiDetailedDto.cs delete mode 100644 Geekbot.net/Commands/Integrations/Google/GoogleKgApiElementDto.cs delete mode 100644 Geekbot.net/Commands/Integrations/Google/GoogleKgApiImageDto.cs delete mode 100644 Geekbot.net/Commands/Integrations/Google/GoogleKgApiResponseDto.cs delete mode 100644 Geekbot.net/Commands/Integrations/Google/GoogleKgApiResultDto.cs diff --git a/Geekbot.net/Commands/Integrations/Google/Google.cs b/Geekbot.net/Commands/Integrations/Google/Google.cs deleted file mode 100644 index db059b4..0000000 --- a/Geekbot.net/Commands/Integrations/Google/Google.cs +++ /dev/null @@ -1,68 +0,0 @@ -using System; -using System.Linq; -using System.Net; -using System.Threading.Tasks; -using Discord; -using Discord.Commands; -using Geekbot.net.Lib.ErrorHandling; -using Geekbot.net.Lib.GlobalSettings; -using Newtonsoft.Json; - -namespace Geekbot.net.Commands.Integrations.Google -{ - public class Google : ModuleBase - { - private readonly IErrorHandler _errorHandler; - private readonly IGlobalSettings _globalSettings; - - public Google(IErrorHandler errorHandler, IGlobalSettings globalSettings) - { - _errorHandler = errorHandler; - _globalSettings = globalSettings; - } - - [Command("google", RunMode = RunMode.Async)] - [Summary("Google Something.")] - public async Task AskGoogle([Remainder, Summary("search-text")] string searchText) - { - try - { - using (var client = new WebClient()) - { - var apiKey = _globalSettings.GetKey("GoogleGraphKey"); - if (string.IsNullOrEmpty(apiKey)) - { - await ReplyAsync("No Google API key has been set, please contact my owner"); - return; - } - - var url = new Uri($"https://kgsearch.googleapis.com/v1/entities:search?languages=en&limit=1&query={searchText}&key={apiKey}"); - var responseString = client.DownloadString(url); - var response = JsonConvert.DeserializeObject(responseString); - - if (!response.ItemListElement.Any()) - { - await ReplyAsync("No results were found..."); - return; - } - - var data = response.ItemListElement.First().Result; - var eb = new EmbedBuilder - { - Title = data.Name - }; - if(!string.IsNullOrEmpty(data.Description)) eb.WithDescription(data.Description); - if(!string.IsNullOrEmpty(data.DetailedDtoDescription?.Url)) eb.WithUrl(data.DetailedDtoDescription.Url); - if(!string.IsNullOrEmpty(data.DetailedDtoDescription?.ArticleBody)) eb.AddField("Details", data.DetailedDtoDescription.ArticleBody); - if(!string.IsNullOrEmpty(data.Image?.ContentUrl)) eb.WithThumbnailUrl(data.Image.ContentUrl); - - await ReplyAsync("", false, eb.Build()); - } - } - catch (Exception e) - { - await _errorHandler.HandleCommandException(e, Context); - } - } - } -} \ No newline at end of file diff --git a/Geekbot.net/Commands/Integrations/Google/GoogleKgApiDetailedDto.cs b/Geekbot.net/Commands/Integrations/Google/GoogleKgApiDetailedDto.cs deleted file mode 100644 index 031d1e7..0000000 --- a/Geekbot.net/Commands/Integrations/Google/GoogleKgApiDetailedDto.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace Geekbot.net.Commands.Integrations.Google -{ - public class GoogleKgApiDetailedDto - { - public string ArticleBody { get; set; } - public string Url { get; set; } - public string License { get; set; } - } -} \ No newline at end of file diff --git a/Geekbot.net/Commands/Integrations/Google/GoogleKgApiElementDto.cs b/Geekbot.net/Commands/Integrations/Google/GoogleKgApiElementDto.cs deleted file mode 100644 index a48b184..0000000 --- a/Geekbot.net/Commands/Integrations/Google/GoogleKgApiElementDto.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Geekbot.net.Commands.Integrations.Google -{ - public class GoogleKgApiElementDto - { - public GoogleKgApiResultDto Result { get; set; } - public double ResultScore { get; set; } - } -} \ No newline at end of file diff --git a/Geekbot.net/Commands/Integrations/Google/GoogleKgApiImageDto.cs b/Geekbot.net/Commands/Integrations/Google/GoogleKgApiImageDto.cs deleted file mode 100644 index fe7cdaa..0000000 --- a/Geekbot.net/Commands/Integrations/Google/GoogleKgApiImageDto.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Geekbot.net.Commands.Integrations.Google -{ - public class GoogleKgApiImageDto - { - public string ContentUrl { get; set; } - public string Url { get; set; } - } -} \ No newline at end of file diff --git a/Geekbot.net/Commands/Integrations/Google/GoogleKgApiResponseDto.cs b/Geekbot.net/Commands/Integrations/Google/GoogleKgApiResponseDto.cs deleted file mode 100644 index af337db..0000000 --- a/Geekbot.net/Commands/Integrations/Google/GoogleKgApiResponseDto.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System.Collections.Generic; - -namespace Geekbot.net.Commands.Integrations.Google -{ - public class GoogleKgApiResponseDto - { - public List ItemListElement { get; set; } - } -} \ No newline at end of file diff --git a/Geekbot.net/Commands/Integrations/Google/GoogleKgApiResultDto.cs b/Geekbot.net/Commands/Integrations/Google/GoogleKgApiResultDto.cs deleted file mode 100644 index 465f1d7..0000000 --- a/Geekbot.net/Commands/Integrations/Google/GoogleKgApiResultDto.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace Geekbot.net.Commands.Integrations.Google -{ - public class GoogleKgApiResultDto - { - public string Name { get; set; } - public string Description { get; set; } - public GoogleKgApiImageDto Image { get; set; } - public GoogleKgApiDetailedDto DetailedDtoDescription { get; set; } - } -} \ No newline at end of file From 309f06370b73c6c656adbebe9e21e45f341c6bc9 Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 19 Sep 2019 13:42:48 +0200 Subject: [PATCH 191/443] Cut urban dictionary word definitions at the 1800 character mark --- .../Commands/Integrations/UbranDictionary/UrbanDictionary.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs b/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs index 4f29f10..932425b 100644 --- a/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs +++ b/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs @@ -48,7 +48,7 @@ namespace Geekbot.net.Commands.Integrations.UbranDictionary Url = definition.Permalink }); eb.WithColor(new Color(239, 255, 0)); - if (!string.IsNullOrEmpty(definition.Definition)) eb.Description = definition.Definition; + if (!string.IsNullOrEmpty(definition.Definition)) eb.Description = definition.Definition.Substring(0, 1800); if (!string.IsNullOrEmpty(definition.Example)) eb.AddField("Example", definition.Example ?? "(no example given...)"); if (!string.IsNullOrEmpty(definition.ThumbsUp)) eb.AddInlineField("Upvotes", definition.ThumbsUp); if (!string.IsNullOrEmpty(definition.ThumbsDown)) eb.AddInlineField("Downvotes", definition.ThumbsDown); From 19df65fc760bec170ffc1f9652f9e2ce94d68559 Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 19 Sep 2019 21:27:34 +0200 Subject: [PATCH 192/443] Fix out of bounds error in the urban dict. command --- .../Integrations/UbranDictionary/UrbanDictionary.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs b/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs index 932425b..254c2bb 100644 --- a/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs +++ b/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs @@ -41,6 +41,12 @@ namespace Geekbot.net.Commands.Integrations.UbranDictionary var definition = definitions.List.First(e => !string.IsNullOrWhiteSpace(e.Example)); + var description = definition.Definition; + if (description.Length > 1801) + { + description = description.Substring(0, 1800) + " [...]"; + } + var eb = new EmbedBuilder(); eb.WithAuthor(new EmbedAuthorBuilder { @@ -48,7 +54,7 @@ namespace Geekbot.net.Commands.Integrations.UbranDictionary Url = definition.Permalink }); eb.WithColor(new Color(239, 255, 0)); - if (!string.IsNullOrEmpty(definition.Definition)) eb.Description = definition.Definition.Substring(0, 1800); + if (!string.IsNullOrEmpty(definition.Definition)) eb.Description = description; if (!string.IsNullOrEmpty(definition.Example)) eb.AddField("Example", definition.Example ?? "(no example given...)"); if (!string.IsNullOrEmpty(definition.ThumbsUp)) eb.AddInlineField("Upvotes", definition.ThumbsUp); if (!string.IsNullOrEmpty(definition.ThumbsDown)) eb.AddInlineField("Downvotes", definition.ThumbsDown); From b7b4a600cd69b0f03d0a1087101778d6e53d3dbf Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 11 Oct 2019 00:55:55 +0200 Subject: [PATCH 193/443] Fix message when a role has been added to the whitelist --- Geekbot.net/Commands/Admin/Role.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Geekbot.net/Commands/Admin/Role.cs b/Geekbot.net/Commands/Admin/Role.cs index 85b7160..decb7fc 100644 --- a/Geekbot.net/Commands/Admin/Role.cs +++ b/Geekbot.net/Commands/Admin/Role.cs @@ -133,7 +133,7 @@ namespace Geekbot.net.Commands.Admin WhiteListName = roleName }); await _database.SaveChangesAsync(); - await ReplyAsync(transContext.GetString("CannotAddDangerousRole", role.Name)); + await ReplyAsync(transContext.GetString("AddedRoleToWhitelist", role.Name)); } catch (Exception e) { From 8dd914e0124471974e56c07bcc4f88a774d0888b Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 11 Oct 2019 01:08:59 +0200 Subject: [PATCH 194/443] Properly log errors when adding a role emoji --- Geekbot.net/Commands/Admin/Role.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Geekbot.net/Commands/Admin/Role.cs b/Geekbot.net/Commands/Admin/Role.cs index decb7fc..b0df4bb 100644 --- a/Geekbot.net/Commands/Admin/Role.cs +++ b/Geekbot.net/Commands/Admin/Role.cs @@ -196,8 +196,7 @@ namespace Geekbot.net.Commands.Admin } catch (Exception e) { - await Context.Channel.SendMessageAsync("Something went wrong... please try again on a new message"); - Console.WriteLine(e); + await _errorHandler.HandleCommandException(e, Context); } } } From 20c75019f9391a612380f96571e4f8377826991a Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 26 Oct 2019 21:54:34 +0200 Subject: [PATCH 195/443] Upgrade to dotnet core 3 and update dependencies --- .gitlab-ci.yml | 2 +- Geekbot.net/Geekbot.net.csproj | 41 +++++++++++++++----------------- Geekbot.net/Handlers.cs | 2 +- Tests/Tests.csproj | 6 ++--- WikipediaApi/WikipediaApi.csproj | 2 +- 5 files changed, 25 insertions(+), 28 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index fc60d1b..7e32f69 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -10,7 +10,7 @@ before_script: build: stage: build - image: mcr.microsoft.com/dotnet/core/sdk:2.2 + image: mcr.microsoft.com/dotnet/core/sdk:3.0 artifacts: expire_in: 1h paths: diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 4749374..3fb4ee1 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -1,7 +1,7 @@  Exe - netcoreapp2.2 + netcoreapp3.0 win-x64;linux-x64 derp.ico 4.1.0 @@ -20,33 +20,30 @@ true - - - 2.1.0 - - - + + + + - + - - - - - - - - - + + + + + + + + + - - - - - + + + + diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index 4671e07..7317274 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -129,7 +129,7 @@ namespace Geekbot.net var channel = (SocketGuildChannel) message.Channel; - var rowId = await _messageCounterDatabaseContext.Database.ExecuteSqlCommandAsync( + var rowId = await _messageCounterDatabaseContext.Database.ExecuteSqlRawAsync( "UPDATE \"Messages\" SET \"MessageCount\" = \"MessageCount\" + 1 WHERE \"GuildId\" = {0} AND \"UserId\" = {1}", channel.Guild.Id.AsLong(), message.Author.Id.AsLong() diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 1c07b9c..854efc4 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -1,13 +1,13 @@  - netcoreapp2.2 + netcoreapp3.0 false NU1701 xUnit1026 - - + + diff --git a/WikipediaApi/WikipediaApi.csproj b/WikipediaApi/WikipediaApi.csproj index 3e82c68..349fc0b 100644 --- a/WikipediaApi/WikipediaApi.csproj +++ b/WikipediaApi/WikipediaApi.csproj @@ -1,6 +1,6 @@  - netcoreapp2.2 + netcoreapp3.0 From cd04549834ca7eb4756a70866462c329b5f303ab Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 26 Oct 2019 22:20:22 +0200 Subject: [PATCH 196/443] Fix deployment to accomodate dotnet core 3 changes --- .gitlab-ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 7e32f69..13858d2 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -16,9 +16,9 @@ build: paths: - Geekbot.net/Binaries/ script: - - dotnet restore -s https://api.nuget.org/v3/index.json -s https://www.myget.org/F/discord-net/api/v3/index.json + - dotnet restore - dotnet test Tests - - dotnet publish --version-suffix ${CI_COMMIT_SHA:0:8} --configuration Release -o Binaries ./ + - dotnet publish --version-suffix ${CI_COMMIT_SHA:0:8} --configuration Release -o ./Geekbot.net/Binaries ./Geekbot.net/ sentry: stage: ops From dd9cf3c5d7ec02384f79b82e3bed054064ad300e Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 8 Feb 2020 15:32:58 +0100 Subject: [PATCH 197/443] Upgrade to dotnet core 3.1 --- .gitlab-ci.yml | 2 +- Geekbot.net/Geekbot.net.csproj | 34 ++++++++++++++++---------------- Tests/Tests.csproj | 8 ++++---- WikipediaApi/WikipediaApi.csproj | 4 ++-- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 13858d2..cffe539 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -10,7 +10,7 @@ before_script: build: stage: build - image: mcr.microsoft.com/dotnet/core/sdk:3.0 + image: mcr.microsoft.com/dotnet/core/sdk:3.1 artifacts: expire_in: 1h paths: diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 3fb4ee1..fe2f8cc 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -1,7 +1,7 @@  Exe - netcoreapp3.0 + netcoreapp3.1 win-x64;linux-x64 derp.ico 4.1.0 @@ -20,32 +20,32 @@ true - + - - + + - - - - - - - - + + + + + + + + - - - - + + + + - + diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 854efc4..235cc0b 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -1,14 +1,14 @@  - netcoreapp3.0 + netcoreapp3.1 false NU1701 xUnit1026 - - - + + + diff --git a/WikipediaApi/WikipediaApi.csproj b/WikipediaApi/WikipediaApi.csproj index 349fc0b..24c20f7 100644 --- a/WikipediaApi/WikipediaApi.csproj +++ b/WikipediaApi/WikipediaApi.csproj @@ -1,8 +1,8 @@  - netcoreapp3.0 + netcoreapp3.1 - + \ No newline at end of file From 21f813d342b847ce552fda6576f7a9459ccf3860 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 8 Feb 2020 15:42:42 +0100 Subject: [PATCH 198/443] Remove redundant code --- Geekbot.net/Commands/Randomness/Kanye/Kanye.cs | 2 -- Geekbot.net/Commands/Utils/Dice/Dice.cs | 3 +-- Geekbot.net/Database/DatabaseInitializer.cs | 1 - Geekbot.net/Handlers.cs | 2 +- Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs | 3 +-- Geekbot.net/Lib/GeekbotExitCode.cs | 2 +- Geekbot.net/Lib/Highscores/HighscoreManager.cs | 2 -- .../Lib/Localization/TranslationGuildContext.cs | 1 - .../Lib/ReactionListener/ReactionListener.cs | 4 +--- Geekbot.net/Program.cs | 15 ++++++++------- .../WebApi/Controllers/Commands/CommandDto.cs | 3 +-- Geekbot.net/WebApi/WebApiStartup.cs | 10 +++++----- 12 files changed, 19 insertions(+), 29 deletions(-) diff --git a/Geekbot.net/Commands/Randomness/Kanye/Kanye.cs b/Geekbot.net/Commands/Randomness/Kanye/Kanye.cs index 42a6ca7..dcd3bdc 100644 --- a/Geekbot.net/Commands/Randomness/Kanye/Kanye.cs +++ b/Geekbot.net/Commands/Randomness/Kanye/Kanye.cs @@ -3,9 +3,7 @@ using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; using Discord.Commands; -using Geekbot.net.Commands.Randomness.Dad; using Geekbot.net.Lib.ErrorHandling; -using Microsoft.AspNetCore.Hosting.Internal; using Newtonsoft.Json; namespace Geekbot.net.Commands.Randomness.Kanye diff --git a/Geekbot.net/Commands/Utils/Dice/Dice.cs b/Geekbot.net/Commands/Utils/Dice/Dice.cs index 6b95903..bc0244e 100644 --- a/Geekbot.net/Commands/Utils/Dice/Dice.cs +++ b/Geekbot.net/Commands/Utils/Dice/Dice.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; diff --git a/Geekbot.net/Database/DatabaseInitializer.cs b/Geekbot.net/Database/DatabaseInitializer.cs index 6adff44..582327f 100644 --- a/Geekbot.net/Database/DatabaseInitializer.cs +++ b/Geekbot.net/Database/DatabaseInitializer.cs @@ -2,7 +2,6 @@ using Geekbot.net.Database.LoggingAdapter; using Geekbot.net.Lib; using Geekbot.net.Lib.Logger; -using Microsoft.EntityFrameworkCore; using Npgsql.Logging; namespace Geekbot.net.Database diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index 7317274..13f523c 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -103,7 +103,7 @@ namespace Geekbot.net if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos))) return Task.CompletedTask; var context = new CommandContext(_client, message); - var commandExec = _commands.ExecuteAsync(context, argPos, _servicesProvider); + _commands.ExecuteAsync(context, argPos, _servicesProvider); _logger.Information(LogSource.Command, context.Message.Content.Split(" ")[0].Replace("!", ""), SimpleConextConverter.ConvertContext(context)); diff --git a/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs b/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs index 5323ee6..7c9d8ff 100644 --- a/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs +++ b/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs @@ -3,7 +3,6 @@ using System.Net; using System.Threading.Tasks; using Discord.Commands; using Discord.Net; -using Geekbot.net.Lib.GlobalSettings; using Geekbot.net.Lib.Localization; using Geekbot.net.Lib.Logger; using SharpRaven; @@ -19,7 +18,7 @@ namespace Geekbot.net.Lib.ErrorHandling private readonly IRavenClient _raven; private readonly bool _errorsInChat; - public ErrorHandler(IGeekbotLogger logger, ITranslationHandler translation, IGlobalSettings globalSettings, bool errorsInChat) + public ErrorHandler(IGeekbotLogger logger, ITranslationHandler translation, bool errorsInChat) { _logger = logger; _translation = translation; diff --git a/Geekbot.net/Lib/GeekbotExitCode.cs b/Geekbot.net/Lib/GeekbotExitCode.cs index d68aa4a..130d435 100644 --- a/Geekbot.net/Lib/GeekbotExitCode.cs +++ b/Geekbot.net/Lib/GeekbotExitCode.cs @@ -1,6 +1,6 @@ namespace Geekbot.net.Lib { - public enum GeekbotExitCode : int + public enum GeekbotExitCode { // General Clean = 0, diff --git a/Geekbot.net/Lib/Highscores/HighscoreManager.cs b/Geekbot.net/Lib/Highscores/HighscoreManager.cs index f3b3a18..e668ff2 100644 --- a/Geekbot.net/Lib/Highscores/HighscoreManager.cs +++ b/Geekbot.net/Lib/Highscores/HighscoreManager.cs @@ -1,9 +1,7 @@ -using System; using System.Collections.Generic; using System.Linq; using Geekbot.net.Database; using Geekbot.net.Lib.Extensions; -using Geekbot.net.Lib.Logger; using Geekbot.net.Lib.UserRepository; namespace Geekbot.net.Lib.Highscores diff --git a/Geekbot.net/Lib/Localization/TranslationGuildContext.cs b/Geekbot.net/Lib/Localization/TranslationGuildContext.cs index 6181c04..fb65aa6 100644 --- a/Geekbot.net/Lib/Localization/TranslationGuildContext.cs +++ b/Geekbot.net/Lib/Localization/TranslationGuildContext.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Text; using System.Threading.Tasks; diff --git a/Geekbot.net/Lib/ReactionListener/ReactionListener.cs b/Geekbot.net/Lib/ReactionListener/ReactionListener.cs index 18490db..26a60d3 100644 --- a/Geekbot.net/Lib/ReactionListener/ReactionListener.cs +++ b/Geekbot.net/Lib/ReactionListener/ReactionListener.cs @@ -18,7 +18,7 @@ namespace Geekbot.net.Lib.ReactionListener LoadListeners(); } - private Task LoadListeners() + private void LoadListeners() { var ids = _redis.SetMembers("MessageIds"); _listener = new Dictionary>(); @@ -43,8 +43,6 @@ namespace Geekbot.net.Lib.ReactionListener } _listener.Add(messageId, emojiDict); } - - return Task.CompletedTask; } public bool IsListener(ulong id) diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 72ec6d5..4907431 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -21,6 +21,7 @@ using Geekbot.net.Lib.Media; using Geekbot.net.Lib.RandomNumberGenerator; using Geekbot.net.Lib.ReactionListener; using Geekbot.net.Lib.UserRepository; +using Geekbot.net.WebApi; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using WikipediaApi; @@ -125,8 +126,8 @@ namespace Geekbot.net var wikipediaClient = new WikipediaClient(); var randomNumberGenerator = new RandomNumberGenerator(); - _services.AddSingleton(_redis); - _services.AddSingleton(_userRepository); + _services.AddSingleton(_redis); + _services.AddSingleton(_userRepository); _services.AddSingleton(logger); _services.AddSingleton(levelCalc); _services.AddSingleton(emojiConverter); @@ -136,9 +137,9 @@ namespace Geekbot.net _services.AddSingleton(mtgManaConverter); _services.AddSingleton(wikipediaClient); _services.AddSingleton(randomNumberGenerator); - _services.AddSingleton(_globalSettings); - _services.AddTransient((e) => new HighscoreManager(_databaseInitializer.Initialize(), _userRepository)); - _services.AddTransient((e) => _databaseInitializer.Initialize()); + _services.AddSingleton(_globalSettings); + _services.AddTransient(e => new HighscoreManager(_databaseInitializer.Initialize(), _userRepository)); + _services.AddTransient(e => _databaseInitializer.Initialize()); logger.Information(LogSource.Geekbot, "Connecting to Discord"); @@ -162,7 +163,7 @@ namespace Geekbot.net _logger.Information(LogSource.Geekbot, "Registering Stuff"); var translationHandler = new TranslationHandler(_databaseInitializer.Initialize(), _logger); - var errorHandler = new ErrorHandler(_logger, translationHandler, _globalSettings, _runParameters.ExposeErrors); + var errorHandler = new ErrorHandler(_logger, translationHandler, _runParameters.ExposeErrors); var reactionListener = new ReactionListener(_redis.Db); _services.AddSingleton(errorHandler); _services.AddSingleton(translationHandler); @@ -207,7 +208,7 @@ namespace Geekbot.net { _logger.Information(LogSource.Api, "Starting Webserver"); var highscoreManager = new HighscoreManager(_databaseInitializer.Initialize(), _userRepository); - WebApi.WebApiStartup.StartWebApi(_logger, _runParameters, _commands, _databaseInitializer.Initialize(), _client, _globalSettings, highscoreManager); + WebApiStartup.StartWebApi(_logger, _runParameters, _commands, _databaseInitializer.Initialize(), _client, _globalSettings, highscoreManager); return Task.CompletedTask; } } diff --git a/Geekbot.net/WebApi/Controllers/Commands/CommandDto.cs b/Geekbot.net/WebApi/Controllers/Commands/CommandDto.cs index 68c4e74..981d0f2 100644 --- a/Geekbot.net/WebApi/Controllers/Commands/CommandDto.cs +++ b/Geekbot.net/WebApi/Controllers/Commands/CommandDto.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; namespace Geekbot.net.WebApi.Controllers.Commands { diff --git a/Geekbot.net/WebApi/WebApiStartup.cs b/Geekbot.net/WebApi/WebApiStartup.cs index 2459362..7d3adc9 100644 --- a/Geekbot.net/WebApi/WebApiStartup.cs +++ b/Geekbot.net/WebApi/WebApiStartup.cs @@ -29,11 +29,11 @@ namespace Geekbot.net.WebApi .ConfigureServices(services => { services.AddMvc(); - services.AddSingleton(commandService); - services.AddSingleton(databaseContext); - services.AddSingleton(client); - services.AddSingleton(globalSettings); - services.AddSingleton(highscoreManager); + services.AddSingleton(commandService); + services.AddSingleton(databaseContext); + services.AddSingleton(client); + services.AddSingleton(globalSettings); + services.AddSingleton(highscoreManager); services.AddCors(options => { options.AddPolicy("AllowSpecificOrigin", From 3568b61f38412abc4080474fcf2b6a6179c472be Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 8 Feb 2020 15:58:17 +0100 Subject: [PATCH 199/443] Use the new Csharp 8 features (pattern matching and using assignments) and cleanup some insignificant resparper complaints --- Geekbot.net/Commands/Admin/Mod.cs | 4 +- .../Integrations/MagicTheGathering.cs | 30 ++++---- .../UbranDictionary/UrbanDictionary.cs | 68 +++++++++---------- Geekbot.net/Commands/Randomness/Cat/Cat.cs | 31 +++++---- Geekbot.net/Commands/Randomness/CheckEm.cs | 2 +- .../Randomness/Chuck/ChuckNorrisJokes.cs | 28 ++++---- .../Commands/Randomness/Dad/DadJokes.cs | 28 ++++---- Geekbot.net/Commands/Randomness/Dog/Dog.cs | 30 ++++---- Geekbot.net/Commands/Randomness/Gdq.cs | 10 ++- .../Commands/Randomness/Kanye/Kanye.cs | 28 ++++---- Geekbot.net/Commands/Randomness/Ship.cs | 10 +-- Geekbot.net/Commands/User/Karma.cs | 4 +- Geekbot.net/Commands/User/Ranking/Rank.cs | 4 +- Geekbot.net/Commands/User/Stats.cs | 2 +- .../Commands/Utils/Changelog/Changelog.cs | 52 +++++++------- Geekbot.net/Commands/Utils/Quote/Quote.cs | 4 +- .../LoggingAdapter/NpgsqlLoggingAdapter.cs | 25 +++---- Geekbot.net/Lib/Clients/MalClient.cs | 2 +- .../DisableInDirectMessageAttribute.cs | 2 +- .../Lib/Converters/MtgManaConverter.cs | 2 +- .../Lib/Highscores/HighscoreManager.cs | 31 +++------ Geekbot.net/Lib/Levels/LevelCalc.cs | 11 +-- .../Lib/Logger/SimpleConextConverter.cs | 2 +- .../Lib/ReactionListener/ReactionListener.cs | 7 +- .../Callback/CallbackController.cs | 18 ++--- .../HighscoreControllerPostBodyDto.cs | 4 +- Geekbot.net/WebApi/Logging/AspLogger.cs | 28 +++----- 27 files changed, 217 insertions(+), 250 deletions(-) diff --git a/Geekbot.net/Commands/Admin/Mod.cs b/Geekbot.net/Commands/Admin/Mod.cs index 1188504..f4beb54 100644 --- a/Geekbot.net/Commands/Admin/Mod.cs +++ b/Geekbot.net/Commands/Admin/Mod.cs @@ -35,7 +35,7 @@ namespace Geekbot.net.Commands.Admin try { var userRepo = _userRepository.Get(user.Id); - if (userRepo != null && userRepo.UsedNames != null) + if (userRepo?.UsedNames != null) { var sb = new StringBuilder(); sb.AppendLine($":bust_in_silhouette: {user.Username} has been known as:"); @@ -50,7 +50,7 @@ namespace Geekbot.net.Commands.Admin catch (Exception e) { await _errorHandler.HandleCommandException(e, Context, - $"I don't have enough permissions do that"); + "I don't have enough permissions do that"); } } } diff --git a/Geekbot.net/Commands/Integrations/MagicTheGathering.cs b/Geekbot.net/Commands/Integrations/MagicTheGathering.cs index ef16fee..1075c46 100644 --- a/Geekbot.net/Commands/Integrations/MagicTheGathering.cs +++ b/Geekbot.net/Commands/Integrations/MagicTheGathering.cs @@ -41,9 +41,11 @@ namespace Geekbot.net.Commands.Integrations return; } - var eb = new EmbedBuilder(); - eb.Title = card.Name; - eb.Description = card.Type; + var eb = new EmbedBuilder + { + Title = card.Name, + Description = card.Type + }; if (card.Colors != null) eb.WithColor(GetColor(card.Colors)); @@ -74,21 +76,15 @@ namespace Geekbot.net.Commands.Integrations private Color GetColor(IEnumerable colors) { var color = colors.FirstOrDefault(); - switch (color) + return color switch { - case "Black": - return new Color(203, 194, 191); - case "White": - return new Color(255, 251, 213); - case "Blue": - return new Color(170, 224, 250); - case "Red": - return new Color(250, 170, 143); - case "Green": - return new Color(155, 211, 174); - default: - return new Color(204, 194, 212); - } + "Black" => new Color(203, 194, 191), + "White" => new Color(255, 251, 213), + "Blue" => new Color(170, 224, 250), + "Red" => new Color(250, 170, 143), + "Green" => new Color(155, 211, 174), + _ => new Color(204, 194, 212) + }; } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs b/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs index 254c2bb..1f33573 100644 --- a/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs +++ b/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs @@ -25,43 +25,43 @@ namespace Geekbot.net.Commands.Integrations.UbranDictionary { try { - using (var client = new HttpClient()) + using var client = new HttpClient { - client.BaseAddress = new Uri("https://api.urbandictionary.com"); - var response = await client.GetAsync($"/v0/define?term={word}"); - response.EnsureSuccessStatusCode(); + BaseAddress = new Uri("https://api.urbandictionary.com") + }; + var response = await client.GetAsync($"/v0/define?term={word}"); + response.EnsureSuccessStatusCode(); - var stringResponse = await response.Content.ReadAsStringAsync(); - var definitions = JsonConvert.DeserializeObject(stringResponse); - if (definitions.List.Count == 0) - { - await ReplyAsync("That word hasn't been defined..."); - return; - } - - var definition = definitions.List.First(e => !string.IsNullOrWhiteSpace(e.Example)); - - var description = definition.Definition; - if (description.Length > 1801) - { - description = description.Substring(0, 1800) + " [...]"; - } - - var eb = new EmbedBuilder(); - eb.WithAuthor(new EmbedAuthorBuilder - { - Name = definition.Word, - Url = definition.Permalink - }); - eb.WithColor(new Color(239, 255, 0)); - if (!string.IsNullOrEmpty(definition.Definition)) eb.Description = description; - if (!string.IsNullOrEmpty(definition.Example)) eb.AddField("Example", definition.Example ?? "(no example given...)"); - if (!string.IsNullOrEmpty(definition.ThumbsUp)) eb.AddInlineField("Upvotes", definition.ThumbsUp); - if (!string.IsNullOrEmpty(definition.ThumbsDown)) eb.AddInlineField("Downvotes", definition.ThumbsDown); - if (definitions.Tags?.Length > 0) eb.AddField("Tags", string.Join(", ", definitions.Tags)); - - await ReplyAsync("", false, eb.Build()); + var stringResponse = await response.Content.ReadAsStringAsync(); + var definitions = JsonConvert.DeserializeObject(stringResponse); + if (definitions.List.Count == 0) + { + await ReplyAsync("That word hasn't been defined..."); + return; } + + var definition = definitions.List.First(e => !string.IsNullOrWhiteSpace(e.Example)); + + var description = definition.Definition; + if (description.Length > 1801) + { + description = description.Substring(0, 1800) + " [...]"; + } + + var eb = new EmbedBuilder(); + eb.WithAuthor(new EmbedAuthorBuilder + { + Name = definition.Word, + Url = definition.Permalink + }); + eb.WithColor(new Color(239, 255, 0)); + if (!string.IsNullOrEmpty(definition.Definition)) eb.Description = description; + if (!string.IsNullOrEmpty(definition.Example)) eb.AddField("Example", definition.Example ?? "(no example given...)"); + if (!string.IsNullOrEmpty(definition.ThumbsUp)) eb.AddInlineField("Upvotes", definition.ThumbsUp); + if (!string.IsNullOrEmpty(definition.ThumbsDown)) eb.AddInlineField("Downvotes", definition.ThumbsDown); + if (definitions.Tags?.Length > 0) eb.AddField("Tags", string.Join(", ", definitions.Tags)); + + await ReplyAsync("", false, eb.Build()); } catch (Exception e) { diff --git a/Geekbot.net/Commands/Randomness/Cat/Cat.cs b/Geekbot.net/Commands/Randomness/Cat/Cat.cs index 4d43a4c..3ac0764 100644 --- a/Geekbot.net/Commands/Randomness/Cat/Cat.cs +++ b/Geekbot.net/Commands/Randomness/Cat/Cat.cs @@ -23,24 +23,27 @@ namespace Geekbot.net.Commands.Randomness.Cat { try { - using (var client = new HttpClient()) + + try { - try + using var client = new HttpClient { - client.BaseAddress = new Uri("https://aws.random.cat"); - var response = await client.GetAsync("/meow"); - response.EnsureSuccessStatusCode(); + BaseAddress = new Uri("https://aws.random.cat") + }; + var response = await client.GetAsync("/meow"); + response.EnsureSuccessStatusCode(); - var stringResponse = await response.Content.ReadAsStringAsync(); - var catFile = JsonConvert.DeserializeObject(stringResponse); - var eb = new EmbedBuilder(); - eb.ImageUrl = catFile.File; - await ReplyAsync("", false, eb.Build()); - } - catch + var stringResponse = await response.Content.ReadAsStringAsync(); + var catFile = JsonConvert.DeserializeObject(stringResponse); + var eb = new EmbedBuilder { - await ReplyAsync("Seems like the dog cought the cat (error occured)"); - } + ImageUrl = catFile.File + }; + await ReplyAsync("", false, eb.Build()); + } + catch + { + await ReplyAsync("Seems like the dog cought the cat (error occured)"); } } catch (Exception e) diff --git a/Geekbot.net/Commands/Randomness/CheckEm.cs b/Geekbot.net/Commands/Randomness/CheckEm.cs index f8191ad..0596872 100644 --- a/Geekbot.net/Commands/Randomness/CheckEm.cs +++ b/Geekbot.net/Commands/Randomness/CheckEm.cs @@ -61,7 +61,7 @@ namespace Geekbot.net.Commands.Randomness while (num > 0) { listOfInts.Add(num % 10); - num = num / 10; + num /= 10; } listOfInts.Reverse(); diff --git a/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs b/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs index 07ea41f..5c28a9a 100644 --- a/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs +++ b/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs @@ -23,23 +23,21 @@ namespace Geekbot.net.Commands.Randomness.Chuck { try { - using (var client = new HttpClient()) + try { - try - { - client.DefaultRequestHeaders.Accept.Clear(); - client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json")); - var response = await client.GetAsync("https://api.chucknorris.io/jokes/random"); - response.EnsureSuccessStatusCode(); + using var client = new HttpClient(); + client.DefaultRequestHeaders.Accept.Clear(); + client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json")); + var response = await client.GetAsync("https://api.chucknorris.io/jokes/random"); + response.EnsureSuccessStatusCode(); - var stringResponse = await response.Content.ReadAsStringAsync(); - var data = JsonConvert.DeserializeObject(stringResponse); - await ReplyAsync(data.Value); - } - catch (HttpRequestException) - { - await ReplyAsync("Api down..."); - } + var stringResponse = await response.Content.ReadAsStringAsync(); + var data = JsonConvert.DeserializeObject(stringResponse); + await ReplyAsync(data.Value); + } + catch (HttpRequestException) + { + await ReplyAsync("Api down..."); } } catch (Exception e) diff --git a/Geekbot.net/Commands/Randomness/Dad/DadJokes.cs b/Geekbot.net/Commands/Randomness/Dad/DadJokes.cs index 2c51781..bcedd96 100644 --- a/Geekbot.net/Commands/Randomness/Dad/DadJokes.cs +++ b/Geekbot.net/Commands/Randomness/Dad/DadJokes.cs @@ -23,23 +23,21 @@ namespace Geekbot.net.Commands.Randomness.Dad { try { - using (var client = new HttpClient()) + try { - try - { - client.DefaultRequestHeaders.Accept.Clear(); - client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json")); - var response = await client.GetAsync("https://icanhazdadjoke.com/"); - response.EnsureSuccessStatusCode(); + using var client = new HttpClient(); + client.DefaultRequestHeaders.Accept.Clear(); + client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json")); + var response = await client.GetAsync("https://icanhazdadjoke.com/"); + response.EnsureSuccessStatusCode(); - var stringResponse = await response.Content.ReadAsStringAsync(); - var data = JsonConvert.DeserializeObject(stringResponse); - await ReplyAsync(data.Joke); - } - catch (HttpRequestException) - { - await ReplyAsync("Api down..."); - } + var stringResponse = await response.Content.ReadAsStringAsync(); + var data = JsonConvert.DeserializeObject(stringResponse); + await ReplyAsync(data.Joke); + } + catch (HttpRequestException) + { + await ReplyAsync("Api down..."); } } catch (Exception e) diff --git a/Geekbot.net/Commands/Randomness/Dog/Dog.cs b/Geekbot.net/Commands/Randomness/Dog/Dog.cs index 176cbad..d7404c6 100644 --- a/Geekbot.net/Commands/Randomness/Dog/Dog.cs +++ b/Geekbot.net/Commands/Randomness/Dog/Dog.cs @@ -23,24 +23,26 @@ namespace Geekbot.net.Commands.Randomness.Dog { try { - using (var client = new HttpClient()) + try { - try + using var client = new HttpClient { - client.BaseAddress = new Uri("http://random.dog"); - var response = await client.GetAsync("/woof.json"); - response.EnsureSuccessStatusCode(); + BaseAddress = new Uri("http://random.dog") + }; + var response = await client.GetAsync("/woof.json"); + response.EnsureSuccessStatusCode(); - var stringResponse = await response.Content.ReadAsStringAsync(); - var dogFile = JsonConvert.DeserializeObject(stringResponse); - var eb = new EmbedBuilder(); - eb.ImageUrl = dogFile.Url; - await ReplyAsync("", false, eb.Build()); - } - catch (HttpRequestException e) + var stringResponse = await response.Content.ReadAsStringAsync(); + var dogFile = JsonConvert.DeserializeObject(stringResponse); + var eb = new EmbedBuilder { - await ReplyAsync($"Seems like the dog got lost (error occured)\r\n{e.Message}"); - } + ImageUrl = dogFile.Url + }; + await ReplyAsync("", false, eb.Build()); + } + catch (HttpRequestException e) + { + await ReplyAsync($"Seems like the dog got lost (error occured)\r\n{e.Message}"); } } catch (Exception e) diff --git a/Geekbot.net/Commands/Randomness/Gdq.cs b/Geekbot.net/Commands/Randomness/Gdq.cs index 2685b33..4c16bed 100644 --- a/Geekbot.net/Commands/Randomness/Gdq.cs +++ b/Geekbot.net/Commands/Randomness/Gdq.cs @@ -21,13 +21,11 @@ namespace Geekbot.net.Commands.Randomness { try { - using (var client = new WebClient()) - { - var url = new Uri("http://taskinoz.com/gdq/api/"); - var response = client.DownloadString(url); + using var client = new WebClient(); + var url = new Uri("http://taskinoz.com/gdq/api/"); + var response = client.DownloadString(url); - await ReplyAsync(response); - } + await ReplyAsync(response); } catch (Exception e) { diff --git a/Geekbot.net/Commands/Randomness/Kanye/Kanye.cs b/Geekbot.net/Commands/Randomness/Kanye/Kanye.cs index dcd3bdc..36f1d1a 100644 --- a/Geekbot.net/Commands/Randomness/Kanye/Kanye.cs +++ b/Geekbot.net/Commands/Randomness/Kanye/Kanye.cs @@ -23,23 +23,21 @@ namespace Geekbot.net.Commands.Randomness.Kanye { try { - using (var client = new HttpClient()) + try { - try - { - client.DefaultRequestHeaders.Accept.Clear(); - client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json")); - var response = await client.GetAsync("https://api.kanye.rest/"); - response.EnsureSuccessStatusCode(); + using var client = new HttpClient(); + client.DefaultRequestHeaders.Accept.Clear(); + client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json")); + var response = await client.GetAsync("https://api.kanye.rest/"); + response.EnsureSuccessStatusCode(); - var stringResponse = await response.Content.ReadAsStringAsync(); - var data = JsonConvert.DeserializeObject(stringResponse); - await ReplyAsync(data.Quote); - } - catch (HttpRequestException) - { - await ReplyAsync("Api down..."); - } + var stringResponse = await response.Content.ReadAsStringAsync(); + var data = JsonConvert.DeserializeObject(stringResponse); + await ReplyAsync(data.Quote); + } + catch (HttpRequestException) + { + await ReplyAsync("Api down..."); } } catch (Exception e) diff --git a/Geekbot.net/Commands/Randomness/Ship.cs b/Geekbot.net/Commands/Randomness/Ship.cs index 2571891..bf52a73 100644 --- a/Geekbot.net/Commands/Randomness/Ship.cs +++ b/Geekbot.net/Commands/Randomness/Ship.cs @@ -56,8 +56,8 @@ namespace Geekbot.net.Commands.Randomness } var reply = ":heartpulse: **Matchmaking** :heartpulse:\r\n"; - reply = reply + $":two_hearts: {user1.Mention} :heart: {user2.Mention} :two_hearts:\r\n"; - reply = reply + $"0% [{BlockCounter(shippingRate)}] 100% - {DeterminateSuccess(shippingRate)}"; + reply += $":two_hearts: {user1.Mention} :heart: {user2.Mention} :two_hearts:\r\n"; + reply += $"0% [{BlockCounter(shippingRate)}] 100% - {DeterminateSuccess(shippingRate)}"; await ReplyAsync(reply); } catch (Exception e) @@ -87,13 +87,13 @@ namespace Geekbot.net.Commands.Randomness for (var i = 1; i <= 10; i++) if (i <= amount) { - blocks = blocks + ":white_medium_small_square:"; + blocks += ":white_medium_small_square:"; if (i == amount) - blocks = blocks + $" {rate}% "; + blocks += $" {rate}% "; } else { - blocks = blocks + ":black_medium_small_square:"; + blocks += ":black_medium_small_square:"; } return blocks; diff --git a/Geekbot.net/Commands/User/Karma.cs b/Geekbot.net/Commands/User/Karma.cs index ed221ff..bf85c91 100644 --- a/Geekbot.net/Commands/User/Karma.cs +++ b/Geekbot.net/Commands/User/Karma.cs @@ -46,7 +46,7 @@ namespace Geekbot.net.Commands.User else { var target = await GetUser(user.Id); - target.Karma = target.Karma + 1; + target.Karma += 1; SetUser(target); actor.TimeOut = DateTimeOffset.Now; @@ -93,7 +93,7 @@ namespace Geekbot.net.Commands.User else { var target = await GetUser(user.Id); - target.Karma = target.Karma - 1; + target.Karma -= 1; SetUser(target); actor.TimeOut = DateTimeOffset.Now; diff --git a/Geekbot.net/Commands/User/Ranking/Rank.cs b/Geekbot.net/Commands/User/Ranking/Rank.cs index 0ad21d5..9021b4e 100644 --- a/Geekbot.net/Commands/User/Ranking/Rank.cs +++ b/Geekbot.net/Commands/User/Ranking/Rank.cs @@ -74,7 +74,7 @@ namespace Geekbot.net.Commands.User.Ranking return; } - int guildMessages = 0; + var guildMessages = 0; if (type == HighscoreTypes.messages) { guildMessages = _database.Messages @@ -99,7 +99,7 @@ namespace Geekbot.net.Commands.User.Ranking : $"**{user.Key.Id}**"); replyBuilder.Append(type == HighscoreTypes.messages - ? $" - {user.Value} {type} - {Math.Round((double) (100 * user.Value) / guildMessages, digits: 2)}%\n" + ? $" - {user.Value} {type} - {Math.Round((double) (100 * user.Value) / guildMessages, 2)}%\n" : $" - {user.Value} {type}\n"); highscorePlace++; diff --git a/Geekbot.net/Commands/User/Stats.cs b/Geekbot.net/Commands/User/Stats.cs index 6ddf7d4..05ea324 100644 --- a/Geekbot.net/Commands/User/Stats.cs +++ b/Geekbot.net/Commands/User/Stats.cs @@ -51,7 +51,7 @@ namespace Geekbot.net.Commands.User var level = _levelCalc.GetLevel(messages); - var percent = Math.Round((double) (100 * messages) / guildMessages, digits: 2); + var percent = Math.Round((double) (100 * messages) / guildMessages, 2); var cookies = _database.Cookies ?.FirstOrDefault(e => e.GuildId.Equals(Context.Guild.Id.AsLong()) && e.UserId.Equals(userInfo.Id.AsLong())) diff --git a/Geekbot.net/Commands/Utils/Changelog/Changelog.cs b/Geekbot.net/Commands/Utils/Changelog/Changelog.cs index adcc9a3..02981df 100644 --- a/Geekbot.net/Commands/Utils/Changelog/Changelog.cs +++ b/Geekbot.net/Commands/Utils/Changelog/Changelog.cs @@ -29,34 +29,34 @@ namespace Geekbot.net.Commands.Utils.Changelog { try { - using (var client = new HttpClient()) + using var client = new HttpClient { - client.BaseAddress = new Uri("https://api.github.com"); - client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", - "http://developer.github.com/v3/#user-agent-required"); - var response = await client.GetAsync("/repos/pizzaandcoffee/geekbot.net/commits"); - response.EnsureSuccessStatusCode(); + BaseAddress = new Uri("https://api.github.com") + }; + client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", + "http://developer.github.com/v3/#user-agent-required"); + var response = await client.GetAsync("/repos/pizzaandcoffee/geekbot.net/commits"); + response.EnsureSuccessStatusCode(); - var stringResponse = await response.Content.ReadAsStringAsync(); - var commits = JsonConvert.DeserializeObject>(stringResponse); - var eb = new EmbedBuilder(); - eb.WithColor(new Color(143, 165, 102)); - eb.WithAuthor(new EmbedAuthorBuilder - { - IconUrl = _client.CurrentUser.GetAvatarUrl(), - Name = "Latest Updates", - Url = "https://geekbot.pizzaandcoffee.rocks/updates" - }); - var sb = new StringBuilder(); - foreach (var commit in commits.Take(10)) - sb.AppendLine($"- {commit.Commit.Message} ({commit.Commit.Author.Date:yyyy-MM-dd})"); - eb.Description = sb.ToString(); - eb.WithFooter(new EmbedFooterBuilder - { - Text = $"List generated from github commits on {DateTime.Now:yyyy-MM-dd}" - }); - await ReplyAsync("", false, eb.Build()); - } + var stringResponse = await response.Content.ReadAsStringAsync(); + var commits = JsonConvert.DeserializeObject>(stringResponse); + var eb = new EmbedBuilder(); + eb.WithColor(new Color(143, 165, 102)); + eb.WithAuthor(new EmbedAuthorBuilder + { + IconUrl = _client.CurrentUser.GetAvatarUrl(), + Name = "Latest Updates", + Url = "https://geekbot.pizzaandcoffee.rocks/updates" + }); + var sb = new StringBuilder(); + foreach (var commit in commits.Take(10)) + sb.AppendLine($"- {commit.Commit.Message} ({commit.Commit.Author.Date:yyyy-MM-dd})"); + eb.Description = sb.ToString(); + eb.WithFooter(new EmbedFooterBuilder + { + Text = $"List generated from github commits on {DateTime.Now:yyyy-MM-dd}" + }); + await ReplyAsync("", false, eb.Build()); } catch (Exception e) { diff --git a/Geekbot.net/Commands/Utils/Quote/Quote.cs b/Geekbot.net/Commands/Utils/Quote/Quote.cs index f06c6ab..e9c4706 100644 --- a/Geekbot.net/Commands/Utils/Quote/Quote.cs +++ b/Geekbot.net/Commands/Utils/Quote/Quote.cs @@ -46,7 +46,7 @@ namespace Geekbot.net.Commands.Utils.Quote return; } - var random = _randomNumberGenerator.Next(0, s.Count()); + var random = _randomNumberGenerator.Next(0, s.Count); var quote = s[random]; var embed = QuoteBuilder(quote); @@ -238,7 +238,7 @@ namespace Geekbot.net.Commands.Utils.Quote var last = _database.Quotes.Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong())) .OrderByDescending(e => e.InternalId).FirstOrDefault(); - int internalId = 1; + var internalId = 1; if (last != null) internalId = last.InternalId + 1; return new QuoteModel() { diff --git a/Geekbot.net/Database/LoggingAdapter/NpgsqlLoggingAdapter.cs b/Geekbot.net/Database/LoggingAdapter/NpgsqlLoggingAdapter.cs index e8850cd..ceebd56 100644 --- a/Geekbot.net/Database/LoggingAdapter/NpgsqlLoggingAdapter.cs +++ b/Geekbot.net/Database/LoggingAdapter/NpgsqlLoggingAdapter.cs @@ -54,23 +54,16 @@ namespace Geekbot.net.Database.LoggingAdapter private static LogLevel ToGeekbotLogLevel(NpgsqlLogLevel level) { - switch (level) + return level switch { - case NpgsqlLogLevel.Trace: - return LogLevel.Trace; - case NpgsqlLogLevel.Debug: - return LogLevel.Debug; - case NpgsqlLogLevel.Info: - return LogLevel.Info; - case NpgsqlLogLevel.Warn: - return LogLevel.Warn; - case NpgsqlLogLevel.Error: - return LogLevel.Error; - case NpgsqlLogLevel.Fatal: - return LogLevel.Fatal; - default: - throw new ArgumentOutOfRangeException(nameof(level)); - } + NpgsqlLogLevel.Trace => LogLevel.Trace, + NpgsqlLogLevel.Debug => LogLevel.Debug, + NpgsqlLogLevel.Info => LogLevel.Info, + NpgsqlLogLevel.Warn => LogLevel.Warn, + NpgsqlLogLevel.Error => LogLevel.Error, + NpgsqlLogLevel.Fatal => LogLevel.Fatal, + _ => throw new ArgumentOutOfRangeException(nameof(level)) + }; } } } \ No newline at end of file diff --git a/Geekbot.net/Lib/Clients/MalClient.cs b/Geekbot.net/Lib/Clients/MalClient.cs index a014cf1..8e5d950 100644 --- a/Geekbot.net/Lib/Clients/MalClient.cs +++ b/Geekbot.net/Lib/Clients/MalClient.cs @@ -22,7 +22,7 @@ namespace Geekbot.net.Lib.Clients ReloadClient(); } - public bool ReloadClient() + private bool ReloadClient() { var malCredentials = _globalSettings.GetKey("MalCredentials"); if (!string.IsNullOrEmpty(malCredentials)) diff --git a/Geekbot.net/Lib/CommandPreconditions/DisableInDirectMessageAttribute.cs b/Geekbot.net/Lib/CommandPreconditions/DisableInDirectMessageAttribute.cs index 110b69b..2bb5cfd 100644 --- a/Geekbot.net/Lib/CommandPreconditions/DisableInDirectMessageAttribute.cs +++ b/Geekbot.net/Lib/CommandPreconditions/DisableInDirectMessageAttribute.cs @@ -4,7 +4,7 @@ using Discord.Commands; namespace Geekbot.net.Lib.CommandPreconditions { - [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)] + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)] public class DisableInDirectMessageAttribute : PreconditionAttribute { public override Task CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services) diff --git a/Geekbot.net/Lib/Converters/MtgManaConverter.cs b/Geekbot.net/Lib/Converters/MtgManaConverter.cs index c29e1e0..31a2db0 100644 --- a/Geekbot.net/Lib/Converters/MtgManaConverter.cs +++ b/Geekbot.net/Lib/Converters/MtgManaConverter.cs @@ -7,7 +7,7 @@ namespace Geekbot.net.Lib.Converters { public class MtgManaConverter : IMtgManaConverter { - private Dictionary _manaDict; + private readonly Dictionary _manaDict; public MtgManaConverter() { diff --git a/Geekbot.net/Lib/Highscores/HighscoreManager.cs b/Geekbot.net/Lib/Highscores/HighscoreManager.cs index e668ff2..8218532 100644 --- a/Geekbot.net/Lib/Highscores/HighscoreManager.cs +++ b/Geekbot.net/Lib/Highscores/HighscoreManager.cs @@ -20,26 +20,15 @@ namespace Geekbot.net.Lib.Highscores public Dictionary GetHighscoresWithUserData(HighscoreTypes type, ulong guildId, int amount) { - Dictionary list; - switch (type) + var list = type switch { - case HighscoreTypes.messages: - list = GetMessageList(guildId, amount); - break; - case HighscoreTypes.karma: - list = GetKarmaList(guildId, amount); - break; - case HighscoreTypes.rolls: - list = GetRollsList(guildId, amount); - break; - case HighscoreTypes.cookies: - list = GetCookiesList(guildId, amount); - break; - default: - list = new Dictionary(); - break; - } - + HighscoreTypes.messages => GetMessageList(guildId, amount), + HighscoreTypes.karma => GetKarmaList(guildId, amount), + HighscoreTypes.rolls => GetRollsList(guildId, amount), + HighscoreTypes.cookies => GetCookiesList(guildId, amount), + _ => new Dictionary() + }; + if (!list.Any()) { throw new HighscoreListEmptyException($"No {type} found for guild {guildId}"); @@ -103,8 +92,8 @@ namespace Geekbot.net.Lib.Highscores .Take(amount) .ToDictionary(key => key.UserId.AsUlong(), key => key.Rolls); } - - public Dictionary GetCookiesList(ulong guildId, int amount) + + private Dictionary GetCookiesList(ulong guildId, int amount) { return _database.Cookies .Where(k => k.GuildId.Equals(guildId.AsLong())) diff --git a/Geekbot.net/Lib/Levels/LevelCalc.cs b/Geekbot.net/Lib/Levels/LevelCalc.cs index f5b6b80..74747b7 100644 --- a/Geekbot.net/Lib/Levels/LevelCalc.cs +++ b/Geekbot.net/Lib/Levels/LevelCalc.cs @@ -1,11 +1,12 @@ using System; using System.Collections.Generic; +using System.Linq; namespace Geekbot.net.Lib.Levels { public class LevelCalc : ILevelCalc { - private int[] _levels; + private readonly int[] _levels; public LevelCalc() { @@ -21,13 +22,7 @@ namespace Geekbot.net.Lib.Levels public int GetLevel(int? messages) { - var returnVal = 1; - foreach (var level in _levels) - { - if (level > messages) break; - returnVal++; - } - return returnVal; + return 1 + _levels.TakeWhile(level => !(level > messages)).Count(); } } } \ No newline at end of file diff --git a/Geekbot.net/Lib/Logger/SimpleConextConverter.cs b/Geekbot.net/Lib/Logger/SimpleConextConverter.cs index 8b8b0ed..d01da67 100644 --- a/Geekbot.net/Lib/Logger/SimpleConextConverter.cs +++ b/Geekbot.net/Lib/Logger/SimpleConextConverter.cs @@ -37,7 +37,7 @@ namespace Geekbot.net.Lib.Logger } public static MessageDto ConvertSocketMessage(SocketMessage message, bool isPrivate = false) { - SocketGuildChannel channel = isPrivate ? null : (SocketGuildChannel) message.Channel; + var channel = isPrivate ? null : (SocketGuildChannel) message.Channel; return new MessageDto { Message = new MessageDto.MessageContent diff --git a/Geekbot.net/Lib/ReactionListener/ReactionListener.cs b/Geekbot.net/Lib/ReactionListener/ReactionListener.cs index 26a60d3..2349028 100644 --- a/Geekbot.net/Lib/ReactionListener/ReactionListener.cs +++ b/Geekbot.net/Lib/ReactionListener/ReactionListener.cs @@ -63,8 +63,11 @@ namespace Geekbot.net.Lib.ReactionListener _listener[messageId].Add(emoji, role.Id); return Task.CompletedTask; } - var dict = new Dictionary(); - dict.Add(emoji, role.Id); + + var dict = new Dictionary + { + {emoji, role.Id} + }; _listener.Add(messageId, dict); return Task.CompletedTask; } diff --git a/Geekbot.net/WebApi/Controllers/Callback/CallbackController.cs b/Geekbot.net/WebApi/Controllers/Callback/CallbackController.cs index 58bdb83..78fb38d 100644 --- a/Geekbot.net/WebApi/Controllers/Callback/CallbackController.cs +++ b/Geekbot.net/WebApi/Controllers/Callback/CallbackController.cs @@ -32,14 +32,16 @@ namespace Geekbot.net.WebApi.Controllers.Callback var accessToken = _globalSettings.GetKey("OAuthToken"); var callbackUrl = _globalSettings.GetKey("OAuthCallbackUrl"); - var form = new Dictionary(); - form.Add("client_id", appInfo.Id.ToString()); - form.Add("client_secret", accessToken); - form.Add("grant_type", "authorization_code"); - form.Add("code", code); - form.Add("scope", "identify email guilds"); - form.Add("redirect_uri", callbackUrl); - + var form = new Dictionary + { + {"client_id", appInfo.Id.ToString()}, + {"client_secret", accessToken}, + {"grant_type", "authorization_code"}, + {"code", code}, + {"scope", "identify email guilds"}, + {"redirect_uri", callbackUrl} + }; + client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded")); var result = await client.PostAsync("/api/oauth2/token", new FormUrlEncodedContent(form)); result.EnsureSuccessStatusCode(); diff --git a/Geekbot.net/WebApi/Controllers/Highscores/HighscoreControllerPostBodyDto.cs b/Geekbot.net/WebApi/Controllers/Highscores/HighscoreControllerPostBodyDto.cs index 22da3c7..6e7aec3 100644 --- a/Geekbot.net/WebApi/Controllers/Highscores/HighscoreControllerPostBodyDto.cs +++ b/Geekbot.net/WebApi/Controllers/Highscores/HighscoreControllerPostBodyDto.cs @@ -8,9 +8,9 @@ namespace Geekbot.net.WebApi.Controllers.Highscores [Required] public ulong GuildId { get; set; } - public HighscoreTypes Type { get; set; } = HighscoreTypes.messages; + public HighscoreTypes Type { get; } = HighscoreTypes.messages; [Range(1, 150)] - public int Amount { get; set; } = 50; + public int Amount { get; } = 50; } } \ No newline at end of file diff --git a/Geekbot.net/WebApi/Logging/AspLogger.cs b/Geekbot.net/WebApi/Logging/AspLogger.cs index 3316150..5899b26 100644 --- a/Geekbot.net/WebApi/Logging/AspLogger.cs +++ b/Geekbot.net/WebApi/Logging/AspLogger.cs @@ -55,25 +55,17 @@ namespace Geekbot.net.WebApi.Logging private static NLog.LogLevel ToGeekbotLogLevel(LogLevel level) { - switch (level) + return level switch { - case LogLevel.Trace: - return NLog.LogLevel.Trace; - case LogLevel.Debug: - return NLog.LogLevel.Debug; - case LogLevel.Information: - return NLog.LogLevel.Info; - case LogLevel.Warning: - return NLog.LogLevel.Warn; - case LogLevel.Error: - return NLog.LogLevel.Error; - case LogLevel.Critical: - return NLog.LogLevel.Fatal; - case LogLevel.None: - return NLog.LogLevel.Off; - default: - throw new ArgumentOutOfRangeException(nameof(level)); - } + LogLevel.Trace => NLog.LogLevel.Trace, + LogLevel.Debug => NLog.LogLevel.Debug, + LogLevel.Information => NLog.LogLevel.Info, + LogLevel.Warning => NLog.LogLevel.Warn, + LogLevel.Error => NLog.LogLevel.Error, + LogLevel.Critical => NLog.LogLevel.Fatal, + LogLevel.None => NLog.LogLevel.Off, + _ => throw new ArgumentOutOfRangeException(nameof(level)) + }; } } } \ No newline at end of file From 3bd7274d68c633599b3fbcbd4eff10f9c1f466aa Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 4 Apr 2020 21:21:15 +0200 Subject: [PATCH 200/443] Remove some unnecessary text from the dice command --- Geekbot.net/Commands/Utils/Dice/Dice.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Geekbot.net/Commands/Utils/Dice/Dice.cs b/Geekbot.net/Commands/Utils/Dice/Dice.cs index bc0244e..c335982 100644 --- a/Geekbot.net/Commands/Utils/Dice/Dice.cs +++ b/Geekbot.net/Commands/Utils/Dice/Dice.cs @@ -67,7 +67,6 @@ namespace Geekbot.net.Commands.Utils.Dice rep.Append("**Result:** "); var resultStrings = new List(); var total = 0; - var extraText = ""; foreach (var dice in dices) { var results = new List(); @@ -76,8 +75,6 @@ namespace Geekbot.net.Commands.Utils.Dice var roll = _randomNumberGenerator.Next(1, dice.Sides); total += roll; results.Add(roll); - if (roll == dice.Sides) extraText = "**Critical Hit!**"; - if (roll == 1) extraText = "**Critical Fail!**"; } resultStrings.Add($"{dice.DiceType} ({string.Join(",", results)})"); @@ -92,7 +89,6 @@ namespace Geekbot.net.Commands.Utils.Dice rep.AppendLine(); rep.AppendLine($"**Total:** {total}"); - if (extraText != "") rep.AppendLine(extraText); await ReplyAsync(rep.ToString()); } From f12bdcf4cd1d1794a22b7997d3ef7416092f66e8 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 4 Apr 2020 23:05:18 +0200 Subject: [PATCH 201/443] Translate the ship command --- Geekbot.net/Commands/Randomness/Ship.cs | 33 +++++++++++-------- Geekbot.net/Lib/Localization/Translations.yml | 22 ++++++++++++- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/Geekbot.net/Commands/Randomness/Ship.cs b/Geekbot.net/Commands/Randomness/Ship.cs index bf52a73..d5386d4 100644 --- a/Geekbot.net/Commands/Randomness/Ship.cs +++ b/Geekbot.net/Commands/Randomness/Ship.cs @@ -7,6 +7,7 @@ using Geekbot.net.Database; using Geekbot.net.Database.Models; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; +using Geekbot.net.Lib.Localization; using Geekbot.net.Lib.RandomNumberGenerator; namespace Geekbot.net.Commands.Randomness @@ -15,13 +16,15 @@ namespace Geekbot.net.Commands.Randomness { private readonly IErrorHandler _errorHandler; private readonly IRandomNumberGenerator _randomNumberGenerator; + private readonly ITranslationHandler _translation; private readonly DatabaseContext _database; - public Ship(DatabaseContext database, IErrorHandler errorHandler, IRandomNumberGenerator randomNumberGenerator) + public Ship(DatabaseContext database, IErrorHandler errorHandler, IRandomNumberGenerator randomNumberGenerator, ITranslationHandler translation) { _database = database; _errorHandler = errorHandler; _randomNumberGenerator = randomNumberGenerator; + _translation = translation; } [Command("Ship", RunMode = RunMode.Async)] @@ -55,9 +58,11 @@ namespace Geekbot.net.Commands.Randomness shippingRate = dbval.Strength; } - var reply = ":heartpulse: **Matchmaking** :heartpulse:\r\n"; + var transContext = await _translation.GetGuildContext(Context); + + var reply = $":heartpulse: **{transContext.GetString("Matchmaking")}** :heartpulse:\r\n"; reply += $":two_hearts: {user1.Mention} :heart: {user2.Mention} :two_hearts:\r\n"; - reply += $"0% [{BlockCounter(shippingRate)}] 100% - {DeterminateSuccess(shippingRate)}"; + reply += $"0% [{BlockCounter(shippingRate)}] 100% - {DeterminateSuccess(shippingRate, transContext)}"; await ReplyAsync(reply); } catch (Exception e) @@ -66,22 +71,22 @@ namespace Geekbot.net.Commands.Randomness } } - private string DeterminateSuccess(int rate) + private string DeterminateSuccess(int rate, TranslationGuildContext transContext) { - if (rate < 20) - return "Not gonna happen"; - if (rate >= 20 && rate < 40) - return "Not such a good idea"; - if (rate >= 40 && rate < 60) - return "There might be a chance"; - if (rate >= 60 && rate < 80) - return "Almost a match, but could work"; - return rate >= 80 ? "It's a match" : "a"; + return (rate / 20) switch + { + 0 => transContext.GetString("NotGonnaToHappen"), + 1 => transContext.GetString("NotSuchAGoodIdea"), + 2 => transContext.GetString("ThereMightBeAChance"), + 3 => transContext.GetString("CouldWork"), + 4 => transContext.GetString("ItsAMatch"), + _ => "nope" + }; } private string BlockCounter(int rate) { - var amount = Math.Floor(decimal.Floor(rate / 10)); + var amount = rate / 10; Console.WriteLine(amount); var blocks = ""; for (var i = 1; i <= 10; i++) diff --git a/Geekbot.net/Lib/Localization/Translations.yml b/Geekbot.net/Lib/Localization/Translations.yml index 730ea40..5744741 100644 --- a/Geekbot.net/Lib/Localization/Translations.yml +++ b/Geekbot.net/Lib/Localization/Translations.yml @@ -175,4 +175,24 @@ rank: CHDE: ":warning: Ich han nid alli benutzername gfunde. villiicht hend sie de server verlah?\n" HighscoresFor: EN: ":bar_chart: **{0} Highscore for {1}**" - CHDE: ":bar_chart: **{0} Highscore für {1}**" \ No newline at end of file + CHDE: ":bar_chart: **{0} Highscore für {1}**" +ship: + Matchmaking: + EN: "Matchmaking" + CHDE: "Verkupple" + NotGonnaToHappen: + EN: "Not gonna happen" + CHDE: "Wird nöd klappe" + NotSuchAGoodIdea: + EN: "Not such a good idea" + CHDE: "Nöd so ä gueti idee" + ThereMightBeAChance: + EN: "There might be a chance" + CHDE: "Es gid eventuel ä chance" + CouldWork: + EN: "Almost a match" + CHDE: "Fasch en match" + ItsAMatch: + EN: "It's a match" + CHDE: "Es isch es traumpaar" + \ No newline at end of file From 77b3d612f2da6dbbcf8fca008deef0c307962583 Mon Sep 17 00:00:00 2001 From: runebaas Date: Mon, 6 Apr 2020 15:35:28 +0200 Subject: [PATCH 202/443] Re-use the database connection everywhere --- Geekbot.net/Handlers.cs | 15 +++++---------- Geekbot.net/Program.cs | 23 ++++++++++++----------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index 13f523c..48bf966 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -9,7 +9,6 @@ using Discord.Rest; using Discord.WebSocket; using Geekbot.net.Database; using Geekbot.net.Database.Models; -using Geekbot.net.Lib.AlmostRedis; using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Logger; using Geekbot.net.Lib.ReactionListener; @@ -23,24 +22,20 @@ namespace Geekbot.net private readonly DatabaseContext _database; private readonly IDiscordClient _client; private readonly IGeekbotLogger _logger; - private readonly IAlmostRedis _redis; private readonly IServiceProvider _servicesProvider; private readonly CommandService _commands; private readonly IUserRepository _userRepository; private readonly IReactionListener _reactionListener; - private readonly DatabaseContext _messageCounterDatabaseContext; private readonly RestApplication _applicationInfo; private readonly List _ignoredServers; - public Handlers(DatabaseInitializer databaseInitializer, IDiscordClient client, IGeekbotLogger logger, IAlmostRedis redis, + public Handlers(DatabaseContext database, IDiscordClient client, IGeekbotLogger logger, IServiceProvider servicesProvider, CommandService commands, IUserRepository userRepository, IReactionListener reactionListener, RestApplication applicationInfo) { - _database = databaseInitializer.Initialize(); - _messageCounterDatabaseContext = databaseInitializer.Initialize(); + _database = database; _client = client; _logger = logger; - _redis = redis; _servicesProvider = servicesProvider; _commands = commands; _userRepository = userRepository; @@ -129,7 +124,7 @@ namespace Geekbot.net var channel = (SocketGuildChannel) message.Channel; - var rowId = await _messageCounterDatabaseContext.Database.ExecuteSqlRawAsync( + var rowId = await _database.Database.ExecuteSqlRawAsync( "UPDATE \"Messages\" SET \"MessageCount\" = \"MessageCount\" + 1 WHERE \"GuildId\" = {0} AND \"UserId\" = {1}", channel.Guild.Id.AsLong(), message.Author.Id.AsLong() @@ -137,13 +132,13 @@ namespace Geekbot.net if (rowId == 0) { - _messageCounterDatabaseContext.Messages.Add(new MessagesModel + await _database.Messages.AddAsync(new MessagesModel { UserId = message.Author.Id.AsLong(), GuildId = channel.Guild.Id.AsLong(), MessageCount = 1 }); - _messageCounterDatabaseContext.SaveChanges(); + await _database.SaveChangesAsync(); } if (message.Author.IsBot) return; diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 4907431..9ff6e90 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -41,6 +41,7 @@ namespace Geekbot.net private IUserRepository _userRepository; private RunParameters _runParameters; private IAlmostRedis _redis; + private DatabaseContext _database; private static void Main(string[] args) { @@ -87,11 +88,11 @@ namespace Geekbot.net _commands = new CommandService(); _databaseInitializer = new DatabaseInitializer(runParameters, logger); - var database = _databaseInitializer.Initialize(); - database.Database.EnsureCreated(); - if(!_runParameters.InMemory) database.Database.Migrate(); + _database = _databaseInitializer.Initialize(); + _database.Database.EnsureCreated(); + if(!_runParameters.InMemory) _database.Database.Migrate(); - _globalSettings = new GlobalSettings(database); + _globalSettings = new GlobalSettings(_database); try { @@ -116,7 +117,7 @@ namespace Geekbot.net _services = new ServiceCollection(); - _userRepository = new UserRepository(_databaseInitializer.Initialize(), logger); + _userRepository = new UserRepository(_database, logger); var fortunes = new FortunesProvider(logger); var mediaProvider = new MediaProvider(logger); var malClient = new MalClient(_globalSettings, logger); @@ -137,9 +138,9 @@ namespace Geekbot.net _services.AddSingleton(mtgManaConverter); _services.AddSingleton(wikipediaClient); _services.AddSingleton(randomNumberGenerator); + _services.AddSingleton(_database); _services.AddSingleton(_globalSettings); - _services.AddTransient(e => new HighscoreManager(_databaseInitializer.Initialize(), _userRepository)); - _services.AddTransient(e => _databaseInitializer.Initialize()); + _services.AddTransient(e => new HighscoreManager(_database, _userRepository)); logger.Information(LogSource.Geekbot, "Connecting to Discord"); @@ -162,7 +163,7 @@ namespace Geekbot.net _logger.Information(LogSource.Geekbot, $"Now Connected as {_client.CurrentUser.Username} to {_client.Guilds.Count} Servers"); _logger.Information(LogSource.Geekbot, "Registering Stuff"); - var translationHandler = new TranslationHandler(_databaseInitializer.Initialize(), _logger); + var translationHandler = new TranslationHandler(_database, _logger); var errorHandler = new ErrorHandler(_logger, translationHandler, _runParameters.ExposeErrors); var reactionListener = new ReactionListener(_redis.Db); _services.AddSingleton(errorHandler); @@ -172,7 +173,7 @@ namespace Geekbot.net _servicesProvider = _services.BuildServiceProvider(); await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _servicesProvider); - var handlers = new Handlers(_databaseInitializer, _client, _logger, _redis, _servicesProvider, _commands, _userRepository, reactionListener, applicationInfo); + var handlers = new Handlers(_database, _client, _logger, _servicesProvider, _commands, _userRepository, reactionListener, applicationInfo); _client.MessageReceived += handlers.RunCommand; _client.MessageDeleted += handlers.MessageDeleted; @@ -207,8 +208,8 @@ namespace Geekbot.net private Task StartWebApi() { _logger.Information(LogSource.Api, "Starting Webserver"); - var highscoreManager = new HighscoreManager(_databaseInitializer.Initialize(), _userRepository); - WebApiStartup.StartWebApi(_logger, _runParameters, _commands, _databaseInitializer.Initialize(), _client, _globalSettings, highscoreManager); + var highscoreManager = new HighscoreManager(_database, _userRepository); + WebApiStartup.StartWebApi(_logger, _runParameters, _commands, _database, _client, _globalSettings, highscoreManager); return Task.CompletedTask; } } From 2a616f8c5d315f253834802821fc0399f81cc618 Mon Sep 17 00:00:00 2001 From: runebaas Date: Mon, 6 Apr 2020 15:51:28 +0200 Subject: [PATCH 203/443] Revert "Re-use the database connection everywhere" This reverts commit 77b3d612f2da6dbbcf8fca008deef0c307962583. --- Geekbot.net/Handlers.cs | 15 ++++++++++----- Geekbot.net/Program.cs | 23 +++++++++++------------ 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index 48bf966..13f523c 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -9,6 +9,7 @@ using Discord.Rest; using Discord.WebSocket; using Geekbot.net.Database; using Geekbot.net.Database.Models; +using Geekbot.net.Lib.AlmostRedis; using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Logger; using Geekbot.net.Lib.ReactionListener; @@ -22,20 +23,24 @@ namespace Geekbot.net private readonly DatabaseContext _database; private readonly IDiscordClient _client; private readonly IGeekbotLogger _logger; + private readonly IAlmostRedis _redis; private readonly IServiceProvider _servicesProvider; private readonly CommandService _commands; private readonly IUserRepository _userRepository; private readonly IReactionListener _reactionListener; + private readonly DatabaseContext _messageCounterDatabaseContext; private readonly RestApplication _applicationInfo; private readonly List _ignoredServers; - public Handlers(DatabaseContext database, IDiscordClient client, IGeekbotLogger logger, + public Handlers(DatabaseInitializer databaseInitializer, IDiscordClient client, IGeekbotLogger logger, IAlmostRedis redis, IServiceProvider servicesProvider, CommandService commands, IUserRepository userRepository, IReactionListener reactionListener, RestApplication applicationInfo) { - _database = database; + _database = databaseInitializer.Initialize(); + _messageCounterDatabaseContext = databaseInitializer.Initialize(); _client = client; _logger = logger; + _redis = redis; _servicesProvider = servicesProvider; _commands = commands; _userRepository = userRepository; @@ -124,7 +129,7 @@ namespace Geekbot.net var channel = (SocketGuildChannel) message.Channel; - var rowId = await _database.Database.ExecuteSqlRawAsync( + var rowId = await _messageCounterDatabaseContext.Database.ExecuteSqlRawAsync( "UPDATE \"Messages\" SET \"MessageCount\" = \"MessageCount\" + 1 WHERE \"GuildId\" = {0} AND \"UserId\" = {1}", channel.Guild.Id.AsLong(), message.Author.Id.AsLong() @@ -132,13 +137,13 @@ namespace Geekbot.net if (rowId == 0) { - await _database.Messages.AddAsync(new MessagesModel + _messageCounterDatabaseContext.Messages.Add(new MessagesModel { UserId = message.Author.Id.AsLong(), GuildId = channel.Guild.Id.AsLong(), MessageCount = 1 }); - await _database.SaveChangesAsync(); + _messageCounterDatabaseContext.SaveChanges(); } if (message.Author.IsBot) return; diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 9ff6e90..4907431 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -41,7 +41,6 @@ namespace Geekbot.net private IUserRepository _userRepository; private RunParameters _runParameters; private IAlmostRedis _redis; - private DatabaseContext _database; private static void Main(string[] args) { @@ -88,11 +87,11 @@ namespace Geekbot.net _commands = new CommandService(); _databaseInitializer = new DatabaseInitializer(runParameters, logger); - _database = _databaseInitializer.Initialize(); - _database.Database.EnsureCreated(); - if(!_runParameters.InMemory) _database.Database.Migrate(); + var database = _databaseInitializer.Initialize(); + database.Database.EnsureCreated(); + if(!_runParameters.InMemory) database.Database.Migrate(); - _globalSettings = new GlobalSettings(_database); + _globalSettings = new GlobalSettings(database); try { @@ -117,7 +116,7 @@ namespace Geekbot.net _services = new ServiceCollection(); - _userRepository = new UserRepository(_database, logger); + _userRepository = new UserRepository(_databaseInitializer.Initialize(), logger); var fortunes = new FortunesProvider(logger); var mediaProvider = new MediaProvider(logger); var malClient = new MalClient(_globalSettings, logger); @@ -138,9 +137,9 @@ namespace Geekbot.net _services.AddSingleton(mtgManaConverter); _services.AddSingleton(wikipediaClient); _services.AddSingleton(randomNumberGenerator); - _services.AddSingleton(_database); _services.AddSingleton(_globalSettings); - _services.AddTransient(e => new HighscoreManager(_database, _userRepository)); + _services.AddTransient(e => new HighscoreManager(_databaseInitializer.Initialize(), _userRepository)); + _services.AddTransient(e => _databaseInitializer.Initialize()); logger.Information(LogSource.Geekbot, "Connecting to Discord"); @@ -163,7 +162,7 @@ namespace Geekbot.net _logger.Information(LogSource.Geekbot, $"Now Connected as {_client.CurrentUser.Username} to {_client.Guilds.Count} Servers"); _logger.Information(LogSource.Geekbot, "Registering Stuff"); - var translationHandler = new TranslationHandler(_database, _logger); + var translationHandler = new TranslationHandler(_databaseInitializer.Initialize(), _logger); var errorHandler = new ErrorHandler(_logger, translationHandler, _runParameters.ExposeErrors); var reactionListener = new ReactionListener(_redis.Db); _services.AddSingleton(errorHandler); @@ -173,7 +172,7 @@ namespace Geekbot.net _servicesProvider = _services.BuildServiceProvider(); await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _servicesProvider); - var handlers = new Handlers(_database, _client, _logger, _servicesProvider, _commands, _userRepository, reactionListener, applicationInfo); + var handlers = new Handlers(_databaseInitializer, _client, _logger, _redis, _servicesProvider, _commands, _userRepository, reactionListener, applicationInfo); _client.MessageReceived += handlers.RunCommand; _client.MessageDeleted += handlers.MessageDeleted; @@ -208,8 +207,8 @@ namespace Geekbot.net private Task StartWebApi() { _logger.Information(LogSource.Api, "Starting Webserver"); - var highscoreManager = new HighscoreManager(_database, _userRepository); - WebApiStartup.StartWebApi(_logger, _runParameters, _commands, _database, _client, _globalSettings, highscoreManager); + var highscoreManager = new HighscoreManager(_databaseInitializer.Initialize(), _userRepository); + WebApiStartup.StartWebApi(_logger, _runParameters, _commands, _databaseInitializer.Initialize(), _client, _globalSettings, highscoreManager); return Task.CompletedTask; } } From ee548390a560aa95842d2efd344a03d2d9d48fdb Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 17 Apr 2020 23:48:50 +0200 Subject: [PATCH 204/443] Add Prometheus with 1 metric --- Geekbot.net/Geekbot.net.csproj | 1 + Geekbot.net/Handlers.cs | 6 ++++++ Geekbot.net/Lib/RunParameters.cs | 9 +++++++++ Geekbot.net/Program.cs | 11 +++++++++++ 4 files changed, 27 insertions(+) diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index fe2f8cc..e50613a 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -44,6 +44,7 @@ + diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index 13f523c..dc1ce71 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -15,6 +15,7 @@ using Geekbot.net.Lib.Logger; using Geekbot.net.Lib.ReactionListener; using Geekbot.net.Lib.UserRepository; using Microsoft.EntityFrameworkCore; +using Prometheus; namespace Geekbot.net { @@ -32,6 +33,9 @@ namespace Geekbot.net private readonly RestApplication _applicationInfo; private readonly List _ignoredServers; + private readonly Counter _messageCounterPrometheus = + Metrics.CreateCounter("messages", "Number of discord messages", new CounterConfiguration() {LabelNames = new[] {"guild", "channel", "user"}}); + public Handlers(DatabaseInitializer databaseInitializer, IDiscordClient client, IGeekbotLogger logger, IAlmostRedis redis, IServiceProvider servicesProvider, CommandService commands, IUserRepository userRepository, IReactionListener reactionListener, RestApplication applicationInfo) @@ -146,6 +150,8 @@ namespace Geekbot.net _messageCounterDatabaseContext.SaveChanges(); } + _messageCounterPrometheus.WithLabels(channel.Guild.Id.ToString(), channel.Id.ToString(), message.Author.Id.ToString()).Inc(); + if (message.Author.IsBot) return; _logger.Information(LogSource.Message, message.Content, SimpleConextConverter.ConvertSocketMessage(message)); } diff --git a/Geekbot.net/Lib/RunParameters.cs b/Geekbot.net/Lib/RunParameters.cs index 444a60f..e16f644 100644 --- a/Geekbot.net/Lib/RunParameters.cs +++ b/Geekbot.net/Lib/RunParameters.cs @@ -72,5 +72,14 @@ namespace Geekbot.net.Lib [Option("api-port", Default = "12995", HelpText = "Port on which the WebApi listens")] public string ApiPort { get; set; } + + /************************************ + * Prometheus * + ************************************/ + [Option("prometheus-host", Default = "localhost", HelpText = "Host on which the Prometheus Metric Server listens")] + public string PrometheusHost { get; set; } + + [Option("prometheus-port", Default = "12991", HelpText = "Port on which the Prometheus Metric Server listens")] + public string PrometheusPort { get; set; } } } \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 4907431..bccc23f 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -24,6 +24,7 @@ using Geekbot.net.Lib.UserRepository; using Geekbot.net.WebApi; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; +using Prometheus; using WikipediaApi; namespace Geekbot.net @@ -185,6 +186,8 @@ namespace Geekbot.net var webserver = _runParameters.DisableApi ? Task.Delay(10) : StartWebApi(); + StartPrometheusServer(); + _logger.Information(LogSource.Geekbot, "Done and ready for use"); await webserver; @@ -211,5 +214,13 @@ namespace Geekbot.net WebApiStartup.StartWebApi(_logger, _runParameters, _commands, _databaseInitializer.Initialize(), _client, _globalSettings, highscoreManager); return Task.CompletedTask; } + + private void StartPrometheusServer() + { + var port = int.Parse(_runParameters.PrometheusPort); + var server = new MetricServer(_runParameters.PrometheusHost, port); + server.Start(); + _logger.Information(LogSource.Geekbot, $"Prometheus Metric Server running on {_runParameters.PrometheusHost}:{_runParameters.PrometheusPort}"); + } } } \ No newline at end of file From 569715f124fe2be626c753a20bda6ec892c53819 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 18 Apr 2020 00:05:09 +0200 Subject: [PATCH 205/443] fix prometheus metric server startup --- Geekbot.net/Program.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index bccc23f..6830bba 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -184,10 +184,9 @@ namespace Geekbot.net _client.ReactionRemoved += handlers.ReactionRemoved; if (!_runParameters.InMemory) _client.MessageReceived += handlers.UpdateStats; - var webserver = _runParameters.DisableApi ? Task.Delay(10) : StartWebApi(); - StartPrometheusServer(); + var webserver = _runParameters.DisableApi ? Task.Delay(10) : StartWebApi(); _logger.Information(LogSource.Geekbot, "Done and ready for use"); await webserver; @@ -217,7 +216,7 @@ namespace Geekbot.net private void StartPrometheusServer() { - var port = int.Parse(_runParameters.PrometheusPort); + var port = _runParameters.PrometheusPort == "12991" ? 12991 : int.Parse(_runParameters.PrometheusPort); var server = new MetricServer(_runParameters.PrometheusHost, port); server.Start(); _logger.Information(LogSource.Geekbot, $"Prometheus Metric Server running on {_runParameters.PrometheusHost}:{_runParameters.PrometheusPort}"); From fc5ff87c8fd4880ba26f78c223aae35755589d35 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 19 Apr 2020 13:52:44 +0200 Subject: [PATCH 206/443] Only the ManageMessages is now required to delete a quote --- Geekbot.net/Commands/Utils/Quote/Quote.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Geekbot.net/Commands/Utils/Quote/Quote.cs b/Geekbot.net/Commands/Utils/Quote/Quote.cs index e9c4706..f568036 100644 --- a/Geekbot.net/Commands/Utils/Quote/Quote.cs +++ b/Geekbot.net/Commands/Utils/Quote/Quote.cs @@ -166,10 +166,8 @@ namespace Geekbot.net.Commands.Utils.Quote } [Command("remove")] - [RequireUserPermission(GuildPermission.KickMembers)] [RequireUserPermission(GuildPermission.ManageMessages)] - [RequireUserPermission(GuildPermission.ManageRoles)] - [Summary("Remove a quote (required mod permissions)")] + [Summary("Remove a quote (user needs the 'ManageMessages' permission)")] public async Task RemoveQuote([Summary("quote-ID")] int id) { try From c031d2bfb42ef5ecba58710f3453a0e58cf61884 Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 7 May 2020 13:11:00 +0200 Subject: [PATCH 207/443] Remove Prometheus --- Geekbot.net/Handlers.cs | 11 +---------- Geekbot.net/Program.cs | 13 +------------ 2 files changed, 2 insertions(+), 22 deletions(-) diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index dc1ce71..52cf937 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -15,8 +15,6 @@ using Geekbot.net.Lib.Logger; using Geekbot.net.Lib.ReactionListener; using Geekbot.net.Lib.UserRepository; using Microsoft.EntityFrameworkCore; -using Prometheus; - namespace Geekbot.net { public class Handlers @@ -24,7 +22,6 @@ namespace Geekbot.net private readonly DatabaseContext _database; private readonly IDiscordClient _client; private readonly IGeekbotLogger _logger; - private readonly IAlmostRedis _redis; private readonly IServiceProvider _servicesProvider; private readonly CommandService _commands; private readonly IUserRepository _userRepository; @@ -33,10 +30,7 @@ namespace Geekbot.net private readonly RestApplication _applicationInfo; private readonly List _ignoredServers; - private readonly Counter _messageCounterPrometheus = - Metrics.CreateCounter("messages", "Number of discord messages", new CounterConfiguration() {LabelNames = new[] {"guild", "channel", "user"}}); - - public Handlers(DatabaseInitializer databaseInitializer, IDiscordClient client, IGeekbotLogger logger, IAlmostRedis redis, + public Handlers(DatabaseInitializer databaseInitializer, IDiscordClient client, IGeekbotLogger logger, IServiceProvider servicesProvider, CommandService commands, IUserRepository userRepository, IReactionListener reactionListener, RestApplication applicationInfo) { @@ -44,7 +38,6 @@ namespace Geekbot.net _messageCounterDatabaseContext = databaseInitializer.Initialize(); _client = client; _logger = logger; - _redis = redis; _servicesProvider = servicesProvider; _commands = commands; _userRepository = userRepository; @@ -150,8 +143,6 @@ namespace Geekbot.net _messageCounterDatabaseContext.SaveChanges(); } - _messageCounterPrometheus.WithLabels(channel.Guild.Id.ToString(), channel.Id.ToString(), message.Author.Id.ToString()).Inc(); - if (message.Author.IsBot) return; _logger.Information(LogSource.Message, message.Content, SimpleConextConverter.ConvertSocketMessage(message)); } diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 6830bba..3bd5a8f 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -24,7 +24,6 @@ using Geekbot.net.Lib.UserRepository; using Geekbot.net.WebApi; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; -using Prometheus; using WikipediaApi; namespace Geekbot.net @@ -173,7 +172,7 @@ namespace Geekbot.net _servicesProvider = _services.BuildServiceProvider(); await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _servicesProvider); - var handlers = new Handlers(_databaseInitializer, _client, _logger, _redis, _servicesProvider, _commands, _userRepository, reactionListener, applicationInfo); + var handlers = new Handlers(_databaseInitializer, _client, _logger, _servicesProvider, _commands, _userRepository, reactionListener, applicationInfo); _client.MessageReceived += handlers.RunCommand; _client.MessageDeleted += handlers.MessageDeleted; @@ -183,8 +182,6 @@ namespace Geekbot.net _client.ReactionAdded += handlers.ReactionAdded; _client.ReactionRemoved += handlers.ReactionRemoved; if (!_runParameters.InMemory) _client.MessageReceived += handlers.UpdateStats; - - StartPrometheusServer(); var webserver = _runParameters.DisableApi ? Task.Delay(10) : StartWebApi(); _logger.Information(LogSource.Geekbot, "Done and ready for use"); @@ -213,13 +210,5 @@ namespace Geekbot.net WebApiStartup.StartWebApi(_logger, _runParameters, _commands, _databaseInitializer.Initialize(), _client, _globalSettings, highscoreManager); return Task.CompletedTask; } - - private void StartPrometheusServer() - { - var port = _runParameters.PrometheusPort == "12991" ? 12991 : int.Parse(_runParameters.PrometheusPort); - var server = new MetricServer(_runParameters.PrometheusHost, port); - server.Start(); - _logger.Information(LogSource.Geekbot, $"Prometheus Metric Server running on {_runParameters.PrometheusHost}:{_runParameters.PrometheusPort}"); - } } } \ No newline at end of file From 5cf1248bf04df3b5a6c1fe8bb46a4019057904ad Mon Sep 17 00:00:00 2001 From: runebaas Date: Mon, 11 May 2020 23:44:15 +0200 Subject: [PATCH 208/443] Add !quote stats (v1) --- Geekbot.net/Commands/Utils/Quote/Quote.cs | 48 ++++++++++++++++++- Geekbot.net/Lib/Localization/Translations.yml | 9 ++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/Geekbot.net/Commands/Utils/Quote/Quote.cs b/Geekbot.net/Commands/Utils/Quote/Quote.cs index f568036..454ad9b 100644 --- a/Geekbot.net/Commands/Utils/Quote/Quote.cs +++ b/Geekbot.net/Commands/Utils/Quote/Quote.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Linq; using System.Threading.Tasks; using Discord; @@ -192,6 +192,52 @@ namespace Geekbot.net.Commands.Utils.Quote } } + [Command("stats")] + [Summary("Show quote stats for this server")] + public async Task GetQuoteStatsForServer() + { + try + { + var transContext = await _translationHandler.GetGuildContext(Context); + var eb = new EmbedBuilder(); + eb.Author = new EmbedAuthorBuilder() + { + IconUrl = Context.Guild.IconUrl, + Name = $"{Context.Guild.Name} - {transContext.GetString("QuoteStats")}" + }; + + var totalQuotes = _database.Quotes.Count(row => row.GuildId == Context.Guild.Id.AsLong()); + if (totalQuotes == 0) + { + await ReplyAsync(transContext.GetString("NoQuotesFound")); + return; + } + eb.AddInlineField(transContext.GetString("TotalQuotes"), totalQuotes); + + var mostQuotedPerson = _database.Quotes + .Where(row => row.GuildId == Context.Guild.Id.AsLong()) + .GroupBy(row => row.UserId) + .Max(row => row.Key); + var user = Context.Client.GetUserAsync(mostQuotedPerson.AsUlong()).Result ?? new UserPolyfillDto {Username = "Unknown User"}; + eb.AddInlineField(transContext.GetString("MostQuotesPerson"), user); + + var quotesByYear = _database.Quotes + .Where(row => row.GuildId == Context.Guild.Id.AsLong()) + .GroupBy(row => row.Time.Year) + .Select(row => new { year = row.Key, amount = row.Count()}); + foreach (var year in quotesByYear) + { + eb.AddInlineField(year.year.ToString(), year.amount); + } + + await ReplyAsync("", false, eb.Build()); + } + catch (Exception e) + { + await _errorHandler.HandleCommandException(e, Context); + } + } + private async Task GetLastMessageByUser(IUser user) { try diff --git a/Geekbot.net/Lib/Localization/Translations.yml b/Geekbot.net/Lib/Localization/Translations.yml index 5744741..a19a6f3 100644 --- a/Geekbot.net/Lib/Localization/Translations.yml +++ b/Geekbot.net/Lib/Localization/Translations.yml @@ -160,6 +160,15 @@ quote: NotFoundWithId: EN: "I couldn't find a quote with that ID :disappointed:" CHDE: "Ich chan kei quote finde mit därri ID :disappointed:" + QuoteStats: + EN: "Quote Stats" + CHDE: "Quote statistike" + TotalQuotes: + EN: "Total" + CHDE: "Total" + MostQuotesPerson: + EN: "Most quoted person" + CHDE: "Meist quoteti person" rank: InvalidType: EN: "Valid types are '`messages`' '`karma`', '`rolls`' and '`cookies`'" From 656393cc7b40654023ec44dbcde207d466e9f8fe Mon Sep 17 00:00:00 2001 From: runebaas Date: Tue, 12 May 2020 00:04:20 +0200 Subject: [PATCH 209/443] '!quote stats' bug fixes --- Geekbot.net/Commands/Utils/Quote/Quote.cs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/Geekbot.net/Commands/Utils/Quote/Quote.cs b/Geekbot.net/Commands/Utils/Quote/Quote.cs index 454ad9b..5c2bd3b 100644 --- a/Geekbot.net/Commands/Utils/Quote/Quote.cs +++ b/Geekbot.net/Commands/Utils/Quote/Quote.cs @@ -198,6 +198,7 @@ namespace Geekbot.net.Commands.Utils.Quote { try { + // setup var transContext = await _translationHandler.GetGuildContext(Context); var eb = new EmbedBuilder(); eb.Author = new EmbedAuthorBuilder() @@ -206,25 +207,33 @@ namespace Geekbot.net.Commands.Utils.Quote Name = $"{Context.Guild.Name} - {transContext.GetString("QuoteStats")}" }; + // gather data var totalQuotes = _database.Quotes.Count(row => row.GuildId == Context.Guild.Id.AsLong()); if (totalQuotes == 0) { + // no quotes, no stats, end of the road await ReplyAsync(transContext.GetString("NoQuotesFound")); return; } - eb.AddInlineField(transContext.GetString("TotalQuotes"), totalQuotes); - + var mostQuotedPerson = _database.Quotes .Where(row => row.GuildId == Context.Guild.Id.AsLong()) .GroupBy(row => row.UserId) - .Max(row => row.Key); - var user = Context.Client.GetUserAsync(mostQuotedPerson.AsUlong()).Result ?? new UserPolyfillDto {Username = "Unknown User"}; - eb.AddInlineField(transContext.GetString("MostQuotesPerson"), user); + .Select(row => new { userId = row.Key, amount = row.Count()}) + .OrderBy(row => row.amount) + .Last(); + var mostQuotedPersonUser = Context.Client.GetUserAsync(mostQuotedPerson.userId.AsUlong()).Result ?? new UserPolyfillDto {Username = "Unknown User"}; var quotesByYear = _database.Quotes .Where(row => row.GuildId == Context.Guild.Id.AsLong()) .GroupBy(row => row.Time.Year) - .Select(row => new { year = row.Key, amount = row.Count()}); + .Select(row => new { year = row.Key, amount = row.Count()}) + .OrderBy(row => row.year); + + // add data to the embed + eb.AddField(transContext.GetString("MostQuotesPerson"), $"{mostQuotedPersonUser.Username} ({mostQuotedPerson.amount})"); + eb.AddInlineField(transContext.GetString("TotalQuotes"), totalQuotes); + foreach (var year in quotesByYear) { eb.AddInlineField(year.year.ToString(), year.amount); From 2e501008df466b6fb2eda39dcd7bda6f823edaf3 Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 21 May 2020 15:55:04 +0200 Subject: [PATCH 210/443] Remove !checkem --- Geekbot.net/Commands/Randomness/CheckEm.cs | 71 ------------ Geekbot.net/Geekbot.net.csproj | 3 - Geekbot.net/Lib/Media/IMediaProvider.cs | 1 - Geekbot.net/Lib/Media/MediaProvider.cs | 18 +-- Geekbot.net/Storage/checkEmPics | 122 --------------------- 5 files changed, 2 insertions(+), 213 deletions(-) delete mode 100644 Geekbot.net/Commands/Randomness/CheckEm.cs delete mode 100644 Geekbot.net/Storage/checkEmPics diff --git a/Geekbot.net/Commands/Randomness/CheckEm.cs b/Geekbot.net/Commands/Randomness/CheckEm.cs deleted file mode 100644 index 0596872..0000000 --- a/Geekbot.net/Commands/Randomness/CheckEm.cs +++ /dev/null @@ -1,71 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading.Tasks; -using Discord.Commands; -using Geekbot.net.Lib.ErrorHandling; -using Geekbot.net.Lib.Media; - -namespace Geekbot.net.Commands.Randomness -{ - public class CheckEm : ModuleBase - { - private readonly IMediaProvider _checkEmImages; - private readonly IErrorHandler _errorHandler; - - public CheckEm(IMediaProvider mediaProvider, IErrorHandler errorHandler) - { - _checkEmImages = mediaProvider; - _errorHandler = errorHandler; - } - - [Command("checkem", RunMode = RunMode.Async)] - [Summary("Check for dubs")] - public async Task MuhDubs() - { - try - { - var number = new Random().Next(10000000, 99999999); - var dubtriqua = ""; - - var ns = GetIntArray(number); - if (ns[7] == ns[6]) - { - dubtriqua = "DUBS"; - if (ns[6] == ns[5]) - { - dubtriqua = "TRIPS"; - if (ns[5] == ns[4]) - dubtriqua = "QUADS"; - } - } - - var sb = new StringBuilder(); - sb.AppendLine($"Check em {Context.User.Mention}"); - sb.AppendLine($"**{number}**"); - if (!string.IsNullOrEmpty(dubtriqua)) - sb.AppendLine($":tada: {dubtriqua} :tada:"); - sb.AppendLine(_checkEmImages.GetCheckem()); - - await ReplyAsync(sb.ToString()); - } - catch (Exception e) - { - await _errorHandler.HandleCommandException(e, Context); - } - } - - private int[] GetIntArray(int num) - { - var listOfInts = new List(); - while (num > 0) - { - listOfInts.Add(num % 10); - num /= 10; - } - - listOfInts.Reverse(); - return listOfInts.ToArray(); - } - } -} \ No newline at end of file diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index e50613a..baf326d 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -59,9 +59,6 @@ - - PreserveNewest - PreserveNewest diff --git a/Geekbot.net/Lib/Media/IMediaProvider.cs b/Geekbot.net/Lib/Media/IMediaProvider.cs index a646cea..33a0d7c 100644 --- a/Geekbot.net/Lib/Media/IMediaProvider.cs +++ b/Geekbot.net/Lib/Media/IMediaProvider.cs @@ -2,7 +2,6 @@ { public interface IMediaProvider { - string GetCheckem(); string GetPanda(); string GetCrossant(); string GetSquirrel(); diff --git a/Geekbot.net/Lib/Media/MediaProvider.cs b/Geekbot.net/Lib/Media/MediaProvider.cs index c88b696..f7ea9df 100644 --- a/Geekbot.net/Lib/Media/MediaProvider.cs +++ b/Geekbot.net/Lib/Media/MediaProvider.cs @@ -8,7 +8,6 @@ namespace Geekbot.net.Lib.Media { private readonly Random _random; private readonly IGeekbotLogger _logger; - private string[] _checkemImages; private string[] _pandaImages; private string[] _croissantImages; private string[] _squirrelImages; @@ -24,8 +23,7 @@ namespace Geekbot.net.Lib.Media _logger = logger; logger.Information(LogSource.Geekbot, "Loading Media Files"); - - LoadCheckem(); +; LoadPandas(); BakeCroissants(); LoadSquirrels(); @@ -35,13 +33,6 @@ namespace Geekbot.net.Lib.Media LoadFoxes(); LoadDab(); } - - private void LoadCheckem() - { - var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/checkEmPics")); - _checkemImages = rawLinks.Split("\n"); - _logger.Trace(LogSource.Geekbot, $"Loaded {_checkemImages.Length} CheckEm Images"); - } private void LoadPandas() { @@ -98,12 +89,7 @@ namespace Geekbot.net.Lib.Media _dabImages = rawLinks.Split("\n"); _logger.Trace(LogSource.Geekbot, $"Loaded {_dabImages.Length} Dab Images"); } - - public string GetCheckem() - { - return _checkemImages[_random.Next(0, _checkemImages.Length)]; - } - + public string GetPanda() { return _pandaImages[_random.Next(0, _pandaImages.Length)]; diff --git a/Geekbot.net/Storage/checkEmPics b/Geekbot.net/Storage/checkEmPics deleted file mode 100644 index 03be23e..0000000 --- a/Geekbot.net/Storage/checkEmPics +++ /dev/null @@ -1,122 +0,0 @@ -http://s19.postimg.org/pcq2kwzoj/4cb.png -http://s19.postimg.org/cvetk0f4z/5_Dim_Dy6p.jpg -http://s19.postimg.org/5hzfl1v37/1310151998600.jpg -http://s19.postimg.org/53y3lgazn/1324181141954.jpg -http://s19.postimg.org/724rjg3hf/1392512742365.png -http://s19.postimg.org/3rgejkdk3/1393501296733.png -http://s19.postimg.org/a6ffg8k9v/1401667341503.jpg -http://s19.postimg.org/qiph5yylf/1419231572452.jpg -http://s19.postimg.org/fqwi4m8ir/1427600681401.png -http://s19.postimg.org/4c00zzw6b/1447813628974.png -http://s19.postimg.org/uuio8puw3/b5_3q_ycaaavxtf.jpg -http://s19.postimg.org/bghu913fn/check_em_by_boyboy99100_d57xp3y.png -http://s19.postimg.org/s1pgooujn/l_Hkppjs.jpg -http://s19.postimg.org/m08itft0j/checkem.jpg -https://old.postimg.org/image/6vx33rb1b/ -https://old.postimg.org/image/wxiaz1mov/ -https://old.postimg.org/image/azqfizx27/ -https://old.postimg.org/image/6iy2kbiu7/ -https://old.postimg.org/image/k8slt45y7/ -https://old.postimg.org/image/t7ruxmplr/ -https://old.postimg.org/image/ssbzqvean/ -https://old.postimg.org/image/kbchfy9lr/ -https://old.postimg.org/image/dl0lk9btr/ -https://old.postimg.org/image/e5k80oufz/ -https://old.postimg.org/image/er005baqn/ -https://old.postimg.org/image/bfk2uzcin/ -https://old.postimg.org/image/556fp0jkv/ -https://old.postimg.org/image/i0efbryu7/ -https://old.postimg.org/image/943n7u87z/ -https://old.postimg.org/image/xn5op5cm7/ -https://old.postimg.org/image/3l5p4d0kf/ -https://old.postimg.org/image/5boq5ui3j/ -https://old.postimg.org/image/ru082bqcf/ -https://old.postimg.org/image/ytea1oqan/ -https://old.postimg.org/image/vu7dekgtb/ -https://old.postimg.org/image/hl7qwi2an/ -https://old.postimg.org/image/5aescfg9r/ -https://old.postimg.org/image/9gzmrrfvj/ -https://old.postimg.org/image/50bv6tr1b/ -https://old.postimg.org/image/afkl7silb/ -https://old.postimg.org/image/nrdsgzllr/ -https://old.postimg.org/image/s32e5zsin/ -https://old.postimg.org/image/5sej60v8f/ -https://old.postimg.org/image/lgfqctau7/ -https://old.postimg.org/image/tn7q4e0wv/ -https://old.postimg.org/image/8612arz1b/ -https://old.postimg.org/image/w5tf52mn3/ -https://old.postimg.org/image/zdxwi48wv/ -https://old.postimg.org/image/lphwghd0f/ -https://old.postimg.org/image/uzu0k0nq7/ -https://old.postimg.org/image/3vqzsxjbz/ -https://old.postimg.org/image/5d7uqqyov/ -https://old.postimg.org/image/dntnyku8v/ -https://old.postimg.org/image/dsxf891jz/ -https://old.postimg.org/image/3nyrioizj/ -https://old.postimg.org/image/6zx2bzaqn/ -https://old.postimg.org/image/wu6v1raqn/ -https://old.postimg.org/image/hb9f4n2fz/ -https://old.postimg.org/image/p7yhqm3a7/ -https://old.postimg.org/image/oelvxzx9b/ -https://old.postimg.org/image/vcq03xvdr/ -https://old.postimg.org/image/b08t1yqlb/ -https://old.postimg.org/image/6yrpwayan/ -https://old.postimg.org/image/btleukwm7/ -https://old.postimg.org/image/62ztuldzz/ -https://old.postimg.org/image/w3iq9pxr3/ -https://old.postimg.org/image/byp6493xb/ -https://old.postimg.org/image/xp2lf9xcv/ -https://old.postimg.org/image/j9p9u49pb/ -https://old.postimg.org/image/hvxmytafz/ -https://old.postimg.org/image/5eqzbnfa7/ -https://old.postimg.org/image/do2uq290f/ -https://old.postimg.org/image/54o261q1r/ -https://old.postimg.org/image/94qm4jr4v/ -https://old.postimg.org/image/lee88y0pr/ -https://old.postimg.org/image/bncb58cv3/ -https://old.postimg.org/image/5246j7me7/ -https://old.postimg.org/image/4uby8ym1r/ -https://old.postimg.org/image/qn996tj4v/ -https://old.postimg.org/image/c1dn4twyn/ -https://old.postimg.org/image/6rd9ra23j/ -https://lehcark14.files.wordpress.com/2008/08/botan16.jpg -http://i.imgur.com/p9vALew.jpg -http://i.imgur.com/4a9l2Rm.png -http://i.imgur.com/RNtixMQ.jpg -https://pbs.twimg.com/media/Cro9aIGUEAAkXCP.jpg -http://s16.postimg.org/empvloimd/Check_em_Guts.png -https://s18.postimg.io/qgbhe7u09/1424491645996.gif -http://s19.postimg.org/hhemlt7xf/3eb.jpg -http://s19.postimg.org/cwsg6vo83/8aa.png -http://s19.postimg.org/rh9j1pj6r/28mohl4.png -http://s19.postimg.org/zba4n3qzn/86d.jpg -http://s19.postimg.org/cb3hart5v/2016_09_16_08_58_45.png -http://s19.postimg.org/m9ofx92lf/bb1.jpg -http://s19.postimg.org/maydqo4f7/e8b.jpg -http://s19.postimg.org/yqzoy5n4z/fbe.png -http://s19.postimg.org/xd822unvn/giphy.gif -http://s19.postimg.org/c4udlf9er/l_TU3eup.jpg -https://66.media.tumblr.com/cc893a0ee40d73d083da3df4bdaf45cc/tumblr_mx8psiFduG1t1g1k8o1_500.gif -http://i.imgur.com/swbXHSy.gif -http://img1.reactor.cc/pics/post/full/Anime-Touhou-Project-Yakumo-Yukari-%D0%A0%D0%B5%D0%BA%D1%83%D1%80%D1%81%D0%B8%D1%8F-1303807.jpeg -http://i.imgur.com/ftGLHE0.png -http://i.imgur.com/JELDhKQ.png -http://imgur.com/yBJound -http://i.imgur.com/f7gAVPJ.png -http://i.imgur.com/HxWyo2Z.jpg -http://i.imgur.com/8Eb9CxQ.png -http://i.imgur.com/kOECcjz.png -http://i.imgur.com/MJLu7oJ.jpg -http://i.imgur.com/itG3rPM.jpg -http://i.imgur.com/G83Go9t.jpg -http://i.imgur.com/jI2dBnU.jpg -http://i.imgur.com/FtALzg0.jpg -http://i.imgur.com/GwZpJEv.gif -http://i.imgur.com/TYGRD3B.gif -http://i.imgur.com/P6TxLS3.png -http://i.imgur.com/phTVTdn.jpg -http://i.imgur.com/thhR6UE.jpg -http://i.imgur.com/KbROufx.jpg -http://i.imgur.com/sQqWbcm.jpg -http://i.imgur.com/YYpis53.png -http://i.imgur.com/kwaRd54.gif \ No newline at end of file From 33b17b373f1a55e829d6b4bcb953e7a6384ffae3 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 30 May 2020 17:02:17 +0200 Subject: [PATCH 211/443] Remove all dependencies on redis --- Geekbot.net/Commands/Admin/Role.cs | 19 ++--- Geekbot.net/Commands/Games/Roll.cs | 23 +++-- Geekbot.net/Commands/User/Stats.cs | 5 +- Geekbot.net/Database/DatabaseContext.cs | 1 + .../Database/Models/ReactionListenerModel.cs | 22 +++++ Geekbot.net/Handlers.cs | 2 +- .../Lib/KvInMemoryStore/IKvInMemoryStore.cs | 9 ++ .../Lib/KvInMemoryStore/KvInMemoryStore.cs | 32 +++++++ .../Lib/ReactionListener/IReactionListener.cs | 3 +- .../Lib/ReactionListener/ReactionListener.cs | 85 +++++++++---------- Geekbot.net/Program.cs | 5 +- 11 files changed, 129 insertions(+), 77 deletions(-) create mode 100644 Geekbot.net/Database/Models/ReactionListenerModel.cs create mode 100644 Geekbot.net/Lib/KvInMemoryStore/IKvInMemoryStore.cs create mode 100644 Geekbot.net/Lib/KvInMemoryStore/KvInMemoryStore.cs diff --git a/Geekbot.net/Commands/Admin/Role.cs b/Geekbot.net/Commands/Admin/Role.cs index b0df4bb..6d4557d 100644 --- a/Geekbot.net/Commands/Admin/Role.cs +++ b/Geekbot.net/Commands/Admin/Role.cs @@ -170,23 +170,16 @@ namespace Geekbot.net.Commands.Admin [RequireUserPermission(GuildPermission.ManageRoles)] [Summary("Give a role by clicking on an emoji")] [Command("listen", RunMode = RunMode.Async)] - public async Task AddListener([Summary("message-ID")] string messageId, [Summary("Emoji")] string emoji, [Summary("@role")] IRole role) + public async Task AddListener([Summary("message-ID")] string messageIdStr, [Summary("Emoji")] string emoji, [Summary("@role")] IRole role) { try { - var message = (IUserMessage) await Context.Channel.GetMessageAsync(ulong.Parse(messageId)); - IEmote emote; - if (!emoji.StartsWith('<')) - { - var emo = new Emoji(emoji); - emote = emo; - } - else - { - emote = Emote.Parse(emoji); - } + var messageId = ulong.Parse(messageIdStr); + var message = (IUserMessage) await Context.Channel.GetMessageAsync(messageId); + var emote = _reactionListener.ConvertStringToEmote(emoji); + await message.AddReactionAsync(emote); - await _reactionListener.AddRoleToListener(messageId, emote, role); + await _reactionListener.AddRoleToListener(messageId, Context.Guild.Id, emoji, role); await Context.Message.DeleteAsync(); } catch (HttpException e) diff --git a/Geekbot.net/Commands/Games/Roll.cs b/Geekbot.net/Commands/Games/Roll.cs index dfecb13..076cf8f 100644 --- a/Geekbot.net/Commands/Games/Roll.cs +++ b/Geekbot.net/Commands/Games/Roll.cs @@ -4,26 +4,25 @@ using System.Threading.Tasks; using Discord.Commands; using Geekbot.net.Database; using Geekbot.net.Database.Models; -using Geekbot.net.Lib.AlmostRedis; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; +using Geekbot.net.Lib.KvInMemoryStore; using Geekbot.net.Lib.Localization; using Geekbot.net.Lib.RandomNumberGenerator; -using StackExchange.Redis; namespace Geekbot.net.Commands.Games { public class Roll : ModuleBase { private readonly IErrorHandler _errorHandler; - private readonly IAlmostRedis _redis; + private readonly IKvInMemoryStore _kvInMemoryStore; private readonly ITranslationHandler _translation; private readonly DatabaseContext _database; private readonly IRandomNumberGenerator _randomNumberGenerator; - public Roll(IAlmostRedis redis, IErrorHandler errorHandler, ITranslationHandler translation, DatabaseContext database, IRandomNumberGenerator randomNumberGenerator) + public Roll(IKvInMemoryStore kvInMemoryStore,IErrorHandler errorHandler, ITranslationHandler translation, DatabaseContext database, IRandomNumberGenerator randomNumberGenerator) { - _redis = redis; + _kvInMemoryStore = kvInMemoryStore; _translation = translation; _database = database; _randomNumberGenerator = randomNumberGenerator; @@ -37,28 +36,28 @@ namespace Geekbot.net.Commands.Games try { var number = _randomNumberGenerator.Next(1, 100); - var guess = 1000; - int.TryParse(stuff, out guess); + int.TryParse(stuff, out var guess); var transContext = await _translation.GetGuildContext(Context); if (guess <= 100 && guess > 0) { - var prevRoll = _redis.Db.HashGet($"{Context.Guild.Id}:RollsPrevious2", Context.Message.Author.Id).ToString()?.Split('|'); - if (prevRoll?.Length == 2) + var kvKey = $"{Context.Guild.Id}:{Context.User.Id}:RollsPrevious"; + + var prevRoll = _kvInMemoryStore.Get(kvKey); + if (prevRoll > 0) { - if (prevRoll[0] == guess.ToString() && DateTime.Parse(prevRoll[1]) > DateTime.Now.AddDays(-1)) + if (prevRoll == guess) { await ReplyAsync(transContext.GetString("NoPrevGuess", Context.Message.Author.Mention)); return; } } - _redis.Db.HashSet($"{Context.Guild.Id}:RollsPrevious2", new[] {new HashEntry(Context.Message.Author.Id, $"{guess}|{DateTime.Now}")}); + _kvInMemoryStore.Set(kvKey, guess); await ReplyAsync(transContext.GetString("Rolled", Context.Message.Author.Mention, number, guess)); if (guess == number) { await ReplyAsync(transContext.GetString("Gratz", Context.Message.Author)); - _redis.Db.HashIncrement($"{Context.Guild.Id}:Rolls", Context.User.Id.ToString()); var user = await GetUser(Context.User.Id); user.Rolls += 1; _database.Rolls.Update(user); diff --git a/Geekbot.net/Commands/User/Stats.cs b/Geekbot.net/Commands/User/Stats.cs index 05ea324..08b69d2 100644 --- a/Geekbot.net/Commands/User/Stats.cs +++ b/Geekbot.net/Commands/User/Stats.cs @@ -4,7 +4,6 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Geekbot.net.Database; -using Geekbot.net.Lib.AlmostRedis; using Geekbot.net.Lib.CommandPreconditions; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; @@ -16,12 +15,10 @@ namespace Geekbot.net.Commands.User { private readonly IErrorHandler _errorHandler; private readonly ILevelCalc _levelCalc; - private readonly IAlmostRedis _redis; private readonly DatabaseContext _database; - public Stats(IAlmostRedis redis, DatabaseContext database, IErrorHandler errorHandler, ILevelCalc levelCalc) + public Stats(DatabaseContext database, IErrorHandler errorHandler, ILevelCalc levelCalc) { - _redis = redis; _database = database; _errorHandler = errorHandler; _levelCalc = levelCalc; diff --git a/Geekbot.net/Database/DatabaseContext.cs b/Geekbot.net/Database/DatabaseContext.cs index cbfa30d..56c19f8 100644 --- a/Geekbot.net/Database/DatabaseContext.cs +++ b/Geekbot.net/Database/DatabaseContext.cs @@ -18,5 +18,6 @@ namespace Geekbot.net.Database public DbSet RoleSelfService { get; set; } public DbSet Polls { get; set; } public DbSet Cookies { get; set; } + public DbSet ReactionListeners { get; set; } } } \ No newline at end of file diff --git a/Geekbot.net/Database/Models/ReactionListenerModel.cs b/Geekbot.net/Database/Models/ReactionListenerModel.cs new file mode 100644 index 0000000..05ab5b3 --- /dev/null +++ b/Geekbot.net/Database/Models/ReactionListenerModel.cs @@ -0,0 +1,22 @@ +using System.ComponentModel.DataAnnotations; + +namespace Geekbot.net.Database.Models +{ + public class ReactionListenerModel + { + [Key] + public int Id { get; set; } + + [Required] + public long GuildId { get; set; } + + [Required] + public long MessageId { get; set; } + + [Required] + public long RoleId { get; set; } + + [Required] + public string Reaction { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index 52cf937..6f98041 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -9,12 +9,12 @@ using Discord.Rest; using Discord.WebSocket; using Geekbot.net.Database; using Geekbot.net.Database.Models; -using Geekbot.net.Lib.AlmostRedis; using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Logger; using Geekbot.net.Lib.ReactionListener; using Geekbot.net.Lib.UserRepository; using Microsoft.EntityFrameworkCore; + namespace Geekbot.net { public class Handlers diff --git a/Geekbot.net/Lib/KvInMemoryStore/IKvInMemoryStore.cs b/Geekbot.net/Lib/KvInMemoryStore/IKvInMemoryStore.cs new file mode 100644 index 0000000..88f3863 --- /dev/null +++ b/Geekbot.net/Lib/KvInMemoryStore/IKvInMemoryStore.cs @@ -0,0 +1,9 @@ +namespace Geekbot.net.Lib.KvInMemoryStore +{ + public interface IKvInMemoryStore + { + public T Get(string key); + public void Set(string key, T value); + public void Remove(string key); + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/KvInMemoryStore/KvInMemoryStore.cs b/Geekbot.net/Lib/KvInMemoryStore/KvInMemoryStore.cs new file mode 100644 index 0000000..33b7155 --- /dev/null +++ b/Geekbot.net/Lib/KvInMemoryStore/KvInMemoryStore.cs @@ -0,0 +1,32 @@ +using System.Collections.Generic; + +namespace Geekbot.net.Lib.KvInMemoryStore +{ + public class KvInInMemoryStore : IKvInMemoryStore + { + private readonly Dictionary _storage = new Dictionary(); + + public T Get(string key) + { + try + { + return (T) _storage[key]; + } + catch + { + return default; + } + } + + public void Set(string key, T value) + { + _storage.Remove(key); + _storage.Add(key, value); + } + + public void Remove(string key) + { + _storage.Remove(key); + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/ReactionListener/IReactionListener.cs b/Geekbot.net/Lib/ReactionListener/IReactionListener.cs index 792a516..b2ab9fb 100644 --- a/Geekbot.net/Lib/ReactionListener/IReactionListener.cs +++ b/Geekbot.net/Lib/ReactionListener/IReactionListener.cs @@ -7,8 +7,9 @@ namespace Geekbot.net.Lib.ReactionListener public interface IReactionListener { bool IsListener(ulong id); - Task AddRoleToListener(string messageId, IEmote emoji, IRole role); + Task AddRoleToListener(ulong messageId, ulong guildId, string emoji, IRole role); void RemoveRole(ISocketMessageChannel channel, SocketReaction reaction); void GiveRole(ISocketMessageChannel message, SocketReaction reaction); + IEmote ConvertStringToEmote(string emoji); } } \ No newline at end of file diff --git a/Geekbot.net/Lib/ReactionListener/ReactionListener.cs b/Geekbot.net/Lib/ReactionListener/ReactionListener.cs index 2349028..4a9a08c 100644 --- a/Geekbot.net/Lib/ReactionListener/ReactionListener.cs +++ b/Geekbot.net/Lib/ReactionListener/ReactionListener.cs @@ -1,80 +1,66 @@ using System.Collections.Generic; -using System.Linq; using System.Threading.Tasks; using Discord; using Discord.WebSocket; -using StackExchange.Redis; +using Geekbot.net.Database; +using Geekbot.net.Database.Models; +using Geekbot.net.Lib.Extensions; namespace Geekbot.net.Lib.ReactionListener { public class ReactionListener : IReactionListener { - private readonly IDatabase _redis; - private Dictionary> _listener; + private readonly DatabaseContext _database; + private Dictionary> _listener; - public ReactionListener(IDatabase redis) + public ReactionListener(DatabaseContext database) { - _redis = redis; + _database = database; LoadListeners(); } private void LoadListeners() { - var ids = _redis.SetMembers("MessageIds"); - _listener = new Dictionary>(); - foreach (var id in ids) + _listener = new Dictionary>(); + foreach (var row in _database.ReactionListeners) { - var reactions = _redis.HashGetAll($"Messages:{id}"); - var messageId = id; - var emojiDict = new Dictionary(); - foreach (var r in reactions) + var messageId = row.MessageId.AsUlong(); + if (!_listener.ContainsKey(messageId)) { - IEmote emote; - if (!r.Name.ToString().StartsWith('<')) - { - var emo = new Emoji(r.Name); - emote = emo; - } - else - { - emote = Emote.Parse(r.Name); - } - emojiDict.Add(emote, ulong.Parse(r.Value)); + _listener.Add(messageId, new Dictionary()); } - _listener.Add(messageId, emojiDict); + + _listener[messageId].Add(ConvertStringToEmote(row.Reaction), row.RoleId.AsUlong()); } } public bool IsListener(ulong id) { - return _listener.ContainsKey(id.ToString()); + return _listener.ContainsKey(id); } - public Task AddRoleToListener(string messageId, IEmote emoji, IRole role) + public async Task AddRoleToListener(ulong messageId, ulong guildId, string emoji, IRole role) { - if (_redis.SetMembers("MessageIds").All(e => e.ToString() != messageId)) - { - _redis.SetAdd("MessageIds", messageId); - } - _redis.HashSet($"Messages:{messageId}", new[] {new HashEntry(emoji.ToString(), role.Id.ToString())}); - _redis.SetAdd("MessageIds", messageId); - if (_listener.ContainsKey(messageId)) - { - _listener[messageId].Add(emoji, role.Id); - return Task.CompletedTask; - } + var emote = ConvertStringToEmote(emoji); - var dict = new Dictionary + await _database.ReactionListeners.AddAsync(new ReactionListenerModel() { - {emoji, role.Id} - }; - _listener.Add(messageId, dict); - return Task.CompletedTask; + GuildId = guildId.AsLong(), + MessageId = messageId.AsLong(), + RoleId = role.Id.AsLong(), + Reaction = emoji + }); + + if (!_listener.ContainsKey(messageId)) + { + _listener.Add(messageId, new Dictionary()); + } + _listener[messageId].Add(emote, role.Id); } public async void RemoveRole(ISocketMessageChannel channel, SocketReaction reaction) { - var roleId = _listener[reaction.MessageId.ToString()][reaction.Emote]; + var roleId = _listener[reaction.MessageId][reaction.Emote]; var guild = (SocketGuildChannel) channel; var role = guild.Guild.GetRole(roleId); await ((IGuildUser) reaction.User.Value).RemoveRoleAsync(role); @@ -82,10 +68,19 @@ namespace Geekbot.net.Lib.ReactionListener public async void GiveRole(ISocketMessageChannel channel, SocketReaction reaction) { - var roleId = _listener[reaction.MessageId.ToString()][reaction.Emote]; + var roleId = _listener[reaction.MessageId][reaction.Emote]; var guild = (SocketGuildChannel) channel; var role = guild.Guild.GetRole(roleId); await ((IGuildUser) reaction.User.Value).AddRoleAsync(role); } + + public IEmote ConvertStringToEmote(string emoji) + { + if (!emoji.StartsWith('<')) + { + return new Emoji(emoji); + } + return Emote.Parse(emoji); + } } } \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 3bd5a8f..389c051 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -14,6 +14,7 @@ using Geekbot.net.Lib.Converters; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.GlobalSettings; using Geekbot.net.Lib.Highscores; +using Geekbot.net.Lib.KvInMemoryStore; using Geekbot.net.Lib.Levels; using Geekbot.net.Lib.Localization; using Geekbot.net.Lib.Logger; @@ -125,6 +126,7 @@ namespace Geekbot.net var mtgManaConverter = new MtgManaConverter(); var wikipediaClient = new WikipediaClient(); var randomNumberGenerator = new RandomNumberGenerator(); + var kvMemoryStore = new KvInInMemoryStore(); _services.AddSingleton(_redis); _services.AddSingleton(_userRepository); @@ -137,6 +139,7 @@ namespace Geekbot.net _services.AddSingleton(mtgManaConverter); _services.AddSingleton(wikipediaClient); _services.AddSingleton(randomNumberGenerator); + _services.AddSingleton(kvMemoryStore); _services.AddSingleton(_globalSettings); _services.AddTransient(e => new HighscoreManager(_databaseInitializer.Initialize(), _userRepository)); _services.AddTransient(e => _databaseInitializer.Initialize()); @@ -164,7 +167,7 @@ namespace Geekbot.net _logger.Information(LogSource.Geekbot, "Registering Stuff"); var translationHandler = new TranslationHandler(_databaseInitializer.Initialize(), _logger); var errorHandler = new ErrorHandler(_logger, translationHandler, _runParameters.ExposeErrors); - var reactionListener = new ReactionListener(_redis.Db); + var reactionListener = new ReactionListener(_databaseInitializer.Initialize()); _services.AddSingleton(errorHandler); _services.AddSingleton(translationHandler); _services.AddSingleton(_client); From 46fee88f039bf796f54278298151b0b9aee82656 Mon Sep 17 00:00:00 2001 From: runebaas Date: Mon, 1 Jun 2020 00:52:56 +0200 Subject: [PATCH 212/443] Add a migration script for role listeners --- Geekbot.net/Commands/Admin/Owner/Owner.cs | 79 ++++++++++++++++++- .../Lib/ReactionListener/ReactionListener.cs | 2 + 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/Geekbot.net/Commands/Admin/Owner/Owner.cs b/Geekbot.net/Commands/Admin/Owner/Owner.cs index 996d848..12f41a2 100644 --- a/Geekbot.net/Commands/Admin/Owner/Owner.cs +++ b/Geekbot.net/Commands/Admin/Owner/Owner.cs @@ -1,12 +1,16 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using Discord; using Discord.Commands; using Discord.WebSocket; +using Geekbot.net.Lib.AlmostRedis; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.GlobalSettings; using Geekbot.net.Lib.Logger; +using Geekbot.net.Lib.ReactionListener; using Geekbot.net.Lib.UserRepository; +using StackExchange.Redis; namespace Geekbot.net.Commands.Admin.Owner { @@ -17,16 +21,22 @@ namespace Geekbot.net.Commands.Admin.Owner private readonly DiscordSocketClient _client; private readonly IErrorHandler _errorHandler; private readonly IGlobalSettings _globalSettings; + private readonly IDatabase _redis; + private readonly IReactionListener _reactionListener; private readonly IGeekbotLogger _logger; private readonly IUserRepository _userRepository; - public Owner(DiscordSocketClient client, IGeekbotLogger logger, IUserRepository userRepositry, IErrorHandler errorHandler, IGlobalSettings globalSettings) + public Owner(DiscordSocketClient client, IGeekbotLogger logger, IUserRepository userRepositry, IErrorHandler errorHandler, IGlobalSettings globalSettings, + IAlmostRedis redis, IReactionListener reactionListener + ) { _client = client; _logger = logger; _userRepository = userRepositry; _errorHandler = errorHandler; _globalSettings = globalSettings; + _redis = redis.Db; + _reactionListener = reactionListener; } [Command("youtubekey", RunMode = RunMode.Async)] @@ -122,5 +132,72 @@ namespace Geekbot.net.Commands.Admin.Owner await _errorHandler.HandleCommandException(e, Context); } } + + [Command("migrate_listeners", RunMode = RunMode.Async)] + public async Task MigrateListeners() + { + try + { + var messageIds = _redis.SetMembers("MessageIds"); + var connectedGuilds = _client.Guilds; + var messageGuildAssociation = new Dictionary(); + foreach (var messageIdUnparsed in messageIds) + { + var messageId = ulong.Parse(messageIdUnparsed); + var reactions = _redis.HashGetAll($"Messages:{messageIdUnparsed.ToString()}"); + + foreach (var reaction in reactions) + { + _logger.Information(LogSource.Migration, $"{messageIdUnparsed.ToString()} - Starting"); + try + { + ulong guildId = 0; + IRole role = null; + + var roleId = ulong.Parse(reaction.Value); + if (messageGuildAssociation.ContainsKey(messageId)) + { + guildId = messageGuildAssociation[messageId]; + _logger.Information(LogSource.Migration, $"{messageIdUnparsed.ToString()} - known to be in {guildId}"); + role = _client.GetGuild(guildId).GetRole(roleId); + } + else + { + _logger.Information(LogSource.Migration, $"{messageIdUnparsed.ToString()} - Attempting to find guild"); + IRole foundRole = null; + try + { + foreach (var guild in connectedGuilds) + { + foundRole = guild.GetRole(roleId); + if (foundRole != null) + { + role = _client.GetGuild(foundRole.Guild.Id).GetRole(ulong.Parse(reaction.Value)); + messageGuildAssociation.Add(messageId, foundRole.Guild.Id); + } + } + } catch { /* ignore */ } + + if (foundRole == null) + { + _logger.Warning(LogSource.Migration, $"{messageIdUnparsed.ToString()} - Could not find guild for message"); + continue; + } + } + _logger.Information(LogSource.Migration, $"{messageIdUnparsed.ToString()} - Found Role {roleId.ToString()}"); + await _reactionListener.AddRoleToListener(ulong.Parse(messageIdUnparsed), guildId, reaction.Name, role); + } + catch (Exception e) + { + _logger.Error(LogSource.Migration, $"Failed to migrate reaction for {messageIdUnparsed.ToString()}", e); + } + } + } + } + catch (Exception e) + { + await _errorHandler.HandleCommandException(e, Context); + } + } } } \ No newline at end of file diff --git a/Geekbot.net/Lib/ReactionListener/ReactionListener.cs b/Geekbot.net/Lib/ReactionListener/ReactionListener.cs index 4a9a08c..bd335af 100644 --- a/Geekbot.net/Lib/ReactionListener/ReactionListener.cs +++ b/Geekbot.net/Lib/ReactionListener/ReactionListener.cs @@ -11,6 +11,7 @@ namespace Geekbot.net.Lib.ReactionListener public class ReactionListener : IReactionListener { private readonly DatabaseContext _database; + // private Dictionary> _listener; public ReactionListener(DatabaseContext database) @@ -50,6 +51,7 @@ namespace Geekbot.net.Lib.ReactionListener RoleId = role.Id.AsLong(), Reaction = emoji }); + await _database.SaveChangesAsync(); if (!_listener.ContainsKey(messageId)) { From 520633c590a7e1f51794145e18b12f1e3730eedb Mon Sep 17 00:00:00 2001 From: runebaas Date: Mon, 1 Jun 2020 01:02:36 +0200 Subject: [PATCH 213/443] Remove all redis leftovers --- Geekbot.net/Commands/Admin/Owner/Owner.cs | 79 +-------------------- Geekbot.net/Geekbot.net.csproj | 1 - Geekbot.net/Lib/AlmostRedis/AlmostRedis.cs | 35 --------- Geekbot.net/Lib/AlmostRedis/IAlmostRedis.cs | 13 ---- Geekbot.net/Lib/GeekbotExitCode.cs | 2 +- Geekbot.net/Lib/Logger/LogSource.cs | 1 - Geekbot.net/Lib/RunParameters.cs | 13 ---- Geekbot.net/Program.cs | 14 ---- 8 files changed, 2 insertions(+), 156 deletions(-) delete mode 100644 Geekbot.net/Lib/AlmostRedis/AlmostRedis.cs delete mode 100644 Geekbot.net/Lib/AlmostRedis/IAlmostRedis.cs diff --git a/Geekbot.net/Commands/Admin/Owner/Owner.cs b/Geekbot.net/Commands/Admin/Owner/Owner.cs index 12f41a2..996d848 100644 --- a/Geekbot.net/Commands/Admin/Owner/Owner.cs +++ b/Geekbot.net/Commands/Admin/Owner/Owner.cs @@ -1,16 +1,12 @@ using System; -using System.Collections.Generic; using System.Threading.Tasks; using Discord; using Discord.Commands; using Discord.WebSocket; -using Geekbot.net.Lib.AlmostRedis; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.GlobalSettings; using Geekbot.net.Lib.Logger; -using Geekbot.net.Lib.ReactionListener; using Geekbot.net.Lib.UserRepository; -using StackExchange.Redis; namespace Geekbot.net.Commands.Admin.Owner { @@ -21,22 +17,16 @@ namespace Geekbot.net.Commands.Admin.Owner private readonly DiscordSocketClient _client; private readonly IErrorHandler _errorHandler; private readonly IGlobalSettings _globalSettings; - private readonly IDatabase _redis; - private readonly IReactionListener _reactionListener; private readonly IGeekbotLogger _logger; private readonly IUserRepository _userRepository; - public Owner(DiscordSocketClient client, IGeekbotLogger logger, IUserRepository userRepositry, IErrorHandler errorHandler, IGlobalSettings globalSettings, - IAlmostRedis redis, IReactionListener reactionListener - ) + public Owner(DiscordSocketClient client, IGeekbotLogger logger, IUserRepository userRepositry, IErrorHandler errorHandler, IGlobalSettings globalSettings) { _client = client; _logger = logger; _userRepository = userRepositry; _errorHandler = errorHandler; _globalSettings = globalSettings; - _redis = redis.Db; - _reactionListener = reactionListener; } [Command("youtubekey", RunMode = RunMode.Async)] @@ -132,72 +122,5 @@ namespace Geekbot.net.Commands.Admin.Owner await _errorHandler.HandleCommandException(e, Context); } } - - [Command("migrate_listeners", RunMode = RunMode.Async)] - public async Task MigrateListeners() - { - try - { - var messageIds = _redis.SetMembers("MessageIds"); - var connectedGuilds = _client.Guilds; - var messageGuildAssociation = new Dictionary(); - foreach (var messageIdUnparsed in messageIds) - { - var messageId = ulong.Parse(messageIdUnparsed); - var reactions = _redis.HashGetAll($"Messages:{messageIdUnparsed.ToString()}"); - - foreach (var reaction in reactions) - { - _logger.Information(LogSource.Migration, $"{messageIdUnparsed.ToString()} - Starting"); - try - { - ulong guildId = 0; - IRole role = null; - - var roleId = ulong.Parse(reaction.Value); - if (messageGuildAssociation.ContainsKey(messageId)) - { - guildId = messageGuildAssociation[messageId]; - _logger.Information(LogSource.Migration, $"{messageIdUnparsed.ToString()} - known to be in {guildId}"); - role = _client.GetGuild(guildId).GetRole(roleId); - } - else - { - _logger.Information(LogSource.Migration, $"{messageIdUnparsed.ToString()} - Attempting to find guild"); - IRole foundRole = null; - try - { - foreach (var guild in connectedGuilds) - { - foundRole = guild.GetRole(roleId); - if (foundRole != null) - { - role = _client.GetGuild(foundRole.Guild.Id).GetRole(ulong.Parse(reaction.Value)); - messageGuildAssociation.Add(messageId, foundRole.Guild.Id); - } - } - } catch { /* ignore */ } - - if (foundRole == null) - { - _logger.Warning(LogSource.Migration, $"{messageIdUnparsed.ToString()} - Could not find guild for message"); - continue; - } - } - _logger.Information(LogSource.Migration, $"{messageIdUnparsed.ToString()} - Found Role {roleId.ToString()}"); - await _reactionListener.AddRoleToListener(ulong.Parse(messageIdUnparsed), guildId, reaction.Name, role); - } - catch (Exception e) - { - _logger.Error(LogSource.Migration, $"Failed to migrate reaction for {messageIdUnparsed.ToString()}", e); - } - } - } - } - catch (Exception e) - { - await _errorHandler.HandleCommandException(e, Context); - } - } } } \ No newline at end of file diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index baf326d..32555e7 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -25,7 +25,6 @@ - diff --git a/Geekbot.net/Lib/AlmostRedis/AlmostRedis.cs b/Geekbot.net/Lib/AlmostRedis/AlmostRedis.cs deleted file mode 100644 index c6faf52..0000000 --- a/Geekbot.net/Lib/AlmostRedis/AlmostRedis.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System.Collections.Generic; -using Geekbot.net.Lib.Logger; -using StackExchange.Redis; - -namespace Geekbot.net.Lib.AlmostRedis -{ - // if anyone ever sees this, please come up with a better fucking name, i'd appriciate it - public class AlmostRedis : IAlmostRedis - { - private readonly GeekbotLogger _logger; - private readonly RunParameters _runParameters; - - public AlmostRedis(GeekbotLogger logger, RunParameters runParameters) - { - _logger = logger; - _runParameters = runParameters; - } - - public void Connect() - { - Connection = ConnectionMultiplexer.Connect($"{_runParameters.RedisHost}:{_runParameters.RedisPort}"); - Db = Connection.GetDatabase(int.Parse(_runParameters.RedisDatabase)); - _logger.Information(LogSource.Redis, $"Connected to Redis on {Connection.Configuration} at {Db.Database}"); - } - - public IDatabase Db { get; private set; } - - public ConnectionMultiplexer Connection { get; private set; } - - public IEnumerable GetAllKeys() - { - return Connection.GetServer($"{_runParameters.RedisHost}:{_runParameters.RedisPort}").Keys(int.Parse(_runParameters.RedisDatabase)); - } - } -} \ No newline at end of file diff --git a/Geekbot.net/Lib/AlmostRedis/IAlmostRedis.cs b/Geekbot.net/Lib/AlmostRedis/IAlmostRedis.cs deleted file mode 100644 index 549c49e..0000000 --- a/Geekbot.net/Lib/AlmostRedis/IAlmostRedis.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System.Collections.Generic; -using StackExchange.Redis; - -namespace Geekbot.net.Lib.AlmostRedis -{ - public interface IAlmostRedis - { - void Connect(); - IDatabase Db { get; } - ConnectionMultiplexer Connection { get; } - IEnumerable GetAllKeys(); - } -} \ No newline at end of file diff --git a/Geekbot.net/Lib/GeekbotExitCode.cs b/Geekbot.net/Lib/GeekbotExitCode.cs index 130d435..4d91d18 100644 --- a/Geekbot.net/Lib/GeekbotExitCode.cs +++ b/Geekbot.net/Lib/GeekbotExitCode.cs @@ -10,7 +10,7 @@ TranslationsFailed = 201, // Dependent Services - RedisConnectionFailed = 301, + /* 301 not in use anymore (redis) */ DatabaseConnectionFailed = 302, // Discord Related diff --git a/Geekbot.net/Lib/Logger/LogSource.cs b/Geekbot.net/Lib/Logger/LogSource.cs index 7cd92ff..48ad7d0 100644 --- a/Geekbot.net/Lib/Logger/LogSource.cs +++ b/Geekbot.net/Lib/Logger/LogSource.cs @@ -10,7 +10,6 @@ namespace Geekbot.net.Lib.Logger Rest, Gateway, Discord, - Redis, Database, Message, UserRepository, diff --git a/Geekbot.net/Lib/RunParameters.cs b/Geekbot.net/Lib/RunParameters.cs index e16f644..e17825f 100644 --- a/Geekbot.net/Lib/RunParameters.cs +++ b/Geekbot.net/Lib/RunParameters.cs @@ -50,19 +50,6 @@ namespace Geekbot.net.Lib [Option("db-logging", Default = false, HelpText = "Enable database logging")] public bool DbLogging { get; set; } - /************************************ - * Redis * - ************************************/ - - [Option("redis-host", Default = "127.0.0.1", HelpText = "Set a redis host")] - public string RedisHost { get; set; } - - [Option("redis-port", Default = "6379", HelpText = "Set a redis port")] - public string RedisPort { get; set; } - - [Option("redis-database", Default = "6", HelpText = "Select a redis database (1-15)")] - public string RedisDatabase { get; set; } - /************************************ * WebApi * ************************************/ diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 389c051..d46df90 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -8,7 +8,6 @@ using Discord.Commands; using Discord.WebSocket; using Geekbot.net.Database; using Geekbot.net.Lib; -using Geekbot.net.Lib.AlmostRedis; using Geekbot.net.Lib.Clients; using Geekbot.net.Lib.Converters; using Geekbot.net.Lib.ErrorHandling; @@ -41,7 +40,6 @@ namespace Geekbot.net private GeekbotLogger _logger; private IUserRepository _userRepository; private RunParameters _runParameters; - private IAlmostRedis _redis; private static void Main(string[] args) { @@ -94,17 +92,6 @@ namespace Geekbot.net _globalSettings = new GlobalSettings(database); - try - { - _redis = new AlmostRedis(logger, runParameters); - _redis.Connect(); - } - catch (Exception e) - { - logger.Error(LogSource.Redis, "Redis Connection Failed", e); - Environment.Exit(GeekbotExitCode.RedisConnectionFailed.GetHashCode()); - } - _token = runParameters.Token ?? _globalSettings.GetKey("DiscordToken"); if (string.IsNullOrEmpty(_token)) { @@ -128,7 +115,6 @@ namespace Geekbot.net var randomNumberGenerator = new RandomNumberGenerator(); var kvMemoryStore = new KvInInMemoryStore(); - _services.AddSingleton(_redis); _services.AddSingleton(_userRepository); _services.AddSingleton(logger); _services.AddSingleton(levelCalc); From ff968a490f6b0ebf36235b861240e0245950c9b8 Mon Sep 17 00:00:00 2001 From: runebaas Date: Mon, 1 Jun 2020 01:13:45 +0200 Subject: [PATCH 214/443] Cleanup some dead code and database models --- Geekbot.net/Database/DatabaseContext.cs | 2 -- Geekbot.net/Database/Models/GuildsModel.cs | 24 ----------------- Geekbot.net/Database/Models/PollModel.cs | 27 ------------------- .../Database/Models/PollQuestionModel.cs | 16 ----------- Geekbot.net/Lib/RunParameters.cs | 9 ------- 5 files changed, 78 deletions(-) delete mode 100644 Geekbot.net/Database/Models/GuildsModel.cs delete mode 100644 Geekbot.net/Database/Models/PollModel.cs delete mode 100644 Geekbot.net/Database/Models/PollQuestionModel.cs diff --git a/Geekbot.net/Database/DatabaseContext.cs b/Geekbot.net/Database/DatabaseContext.cs index 56c19f8..505486f 100644 --- a/Geekbot.net/Database/DatabaseContext.cs +++ b/Geekbot.net/Database/DatabaseContext.cs @@ -7,7 +7,6 @@ namespace Geekbot.net.Database { public DbSet Quotes { get; set; } public DbSet Users { get; set; } - public DbSet Guilds { get; set; } public DbSet GuildSettings { get; set; } public DbSet Karma { get; set; } public DbSet Ships { get; set; } @@ -16,7 +15,6 @@ namespace Geekbot.net.Database public DbSet Slaps { get; set; } public DbSet Globals { get; set; } public DbSet RoleSelfService { get; set; } - public DbSet Polls { get; set; } public DbSet Cookies { get; set; } public DbSet ReactionListeners { get; set; } } diff --git a/Geekbot.net/Database/Models/GuildsModel.cs b/Geekbot.net/Database/Models/GuildsModel.cs deleted file mode 100644 index b6347a6..0000000 --- a/Geekbot.net/Database/Models/GuildsModel.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.ComponentModel.DataAnnotations; - -namespace Geekbot.net.Database.Models -{ - public class GuildsModel - { - [Key] - public int Id { get; set; } - - [Required] - public long GuildId { get; set; } - - [Required] - public string Name { get; set; } - - [Required] - public long Owner { get; set; } - - public string IconUrl { get; set; } - - public DateTimeOffset CreatedAt { get; set; } - } -} \ No newline at end of file diff --git a/Geekbot.net/Database/Models/PollModel.cs b/Geekbot.net/Database/Models/PollModel.cs deleted file mode 100644 index 3b12410..0000000 --- a/Geekbot.net/Database/Models/PollModel.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; - -namespace Geekbot.net.Database.Models -{ - public class PollModel - { - [Key] - public int Id { get; set; } - - [Required] - public long GuildId { get; set; } - - [Required] - public long ChannelId { get; set; } - - public string Question { get; set; } - - public long Creator { get; set; } - - public long MessageId { get; set; } - - public List Options { get; set; } - - public bool IsFinshed { get; set; } - } -} \ No newline at end of file diff --git a/Geekbot.net/Database/Models/PollQuestionModel.cs b/Geekbot.net/Database/Models/PollQuestionModel.cs deleted file mode 100644 index e251fe2..0000000 --- a/Geekbot.net/Database/Models/PollQuestionModel.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System.ComponentModel.DataAnnotations; - -namespace Geekbot.net.Database.Models -{ - public class PollQuestionModel - { - [Key] - public int Id { get; set; } - - public int OptionId { get; set; } - - public string OptionText { get; set; } - - public int Votes { get; set; } - } -} \ No newline at end of file diff --git a/Geekbot.net/Lib/RunParameters.cs b/Geekbot.net/Lib/RunParameters.cs index e17825f..e1386d3 100644 --- a/Geekbot.net/Lib/RunParameters.cs +++ b/Geekbot.net/Lib/RunParameters.cs @@ -59,14 +59,5 @@ namespace Geekbot.net.Lib [Option("api-port", Default = "12995", HelpText = "Port on which the WebApi listens")] public string ApiPort { get; set; } - - /************************************ - * Prometheus * - ************************************/ - [Option("prometheus-host", Default = "localhost", HelpText = "Host on which the Prometheus Metric Server listens")] - public string PrometheusHost { get; set; } - - [Option("prometheus-port", Default = "12991", HelpText = "Port on which the Prometheus Metric Server listens")] - public string PrometheusPort { get; set; } } } \ No newline at end of file From 3108a68407341a64b3ce3d07ef7419414ef35c3c Mon Sep 17 00:00:00 2001 From: runebaas Date: Mon, 1 Jun 2020 02:16:29 +0200 Subject: [PATCH 215/443] Dependency Upgrades --- Geekbot.net/Commands/Utils/Quote/Quote.cs | 2 +- Geekbot.net/Geekbot.net.csproj | 30 +++++++++---------- Geekbot.net/Lib/Extensions/DbSetExtensions.cs | 11 +++++++ Geekbot.net/Lib/Polyfills/UserPolyfillDto.cs | 2 ++ Tests/Tests.csproj | 6 ++-- 5 files changed, 32 insertions(+), 19 deletions(-) diff --git a/Geekbot.net/Commands/Utils/Quote/Quote.cs b/Geekbot.net/Commands/Utils/Quote/Quote.cs index 5c2bd3b..6288bef 100644 --- a/Geekbot.net/Commands/Utils/Quote/Quote.cs +++ b/Geekbot.net/Commands/Utils/Quote/Quote.cs @@ -252,7 +252,7 @@ namespace Geekbot.net.Commands.Utils.Quote try { var list = Context.Channel.GetMessagesAsync().Flatten(); - return await list.FirstOrDefault(msg => + return await list.FirstOrDefaultAsync(msg => msg.Author.Id == user.Id && msg.Embeds.Count == 0 && msg.Id != Context.Message.Id && diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 32555e7..0d39cae 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -20,28 +20,28 @@ true - - - - + + + + - - - - - - - - + + + + + + + + - - - + + + diff --git a/Geekbot.net/Lib/Extensions/DbSetExtensions.cs b/Geekbot.net/Lib/Extensions/DbSetExtensions.cs index c731fc0..e08b2d4 100644 --- a/Geekbot.net/Lib/Extensions/DbSetExtensions.cs +++ b/Geekbot.net/Lib/Extensions/DbSetExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using Microsoft.EntityFrameworkCore; @@ -13,5 +14,15 @@ namespace Geekbot.net.Lib.Extensions var exists = predicate != null ? dbSet.Any(predicate) : dbSet.Any(); return !exists ? dbSet.Add(entity) : null; } + + // https://github.com/dotnet/efcore/issues/18124 + public static IAsyncEnumerable AsAsyncEnumerable(this Microsoft.EntityFrameworkCore.DbSet obj) where TEntity : class + { + return Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.AsAsyncEnumerable(obj); + } + public static IQueryable Where(this Microsoft.EntityFrameworkCore.DbSet obj, System.Linq.Expressions.Expression> predicate) where TEntity : class + { + return System.Linq.Queryable.Where(obj, predicate); + } } } \ No newline at end of file diff --git a/Geekbot.net/Lib/Polyfills/UserPolyfillDto.cs b/Geekbot.net/Lib/Polyfills/UserPolyfillDto.cs index 6d81581..32ea663 100644 --- a/Geekbot.net/Lib/Polyfills/UserPolyfillDto.cs +++ b/Geekbot.net/Lib/Polyfills/UserPolyfillDto.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Immutable; using System.Threading.Tasks; using Discord; @@ -11,6 +12,7 @@ namespace Geekbot.net.Lib.Polyfills public string Mention { get; set; } public IActivity Activity { get; } public UserStatus Status { get; set; } + public IImmutableSet ActiveClients { get; } public string AvatarId { get; set; } public string Discriminator { get; set; } public ushort DiscriminatorValue { get; set; } diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 235cc0b..345dacd 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -6,9 +6,9 @@ xUnit1026 - - - + + + From 6692b3bc778bdb90db6a737e44ecddeae1cd7431 Mon Sep 17 00:00:00 2001 From: runebaas Date: Mon, 1 Jun 2020 02:17:15 +0200 Subject: [PATCH 216/443] Don't show a quote id when using '!quote make' --- Geekbot.net/Commands/Utils/Quote/Quote.cs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Geekbot.net/Commands/Utils/Quote/Quote.cs b/Geekbot.net/Commands/Utils/Quote/Quote.cs index 6288bef..7af8a4c 100644 --- a/Geekbot.net/Commands/Utils/Quote/Quote.cs +++ b/Geekbot.net/Commands/Utils/Quote/Quote.cs @@ -270,7 +270,14 @@ namespace Geekbot.net.Commands.Utils.Quote var user = Context.Client.GetUserAsync(quote.UserId.AsUlong()).Result ?? new UserPolyfillDto { Username = "Unknown User" }; var eb = new EmbedBuilder(); eb.WithColor(new Color(143, 167, 232)); - eb.Title = $"#{quote.InternalId} | {user.Username} @ {quote.Time.Day}.{quote.Time.Month}.{quote.Time.Year}"; + if (quote.InternalId == 0) + { + eb.Title = $"{user.Username} @ {quote.Time.Day}.{quote.Time.Month}.{quote.Time.Year}"; + } + else + { + eb.Title = $"#{quote.InternalId} | {user.Username} @ {quote.Time.Day}.{quote.Time.Month}.{quote.Time.Year}"; + } eb.Description = quote.Quote; eb.ThumbnailUrl = user.GetAvatarUrl(); if (quote.Image != null) eb.ImageUrl = quote.Image; @@ -289,9 +296,8 @@ namespace Geekbot.net.Commands.Utils.Quote image = null; } - var last = _database.Quotes.Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong())) - .OrderByDescending(e => e.InternalId).FirstOrDefault(); - var internalId = 1; + var last = _database.Quotes.Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong())).OrderByDescending(e => e.InternalId).FirstOrDefault(); + var internalId = 0; if (last != null) internalId = last.InternalId + 1; return new QuoteModel() { From d91c21e607f4aeb7be3eee88106319b22914aef0 Mon Sep 17 00:00:00 2001 From: runebaas Date: Mon, 1 Jun 2020 02:20:27 +0200 Subject: [PATCH 217/443] update readme --- readme.md | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/readme.md b/readme.md index 7d8544f..ffc320c 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,4 @@ -[![pipeline status](https://git.boerlage.me/open/Geekbot.net/badges/master/pipeline.svg)](https://git.boerlage.me/open/Geekbot.net/commits/master) +[![pipeline status](https://gitlab.com/dbgit/open/geekbot/badges/master/pipeline.svg)](https://gitlab.com/dbgit/open/geekbot/commits/master) # [Geekbot.net](https://geekbot.pizzaandcoffee.rocks/) @@ -8,22 +8,19 @@ You can invite Geekbot to your server with [this link](https://discordapp.com/oa ## Technologies -* DotNet Core 2 -* Redis +* DotNet Core 3.1 +* PostgreSQL * Discord.net ## Running -Make sure redis is running +You can start geekbot with: `dotnet run` -Run these commands +On your first run geekbot will ask for your bot token. -* `dotnet restore` -* `dotnet run` +You might need to pass some additional configuration (e.g. database credentials), these can be passed as commandline arguments. -On your first run geekbot will ask for your bot token, everything else is taken care of. - -For a list of launch options use `dotnet run -h` +For a list of commandline arguments use `dotnet run -- -h` ## Contributing From 8018d5e7501d09a32186a4fe5bd374ab9bb2b957 Mon Sep 17 00:00:00 2001 From: runebaas Date: Mon, 1 Jun 2020 15:22:41 +0200 Subject: [PATCH 218/443] Allow configuration to be passed as environment variables --- Geekbot.net/Lib/RunParameters.cs | 98 +++++++++++++++++++++----------- readme.md | 6 +- 2 files changed, 70 insertions(+), 34 deletions(-) diff --git a/Geekbot.net/Lib/RunParameters.cs b/Geekbot.net/Lib/RunParameters.cs index e1386d3..1ddbd46 100644 --- a/Geekbot.net/Lib/RunParameters.cs +++ b/Geekbot.net/Lib/RunParameters.cs @@ -1,4 +1,5 @@ -using CommandLine; +using System; +using CommandLine; namespace Geekbot.net.Lib { @@ -8,56 +9,89 @@ namespace Geekbot.net.Lib * General * ************************************/ - [Option('V', "verbose", Default = false, HelpText = "Logs everything.")] - public bool Verbose { get; set; } + [Option("token", HelpText = "Set a new bot token. By default it will use your previous bot token which was stored in the database (default: null) (env: TOKEN)")] + public string Token { get; set; } = ParamFallback("TOKEN"); - [Option('j', "log-json", Default = false, HelpText = "Logger outputs json")] - public bool LogJson { get; set; } + [Option('V', "verbose", HelpText = "Logs everything. (default: false) (env: LOG_VERBOSE)")] + public bool Verbose { get; set; } = ParamFallback("LOG_VERBOSE", false); - [Option('a', "disable-api", Default = false, HelpText = "Disables the web api")] - public bool DisableApi { get; set; } + [Option('j', "log-json", HelpText = "Logger outputs json (default: false ) (env: LOG_JSON)")] + public bool LogJson { get; set; } = ParamFallback("LOG_JSON", false); + + [Option('e', "expose-errors", HelpText = "Shows internal errors in the chat (default: false) (env: EXPOSE_ERRORS)")] + public bool ExposeErrors { get; set; } = ParamFallback("EXPOSE_ERRORS", false); - [Option('e', "expose-errors", Default = false, HelpText = "Shows internal errors in the chat")] - public bool ExposeErrors { get; set; } - - [Option("token", Default = null, HelpText = "Set a new bot token")] - public string Token { get; set; } - /************************************ * Database * ************************************/ - [Option("in-memory", Default = false, HelpText = "Uses the in-memory database instead of postgresql")] - public bool InMemory { get; set; } + [Option("in-memory", HelpText = "Uses the in-memory database instead of postgresql (default: false) (env: DB_INMEMORY)")] + public bool InMemory { get; set; } = ParamFallback("DB_INMEMORY", false); // Postresql connection - [Option("database", Default = "geekbot", HelpText = "Select a postgresql database")] - public string DbDatabase { get; set; } + [Option("database", HelpText = "Select a postgresql database (default: geekbot) (env: DB_DATABASE)")] + public string DbDatabase { get; set; } = ParamFallback("DB_DATABASE", "geekbot"); - [Option("db-host", Default = "localhost", HelpText = "Set a postgresql host (e.g. 127.0.0.1)")] - public string DbHost { get; set; } + [Option("db-host", HelpText = "Set a postgresql host (default: localhost) (env: DB_HOST)")] + public string DbHost { get; set; } = ParamFallback("DB_HOST", "localhost"); - [Option("db-port", Default = "5432", HelpText = "Set a postgresql host (e.g. 5432)")] - public string DbPort { get; set; } + [Option("db-port", HelpText = "Set a postgresql host (default: 5432) (env: DB_PORT)")] + public string DbPort { get; set; } = ParamFallback("DB_PORT", "5432"); - [Option("db-user", Default = "geekbot", HelpText = "Set a postgresql user")] - public string DbUser { get; set; } + [Option("db-user", HelpText = "Set a postgresql user (default: geekbot) (env: DB_USER)")] + public string DbUser { get; set; } = ParamFallback("DB_USER", "geekbot"); - [Option("db-password", Default = "", HelpText = "Set a posgresql password")] - public string DbPassword { get; set; } + [Option("db-password", HelpText = "Set a posgresql password (default: empty) (env: DB_PASSWORD)")] + public string DbPassword { get; set; } = ParamFallback("DB_PASSWORD", ""); // Logging - [Option("db-logging", Default = false, HelpText = "Enable database logging")] - public bool DbLogging { get; set; } + [Option("db-logging", HelpText = "Enable database logging (default: false) (env: DB_LOGGING)")] + public bool DbLogging { get; set; } = ParamFallback("DB_LOGGING", false); /************************************ * WebApi * ************************************/ - - [Option("api-host", Default = "localhost", HelpText = "Host on which the WebApi listens")] - public string ApiHost { get; set; } - [Option("api-port", Default = "12995", HelpText = "Port on which the WebApi listens")] - public string ApiPort { get; set; } + [Option('a', "disable-api", HelpText = "Disables the WebApi (default: false) (env: API_DISABLE)")] + public bool DisableApi { get; set; } = ParamFallback("API_DISABLE", false); + + [Option("api-host", HelpText = "Host on which the WebApi listens (default: localhost) (env: API_HOST)")] + public string ApiHost { get; set; } = ParamFallback("API_HOST", "localhost"); + + [Option("api-port", HelpText = "Port on which the WebApi listens (default: 12995) (env: API_PORT)")] + public string ApiPort { get; set; } = ParamFallback("API_PORT", "12995"); + + /************************************ + * Helper Functions * + ************************************/ + + private static string ParamFallback(string key, string defaultValue = null) + { + var envVar = GetEnvironmentVariable(key); + return !string.IsNullOrEmpty(envVar) ? envVar : defaultValue; + } + + private static bool ParamFallback(string key, bool defaultValue) + { + var envVar = GetEnvironmentVariable(key); + if (!string.IsNullOrEmpty(envVar)) + { + return envVar.ToLower() switch + { + "true" => true, + "1" => true, + "false" => false, + "0" => false, + _ => defaultValue + }; + } + + return defaultValue; + } + + private static string GetEnvironmentVariable(string name) + { + return Environment.GetEnvironmentVariable($"GEEKBOT_{name}"); + } } } \ No newline at end of file diff --git a/readme.md b/readme.md index ffc320c..c7f3f33 100644 --- a/readme.md +++ b/readme.md @@ -18,9 +18,11 @@ You can start geekbot with: `dotnet run` On your first run geekbot will ask for your bot token. -You might need to pass some additional configuration (e.g. database credentials), these can be passed as commandline arguments. +You might need to pass some additional configuration (e.g. database credentials), these can be passed as commandline arguments or environment variables. -For a list of commandline arguments use `dotnet run -- -h` +For a list of commandline arguments and environment variables use `dotnet run -- -h` + +All Environment Variables must be prefixed with `GEEKBOT_` ## Contributing From 0898e9b6bd9e6c135e951e0a1a0c89fbb6df00c6 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 14 Jun 2020 17:27:07 +0200 Subject: [PATCH 219/443] Add the timeout in !roll back --- Geekbot.net/Commands/Games/{ => Roll}/Roll.cs | 19 ++++++++++--------- .../Commands/Games/Roll/RollTimeout.cs | 10 ++++++++++ Geekbot.net/Lib/Localization/Translations.yml | 4 ++-- 3 files changed, 22 insertions(+), 11 deletions(-) rename Geekbot.net/Commands/Games/{ => Roll}/Roll.cs (83%) create mode 100644 Geekbot.net/Commands/Games/Roll/RollTimeout.cs diff --git a/Geekbot.net/Commands/Games/Roll.cs b/Geekbot.net/Commands/Games/Roll/Roll.cs similarity index 83% rename from Geekbot.net/Commands/Games/Roll.cs rename to Geekbot.net/Commands/Games/Roll/Roll.cs index 076cf8f..1c89f19 100644 --- a/Geekbot.net/Commands/Games/Roll.cs +++ b/Geekbot.net/Commands/Games/Roll/Roll.cs @@ -10,7 +10,7 @@ using Geekbot.net.Lib.KvInMemoryStore; using Geekbot.net.Lib.Localization; using Geekbot.net.Lib.RandomNumberGenerator; -namespace Geekbot.net.Commands.Games +namespace Geekbot.net.Commands.Games.Roll { public class Roll : ModuleBase { @@ -42,17 +42,18 @@ namespace Geekbot.net.Commands.Games { var kvKey = $"{Context.Guild.Id}:{Context.User.Id}:RollsPrevious"; - var prevRoll = _kvInMemoryStore.Get(kvKey); - if (prevRoll > 0) + var prevRoll = _kvInMemoryStore.Get(kvKey); + + if (prevRoll?.LastGuess == guess && prevRoll?.GuessedOn.AddDays(1) > DateTime.Now) { - if (prevRoll == guess) - { - await ReplyAsync(transContext.GetString("NoPrevGuess", Context.Message.Author.Mention)); - return; - } + await ReplyAsync(transContext.GetString( + "NoPrevGuess", + Context.Message.Author.Mention, + transContext.FormatDateTimeAsRemaining(prevRoll.GuessedOn.AddDays(1)))); + return; } - _kvInMemoryStore.Set(kvKey, guess); + _kvInMemoryStore.Set(kvKey, new RollTimeout { LastGuess = guess, GuessedOn = DateTime.Now }); await ReplyAsync(transContext.GetString("Rolled", Context.Message.Author.Mention, number, guess)); if (guess == number) diff --git a/Geekbot.net/Commands/Games/Roll/RollTimeout.cs b/Geekbot.net/Commands/Games/Roll/RollTimeout.cs new file mode 100644 index 0000000..81a520f --- /dev/null +++ b/Geekbot.net/Commands/Games/Roll/RollTimeout.cs @@ -0,0 +1,10 @@ +using System; + +namespace Geekbot.net.Commands.Games.Roll +{ + public class RollTimeout + { + public int LastGuess { get; set; } + public DateTime GuessedOn { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/Localization/Translations.yml b/Geekbot.net/Lib/Localization/Translations.yml index a19a6f3..3c59977 100644 --- a/Geekbot.net/Lib/Localization/Translations.yml +++ b/Geekbot.net/Lib/Localization/Translations.yml @@ -83,8 +83,8 @@ roll: EN: "{0}, you rolled {1}" CHDE: "{0}, du hesch {1} grollt" NoPrevGuess: - EN: ":red_circle: {0}, you can't guess the same number again" - CHDE: ":red_circle: {0}, du chasch nid nomol es gliche rate" + EN: ":red_circle: {0}, you can't guess the same number again, guess another number or wait {1}" + CHDE: ":red_circle: {0}, du chasch nid nomol es gliche rate, rate öppis anders oder warte {1}" cookies: &cookiesAlias GetCookies: EN: "You got {0} cookies, there are now {1} cookies in you cookie jar" From 12199f4ad475554be327dada2e2369fd428ab9e7 Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 18 Jun 2020 19:19:02 +0200 Subject: [PATCH 220/443] Add greetings in 299 languages --- .../Randomness/Greetings/GreetingDto.cs | 10 + .../Randomness/Greetings/GreetingProvider.cs | 1600 +++++++++++++++++ .../Randomness/Greetings/Greetings.cs | 50 + 3 files changed, 1660 insertions(+) create mode 100644 Geekbot.net/Commands/Randomness/Greetings/GreetingDto.cs create mode 100644 Geekbot.net/Commands/Randomness/Greetings/GreetingProvider.cs create mode 100644 Geekbot.net/Commands/Randomness/Greetings/Greetings.cs diff --git a/Geekbot.net/Commands/Randomness/Greetings/GreetingDto.cs b/Geekbot.net/Commands/Randomness/Greetings/GreetingDto.cs new file mode 100644 index 0000000..42792a5 --- /dev/null +++ b/Geekbot.net/Commands/Randomness/Greetings/GreetingDto.cs @@ -0,0 +1,10 @@ +namespace Geekbot.net.Commands.Randomness.Greetings +{ + public class GreetingDto + { + public string Text { get; set; } + public string Language { get; set; } + public string Dialect { get; set; } = null; + public string Romanization { get; set; } = null; + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/Randomness/Greetings/GreetingProvider.cs b/Geekbot.net/Commands/Randomness/Greetings/GreetingProvider.cs new file mode 100644 index 0000000..52d8d18 --- /dev/null +++ b/Geekbot.net/Commands/Randomness/Greetings/GreetingProvider.cs @@ -0,0 +1,1600 @@ +using System.Collections.Generic; + +namespace Geekbot.net.Commands.Randomness.Greetings +{ + public class GreetingProvider + { + public static readonly List Greetings = new List + { + new GreetingDto() + { + Text = "Бзиа збаша", + Language = "Abkhaz", + Romanization = "Bzia zbaşa" + }, + new GreetingDto() + { + Text = "Фэсапщы", + Language = "Adyghe", + Romanization = "Fèsapŝy" + }, + new GreetingDto() + { + Text = "Haai", + Language = "Afrikaans" + }, + new GreetingDto() + { + Text = "Kamusta", + Language = "Aklan" + }, + new GreetingDto() + { + Text = "Fâla", + Language = "Albanian" + }, + new GreetingDto() + { + Text = "Ç'kemi", + Language = "Albanian" + }, + new GreetingDto() + { + Text = "Aang", + Language = "Aleut" + }, + new GreetingDto() + { + Text = "Hallo", + Language = "Alsatian" + }, + new GreetingDto() + { + Text = "Эзендер", + Language = "Altay", + Romanization = "Ezender" + }, + new GreetingDto() + { + Text = "ሰላም።", + Language = "Amharic", + Romanization = "sälam" + }, + new GreetingDto() + { + Text = "السلام عليكم", + Language = "Arabic", + Romanization = "as-salām 'alaykum" + }, + new GreetingDto() + { + Text = "Ola", + Language = "Aragonese" + }, + new GreetingDto() + { + Text = "Shl'am lak", + Language = "Aramaic" + }, + new GreetingDto() + { + Text = "Héébee", + Language = "Arapaho" + }, + new GreetingDto() + { + Text = "Բարեւ", + Language = "Armenian", + Romanization = "Parev" + }, + new GreetingDto() + { + Text = "Bunâ dzuâ", + Language = "Aromanian" + }, + new GreetingDto() + { + Text = "Werte", + Language = "Arrernte" + }, + new GreetingDto() + { + Text = "নমস্কাৰ", + Language = "Assamese", + Romanization = "নমস্কাৰ" + }, + new GreetingDto() + { + Text = "Hola", + Language = "Asturian" + }, + new GreetingDto() + { + Text = "Kwei", + Language = "Atikamekw" + }, + new GreetingDto() + { + Text = "Laphi!", + Language = "Aymara" + }, + new GreetingDto() + { + Text = "Salam", + Language = "Azerbaijani" + }, + new GreetingDto() + { + Text = "Swastyastu", + Language = "Balinese" + }, + new GreetingDto() + { + Text = "I ni ce", + Language = "Bambara" + }, + new GreetingDto() + { + Text = "Haku'", + Language = "Barbareño" + }, + new GreetingDto() + { + Text = "сәләм", + Language = "Bashkir", + Romanization = "sәlәm" + }, + new GreetingDto() + { + Text = "Kaixo", + Language = "Basque" + }, + new GreetingDto() + { + Text = "Horas", + Language = "Batak" + }, + new GreetingDto() + { + Text = "მარშიხ ვალ", + Language = "Batsbi", + Romanization = "Maršix val" + }, + new GreetingDto() + { + Text = "Seavus", + Language = "Bavarian" + }, + new GreetingDto() + { + Text = "Вітаю", + Language = "Belarusian", + Romanization = "Vіtaju" + }, + new GreetingDto() + { + Text = "Shani", + Language = "Bemba" + }, + new GreetingDto() + { + Text = "নমস্কার", + Language = "Bengali", + Romanization = "Nômôskar", + Dialect = "Hindus" + }, + new GreetingDto() + { + Text = "আসসালামু আলাইকুম", + Language = "Bengali", + Romanization = "Asôlamu alaikum", + Dialect = "Muslims" + }, + new GreetingDto() + { + Text = "प्रणाम", + Language = "Bhojpuri", + Romanization = "prannam" + }, + new GreetingDto() + { + Text = "Hello", + Language = "Bikol" + }, + new GreetingDto() + { + Text = "Halo", + Language = "Bislama" + }, + new GreetingDto() + { + Text = "Dobar dan", + Language = "Bosnian" + }, + new GreetingDto() + { + Text = "Salud", + Language = "Breton" + }, + new GreetingDto() + { + Text = "Здравейте", + Language = "Bulgarian" + }, + new GreetingDto() + { + Text = "မဂႆလာပၝ", + Language = "Burmese", + Romanization = "min-ga-la-ba" + }, + new GreetingDto() + { + Text = "Olá", + Language = "Cape Verdean Creole" + }, + new GreetingDto() + { + Text = "Hola", + Language = "Catalan" + }, + new GreetingDto() + { + Text = "Hello", + Language = "Cebuano" + }, + new GreetingDto() + { + Text = "Kopisanangan", + Language = "Central Dusun" + }, + new GreetingDto() + { + Text = "Que tal?", + Language = "Chabacano de Zamboanga" + }, + new GreetingDto() + { + Text = "Ola", + Language = "Chabacano de Cavite" + }, + new GreetingDto() + { + Text = "Håfa ådai", + Language = "Chamorro", + Dialect = "Guam" + }, + new GreetingDto() + { + Text = "Hafa Adai", + Language = "Chamorro", + Dialect = "North Marianas" + }, + new GreetingDto() + { + Text = "Салам", + Language = "Chechen", + Romanization = "Salam" + }, + new GreetingDto() + { + Text = "ᎣᏏᏲ", + Language = "Cherokee", + Romanization = "Osiyo" + }, + new GreetingDto() + { + Text = "Moni", + Language = "Chichewa" + }, + new GreetingDto() + { + Text = "你好", + Language = "Chinese", + Romanization = "nǐ hǎo", + Dialect = "Mandarin" + }, + new GreetingDto() + { + Text = "ʔédlánet’é", + Language = "Chipewyan" + }, + new GreetingDto() + { + Text = "Halito", + Language = "Choctaw" + }, + new GreetingDto() + { + Text = "Ran annim", + Language = "Chuukese" + }, + new GreetingDto() + { + Text = "Салам!", + Language = "Chuvash", + Romanization = "Salam!" + }, + new GreetingDto() + { + Text = "Guuten takh!", + Language = "Cimbrian" + }, + new GreetingDto() + { + Text = "Kopivosian", + Language = "Coastal Kadazan" + }, + new GreetingDto() + { + Text = "Marʉ́awe", + Language = "Comanche" + }, + new GreetingDto() + { + Text = "Dydh da", + Language = "Cornish" + }, + new GreetingDto() + { + Text = "Salute", + Language = "Corsican" + }, + new GreetingDto() + { + Text = "ᑕᓂᓯ", + Language = "Cree", + Romanization = "Tanisi" + }, + new GreetingDto() + { + Text = "Bok", + Language = "Croatian" + }, + new GreetingDto() + { + Text = "Sho'daache", + Language = "Crow" + }, + new GreetingDto() + { + Text = "Míyaxwe", + Language = "Cupeño" + }, + new GreetingDto() + { + Text = "Hello", + Language = "Cuyonon" + }, + new GreetingDto() + { + Text = "Ahoj", + Language = "Czech" + }, + new GreetingDto() + { + Text = "Hej", + Language = "Danish" + }, + new GreetingDto() + { + Text = "ااسال م عليكم", + Language = "Dari", + Romanization = "As-salâmo 'alaykom" + }, + new GreetingDto() + { + Text = "Misawa", + Language = "Dholuo" + }, + new GreetingDto() + { + Text = "Goededag", + Language = "Dutch" + }, + new GreetingDto() + { + Text = "སྐུ་གཟུགས་བཟང་པོ་ལགས།", + Language = "Dzongkha", + Romanization = "Kuzu zangpo la" + }, + new GreetingDto() + { + Text = "Mọkọm", + Language = "Efik" + }, + new GreetingDto() + { + Text = "Häj ą̊ dig!", + Language = "Elfdalian" + }, + new GreetingDto() + { + Text = "Hello", + Language = "English" + }, + new GreetingDto() + { + Text = "Tere", + Language = "Estonian" + }, + new GreetingDto() + { + Text = "Ola!", + Language = "Extremaduran" + }, + new GreetingDto() + { + Text = "Hallo", + Language = "Faroese" + }, + new GreetingDto() + { + Text = "Bula", + Language = "Fijian" + }, + new GreetingDto() + { + Text = "Hyvää päivää", + Language = "Finnish" + }, + new GreetingDto() + { + Text = "Bonjour", + Language = "French" + }, + new GreetingDto() + { + Text = "Guundach", + Language = "Frisian", + Dialect = "North Frisian" + }, + new GreetingDto() + { + Text = "Gouden Dai", + Language = "Frisian", + Dialect = "Saterland" + }, + new GreetingDto() + { + Text = "Goeie", + Language = "Frisian", + Dialect = "West Frisian" + }, + new GreetingDto() + { + Text = "Mandi", + Language = "Friulian" + }, + new GreetingDto() + { + Text = "Ola", + Language = "Galician" + }, + new GreetingDto() + { + Text = "सिवासौँळी", + Language = "Garhwali", + Romanization = "sivāsauṁḻī" + }, + new GreetingDto() + { + Text = "Buiti binafi", + Language = "Garifuna" + }, + new GreetingDto() + { + Text = "გამარჯობა", + Language = "Georgian", + Romanization = "gamarjoba" + }, + new GreetingDto() + { + Text = "Guten Tag", + Language = "German" + }, + new GreetingDto() + { + Text = "Χαῖρε", + Language = "Greek", + Romanization = "Khaĩre", + Dialect = "Ancient" + }, + new GreetingDto() + { + Text = "Γειά", + Language = "Greek", + Romanization = "Geiá", + Dialect = "Ancient" + }, + new GreetingDto() + { + Text = "something", + Language = "something" + }, + new GreetingDto() + { + Text = "Aluu", + Language = "Greenlandic" + }, + new GreetingDto() + { + Text = "નમસ્તે", + Language = "Gujarati", + Romanization = "namaste" + }, + new GreetingDto() + { + Text = "Bonjou", + Language = "Haitian Creole" + }, + new GreetingDto() + { + Text = "Sannu", + Language = "Hausa" + }, + new GreetingDto() + { + Text = "Aloha", + Language = "Hawaiian" + }, + new GreetingDto() + { + Text = "שלום", + Language = "Hebrew", + Romanization = "Shalom" + }, + new GreetingDto() + { + Text = "something", + Language = "something" + }, + new GreetingDto() + { + Text = "Tjike", + Language = "Herero" + }, + new GreetingDto() + { + Text = "Mono", + Language = "Himba" + }, + new GreetingDto() + { + Text = "नमस्ते", + Language = "Hindi", + Romanization = "namaste" + }, + new GreetingDto() + { + Text = "Nyob zoo", + Language = "Hmong", + Dialect = "White" + }, + new GreetingDto() + { + Text = "Jó napot kívánok", + Language = "Hungarian" + }, + new GreetingDto() + { + Text = "Góðan dag", + Language = "Icelandic" + }, + new GreetingDto() + { + Text = "Ndeewo", + Language = "Igbo" + }, + new GreetingDto() + { + Text = "Kablaaw", + Language = "Iloko" + }, + new GreetingDto() + { + Text = "Tiervâ", + Language = "Inari Saami" + }, + new GreetingDto() + { + Text = "Hi", + Language = "Indonesian" + }, + new GreetingDto() + { + Text = "ᐊᐃ", + Language = "Inuktitut", + Romanization = "Ai" + }, + new GreetingDto() + { + Text = "Haluu", + Language = "Iñupiaq" + }, + new GreetingDto() + { + Text = "Dia dhuit", + Language = "Irish" + }, + new GreetingDto() + { + Text = "Ciao", + Language = "Italian" + }, + new GreetingDto() + { + Text = "Míyaxwen", + Language = "Ivilyuat" + }, + new GreetingDto() + { + Text = "Ello", + Language = "Jamaican" + }, + new GreetingDto() + { + Text = "今日は", + Language = "Japanese", + Romanization = "konnichiwa" + }, + new GreetingDto() + { + Text = "ꦲꦭꦺꦴ", + Language = "Javanese", + Romanization = "Halo" + }, + new GreetingDto() + { + Text = "Puiznu", + Language = "Jenesch" + }, + new GreetingDto() + { + Text = "Salut", + Language = "Jèrriais" + }, + new GreetingDto() + { + Text = "Godaw", + Language = "Jutish" + }, + new GreetingDto() + { + Text = "Мендвт", + Language = "Kalmyk" + }, + new GreetingDto() + { + Text = "Tọi", + Language = "Kam" + }, + new GreetingDto() + { + Text = "ನಮಸ್ತೆ", + Language = "Kannada", + Romanization = "namaste" + }, + new GreetingDto() + { + Text = "Xsaqär", + Language = "Kaqchikel" + }, + new GreetingDto() + { + Text = "Ayukîi", + Language = "Karuk" + }, + new GreetingDto() + { + Text = "Witéj", + Language = "Kashubian" + }, + new GreetingDto() + { + Text = "असलामु अलैकुम", + Language = "Kashmiri", + Romanization = "asalāmu alaikuma" + }, + new GreetingDto() + { + Text = "Hagare'enaam", + Language = "Kawaiisu" + }, + new GreetingDto() + { + Text = "Сәлем", + Language = "Kazakh", + Romanization = "Sälem" + }, + new GreetingDto() + { + Text = "ជំរាបសួរ", + Language = "Khmer", + Romanization = "chôm rab suôr" + }, + new GreetingDto() + { + Text = "Halau", + Language = "Khoekhoe" + }, + new GreetingDto() + { + Text = "wĩmwega", + Language = "Kikuyu" + }, + new GreetingDto() + { + Text = "Muraho", + Language = "Kinyarwanda" + }, + new GreetingDto() + { + Text = "Ko na mauri", + Language = "Kiribati" + }, + new GreetingDto() + { + Text = "안녕하십니까", + Language = "Korean", + Romanization = "annyeong-hasimnikka" + }, + new GreetingDto() + { + Text = "Haawka", + Language = "Kumeyaay" + }, + new GreetingDto() + { + Text = "!kao", + Language = "Kung San" + }, + new GreetingDto() + { + Text = "Rojbash", + Language = "Kurdish", + Dialect = "Kurmanji" + }, + new GreetingDto() + { + Text = "Sillaw", + Language = "Kurdish", + Dialect = "Sorani" + }, + new GreetingDto() + { + Text = "Салам!", + Language = "Kyrgyz", + Romanization = "Salam!" + }, + new GreetingDto() + { + Text = "בוינוס דייאס", + Language = "Ladino", + Romanization = "Buenos diyas" + }, + new GreetingDto() + { + Text = "Hau", + Language = "Lakota Sioux" + }, + new GreetingDto() + { + Text = "ສະບາຍດີ", + Language = "Lao", + Romanization = "sába̖ai-di̖i" + }, + new GreetingDto() + { + Text = "Ave", + Language = "Latin" + }, + new GreetingDto() + { + Text = "Sveiki", + Language = "Latvian" + }, + new GreetingDto() + { + Text = "გეგაჯგინას", + Language = "Laz", + Romanization = "Gegacginas" + }, + new GreetingDto() + { + Text = "Салам алейкум", + Language = "Lezgi", + Romanization = "Salam alejkum" + }, + new GreetingDto() + { + Text = "Hallo", + Language = "Limburgish" + }, + new GreetingDto() + { + Text = "Mbote", + Language = "Lingala" + }, + new GreetingDto() + { + Text = "Labas", + Language = "Lithuanian" + }, + new GreetingDto() + { + Text = "Moin", + Language = "Low Saxon" + }, + new GreetingDto() + { + Text = "Lumela", + Language = "Lozi" + }, + new GreetingDto() + { + Text = "Gyebale ko", + Language = "Luganda" + }, + new GreetingDto() + { + Text = "Buoris", + Language = "Lule Sámi" + }, + new GreetingDto() + { + Text = "Mííyu", + Language = "Luiseño" + }, + new GreetingDto() + { + Text = "Moien", + Language = "Luxembourgish" + }, + new GreetingDto() + { + Text = "Здраво", + Language = "Macedonian", + Romanization = "Zdravo" + }, + new GreetingDto() + { + Text = "परनाम", + Language = "Magahi", + Romanization = "Pernaam" + }, + new GreetingDto() + { + Text = "प्रनाम", + Language = "Maithili", + Romanization = "Pranaam" + }, + new GreetingDto() + { + Text = "Manao ahoana", + Language = "Malagasy" + }, + new GreetingDto() + { + Text = "Selamat pagi", + Language = "Malay" + }, + new GreetingDto() + { + Text = "നമസ്തേ", + Language = "Malayalam", + Romanization = "namastē" + }, + new GreetingDto() + { + Text = "Jeeka, ma tzuula", + Language = "Mam" + }, + new GreetingDto() + { + Text = "Moghrey mie", + Language = "Gaelic", + Dialect = "Manx" + }, + new GreetingDto() + { + Text = "Assalaamu Alaikum", + Language = "Maldivian" + }, + new GreetingDto() + { + Text = "Ħello", + Language = "Maltese" + }, + new GreetingDto() + { + Text = "Kia ora", + Language = "Māori" + }, + new GreetingDto() + { + Text = "Mari mari", + Language = "Mapuche" + }, + new GreetingDto() + { + Text = "नमस्कार", + Language = "Marathi", + Romanization = "namaskāra" + }, + new GreetingDto() + { + Text = "Io̧kwe", + Language = "Marshallese" + }, + new GreetingDto() + { + Text = "Bonzur", + Language = "Mauritian Creole" + }, + new GreetingDto() + { + Text = "Gwe’", + Language = "Míkmaq" + }, + new GreetingDto() + { + Text = "ဟာဲ", + Language = "Mon", + Romanization = "hāai" + }, + new GreetingDto() + { + Text = "Ciau", + Language = "Monégasque" + }, + new GreetingDto() + { + Text = "Сайн уу", + Language = "Mongolian", + Romanization = "Sain uu" + }, + new GreetingDto() + { + Text = "Manahúú", + Language = "Mono" + }, + new GreetingDto() + { + Text = "Ne y windiga", + Language = "Mossi" + }, + new GreetingDto() + { + Text = "Niltze", + Language = "Nahuatl" + }, + new GreetingDto() + { + Text = "Ekamowir omo", + Language = "Nauruan" + }, + new GreetingDto() + { + Text = "Yá'át'ééh", + Language = "Navajo" + }, + new GreetingDto() + { + Text = "ज्वजलपा", + Language = "Newari", + Romanization = "jvajalapā" + }, + new GreetingDto() + { + Text = "Oraire ota?", + Language = "Nkore" + }, + new GreetingDto() + { + Text = "Salibonani", + Language = "Ndebele" + }, + new GreetingDto() + { + Text = "Lotjhani", + Language = "Southern Ndebele" + }, + new GreetingDto() + { + Text = "नमस्ते", + Language = "Nepali", + Romanization = "namaste" + }, + new GreetingDto() + { + Text = "Fakaalofa atu", + Language = "Niuean" + }, + new GreetingDto() + { + Text = "Салам!", + Language = "Nogai" + }, + new GreetingDto() + { + Text = "Bures", + Language = "Northern Sámi" + }, + new GreetingDto() + { + Text = "Dumêlang", + Language = "Northern Sotho" + }, + new GreetingDto() + { + Text = "Goddag", + Language = "Norwegian" + }, + new GreetingDto() + { + Text = "Hatʸu", + Language = "Northern Chumash" + }, + new GreetingDto() + { + Text = "Bonjorn", + Language = "Occitan" + }, + new GreetingDto() + { + Text = "Aniin", + Language = "Ojibwe" + }, + new GreetingDto() + { + Text = "今日拝なびら", + Language = "Okinawan", + Romanization = "chuu wuganabira" + }, + new GreetingDto() + { + Text = "Wes hāl", + Language = "Old English" + }, + new GreetingDto() + { + Text = "Ƿes hāl", + Language = "Old Prussian" + }, + new GreetingDto() + { + Text = "Ahó", + Language = "Omaha" + }, + new GreetingDto() + { + Text = "ନମସ୍କାର", + Language = "Oriya", + Romanization = "Namascāra" + }, + new GreetingDto() + { + Text = "Alii", + Language = "Palauan" + }, + new GreetingDto() + { + Text = "Bon dia", + Language = "Papiamento" + }, + new GreetingDto() + { + Text = "ښې چارې", + Language = "Pashto", + Romanization = "khe chare" + }, + new GreetingDto() + { + Text = "Gude Daag", + Language = "Pennsylvania Deitsch" + }, + new GreetingDto() + { + Text = "درود", + Language = "Persian", + Romanization = "dorood", + Dialect = "Farsi" + }, + new GreetingDto() + { + Text = "Bojour", + Language = "Picard" + }, + new GreetingDto() + { + Text = "Wai", + Language = "Pitjantjatjara" + }, + new GreetingDto() + { + Text = "Cześć", + Language = "Polish" + }, + new GreetingDto() + { + Text = "Olá", + Language = "Portuguese", + Dialect = "Protugal" + }, + new GreetingDto() + { + Text = "Oi", + Language = "Portuguese", + Dialect = "Brazilian" + }, + new GreetingDto() + { + Text = "ਸਤਿ ਸ੍ਰੀ ਅਕਾਲ।", + Language = "Punjabi", + Romanization = "sat srī akāl" + }, + new GreetingDto() + { + Text = "Rimaykullayki", + Language = "Quechua" + }, + new GreetingDto() + { + Text = "'Iorana", + Language = "Rapa Nui" + }, + new GreetingDto() + { + Text = "Bonzour", + Language = "Réunion Creole" + }, + new GreetingDto() + { + Text = "Sastipe", + Language = "Romani" + }, + new GreetingDto() + { + Text = "Salut", + Language = "Romanian" + }, + new GreetingDto() + { + Text = "Ciao", + Language = "Romansh" + }, + new GreetingDto() + { + Text = "Привет", + Language = "Russian", + Romanization = "Privet" + }, + new GreetingDto() + { + Text = "Слава Исусу Христу", + Language = "Rusyn", + Dialect = "Slava Ysusu Chrystu" + }, + new GreetingDto() + { + Text = "бирибиэт", + Language = "Sakha", + Romanization = "biribiet" + }, + new GreetingDto() + { + Text = "Malō", + Language = "Samoan" + }, + new GreetingDto() + { + Text = "Bara mo", + Language = "Sango" + }, + new GreetingDto() + { + Text = "नमस्ते", + Language = "Sanskrit", + Romanization = "namaste" + }, + new GreetingDto() + { + Text = "Saludi", + Language = "Sardinian", + Dialect = "Campidanese" + }, + new GreetingDto() + { + Text = "Bone die", + Language = "Sardinian", + Dialect = "Logudorese" + }, + new GreetingDto() + { + Text = "Hullo", + Language = "Scots" + }, + new GreetingDto() + { + Text = "Halò", + Language = "Gaelic", + Dialect = "Scottish" + }, + new GreetingDto() + { + Text = "Здраво", + Language = "Serbian", + Dialect = "Zdravo" + }, + new GreetingDto() + { + Text = "Lumela", + Language = "Sesotho" + }, + new GreetingDto() + { + Text = "Mhoro", + Language = "Shona" + }, + new GreetingDto() + { + Text = "Ciau", + Language = "Sicilian" + }, + new GreetingDto() + { + Text = "سَلامُ", + Language = "Sindhi", + Romanization = "Salāmu" + }, + new GreetingDto() + { + Text = "ආයුඛෝවන්", + Language = "Sinhala", + Romanization = "āyubūvan" + }, + new GreetingDto() + { + Text = "Dobry den", + Language = "Slovak" + }, + new GreetingDto() + { + Text = "Pozdravljeni", + Language = "Slovenian" + }, + new GreetingDto() + { + Text = "Simi", + Language = "Solresol" + }, + new GreetingDto() + { + Text = "Salaam alaykum", + Language = "Somali" + }, + new GreetingDto() + { + Text = "Dobry źeń", + Language = "Sorbian", + Dialect = "Lower Sorbian" + }, + new GreetingDto() + { + Text = "Dobry dźeń", + Language = "Sorbian", + Dialect = "Upper Sorbian" + }, + new GreetingDto() + { + Text = "Buaregh", + Language = "Southern Sámi" + }, + new GreetingDto() + { + Text = "¡Hola!", + Language = "Spanish" + }, + new GreetingDto() + { + Text = "Hoj", + Language = "Stellingwarfs" + }, + new GreetingDto() + { + Text = "Sampurasun", + Language = "Sundanese" + }, + new GreetingDto() + { + Text = "Habari", + Language = "Swahili" + }, + new GreetingDto() + { + Text = "Sawubona", + Language = "Swazi" + }, + new GreetingDto() + { + Text = "God dag", + Language = "Swedish" + }, + new GreetingDto() + { + Text = "Grüezi", + Language = "Swiss German" + }, + new GreetingDto() + { + Text = "Салам", + Language = "Tabassaran", + Romanization = "Salam" + }, + new GreetingDto() + { + Text = "Musta?", + Language = "Tagalog" + }, + new GreetingDto() + { + Text = "'Ia ora na", + Language = "Tahitian" + }, + new GreetingDto() + { + Text = "Ассалому алейкум", + Language = "Tajik", + Romanization = "Assalomu alejkym" + }, + new GreetingDto() + { + Text = "வணக்கம்", + Language = "Tamil", + Romanization = "vaṇakkam" + }, + new GreetingDto() + { + Text = "Isänme", + Language = "Tatar" + }, + new GreetingDto() + { + Text = "నమస్కారం", + Language = "Telugu", + Romanization = "namaskārām" + }, + new GreetingDto() + { + Text = "Elo", + Language = "Tetum" + }, + new GreetingDto() + { + Text = "Miga", + Language = "Teribe" + }, + new GreetingDto() + { + Text = "สวัสดี", + Language = "Thai", + Romanization = "sà-wàt-dee" + }, + new GreetingDto() + { + Text = "བཀྲ་ཤིས་བདེ་ལེགས།", + Language = "Tibetan", + Romanization = "tashi delek" + }, + new GreetingDto() + { + Text = "ሰላም", + Language = "Tigrinya", + Romanization = "selam" + }, + new GreetingDto() + { + Text = "Tālofa", + Language = "Tokelauan" + }, + new GreetingDto() + { + Text = "Gude", + Language = "Tok Pisin" + }, + new GreetingDto() + { + Text = "Mālō e lelei", + Language = "Tongan" + }, + new GreetingDto() + { + Text = "Miiyiha", + Language = "Tongva", + Dialect = "Gabrielino" + }, + new GreetingDto() + { + Text = "Xewani", + Language = "Tsonga" + }, + new GreetingDto() + { + Text = "K'uxi", + Language = "Tsotsil" + }, + new GreetingDto() + { + Text = "Dumela", + Language = "Tswana" + }, + new GreetingDto() + { + Text = "АсаламугIалейкум!", + Language = "Tsez", + Romanization = "AsalamugIalejkum!" + }, + new GreetingDto() + { + Text = "Monile", + Language = "Tumbuka" + }, + new GreetingDto() + { + Text = "Merhaba", + Language = "Turkish" + }, + new GreetingDto() + { + Text = "Salam", + Language = "Turkmen" + }, + new GreetingDto() + { + Text = "Вітаю", + Language = "Ukrainian" + }, + new GreetingDto() + { + Text = "Сородэ", + Language = "Ulch", + Romanization = "Sorodè" + }, + new GreetingDto() + { + Text = "السلام علیکم", + Language = "Urdu", + Romanization = "āssālam 'alaykum" + }, + new GreetingDto() + { + Text = "ئەسسالامۇ ئەلەيكۇم", + Language = "Uyghur", + Romanization = "Ässalamu äläykum" + }, + new GreetingDto() + { + Text = "Assalomu Alaykum", + Language = "Uzbek" + }, + new GreetingDto() + { + Text = "I nhlikanhi", + Language = "Venda" + }, + new GreetingDto() + { + Text = "Ciao", + Language = "Venetian" + }, + new GreetingDto() + { + Text = "Tervhen", + Language = "Veps" + }, + new GreetingDto() + { + Text = "Chào anh", + Language = "Vietnamese" + }, + new GreetingDto() + { + Text = "Tereq", + Language = "Võro" + }, + new GreetingDto() + { + Text = "Bondjoû", + Language = "Walloon" + }, + new GreetingDto() + { + Text = "Ngurrju mayinpa", + Language = "Warlpiri" + }, + new GreetingDto() + { + Text = "Helô", + Language = "Welsh" + }, + new GreetingDto() + { + Text = "Hej", + Language = "Westrobothnian" + }, + new GreetingDto() + { + Text = "Na nga def", + Language = "Wolof" + }, + new GreetingDto() + { + Text = "Molo", + Language = "Xhosa" + }, + new GreetingDto() + { + Text = "Mogethin", + Language = "Yapese" + }, + new GreetingDto() + { + Text = "אַ גוטן טאָג", + Language = "Yiddish", + Romanization = "A gutn tog" + }, + new GreetingDto() + { + Text = "Ẹ n lẹ", + Language = "Yoruba" + }, + new GreetingDto() + { + Text = "Ba'ax ka wa'alik?", + Language = "Yucatec Maya" + }, + new GreetingDto() + { + Text = "Selam", + Language = "Zazaki" + }, + new GreetingDto() + { + Text = "Sawubona", + Language = "Zulu" + }, + new GreetingDto() + { + Text = "Saluton", + Language = "Esperanto" + }, + new GreetingDto() + { + Text = "Bon die!", + Language = "Interlingua" + }, + new GreetingDto() + { + Text = "nuqneH", + Language = "Klingon" + }, + new GreetingDto() + { + Text = "Aiya", + Language = "Quenya" + }, + new GreetingDto() + { + Text = "Glidö", + Language = "Volapük" + }, + new GreetingDto() + { + Text = "Saluto", + Language = "Ido" + } + }; + + public static readonly int GreetingCount = Greetings.Count; + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/Randomness/Greetings/Greetings.cs b/Geekbot.net/Commands/Randomness/Greetings/Greetings.cs new file mode 100644 index 0000000..83cdd63 --- /dev/null +++ b/Geekbot.net/Commands/Randomness/Greetings/Greetings.cs @@ -0,0 +1,50 @@ +using System; +using System.Threading.Tasks; +using Discord; +using Discord.Commands; +using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.Extensions; + +namespace Geekbot.net.Commands.Randomness.Greetings +{ + public class Greetings : ModuleBase + { + private readonly IErrorHandler _errorHandler; + + public Greetings(IErrorHandler errorHandler) + { + _errorHandler = errorHandler; + } + + [Command("hello", RunMode = RunMode.Async)] + [Alias("greeting")] + [Summary("Say hello to the bot and get a reply in a random language")] + public async Task GetGreeting() + { + try + { + var greeting = GreetingProvider.Greetings[new Random().Next(GreetingProvider.GreetingCount - 1)]; + + var eb = new EmbedBuilder(); + eb.Title = greeting.Text; + eb.AddInlineField("Language", greeting.Language); + + if (greeting.Dialect != null) + { + eb.AddInlineField("Dialect", greeting.Dialect); + } + + if (greeting.Romanization != null) + { + eb.AddInlineField("Romanization", greeting.Romanization); + } + + await ReplyAsync(string.Empty, false, eb.Build()); + } + catch (Exception e) + { + await _errorHandler.HandleCommandException(e, Context); + } + } + } +} \ No newline at end of file From f7908c2a0c2a48ba2d0c73f654cf01a5884155a1 Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 19 Jun 2020 00:10:43 +0200 Subject: [PATCH 221/443] - Remove accidental leftover from the GreetingProvider - Add 2 additional aliases for !hello - Change Romanization to Roman in the !hello embed --- .../Commands/Randomness/Greetings/GreetingProvider.cs | 5 ----- Geekbot.net/Commands/Randomness/Greetings/Greetings.cs | 4 ++-- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/Geekbot.net/Commands/Randomness/Greetings/GreetingProvider.cs b/Geekbot.net/Commands/Randomness/Greetings/GreetingProvider.cs index 52d8d18..a15f32e 100644 --- a/Geekbot.net/Commands/Randomness/Greetings/GreetingProvider.cs +++ b/Geekbot.net/Commands/Randomness/Greetings/GreetingProvider.cs @@ -507,11 +507,6 @@ namespace Geekbot.net.Commands.Randomness.Greetings Dialect = "Ancient" }, new GreetingDto() - { - Text = "something", - Language = "something" - }, - new GreetingDto() { Text = "Aluu", Language = "Greenlandic" diff --git a/Geekbot.net/Commands/Randomness/Greetings/Greetings.cs b/Geekbot.net/Commands/Randomness/Greetings/Greetings.cs index 83cdd63..334b894 100644 --- a/Geekbot.net/Commands/Randomness/Greetings/Greetings.cs +++ b/Geekbot.net/Commands/Randomness/Greetings/Greetings.cs @@ -17,7 +17,7 @@ namespace Geekbot.net.Commands.Randomness.Greetings } [Command("hello", RunMode = RunMode.Async)] - [Alias("greeting")] + [Alias("greeting", "hi", "hallo")] [Summary("Say hello to the bot and get a reply in a random language")] public async Task GetGreeting() { @@ -36,7 +36,7 @@ namespace Geekbot.net.Commands.Randomness.Greetings if (greeting.Romanization != null) { - eb.AddInlineField("Romanization", greeting.Romanization); + eb.AddInlineField("Roman", greeting.Romanization); } await ReplyAsync(string.Empty, false, eb.Build()); From 83dc2c8e49350a3bcb1e3cc2cd8ca3ec1a81338b Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 19 Jun 2020 03:34:37 +0200 Subject: [PATCH 222/443] Attempt to improve code quality with a fearless refactor - Completely refactor Program.cs - Split Handlers into separate files - Split up the command handler into multiple functions --- Geekbot.net/Handlers.cs | 280 ------------------ Geekbot.net/Handlers/CommandHandler.cs | 117 ++++++++ Geekbot.net/Handlers/MessageDeletedHandler.cs | 55 ++++ Geekbot.net/Handlers/ReactionHandler.cs | 33 +++ Geekbot.net/Handlers/StatsHandler.cs | 62 ++++ Geekbot.net/Handlers/UserHandler.cs | 96 ++++++ .../Lib/Logger/SimpleConextConverter.cs | 3 +- Geekbot.net/Program.cs | 223 +++++++------- 8 files changed, 488 insertions(+), 381 deletions(-) delete mode 100644 Geekbot.net/Handlers.cs create mode 100644 Geekbot.net/Handlers/CommandHandler.cs create mode 100644 Geekbot.net/Handlers/MessageDeletedHandler.cs create mode 100644 Geekbot.net/Handlers/ReactionHandler.cs create mode 100644 Geekbot.net/Handlers/StatsHandler.cs create mode 100644 Geekbot.net/Handlers/UserHandler.cs diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs deleted file mode 100644 index 6f98041..0000000 --- a/Geekbot.net/Handlers.cs +++ /dev/null @@ -1,280 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Discord; -using Discord.Commands; -using Discord.Rest; -using Discord.WebSocket; -using Geekbot.net.Database; -using Geekbot.net.Database.Models; -using Geekbot.net.Lib.Extensions; -using Geekbot.net.Lib.Logger; -using Geekbot.net.Lib.ReactionListener; -using Geekbot.net.Lib.UserRepository; -using Microsoft.EntityFrameworkCore; - -namespace Geekbot.net -{ - public class Handlers - { - private readonly DatabaseContext _database; - private readonly IDiscordClient _client; - private readonly IGeekbotLogger _logger; - private readonly IServiceProvider _servicesProvider; - private readonly CommandService _commands; - private readonly IUserRepository _userRepository; - private readonly IReactionListener _reactionListener; - private readonly DatabaseContext _messageCounterDatabaseContext; - private readonly RestApplication _applicationInfo; - private readonly List _ignoredServers; - - public Handlers(DatabaseInitializer databaseInitializer, IDiscordClient client, IGeekbotLogger logger, - IServiceProvider servicesProvider, CommandService commands, IUserRepository userRepository, - IReactionListener reactionListener, RestApplication applicationInfo) - { - _database = databaseInitializer.Initialize(); - _messageCounterDatabaseContext = databaseInitializer.Initialize(); - _client = client; - _logger = logger; - _servicesProvider = servicesProvider; - _commands = commands; - _userRepository = userRepository; - _reactionListener = reactionListener; - _applicationInfo = applicationInfo; - // ToDo: create a clean solution for this... - _ignoredServers = new List() - { - 228623803201224704, // SwitzerLAN - 169844523181015040, // EEvent - 248531441548263425, // MYI - 110373943822540800 // Discord Bots - }; - } - - // - // Incoming Messages - // - - public Task RunCommand(SocketMessage messageParam) - { - try - { - if (!(messageParam is SocketUserMessage message)) return Task.CompletedTask; - if (message.Author.IsBot) return Task.CompletedTask; - var argPos = 0; - - var guildId = ((SocketGuildChannel) message.Channel).Guild.Id; - // Some guilds only wanted very specific functionally without any of the commands, a quick hack that solves that short term... - // ToDo: cleanup - if (_ignoredServers.Contains(guildId)) - { - if (message.Author.Id != _applicationInfo.Owner.Id) - { - return Task.CompletedTask; - } - } - - var lowCaseMsg = message.ToString().ToLower(); - if (lowCaseMsg.StartsWith("hui")) - { - var hasPing = _database.GuildSettings.FirstOrDefault(guild => guild.GuildId.Equals(guildId.AsLong()))?.Hui ?? false; - if (hasPing) - { - message.Channel.SendMessageAsync("hui!!!"); - return Task.CompletedTask; - } - } - - if (lowCaseMsg.StartsWith("ping ") || lowCaseMsg.Equals("ping")) - { - var hasPing = _database.GuildSettings.FirstOrDefault(guild => guild.GuildId.Equals(guildId.AsLong()))?.Ping ?? false; - if (hasPing) - { - message.Channel.SendMessageAsync("pong"); - return Task.CompletedTask; - } - } - - if (!(message.HasCharPrefix('!', ref argPos) || - message.HasMentionPrefix(_client.CurrentUser, ref argPos))) return Task.CompletedTask; - var context = new CommandContext(_client, message); - _commands.ExecuteAsync(context, argPos, _servicesProvider); - _logger.Information(LogSource.Command, - context.Message.Content.Split(" ")[0].Replace("!", ""), - SimpleConextConverter.ConvertContext(context)); - return Task.CompletedTask; - } - catch (Exception e) - { - _logger.Error(LogSource.Geekbot, "Failed to Process Message", e); - return Task.CompletedTask; - } - } - - public async Task UpdateStats(SocketMessage message) - { - try - { - if (message == null) return; - if (message.Channel.Name.StartsWith('@')) - { - _logger.Information(LogSource.Message, $"[DM-Channel] {message.Content}", SimpleConextConverter.ConvertSocketMessage(message, true)); - return; - } - - var channel = (SocketGuildChannel) message.Channel; - - var rowId = await _messageCounterDatabaseContext.Database.ExecuteSqlRawAsync( - "UPDATE \"Messages\" SET \"MessageCount\" = \"MessageCount\" + 1 WHERE \"GuildId\" = {0} AND \"UserId\" = {1}", - channel.Guild.Id.AsLong(), - message.Author.Id.AsLong() - ); - - if (rowId == 0) - { - _messageCounterDatabaseContext.Messages.Add(new MessagesModel - { - UserId = message.Author.Id.AsLong(), - GuildId = channel.Guild.Id.AsLong(), - MessageCount = 1 - }); - _messageCounterDatabaseContext.SaveChanges(); - } - - if (message.Author.IsBot) return; - _logger.Information(LogSource.Message, message.Content, SimpleConextConverter.ConvertSocketMessage(message)); - } - catch (Exception e) - { - _logger.Error(LogSource.Message, "Could not process message stats", e); - } - } - - // - // User Stuff - // - - public async Task UserJoined(SocketGuildUser user) - { - try - { - var userRepoUpdate = _userRepository.Update(user); - _logger.Information(LogSource.Geekbot, $"{user.Username} ({user.Id}) joined {user.Guild.Name} ({user.Guild.Id})"); - - if (!user.IsBot) - { - var guildSettings = _database.GuildSettings.FirstOrDefault(guild => guild.GuildId == user.Guild.Id.AsLong()); - var message = guildSettings?.WelcomeMessage; - if (string.IsNullOrEmpty(message)) return; - message = message.Replace("$user", user.Mention); - - var fallbackSender = new Func>(() => user.Guild.DefaultChannel.SendMessageAsync(message)); - if (guildSettings.WelcomeChannel != 0) - { - try - { - var target = await _client.GetChannelAsync(guildSettings.WelcomeChannel.AsUlong()); - var channel = target as ISocketMessageChannel; - await channel.SendMessageAsync(message); - } - catch (Exception e) - { - _logger.Error(LogSource.Geekbot, "Failed to send welcome message to user defined welcome channel", e); - await fallbackSender(); - } - } - else - { - await fallbackSender(); - } - } - - await userRepoUpdate; - } - catch (Exception e) - { - _logger.Error(LogSource.Geekbot, "Failed to send welcome message", e); - } - } - - public async Task UserUpdated(SocketUser oldUser, SocketUser newUser) - { - await _userRepository.Update(newUser); - } - - public async Task UserLeft(SocketGuildUser user) - { - try - { - var guild = _database.GuildSettings.FirstOrDefault(g => - g.GuildId.Equals(user.Guild.Id.AsLong())); - if (guild?.ShowLeave ?? false) - { - var modChannelSocket = (ISocketMessageChannel) await _client.GetChannelAsync(guild.ModChannel.AsUlong()); - await modChannelSocket.SendMessageAsync($"{user.Username}#{user.Discriminator} left the server"); - } - } - catch (Exception e) - { - _logger.Error(LogSource.Geekbot, "Failed to send leave message", e); - } - - _logger.Information(LogSource.Geekbot, $"{user.Username} ({user.Id}) joined {user.Guild.Name} ({user.Guild.Id})"); - } - - // - // Message Stuff - // - - public async Task MessageDeleted(Cacheable message, ISocketMessageChannel channel) - { - try - { - var guildSocketData = ((IGuildChannel) channel).Guild; - var guild = _database.GuildSettings.FirstOrDefault(g => g.GuildId.Equals(guildSocketData.Id.AsLong())); - if ((guild?.ShowDelete ?? false) && guild?.ModChannel != 0) - { - var modChannelSocket = (ISocketMessageChannel) await _client.GetChannelAsync(guild.ModChannel.AsUlong()); - var sb = new StringBuilder(); - if (message.Value != null) - { - sb.AppendLine($"The following message from {message.Value.Author.Username}#{message.Value.Author.Discriminator} was deleted in <#{channel.Id}>"); - sb.AppendLine(message.Value.Content); - } - else - { - sb.AppendLine("Someone deleted a message, the message was not cached..."); - } - - await modChannelSocket.SendMessageAsync(sb.ToString()); - } - } - catch (Exception e) - { - _logger.Error(LogSource.Geekbot, "Failed to send delete message...", e); - } - } - - // - // Reactions - // - - public Task ReactionAdded(Cacheable cacheable, ISocketMessageChannel socketMessageChannel, SocketReaction reaction) - { - if (reaction.User.Value.IsBot) return Task.CompletedTask; - if (!_reactionListener.IsListener(reaction.MessageId)) return Task.CompletedTask; - _reactionListener.GiveRole(socketMessageChannel, reaction); - return Task.CompletedTask; - } - - public Task ReactionRemoved(Cacheable cacheable, ISocketMessageChannel socketMessageChannel, SocketReaction reaction) - { - if (reaction.User.Value.IsBot) return Task.CompletedTask; - if (!_reactionListener.IsListener(reaction.MessageId)) return Task.CompletedTask; - _reactionListener.RemoveRole(socketMessageChannel, reaction); - return Task.CompletedTask; - } - } -} \ No newline at end of file diff --git a/Geekbot.net/Handlers/CommandHandler.cs b/Geekbot.net/Handlers/CommandHandler.cs new file mode 100644 index 0000000..5074ff2 --- /dev/null +++ b/Geekbot.net/Handlers/CommandHandler.cs @@ -0,0 +1,117 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Discord; +using Discord.Commands; +using Discord.Rest; +using Discord.WebSocket; +using Geekbot.net.Database; +using Geekbot.net.Database.Models; +using Geekbot.net.Lib.Extensions; +using Geekbot.net.Lib.Logger; + +namespace Geekbot.net.Handlers +{ + public class CommandHandler + { + private readonly IDiscordClient _client; + private readonly IGeekbotLogger _logger; + private readonly IServiceProvider _servicesProvider; + private readonly CommandService _commands; + private readonly RestApplication _applicationInfo; + private readonly List _ignoredServers; + private readonly DatabaseContext _database; + + public CommandHandler(DatabaseContext database, IDiscordClient client, IGeekbotLogger logger, IServiceProvider servicesProvider, CommandService commands, RestApplication applicationInfo) + { + _database = database; + _client = client; + _logger = logger; + _servicesProvider = servicesProvider; + _commands = commands; + _applicationInfo = applicationInfo; + + // Some guilds only want very specific functionally without any of the commands, a quick hack that solves that "short term" + // ToDo: create a clean solution for this... + _ignoredServers = new List + { + 228623803201224704, // SwitzerLAN + 169844523181015040, // EEvent + 248531441548263425, // MYI + 110373943822540800 // Discord Bots + }; + } + + public Task RunCommand(SocketMessage messageParam) + { + try + { + if (!(messageParam is SocketUserMessage message)) return Task.CompletedTask; + if (message.Author.IsBot) return Task.CompletedTask; + + var guildId = ((SocketGuildChannel) message.Channel).Guild.Id; + if (IsIgnoredGuild(guildId, message.Author.Id)) return Task.CompletedTask; + + var lowCaseMsg = message.ToString().ToLower(); + if (ShouldHui(lowCaseMsg, guildId)) + { + message.Channel.SendMessageAsync("hui!!!"); + return Task.CompletedTask; + } + + if (ShouldPong(lowCaseMsg, guildId)) + { + message.Channel.SendMessageAsync("pong"); + return Task.CompletedTask; + } + + var argPos = 0; + if (!IsCommand(message, ref argPos)) return Task.CompletedTask; + + ExecuteCommand(message, argPos); + } + catch (Exception e) + { + _logger.Error(LogSource.Geekbot, "Failed to Process Message", e); + } + + return Task.CompletedTask; + } + + private void ExecuteCommand(IUserMessage message, int argPos) + { + var context = new CommandContext(_client, message); + _commands.ExecuteAsync(context, argPos, _servicesProvider); + _logger.Information(LogSource.Command, context.Message.Content.Split(" ")[0].Replace("!", ""), SimpleConextConverter.ConvertContext(context)); + } + + private bool IsIgnoredGuild(ulong guildId, ulong authorId) + { + if (!_ignoredServers.Contains(guildId)) return false; + return authorId == _applicationInfo.Owner.Id; + } + + private bool IsCommand(IUserMessage message, ref int argPos) + { + return message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos); + } + + private bool ShouldPong(string lowerCaseMessage, ulong guildId) + { + if (!lowerCaseMessage.StartsWith("ping ") && !lowerCaseMessage.Equals("ping")) return false; + return GetGuildSettings(guildId)?.Ping ?? false; + } + + private bool ShouldHui(string lowerCaseMessage, ulong guildId) + { + if (!lowerCaseMessage.StartsWith("hui")) return false; + return GetGuildSettings(guildId)?.Hui ?? false; + } + + private GuildSettingsModel GetGuildSettings(ulong guildId) + { + return _database.GuildSettings.FirstOrDefault(guild => guild.GuildId.Equals(guildId.AsLong())); + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Handlers/MessageDeletedHandler.cs b/Geekbot.net/Handlers/MessageDeletedHandler.cs new file mode 100644 index 0000000..5595cff --- /dev/null +++ b/Geekbot.net/Handlers/MessageDeletedHandler.cs @@ -0,0 +1,55 @@ +using System; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Discord; +using Discord.WebSocket; +using Geekbot.net.Database; +using Geekbot.net.Lib.Extensions; +using Geekbot.net.Lib.Logger; + +namespace Geekbot.net.Handlers +{ + public class MessageDeletedHandler + { + private readonly DatabaseContext _database; + private readonly IGeekbotLogger _logger; + private readonly IDiscordClient _client; + + public MessageDeletedHandler(DatabaseContext database, IGeekbotLogger logger, IDiscordClient client) + { + _database = database; + _logger = logger; + _client = client; + } + + public async Task HandleMessageDeleted(Cacheable message, ISocketMessageChannel channel) + { + try + { + var guildSocketData = ((IGuildChannel) channel).Guild; + var guild = _database.GuildSettings.FirstOrDefault(g => g.GuildId.Equals(guildSocketData.Id.AsLong())); + if ((guild?.ShowDelete ?? false) && guild?.ModChannel != 0) + { + var modChannelSocket = (ISocketMessageChannel) await _client.GetChannelAsync(guild.ModChannel.AsUlong()); + var sb = new StringBuilder(); + if (message.Value != null) + { + sb.AppendLine($"The following message from {message.Value.Author.Username}#{message.Value.Author.Discriminator} was deleted in <#{channel.Id}>"); + sb.AppendLine(message.Value.Content); + } + else + { + sb.AppendLine("Someone deleted a message, the message was not cached..."); + } + + await modChannelSocket.SendMessageAsync(sb.ToString()); + } + } + catch (Exception e) + { + _logger.Error(LogSource.Geekbot, "Failed to send delete message...", e); + } + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Handlers/ReactionHandler.cs b/Geekbot.net/Handlers/ReactionHandler.cs new file mode 100644 index 0000000..e2355a8 --- /dev/null +++ b/Geekbot.net/Handlers/ReactionHandler.cs @@ -0,0 +1,33 @@ +using System.Threading.Tasks; +using Discord; +using Discord.WebSocket; +using Geekbot.net.Lib.ReactionListener; + +namespace Geekbot.net.Handlers +{ + public class ReactionHandler + { + private readonly IReactionListener _reactionListener; + + public ReactionHandler(IReactionListener reactionListener) + { + _reactionListener = reactionListener; + } + + public Task Added(Cacheable cacheable, ISocketMessageChannel socketMessageChannel, SocketReaction reaction) + { + if (reaction.User.Value.IsBot) return Task.CompletedTask; + if (!_reactionListener.IsListener(reaction.MessageId)) return Task.CompletedTask; + _reactionListener.GiveRole(socketMessageChannel, reaction); + return Task.CompletedTask; + } + + public Task Removed(Cacheable cacheable, ISocketMessageChannel socketMessageChannel, SocketReaction reaction) + { + if (reaction.User.Value.IsBot) return Task.CompletedTask; + if (!_reactionListener.IsListener(reaction.MessageId)) return Task.CompletedTask; + _reactionListener.RemoveRole(socketMessageChannel, reaction); + return Task.CompletedTask; + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Handlers/StatsHandler.cs b/Geekbot.net/Handlers/StatsHandler.cs new file mode 100644 index 0000000..56c0894 --- /dev/null +++ b/Geekbot.net/Handlers/StatsHandler.cs @@ -0,0 +1,62 @@ +using System; +using System.Threading.Tasks; +using Discord.WebSocket; +using Geekbot.net.Database; +using Geekbot.net.Database.Models; +using Geekbot.net.Lib.Extensions; +using Geekbot.net.Lib.Logger; +using Microsoft.EntityFrameworkCore; + +namespace Geekbot.net.Handlers +{ + public class StatsHandler + { + private readonly IGeekbotLogger _logger; + private readonly DatabaseContext _database; + + public StatsHandler(IGeekbotLogger logger, DatabaseContext database) + { + _logger = logger; + _database = database; + } + + public async Task UpdateStats(SocketMessage message) + { + try + { + if (message == null) return; + if (message.Channel.Name.StartsWith('@')) + { + _logger.Information(LogSource.Message, $"[DM-Channel] {message.Content}", SimpleConextConverter.ConvertSocketMessage(message, true)); + return; + } + + var channel = (SocketGuildChannel) message.Channel; + + var rowId = await _database.Database.ExecuteSqlRawAsync( + "UPDATE \"Messages\" SET \"MessageCount\" = \"MessageCount\" + 1 WHERE \"GuildId\" = {0} AND \"UserId\" = {1}", + channel.Guild.Id.AsLong(), + message.Author.Id.AsLong() + ); + + if (rowId == 0) + { + await _database.Messages.AddAsync(new MessagesModel + { + UserId = message.Author.Id.AsLong(), + GuildId = channel.Guild.Id.AsLong(), + MessageCount = 1 + }); + await _database.SaveChangesAsync(); + } + + if (message.Author.IsBot) return; + _logger.Information(LogSource.Message, message.Content, SimpleConextConverter.ConvertSocketMessage(message)); + } + catch (Exception e) + { + _logger.Error(LogSource.Message, "Could not process message stats", e); + } + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Handlers/UserHandler.cs b/Geekbot.net/Handlers/UserHandler.cs new file mode 100644 index 0000000..fb26d70 --- /dev/null +++ b/Geekbot.net/Handlers/UserHandler.cs @@ -0,0 +1,96 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using Discord; +using Discord.Rest; +using Discord.WebSocket; +using Geekbot.net.Database; +using Geekbot.net.Lib.Extensions; +using Geekbot.net.Lib.Logger; +using Geekbot.net.Lib.UserRepository; + +namespace Geekbot.net.Handlers +{ + public class UserHandler + { + private readonly IUserRepository _userRepository; + private readonly IGeekbotLogger _logger; + private readonly DatabaseContext _database; + private readonly IDiscordClient _client; + + public UserHandler(IUserRepository userRepository, IGeekbotLogger logger, DatabaseContext database, IDiscordClient client) + { + _userRepository = userRepository; + _logger = logger; + _database = database; + _client = client; + } + + public async Task Joined(SocketGuildUser user) + { + try + { + var userRepoUpdate = _userRepository.Update(user); + _logger.Information(LogSource.Geekbot, $"{user.Username} ({user.Id}) joined {user.Guild.Name} ({user.Guild.Id})"); + + if (!user.IsBot) + { + var guildSettings = _database.GuildSettings.FirstOrDefault(guild => guild.GuildId == user.Guild.Id.AsLong()); + var message = guildSettings?.WelcomeMessage; + if (string.IsNullOrEmpty(message)) return; + message = message.Replace("$user", user.Mention); + + var fallbackSender = new Func>(() => user.Guild.DefaultChannel.SendMessageAsync(message)); + if (guildSettings.WelcomeChannel != 0) + { + try + { + var target = await _client.GetChannelAsync(guildSettings.WelcomeChannel.AsUlong()); + var channel = target as ISocketMessageChannel; + await channel.SendMessageAsync(message); + } + catch (Exception e) + { + _logger.Error(LogSource.Geekbot, "Failed to send welcome message to user defined welcome channel", e); + await fallbackSender(); + } + } + else + { + await fallbackSender(); + } + } + + await userRepoUpdate; + } + catch (Exception e) + { + _logger.Error(LogSource.Geekbot, "Failed to send welcome message", e); + } + } + + public async Task Updated(SocketUser oldUser, SocketUser newUser) + { + await _userRepository.Update(newUser); + } + + public async Task Left(SocketGuildUser user) + { + try + { + var guild = _database.GuildSettings.FirstOrDefault(g => g.GuildId.Equals(user.Guild.Id.AsLong())); + if (guild?.ShowLeave ?? false) + { + var modChannelSocket = (ISocketMessageChannel) await _client.GetChannelAsync(guild.ModChannel.AsUlong()); + await modChannelSocket.SendMessageAsync($"{user.Username}#{user.Discriminator} left the server"); + } + } + catch (Exception e) + { + _logger.Error(LogSource.Geekbot, "Failed to send leave message", e); + } + + _logger.Information(LogSource.Geekbot, $"{user.Username} ({user.Id}) joined {user.Guild.Name} ({user.Guild.Id})"); + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/Logger/SimpleConextConverter.cs b/Geekbot.net/Lib/Logger/SimpleConextConverter.cs index d01da67..1eef12c 100644 --- a/Geekbot.net/Lib/Logger/SimpleConextConverter.cs +++ b/Geekbot.net/Lib/Logger/SimpleConextConverter.cs @@ -11,7 +11,7 @@ namespace Geekbot.net.Lib.Logger { Message = new MessageDto.MessageContent { - Content = context.Message.Content, + Content = context.Message.Content, // Only when an error occurs, including for diagnostic reason Id = context.Message.Id.ToString(), Attachments = context.Message.Attachments.Count, ChannelMentions = context.Message.MentionedChannelIds.Count, @@ -42,7 +42,6 @@ namespace Geekbot.net.Lib.Logger { Message = new MessageDto.MessageContent { - Content = message.Content, Id = message.Id.ToString(), Attachments = message.Attachments.Count, ChannelMentions = message.MentionedChannels.Count, diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index d46df90..262d830 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -7,6 +7,7 @@ using Discord; using Discord.Commands; using Discord.WebSocket; using Geekbot.net.Database; +using Geekbot.net.Handlers; using Geekbot.net.Lib; using Geekbot.net.Lib.Clients; using Geekbot.net.Lib.Converters; @@ -34,12 +35,11 @@ namespace Geekbot.net private CommandService _commands; private DatabaseInitializer _databaseInitializer; private IGlobalSettings _globalSettings; - private IServiceCollection _services; private IServiceProvider _servicesProvider; - private string _token; private GeekbotLogger _logger; private IUserRepository _userRepository; private RunParameters _runParameters; + private IReactionListener _reactionListener; private static void Main(string[] args) { @@ -73,66 +73,25 @@ namespace Geekbot.net { _logger = logger; _runParameters = runParameters; - logger.Information(LogSource.Geekbot, "Initing Stuff"); - var discordLogger = new DiscordLogger(logger); - - _client = new DiscordSocketClient(new DiscordSocketConfig - { - LogLevel = LogSeverity.Verbose, - MessageCacheSize = 1000, - ExclusiveBulkDelete = true - }); - _client.Log += discordLogger.Log; - _commands = new CommandService(); - - _databaseInitializer = new DatabaseInitializer(runParameters, logger); - var database = _databaseInitializer.Initialize(); - database.Database.EnsureCreated(); - if(!_runParameters.InMemory) database.Database.Migrate(); + logger.Information(LogSource.Geekbot, "Connecting to Database"); + var database = ConnectToDatabase(); _globalSettings = new GlobalSettings(database); - - _token = runParameters.Token ?? _globalSettings.GetKey("DiscordToken"); - if (string.IsNullOrEmpty(_token)) - { - Console.Write("Your bot Token: "); - var newToken = Console.ReadLine(); - await _globalSettings.SetKey("DiscordToken", newToken); - await _globalSettings.SetKey("Game", "Ping Pong"); - _token = newToken; - } - - _services = new ServiceCollection(); - - _userRepository = new UserRepository(_databaseInitializer.Initialize(), logger); - var fortunes = new FortunesProvider(logger); - var mediaProvider = new MediaProvider(logger); - var malClient = new MalClient(_globalSettings, logger); - var levelCalc = new LevelCalc(); - var emojiConverter = new EmojiConverter(); - var mtgManaConverter = new MtgManaConverter(); - var wikipediaClient = new WikipediaClient(); - var randomNumberGenerator = new RandomNumberGenerator(); - var kvMemoryStore = new KvInInMemoryStore(); - - _services.AddSingleton(_userRepository); - _services.AddSingleton(logger); - _services.AddSingleton(levelCalc); - _services.AddSingleton(emojiConverter); - _services.AddSingleton(fortunes); - _services.AddSingleton(mediaProvider); - _services.AddSingleton(malClient); - _services.AddSingleton(mtgManaConverter); - _services.AddSingleton(wikipediaClient); - _services.AddSingleton(randomNumberGenerator); - _services.AddSingleton(kvMemoryStore); - _services.AddSingleton(_globalSettings); - _services.AddTransient(e => new HighscoreManager(_databaseInitializer.Initialize(), _userRepository)); - _services.AddTransient(e => _databaseInitializer.Initialize()); logger.Information(LogSource.Geekbot, "Connecting to Discord"); - + SetupDiscordClient(); await Login(); + _logger.Information(LogSource.Geekbot, $"Now Connected as {_client.CurrentUser.Username} to {_client.Guilds.Count} Servers"); + await _client.SetGameAsync(_globalSettings.GetKey("Game")); + + _logger.Information(LogSource.Geekbot, "Loading Dependencies and Handlers"); + RegisterDependencies(); + await RegisterHandlers(); + + _logger.Information(LogSource.Api, "Starting Web API"); + StartWebApi(); + + _logger.Information(LogSource.Geekbot, "Done and ready for use"); await Task.Delay(-1); } @@ -141,42 +100,10 @@ namespace Geekbot.net { try { - await _client.LoginAsync(TokenType.Bot, _token); + var token = await GetToken(); + await _client.LoginAsync(TokenType.Bot, token); await _client.StartAsync(); - var isConneted = await IsConnected(); - if (isConneted) - { - var applicationInfo = await _client.GetApplicationInfoAsync(); - await _client.SetGameAsync(_globalSettings.GetKey("Game")); - _logger.Information(LogSource.Geekbot, $"Now Connected as {_client.CurrentUser.Username} to {_client.Guilds.Count} Servers"); - - _logger.Information(LogSource.Geekbot, "Registering Stuff"); - var translationHandler = new TranslationHandler(_databaseInitializer.Initialize(), _logger); - var errorHandler = new ErrorHandler(_logger, translationHandler, _runParameters.ExposeErrors); - var reactionListener = new ReactionListener(_databaseInitializer.Initialize()); - _services.AddSingleton(errorHandler); - _services.AddSingleton(translationHandler); - _services.AddSingleton(_client); - _services.AddSingleton(reactionListener); - _servicesProvider = _services.BuildServiceProvider(); - await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _servicesProvider); - - var handlers = new Handlers(_databaseInitializer, _client, _logger, _servicesProvider, _commands, _userRepository, reactionListener, applicationInfo); - - _client.MessageReceived += handlers.RunCommand; - _client.MessageDeleted += handlers.MessageDeleted; - _client.UserJoined += handlers.UserJoined; - _client.UserUpdated += handlers.UserUpdated; - _client.UserLeft += handlers.UserLeft; - _client.ReactionAdded += handlers.ReactionAdded; - _client.ReactionRemoved += handlers.ReactionRemoved; - if (!_runParameters.InMemory) _client.MessageReceived += handlers.UpdateStats; - - var webserver = _runParameters.DisableApi ? Task.Delay(10) : StartWebApi(); - _logger.Information(LogSource.Geekbot, "Done and ready for use"); - - await webserver; - } + while (!_client.ConnectionState.Equals(ConnectionState.Connected)) await Task.Delay(25); } catch (Exception e) { @@ -185,19 +112,117 @@ namespace Geekbot.net } } - private async Task IsConnected() + private DatabaseContext ConnectToDatabase() { - while (!_client.ConnectionState.Equals(ConnectionState.Connected)) - await Task.Delay(25); - return true; + _databaseInitializer = new DatabaseInitializer(_runParameters, _logger); + var database = _databaseInitializer.Initialize(); + database.Database.EnsureCreated(); + if(!_runParameters.InMemory) database.Database.Migrate(); + + return database; } - private Task StartWebApi() + private async Task GetToken() { - _logger.Information(LogSource.Api, "Starting Webserver"); + var token = _runParameters.Token ?? _globalSettings.GetKey("DiscordToken"); + if (string.IsNullOrEmpty(token)) + { + Console.Write("Your bot Token: "); + var newToken = Console.ReadLine(); + await _globalSettings.SetKey("DiscordToken", newToken); + await _globalSettings.SetKey("Game", "Ping Pong"); + token = newToken; + } + + return token; + } + + private void SetupDiscordClient() + { + _client = new DiscordSocketClient(new DiscordSocketConfig + { + LogLevel = LogSeverity.Verbose, + MessageCacheSize = 1000, + ExclusiveBulkDelete = true + }); + + var discordLogger = new DiscordLogger(_logger); + _client.Log += discordLogger.Log; + } + + private void RegisterDependencies() + { + var services = new ServiceCollection(); + + _userRepository = new UserRepository(_databaseInitializer.Initialize(), _logger); + _reactionListener = new ReactionListener(_databaseInitializer.Initialize()); + var fortunes = new FortunesProvider(_logger); + var mediaProvider = new MediaProvider(_logger); + var malClient = new MalClient(_globalSettings, _logger); + var levelCalc = new LevelCalc(); + var emojiConverter = new EmojiConverter(); + var mtgManaConverter = new MtgManaConverter(); + var wikipediaClient = new WikipediaClient(); + var randomNumberGenerator = new RandomNumberGenerator(); + var kvMemoryStore = new KvInInMemoryStore(); + var translationHandler = new TranslationHandler(_databaseInitializer.Initialize(), _logger); + var errorHandler = new ErrorHandler(_logger, translationHandler, _runParameters.ExposeErrors); + + services.AddSingleton(_userRepository); + services.AddSingleton(_logger); + services.AddSingleton(levelCalc); + services.AddSingleton(emojiConverter); + services.AddSingleton(fortunes); + services.AddSingleton(mediaProvider); + services.AddSingleton(malClient); + services.AddSingleton(mtgManaConverter); + services.AddSingleton(wikipediaClient); + services.AddSingleton(randomNumberGenerator); + services.AddSingleton(kvMemoryStore); + services.AddSingleton(_globalSettings); + services.AddSingleton(errorHandler); + services.AddSingleton(translationHandler); + services.AddSingleton(_reactionListener); + services.AddSingleton(_client); + services.AddTransient(e => new HighscoreManager(_databaseInitializer.Initialize(), _userRepository)); + services.AddTransient(e => _databaseInitializer.Initialize()); + + _servicesProvider = services.BuildServiceProvider(); + } + + private async Task RegisterHandlers() + { + var applicationInfo = await _client.GetApplicationInfoAsync(); + + _commands = new CommandService(); + await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _servicesProvider); + + var commandHandler = new CommandHandler(_databaseInitializer.Initialize(), _client, _logger, _servicesProvider, _commands, applicationInfo); + var userHandler = new UserHandler(_userRepository, _logger, _databaseInitializer.Initialize(), _client); + var reactionHandler = new ReactionHandler(_reactionListener); + var statsHandler = new StatsHandler(_logger, _databaseInitializer.Initialize()); + var messageDeletedHandler = new MessageDeletedHandler(_databaseInitializer.Initialize(), _logger, _client); + + _client.MessageReceived += commandHandler.RunCommand; + _client.MessageDeleted += messageDeletedHandler.HandleMessageDeleted; + _client.UserJoined += userHandler.Joined; + _client.UserUpdated += userHandler.Updated; + _client.UserLeft += userHandler.Left; + _client.ReactionAdded += reactionHandler.Added; + _client.ReactionRemoved += reactionHandler.Removed; + if (!_runParameters.InMemory) _client.MessageReceived += statsHandler.UpdateStats; + } + + private void StartWebApi() + { + if (_runParameters.DisableApi) + { + _logger.Warning(LogSource.Api, "Web API is disabled"); + return; + } + var highscoreManager = new HighscoreManager(_databaseInitializer.Initialize(), _userRepository); WebApiStartup.StartWebApi(_logger, _runParameters, _commands, _databaseInitializer.Initialize(), _client, _globalSettings, highscoreManager); - return Task.CompletedTask; } } } \ No newline at end of file From fb676e8918750ccaaad04cb6e2244d831626b0a5 Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 19 Jun 2020 04:10:26 +0200 Subject: [PATCH 223/443] Add GuildSettingsManager to centrally manage guild settings --- Geekbot.net/Commands/Admin/Admin.cs | 107 ++++++------------ Geekbot.net/Handlers/CommandHandler.cs | 16 +-- .../GuildSettingsManager.cs | 68 +++++++++++ .../IGuildSettingsManager.cs | 11 ++ .../Lib/Localization/TranslationHandler.cs | 28 ++--- Geekbot.net/Program.cs | 8 +- 6 files changed, 138 insertions(+), 100 deletions(-) create mode 100644 Geekbot.net/Lib/GuildSettingsManager/GuildSettingsManager.cs create mode 100644 Geekbot.net/Lib/GuildSettingsManager/IGuildSettingsManager.cs diff --git a/Geekbot.net/Commands/Admin/Admin.cs b/Geekbot.net/Commands/Admin/Admin.cs index 15b4df1..9120971 100644 --- a/Geekbot.net/Commands/Admin/Admin.cs +++ b/Geekbot.net/Commands/Admin/Admin.cs @@ -1,15 +1,13 @@ using System; -using System.Linq; using System.Text; using System.Threading.Tasks; using Discord; using Discord.Commands; using Discord.WebSocket; -using Geekbot.net.Database; -using Geekbot.net.Database.Models; using Geekbot.net.Lib.CommandPreconditions; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; +using Geekbot.net.Lib.GuildSettingsManager; using Geekbot.net.Lib.Localization; namespace Geekbot.net.Commands.Admin @@ -21,15 +19,14 @@ namespace Geekbot.net.Commands.Admin { private readonly DiscordSocketClient _client; private readonly IErrorHandler _errorHandler; - private readonly DatabaseContext _database; + private readonly IGuildSettingsManager _guildSettingsManager; private readonly ITranslationHandler _translation; - public Admin(DatabaseContext database, DiscordSocketClient client, IErrorHandler errorHandler, - ITranslationHandler translationHandler) + public Admin(DiscordSocketClient client, IErrorHandler errorHandler, IGuildSettingsManager guildSettingsManager, ITranslationHandler translationHandler) { - _database = database; _client = client; _errorHandler = errorHandler; + _guildSettingsManager = guildSettingsManager; _translation = translationHandler; } @@ -37,11 +34,10 @@ namespace Geekbot.net.Commands.Admin [Summary("Set a Welcome Message (use '$user' to mention the new joined user).")] public async Task SetWelcomeMessage([Remainder, Summary("message")] string welcomeMessage) { - var guild = await GetGuildSettings(Context.Guild.Id); + var guild = _guildSettingsManager.GetSettings(Context.Guild.Id); guild.WelcomeMessage = welcomeMessage; - _database.GuildSettings.Update(guild); - await _database.SaveChangesAsync(); - + await _guildSettingsManager.UpdateSettings(guild); + var formatedMessage = welcomeMessage.Replace("$user", Context.User.Mention); await ReplyAsync($"Welcome message has been changed\r\nHere is an example of how it would look:\r\n{formatedMessage}"); } @@ -54,13 +50,12 @@ namespace Geekbot.net.Commands.Admin { var m = await channel.SendMessageAsync("..."); - var guild = await GetGuildSettings(Context.Guild.Id); + var guild = _guildSettingsManager.GetSettings(Context.Guild.Id); guild.WelcomeChannel = channel.Id.AsLong(); - _database.GuildSettings.Update(guild); - await _database.SaveChangesAsync(); - + await _guildSettingsManager.UpdateSettings(guild); + await m.DeleteAsync(); - + await ReplyAsync("Successfully saved the welcome channel"); } catch (Exception e) @@ -68,7 +63,7 @@ namespace Geekbot.net.Commands.Admin await _errorHandler.HandleCommandException(e, Context, "That channel doesn't seem to exist or i don't have write permissions"); } } - + [Command("modchannel", RunMode = RunMode.Async)] [Summary("Set a channel for moderation purposes")] public async Task SelectModChannel([Summary("#Channel")] ISocketMessageChannel channel) @@ -77,11 +72,10 @@ namespace Geekbot.net.Commands.Admin { var m = await channel.SendMessageAsync("verifying..."); - var guild = await GetGuildSettings(Context.Guild.Id); + var guild = _guildSettingsManager.GetSettings(Context.Guild.Id); guild.ModChannel = channel.Id.AsLong(); - _database.GuildSettings.Update(guild); - await _database.SaveChangesAsync(); - + await _guildSettingsManager.UpdateSettings(guild); + var sb = new StringBuilder(); sb.AppendLine("Successfully saved mod channel, you can now do the following"); sb.AppendLine("- `!admin showleave` - send message to mod channel when someone leaves"); @@ -100,13 +94,12 @@ namespace Geekbot.net.Commands.Admin { try { - var guild = await GetGuildSettings(Context.Guild.Id); + var guild = _guildSettingsManager.GetSettings(Context.Guild.Id); var modChannel = await GetModChannel(guild.ModChannel.AsUlong()); if (modChannel == null) return; - + guild.ShowLeave = !guild.ShowLeave; - _database.GuildSettings.Update(guild); - await _database.SaveChangesAsync(); + await _guildSettingsManager.UpdateSettings(guild); await modChannel.SendMessageAsync(guild.ShowLeave ? "Saved - now sending messages here when someone leaves" : "Saved - stopping sending messages here when someone leaves" @@ -124,13 +117,12 @@ namespace Geekbot.net.Commands.Admin { try { - var guild = await GetGuildSettings(Context.Guild.Id); + var guild = _guildSettingsManager.GetSettings(Context.Guild.Id); var modChannel = await GetModChannel(guild.ModChannel.AsUlong()); if (modChannel == null) return; - + guild.ShowDelete = !guild.ShowDelete; - _database.GuildSettings.Update(guild); - await _database.SaveChangesAsync(); + await _guildSettingsManager.UpdateSettings(guild); await modChannel.SendMessageAsync(guild.ShowDelete ? "Saved - now sending messages here when someone deletes a message" : "Saved - stopping sending messages here when someone deletes a message" @@ -152,11 +144,10 @@ namespace Geekbot.net.Commands.Admin var success = await _translation.SetLanguage(Context.Guild.Id, language); if (success) { - var guild = await GetGuildSettings(Context.Guild.Id); + var guild = _guildSettingsManager.GetSettings(Context.Guild.Id); guild.Language = language; - _database.GuildSettings.Update(guild); - await _database.SaveChangesAsync(); - + await _guildSettingsManager.UpdateSettings(guild); + var transContext = await _translation.GetGuildContext(Context); await ReplyAsync(transContext.GetString("NewLanguageSet")); return; @@ -170,7 +161,7 @@ namespace Geekbot.net.Commands.Admin await _errorHandler.HandleCommandException(e, Context); } } - + [Command("wiki", RunMode = RunMode.Async)] [Summary("Change the wikipedia instance (use lang code in xx.wikipedia.org)")] public async Task SetWikiLanguage([Summary("language")] string languageRaw) @@ -178,11 +169,10 @@ namespace Geekbot.net.Commands.Admin try { var language = languageRaw.ToLower(); - var guild = await GetGuildSettings(Context.Guild.Id); + var guild = _guildSettingsManager.GetSettings(Context.Guild.Id); guild.WikiLang = language; - _database.GuildSettings.Update(guild); - await _database.SaveChangesAsync(); - + await _guildSettingsManager.UpdateSettings(guild); + await ReplyAsync($"Now using the {language} wikipedia"); } catch (Exception e) @@ -190,17 +180,16 @@ namespace Geekbot.net.Commands.Admin await _errorHandler.HandleCommandException(e, Context); } } - + [Command("ping", RunMode = RunMode.Async)] [Summary("Enable the ping reply.")] public async Task TogglePing() { try { - var guild = await GetGuildSettings(Context.Guild.Id); + var guild = _guildSettingsManager.GetSettings(Context.Guild.Id); guild.Ping = !guild.Ping; - _database.GuildSettings.Update(guild); - await _database.SaveChangesAsync(); + await _guildSettingsManager.UpdateSettings(guild); await ReplyAsync(guild.Ping ? "i will reply to ping now" : "No more pongs..."); } catch (Exception e) @@ -208,17 +197,16 @@ namespace Geekbot.net.Commands.Admin await _errorHandler.HandleCommandException(e, Context); } } - + [Command("hui", RunMode = RunMode.Async)] [Summary("Enable the ping reply.")] public async Task ToggleHui() { try { - var guild = await GetGuildSettings(Context.Guild.Id); + var guild = _guildSettingsManager.GetSettings(Context.Guild.Id); guild.Hui = !guild.Hui; - _database.GuildSettings.Update(guild); - await _database.SaveChangesAsync(); + await _guildSettingsManager.UpdateSettings(guild); await ReplyAsync(guild.Hui ? "i will reply to hui now" : "No more hui's..."); } catch (Exception e) @@ -227,41 +215,20 @@ namespace Geekbot.net.Commands.Admin } } - private async Task GetGuildSettings(ulong guildId) - { - var guild = _database.GuildSettings.FirstOrDefault(g => g.GuildId.Equals(guildId.AsLong())); - if (guild != null) return guild; - Console.WriteLine("Adding non-exist Guild Settings to database"); - _database.GuildSettings.Add(new GuildSettingsModel() - { - GuildId = guildId.AsLong(), - Hui = false, - Ping = false, - Language = "EN", - ShowDelete = false, - ShowLeave = false, - WikiLang = "en" - }); - await _database.SaveChangesAsync(); - return _database.GuildSettings.FirstOrDefault(g => g.GuildId.Equals(guildId.AsLong())); - } - private async Task GetModChannel(ulong channelId) { try { - if(channelId == ulong.MinValue) throw new Exception(); + if (channelId == ulong.MinValue) throw new Exception(); var modChannel = (ISocketMessageChannel) _client.GetChannel(channelId); - if(modChannel == null) throw new Exception(); + if (modChannel == null) throw new Exception(); return modChannel; } catch { - await ReplyAsync( - "Modchannel doesn't seem to exist, please set one with `!admin modchannel [channelId]`"); + await ReplyAsync("Modchannel doesn't seem to exist, please set one with `!admin modchannel [channelId]`"); return null; } } - } } \ No newline at end of file diff --git a/Geekbot.net/Handlers/CommandHandler.cs b/Geekbot.net/Handlers/CommandHandler.cs index 5074ff2..b811a8e 100644 --- a/Geekbot.net/Handlers/CommandHandler.cs +++ b/Geekbot.net/Handlers/CommandHandler.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; @@ -8,7 +7,7 @@ using Discord.Rest; using Discord.WebSocket; using Geekbot.net.Database; using Geekbot.net.Database.Models; -using Geekbot.net.Lib.Extensions; +using Geekbot.net.Lib.GuildSettingsManager; using Geekbot.net.Lib.Logger; namespace Geekbot.net.Handlers @@ -20,10 +19,12 @@ namespace Geekbot.net.Handlers private readonly IServiceProvider _servicesProvider; private readonly CommandService _commands; private readonly RestApplication _applicationInfo; + private readonly IGuildSettingsManager _guildSettingsManager; private readonly List _ignoredServers; private readonly DatabaseContext _database; - public CommandHandler(DatabaseContext database, IDiscordClient client, IGeekbotLogger logger, IServiceProvider servicesProvider, CommandService commands, RestApplication applicationInfo) + public CommandHandler(DatabaseContext database, IDiscordClient client, IGeekbotLogger logger, IServiceProvider servicesProvider, CommandService commands, RestApplication applicationInfo, + IGuildSettingsManager guildSettingsManager) { _database = database; _client = client; @@ -31,6 +32,7 @@ namespace Geekbot.net.Handlers _servicesProvider = servicesProvider; _commands = commands; _applicationInfo = applicationInfo; + _guildSettingsManager = guildSettingsManager; // Some guilds only want very specific functionally without any of the commands, a quick hack that solves that "short term" // ToDo: create a clean solution for this... @@ -68,14 +70,14 @@ namespace Geekbot.net.Handlers var argPos = 0; if (!IsCommand(message, ref argPos)) return Task.CompletedTask; - + ExecuteCommand(message, argPos); } catch (Exception e) { _logger.Error(LogSource.Geekbot, "Failed to Process Message", e); } - + return Task.CompletedTask; } @@ -102,7 +104,7 @@ namespace Geekbot.net.Handlers if (!lowerCaseMessage.StartsWith("ping ") && !lowerCaseMessage.Equals("ping")) return false; return GetGuildSettings(guildId)?.Ping ?? false; } - + private bool ShouldHui(string lowerCaseMessage, ulong guildId) { if (!lowerCaseMessage.StartsWith("hui")) return false; @@ -111,7 +113,7 @@ namespace Geekbot.net.Handlers private GuildSettingsModel GetGuildSettings(ulong guildId) { - return _database.GuildSettings.FirstOrDefault(guild => guild.GuildId.Equals(guildId.AsLong())); + return _guildSettingsManager.GetSettings(guildId, false); } } } \ No newline at end of file diff --git a/Geekbot.net/Lib/GuildSettingsManager/GuildSettingsManager.cs b/Geekbot.net/Lib/GuildSettingsManager/GuildSettingsManager.cs new file mode 100644 index 0000000..bfa7556 --- /dev/null +++ b/Geekbot.net/Lib/GuildSettingsManager/GuildSettingsManager.cs @@ -0,0 +1,68 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Geekbot.net.Database; +using Geekbot.net.Database.Models; +using Geekbot.net.Lib.Extensions; + +namespace Geekbot.net.Lib.GuildSettingsManager +{ + public class GuildSettingsManager : IGuildSettingsManager + { + private readonly DatabaseContext _database; + private readonly Dictionary _settings; + + public GuildSettingsManager(DatabaseContext database) + { + _database = database; + _settings = new Dictionary(); + } + + public GuildSettingsModel GetSettings(ulong guildId, bool createIfNonExist = true) + { + return _settings.ContainsKey(guildId) ? _settings[guildId] : GetFromDatabase(guildId, createIfNonExist); + } + + public async Task UpdateSettings(GuildSettingsModel settings) + { + _database.GuildSettings.Update(settings); + if (_settings.ContainsKey(settings.GuildId.AsUlong())) + { + _settings[settings.GuildId.AsUlong()] = settings; + } + else + { + _settings.Add(settings.GuildId.AsUlong(), settings); + } + await _database.SaveChangesAsync(); + } + + private GuildSettingsModel GetFromDatabase(ulong guildId, bool createIfNonExist) + { + var settings = _database.GuildSettings.FirstOrDefault(guild => guild.GuildId.Equals(guildId.AsLong())); + if (createIfNonExist && settings == null) + { + settings = CreateSettings(guildId); + } + + _settings.Add(guildId, settings); + return settings; + } + + private GuildSettingsModel CreateSettings(ulong guildId) + { + _database.GuildSettings.Add(new GuildSettingsModel + { + GuildId = guildId.AsLong(), + Hui = false, + Ping = false, + Language = "EN", + ShowDelete = false, + ShowLeave = false, + WikiLang = "en" + }); + _database.SaveChanges(); + return _database.GuildSettings.FirstOrDefault(g => g.GuildId.Equals(guildId.AsLong())); + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/GuildSettingsManager/IGuildSettingsManager.cs b/Geekbot.net/Lib/GuildSettingsManager/IGuildSettingsManager.cs new file mode 100644 index 0000000..a45b263 --- /dev/null +++ b/Geekbot.net/Lib/GuildSettingsManager/IGuildSettingsManager.cs @@ -0,0 +1,11 @@ +using System.Threading.Tasks; +using Geekbot.net.Database.Models; + +namespace Geekbot.net.Lib.GuildSettingsManager +{ + public interface IGuildSettingsManager + { + GuildSettingsModel GetSettings(ulong guildId, bool createIfNonExist = true); + Task UpdateSettings(GuildSettingsModel settings); + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/Localization/TranslationHandler.cs b/Geekbot.net/Lib/Localization/TranslationHandler.cs index a7a1a12..89ba632 100644 --- a/Geekbot.net/Lib/Localization/TranslationHandler.cs +++ b/Geekbot.net/Lib/Localization/TranslationHandler.cs @@ -4,9 +4,7 @@ using System.IO; using System.Linq; using System.Threading.Tasks; using Discord.Commands; -using Geekbot.net.Database; -using Geekbot.net.Database.Models; -using Geekbot.net.Lib.Extensions; +using Geekbot.net.Lib.GuildSettingsManager; using Geekbot.net.Lib.Logger; using YamlDotNet.Core; using YamlDotNet.Serialization; @@ -15,15 +13,15 @@ namespace Geekbot.net.Lib.Localization { public class TranslationHandler : ITranslationHandler { - private readonly DatabaseContext _database; private readonly IGeekbotLogger _logger; + private readonly IGuildSettingsManager _guildSettingsManager; private readonly Dictionary _serverLanguages; private Dictionary>> _translations; - public TranslationHandler(DatabaseContext database, IGeekbotLogger logger) + public TranslationHandler(IGeekbotLogger logger, IGuildSettingsManager guildSettingsManager) { - _database = database; _logger = logger; + _guildSettingsManager = guildSettingsManager; _logger.Information(LogSource.Geekbot, "Loading Translations"); LoadTranslations(); _serverLanguages = new Dictionary(); @@ -107,7 +105,7 @@ namespace Geekbot.net.Lib.Localization } catch { - lang = (await GetGuild(guildId)).Language ?? "EN"; + lang = _guildSettingsManager.GetSettings(guildId, false)?.Language ?? "EN"; _serverLanguages[guildId] = lang; return lang; } @@ -178,9 +176,9 @@ namespace Geekbot.net.Lib.Localization try { if (!SupportedLanguages.Contains(language)) return false; - var guild = await GetGuild(guildId); + var guild = _guildSettingsManager.GetSettings(guildId); guild.Language = language; - _database.GuildSettings.Update(guild); + await _guildSettingsManager.UpdateSettings(guild); _serverLanguages[guildId] = language; return true; } @@ -192,17 +190,5 @@ namespace Geekbot.net.Lib.Localization } public List SupportedLanguages { get; private set; } - - private async Task GetGuild(ulong guildId) - { - var guild = _database.GuildSettings.FirstOrDefault(g => g.GuildId.Equals(guildId.AsLong())); - if (guild != null) return guild; - _database.GuildSettings.Add(new GuildSettingsModel - { - GuildId = guildId.AsLong() - }); - await _database.SaveChangesAsync(); - return _database.GuildSettings.FirstOrDefault(g => g.GuildId.Equals(guildId.AsLong())); - } } } \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 262d830..1299583 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -13,6 +13,7 @@ using Geekbot.net.Lib.Clients; using Geekbot.net.Lib.Converters; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.GlobalSettings; +using Geekbot.net.Lib.GuildSettingsManager; using Geekbot.net.Lib.Highscores; using Geekbot.net.Lib.KvInMemoryStore; using Geekbot.net.Lib.Levels; @@ -40,6 +41,7 @@ namespace Geekbot.net private IUserRepository _userRepository; private RunParameters _runParameters; private IReactionListener _reactionListener; + private IGuildSettingsManager _guildSettingsManager; private static void Main(string[] args) { @@ -156,6 +158,7 @@ namespace Geekbot.net _userRepository = new UserRepository(_databaseInitializer.Initialize(), _logger); _reactionListener = new ReactionListener(_databaseInitializer.Initialize()); + _guildSettingsManager = new GuildSettingsManager(_databaseInitializer.Initialize()); var fortunes = new FortunesProvider(_logger); var mediaProvider = new MediaProvider(_logger); var malClient = new MalClient(_globalSettings, _logger); @@ -165,7 +168,7 @@ namespace Geekbot.net var wikipediaClient = new WikipediaClient(); var randomNumberGenerator = new RandomNumberGenerator(); var kvMemoryStore = new KvInInMemoryStore(); - var translationHandler = new TranslationHandler(_databaseInitializer.Initialize(), _logger); + var translationHandler = new TranslationHandler(_logger, _guildSettingsManager); var errorHandler = new ErrorHandler(_logger, translationHandler, _runParameters.ExposeErrors); services.AddSingleton(_userRepository); @@ -183,6 +186,7 @@ namespace Geekbot.net services.AddSingleton(errorHandler); services.AddSingleton(translationHandler); services.AddSingleton(_reactionListener); + services.AddSingleton(_guildSettingsManager); services.AddSingleton(_client); services.AddTransient(e => new HighscoreManager(_databaseInitializer.Initialize(), _userRepository)); services.AddTransient(e => _databaseInitializer.Initialize()); @@ -197,7 +201,7 @@ namespace Geekbot.net _commands = new CommandService(); await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _servicesProvider); - var commandHandler = new CommandHandler(_databaseInitializer.Initialize(), _client, _logger, _servicesProvider, _commands, applicationInfo); + var commandHandler = new CommandHandler(_databaseInitializer.Initialize(), _client, _logger, _servicesProvider, _commands, applicationInfo, _guildSettingsManager); var userHandler = new UserHandler(_userRepository, _logger, _databaseInitializer.Initialize(), _client); var reactionHandler = new ReactionHandler(_reactionListener); var statsHandler = new StatsHandler(_logger, _databaseInitializer.Initialize()); From 1ea851be22cff73ee85536a287f5f55fdf902de2 Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 19 Jun 2020 04:13:26 +0200 Subject: [PATCH 224/443] Release Geekbot 4.2 --- .gitlab-ci.yml | 6 +++--- Geekbot.net/Geekbot.net.csproj | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index cffe539..7b6b593 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -29,9 +29,9 @@ sentry: dependencies: - build script: - - sentry-cli releases new -p geekbot 4.1.0-${CI_COMMIT_SHA:0:8} - - sentry-cli releases set-commits --auto 4.1.0-${CI_COMMIT_SHA:0:8} - - sentry-cli releases deploys 4.1.0-${CI_COMMIT_SHA:0:8} new -e Production + - sentry-cli releases new -p geekbot 4.2.0-${CI_COMMIT_SHA:0:8} + - sentry-cli releases set-commits --auto 4.2.0-${CI_COMMIT_SHA:0:8} + - sentry-cli releases deploys 4.2.0-${CI_COMMIT_SHA:0:8} new -e Production deploy: stage: deploy diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 0d39cae..0d58104 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -4,7 +4,7 @@ netcoreapp3.1 win-x64;linux-x64 derp.ico - 4.1.0 + 4.2.0 $(VersionSuffix) $(Version)-$(VersionSuffix) $(Version)-DEV From 76bb645394d8cb447d8495b9334f06d20aa774c6 Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 19 Jun 2020 12:08:28 +0200 Subject: [PATCH 225/443] Inline MTG Emoji converter --- Geekbot.net/Geekbot.net.csproj | 3 -- .../Lib/Converters/MtgManaConverter.cs | 54 ++++++++++++++++++- Geekbot.net/Lib/Converters/MtgManaEmojis.json | 50 ----------------- 3 files changed, 52 insertions(+), 55 deletions(-) delete mode 100644 Geekbot.net/Lib/Converters/MtgManaEmojis.json diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 0d58104..b3f537e 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -82,9 +82,6 @@ PreserveNewest - - Always - PreserveNewest diff --git a/Geekbot.net/Lib/Converters/MtgManaConverter.cs b/Geekbot.net/Lib/Converters/MtgManaConverter.cs index 31a2db0..7e90df0 100644 --- a/Geekbot.net/Lib/Converters/MtgManaConverter.cs +++ b/Geekbot.net/Lib/Converters/MtgManaConverter.cs @@ -12,8 +12,57 @@ namespace Geekbot.net.Lib.Converters public MtgManaConverter() { // these emotes can be found at https://discord.gg/bz8HyA7 - var mtgEmojis = File.ReadAllText(Path.GetFullPath("./Lib/Converters/MtgManaEmojis.json")); - _manaDict = JsonSerializer.Deserialize>(mtgEmojis); + _manaDict = new Dictionary + { + {"{0}", "<:mtg_0:415216130043412482>"}, + {"{1}", "<:mtg_1:415216130253389835>"}, + {"{2}", "<:mtg_2:415216130031091713>"}, + {"{3}", "<:mtg_3:415216130467037194>"}, + {"{4}", "<:mtg_4:415216130026635295>"}, + {"{5}", "<:mtg_5:415216130492203008>"}, + {"{6}", "<:mtg_6:415216130458779658>"}, + {"{7}", "<:mtg_7:415216130190475265>"}, + {"{8}", "<:mtg_8:415216130517630986>"}, + {"{9}", "<:mtg_9:415216130500722689>"}, + {"{10", "<:mtg_10:415216130450391051>"}, + {"{11}", "<:mtg_11:415216130811101185>"}, + {"{12}", "<:mtg_12:415216130525888532>"}, + {"{13}", "<:mtg_13:415216130517631000>"}, + {"{14}", "<:mtg_14:415216130165178370>"}, + {"{15}", "<:mtg_15:415216130576089108>"}, + {"{16}", "<:mtg_16:415216130358247425>"}, + {"{17}", "<:mtg_17:415216130601517056>"}, + {"{18}", "<:mtg_18:415216130462842891>"}, + {"{19}", "<:mtg_19:415216130614099988>"}, + {"{20}", "<:mtg_20:415216130656043038>"}, + {"{W}", "<:mtg_white:415216131515744256>"}, + {"{U}", "<:mtg_blue:415216130521694209>"}, + {"{B}", "<:mtg_black:415216130873884683>"}, + {"{R}", "<:mtg_red:415216131322806272>"}, + {"{G}", "<:mtg_green:415216131180331009>"}, + {"{S}", "<:mtg_s:415216131293446144>"}, + {"{T}", "<:mtg_tap:415258392727257088>"}, + {"{C}", "<:mtg_colorless:415216130706374666>"}, + {"{2/W}", "<:mtg_2w:415216130446065664>"}, + {"{2/U}", "<:mtg_2u:415216130429550592>"}, + {"{2/B}", "<:mtg_2b:415216130160984065>"}, + {"{2/R}", "<:mtg_2r:415216130454716436>"}, + {"{2/G}", "<:mtg_2g:415216130420899840>"}, + {"{W/U}", "<:mtg_wu:415216130970484736>"}, + {"{W/B}", "<:mtg_wb:415216131222011914>"}, + {"{U/R}", "<:mtg_ur:415216130962096128>"}, + {"{U/B}", "<:mtg_ub:415216130865758218>"}, + {"{R/W}", "<:mtg_rw:415216130878210057>"}, + {"{G/W}", "<:mtg_gw:415216130567962646>"}, + {"{G/U}", "<:mtg_gu:415216130739666945>"}, + {"{B/R}", "<:mtg_br:415216130580283394>"}, + {"{B/G}", "<:mtg_bg:415216130781609994>"}, + {"{U/P}", "<:mtg_up:415216130861432842>"}, + {"{R/P}", "<:mtg_rp:415216130597322783>"}, + {"{G/P}", "<:mtg_gp:415216130760769546>"}, + {"{W/P}", "<:mtg_wp:415216131541041172>"}, + {"{B/P}", "<:mtg_bp:415216130664169482>"} + }; } public string ConvertMana(string mana) @@ -27,6 +76,7 @@ namespace Geekbot.net.Lib.Converters mana = mana.Replace(manaTypes.Value, m); } } + return mana; } } diff --git a/Geekbot.net/Lib/Converters/MtgManaEmojis.json b/Geekbot.net/Lib/Converters/MtgManaEmojis.json deleted file mode 100644 index 8ebe75b..0000000 --- a/Geekbot.net/Lib/Converters/MtgManaEmojis.json +++ /dev/null @@ -1,50 +0,0 @@ -{ - "{0}": "<:mtg_0:415216130043412482>", - "{1}": "<:mtg_1:415216130253389835>", - "{2}": "<:mtg_2:415216130031091713>", - "{3}": "<:mtg_3:415216130467037194>", - "{4}": "<:mtg_4:415216130026635295>", - "{5}": "<:mtg_5:415216130492203008>", - "{6}": "<:mtg_6:415216130458779658>", - "{7}": "<:mtg_7:415216130190475265>", - "{8}": "<:mtg_8:415216130517630986>", - "{9}": "<:mtg_9:415216130500722689>", - "{10": "<:mtg_10:415216130450391051>", - "{11}": "<:mtg_11:415216130811101185>", - "{12}": "<:mtg_12:415216130525888532>", - "{13}": "<:mtg_13:415216130517631000>", - "{14}": "<:mtg_14:415216130165178370>", - "{15}": "<:mtg_15:415216130576089108>", - "{16}": "<:mtg_16:415216130358247425>", - "{17}": "<:mtg_17:415216130601517056>", - "{18}": "<:mtg_18:415216130462842891>", - "{19}": "<:mtg_19:415216130614099988>", - "{20}": "<:mtg_20:415216130656043038>", - "{W}": "<:mtg_white:415216131515744256>", - "{U}": "<:mtg_blue:415216130521694209>", - "{B}": "<:mtg_black:415216130873884683>", - "{R}": "<:mtg_red:415216131322806272>", - "{G}": "<:mtg_green:415216131180331009>", - "{S}": "<:mtg_s:415216131293446144>", - "{T}": "<:mtg_tap:415258392727257088>", - "{C}": "<:mtg_colorless:415216130706374666>", - "{2/W}": "<:mtg_2w:415216130446065664>", - "{2/U}": "<:mtg_2u:415216130429550592>", - "{2/B}": "<:mtg_2b:415216130160984065>", - "{2/R}": "<:mtg_2r:415216130454716436>", - "{2/G}": "<:mtg_2g:415216130420899840>", - "{W/U}": "<:mtg_wu:415216130970484736>", - "{W/B}": "<:mtg_wb:415216131222011914>", - "{U/R}": "<:mtg_ur:415216130962096128>", - "{U/B}": "<:mtg_ub:415216130865758218>", - "{R/W}": "<:mtg_rw:415216130878210057>", - "{G/W}": "<:mtg_gw:415216130567962646>", - "{G/U}": "<:mtg_gu:415216130739666945>", - "{B/R}": "<:mtg_br:415216130580283394>", - "{B/G}": "<:mtg_bg:415216130781609994>", - "{U/P}": "<:mtg_up:415216130861432842>", - "{R/P}": "<:mtg_rp:415216130597322783>", - "{G/P}": "<:mtg_gp:415216130760769546>", - "{W/P}": "<:mtg_wp:415216131541041172>", - "{B/P}": "<:mtg_bp:415216130664169482>" -} \ No newline at end of file From e564e80849c5f425dac1c729120966de5a7dbcb4 Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 19 Jun 2020 12:15:17 +0200 Subject: [PATCH 226/443] Copy every file in storage by default instead of listing every single one --- Geekbot.net/Geekbot.net.csproj | 26 +------------------------- 1 file changed, 1 insertion(+), 25 deletions(-) diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index b3f537e..23f331f 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -58,31 +58,7 @@ - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - + PreserveNewest From 9cc944fcc10d7fd284de89357c7a65ff2535dce0 Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 19 Jun 2020 12:28:22 +0200 Subject: [PATCH 227/443] Use a single message for !mtg and show a searching message when the command starts executing --- Geekbot.net/Commands/Integrations/MagicTheGathering.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Geekbot.net/Commands/Integrations/MagicTheGathering.cs b/Geekbot.net/Commands/Integrations/MagicTheGathering.cs index 1075c46..f9e006c 100644 --- a/Geekbot.net/Commands/Integrations/MagicTheGathering.cs +++ b/Geekbot.net/Commands/Integrations/MagicTheGathering.cs @@ -28,6 +28,8 @@ namespace Geekbot.net.Commands.Integrations { try { + var message = await Context.Channel.SendMessageAsync($":mag: Looking up\"{cardName}\", please wait..."); + var service = new CardService(); var result = service .Where(x => x.Name, cardName) @@ -37,7 +39,7 @@ namespace Geekbot.net.Commands.Integrations var card = result.All().Value.FirstOrDefault(); if (card == null) { - await ReplyAsync("I couldn't find that card..."); + await message.ModifyAsync(properties => properties.Content = ":red_circle: I couldn't find a card with that name..."); return; } @@ -65,7 +67,11 @@ namespace Geekbot.net.Commands.Integrations if (card.Legalities != null && card.Legalities.Count > 0) eb.AddField("Legality", string.Join(", ", card.Legalities.Select(e => e.Format))); - await ReplyAsync("", false, eb.Build()); + await message.ModifyAsync(properties => + { + properties.Content = string.Empty; + properties.Embed = eb.Build(); + }); } catch (Exception e) { From 4655424fb034adbf9f0c11c60a50565ddbe8afc4 Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 19 Jun 2020 12:40:17 +0200 Subject: [PATCH 228/443] Remove prometheus-net from the dependencies --- Geekbot.net/Geekbot.net.csproj | 1 - 1 file changed, 1 deletion(-) diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 23f331f..8fc35f4 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -43,7 +43,6 @@ - From a0b1ec44f65ef39495755326f39fd278c04d906e Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 19 Jun 2020 21:46:19 +0200 Subject: [PATCH 229/443] Make it possible to connect to the database with ssl enabled --- Geekbot.net/Database/DatabaseInitializer.cs | 4 +++- Geekbot.net/Database/SqlConnectionString.cs | 5 ++++- Geekbot.net/Lib/RunParameters.cs | 6 ++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Geekbot.net/Database/DatabaseInitializer.cs b/Geekbot.net/Database/DatabaseInitializer.cs index 582327f..637e7b8 100644 --- a/Geekbot.net/Database/DatabaseInitializer.cs +++ b/Geekbot.net/Database/DatabaseInitializer.cs @@ -35,7 +35,9 @@ namespace Geekbot.net.Database Port = _runParameters.DbPort, Database = _runParameters.DbDatabase, Username = _runParameters.DbUser, - Password = _runParameters.DbPassword + Password = _runParameters.DbPassword, + RequireSsl = _runParameters.DbSsl, + TrustServerCertificate = _runParameters.DbTrustCert }); } } diff --git a/Geekbot.net/Database/SqlConnectionString.cs b/Geekbot.net/Database/SqlConnectionString.cs index 3228a05..70346eb 100644 --- a/Geekbot.net/Database/SqlConnectionString.cs +++ b/Geekbot.net/Database/SqlConnectionString.cs @@ -7,10 +7,13 @@ public string Database { get; set; } public string Username { get; set; } public string Password { get; set; } + public bool RequireSsl { get; set; } + public bool TrustServerCertificate { get; set; } public override string ToString() { - return $"Server={Host};Port={Port};Database={Database};Uid={Username};Pwd={Password};"; + var sslMode = RequireSsl ? "Require" : "Prefer"; + return $"ApplicationName=Geekbot;Server={Host};Port={Port};Database={Database};Uid={Username};Pwd={Password};SSLMode={sslMode};TrustServerCertificate={TrustServerCertificate.ToString()};"; } } } \ No newline at end of file diff --git a/Geekbot.net/Lib/RunParameters.cs b/Geekbot.net/Lib/RunParameters.cs index 1ddbd46..8ebed67 100644 --- a/Geekbot.net/Lib/RunParameters.cs +++ b/Geekbot.net/Lib/RunParameters.cs @@ -43,6 +43,12 @@ namespace Geekbot.net.Lib [Option("db-password", HelpText = "Set a posgresql password (default: empty) (env: DB_PASSWORD)")] public string DbPassword { get; set; } = ParamFallback("DB_PASSWORD", ""); + + [Option("db-require-ssl", HelpText = "Require SSL to connect to the database (default: false) (env: DB_REQUIRE_SSL)")] + public bool DbSsl { get; set; } = ParamFallback("DB_REQUIRE_SSL", false); + + [Option("db-trust-cert", HelpText = "Trust the database certificate, regardless if it is valid (default: false) (env: DB_TRUST_CERT)")] + public bool DbTrustCert { get; set; } = ParamFallback("DB_TRUST_CERT", false); // Logging [Option("db-logging", HelpText = "Enable database logging (default: false) (env: DB_LOGGING)")] From e0f17d00ea90021e1864963de6a60938087cb8b9 Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 19 Jun 2020 22:19:26 +0200 Subject: [PATCH 230/443] Upgrade to .NET 5 --- .gitlab-ci.yml | 2 +- Geekbot.net/Geekbot.net.csproj | 3 +-- Tests/Tests.csproj | 2 +- WikipediaApi/WikipediaApi.csproj | 2 +- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 7b6b593..d6f3e15 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -10,7 +10,7 @@ before_script: build: stage: build - image: mcr.microsoft.com/dotnet/core/sdk:3.1 + image: mcr.microsoft.com/dotnet/core/sdk:5.0-focal artifacts: expire_in: 1h paths: diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 8fc35f4..4e13438 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -1,7 +1,7 @@  Exe - netcoreapp3.1 + net5.0 win-x64;linux-x64 derp.ico 4.2.0 @@ -53,7 +53,6 @@ 4.3.0 - diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 345dacd..eaf88a3 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -1,6 +1,6 @@  - netcoreapp3.1 + net5.0 false NU1701 xUnit1026 diff --git a/WikipediaApi/WikipediaApi.csproj b/WikipediaApi/WikipediaApi.csproj index 24c20f7..e53e3c2 100644 --- a/WikipediaApi/WikipediaApi.csproj +++ b/WikipediaApi/WikipediaApi.csproj @@ -1,6 +1,6 @@  - netcoreapp3.1 + net5.0 From f23b8099f1b5687cf17b62dfe1a33c8f951d7e32 Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 19 Jun 2020 22:20:05 +0200 Subject: [PATCH 231/443] Try to create a docker image --- .gitignore | 10 +-- .gitlab-ci.yml | 62 ++++++++++--------- Dockerfile | 6 +- .../Lib/Converters/MtgManaConverter.cs | 2 - 4 files changed, 38 insertions(+), 42 deletions(-) diff --git a/.gitignore b/.gitignore index fe7e3d4..5db124b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,6 @@ -Geekbot.net/bin -Geekbot.net/obj +*/bin/ +*/obj/ Geekbot.net/tmp/ -Tests/bin -Tests/obj -Backup/ .vs/ UpgradeLog.htm .idea @@ -12,5 +9,4 @@ Geekbot.net/Logs/* !/Geekbot.net/Logs/.keep Geekbot.net.sln.DotSettings.user Geekbot.net/temp/ -WikipediaApi/bin/ -WikipediaApi/obj/ +app \ No newline at end of file diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index d6f3e15..86c6459 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,46 +1,40 @@ stages: - build - - ops + - docker - deploy + - ops -before_script: - - set -e - - set -u - - set -o pipefail - -build: +Build: stage: build image: mcr.microsoft.com/dotnet/core/sdk:5.0-focal artifacts: expire_in: 1h paths: - - Geekbot.net/Binaries/ + - app script: - dotnet restore - dotnet test Tests - - dotnet publish --version-suffix ${CI_COMMIT_SHA:0:8} --configuration Release -o ./Geekbot.net/Binaries ./Geekbot.net/ + - dotnet publish --version-suffix ${CI_COMMIT_SHA:0:8} -r linux-x64 -c Release -o ./app ./Geekbot.net/ -sentry: - stage: ops - image: getsentry/sentry-cli - allow_failure: true - only: - - master - dependencies: - - build - script: - - sentry-cli releases new -p geekbot 4.2.0-${CI_COMMIT_SHA:0:8} - - sentry-cli releases set-commits --auto 4.2.0-${CI_COMMIT_SHA:0:8} - - sentry-cli releases deploys 4.2.0-${CI_COMMIT_SHA:0:8} new -e Production +Package: + stage: docker + image: docker +# only: +# - master + services: + - docker:stable-dind + variables: + IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG + script: + - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY + - docker build -t $IMAGE_TAG . + - docker push $IMAGE_TAG -deploy: +Deploy: stage: deploy image: runebaas/rsync-ssh-git only: - master - dependencies: - - build - - sentry environment: name: Production url: https://discordapp.com/oauth2/authorize?client_id=171249478546882561&scope=bot&permissions=1416834054 @@ -54,13 +48,21 @@ deploy: - rsync -rav -e "ssh -p 65432" ./Geekbot.net/Binaries/* geekbot@$DEPIP:$DEPPATH - ssh -p 65432 geekbot@$DEPIP "sudo systemctl restart geekbot.service" -mirror: - stage: deploy +Sentry: + stage: ops + image: getsentry/sentry-cli + allow_failure: true + only: + - master + script: + - sentry-cli releases new -p geekbot 4.2.0-${CI_COMMIT_SHA:0:8} + - sentry-cli releases set-commits --auto 4.2.0-${CI_COMMIT_SHA:0:8} + - sentry-cli releases deploys 4.2.0-${CI_COMMIT_SHA:0:8} new -e Production + +Github Mirror: + stage: ops image: runebaas/rsync-ssh-git only: - master - dependencies: - - build - - sentry script: - git push https://runebaas:$TOKEN@github.com/pizzaandcoffee/Geekbot.net.git origin/master:master -f diff --git a/Dockerfile b/Dockerfile index 7594bec..822471d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ -FROM microsoft/dotnet:2.1-aspnetcore-runtime +FROM mcr.microsoft.com/dotnet/core/aspnet:5.0-focal -COPY Geekbot.net/Binaries /app/ +COPY ./app /app/ EXPOSE 12995/tcp WORKDIR /app -ENTRYPOINT ./run.sh +ENTRYPOINT ./Geekbot.net diff --git a/Geekbot.net/Lib/Converters/MtgManaConverter.cs b/Geekbot.net/Lib/Converters/MtgManaConverter.cs index 7e90df0..cfd893b 100644 --- a/Geekbot.net/Lib/Converters/MtgManaConverter.cs +++ b/Geekbot.net/Lib/Converters/MtgManaConverter.cs @@ -1,7 +1,5 @@ using System.Collections.Generic; -using System.IO; using System.Text.RegularExpressions; -using Utf8Json; namespace Geekbot.net.Lib.Converters { From 3213e10b88ef131a53d7ad53cf2cf77e0e154d29 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 20 Jun 2020 00:20:00 +0200 Subject: [PATCH 232/443] Add Sumologic and Sentry to the run parameters --- Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs | 6 +++--- Geekbot.net/Lib/Logger/GeekbotLogger.cs | 6 +++--- Geekbot.net/Lib/Logger/LoggerFactory.cs | 6 +++--- Geekbot.net/Lib/RunParameters.cs | 10 ++++++++++ Geekbot.net/Program.cs | 5 ++--- 5 files changed, 21 insertions(+), 12 deletions(-) diff --git a/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs b/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs index 7c9d8ff..cda9129 100644 --- a/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs +++ b/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs @@ -18,13 +18,13 @@ namespace Geekbot.net.Lib.ErrorHandling private readonly IRavenClient _raven; private readonly bool _errorsInChat; - public ErrorHandler(IGeekbotLogger logger, ITranslationHandler translation, bool errorsInChat) + public ErrorHandler(IGeekbotLogger logger, ITranslationHandler translation, RunParameters runParameters) { _logger = logger; _translation = translation; - _errorsInChat = errorsInChat; + _errorsInChat = runParameters.ExposeErrors; - var sentryDsn = Environment.GetEnvironmentVariable("SENTRY"); + var sentryDsn = runParameters.SentryEndpoint; if (!string.IsNullOrEmpty(sentryDsn)) { _raven = new RavenClient(sentryDsn) { Release = Constants.BotVersion(), Environment = "Production" }; diff --git a/Geekbot.net/Lib/Logger/GeekbotLogger.cs b/Geekbot.net/Lib/Logger/GeekbotLogger.cs index 09a3ecb..e2a3714 100644 --- a/Geekbot.net/Lib/Logger/GeekbotLogger.cs +++ b/Geekbot.net/Lib/Logger/GeekbotLogger.cs @@ -9,10 +9,10 @@ namespace Geekbot.net.Lib.Logger private readonly NLog.Logger _logger; private readonly JsonSerializerSettings _serializerSettings; - public GeekbotLogger(RunParameters runParameters, bool sumologicActive) + public GeekbotLogger(RunParameters runParameters) { - _logAsJson = sumologicActive || runParameters.LogJson; - _logger = LoggerFactory.CreateNLog(runParameters, sumologicActive); + _logAsJson = !string.IsNullOrEmpty(runParameters.SumologicEndpoint) || runParameters.LogJson; + _logger = LoggerFactory.CreateNLog(runParameters); _serializerSettings = new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Serialize, diff --git a/Geekbot.net/Lib/Logger/LoggerFactory.cs b/Geekbot.net/Lib/Logger/LoggerFactory.cs index b07bcda..e31686e 100644 --- a/Geekbot.net/Lib/Logger/LoggerFactory.cs +++ b/Geekbot.net/Lib/Logger/LoggerFactory.cs @@ -9,18 +9,18 @@ namespace Geekbot.net.Lib.Logger { public class LoggerFactory { - public static NLog.Logger CreateNLog(RunParameters runParameters, bool sumologicActive) + public static NLog.Logger CreateNLog(RunParameters runParameters) { var config = new LoggingConfiguration(); - if (sumologicActive) + if (!string.IsNullOrEmpty(runParameters.SumologicEndpoint)) { Console.WriteLine("Logging Geekbot Logs to Sumologic"); config.LoggingRules.Add( new LoggingRule("*", LogLevel.Debug, LogLevel.Fatal, new SumoLogicTarget() { - Url = Environment.GetEnvironmentVariable("GEEKBOT_SUMO"), + Url = runParameters.SumologicEndpoint, SourceName = "GeekbotLogger", Layout = "${message}", UseConsoleLog = false, diff --git a/Geekbot.net/Lib/RunParameters.cs b/Geekbot.net/Lib/RunParameters.cs index 8ebed67..bd8ce25 100644 --- a/Geekbot.net/Lib/RunParameters.cs +++ b/Geekbot.net/Lib/RunParameters.cs @@ -67,6 +67,16 @@ namespace Geekbot.net.Lib [Option("api-port", HelpText = "Port on which the WebApi listens (default: 12995) (env: API_PORT)")] public string ApiPort { get; set; } = ParamFallback("API_PORT", "12995"); + /************************************ + * Intergrations * + ************************************/ + + [Option("sumologic", HelpText = "Sumologic endpoint for logging (default: null) (env: SUMOLOGIC)")] + public string SumologicEndpoint { get; set; } = ParamFallback("SUMOLOGIC"); + + [Option("sentry", HelpText = "Sentry endpoint for error reporting (default: null) (env: SENTRY)")] + public string SentryEndpoint { get; set; } = ParamFallback("SENTRY"); + /************************************ * Helper Functions * ************************************/ diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 1299583..a5b0668 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -58,8 +58,7 @@ namespace Geekbot.net logo.AppendLine(@" \____|_____|_____|_|\_\____/ \___/ |_|"); logo.AppendLine($"Version {Constants.BotVersion()} ".PadRight(41, '=')); Console.WriteLine(logo.ToString()); - var sumologicActive = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GEEKBOT_SUMO")); - var logger = new GeekbotLogger(runParameters, sumologicActive); + var logger = new GeekbotLogger(runParameters); logger.Information(LogSource.Geekbot, "Starting..."); try { @@ -169,7 +168,7 @@ namespace Geekbot.net var randomNumberGenerator = new RandomNumberGenerator(); var kvMemoryStore = new KvInInMemoryStore(); var translationHandler = new TranslationHandler(_logger, _guildSettingsManager); - var errorHandler = new ErrorHandler(_logger, translationHandler, _runParameters.ExposeErrors); + var errorHandler = new ErrorHandler(_logger, translationHandler, _runParameters); services.AddSingleton(_userRepository); services.AddSingleton(_logger); From a4b914d576fd3effdded6ad13c8fd3f3db30faf8 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 20 Jun 2020 00:43:59 +0200 Subject: [PATCH 233/443] Use Ansible for deployment --- .deploy.yml | 37 +++++++++++++++++++++++++++++++++++++ .gitlab-ci.yml | 31 +++++++++++++++++-------------- 2 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 .deploy.yml diff --git a/.deploy.yml b/.deploy.yml new file mode 100644 index 0000000..39e3233 --- /dev/null +++ b/.deploy.yml @@ -0,0 +1,37 @@ +--- +- name: Geekbot Deploy + hosts: all + remote_user: geekbot + vars: + ansible_port: 65432 + ansible_python_interpreter: /usr/bin/python3 + tasks: + - name: Login to Gitlab Docker Registry + docker_login: + registry_url: "{{ lookup('env', 'CI_REGISTRY') }}" + username: "{{ lookup('env', 'CI_REGISTRY_USER') }}" + password: "{{ lookup('env', 'CI_REGISTRY_PASSWORD') }}" + reauthorize: yes + - name: Replace Prod Container + docker_container: + name: GeekbotProd + image: "{{ lookup('env', 'IMAGE_TAG') }}" + recreate: yes + pull: yes + restart_policy: always + keep_volumes: no + ports: + - "12995:12995" + env: + GEEKBOT_DB_HOST: "{{ lookup('env', 'GEEKBOT_DB_HOST') }}" + GEEKBOT_DB_USER: "{{ lookup('env', 'GEEKBOT_DB_USER') }}" + GEEKBOT_DB_PASSWORD: "{{ lookup('env', 'GEEKBOT_DB_PASSWORD') }}" + GEEKBOT_DB_PORT: "{{ lookup('env', 'GEEKBOT_DB_PORT') }}" + GEEKBOT_DB_DATABASE: "{{ lookup('env', 'GEEKBOT_DB_DATABASE') }}" + GEEKBOT_DB_REQUIRE_SSL: "true" + GEEKBOT_DB_TRUST_CERT: "true" + GEEKBOT_SUMOLOCIG: "{{ lookup('env', 'GEEKBOT_SUMOLOCIG') }}" + GEEKBOT_SENTRY: "{{ lookup('env', 'GEEKBOT_SENTRY') }}" + - name: Cleanup Old Container + docker_prune: + images: yes diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 86c6459..6015b14 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -4,6 +4,8 @@ stages: - deploy - ops +.imageTag: &IMAGE_TAG $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG + Build: stage: build image: mcr.microsoft.com/dotnet/core/sdk:5.0-focal @@ -19,12 +21,13 @@ Build: Package: stage: docker image: docker -# only: -# - master + only: + - master + - docker services: - docker:stable-dind variables: - IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG + IMAGE_TAG: *IMAGE_TAG script: - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY - docker build -t $IMAGE_TAG . @@ -32,21 +35,21 @@ Package: Deploy: stage: deploy - image: runebaas/rsync-ssh-git + image: ansible/ansible-runner only: - master - environment: - name: Production - url: https://discordapp.com/oauth2/authorize?client_id=171249478546882561&scope=bot&permissions=1416834054 + - docker + variables: + ANSIBLE_NOCOWS: 1 + IMAGE_TAG: *IMAGE_TAG before_script: - - eval $(ssh-agent -s) - - mkdir -p ~/.ssh - - '[[ -f /.dockerenv ]] && echo -e "Host *\n StrictHostKeyChecking no" > ~/.ssh/config' - - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null - - chmod 700 ~/.ssh + - mkdir /root/.ssh + - cp $SSH_PRIVATE_KEY /root/.ssh/id_ed25519 + - cp $SSH_PUBLIC_KEY /root/.ssh/id_ed25519.pub + - chmod -R 600 /root/.ssh + - ssh-keyscan -p 65432 $PROD_IP > /root/.ssh/known_hosts script: - - rsync -rav -e "ssh -p 65432" ./Geekbot.net/Binaries/* geekbot@$DEPIP:$DEPPATH - - ssh -p 65432 geekbot@$DEPIP "sudo systemctl restart geekbot.service" + - ansible-playbook -i $PROD_IP, .deploy.yml Sentry: stage: ops From d9f8e9a80ecb76b89c19fbfa2445cd789088754d Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 20 Jun 2020 03:05:51 +0200 Subject: [PATCH 234/443] Add redshift (and DigitalOcean Managed DB) compatibility and start using a string building to create the sql connection string --- .deploy.yml | 1 + Geekbot.net/Database/DatabaseInitializer.cs | 3 ++- Geekbot.net/Database/SqlConnectionString.cs | 24 +++++++++++++++++++-- Geekbot.net/Lib/RunParameters.cs | 3 +++ 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/.deploy.yml b/.deploy.yml index 39e3233..fd2df00 100644 --- a/.deploy.yml +++ b/.deploy.yml @@ -32,6 +32,7 @@ GEEKBOT_DB_TRUST_CERT: "true" GEEKBOT_SUMOLOCIG: "{{ lookup('env', 'GEEKBOT_SUMOLOCIG') }}" GEEKBOT_SENTRY: "{{ lookup('env', 'GEEKBOT_SENTRY') }}" + GEEKBOT_DB_REDSHIFT_COMPAT: "true" - name: Cleanup Old Container docker_prune: images: yes diff --git a/Geekbot.net/Database/DatabaseInitializer.cs b/Geekbot.net/Database/DatabaseInitializer.cs index 637e7b8..9f60d8a 100644 --- a/Geekbot.net/Database/DatabaseInitializer.cs +++ b/Geekbot.net/Database/DatabaseInitializer.cs @@ -37,7 +37,8 @@ namespace Geekbot.net.Database Username = _runParameters.DbUser, Password = _runParameters.DbPassword, RequireSsl = _runParameters.DbSsl, - TrustServerCertificate = _runParameters.DbTrustCert + TrustServerCertificate = _runParameters.DbTrustCert, + RedshiftCompatibility = _runParameters.DbRedshiftCompatibility }); } } diff --git a/Geekbot.net/Database/SqlConnectionString.cs b/Geekbot.net/Database/SqlConnectionString.cs index 70346eb..61804cf 100644 --- a/Geekbot.net/Database/SqlConnectionString.cs +++ b/Geekbot.net/Database/SqlConnectionString.cs @@ -1,4 +1,6 @@ -namespace Geekbot.net.Database +using System.Text; + +namespace Geekbot.net.Database { public class SqlConnectionString { @@ -9,11 +11,29 @@ public string Password { get; set; } public bool RequireSsl { get; set; } public bool TrustServerCertificate { get; set; } + public bool RedshiftCompatibility { get; set; } public override string ToString() { + var sb = new StringBuilder(); + sb.Append("Application Name=Geekbot;"); + + sb.Append($"Host={Host};"); + sb.Append($"Port={Port};"); + sb.Append($"Database={Database};"); + sb.Append($"Username={Username};"); + sb.Append($"Password={Password};"); + var sslMode = RequireSsl ? "Require" : "Prefer"; - return $"ApplicationName=Geekbot;Server={Host};Port={Port};Database={Database};Uid={Username};Pwd={Password};SSLMode={sslMode};TrustServerCertificate={TrustServerCertificate.ToString()};"; + sb.Append($"SSL Mode={sslMode};"); + sb.Append($"Trust Server Certificate={TrustServerCertificate.ToString()};"); + + if (RedshiftCompatibility) + { + sb.Append("Server Compatibility Mode=Redshift"); + } + + return sb.ToString(); } } } \ No newline at end of file diff --git a/Geekbot.net/Lib/RunParameters.cs b/Geekbot.net/Lib/RunParameters.cs index bd8ce25..eb8c078 100644 --- a/Geekbot.net/Lib/RunParameters.cs +++ b/Geekbot.net/Lib/RunParameters.cs @@ -49,6 +49,9 @@ namespace Geekbot.net.Lib [Option("db-trust-cert", HelpText = "Trust the database certificate, regardless if it is valid (default: false) (env: DB_TRUST_CERT)")] public bool DbTrustCert { get; set; } = ParamFallback("DB_TRUST_CERT", false); + + [Option("db-redshift-compat", HelpText = "Enable compatibility for AWS Redshift and DigitalOcean Managed Database (default: false) (env: DB_REDSHIFT_COMPAT)")] + public bool DbRedshiftCompatibility { get; set; } = ParamFallback("DB_REDSHIFT_COMPAT", false); // Logging [Option("db-logging", HelpText = "Enable database logging (default: false) (env: DB_LOGGING)")] From 56f788878ad83623da69daf6b13ebddb2e1336e5 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 20 Jun 2020 03:17:17 +0200 Subject: [PATCH 235/443] Fix Sumologic environment variable in docker --- .deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.deploy.yml b/.deploy.yml index fd2df00..cf0e485 100644 --- a/.deploy.yml +++ b/.deploy.yml @@ -30,7 +30,7 @@ GEEKBOT_DB_DATABASE: "{{ lookup('env', 'GEEKBOT_DB_DATABASE') }}" GEEKBOT_DB_REQUIRE_SSL: "true" GEEKBOT_DB_TRUST_CERT: "true" - GEEKBOT_SUMOLOCIG: "{{ lookup('env', 'GEEKBOT_SUMOLOCIG') }}" + GEEKBOT_SUMOLOGIC: "{{ lookup('env', 'GEEKBOT_SUMOLOCIG') }}" GEEKBOT_SENTRY: "{{ lookup('env', 'GEEKBOT_SENTRY') }}" GEEKBOT_DB_REDSHIFT_COMPAT: "true" - name: Cleanup Old Container From 619f63067c9f4726f06cda4982b4a800c34d38aa Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 20 Jun 2020 03:23:27 +0200 Subject: [PATCH 236/443] Remove the ability the lookup username history for moderators --- Geekbot.net/Commands/Admin/Mod.cs | 25 +++---------------- Geekbot.net/Database/Models/UserModel.cs | 2 -- .../Database/Models/UserUsedNamesModel.cs | 15 ----------- .../Lib/UserRepository/UserRepository.cs | 5 ---- 4 files changed, 3 insertions(+), 44 deletions(-) delete mode 100644 Geekbot.net/Database/Models/UserUsedNamesModel.cs diff --git a/Geekbot.net/Commands/Admin/Mod.cs b/Geekbot.net/Commands/Admin/Mod.cs index f4beb54..b0114a8 100644 --- a/Geekbot.net/Commands/Admin/Mod.cs +++ b/Geekbot.net/Commands/Admin/Mod.cs @@ -1,12 +1,9 @@ using System; -using System.Text; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Discord.WebSocket; using Geekbot.net.Lib.CommandPreconditions; using Geekbot.net.Lib.ErrorHandling; -using Geekbot.net.Lib.UserRepository; namespace Geekbot.net.Commands.Admin { @@ -17,15 +14,11 @@ namespace Geekbot.net.Commands.Admin [DisableInDirectMessage] public class Mod : ModuleBase { - private readonly DiscordSocketClient _client; private readonly IErrorHandler _errorHandler; - private readonly IUserRepository _userRepository; - public Mod(IUserRepository userRepositry, IErrorHandler errorHandler, DiscordSocketClient client) + public Mod(IErrorHandler errorHandler) { - _userRepository = userRepositry; _errorHandler = errorHandler; - _client = client; } [Command("namehistory", RunMode = RunMode.Async)] @@ -34,23 +27,11 @@ namespace Geekbot.net.Commands.Admin { try { - var userRepo = _userRepository.Get(user.Id); - if (userRepo?.UsedNames != null) - { - var sb = new StringBuilder(); - sb.AppendLine($":bust_in_silhouette: {user.Username} has been known as:"); - foreach (var name in userRepo.UsedNames) sb.AppendLine($"- `{name.Name}`"); - await ReplyAsync(sb.ToString()); - } - else - { - await ReplyAsync($"No name changes found for {user.Username}"); - } + await Context.Channel.SendMessageAsync("This command has been removed due to low usage and excessively high database usage"); } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context, - "I don't have enough permissions do that"); + await _errorHandler.HandleCommandException(e, Context); } } } diff --git a/Geekbot.net/Database/Models/UserModel.cs b/Geekbot.net/Database/Models/UserModel.cs index b9c60ca..43d3a9a 100644 --- a/Geekbot.net/Database/Models/UserModel.cs +++ b/Geekbot.net/Database/Models/UserModel.cs @@ -24,7 +24,5 @@ namespace Geekbot.net.Database.Models public bool IsBot { get; set; } public DateTimeOffset Joined { get; set; } - - public List UsedNames { get; set; } } } \ No newline at end of file diff --git a/Geekbot.net/Database/Models/UserUsedNamesModel.cs b/Geekbot.net/Database/Models/UserUsedNamesModel.cs deleted file mode 100644 index 48ba873..0000000 --- a/Geekbot.net/Database/Models/UserUsedNamesModel.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.ComponentModel.DataAnnotations; - -namespace Geekbot.net.Database.Models -{ - public class UserUsedNamesModel - { - [Key] - public int Id { get; set; } - - public string Name { get; set; } - - public DateTimeOffset FirstSeen { get; set; } - } -} \ No newline at end of file diff --git a/Geekbot.net/Lib/UserRepository/UserRepository.cs b/Geekbot.net/Lib/UserRepository/UserRepository.cs index b27a0a2..41e8e73 100644 --- a/Geekbot.net/Lib/UserRepository/UserRepository.cs +++ b/Geekbot.net/Lib/UserRepository/UserRepository.cs @@ -37,11 +37,6 @@ namespace Geekbot.net.Lib.UserRepository savedUser.AvatarUrl = user.GetAvatarUrl() ?? ""; savedUser.IsBot = user.IsBot; savedUser.Joined = user.CreatedAt; - if (savedUser.UsedNames == null) savedUser.UsedNames = new List(); - if (!savedUser.UsedNames.Any(e => e.Name.Equals(user.Username))) - { - savedUser.UsedNames.Add(new UserUsedNamesModel { Name = user.Username, FirstSeen = DateTimeOffset.Now }); - } if (isNew) { From 8f41999015f90817e556b0c2958b28fa3fb48251 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 20 Jun 2020 03:25:28 +0200 Subject: [PATCH 237/443] Expose the web api directly on port 80 --- .deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.deploy.yml b/.deploy.yml index cf0e485..e9915ba 100644 --- a/.deploy.yml +++ b/.deploy.yml @@ -21,7 +21,7 @@ restart_policy: always keep_volumes: no ports: - - "12995:12995" + - "80:12995" env: GEEKBOT_DB_HOST: "{{ lookup('env', 'GEEKBOT_DB_HOST') }}" GEEKBOT_DB_USER: "{{ lookup('env', 'GEEKBOT_DB_USER') }}" From 194bfd3d3b61c0a8ae4534fe742ef743a8cf32fe Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 20 Jun 2020 03:27:08 +0200 Subject: [PATCH 238/443] remove traces of the docker branch from the ci file --- .gitlab-ci.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 6015b14..2e22e02 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -23,7 +23,6 @@ Package: image: docker only: - master - - docker services: - docker:stable-dind variables: @@ -38,7 +37,6 @@ Deploy: image: ansible/ansible-runner only: - master - - docker variables: ANSIBLE_NOCOWS: 1 IMAGE_TAG: *IMAGE_TAG From 279a8975c91eb8c061d1320709617d31ffaf1fd5 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 20 Jun 2020 03:42:41 +0200 Subject: [PATCH 239/443] Expose web api on port 12995 again --- .deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.deploy.yml b/.deploy.yml index e9915ba..cf0e485 100644 --- a/.deploy.yml +++ b/.deploy.yml @@ -21,7 +21,7 @@ restart_policy: always keep_volumes: no ports: - - "80:12995" + - "12995:12995" env: GEEKBOT_DB_HOST: "{{ lookup('env', 'GEEKBOT_DB_HOST') }}" GEEKBOT_DB_USER: "{{ lookup('env', 'GEEKBOT_DB_USER') }}" From d7e313c9fa996cf008dae2721f892d4bad23ca30 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 20 Jun 2020 03:43:02 +0200 Subject: [PATCH 240/443] Update readme --- readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index c7f3f33..883e553 100644 --- a/readme.md +++ b/readme.md @@ -2,13 +2,13 @@ # [Geekbot.net](https://geekbot.pizzaandcoffee.rocks/) -A General Purpose Discord Bot written in DotNet Core. +A General Purpose Discord Bot written in C# You can invite Geekbot to your server with [this link](https://discordapp.com/oauth2/authorize?client_id=171249478546882561&scope=bot&permissions=1416834054) ## Technologies -* DotNet Core 3.1 +* .NET 5 * PostgreSQL * Discord.net From 6d449608671cb47c5705d65ad6f0f367d6b9801c Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 21 Jun 2020 03:33:05 +0200 Subject: [PATCH 241/443] Rewrite the !dice command from scratch --- Geekbot.net/Commands/Utils/Dice.cs | 107 ++++++++++++++++ Geekbot.net/Commands/Utils/Dice/Dice.cs | 116 ------------------ .../Commands/Utils/Dice/DiceTypeDto.cs | 10 -- Geekbot.net/Lib/DiceParser/DiceException.cs | 13 ++ Geekbot.net/Lib/DiceParser/DiceInput.cs | 11 ++ .../Lib/DiceParser/DiceInputOptions.cs | 7 ++ Geekbot.net/Lib/DiceParser/DiceParser.cs | 102 +++++++++++++++ .../Lib/DiceParser/DieAdvantageType.cs | 9 ++ Geekbot.net/Lib/DiceParser/DieResult.cs | 30 +++++ Geekbot.net/Lib/DiceParser/IDiceParser.cs | 7 ++ Geekbot.net/Lib/DiceParser/SingleDie.cs | 72 +++++++++++ Geekbot.net/Lib/Extensions/IntExtensions.cs | 15 +++ Geekbot.net/Program.cs | 3 + 13 files changed, 376 insertions(+), 126 deletions(-) create mode 100644 Geekbot.net/Commands/Utils/Dice.cs delete mode 100644 Geekbot.net/Commands/Utils/Dice/Dice.cs delete mode 100644 Geekbot.net/Commands/Utils/Dice/DiceTypeDto.cs create mode 100644 Geekbot.net/Lib/DiceParser/DiceException.cs create mode 100644 Geekbot.net/Lib/DiceParser/DiceInput.cs create mode 100644 Geekbot.net/Lib/DiceParser/DiceInputOptions.cs create mode 100644 Geekbot.net/Lib/DiceParser/DiceParser.cs create mode 100644 Geekbot.net/Lib/DiceParser/DieAdvantageType.cs create mode 100644 Geekbot.net/Lib/DiceParser/DieResult.cs create mode 100644 Geekbot.net/Lib/DiceParser/IDiceParser.cs create mode 100644 Geekbot.net/Lib/DiceParser/SingleDie.cs create mode 100644 Geekbot.net/Lib/Extensions/IntExtensions.cs diff --git a/Geekbot.net/Commands/Utils/Dice.cs b/Geekbot.net/Commands/Utils/Dice.cs new file mode 100644 index 0000000..f570271 --- /dev/null +++ b/Geekbot.net/Commands/Utils/Dice.cs @@ -0,0 +1,107 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using Discord.Commands; +using Geekbot.net.Lib.DiceParser; +using Geekbot.net.Lib.ErrorHandling; + +namespace Geekbot.net.Commands.Utils +{ + public class Dice : ModuleBase + { + private readonly IErrorHandler _errorHandler; + private readonly IDiceParser _diceParser; + + public Dice(IErrorHandler errorHandler, IDiceParser diceParser) + { + _errorHandler = errorHandler; + _diceParser = diceParser; + } + + // ToDo: Separate representation and logic + // ToDo: Translate + [Command("dice", RunMode = RunMode.Async)] + [Summary("Roll a dice. (use '!dice help' for a manual)")] + public async Task RollCommand([Remainder] [Summary("input")] string diceInput = "1d20") + { + try + { + if (diceInput == "help") + { + await ShowDiceHelp(); + return; + } + + var parsed = _diceParser.Parse(diceInput); + + var sb = new StringBuilder(); + sb.AppendLine($"{Context.User.Mention} :game_die:"); + foreach (var die in parsed.Dice) + { + sb.AppendLine($"**{die.DiceName}**"); + var diceResultList = new List(); + var total = 0; + + foreach (var roll in die.Roll()) + { + diceResultList.Add(roll.ToString()); + total += roll.Result; + } + + sb.AppendLine(string.Join(" | ", diceResultList)); + + if (parsed.SkillModifier != 0) + { + sb.AppendLine($"Skill: {parsed.SkillModifier}"); + } + + if (parsed.Options.ShowTotal) + { + var totalLine = $"Total: {total}"; + if (parsed.SkillModifier > 0) + { + totalLine += ($" (+{parsed.SkillModifier} = {total + parsed.SkillModifier})"); + } + + if (parsed.SkillModifier < 0) + { + totalLine += ($" ({parsed.SkillModifier} = {total - parsed.SkillModifier})"); + } + + sb.AppendLine(totalLine); + } + } + + await Context.Channel.SendMessageAsync(sb.ToString()); + } + catch (DiceException e) + { + await Context.Channel.SendMessageAsync($"**:warning: {e.DiceName} is invalid:** {e.Message}"); + } + catch (Exception e) + { + await _errorHandler.HandleCommandException(e, Context); + } + } + + private async Task ShowDiceHelp() + { + var sb = new StringBuilder(); + sb.AppendLine("**__Examples__**"); + sb.AppendLine("```"); + sb.AppendLine("'!dice' - throw a 1d20"); + sb.AppendLine("'!dice 1d12' - throw a 1d12"); + sb.AppendLine("'!dice +1d20' - throw with advantage"); + sb.AppendLine("'!dice -1d20' - throw with disadvantage"); + sb.AppendLine("'!dice 1d20 +2' - throw with a +2 skill bonus"); + sb.AppendLine("'!dice 1d20 -2' - throw with a -2 skill bonus"); + sb.AppendLine("'!dice 8d6' - throw ~~a fireball~~ a 8d6"); + sb.AppendLine("'!dice 8d6 total' - calculate the total"); + sb.AppendLine("'!dice 2d20 6d6 2d4 2d12' - drop your dice pouch"); + sb.AppendLine("```"); + + await Context.Channel.SendMessageAsync(sb.ToString()); + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/Utils/Dice/Dice.cs b/Geekbot.net/Commands/Utils/Dice/Dice.cs deleted file mode 100644 index c335982..0000000 --- a/Geekbot.net/Commands/Utils/Dice/Dice.cs +++ /dev/null @@ -1,116 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Discord.Commands; -using Geekbot.net.Lib.RandomNumberGenerator; - -namespace Geekbot.net.Commands.Utils.Dice -{ - public class Dice : ModuleBase - { - private readonly IRandomNumberGenerator _randomNumberGenerator; - - public Dice(IRandomNumberGenerator randomNumberGenerator) - { - _randomNumberGenerator = randomNumberGenerator; - } - - [Command("dice", RunMode = RunMode.Async)] - [Summary("Roll a dice.")] - public async Task RollCommand([Remainder] [Summary("dice-type")] string diceType = "1d20") - { - var splitedDices = diceType.Split("+"); - var dices = new List(); - var mod = 0; - foreach (var i in splitedDices) - { - var dice = ToDice(i); - if (dice.Sides != 0 && dice.Times != 0) - { - dices.Add(dice); - } - else if (dice.Mod != 0) - { - if (mod != 0) - { - await ReplyAsync("You can only have one mod"); - return; - } - - mod = dice.Mod; - } - } - - if (!dices.Any()) - { - await ReplyAsync( - "That is not a valid dice, examples are: 1d20, 1d6, 2d6, 1d6+2, 1d6+2d8+1d20+6, etc..."); - return; - } - - - if (dices.Any(d => d.Times > 20)) - { - await ReplyAsync("You can't throw more than 20 dices"); - return; - } - - if (dices.Any(d => d.Sides > 144)) - { - await ReplyAsync("A dice can't have more than 144 sides"); - return; - } - - var rep = new StringBuilder(); - rep.AppendLine($":game_die: {Context.User.Mention}"); - rep.Append("**Result:** "); - var resultStrings = new List(); - var total = 0; - foreach (var dice in dices) - { - var results = new List(); - for (var i = 0; i < dice.Times; i++) - { - var roll = _randomNumberGenerator.Next(1, dice.Sides); - total += roll; - results.Add(roll); - } - - resultStrings.Add($"{dice.DiceType} ({string.Join(",", results)})"); - } - - rep.Append(string.Join(" + ", resultStrings)); - if (mod != 0) - { - rep.Append($" + {mod}"); - total += mod; - } - - rep.AppendLine(); - rep.AppendLine($"**Total:** {total}"); - await ReplyAsync(rep.ToString()); - } - - private DiceTypeDto ToDice(string dice) - { - var diceParts = dice.Split('d'); - if (diceParts.Length == 2 - && int.TryParse(diceParts[0], out var times) - && int.TryParse(diceParts[1], out var max)) - return new DiceTypeDto - { - DiceType = dice, - Times = times, - Sides = max - }; - if (dice.Length == 1 - && int.TryParse(diceParts[0], out var mod)) - return new DiceTypeDto - { - Mod = mod - }; - return new DiceTypeDto(); - } - } -} \ No newline at end of file diff --git a/Geekbot.net/Commands/Utils/Dice/DiceTypeDto.cs b/Geekbot.net/Commands/Utils/Dice/DiceTypeDto.cs deleted file mode 100644 index 5c54792..0000000 --- a/Geekbot.net/Commands/Utils/Dice/DiceTypeDto.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace Geekbot.net.Commands.Utils.Dice -{ - internal class DiceTypeDto - { - public string DiceType { get; set; } - public int Times { get; set; } - public int Sides { get; set; } - public int Mod { get; set; } - } -} \ No newline at end of file diff --git a/Geekbot.net/Lib/DiceParser/DiceException.cs b/Geekbot.net/Lib/DiceParser/DiceException.cs new file mode 100644 index 0000000..d2a66ad --- /dev/null +++ b/Geekbot.net/Lib/DiceParser/DiceException.cs @@ -0,0 +1,13 @@ +using System; + +namespace Geekbot.net.Lib.DiceParser +{ + public class DiceException : Exception + { + public DiceException(string message) : base(message) + { + } + + public string DiceName { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/DiceParser/DiceInput.cs b/Geekbot.net/Lib/DiceParser/DiceInput.cs new file mode 100644 index 0000000..baca42c --- /dev/null +++ b/Geekbot.net/Lib/DiceParser/DiceInput.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; + +namespace Geekbot.net.Lib.DiceParser +{ + public class DiceInput + { + public List Dice { get; set; } = new List(); + public DiceInputOptions Options { get; set; } = new DiceInputOptions(); + public int SkillModifier { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/DiceParser/DiceInputOptions.cs b/Geekbot.net/Lib/DiceParser/DiceInputOptions.cs new file mode 100644 index 0000000..41f2def --- /dev/null +++ b/Geekbot.net/Lib/DiceParser/DiceInputOptions.cs @@ -0,0 +1,7 @@ +namespace Geekbot.net.Lib.DiceParser +{ + public struct DiceInputOptions + { + public bool ShowTotal { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/DiceParser/DiceParser.cs b/Geekbot.net/Lib/DiceParser/DiceParser.cs new file mode 100644 index 0000000..7f90865 --- /dev/null +++ b/Geekbot.net/Lib/DiceParser/DiceParser.cs @@ -0,0 +1,102 @@ +using System; +using System.Linq; +using System.Text.RegularExpressions; +using Geekbot.net.Lib.RandomNumberGenerator; + +namespace Geekbot.net.Lib.DiceParser +{ + public class DiceParser : IDiceParser + { + private readonly IRandomNumberGenerator _randomNumberGenerator; + private readonly Regex _inputRegex; + private readonly Regex _singleDieRegex; + + public DiceParser(IRandomNumberGenerator randomNumberGenerator) + { + _randomNumberGenerator = randomNumberGenerator; + _inputRegex = new Regex( + @"((?\+\d+d\d+)|(?\-\d+d\d+)|(?\d+d\d+)|(?(total))|(?(\+|\-)\d+))\s", + RegexOptions.Compiled | RegexOptions.IgnoreCase, + new TimeSpan(0, 0, 2)); + _singleDieRegex = new Regex( + @"\d+d\d+", + RegexOptions.Compiled | RegexOptions.IgnoreCase, + new TimeSpan(0, 0, 0, 0, 500)); + } + + public DiceInput Parse(string input) + { + // adding a whitespace at the end, otherwise the parser might pickup on false items + var inputWithExtraWhitespace = $"{input} "; + + var matches = _inputRegex.Matches(inputWithExtraWhitespace); + var result = new DiceInput(); + var resultOptions = new DiceInputOptions(); + + foreach (Match match in matches) + { + foreach (Group matchGroup in match.Groups) + { + if (matchGroup.Success) + { + switch (matchGroup.Name) + { + case "DieNormal": + result.Dice.Add(Die(matchGroup.Value, DieAdvantageType.None)); + break; + case "DieAdvantage": + result.Dice.Add(Die(matchGroup.Value, DieAdvantageType.Advantage)); + break; + case "DieDisadvantage": + result.Dice.Add(Die(matchGroup.Value, DieAdvantageType.Disadvantage)); + break; + case "Keywords": + Keywords(matchGroup.Value, ref resultOptions); + break; + case "SkillModifer": + result.SkillModifier = SkillModifer(matchGroup.Value); + break; + } + } + } + } + + if (!result.Dice.Any()) + { + result.Dice.Add(new SingleDie(_randomNumberGenerator)); + } + + result.Options = resultOptions; + + return result; + } + + private SingleDie Die(string match, DieAdvantageType advantageType) + { + var x = _singleDieRegex.Match(match).Value.Split('d'); + var die = new SingleDie(_randomNumberGenerator) + { + Amount = int.Parse(x[0]), + Sides = int.Parse(x[1]), + AdvantageType = advantageType + }; + die.ValidateDie(); + return die; + } + + private int SkillModifer(string match) + { + return int.Parse(match); + } + + private void Keywords(string match, ref DiceInputOptions options) + { + switch (match) + { + case "total": + options.ShowTotal = true; + break; + } + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/DiceParser/DieAdvantageType.cs b/Geekbot.net/Lib/DiceParser/DieAdvantageType.cs new file mode 100644 index 0000000..91818cb --- /dev/null +++ b/Geekbot.net/Lib/DiceParser/DieAdvantageType.cs @@ -0,0 +1,9 @@ +namespace Geekbot.net.Lib.DiceParser +{ + public enum DieAdvantageType + { + Advantage, + Disadvantage, + None + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/DiceParser/DieResult.cs b/Geekbot.net/Lib/DiceParser/DieResult.cs new file mode 100644 index 0000000..2f99fb6 --- /dev/null +++ b/Geekbot.net/Lib/DiceParser/DieResult.cs @@ -0,0 +1,30 @@ +using System; + +namespace Geekbot.net.Lib.DiceParser +{ + public class DieResult + { + // public int Result { get; set; } + public int Roll1 { get; set; } + public int Roll2 { get; set; } + public DieAdvantageType AdvantageType { get; set; } + + public override string ToString() + { + return AdvantageType switch + { + DieAdvantageType.Advantage => Roll1 > Roll2 ? $"(**{Roll1}**, {Roll2})" : $"({Roll1}, **{Roll2}**)", + DieAdvantageType.Disadvantage => Roll1 < Roll2 ? $"(**{Roll1}**, {Roll2})" : $"({Roll1}, **{Roll2}**)", + _ => Result.ToString() + }; + } + + public int Result => AdvantageType switch + { + DieAdvantageType.None => Roll1, + DieAdvantageType.Advantage => Math.Max(Roll1, Roll2), + DieAdvantageType.Disadvantage => Math.Min(Roll1, Roll2), + _ => 0 + }; + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/DiceParser/IDiceParser.cs b/Geekbot.net/Lib/DiceParser/IDiceParser.cs new file mode 100644 index 0000000..608b379 --- /dev/null +++ b/Geekbot.net/Lib/DiceParser/IDiceParser.cs @@ -0,0 +1,7 @@ +namespace Geekbot.net.Lib.DiceParser +{ + public interface IDiceParser + { + DiceInput Parse(string input); + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/DiceParser/SingleDie.cs b/Geekbot.net/Lib/DiceParser/SingleDie.cs new file mode 100644 index 0000000..24280d1 --- /dev/null +++ b/Geekbot.net/Lib/DiceParser/SingleDie.cs @@ -0,0 +1,72 @@ +using System.Collections.Generic; +using Geekbot.net.Lib.Extensions; +using Geekbot.net.Lib.RandomNumberGenerator; + +namespace Geekbot.net.Lib.DiceParser +{ + public class SingleDie + { + private readonly IRandomNumberGenerator _random; + + public SingleDie(IRandomNumberGenerator random) + { + _random = random; + } + + public int Sides { get; set; } = 20; + public int Amount { get; set; } = 1; + public DieAdvantageType AdvantageType { get; set; } = DieAdvantageType.None; + + public string DiceName => AdvantageType switch + { + DieAdvantageType.Advantage => $"{Amount}d{Sides} (with advantage)", + DieAdvantageType.Disadvantage => $"{Amount}d{Sides} (with disadvantage)", + _ => $"{Amount}d{Sides}" + }; + + public List Roll() + { + var results = new List(); + + Amount.Times(() => + { + var result = new DieResult + { + Roll1 = _random.Next(1, Sides), + AdvantageType = AdvantageType + }; + + if (AdvantageType == DieAdvantageType.Advantage || AdvantageType == DieAdvantageType.Disadvantage) + { + result.Roll2 = _random.Next(1, Sides); + } + + results.Add(result); + }); + + return results; + } + + public void ValidateDie() + { + if (Amount < 1) + { + throw new DiceException("To few dice, must be a minimum of 1"); + } + if (Amount > 24) + { + throw new DiceException("To many dice, maximum allowed is 24") { DiceName = DiceName }; + } + + if (Sides < 2) + { + throw new DiceException("Die must have at least 2 sides") { DiceName = DiceName }; + } + + if (Sides > 144) + { + throw new DiceException("Die can not have more than 144 sides") { DiceName = DiceName }; + } + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/Extensions/IntExtensions.cs b/Geekbot.net/Lib/Extensions/IntExtensions.cs new file mode 100644 index 0000000..ed0fc10 --- /dev/null +++ b/Geekbot.net/Lib/Extensions/IntExtensions.cs @@ -0,0 +1,15 @@ +using System; + +namespace Geekbot.net.Lib.Extensions +{ + public static class IntExtensions + { + public static void Times(this int count, Action action) + { + for (var i = 0; i < count; i++) + { + action(); + } + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index a5b0668..3d98ad9 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -11,6 +11,7 @@ using Geekbot.net.Handlers; using Geekbot.net.Lib; using Geekbot.net.Lib.Clients; using Geekbot.net.Lib.Converters; +using Geekbot.net.Lib.DiceParser; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.GlobalSettings; using Geekbot.net.Lib.GuildSettingsManager; @@ -169,6 +170,7 @@ namespace Geekbot.net var kvMemoryStore = new KvInInMemoryStore(); var translationHandler = new TranslationHandler(_logger, _guildSettingsManager); var errorHandler = new ErrorHandler(_logger, translationHandler, _runParameters); + var diceParser = new DiceParser(randomNumberGenerator); services.AddSingleton(_userRepository); services.AddSingleton(_logger); @@ -183,6 +185,7 @@ namespace Geekbot.net services.AddSingleton(kvMemoryStore); services.AddSingleton(_globalSettings); services.AddSingleton(errorHandler); + services.AddSingleton(diceParser); services.AddSingleton(translationHandler); services.AddSingleton(_reactionListener); services.AddSingleton(_guildSettingsManager); From acd1cee16cbdc2c695be7662cb929fc5da0f6581 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 21 Jun 2020 03:43:11 +0200 Subject: [PATCH 242/443] Reformat !dice help --- Geekbot.net/Commands/Utils/Dice.cs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Geekbot.net/Commands/Utils/Dice.cs b/Geekbot.net/Commands/Utils/Dice.cs index f570271..2cd2e19 100644 --- a/Geekbot.net/Commands/Utils/Dice.cs +++ b/Geekbot.net/Commands/Utils/Dice.cs @@ -90,15 +90,15 @@ namespace Geekbot.net.Commands.Utils var sb = new StringBuilder(); sb.AppendLine("**__Examples__**"); sb.AppendLine("```"); - sb.AppendLine("'!dice' - throw a 1d20"); - sb.AppendLine("'!dice 1d12' - throw a 1d12"); - sb.AppendLine("'!dice +1d20' - throw with advantage"); - sb.AppendLine("'!dice -1d20' - throw with disadvantage"); - sb.AppendLine("'!dice 1d20 +2' - throw with a +2 skill bonus"); - sb.AppendLine("'!dice 1d20 -2' - throw with a -2 skill bonus"); - sb.AppendLine("'!dice 8d6' - throw ~~a fireball~~ a 8d6"); - sb.AppendLine("'!dice 8d6 total' - calculate the total"); - sb.AppendLine("'!dice 2d20 6d6 2d4 2d12' - drop your dice pouch"); + sb.AppendLine("!dice - throw a 1d20"); + sb.AppendLine("!dice 1d12 - throw a 1d12"); + sb.AppendLine("!dice +1d20 - throw with advantage"); + sb.AppendLine("!dice -1d20 - throw with disadvantage"); + sb.AppendLine("!dice 1d20 +2 - throw with a +2 skill bonus"); + sb.AppendLine("!dice 1d20 -2 - throw with a -2 skill bonus"); + sb.AppendLine("!dice 8d6 - throw a fireball 🔥"); + sb.AppendLine("!dice 8d6 total - calculate the total"); + sb.AppendLine("!dice 2d20 6d6 2d12 - drop your dice pouch"); sb.AppendLine("```"); await Context.Channel.SendMessageAsync(sb.ToString()); From 859db4ebddc4322ab1745c8b7f5744574813b465 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 21 Jun 2020 15:49:18 +0200 Subject: [PATCH 243/443] Add some unit tests for the dice parser --- Tests/Lib/DiceParser/DiceParser.test.cs | 209 ++++++++++++++++++++++++ Tests/Lib/DiceParser/SingleDie.test.cs | 125 ++++++++++++++ 2 files changed, 334 insertions(+) create mode 100644 Tests/Lib/DiceParser/DiceParser.test.cs create mode 100644 Tests/Lib/DiceParser/SingleDie.test.cs diff --git a/Tests/Lib/DiceParser/DiceParser.test.cs b/Tests/Lib/DiceParser/DiceParser.test.cs new file mode 100644 index 0000000..48a362c --- /dev/null +++ b/Tests/Lib/DiceParser/DiceParser.test.cs @@ -0,0 +1,209 @@ +using System.Collections.Generic; +using System.Text.Json; +using Geekbot.net.Lib.DiceParser; +using Geekbot.net.Lib.RandomNumberGenerator; +using Xunit; +using YamlDotNet.Serialization; + +namespace Tests.Lib.DiceParser +{ + public class DiceParserTest + { + private static readonly RandomNumberGenerator _randomNumberGenerator = new RandomNumberGenerator(); + + public struct DiceParserTestDto + { + public string Input { get; set; } + public DiceInput Expected { get; set; } + } + + public static TestData DiceParserTestData => + new TestData + { + { + "Empty Input", + new DiceParserTestDto + { + Input = "", + Expected = new DiceInput() + { + Dice = new List + { + new SingleDie(_randomNumberGenerator) + { + Amount = 1, + Sides = 20, + AdvantageType = DieAdvantageType.None + } + } + } + } + }, + { + "Simple 1d20 input", + new DiceParserTestDto + { + Input = "1d20", + Expected = new DiceInput() + { + Dice = new List + { + new SingleDie(_randomNumberGenerator) + { + Amount = 1, + Sides = 20, + AdvantageType = DieAdvantageType.None + } + } + } + } + }, + { + "2d8 with advantage", + new DiceParserTestDto + { + Input = "+2d8", + Expected = new DiceInput() + { + Dice = new List + { + new SingleDie(_randomNumberGenerator) + { + Amount = 2, + Sides = 8, + AdvantageType = DieAdvantageType.Advantage + } + } + } + } + }, + { + "2d8 with disadvantage", + new DiceParserTestDto + { + Input = "-2d8", + Expected = new DiceInput() + { + Dice = new List + { + new SingleDie(_randomNumberGenerator) + { + Amount = 2, + Sides = 8, + AdvantageType = DieAdvantageType.Disadvantage + } + } + } + } + }, + { + "multiple dice", + new DiceParserTestDto + { + Input = "2d8 2d6", + Expected = new DiceInput() + { + Dice = new List + { + new SingleDie(_randomNumberGenerator) + { + Amount = 2, + Sides = 8 + }, + new SingleDie(_randomNumberGenerator) + { + Amount = 2, + Sides = 6 + } + } + } + } + }, + { + "with skill modifier, no dice", + new DiceParserTestDto + { + Input = "+2", + Expected = new DiceInput() + { + Dice = new List + { + new SingleDie(_randomNumberGenerator) + { + Amount = 1, + Sides = 20 + } + }, + SkillModifier = 2 + } + } + }, + { + "8d6 with total", + new DiceParserTestDto + { + Input = "8d6 total", + Expected = new DiceInput() + { + Dice = new List + { + new SingleDie(_randomNumberGenerator) + { + Amount = 8, + Sides = 6 + } + }, + Options = new DiceInputOptions + { + ShowTotal = true + } + } + } + }, + { + "All posibilities", + new DiceParserTestDto + { + Input = "2d20 +1d20 -1d20 +1 total", + Expected = new DiceInput() + { + Dice = new List + { + new SingleDie(_randomNumberGenerator) + { + Amount = 2, + Sides = 20 + }, + new SingleDie(_randomNumberGenerator) + { + Amount = 1, + Sides = 20, + AdvantageType = DieAdvantageType.Advantage + }, + new SingleDie(_randomNumberGenerator) + { + Amount = 1, + Sides = 20, + AdvantageType = DieAdvantageType.Disadvantage + }, + }, + Options = new DiceInputOptions + { + ShowTotal = true + }, + SkillModifier = 1 + } + } + }, + }; + + [Theory, MemberData(nameof(DiceParserTestData))] + public void DiceParserTestFunc(string testName, DiceParserTestDto testData) + { + var parser = new Geekbot.net.Lib.DiceParser.DiceParser(_randomNumberGenerator); + var result = parser.Parse(testData.Input); + + Assert.Equal(JsonSerializer.Serialize(result), JsonSerializer.Serialize(testData.Expected)); + } + } +} \ No newline at end of file diff --git a/Tests/Lib/DiceParser/SingleDie.test.cs b/Tests/Lib/DiceParser/SingleDie.test.cs new file mode 100644 index 0000000..2309f98 --- /dev/null +++ b/Tests/Lib/DiceParser/SingleDie.test.cs @@ -0,0 +1,125 @@ +using Geekbot.net.Lib.DiceParser; +using Geekbot.net.Lib.RandomNumberGenerator; +using Xunit; + +namespace Tests.Lib.DiceParser +{ + public class SingleDieTest + { + public struct SingleDieNameTestDto + { + public DieAdvantageType AdvantageType { get; set; } + public string Expected { get; set; } + } + + public static TestData SingleDieNameTestData => new TestData + { + { + "No advantage", + new SingleDieNameTestDto() + { + AdvantageType = DieAdvantageType.None, + Expected = "1d20" + } + }, + { + "With advantage", + new SingleDieNameTestDto() + { + AdvantageType = DieAdvantageType.Advantage, + Expected = "1d20 (with advantage)" + } + }, + { + "With disadvantage", + new SingleDieNameTestDto() + { + AdvantageType = DieAdvantageType.Disadvantage, + Expected = "1d20 (with disadvantage)" + } + } + }; + + [Theory, MemberData(nameof(SingleDieNameTestData))] + public void SingleDieNameTestFunc(string testName, SingleDieNameTestDto testData) + { + var die = new SingleDie(new RandomNumberGenerator()) {AdvantageType = testData.AdvantageType}; + Assert.Equal(die.DiceName, testData.Expected); + } + + public struct SingleDieValidationTestDto + { + public int Sides { get; set; } + public int Amount { get; set; } + public bool PassValidation { get; set; } + } + + public static TestData SingleDieValidationTestData => new TestData + { + { + "To many sides", + new SingleDieValidationTestDto() + { + Amount = 1, + Sides = 200, + PassValidation = false + } + }, + { + "To few sides", + new SingleDieValidationTestDto() + { + Amount = 1, + Sides = 1, + PassValidation = false + } + }, + { + "To many Dice", + new SingleDieValidationTestDto() + { + Amount = 200, + Sides = 20, + PassValidation = false + } + }, + { + "To few Dice", + new SingleDieValidationTestDto() + { + Amount = 0, + Sides = 20, + PassValidation = false + } + }, + { + "Correct Dice", + new SingleDieValidationTestDto() + { + Amount = 1, + Sides = 20, + PassValidation = true + } + } + }; + + [Theory, MemberData(nameof(SingleDieValidationTestData))] + public void SingleDieValidationTestFunc(string testName, SingleDieValidationTestDto testData) + { + var die = new SingleDie(new RandomNumberGenerator()) + { + Amount = testData.Amount, + Sides = testData.Sides + }; + + if (testData.PassValidation) + { + die.ValidateDie(); + } + else + { + Assert.Throws(() => die.ValidateDie()); + } + } + } +} \ No newline at end of file From cc227747295afb9eae1b1fd365d0d959b779c50d Mon Sep 17 00:00:00 2001 From: runebaas Date: Sun, 21 Jun 2020 21:12:34 +0200 Subject: [PATCH 244/443] Make sure a die can actually land on its highest number --- Geekbot.net/Lib/DiceParser/SingleDie.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Geekbot.net/Lib/DiceParser/SingleDie.cs b/Geekbot.net/Lib/DiceParser/SingleDie.cs index 24280d1..582fe29 100644 --- a/Geekbot.net/Lib/DiceParser/SingleDie.cs +++ b/Geekbot.net/Lib/DiceParser/SingleDie.cs @@ -32,7 +32,7 @@ namespace Geekbot.net.Lib.DiceParser { var result = new DieResult { - Roll1 = _random.Next(1, Sides), + Roll1 = _random.Next(1, Sides + 1), AdvantageType = AdvantageType }; From 580a514ce5c648a72c3d4deda0eec8cf07c026b3 Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 25 Jun 2020 15:12:41 +0200 Subject: [PATCH 245/443] Rework the media provider --- .../Commands/Randomness/RandomAnimals.cs | 24 ++-- Geekbot.net/Lib/Media/IMediaProvider.cs | 9 +- Geekbot.net/Lib/Media/MediaProvider.cs | 135 +++++------------- Geekbot.net/Lib/Media/MediaType.cs | 14 ++ Geekbot.net/Program.cs | 2 +- Geekbot.net/Storage/{pinguins => penguins} | 24 ++-- 6 files changed, 72 insertions(+), 136 deletions(-) create mode 100644 Geekbot.net/Lib/Media/MediaType.cs rename Geekbot.net/Storage/{pinguins => penguins} (99%) diff --git a/Geekbot.net/Commands/Randomness/RandomAnimals.cs b/Geekbot.net/Commands/Randomness/RandomAnimals.cs index 8562908..47b6ea6 100644 --- a/Geekbot.net/Commands/Randomness/RandomAnimals.cs +++ b/Geekbot.net/Commands/Randomness/RandomAnimals.cs @@ -18,7 +18,7 @@ namespace Geekbot.net.Commands.Randomness [Summary("Get a random panda image")] public async Task Panda() { - await ReplyAsync("", false, Eb(_mediaProvider.GetPanda())); + await ReplyAsync("", false, Eb(_mediaProvider.GetMedia(MediaType.Panda))); } [Command("croissant", RunMode = RunMode.Async)] @@ -26,50 +26,50 @@ namespace Geekbot.net.Commands.Randomness [Summary("Get a random croissant image")] public async Task Croissant() { - await ReplyAsync("", false, Eb(_mediaProvider.GetCrossant())); + await ReplyAsync("", false, Eb(_mediaProvider.GetMedia(MediaType.Croissant))); } [Command("pumpkin", RunMode = RunMode.Async)] [Summary("Get a random pumpkin image")] public async Task Pumpkin() { - await ReplyAsync("", false, Eb(_mediaProvider.GetPumpkin())); + await ReplyAsync("", false, Eb(_mediaProvider.GetMedia(MediaType.Pumpkin))); } [Command("squirrel", RunMode = RunMode.Async)] [Summary("Get a random squirrel image")] public async Task Squirrel() { - await ReplyAsync("", false, Eb(_mediaProvider.GetSquirrel())); + await ReplyAsync("", false, Eb(_mediaProvider.GetMedia(MediaType.Squirrel))); } [Command("turtle", RunMode = RunMode.Async)] [Summary("Get a random turtle image")] public async Task Turtle() { - await ReplyAsync("", false, Eb(_mediaProvider.GetTurtle())); + await ReplyAsync("", false, Eb(_mediaProvider.GetMedia(MediaType.Turtle))); } - [Command("pinguin", RunMode = RunMode.Async)] - [Alias("pingu")] - [Summary("Get a random pinguin image")] - public async Task Pinguin() + [Command("penguin", RunMode = RunMode.Async)] + [Alias("pengu")] + [Summary("Get a random penguin image")] + public async Task Penguin() { - await ReplyAsync("", false, Eb(_mediaProvider.GetPinguin())); + await ReplyAsync("", false, Eb(_mediaProvider.GetMedia(MediaType.Penguin))); } [Command("fox", RunMode = RunMode.Async)] [Summary("Get a random fox image")] public async Task Fox() { - await ReplyAsync("", false, Eb(_mediaProvider.GetFox())); + await ReplyAsync("", false, Eb(_mediaProvider.GetMedia(MediaType.Fox))); } [Command("dab", RunMode = RunMode.Async)] [Summary("Get a random dab image")] public async Task Dab() { - await ReplyAsync("", false, Eb(_mediaProvider.GetDab())); + await ReplyAsync("", false, Eb(_mediaProvider.GetMedia(MediaType.Dab))); } private static Embed Eb(string image) diff --git a/Geekbot.net/Lib/Media/IMediaProvider.cs b/Geekbot.net/Lib/Media/IMediaProvider.cs index 33a0d7c..63c0f41 100644 --- a/Geekbot.net/Lib/Media/IMediaProvider.cs +++ b/Geekbot.net/Lib/Media/IMediaProvider.cs @@ -2,13 +2,6 @@ { public interface IMediaProvider { - string GetPanda(); - string GetCrossant(); - string GetSquirrel(); - string GetPumpkin(); - string GetTurtle(); - string GetPinguin(); - string GetFox(); - string GetDab(); + string GetMedia(MediaType type); } } \ No newline at end of file diff --git a/Geekbot.net/Lib/Media/MediaProvider.cs b/Geekbot.net/Lib/Media/MediaProvider.cs index f7ea9df..45524de 100644 --- a/Geekbot.net/Lib/Media/MediaProvider.cs +++ b/Geekbot.net/Lib/Media/MediaProvider.cs @@ -1,133 +1,62 @@ using System; using System.IO; using Geekbot.net.Lib.Logger; +using Geekbot.net.Lib.RandomNumberGenerator; namespace Geekbot.net.Lib.Media { public class MediaProvider : IMediaProvider { - private readonly Random _random; + private readonly IRandomNumberGenerator _random; private readonly IGeekbotLogger _logger; private string[] _pandaImages; private string[] _croissantImages; private string[] _squirrelImages; private string[] _pumpkinImages; private string[] _turtlesImages; - private string[] _pinguinImages; + private string[] _penguinImages; private string[] _foxImages; private string[] _dabImages; - public MediaProvider(IGeekbotLogger logger) + public MediaProvider(IGeekbotLogger logger, IRandomNumberGenerator random) { - _random = new Random(); + _random = random; _logger = logger; logger.Information(LogSource.Geekbot, "Loading Media Files"); ; - LoadPandas(); - BakeCroissants(); - LoadSquirrels(); - LoadPumpkins(); - LoadTurtles(); - LoadPinguins(); - LoadFoxes(); - LoadDab(); - } - - private void LoadPandas() - { - var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/pandas")); - _pandaImages = rawLinks.Split("\n"); - _logger.Trace(LogSource.Geekbot, $"Loaded {_pandaImages.Length} Panda Images"); - } - - private void BakeCroissants() - { - var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/croissant")); - _croissantImages = rawLinks.Split("\n"); - _logger.Trace(LogSource.Geekbot, $"Loaded {_croissantImages.Length} Croissant Images"); - } - - private void LoadSquirrels() - { - var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/squirrel")); - _squirrelImages = rawLinks.Split("\n"); - _logger.Trace(LogSource.Geekbot, $"Loaded {_squirrelImages.Length} Squirrel Images"); - } - - private void LoadPumpkins() - { - var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/pumpkin")); - _pumpkinImages = rawLinks.Split("\n"); - _logger.Trace(LogSource.Geekbot, $"Loaded {_pumpkinImages.Length} Pumpkin Images"); - } - - private void LoadTurtles() - { - var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/turtles")); - _turtlesImages = rawLinks.Split("\n"); - _logger.Trace(LogSource.Geekbot, $"Loaded {_turtlesImages.Length} Turtle Images"); - } - - private void LoadPinguins() - { - var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/pinguins")); - _pinguinImages = rawLinks.Split("\n"); - _logger.Trace(LogSource.Geekbot, $"Loaded {_pinguinImages.Length} Pinguin Images"); - } - - private void LoadFoxes() - { - var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/foxes")); - _foxImages = rawLinks.Split("\n"); - _logger.Trace(LogSource.Geekbot, $"Loaded {_foxImages.Length} Foxes Images"); - } - - private void LoadDab() - { - var rawLinks = File.ReadAllText(Path.GetFullPath("./Storage/dab")); - _dabImages = rawLinks.Split("\n"); - _logger.Trace(LogSource.Geekbot, $"Loaded {_dabImages.Length} Dab Images"); + LoadMedia("./Storage/pandas", ref _pandaImages); + LoadMedia("./Storage/croissant", ref _croissantImages); + LoadMedia("./Storage/squirrel", ref _squirrelImages); + LoadMedia("./Storage/pumpkin", ref _pumpkinImages); + LoadMedia("./Storage/turtles", ref _turtlesImages); + LoadMedia("./Storage/penguins", ref _penguinImages); + LoadMedia("./Storage/foxes", ref _foxImages); + LoadMedia("./Storage/dab", ref _dabImages); } - public string GetPanda() + private void LoadMedia(string path, ref string[] storage) { - return _pandaImages[_random.Next(0, _pandaImages.Length)]; + var rawLinks = File.ReadAllText(Path.GetFullPath(path)); + storage = rawLinks.Split("\n"); + _logger.Trace(LogSource.Geekbot, $"Loaded {storage.Length} Images from ${path}"); } - - public string GetCrossant() + + public string GetMedia(MediaType type) { - return _croissantImages[_random.Next(0, _croissantImages.Length)]; - } - - public string GetSquirrel() - { - return _squirrelImages[_random.Next(0, _squirrelImages.Length)]; - } - - public string GetPumpkin() - { - return _pumpkinImages[_random.Next(0, _pumpkinImages.Length)]; - } - - public string GetTurtle() - { - return _turtlesImages[_random.Next(0, _turtlesImages.Length)]; - } - - public string GetPinguin() - { - return _pinguinImages[_random.Next(0, _pinguinImages.Length)]; - } - - public string GetFox() - { - return _foxImages[_random.Next(0, _foxImages.Length)]; - } - - public string GetDab() - { - return _dabImages[_random.Next(0, _dabImages.Length)]; + var collection = type switch + { + MediaType.Panda => _pandaImages, + MediaType.Croissant => _croissantImages, + MediaType.Squirrel => _squirrelImages, + MediaType.Pumpkin => _pumpkinImages, + MediaType.Turtle => _turtlesImages, + MediaType.Penguin => _penguinImages, + MediaType.Fox => _foxImages, + MediaType.Dab => _dabImages + }; + + return collection[_random.Next(0, collection.Length)]; } } } \ No newline at end of file diff --git a/Geekbot.net/Lib/Media/MediaType.cs b/Geekbot.net/Lib/Media/MediaType.cs new file mode 100644 index 0000000..aee317b --- /dev/null +++ b/Geekbot.net/Lib/Media/MediaType.cs @@ -0,0 +1,14 @@ +namespace Geekbot.net.Lib.Media +{ + public enum MediaType + { + Panda, + Croissant, + Squirrel, + Pumpkin, + Turtle, + Penguin, + Fox, + Dab + } +} \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 3d98ad9..c96e2f5 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -160,13 +160,13 @@ namespace Geekbot.net _reactionListener = new ReactionListener(_databaseInitializer.Initialize()); _guildSettingsManager = new GuildSettingsManager(_databaseInitializer.Initialize()); var fortunes = new FortunesProvider(_logger); - var mediaProvider = new MediaProvider(_logger); var malClient = new MalClient(_globalSettings, _logger); var levelCalc = new LevelCalc(); var emojiConverter = new EmojiConverter(); var mtgManaConverter = new MtgManaConverter(); var wikipediaClient = new WikipediaClient(); var randomNumberGenerator = new RandomNumberGenerator(); + var mediaProvider = new MediaProvider(_logger, randomNumberGenerator); var kvMemoryStore = new KvInInMemoryStore(); var translationHandler = new TranslationHandler(_logger, _guildSettingsManager); var errorHandler = new ErrorHandler(_logger, translationHandler, _runParameters); diff --git a/Geekbot.net/Storage/pinguins b/Geekbot.net/Storage/penguins similarity index 99% rename from Geekbot.net/Storage/pinguins rename to Geekbot.net/Storage/penguins index 631f9d0..d38d853 100644 --- a/Geekbot.net/Storage/pinguins +++ b/Geekbot.net/Storage/penguins @@ -1,13 +1,13 @@ -https://i.ytimg.com/vi/Qr6sULJnu2o/maxresdefault.jpg -https://www.apex-expeditions.com/wp-content/uploads/2015/08/newzealandSlider_Macquarie_ElephantSealKingPenguins_GRiehle_1366x601.jpg -https://www.birdlife.org/sites/default/files/styles/1600/public/slide.jpg?itok=HRhQfA1S -http://experimentexchange.com/wp-content/uploads/2016/07/penguins-fact.jpg -http://images.mentalfloss.com/sites/default/files/styles/mf_image_16x9/public/istock-511366776.jpg?itok=cWhdWNZ8&resize=1100x619 -https://www.thevaporplace.ch/media/catalog/product/cache/1/thumbnail/800x800/9df78eab33525d08d6e5fb8d27136e95/a/t/atopack_penguin-15.jpg -https://www.superfastbusiness.com/wp-content/uploads/2015/10/real-time-penguin-algorithm-featured.jpg -http://www.antarctica.gov.au/__data/assets/image/0011/147737/varieties/antarctic.jpg -https://vignette.wikia.nocookie.net/robloxcreepypasta/images/1/11/AAEAAQAAAAAAAAdkAAAAJDc3YzkyYjJhLTYyZjctNDY2Mi04M2VjLTg4NjY4ZjgwYzRmNg.png/revision/latest?cb=20180207200526 -https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR3xV0lhpZuhT8Nmm6LaITsppZ7VfWcWXuyu2cPHrlv_dt_M92K5g -http://goboiano.com/wp-content/uploads/2017/04/Penguin-Kemeno-Friends-Waifu.jpg -https://cdn.yoast.com/app/uploads/2015/10/Penguins_1200x628.png +https://i.ytimg.com/vi/Qr6sULJnu2o/maxresdefault.jpg +https://www.apex-expeditions.com/wp-content/uploads/2015/08/newzealandSlider_Macquarie_ElephantSealKingPenguins_GRiehle_1366x601.jpg +https://www.birdlife.org/sites/default/files/styles/1600/public/slide.jpg?itok=HRhQfA1S +http://experimentexchange.com/wp-content/uploads/2016/07/penguins-fact.jpg +http://images.mentalfloss.com/sites/default/files/styles/mf_image_16x9/public/istock-511366776.jpg?itok=cWhdWNZ8&resize=1100x619 +https://www.thevaporplace.ch/media/catalog/product/cache/1/thumbnail/800x800/9df78eab33525d08d6e5fb8d27136e95/a/t/atopack_penguin-15.jpg +https://www.superfastbusiness.com/wp-content/uploads/2015/10/real-time-penguin-algorithm-featured.jpg +http://www.antarctica.gov.au/__data/assets/image/0011/147737/varieties/antarctic.jpg +https://vignette.wikia.nocookie.net/robloxcreepypasta/images/1/11/AAEAAQAAAAAAAAdkAAAAJDc3YzkyYjJhLTYyZjctNDY2Mi04M2VjLTg4NjY4ZjgwYzRmNg.png/revision/latest?cb=20180207200526 +https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR3xV0lhpZuhT8Nmm6LaITsppZ7VfWcWXuyu2cPHrlv_dt_M92K5g +http://goboiano.com/wp-content/uploads/2017/04/Penguin-Kemeno-Friends-Waifu.jpg +https://cdn.yoast.com/app/uploads/2015/10/Penguins_1200x628.png https://images.justwatch.com/backdrop/8611153/s1440/pingu \ No newline at end of file From c94d73736d6bf5a70273cd2ed020bcc106da9123 Mon Sep 17 00:00:00 2001 From: runebaas Date: Tue, 30 Jun 2020 17:54:01 +0200 Subject: [PATCH 246/443] Use greetings.dev instead of hardcoded greetings --- .../Randomness/Greetings/GreetingBaseDto.cs | 11 + .../Randomness/Greetings/GreetingDto.cs | 8 +- .../Randomness/Greetings/GreetingProvider.cs | 1595 ----------------- .../Randomness/Greetings/Greetings.cs | 28 +- 4 files changed, 37 insertions(+), 1605 deletions(-) create mode 100644 Geekbot.net/Commands/Randomness/Greetings/GreetingBaseDto.cs delete mode 100644 Geekbot.net/Commands/Randomness/Greetings/GreetingProvider.cs diff --git a/Geekbot.net/Commands/Randomness/Greetings/GreetingBaseDto.cs b/Geekbot.net/Commands/Randomness/Greetings/GreetingBaseDto.cs new file mode 100644 index 0000000..7f4e02f --- /dev/null +++ b/Geekbot.net/Commands/Randomness/Greetings/GreetingBaseDto.cs @@ -0,0 +1,11 @@ +namespace Geekbot.net.Commands.Randomness.Greetings +{ + public class GreetingBaseDto + { + public string Language { get; set; } + public string LanguageNative { get; set; } + public string LanguageCode { get; set; } + public string Script { get; set; } + public GreetingDto Primary { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/Randomness/Greetings/GreetingDto.cs b/Geekbot.net/Commands/Randomness/Greetings/GreetingDto.cs index 42792a5..c967885 100644 --- a/Geekbot.net/Commands/Randomness/Greetings/GreetingDto.cs +++ b/Geekbot.net/Commands/Randomness/Greetings/GreetingDto.cs @@ -2,9 +2,9 @@ namespace Geekbot.net.Commands.Randomness.Greetings { public class GreetingDto { - public string Text { get; set; } - public string Language { get; set; } - public string Dialect { get; set; } = null; - public string Romanization { get; set; } = null; + public string Text { get; set; } + public string Dialect { get; set; } + public string Romanization { get; set; } + public string[] Use { get; set; } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/Randomness/Greetings/GreetingProvider.cs b/Geekbot.net/Commands/Randomness/Greetings/GreetingProvider.cs deleted file mode 100644 index a15f32e..0000000 --- a/Geekbot.net/Commands/Randomness/Greetings/GreetingProvider.cs +++ /dev/null @@ -1,1595 +0,0 @@ -using System.Collections.Generic; - -namespace Geekbot.net.Commands.Randomness.Greetings -{ - public class GreetingProvider - { - public static readonly List Greetings = new List - { - new GreetingDto() - { - Text = "Бзиа збаша", - Language = "Abkhaz", - Romanization = "Bzia zbaşa" - }, - new GreetingDto() - { - Text = "Фэсапщы", - Language = "Adyghe", - Romanization = "Fèsapŝy" - }, - new GreetingDto() - { - Text = "Haai", - Language = "Afrikaans" - }, - new GreetingDto() - { - Text = "Kamusta", - Language = "Aklan" - }, - new GreetingDto() - { - Text = "Fâla", - Language = "Albanian" - }, - new GreetingDto() - { - Text = "Ç'kemi", - Language = "Albanian" - }, - new GreetingDto() - { - Text = "Aang", - Language = "Aleut" - }, - new GreetingDto() - { - Text = "Hallo", - Language = "Alsatian" - }, - new GreetingDto() - { - Text = "Эзендер", - Language = "Altay", - Romanization = "Ezender" - }, - new GreetingDto() - { - Text = "ሰላም።", - Language = "Amharic", - Romanization = "sälam" - }, - new GreetingDto() - { - Text = "السلام عليكم", - Language = "Arabic", - Romanization = "as-salām 'alaykum" - }, - new GreetingDto() - { - Text = "Ola", - Language = "Aragonese" - }, - new GreetingDto() - { - Text = "Shl'am lak", - Language = "Aramaic" - }, - new GreetingDto() - { - Text = "Héébee", - Language = "Arapaho" - }, - new GreetingDto() - { - Text = "Բարեւ", - Language = "Armenian", - Romanization = "Parev" - }, - new GreetingDto() - { - Text = "Bunâ dzuâ", - Language = "Aromanian" - }, - new GreetingDto() - { - Text = "Werte", - Language = "Arrernte" - }, - new GreetingDto() - { - Text = "নমস্কাৰ", - Language = "Assamese", - Romanization = "নমস্কাৰ" - }, - new GreetingDto() - { - Text = "Hola", - Language = "Asturian" - }, - new GreetingDto() - { - Text = "Kwei", - Language = "Atikamekw" - }, - new GreetingDto() - { - Text = "Laphi!", - Language = "Aymara" - }, - new GreetingDto() - { - Text = "Salam", - Language = "Azerbaijani" - }, - new GreetingDto() - { - Text = "Swastyastu", - Language = "Balinese" - }, - new GreetingDto() - { - Text = "I ni ce", - Language = "Bambara" - }, - new GreetingDto() - { - Text = "Haku'", - Language = "Barbareño" - }, - new GreetingDto() - { - Text = "сәләм", - Language = "Bashkir", - Romanization = "sәlәm" - }, - new GreetingDto() - { - Text = "Kaixo", - Language = "Basque" - }, - new GreetingDto() - { - Text = "Horas", - Language = "Batak" - }, - new GreetingDto() - { - Text = "მარშიხ ვალ", - Language = "Batsbi", - Romanization = "Maršix val" - }, - new GreetingDto() - { - Text = "Seavus", - Language = "Bavarian" - }, - new GreetingDto() - { - Text = "Вітаю", - Language = "Belarusian", - Romanization = "Vіtaju" - }, - new GreetingDto() - { - Text = "Shani", - Language = "Bemba" - }, - new GreetingDto() - { - Text = "নমস্কার", - Language = "Bengali", - Romanization = "Nômôskar", - Dialect = "Hindus" - }, - new GreetingDto() - { - Text = "আসসালামু আলাইকুম", - Language = "Bengali", - Romanization = "Asôlamu alaikum", - Dialect = "Muslims" - }, - new GreetingDto() - { - Text = "प्रणाम", - Language = "Bhojpuri", - Romanization = "prannam" - }, - new GreetingDto() - { - Text = "Hello", - Language = "Bikol" - }, - new GreetingDto() - { - Text = "Halo", - Language = "Bislama" - }, - new GreetingDto() - { - Text = "Dobar dan", - Language = "Bosnian" - }, - new GreetingDto() - { - Text = "Salud", - Language = "Breton" - }, - new GreetingDto() - { - Text = "Здравейте", - Language = "Bulgarian" - }, - new GreetingDto() - { - Text = "မဂႆလာပၝ", - Language = "Burmese", - Romanization = "min-ga-la-ba" - }, - new GreetingDto() - { - Text = "Olá", - Language = "Cape Verdean Creole" - }, - new GreetingDto() - { - Text = "Hola", - Language = "Catalan" - }, - new GreetingDto() - { - Text = "Hello", - Language = "Cebuano" - }, - new GreetingDto() - { - Text = "Kopisanangan", - Language = "Central Dusun" - }, - new GreetingDto() - { - Text = "Que tal?", - Language = "Chabacano de Zamboanga" - }, - new GreetingDto() - { - Text = "Ola", - Language = "Chabacano de Cavite" - }, - new GreetingDto() - { - Text = "Håfa ådai", - Language = "Chamorro", - Dialect = "Guam" - }, - new GreetingDto() - { - Text = "Hafa Adai", - Language = "Chamorro", - Dialect = "North Marianas" - }, - new GreetingDto() - { - Text = "Салам", - Language = "Chechen", - Romanization = "Salam" - }, - new GreetingDto() - { - Text = "ᎣᏏᏲ", - Language = "Cherokee", - Romanization = "Osiyo" - }, - new GreetingDto() - { - Text = "Moni", - Language = "Chichewa" - }, - new GreetingDto() - { - Text = "你好", - Language = "Chinese", - Romanization = "nǐ hǎo", - Dialect = "Mandarin" - }, - new GreetingDto() - { - Text = "ʔédlánet’é", - Language = "Chipewyan" - }, - new GreetingDto() - { - Text = "Halito", - Language = "Choctaw" - }, - new GreetingDto() - { - Text = "Ran annim", - Language = "Chuukese" - }, - new GreetingDto() - { - Text = "Салам!", - Language = "Chuvash", - Romanization = "Salam!" - }, - new GreetingDto() - { - Text = "Guuten takh!", - Language = "Cimbrian" - }, - new GreetingDto() - { - Text = "Kopivosian", - Language = "Coastal Kadazan" - }, - new GreetingDto() - { - Text = "Marʉ́awe", - Language = "Comanche" - }, - new GreetingDto() - { - Text = "Dydh da", - Language = "Cornish" - }, - new GreetingDto() - { - Text = "Salute", - Language = "Corsican" - }, - new GreetingDto() - { - Text = "ᑕᓂᓯ", - Language = "Cree", - Romanization = "Tanisi" - }, - new GreetingDto() - { - Text = "Bok", - Language = "Croatian" - }, - new GreetingDto() - { - Text = "Sho'daache", - Language = "Crow" - }, - new GreetingDto() - { - Text = "Míyaxwe", - Language = "Cupeño" - }, - new GreetingDto() - { - Text = "Hello", - Language = "Cuyonon" - }, - new GreetingDto() - { - Text = "Ahoj", - Language = "Czech" - }, - new GreetingDto() - { - Text = "Hej", - Language = "Danish" - }, - new GreetingDto() - { - Text = "ااسال م عليكم", - Language = "Dari", - Romanization = "As-salâmo 'alaykom" - }, - new GreetingDto() - { - Text = "Misawa", - Language = "Dholuo" - }, - new GreetingDto() - { - Text = "Goededag", - Language = "Dutch" - }, - new GreetingDto() - { - Text = "སྐུ་གཟུགས་བཟང་པོ་ལགས།", - Language = "Dzongkha", - Romanization = "Kuzu zangpo la" - }, - new GreetingDto() - { - Text = "Mọkọm", - Language = "Efik" - }, - new GreetingDto() - { - Text = "Häj ą̊ dig!", - Language = "Elfdalian" - }, - new GreetingDto() - { - Text = "Hello", - Language = "English" - }, - new GreetingDto() - { - Text = "Tere", - Language = "Estonian" - }, - new GreetingDto() - { - Text = "Ola!", - Language = "Extremaduran" - }, - new GreetingDto() - { - Text = "Hallo", - Language = "Faroese" - }, - new GreetingDto() - { - Text = "Bula", - Language = "Fijian" - }, - new GreetingDto() - { - Text = "Hyvää päivää", - Language = "Finnish" - }, - new GreetingDto() - { - Text = "Bonjour", - Language = "French" - }, - new GreetingDto() - { - Text = "Guundach", - Language = "Frisian", - Dialect = "North Frisian" - }, - new GreetingDto() - { - Text = "Gouden Dai", - Language = "Frisian", - Dialect = "Saterland" - }, - new GreetingDto() - { - Text = "Goeie", - Language = "Frisian", - Dialect = "West Frisian" - }, - new GreetingDto() - { - Text = "Mandi", - Language = "Friulian" - }, - new GreetingDto() - { - Text = "Ola", - Language = "Galician" - }, - new GreetingDto() - { - Text = "सिवासौँळी", - Language = "Garhwali", - Romanization = "sivāsauṁḻī" - }, - new GreetingDto() - { - Text = "Buiti binafi", - Language = "Garifuna" - }, - new GreetingDto() - { - Text = "გამარჯობა", - Language = "Georgian", - Romanization = "gamarjoba" - }, - new GreetingDto() - { - Text = "Guten Tag", - Language = "German" - }, - new GreetingDto() - { - Text = "Χαῖρε", - Language = "Greek", - Romanization = "Khaĩre", - Dialect = "Ancient" - }, - new GreetingDto() - { - Text = "Γειά", - Language = "Greek", - Romanization = "Geiá", - Dialect = "Ancient" - }, - new GreetingDto() - { - Text = "Aluu", - Language = "Greenlandic" - }, - new GreetingDto() - { - Text = "નમસ્તે", - Language = "Gujarati", - Romanization = "namaste" - }, - new GreetingDto() - { - Text = "Bonjou", - Language = "Haitian Creole" - }, - new GreetingDto() - { - Text = "Sannu", - Language = "Hausa" - }, - new GreetingDto() - { - Text = "Aloha", - Language = "Hawaiian" - }, - new GreetingDto() - { - Text = "שלום", - Language = "Hebrew", - Romanization = "Shalom" - }, - new GreetingDto() - { - Text = "something", - Language = "something" - }, - new GreetingDto() - { - Text = "Tjike", - Language = "Herero" - }, - new GreetingDto() - { - Text = "Mono", - Language = "Himba" - }, - new GreetingDto() - { - Text = "नमस्ते", - Language = "Hindi", - Romanization = "namaste" - }, - new GreetingDto() - { - Text = "Nyob zoo", - Language = "Hmong", - Dialect = "White" - }, - new GreetingDto() - { - Text = "Jó napot kívánok", - Language = "Hungarian" - }, - new GreetingDto() - { - Text = "Góðan dag", - Language = "Icelandic" - }, - new GreetingDto() - { - Text = "Ndeewo", - Language = "Igbo" - }, - new GreetingDto() - { - Text = "Kablaaw", - Language = "Iloko" - }, - new GreetingDto() - { - Text = "Tiervâ", - Language = "Inari Saami" - }, - new GreetingDto() - { - Text = "Hi", - Language = "Indonesian" - }, - new GreetingDto() - { - Text = "ᐊᐃ", - Language = "Inuktitut", - Romanization = "Ai" - }, - new GreetingDto() - { - Text = "Haluu", - Language = "Iñupiaq" - }, - new GreetingDto() - { - Text = "Dia dhuit", - Language = "Irish" - }, - new GreetingDto() - { - Text = "Ciao", - Language = "Italian" - }, - new GreetingDto() - { - Text = "Míyaxwen", - Language = "Ivilyuat" - }, - new GreetingDto() - { - Text = "Ello", - Language = "Jamaican" - }, - new GreetingDto() - { - Text = "今日は", - Language = "Japanese", - Romanization = "konnichiwa" - }, - new GreetingDto() - { - Text = "ꦲꦭꦺꦴ", - Language = "Javanese", - Romanization = "Halo" - }, - new GreetingDto() - { - Text = "Puiznu", - Language = "Jenesch" - }, - new GreetingDto() - { - Text = "Salut", - Language = "Jèrriais" - }, - new GreetingDto() - { - Text = "Godaw", - Language = "Jutish" - }, - new GreetingDto() - { - Text = "Мендвт", - Language = "Kalmyk" - }, - new GreetingDto() - { - Text = "Tọi", - Language = "Kam" - }, - new GreetingDto() - { - Text = "ನಮಸ್ತೆ", - Language = "Kannada", - Romanization = "namaste" - }, - new GreetingDto() - { - Text = "Xsaqär", - Language = "Kaqchikel" - }, - new GreetingDto() - { - Text = "Ayukîi", - Language = "Karuk" - }, - new GreetingDto() - { - Text = "Witéj", - Language = "Kashubian" - }, - new GreetingDto() - { - Text = "असलामु अलैकुम", - Language = "Kashmiri", - Romanization = "asalāmu alaikuma" - }, - new GreetingDto() - { - Text = "Hagare'enaam", - Language = "Kawaiisu" - }, - new GreetingDto() - { - Text = "Сәлем", - Language = "Kazakh", - Romanization = "Sälem" - }, - new GreetingDto() - { - Text = "ជំរាបសួរ", - Language = "Khmer", - Romanization = "chôm rab suôr" - }, - new GreetingDto() - { - Text = "Halau", - Language = "Khoekhoe" - }, - new GreetingDto() - { - Text = "wĩmwega", - Language = "Kikuyu" - }, - new GreetingDto() - { - Text = "Muraho", - Language = "Kinyarwanda" - }, - new GreetingDto() - { - Text = "Ko na mauri", - Language = "Kiribati" - }, - new GreetingDto() - { - Text = "안녕하십니까", - Language = "Korean", - Romanization = "annyeong-hasimnikka" - }, - new GreetingDto() - { - Text = "Haawka", - Language = "Kumeyaay" - }, - new GreetingDto() - { - Text = "!kao", - Language = "Kung San" - }, - new GreetingDto() - { - Text = "Rojbash", - Language = "Kurdish", - Dialect = "Kurmanji" - }, - new GreetingDto() - { - Text = "Sillaw", - Language = "Kurdish", - Dialect = "Sorani" - }, - new GreetingDto() - { - Text = "Салам!", - Language = "Kyrgyz", - Romanization = "Salam!" - }, - new GreetingDto() - { - Text = "בוינוס דייאס", - Language = "Ladino", - Romanization = "Buenos diyas" - }, - new GreetingDto() - { - Text = "Hau", - Language = "Lakota Sioux" - }, - new GreetingDto() - { - Text = "ສະບາຍດີ", - Language = "Lao", - Romanization = "sába̖ai-di̖i" - }, - new GreetingDto() - { - Text = "Ave", - Language = "Latin" - }, - new GreetingDto() - { - Text = "Sveiki", - Language = "Latvian" - }, - new GreetingDto() - { - Text = "გეგაჯგინას", - Language = "Laz", - Romanization = "Gegacginas" - }, - new GreetingDto() - { - Text = "Салам алейкум", - Language = "Lezgi", - Romanization = "Salam alejkum" - }, - new GreetingDto() - { - Text = "Hallo", - Language = "Limburgish" - }, - new GreetingDto() - { - Text = "Mbote", - Language = "Lingala" - }, - new GreetingDto() - { - Text = "Labas", - Language = "Lithuanian" - }, - new GreetingDto() - { - Text = "Moin", - Language = "Low Saxon" - }, - new GreetingDto() - { - Text = "Lumela", - Language = "Lozi" - }, - new GreetingDto() - { - Text = "Gyebale ko", - Language = "Luganda" - }, - new GreetingDto() - { - Text = "Buoris", - Language = "Lule Sámi" - }, - new GreetingDto() - { - Text = "Mííyu", - Language = "Luiseño" - }, - new GreetingDto() - { - Text = "Moien", - Language = "Luxembourgish" - }, - new GreetingDto() - { - Text = "Здраво", - Language = "Macedonian", - Romanization = "Zdravo" - }, - new GreetingDto() - { - Text = "परनाम", - Language = "Magahi", - Romanization = "Pernaam" - }, - new GreetingDto() - { - Text = "प्रनाम", - Language = "Maithili", - Romanization = "Pranaam" - }, - new GreetingDto() - { - Text = "Manao ahoana", - Language = "Malagasy" - }, - new GreetingDto() - { - Text = "Selamat pagi", - Language = "Malay" - }, - new GreetingDto() - { - Text = "നമസ്തേ", - Language = "Malayalam", - Romanization = "namastē" - }, - new GreetingDto() - { - Text = "Jeeka, ma tzuula", - Language = "Mam" - }, - new GreetingDto() - { - Text = "Moghrey mie", - Language = "Gaelic", - Dialect = "Manx" - }, - new GreetingDto() - { - Text = "Assalaamu Alaikum", - Language = "Maldivian" - }, - new GreetingDto() - { - Text = "Ħello", - Language = "Maltese" - }, - new GreetingDto() - { - Text = "Kia ora", - Language = "Māori" - }, - new GreetingDto() - { - Text = "Mari mari", - Language = "Mapuche" - }, - new GreetingDto() - { - Text = "नमस्कार", - Language = "Marathi", - Romanization = "namaskāra" - }, - new GreetingDto() - { - Text = "Io̧kwe", - Language = "Marshallese" - }, - new GreetingDto() - { - Text = "Bonzur", - Language = "Mauritian Creole" - }, - new GreetingDto() - { - Text = "Gwe’", - Language = "Míkmaq" - }, - new GreetingDto() - { - Text = "ဟာဲ", - Language = "Mon", - Romanization = "hāai" - }, - new GreetingDto() - { - Text = "Ciau", - Language = "Monégasque" - }, - new GreetingDto() - { - Text = "Сайн уу", - Language = "Mongolian", - Romanization = "Sain uu" - }, - new GreetingDto() - { - Text = "Manahúú", - Language = "Mono" - }, - new GreetingDto() - { - Text = "Ne y windiga", - Language = "Mossi" - }, - new GreetingDto() - { - Text = "Niltze", - Language = "Nahuatl" - }, - new GreetingDto() - { - Text = "Ekamowir omo", - Language = "Nauruan" - }, - new GreetingDto() - { - Text = "Yá'át'ééh", - Language = "Navajo" - }, - new GreetingDto() - { - Text = "ज्वजलपा", - Language = "Newari", - Romanization = "jvajalapā" - }, - new GreetingDto() - { - Text = "Oraire ota?", - Language = "Nkore" - }, - new GreetingDto() - { - Text = "Salibonani", - Language = "Ndebele" - }, - new GreetingDto() - { - Text = "Lotjhani", - Language = "Southern Ndebele" - }, - new GreetingDto() - { - Text = "नमस्ते", - Language = "Nepali", - Romanization = "namaste" - }, - new GreetingDto() - { - Text = "Fakaalofa atu", - Language = "Niuean" - }, - new GreetingDto() - { - Text = "Салам!", - Language = "Nogai" - }, - new GreetingDto() - { - Text = "Bures", - Language = "Northern Sámi" - }, - new GreetingDto() - { - Text = "Dumêlang", - Language = "Northern Sotho" - }, - new GreetingDto() - { - Text = "Goddag", - Language = "Norwegian" - }, - new GreetingDto() - { - Text = "Hatʸu", - Language = "Northern Chumash" - }, - new GreetingDto() - { - Text = "Bonjorn", - Language = "Occitan" - }, - new GreetingDto() - { - Text = "Aniin", - Language = "Ojibwe" - }, - new GreetingDto() - { - Text = "今日拝なびら", - Language = "Okinawan", - Romanization = "chuu wuganabira" - }, - new GreetingDto() - { - Text = "Wes hāl", - Language = "Old English" - }, - new GreetingDto() - { - Text = "Ƿes hāl", - Language = "Old Prussian" - }, - new GreetingDto() - { - Text = "Ahó", - Language = "Omaha" - }, - new GreetingDto() - { - Text = "ନମସ୍କାର", - Language = "Oriya", - Romanization = "Namascāra" - }, - new GreetingDto() - { - Text = "Alii", - Language = "Palauan" - }, - new GreetingDto() - { - Text = "Bon dia", - Language = "Papiamento" - }, - new GreetingDto() - { - Text = "ښې چارې", - Language = "Pashto", - Romanization = "khe chare" - }, - new GreetingDto() - { - Text = "Gude Daag", - Language = "Pennsylvania Deitsch" - }, - new GreetingDto() - { - Text = "درود", - Language = "Persian", - Romanization = "dorood", - Dialect = "Farsi" - }, - new GreetingDto() - { - Text = "Bojour", - Language = "Picard" - }, - new GreetingDto() - { - Text = "Wai", - Language = "Pitjantjatjara" - }, - new GreetingDto() - { - Text = "Cześć", - Language = "Polish" - }, - new GreetingDto() - { - Text = "Olá", - Language = "Portuguese", - Dialect = "Protugal" - }, - new GreetingDto() - { - Text = "Oi", - Language = "Portuguese", - Dialect = "Brazilian" - }, - new GreetingDto() - { - Text = "ਸਤਿ ਸ੍ਰੀ ਅਕਾਲ।", - Language = "Punjabi", - Romanization = "sat srī akāl" - }, - new GreetingDto() - { - Text = "Rimaykullayki", - Language = "Quechua" - }, - new GreetingDto() - { - Text = "'Iorana", - Language = "Rapa Nui" - }, - new GreetingDto() - { - Text = "Bonzour", - Language = "Réunion Creole" - }, - new GreetingDto() - { - Text = "Sastipe", - Language = "Romani" - }, - new GreetingDto() - { - Text = "Salut", - Language = "Romanian" - }, - new GreetingDto() - { - Text = "Ciao", - Language = "Romansh" - }, - new GreetingDto() - { - Text = "Привет", - Language = "Russian", - Romanization = "Privet" - }, - new GreetingDto() - { - Text = "Слава Исусу Христу", - Language = "Rusyn", - Dialect = "Slava Ysusu Chrystu" - }, - new GreetingDto() - { - Text = "бирибиэт", - Language = "Sakha", - Romanization = "biribiet" - }, - new GreetingDto() - { - Text = "Malō", - Language = "Samoan" - }, - new GreetingDto() - { - Text = "Bara mo", - Language = "Sango" - }, - new GreetingDto() - { - Text = "नमस्ते", - Language = "Sanskrit", - Romanization = "namaste" - }, - new GreetingDto() - { - Text = "Saludi", - Language = "Sardinian", - Dialect = "Campidanese" - }, - new GreetingDto() - { - Text = "Bone die", - Language = "Sardinian", - Dialect = "Logudorese" - }, - new GreetingDto() - { - Text = "Hullo", - Language = "Scots" - }, - new GreetingDto() - { - Text = "Halò", - Language = "Gaelic", - Dialect = "Scottish" - }, - new GreetingDto() - { - Text = "Здраво", - Language = "Serbian", - Dialect = "Zdravo" - }, - new GreetingDto() - { - Text = "Lumela", - Language = "Sesotho" - }, - new GreetingDto() - { - Text = "Mhoro", - Language = "Shona" - }, - new GreetingDto() - { - Text = "Ciau", - Language = "Sicilian" - }, - new GreetingDto() - { - Text = "سَلامُ", - Language = "Sindhi", - Romanization = "Salāmu" - }, - new GreetingDto() - { - Text = "ආයුඛෝවන්", - Language = "Sinhala", - Romanization = "āyubūvan" - }, - new GreetingDto() - { - Text = "Dobry den", - Language = "Slovak" - }, - new GreetingDto() - { - Text = "Pozdravljeni", - Language = "Slovenian" - }, - new GreetingDto() - { - Text = "Simi", - Language = "Solresol" - }, - new GreetingDto() - { - Text = "Salaam alaykum", - Language = "Somali" - }, - new GreetingDto() - { - Text = "Dobry źeń", - Language = "Sorbian", - Dialect = "Lower Sorbian" - }, - new GreetingDto() - { - Text = "Dobry dźeń", - Language = "Sorbian", - Dialect = "Upper Sorbian" - }, - new GreetingDto() - { - Text = "Buaregh", - Language = "Southern Sámi" - }, - new GreetingDto() - { - Text = "¡Hola!", - Language = "Spanish" - }, - new GreetingDto() - { - Text = "Hoj", - Language = "Stellingwarfs" - }, - new GreetingDto() - { - Text = "Sampurasun", - Language = "Sundanese" - }, - new GreetingDto() - { - Text = "Habari", - Language = "Swahili" - }, - new GreetingDto() - { - Text = "Sawubona", - Language = "Swazi" - }, - new GreetingDto() - { - Text = "God dag", - Language = "Swedish" - }, - new GreetingDto() - { - Text = "Grüezi", - Language = "Swiss German" - }, - new GreetingDto() - { - Text = "Салам", - Language = "Tabassaran", - Romanization = "Salam" - }, - new GreetingDto() - { - Text = "Musta?", - Language = "Tagalog" - }, - new GreetingDto() - { - Text = "'Ia ora na", - Language = "Tahitian" - }, - new GreetingDto() - { - Text = "Ассалому алейкум", - Language = "Tajik", - Romanization = "Assalomu alejkym" - }, - new GreetingDto() - { - Text = "வணக்கம்", - Language = "Tamil", - Romanization = "vaṇakkam" - }, - new GreetingDto() - { - Text = "Isänme", - Language = "Tatar" - }, - new GreetingDto() - { - Text = "నమస్కారం", - Language = "Telugu", - Romanization = "namaskārām" - }, - new GreetingDto() - { - Text = "Elo", - Language = "Tetum" - }, - new GreetingDto() - { - Text = "Miga", - Language = "Teribe" - }, - new GreetingDto() - { - Text = "สวัสดี", - Language = "Thai", - Romanization = "sà-wàt-dee" - }, - new GreetingDto() - { - Text = "བཀྲ་ཤིས་བདེ་ལེགས།", - Language = "Tibetan", - Romanization = "tashi delek" - }, - new GreetingDto() - { - Text = "ሰላም", - Language = "Tigrinya", - Romanization = "selam" - }, - new GreetingDto() - { - Text = "Tālofa", - Language = "Tokelauan" - }, - new GreetingDto() - { - Text = "Gude", - Language = "Tok Pisin" - }, - new GreetingDto() - { - Text = "Mālō e lelei", - Language = "Tongan" - }, - new GreetingDto() - { - Text = "Miiyiha", - Language = "Tongva", - Dialect = "Gabrielino" - }, - new GreetingDto() - { - Text = "Xewani", - Language = "Tsonga" - }, - new GreetingDto() - { - Text = "K'uxi", - Language = "Tsotsil" - }, - new GreetingDto() - { - Text = "Dumela", - Language = "Tswana" - }, - new GreetingDto() - { - Text = "АсаламугIалейкум!", - Language = "Tsez", - Romanization = "AsalamugIalejkum!" - }, - new GreetingDto() - { - Text = "Monile", - Language = "Tumbuka" - }, - new GreetingDto() - { - Text = "Merhaba", - Language = "Turkish" - }, - new GreetingDto() - { - Text = "Salam", - Language = "Turkmen" - }, - new GreetingDto() - { - Text = "Вітаю", - Language = "Ukrainian" - }, - new GreetingDto() - { - Text = "Сородэ", - Language = "Ulch", - Romanization = "Sorodè" - }, - new GreetingDto() - { - Text = "السلام علیکم", - Language = "Urdu", - Romanization = "āssālam 'alaykum" - }, - new GreetingDto() - { - Text = "ئەسسالامۇ ئەلەيكۇم", - Language = "Uyghur", - Romanization = "Ässalamu äläykum" - }, - new GreetingDto() - { - Text = "Assalomu Alaykum", - Language = "Uzbek" - }, - new GreetingDto() - { - Text = "I nhlikanhi", - Language = "Venda" - }, - new GreetingDto() - { - Text = "Ciao", - Language = "Venetian" - }, - new GreetingDto() - { - Text = "Tervhen", - Language = "Veps" - }, - new GreetingDto() - { - Text = "Chào anh", - Language = "Vietnamese" - }, - new GreetingDto() - { - Text = "Tereq", - Language = "Võro" - }, - new GreetingDto() - { - Text = "Bondjoû", - Language = "Walloon" - }, - new GreetingDto() - { - Text = "Ngurrju mayinpa", - Language = "Warlpiri" - }, - new GreetingDto() - { - Text = "Helô", - Language = "Welsh" - }, - new GreetingDto() - { - Text = "Hej", - Language = "Westrobothnian" - }, - new GreetingDto() - { - Text = "Na nga def", - Language = "Wolof" - }, - new GreetingDto() - { - Text = "Molo", - Language = "Xhosa" - }, - new GreetingDto() - { - Text = "Mogethin", - Language = "Yapese" - }, - new GreetingDto() - { - Text = "אַ גוטן טאָג", - Language = "Yiddish", - Romanization = "A gutn tog" - }, - new GreetingDto() - { - Text = "Ẹ n lẹ", - Language = "Yoruba" - }, - new GreetingDto() - { - Text = "Ba'ax ka wa'alik?", - Language = "Yucatec Maya" - }, - new GreetingDto() - { - Text = "Selam", - Language = "Zazaki" - }, - new GreetingDto() - { - Text = "Sawubona", - Language = "Zulu" - }, - new GreetingDto() - { - Text = "Saluton", - Language = "Esperanto" - }, - new GreetingDto() - { - Text = "Bon die!", - Language = "Interlingua" - }, - new GreetingDto() - { - Text = "nuqneH", - Language = "Klingon" - }, - new GreetingDto() - { - Text = "Aiya", - Language = "Quenya" - }, - new GreetingDto() - { - Text = "Glidö", - Language = "Volapük" - }, - new GreetingDto() - { - Text = "Saluto", - Language = "Ido" - } - }; - - public static readonly int GreetingCount = Greetings.Count; - } -} \ No newline at end of file diff --git a/Geekbot.net/Commands/Randomness/Greetings/Greetings.cs b/Geekbot.net/Commands/Randomness/Greetings/Greetings.cs index 334b894..c47bf2a 100644 --- a/Geekbot.net/Commands/Randomness/Greetings/Greetings.cs +++ b/Geekbot.net/Commands/Randomness/Greetings/Greetings.cs @@ -1,9 +1,12 @@ using System; +using System.Net.Http; using System.Threading.Tasks; using Discord; using Discord.Commands; +using Geekbot.net.Commands.Randomness.Cat; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; +using Newtonsoft.Json; namespace Geekbot.net.Commands.Randomness.Greetings { @@ -23,20 +26,20 @@ namespace Geekbot.net.Commands.Randomness.Greetings { try { - var greeting = GreetingProvider.Greetings[new Random().Next(GreetingProvider.GreetingCount - 1)]; + var greeting = await GetRandomGreeting(); var eb = new EmbedBuilder(); - eb.Title = greeting.Text; + eb.Title = greeting.Primary.Text; eb.AddInlineField("Language", greeting.Language); - if (greeting.Dialect != null) + if (greeting.Primary.Dialect != null) { - eb.AddInlineField("Dialect", greeting.Dialect); + eb.AddInlineField("Dialect", greeting.Primary.Dialect); } - if (greeting.Romanization != null) + if (greeting.Primary.Romanization != null) { - eb.AddInlineField("Roman", greeting.Romanization); + eb.AddInlineField("Roman", greeting.Primary.Romanization); } await ReplyAsync(string.Empty, false, eb.Build()); @@ -46,5 +49,18 @@ namespace Geekbot.net.Commands.Randomness.Greetings await _errorHandler.HandleCommandException(e, Context); } } + + private async Task GetRandomGreeting() + { + using var client = new HttpClient + { + BaseAddress = new Uri("https://api.greetings.dev") + }; + var response = await client.GetAsync("/v1/greeting"); + response.EnsureSuccessStatusCode(); + + var stringResponse = await response.Content.ReadAsStringAsync(); + return JsonConvert.DeserializeObject(stringResponse); + } } } \ No newline at end of file From 0589b8e91b8cf00474f7fafabb8ca1f13740dd95 Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 10 Jul 2020 22:32:42 +0200 Subject: [PATCH 247/443] Update EntityFrameworkCore and microsoft.extensions.* to .net5 preview 6 --- Geekbot.net/Geekbot.net.csproj | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 4e13438..969cc30 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -28,31 +28,27 @@ - - - - - - - - + + + + + + + + - + - - 4.3.0 - - - 4.3.0 - + + From ba0d116f3e4e70bdc1a10b9bd8e19e603bf9b774 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 15 Jul 2020 02:52:13 +0200 Subject: [PATCH 248/443] Add an abstraction for http calls --- .../UbranDictionary/UrbanDictionary.cs | 13 +----- Geekbot.net/Commands/Randomness/Cat/Cat.cs | 29 +++---------- .../Randomness/Chuck/ChuckNorrisJokes.cs | 14 ++----- .../Commands/Randomness/Dad/DadJokes.cs | 22 ++-------- Geekbot.net/Commands/Randomness/Dog/Dog.cs | 28 +++---------- .../Randomness/Greetings/Greetings.cs | 19 +-------- .../Commands/Randomness/Kanye/Kanye.cs | 22 ++-------- .../Commands/Utils/Changelog/Changelog.cs | 22 ++++------ Geekbot.net/Lib/HttpAbstractions.cs | 41 +++++++++++++++++++ 9 files changed, 75 insertions(+), 135 deletions(-) create mode 100644 Geekbot.net/Lib/HttpAbstractions.cs diff --git a/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs b/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs index 1f33573..882caa5 100644 --- a/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs +++ b/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs @@ -1,12 +1,11 @@ using System; using System.Linq; -using System.Net.Http; using System.Threading.Tasks; using Discord; using Discord.Commands; +using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; -using Newtonsoft.Json; namespace Geekbot.net.Commands.Integrations.UbranDictionary { @@ -25,15 +24,7 @@ namespace Geekbot.net.Commands.Integrations.UbranDictionary { try { - using var client = new HttpClient - { - BaseAddress = new Uri("https://api.urbandictionary.com") - }; - var response = await client.GetAsync($"/v0/define?term={word}"); - response.EnsureSuccessStatusCode(); - - var stringResponse = await response.Content.ReadAsStringAsync(); - var definitions = JsonConvert.DeserializeObject(stringResponse); + var definitions = await HttpAbstractions.Get(new Uri($"https://api.urbandictionary.com/v0/define?term={word}")); if (definitions.List.Count == 0) { await ReplyAsync("That word hasn't been defined..."); diff --git a/Geekbot.net/Commands/Randomness/Cat/Cat.cs b/Geekbot.net/Commands/Randomness/Cat/Cat.cs index 3ac0764..7cd7b19 100644 --- a/Geekbot.net/Commands/Randomness/Cat/Cat.cs +++ b/Geekbot.net/Commands/Randomness/Cat/Cat.cs @@ -1,10 +1,9 @@ using System; -using System.Net.Http; using System.Threading.Tasks; using Discord; using Discord.Commands; +using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; -using Newtonsoft.Json; namespace Geekbot.net.Commands.Randomness.Cat { @@ -23,28 +22,12 @@ namespace Geekbot.net.Commands.Randomness.Cat { try { - - try + var response = await HttpAbstractions.Get(new Uri("https://aws.random.cat/meow")); + var eb = new EmbedBuilder { - using var client = new HttpClient - { - BaseAddress = new Uri("https://aws.random.cat") - }; - var response = await client.GetAsync("/meow"); - response.EnsureSuccessStatusCode(); - - var stringResponse = await response.Content.ReadAsStringAsync(); - var catFile = JsonConvert.DeserializeObject(stringResponse); - var eb = new EmbedBuilder - { - ImageUrl = catFile.File - }; - await ReplyAsync("", false, eb.Build()); - } - catch - { - await ReplyAsync("Seems like the dog cought the cat (error occured)"); - } + ImageUrl = response.File + }; + await ReplyAsync("", false, eb.Build()); } catch (Exception e) { diff --git a/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs b/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs index 5c28a9a..95dcfd4 100644 --- a/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs +++ b/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs @@ -1,10 +1,9 @@ using System; using System.Net.Http; -using System.Net.Http.Headers; using System.Threading.Tasks; using Discord.Commands; +using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; -using Newtonsoft.Json; namespace Geekbot.net.Commands.Randomness.Chuck { @@ -25,15 +24,8 @@ namespace Geekbot.net.Commands.Randomness.Chuck { try { - using var client = new HttpClient(); - client.DefaultRequestHeaders.Accept.Clear(); - client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json")); - var response = await client.GetAsync("https://api.chucknorris.io/jokes/random"); - response.EnsureSuccessStatusCode(); - - var stringResponse = await response.Content.ReadAsStringAsync(); - var data = JsonConvert.DeserializeObject(stringResponse); - await ReplyAsync(data.Value); + var response = await HttpAbstractions.Get(new Uri("https://api.chucknorris.io/jokes/random")); + await ReplyAsync(response.Value); } catch (HttpRequestException) { diff --git a/Geekbot.net/Commands/Randomness/Dad/DadJokes.cs b/Geekbot.net/Commands/Randomness/Dad/DadJokes.cs index bcedd96..7aabff3 100644 --- a/Geekbot.net/Commands/Randomness/Dad/DadJokes.cs +++ b/Geekbot.net/Commands/Randomness/Dad/DadJokes.cs @@ -1,10 +1,8 @@ using System; -using System.Net.Http; -using System.Net.Http.Headers; using System.Threading.Tasks; using Discord.Commands; +using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; -using Newtonsoft.Json; namespace Geekbot.net.Commands.Randomness.Dad { @@ -23,22 +21,8 @@ namespace Geekbot.net.Commands.Randomness.Dad { try { - try - { - using var client = new HttpClient(); - client.DefaultRequestHeaders.Accept.Clear(); - client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json")); - var response = await client.GetAsync("https://icanhazdadjoke.com/"); - response.EnsureSuccessStatusCode(); - - var stringResponse = await response.Content.ReadAsStringAsync(); - var data = JsonConvert.DeserializeObject(stringResponse); - await ReplyAsync(data.Joke); - } - catch (HttpRequestException) - { - await ReplyAsync("Api down..."); - } + var response = await HttpAbstractions.Get(new Uri("https://icanhazdadjoke.com/")); + await ReplyAsync(response.Joke); } catch (Exception e) { diff --git a/Geekbot.net/Commands/Randomness/Dog/Dog.cs b/Geekbot.net/Commands/Randomness/Dog/Dog.cs index d7404c6..b3d603b 100644 --- a/Geekbot.net/Commands/Randomness/Dog/Dog.cs +++ b/Geekbot.net/Commands/Randomness/Dog/Dog.cs @@ -1,10 +1,9 @@ using System; -using System.Net.Http; using System.Threading.Tasks; using Discord; using Discord.Commands; +using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; -using Newtonsoft.Json; namespace Geekbot.net.Commands.Randomness.Dog { @@ -23,27 +22,12 @@ namespace Geekbot.net.Commands.Randomness.Dog { try { - try + var response = await HttpAbstractions.Get(new Uri("http://random.dog/woof.json")); + var eb = new EmbedBuilder { - using var client = new HttpClient - { - BaseAddress = new Uri("http://random.dog") - }; - var response = await client.GetAsync("/woof.json"); - response.EnsureSuccessStatusCode(); - - var stringResponse = await response.Content.ReadAsStringAsync(); - var dogFile = JsonConvert.DeserializeObject(stringResponse); - var eb = new EmbedBuilder - { - ImageUrl = dogFile.Url - }; - await ReplyAsync("", false, eb.Build()); - } - catch (HttpRequestException e) - { - await ReplyAsync($"Seems like the dog got lost (error occured)\r\n{e.Message}"); - } + ImageUrl = response.Url + }; + await ReplyAsync("", false, eb.Build()); } catch (Exception e) { diff --git a/Geekbot.net/Commands/Randomness/Greetings/Greetings.cs b/Geekbot.net/Commands/Randomness/Greetings/Greetings.cs index c47bf2a..85e9152 100644 --- a/Geekbot.net/Commands/Randomness/Greetings/Greetings.cs +++ b/Geekbot.net/Commands/Randomness/Greetings/Greetings.cs @@ -1,12 +1,10 @@ using System; -using System.Net.Http; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Commands.Randomness.Cat; +using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; -using Newtonsoft.Json; namespace Geekbot.net.Commands.Randomness.Greetings { @@ -26,7 +24,7 @@ namespace Geekbot.net.Commands.Randomness.Greetings { try { - var greeting = await GetRandomGreeting(); + var greeting = await HttpAbstractions.Get(new Uri("https://api.greetings.dev/v1/greeting")); var eb = new EmbedBuilder(); eb.Title = greeting.Primary.Text; @@ -49,18 +47,5 @@ namespace Geekbot.net.Commands.Randomness.Greetings await _errorHandler.HandleCommandException(e, Context); } } - - private async Task GetRandomGreeting() - { - using var client = new HttpClient - { - BaseAddress = new Uri("https://api.greetings.dev") - }; - var response = await client.GetAsync("/v1/greeting"); - response.EnsureSuccessStatusCode(); - - var stringResponse = await response.Content.ReadAsStringAsync(); - return JsonConvert.DeserializeObject(stringResponse); - } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/Randomness/Kanye/Kanye.cs b/Geekbot.net/Commands/Randomness/Kanye/Kanye.cs index 36f1d1a..2aca31d 100644 --- a/Geekbot.net/Commands/Randomness/Kanye/Kanye.cs +++ b/Geekbot.net/Commands/Randomness/Kanye/Kanye.cs @@ -1,10 +1,8 @@ using System; -using System.Net.Http; -using System.Net.Http.Headers; using System.Threading.Tasks; using Discord.Commands; +using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; -using Newtonsoft.Json; namespace Geekbot.net.Commands.Randomness.Kanye { @@ -23,22 +21,8 @@ namespace Geekbot.net.Commands.Randomness.Kanye { try { - try - { - using var client = new HttpClient(); - client.DefaultRequestHeaders.Accept.Clear(); - client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json")); - var response = await client.GetAsync("https://api.kanye.rest/"); - response.EnsureSuccessStatusCode(); - - var stringResponse = await response.Content.ReadAsStringAsync(); - var data = JsonConvert.DeserializeObject(stringResponse); - await ReplyAsync(data.Quote); - } - catch (HttpRequestException) - { - await ReplyAsync("Api down..."); - } + var response = await HttpAbstractions.Get(new Uri("https://api.kanye.rest/")); + await ReplyAsync(response.Quote); } catch (Exception e) { diff --git a/Geekbot.net/Commands/Utils/Changelog/Changelog.cs b/Geekbot.net/Commands/Utils/Changelog/Changelog.cs index 02981df..335ee3f 100644 --- a/Geekbot.net/Commands/Utils/Changelog/Changelog.cs +++ b/Geekbot.net/Commands/Utils/Changelog/Changelog.cs @@ -1,14 +1,13 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Net.Http; using System.Text; using System.Threading.Tasks; using Discord; using Discord.Commands; using Discord.WebSocket; +using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; -using Newtonsoft.Json; namespace Geekbot.net.Commands.Utils.Changelog { @@ -29,17 +28,14 @@ namespace Geekbot.net.Commands.Utils.Changelog { try { - using var client = new HttpClient - { - BaseAddress = new Uri("https://api.github.com") - }; - client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", - "http://developer.github.com/v3/#user-agent-required"); - var response = await client.GetAsync("/repos/pizzaandcoffee/geekbot.net/commits"); - response.EnsureSuccessStatusCode(); - - var stringResponse = await response.Content.ReadAsStringAsync(); - var commits = JsonConvert.DeserializeObject>(stringResponse); + var commits = await HttpAbstractions.Get>( + new Uri("https://api.github.com/repos/pizzaandcoffee/geekbot.net/commits"), + new Dictionary() + { + { "User-Agent", "http://developer.github.com/v3/#user-agent-required" } + } + ); + var eb = new EmbedBuilder(); eb.WithColor(new Color(143, 165, 102)); eb.WithAuthor(new EmbedAuthorBuilder diff --git a/Geekbot.net/Lib/HttpAbstractions.cs b/Geekbot.net/Lib/HttpAbstractions.cs new file mode 100644 index 0000000..cf609f6 --- /dev/null +++ b/Geekbot.net/Lib/HttpAbstractions.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Threading.Tasks; +using Newtonsoft.Json; + +namespace Geekbot.net.Lib +{ + public class HttpAbstractions + { + public static async Task Get(Uri location, Dictionary additionalHeaders = null) + { + using var client = new HttpClient + { + BaseAddress = location, + DefaultRequestHeaders = + { + Accept = + { + MediaTypeWithQualityHeaderValue.Parse("application/json") + } + } + }; + + if (additionalHeaders != null) + { + foreach (var (name, val) in additionalHeaders) + { + client.DefaultRequestHeaders.TryAddWithoutValidation(name, val); + } + } + + var response = await client.GetAsync(location.PathAndQuery); + response.EnsureSuccessStatusCode(); + + var stringResponse = await response.Content.ReadAsStringAsync(); + return JsonConvert.DeserializeObject(stringResponse); + } + } +} \ No newline at end of file From efed2f712048e904de30299d66477003cd1ac533 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 15 Jul 2020 03:09:43 +0200 Subject: [PATCH 249/443] Add the !corona command --- .../Commands/Utils/Corona/CoronaStats.cs | 67 +++++++++++++++++++ .../Commands/Utils/Corona/CoronaSummaryDto.cs | 9 +++ 2 files changed, 76 insertions(+) create mode 100644 Geekbot.net/Commands/Utils/Corona/CoronaStats.cs create mode 100644 Geekbot.net/Commands/Utils/Corona/CoronaSummaryDto.cs diff --git a/Geekbot.net/Commands/Utils/Corona/CoronaStats.cs b/Geekbot.net/Commands/Utils/Corona/CoronaStats.cs new file mode 100644 index 0000000..4be8195 --- /dev/null +++ b/Geekbot.net/Commands/Utils/Corona/CoronaStats.cs @@ -0,0 +1,67 @@ +using System; +using System.Text; +using System.Threading.Tasks; +using Discord; +using Discord.Commands; +using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; +using Geekbot.net.Lib.Extensions; + +namespace Geekbot.net.Commands.Utils.Corona +{ + public class CoronaStats : ModuleBase + { + private readonly IErrorHandler _errorHandler; + + public CoronaStats(IErrorHandler errorHandler) + { + _errorHandler = errorHandler; + } + + [Command("corona", RunMode = RunMode.Async)] + [Summary("Get the latest worldwide corona statistics")] + public async Task Summary() + { + try + { + var summary = await HttpAbstractions.Get(new Uri("https://api.covid19api.com/world/total")); + var activeCases = summary.TotalConfirmed - (summary.TotalRecovered + summary.TotalDeaths); + + string CalculatePercentage(decimal i) => (i / summary.TotalConfirmed).ToString("#0.##%"); + var activePercent = CalculatePercentage(activeCases); + var recoveredPercentage = CalculatePercentage(summary.TotalRecovered); + var deathsPercentage = CalculatePercentage(summary.TotalDeaths); + + var numberFormat = "#,#"; + var totalFormatted = summary.TotalConfirmed.ToString(numberFormat); + var activeFormatted = activeCases.ToString(numberFormat); + var recoveredFormatted = summary.TotalRecovered.ToString(numberFormat); + var deathsFormatted = summary.TotalDeaths.ToString(numberFormat); + + var eb = new EmbedBuilder + { + Author = new EmbedAuthorBuilder + { + Name = "Confirmed Corona Cases", + IconUrl = "https://www.redcross.org/content/dam/icons/disasters/virus/Virus-1000x1000-R-Pl.png" + }, + Footer = new EmbedFooterBuilder + { + Text = "Source: covid19api.com", + }, + Color = Color.Red + }; + eb.AddField("Total", totalFormatted); + eb.AddInlineField("Active", $"{activeFormatted} ({activePercent})"); + eb.AddInlineField("Recovered", $"{recoveredFormatted} ({recoveredPercentage})"); + eb.AddInlineField("Deaths", $"{deathsFormatted} ({deathsPercentage})"); + + await Context.Channel.SendMessageAsync(String.Empty, false, eb.Build()); + } + catch (Exception e) + { + await _errorHandler.HandleCommandException(e, Context); + } + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/Utils/Corona/CoronaSummaryDto.cs b/Geekbot.net/Commands/Utils/Corona/CoronaSummaryDto.cs new file mode 100644 index 0000000..5639249 --- /dev/null +++ b/Geekbot.net/Commands/Utils/Corona/CoronaSummaryDto.cs @@ -0,0 +1,9 @@ +namespace Geekbot.net.Commands.Utils.Corona +{ + public class CoronaSummaryDto + { + public decimal TotalConfirmed { get; set; } + public decimal TotalDeaths { get; set; } + public decimal TotalRecovered { get; set; } + } +} \ No newline at end of file From 5e9cb8a4c18f739e00807511d8fa656591711956 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 15 Jul 2020 03:11:36 +0200 Subject: [PATCH 250/443] Make main async --- Geekbot.net/Program.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index c96e2f5..689c170 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -44,7 +44,7 @@ namespace Geekbot.net private IReactionListener _reactionListener; private IGuildSettingsManager _guildSettingsManager; - private static void Main(string[] args) + private static async Task Main(string[] args) { RunParameters runParameters = null; Parser.Default.ParseArguments(args) @@ -63,7 +63,7 @@ namespace Geekbot.net logger.Information(LogSource.Geekbot, "Starting..."); try { - new Program().MainAsync(runParameters, logger).GetAwaiter().GetResult(); + await new Program().Start(runParameters, logger); } catch (Exception e) { @@ -71,7 +71,7 @@ namespace Geekbot.net } } - private async Task MainAsync(RunParameters runParameters, GeekbotLogger logger) + private async Task Start(RunParameters runParameters, GeekbotLogger logger) { _logger = logger; _runParameters = runParameters; From 4cd7ac1d799acac1b2866e9b064223ebcbb98c19 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 15 Jul 2020 17:10:50 +0200 Subject: [PATCH 251/443] Fix broken DM Channel support --- Geekbot.net/Handlers/CommandHandler.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Geekbot.net/Handlers/CommandHandler.cs b/Geekbot.net/Handlers/CommandHandler.cs index b811a8e..b6a3d9f 100644 --- a/Geekbot.net/Handlers/CommandHandler.cs +++ b/Geekbot.net/Handlers/CommandHandler.cs @@ -52,7 +52,12 @@ namespace Geekbot.net.Handlers if (!(messageParam is SocketUserMessage message)) return Task.CompletedTask; if (message.Author.IsBot) return Task.CompletedTask; - var guildId = ((SocketGuildChannel) message.Channel).Guild.Id; + ulong guildId = message.Author switch + { + SocketGuildUser user => user.Guild.Id, + _ => 0 // DM Channel + }; + if (IsIgnoredGuild(guildId, message.Author.Id)) return Task.CompletedTask; var lowCaseMsg = message.ToString().ToLower(); @@ -102,12 +107,14 @@ namespace Geekbot.net.Handlers private bool ShouldPong(string lowerCaseMessage, ulong guildId) { if (!lowerCaseMessage.StartsWith("ping ") && !lowerCaseMessage.Equals("ping")) return false; + if (guildId == 0) return true; return GetGuildSettings(guildId)?.Ping ?? false; } private bool ShouldHui(string lowerCaseMessage, ulong guildId) { if (!lowerCaseMessage.StartsWith("hui")) return false; + if (guildId == 0) return true; return GetGuildSettings(guildId)?.Hui ?? false; } From 77e912501df42181578c4b1187cb94354510aa84 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 22 Jul 2020 14:09:26 +0200 Subject: [PATCH 252/443] Upgrade to .NET5 preview 7 --- Geekbot.net/Geekbot.net.csproj | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 969cc30..37dc310 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -28,14 +28,14 @@ - - - - - - - - + + + + + + + + From fff232423264abed48ad3a4e1d93c09283d3d290 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 22 Jul 2020 14:17:08 +0200 Subject: [PATCH 253/443] Fix build warnings in MediaProvider.cs and TranslationHandler.cs --- .../Lib/Localization/TranslationHandler.cs | 8 +++---- Geekbot.net/Lib/Media/MediaProvider.cs | 21 ++++++++++--------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/Geekbot.net/Lib/Localization/TranslationHandler.cs b/Geekbot.net/Lib/Localization/TranslationHandler.cs index 89ba632..6166e6a 100644 --- a/Geekbot.net/Lib/Localization/TranslationHandler.cs +++ b/Geekbot.net/Lib/Localization/TranslationHandler.cs @@ -89,7 +89,7 @@ namespace Geekbot.net.Lib.Localization } } - private async Task GetServerLanguage(ulong guildId) + private Task GetServerLanguage(ulong guildId) { try { @@ -99,7 +99,7 @@ namespace Geekbot.net.Lib.Localization lang = _serverLanguages[guildId]; if (!string.IsNullOrEmpty(lang)) { - return lang; + return Task.FromResult(lang); } throw new Exception(); } @@ -107,13 +107,13 @@ namespace Geekbot.net.Lib.Localization { lang = _guildSettingsManager.GetSettings(guildId, false)?.Language ?? "EN"; _serverLanguages[guildId] = lang; - return lang; + return Task.FromResult(lang); } } catch (Exception e) { _logger.Error(LogSource.Geekbot, "Could not get guild language", e); - return "EN"; + return Task.FromResult("EN"); } } diff --git a/Geekbot.net/Lib/Media/MediaProvider.cs b/Geekbot.net/Lib/Media/MediaProvider.cs index 45524de..5a9a055 100644 --- a/Geekbot.net/Lib/Media/MediaProvider.cs +++ b/Geekbot.net/Lib/Media/MediaProvider.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.IO; using Geekbot.net.Lib.Logger; using Geekbot.net.Lib.RandomNumberGenerator; @@ -9,14 +10,14 @@ namespace Geekbot.net.Lib.Media { private readonly IRandomNumberGenerator _random; private readonly IGeekbotLogger _logger; - private string[] _pandaImages; - private string[] _croissantImages; - private string[] _squirrelImages; - private string[] _pumpkinImages; - private string[] _turtlesImages; - private string[] _penguinImages; - private string[] _foxImages; - private string[] _dabImages; + private readonly string[] _pandaImages; + private readonly string[] _croissantImages; + private readonly string[] _squirrelImages; + private readonly string[] _pumpkinImages; + private readonly string[] _turtlesImages; + private readonly string[] _penguinImages; + private readonly string[] _foxImages; + private readonly string[] _dabImages; public MediaProvider(IGeekbotLogger logger, IRandomNumberGenerator random) { @@ -24,7 +25,6 @@ namespace Geekbot.net.Lib.Media _logger = logger; logger.Information(LogSource.Geekbot, "Loading Media Files"); -; LoadMedia("./Storage/pandas", ref _pandaImages); LoadMedia("./Storage/croissant", ref _croissantImages); LoadMedia("./Storage/squirrel", ref _squirrelImages); @@ -53,7 +53,8 @@ namespace Geekbot.net.Lib.Media MediaType.Turtle => _turtlesImages, MediaType.Penguin => _penguinImages, MediaType.Fox => _foxImages, - MediaType.Dab => _dabImages + MediaType.Dab => _dabImages, + _ => new string[0] }; return collection[_random.Next(0, collection.Length)]; From 4659f793f55b689ad5df83987007a7827eccd9e5 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 22 Jul 2020 14:32:47 +0200 Subject: [PATCH 254/443] Add !lmgtfy --- Geekbot.net/Commands/Utils/Lmgtfy.cs | 33 ++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Geekbot.net/Commands/Utils/Lmgtfy.cs diff --git a/Geekbot.net/Commands/Utils/Lmgtfy.cs b/Geekbot.net/Commands/Utils/Lmgtfy.cs new file mode 100644 index 0000000..9976dad --- /dev/null +++ b/Geekbot.net/Commands/Utils/Lmgtfy.cs @@ -0,0 +1,33 @@ +using System; +using System.Threading.Tasks; +using System.Web; +using Discord.Commands; +using Geekbot.net.Lib.ErrorHandling; + +namespace Geekbot.net.Commands.Utils +{ + public class Lmgtfy : ModuleBase + { + private readonly IErrorHandler _errorHandler; + + public Lmgtfy(IErrorHandler errorHandler) + { + _errorHandler = errorHandler; + } + + [Command("lmgtfy", RunMode = RunMode.Async)] + [Summary("Get a 'Let me google that for you' link")] + public async Task GetUrl([Remainder] [Summary("question")] string question) + { + try + { + var encoded = HttpUtility.UrlEncode(question).Replace("%20", "+"); + await Context.Channel.SendMessageAsync($""); + } + catch (Exception e) + { + await _errorHandler.HandleCommandException(e, Context); + } + } + } +} \ No newline at end of file From 913b4a5f10c4dbbb62932d312e35c28839e721c5 Mon Sep 17 00:00:00 2001 From: runebaas Date: Tue, 28 Jul 2020 22:14:22 +0200 Subject: [PATCH 255/443] Change the HttpAbstractions so that the caller can provide its own HttpClient instead of creating parameters for every HttpClient Option --- .../Commands/Utils/Changelog/Changelog.cs | 8 +--- Geekbot.net/Lib/HttpAbstractions.cs | 39 ++++++++++--------- 2 files changed, 21 insertions(+), 26 deletions(-) diff --git a/Geekbot.net/Commands/Utils/Changelog/Changelog.cs b/Geekbot.net/Commands/Utils/Changelog/Changelog.cs index 335ee3f..c217619 100644 --- a/Geekbot.net/Commands/Utils/Changelog/Changelog.cs +++ b/Geekbot.net/Commands/Utils/Changelog/Changelog.cs @@ -28,13 +28,7 @@ namespace Geekbot.net.Commands.Utils.Changelog { try { - var commits = await HttpAbstractions.Get>( - new Uri("https://api.github.com/repos/pizzaandcoffee/geekbot.net/commits"), - new Dictionary() - { - { "User-Agent", "http://developer.github.com/v3/#user-agent-required" } - } - ); + var commits = await HttpAbstractions.Get>(new Uri("https://api.github.com/repos/pizzaandcoffee/geekbot.net/commits")); var eb = new EmbedBuilder(); eb.WithColor(new Color(143, 165, 102)); diff --git a/Geekbot.net/Lib/HttpAbstractions.cs b/Geekbot.net/Lib/HttpAbstractions.cs index cf609f6..2cde4cc 100644 --- a/Geekbot.net/Lib/HttpAbstractions.cs +++ b/Geekbot.net/Lib/HttpAbstractions.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; @@ -7,34 +6,36 @@ using Newtonsoft.Json; namespace Geekbot.net.Lib { - public class HttpAbstractions + public static class HttpAbstractions { - public static async Task Get(Uri location, Dictionary additionalHeaders = null) + public static HttpClient CreateDefaultClient() { - using var client = new HttpClient + var client = new HttpClient { - BaseAddress = location, DefaultRequestHeaders = { - Accept = - { - MediaTypeWithQualityHeaderValue.Parse("application/json") - } + Accept = {MediaTypeWithQualityHeaderValue.Parse("application/json")}, } }; + client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Geekbot/v0.0.0 (+https://geekbot.pizzaandcoffee.rocks/)"); - if (additionalHeaders != null) - { - foreach (var (name, val) in additionalHeaders) - { - client.DefaultRequestHeaders.TryAddWithoutValidation(name, val); - } - } - - var response = await client.GetAsync(location.PathAndQuery); + return client; + } + + public static async Task Get(Uri location, HttpClient httpClient = null, bool disposeClient = true) + { + httpClient ??= CreateDefaultClient(); + httpClient.BaseAddress = location; + + var response = await httpClient.GetAsync(location.PathAndQuery); response.EnsureSuccessStatusCode(); - var stringResponse = await response.Content.ReadAsStringAsync(); + + if (disposeClient) + { + httpClient.Dispose(); + } + return JsonConvert.DeserializeObject(stringResponse); } } From 9003d6249eb62e32302c4b38cf35b42905674cfb Mon Sep 17 00:00:00 2001 From: runebaas Date: Tue, 28 Jul 2020 22:14:59 +0200 Subject: [PATCH 256/443] Add !mmr to get League of Legends MMR numbers --- .../Commands/Integrations/LolMmr/LolMmr.cs | 61 +++++++++++++++++++ .../Commands/Integrations/LolMmr/LolMmrDto.cs | 9 +++ .../Integrations/LolMmr/LolMrrInfoDto.cs | 11 ++++ 3 files changed, 81 insertions(+) create mode 100644 Geekbot.net/Commands/Integrations/LolMmr/LolMmr.cs create mode 100644 Geekbot.net/Commands/Integrations/LolMmr/LolMmrDto.cs create mode 100644 Geekbot.net/Commands/Integrations/LolMmr/LolMrrInfoDto.cs diff --git a/Geekbot.net/Commands/Integrations/LolMmr/LolMmr.cs b/Geekbot.net/Commands/Integrations/LolMmr/LolMmr.cs new file mode 100644 index 0000000..5de7d63 --- /dev/null +++ b/Geekbot.net/Commands/Integrations/LolMmr/LolMmr.cs @@ -0,0 +1,61 @@ +using System; +using System.Net; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using System.Web; +using Discord.Commands; +using Geekbot.net.Lib; +using Geekbot.net.Lib.ErrorHandling; + +namespace Geekbot.net.Commands.Integrations.LolMmr +{ + public class LolMmr : ModuleBase + { + private readonly IErrorHandler _errorHandler; + + public LolMmr(IErrorHandler errorHandler) + { + _errorHandler = errorHandler; + } + + [Command("mmr", RunMode = RunMode.Async)] + [Summary("Get the League of Legends MMR for a specified summoner")] + public async Task GetMmr([Remainder] [Summary("summoner")] string summonerName) + { + try + { + LolMmrDto data; + try + { + var name = HttpUtility.UrlEncode(summonerName.ToLower()); + var httpClient = HttpAbstractions.CreateDefaultClient(); + // setting the user agent in accordance with the whatismymmr.com api rules + httpClient.DefaultRequestHeaders.Remove("User-Agent"); + httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Linux:rocks.pizzaandcoffee.geekbot:v0.0.0"); + data = await HttpAbstractions.Get(new Uri($"https://euw.whatismymmr.com/api/v1/summoner?name={name}"), httpClient); + } + catch (HttpRequestException e) + { + if (e.StatusCode != HttpStatusCode.NotFound) throw e; + + await Context.Channel.SendMessageAsync("Player not found"); + return; + + } + + var sb = new StringBuilder(); + sb.AppendLine($"**MMR for {summonerName}**"); + sb.AppendLine($"Normal: {data.Normal.Avg}"); + sb.AppendLine($"Ranked: {data.Ranked.Avg}"); + sb.AppendLine($"ARAM: {data.ARAM.Avg}"); + + await Context.Channel.SendMessageAsync(sb.ToString()); + } + catch (Exception e) + { + await _errorHandler.HandleCommandException(e, Context); + } + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/Integrations/LolMmr/LolMmrDto.cs b/Geekbot.net/Commands/Integrations/LolMmr/LolMmrDto.cs new file mode 100644 index 0000000..4a9887d --- /dev/null +++ b/Geekbot.net/Commands/Integrations/LolMmr/LolMmrDto.cs @@ -0,0 +1,9 @@ +namespace Geekbot.net.Commands.Integrations.LolMmr +{ + public class LolMmrDto + { + public LolMrrInfoDto Ranked { get; set; } + public LolMrrInfoDto Normal { get; set; } + public LolMrrInfoDto ARAM { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/Integrations/LolMmr/LolMrrInfoDto.cs b/Geekbot.net/Commands/Integrations/LolMmr/LolMrrInfoDto.cs new file mode 100644 index 0000000..55804fd --- /dev/null +++ b/Geekbot.net/Commands/Integrations/LolMmr/LolMrrInfoDto.cs @@ -0,0 +1,11 @@ +using System; +using Newtonsoft.Json; + +namespace Geekbot.net.Commands.Integrations.LolMmr +{ + public class LolMrrInfoDto + { + [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] + public decimal Avg { get; set; } = 0; + } +} \ No newline at end of file From 3813290f89ab7c2c9558318ee375fde18a16ca98 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 8 Aug 2020 20:53:50 +0200 Subject: [PATCH 257/443] Simplefy version number during in CI pipeline --- .gitlab-ci.yml | 15 +++++++-------- Geekbot.net/Geekbot.net.csproj | 5 ++--- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 2e22e02..3d362ce 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -4,7 +4,9 @@ stages: - deploy - ops -.imageTag: &IMAGE_TAG $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG +variables: + VERSION: 4.2.0-$CI_COMMIT_SHORT_SHA + IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG Build: stage: build @@ -16,7 +18,7 @@ Build: script: - dotnet restore - dotnet test Tests - - dotnet publish --version-suffix ${CI_COMMIT_SHA:0:8} -r linux-x64 -c Release -o ./app ./Geekbot.net/ + - dotnet publish --version-suffix $VERSION -r linux-x64 -c Release -o ./app ./Geekbot.net/ Package: stage: docker @@ -25,8 +27,6 @@ Package: - master services: - docker:stable-dind - variables: - IMAGE_TAG: *IMAGE_TAG script: - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY - docker build -t $IMAGE_TAG . @@ -39,7 +39,6 @@ Deploy: - master variables: ANSIBLE_NOCOWS: 1 - IMAGE_TAG: *IMAGE_TAG before_script: - mkdir /root/.ssh - cp $SSH_PRIVATE_KEY /root/.ssh/id_ed25519 @@ -56,9 +55,9 @@ Sentry: only: - master script: - - sentry-cli releases new -p geekbot 4.2.0-${CI_COMMIT_SHA:0:8} - - sentry-cli releases set-commits --auto 4.2.0-${CI_COMMIT_SHA:0:8} - - sentry-cli releases deploys 4.2.0-${CI_COMMIT_SHA:0:8} new -e Production + - sentry-cli releases new -p geekbot $VERSION + - sentry-cli releases set-commits --auto $VERSION + - sentry-cli releases deploys $VERSION new -e Production Github Mirror: stage: ops diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj index 37dc310..006e6cb 100755 --- a/Geekbot.net/Geekbot.net.csproj +++ b/Geekbot.net/Geekbot.net.csproj @@ -4,10 +4,9 @@ net5.0 win-x64;linux-x64 derp.ico - 4.2.0 $(VersionSuffix) - $(Version)-$(VersionSuffix) - $(Version)-DEV + $(VersionSuffix) + 0.0.0-DEV Pizza and Coffee Studios Pizza and Coffee Studios A Discord bot From c22d0cf9419c47dd9ccde2eb4a7245b233633644 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 8 Aug 2020 21:09:27 +0200 Subject: [PATCH 258/443] Merge WikipediaApi into the main code base --- Geekbot.net.sln | 6 ------ Geekbot.net/Commands/Integrations/Wikipedia.cs | 4 ++-- .../Lib/WikipediaClient}/IWikipediaClient.cs | 4 ++-- .../Lib/WikipediaClient}/Page/PageApiUrls.cs | 2 +- .../Lib/WikipediaClient}/Page/PageContentUrlCollection.cs | 2 +- .../Lib/WikipediaClient}/Page/PageContentUrls.cs | 2 +- .../Lib/WikipediaClient}/Page/PageCoordinates.cs | 2 +- .../Lib/WikipediaClient}/Page/PageImage.cs | 2 +- .../Lib/WikipediaClient}/Page/PageNamespace.cs | 2 +- .../Lib/WikipediaClient}/Page/PagePreview.cs | 2 +- .../Lib/WikipediaClient}/Page/PageTitles.cs | 2 +- .../Lib/WikipediaClient}/Page/PageTypes.cs | 2 +- .../Lib/WikipediaClient}/WikipediaClient.cs | 4 ++-- Geekbot.net/Program.cs | 2 +- WikipediaApi/WikipediaApi.csproj | 8 -------- 15 files changed, 16 insertions(+), 30 deletions(-) rename {WikipediaApi => Geekbot.net/Lib/WikipediaClient}/IWikipediaClient.cs (63%) rename {WikipediaApi => Geekbot.net/Lib/WikipediaClient}/Page/PageApiUrls.cs (84%) rename {WikipediaApi => Geekbot.net/Lib/WikipediaClient}/Page/PageContentUrlCollection.cs (73%) rename {WikipediaApi => Geekbot.net/Lib/WikipediaClient}/Page/PageContentUrls.cs (79%) rename {WikipediaApi => Geekbot.net/Lib/WikipediaClient}/Page/PageCoordinates.cs (68%) rename {WikipediaApi => Geekbot.net/Lib/WikipediaClient}/Page/PageImage.cs (76%) rename {WikipediaApi => Geekbot.net/Lib/WikipediaClient}/Page/PageNamespace.cs (68%) rename {WikipediaApi => Geekbot.net/Lib/WikipediaClient}/Page/PagePreview.cs (94%) rename {WikipediaApi => Geekbot.net/Lib/WikipediaClient}/Page/PageTitles.cs (76%) rename {WikipediaApi => Geekbot.net/Lib/WikipediaClient}/Page/PageTypes.cs (84%) rename {WikipediaApi => Geekbot.net/Lib/WikipediaClient}/WikipediaClient.cs (87%) delete mode 100644 WikipediaApi/WikipediaApi.csproj diff --git a/Geekbot.net.sln b/Geekbot.net.sln index b542f25..5011ad4 100644 --- a/Geekbot.net.sln +++ b/Geekbot.net.sln @@ -7,8 +7,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Geekbot.net", "Geekbot.net/ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{4CAF5F02-EFFE-4FDA-BD44-EEADDBA9600E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WikipediaApi", "WikipediaApi\WikipediaApi.csproj", "{1084D499-EF94-4834-9E6A-B2AD81B60078}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -23,10 +21,6 @@ Global {4CAF5F02-EFFE-4FDA-BD44-EEADDBA9600E}.Debug|Any CPU.Build.0 = Debug|Any CPU {4CAF5F02-EFFE-4FDA-BD44-EEADDBA9600E}.Release|Any CPU.ActiveCfg = Release|Any CPU {4CAF5F02-EFFE-4FDA-BD44-EEADDBA9600E}.Release|Any CPU.Build.0 = Release|Any CPU - {1084D499-EF94-4834-9E6A-B2AD81B60078}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {1084D499-EF94-4834-9E6A-B2AD81B60078}.Debug|Any CPU.Build.0 = Debug|Any CPU - {1084D499-EF94-4834-9E6A-B2AD81B60078}.Release|Any CPU.ActiveCfg = Release|Any CPU - {1084D499-EF94-4834-9E6A-B2AD81B60078}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Geekbot.net/Commands/Integrations/Wikipedia.cs b/Geekbot.net/Commands/Integrations/Wikipedia.cs index 9124670..93f68fd 100644 --- a/Geekbot.net/Commands/Integrations/Wikipedia.cs +++ b/Geekbot.net/Commands/Integrations/Wikipedia.cs @@ -8,9 +8,9 @@ using Discord.Commands; using Geekbot.net.Database; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; +using Geekbot.net.Lib.WikipediaClient; +using Geekbot.net.Lib.WikipediaClient.Page; using HtmlAgilityPack; -using WikipediaApi; -using WikipediaApi.Page; namespace Geekbot.net.Commands.Integrations { diff --git a/WikipediaApi/IWikipediaClient.cs b/Geekbot.net/Lib/WikipediaClient/IWikipediaClient.cs similarity index 63% rename from WikipediaApi/IWikipediaClient.cs rename to Geekbot.net/Lib/WikipediaClient/IWikipediaClient.cs index 4d1dae9..c6cdf3d 100644 --- a/WikipediaApi/IWikipediaClient.cs +++ b/Geekbot.net/Lib/WikipediaClient/IWikipediaClient.cs @@ -1,7 +1,7 @@ using System.Threading.Tasks; -using WikipediaApi.Page; +using Geekbot.net.Lib.WikipediaClient.Page; -namespace WikipediaApi +namespace Geekbot.net.Lib.WikipediaClient { public interface IWikipediaClient { diff --git a/WikipediaApi/Page/PageApiUrls.cs b/Geekbot.net/Lib/WikipediaClient/Page/PageApiUrls.cs similarity index 84% rename from WikipediaApi/Page/PageApiUrls.cs rename to Geekbot.net/Lib/WikipediaClient/Page/PageApiUrls.cs index 8a121be..803f4d7 100644 --- a/WikipediaApi/Page/PageApiUrls.cs +++ b/Geekbot.net/Lib/WikipediaClient/Page/PageApiUrls.cs @@ -1,6 +1,6 @@ using System; -namespace WikipediaApi.Page +namespace Geekbot.net.Lib.WikipediaClient.Page { public class PageApiUrls { diff --git a/WikipediaApi/Page/PageContentUrlCollection.cs b/Geekbot.net/Lib/WikipediaClient/Page/PageContentUrlCollection.cs similarity index 73% rename from WikipediaApi/Page/PageContentUrlCollection.cs rename to Geekbot.net/Lib/WikipediaClient/Page/PageContentUrlCollection.cs index f6c680b..614aaa7 100644 --- a/WikipediaApi/Page/PageContentUrlCollection.cs +++ b/Geekbot.net/Lib/WikipediaClient/Page/PageContentUrlCollection.cs @@ -1,4 +1,4 @@ -namespace WikipediaApi.Page +namespace Geekbot.net.Lib.WikipediaClient.Page { public class PageContentUrlCollection { diff --git a/WikipediaApi/Page/PageContentUrls.cs b/Geekbot.net/Lib/WikipediaClient/Page/PageContentUrls.cs similarity index 79% rename from WikipediaApi/Page/PageContentUrls.cs rename to Geekbot.net/Lib/WikipediaClient/Page/PageContentUrls.cs index 64a80dc..faf7bb5 100644 --- a/WikipediaApi/Page/PageContentUrls.cs +++ b/Geekbot.net/Lib/WikipediaClient/Page/PageContentUrls.cs @@ -1,6 +1,6 @@ using System; -namespace WikipediaApi.Page +namespace Geekbot.net.Lib.WikipediaClient.Page { public class PageContentUrls { diff --git a/WikipediaApi/Page/PageCoordinates.cs b/Geekbot.net/Lib/WikipediaClient/Page/PageCoordinates.cs similarity index 68% rename from WikipediaApi/Page/PageCoordinates.cs rename to Geekbot.net/Lib/WikipediaClient/Page/PageCoordinates.cs index 7fa42ba..ce67f9b 100644 --- a/WikipediaApi/Page/PageCoordinates.cs +++ b/Geekbot.net/Lib/WikipediaClient/Page/PageCoordinates.cs @@ -1,4 +1,4 @@ -namespace WikipediaApi.Page +namespace Geekbot.net.Lib.WikipediaClient.Page { public class PageCoordinates { diff --git a/WikipediaApi/Page/PageImage.cs b/Geekbot.net/Lib/WikipediaClient/Page/PageImage.cs similarity index 76% rename from WikipediaApi/Page/PageImage.cs rename to Geekbot.net/Lib/WikipediaClient/Page/PageImage.cs index 4a8b28d..cdcc2a9 100644 --- a/WikipediaApi/Page/PageImage.cs +++ b/Geekbot.net/Lib/WikipediaClient/Page/PageImage.cs @@ -1,6 +1,6 @@ using System; -namespace WikipediaApi.Page +namespace Geekbot.net.Lib.WikipediaClient.Page { public class PageImage { diff --git a/WikipediaApi/Page/PageNamespace.cs b/Geekbot.net/Lib/WikipediaClient/Page/PageNamespace.cs similarity index 68% rename from WikipediaApi/Page/PageNamespace.cs rename to Geekbot.net/Lib/WikipediaClient/Page/PageNamespace.cs index 66600b6..798e6b9 100644 --- a/WikipediaApi/Page/PageNamespace.cs +++ b/Geekbot.net/Lib/WikipediaClient/Page/PageNamespace.cs @@ -1,4 +1,4 @@ -namespace WikipediaApi.Page +namespace Geekbot.net.Lib.WikipediaClient.Page { public class PageNamespace { diff --git a/WikipediaApi/Page/PagePreview.cs b/Geekbot.net/Lib/WikipediaClient/Page/PagePreview.cs similarity index 94% rename from WikipediaApi/Page/PagePreview.cs rename to Geekbot.net/Lib/WikipediaClient/Page/PagePreview.cs index 8db9dad..37434d3 100644 --- a/WikipediaApi/Page/PagePreview.cs +++ b/Geekbot.net/Lib/WikipediaClient/Page/PagePreview.cs @@ -2,7 +2,7 @@ using Newtonsoft.Json; using Newtonsoft.Json.Converters; -namespace WikipediaApi.Page +namespace Geekbot.net.Lib.WikipediaClient.Page { public class PagePreview { diff --git a/WikipediaApi/Page/PageTitles.cs b/Geekbot.net/Lib/WikipediaClient/Page/PageTitles.cs similarity index 76% rename from WikipediaApi/Page/PageTitles.cs rename to Geekbot.net/Lib/WikipediaClient/Page/PageTitles.cs index 31a55b9..dcc2cea 100644 --- a/WikipediaApi/Page/PageTitles.cs +++ b/Geekbot.net/Lib/WikipediaClient/Page/PageTitles.cs @@ -1,4 +1,4 @@ -namespace WikipediaApi.Page +namespace Geekbot.net.Lib.WikipediaClient.Page { public class PageTitles { diff --git a/WikipediaApi/Page/PageTypes.cs b/Geekbot.net/Lib/WikipediaClient/Page/PageTypes.cs similarity index 84% rename from WikipediaApi/Page/PageTypes.cs rename to Geekbot.net/Lib/WikipediaClient/Page/PageTypes.cs index a415d75..63cd4d2 100644 --- a/WikipediaApi/Page/PageTypes.cs +++ b/Geekbot.net/Lib/WikipediaClient/Page/PageTypes.cs @@ -1,6 +1,6 @@ using System.Runtime.Serialization; -namespace WikipediaApi.Page +namespace Geekbot.net.Lib.WikipediaClient.Page { public enum PageTypes { diff --git a/WikipediaApi/WikipediaClient.cs b/Geekbot.net/Lib/WikipediaClient/WikipediaClient.cs similarity index 87% rename from WikipediaApi/WikipediaClient.cs rename to Geekbot.net/Lib/WikipediaClient/WikipediaClient.cs index 6576f3d..ed4902a 100644 --- a/WikipediaApi/WikipediaClient.cs +++ b/Geekbot.net/Lib/WikipediaClient/WikipediaClient.cs @@ -1,9 +1,9 @@ using System.Net.Http; using System.Threading.Tasks; +using Geekbot.net.Lib.WikipediaClient.Page; using Newtonsoft.Json; -using WikipediaApi.Page; -namespace WikipediaApi +namespace Geekbot.net.Lib.WikipediaClient { public class WikipediaClient : IWikipediaClient { diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 689c170..5876c4a 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -24,10 +24,10 @@ using Geekbot.net.Lib.Media; using Geekbot.net.Lib.RandomNumberGenerator; using Geekbot.net.Lib.ReactionListener; using Geekbot.net.Lib.UserRepository; +using Geekbot.net.Lib.WikipediaClient; using Geekbot.net.WebApi; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; -using WikipediaApi; namespace Geekbot.net { diff --git a/WikipediaApi/WikipediaApi.csproj b/WikipediaApi/WikipediaApi.csproj deleted file mode 100644 index e53e3c2..0000000 --- a/WikipediaApi/WikipediaApi.csproj +++ /dev/null @@ -1,8 +0,0 @@ - - - net5.0 - - - - - \ No newline at end of file From 7b6dd2d2f907bb770a8c7a0361ae598c9ab47c32 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 8 Aug 2020 21:17:02 +0200 Subject: [PATCH 259/443] Rename the clients namescape to MalClient, because that was the only thing in there, while other clients got their own namespaces --- Geekbot.net/Commands/Integrations/Mal.cs | 2 +- Geekbot.net/Lib/{Clients => MalClient}/IMalClient.cs | 2 +- Geekbot.net/Lib/{Clients => MalClient}/MalClient.cs | 2 +- Geekbot.net/Program.cs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename Geekbot.net/Lib/{Clients => MalClient}/IMalClient.cs (83%) rename Geekbot.net/Lib/{Clients => MalClient}/MalClient.cs (95%) diff --git a/Geekbot.net/Commands/Integrations/Mal.cs b/Geekbot.net/Commands/Integrations/Mal.cs index ecc91bf..f8aeb95 100644 --- a/Geekbot.net/Commands/Integrations/Mal.cs +++ b/Geekbot.net/Commands/Integrations/Mal.cs @@ -4,9 +4,9 @@ using System.Web; using System.Xml; using Discord; using Discord.Commands; -using Geekbot.net.Lib.Clients; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; +using Geekbot.net.Lib.MalClient; namespace Geekbot.net.Commands.Integrations { diff --git a/Geekbot.net/Lib/Clients/IMalClient.cs b/Geekbot.net/Lib/MalClient/IMalClient.cs similarity index 83% rename from Geekbot.net/Lib/Clients/IMalClient.cs rename to Geekbot.net/Lib/MalClient/IMalClient.cs index f59c511..6aaab05 100644 --- a/Geekbot.net/Lib/Clients/IMalClient.cs +++ b/Geekbot.net/Lib/MalClient/IMalClient.cs @@ -1,7 +1,7 @@ using System.Threading.Tasks; using MyAnimeListSharp.Core; -namespace Geekbot.net.Lib.Clients +namespace Geekbot.net.Lib.MalClient { public interface IMalClient { diff --git a/Geekbot.net/Lib/Clients/MalClient.cs b/Geekbot.net/Lib/MalClient/MalClient.cs similarity index 95% rename from Geekbot.net/Lib/Clients/MalClient.cs rename to Geekbot.net/Lib/MalClient/MalClient.cs index 8e5d950..3f121ea 100644 --- a/Geekbot.net/Lib/Clients/MalClient.cs +++ b/Geekbot.net/Lib/MalClient/MalClient.cs @@ -5,7 +5,7 @@ using MyAnimeListSharp.Auth; using MyAnimeListSharp.Core; using MyAnimeListSharp.Facade.Async; -namespace Geekbot.net.Lib.Clients +namespace Geekbot.net.Lib.MalClient { public class MalClient : IMalClient { diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 5876c4a..c55f390 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -9,7 +9,6 @@ using Discord.WebSocket; using Geekbot.net.Database; using Geekbot.net.Handlers; using Geekbot.net.Lib; -using Geekbot.net.Lib.Clients; using Geekbot.net.Lib.Converters; using Geekbot.net.Lib.DiceParser; using Geekbot.net.Lib.ErrorHandling; @@ -20,6 +19,7 @@ using Geekbot.net.Lib.KvInMemoryStore; using Geekbot.net.Lib.Levels; using Geekbot.net.Lib.Localization; using Geekbot.net.Lib.Logger; +using Geekbot.net.Lib.MalClient; using Geekbot.net.Lib.Media; using Geekbot.net.Lib.RandomNumberGenerator; using Geekbot.net.Lib.ReactionListener; From fc0af492addf933ae08aec4f953110e4be6bcad1 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 8 Aug 2020 22:24:01 +0200 Subject: [PATCH 260/443] Split Geekbot.net into src/Bot, src/Core, and src/Web --- .gitignore | 14 ++-- Geekbot.net.sln | 24 +++++-- Geekbot.net/Geekbot.net.csproj | 64 ------------------ .../Converters/EmojiConverter.test.cs | 4 +- .../DiceParser/DiceParser.test.cs | 9 ++- .../DiceParser/SingleDie.test.cs | 6 +- Tests/{Lib => Core}/Levels/LevelCalc.test.cs | 5 +- .../TranslationGuildContext.test.cs | 4 +- .../Localization/Translations.test.cs | 4 +- Tests/Tests.csproj | 2 +- src/Bot/Bot.csproj | 43 ++++++++++++ .../Bot}/Commands/Admin/Admin.cs | 12 ++-- .../Bot}/Commands/Admin/Mod.cs | 6 +- .../Bot}/Commands/Admin/Owner/Owner.cs | 10 +-- .../Bot}/Commands/Admin/Role.cs | 16 ++--- .../Bot}/Commands/Games/Pokedex.cs | 6 +- .../Bot}/Commands/Games/Roll/Roll.cs | 16 ++--- .../Bot}/Commands/Games/Roll/RollTimeout.cs | 2 +- .../Commands/Integrations/LolMmr/LolMmr.cs | 6 +- .../Commands/Integrations/LolMmr/LolMmrDto.cs | 2 +- .../Integrations/LolMmr/LolMrrInfoDto.cs | 3 +- .../Integrations/MagicTheGathering.cs | 8 +-- .../Bot}/Commands/Integrations/Mal.cs | 8 +-- .../UbranDictionary/UrbanDictListItemDto.cs | 2 +- .../UbranDictionary/UrbanDictResponseDto.cs | 2 +- .../UbranDictionary/UrbanDictionary.cs | 8 +-- .../Bot}/Commands/Integrations/Wikipedia.cs | 12 ++-- .../Bot}/Commands/Integrations/Youtube.cs | 6 +- .../BenedictCumberbatchNameGenerator.cs | 6 +- .../Bot}/Commands/Randomness/Cat/Cat.cs | 6 +- .../Commands/Randomness/Cat/CatResponseDto.cs | 2 +- .../Chuck/ChuckNorrisJokeResponseDto.cs | 2 +- .../Randomness/Chuck/ChuckNorrisJokes.cs | 6 +- .../Randomness/Dad/DadJokeResponseDto.cs | 2 +- .../Bot}/Commands/Randomness/Dad/DadJokes.cs | 6 +- .../Bot}/Commands/Randomness/Dog/Dog.cs | 6 +- .../Commands/Randomness/Dog/DogResponseDto.cs | 2 +- .../Bot}/Commands/Randomness/EightBall.cs | 4 +- .../Bot}/Commands/Randomness/Fortune.cs | 4 +- .../Bot}/Commands/Randomness/Gdq.cs | 4 +- .../Randomness/Greetings/GreetingBaseDto.cs | 2 +- .../Randomness/Greetings/GreetingDto.cs | 2 +- .../Randomness/Greetings/Greetings.cs | 8 +-- .../Bot}/Commands/Randomness/Kanye/Kanye.cs | 6 +- .../Randomness/Kanye/KanyeResponseDto.cs | 2 +- .../Bot}/Commands/Randomness/RandomAnimals.cs | 4 +- .../Bot}/Commands/Randomness/Ship.cs | 14 ++-- .../Bot}/Commands/Randomness/Slap.cs | 10 +-- .../Bot}/Commands/Rpg/Cookies.cs | 16 ++--- .../Bot}/Commands/User/GuildInfo.cs | 12 ++-- .../Bot}/Commands/User/Karma.cs | 14 ++-- .../Bot}/Commands/User/Ranking/Rank.cs | 18 ++--- .../Bot}/Commands/User/Stats.cs | 12 ++-- .../Bot}/Commands/Utils/AvatarGetter.cs | 4 +- .../Commands/Utils/Changelog/Changelog.cs | 6 +- .../Utils/Changelog/CommitAuthorDto.cs | 2 +- .../Commands/Utils/Changelog/CommitDto.cs | 2 +- .../Commands/Utils/Changelog/CommitInfoDto.cs | 2 +- .../Bot}/Commands/Utils/Choose.cs | 6 +- .../Bot}/Commands/Utils/Corona/CoronaStats.cs | 9 ++- .../Commands/Utils/Corona/CoronaSummaryDto.cs | 2 +- .../Bot}/Commands/Utils/Dice.cs | 6 +- .../Bot}/Commands/Utils/Emojify.cs | 6 +- .../Bot}/Commands/Utils/Help.cs | 4 +- .../Bot}/Commands/Utils/Info.cs | 8 +-- .../Bot}/Commands/Utils/Lmgtfy.cs | 4 +- .../Bot}/Commands/Utils/Ping.cs | 2 +- .../Bot}/Commands/Utils/Quote/Quote.cs | 18 ++--- .../Commands/Utils/Quote/QuoteObjectDto.cs | 2 +- .../Bot}/Handlers/CommandHandler.cs | 10 +-- .../Bot}/Handlers/MessageDeletedHandler.cs | 8 +-- .../Bot}/Handlers/ReactionHandler.cs | 4 +- .../Bot}/Handlers/StatsHandler.cs | 10 +-- .../Bot}/Handlers/UserHandler.cs | 10 +-- {Geekbot.net => src/Bot}/Logs/.keep | 0 {Geekbot.net => src/Bot}/Program.cs | 42 ++++++------ {Geekbot.net => src/Bot}/Storage/croissant | 0 {Geekbot.net => src/Bot}/Storage/dab | 0 {Geekbot.net => src/Bot}/Storage/fortunes | 0 {Geekbot.net => src/Bot}/Storage/foxes | 0 {Geekbot.net => src/Bot}/Storage/pandas | 0 {Geekbot.net => src/Bot}/Storage/penguins | 0 {Geekbot.net => src/Bot}/Storage/pumpkin | 0 {Geekbot.net => src/Bot}/Storage/squirrel | 0 {Geekbot.net => src/Bot}/Storage/turtles | 0 {Geekbot.net => src/Bot}/derp.ico | Bin .../DisableInDirectMessageAttribute.cs | 2 +- {Geekbot.net/Lib => src/Core}/Constants.cs | 4 +- .../Core}/Converters/EmojiConverter.cs | 2 +- .../Core}/Converters/IEmojiConverter.cs | 2 +- .../Core}/Converters/IMtgManaConverter.cs | 2 +- .../Core}/Converters/MtgManaConverter.cs | 2 +- src/Core/Core.csproj | 40 +++++++++++ .../Core}/Database/DatabaseContext.cs | 4 +- .../Core}/Database/DatabaseInitializer.cs | 7 +- .../Core}/Database/InMemoryDatabase.cs | 2 +- .../LoggingAdapter/NpgsqlLoggingAdapter.cs | 5 +- .../NpgsqlLoggingProviderAdapter.cs | 5 +- .../Core}/Database/Models/CookiesModel.cs | 2 +- .../Core}/Database/Models/GlobalsModel.cs | 2 +- .../Database/Models/GuildSettingsModel.cs | 2 +- .../Core}/Database/Models/KarmaModel.cs | 2 +- .../Core}/Database/Models/MessagesModel.cs | 2 +- .../Core}/Database/Models/QuoteModel.cs | 2 +- .../Database/Models/ReactionListenerModel.cs | 2 +- .../Database/Models/RoleSelfServiceModel.cs | 2 +- .../Core}/Database/Models/RollsModel.cs | 2 +- .../Core}/Database/Models/ShipsModel.cs | 2 +- .../Core}/Database/Models/SlapsModel.cs | 2 +- .../Core}/Database/Models/UserModel.cs | 3 +- .../Core}/Database/SqlConnectionString.cs | 2 +- .../Core}/Database/SqlDatabase.cs | 2 +- .../Core}/DiceParser/DiceException.cs | 2 +- .../Lib => src/Core}/DiceParser/DiceInput.cs | 2 +- .../Core}/DiceParser/DiceInputOptions.cs | 2 +- .../Lib => src/Core}/DiceParser/DiceParser.cs | 4 +- .../Core}/DiceParser/DieAdvantageType.cs | 2 +- .../Lib => src/Core}/DiceParser/DieResult.cs | 2 +- .../Core}/DiceParser/IDiceParser.cs | 2 +- .../Lib => src/Core}/DiceParser/SingleDie.cs | 6 +- .../Core}/ErrorHandling/ErrorHandler.cs | 6 +- .../Core}/ErrorHandling/IErrorHandler.cs | 2 +- .../Core}/Extensions/DbSetExtensions.cs | 2 +- .../Extensions/EmbedBuilderExtensions.cs | 2 +- .../Core}/Extensions/IntExtensions.cs | 2 +- .../Core}/Extensions/LongExtensions.cs | 2 +- .../Core}/Extensions/StringExtensions.cs | 2 +- .../Core}/Extensions/UlongExtensions.cs | 2 +- .../Lib => src/Core}/GeekbotExitCode.cs | 2 +- .../Core}/GlobalSettings/GlobalSettings.cs | 6 +- .../Core}/GlobalSettings/IGlobalSettings.cs | 4 +- .../GuildSettingsManager.cs | 8 +-- .../IGuildSettingsManager.cs | 4 +- .../Highscores/HighscoreListEmptyException.cs | 2 +- .../Core}/Highscores/HighscoreManager.cs | 8 +-- .../Core}/Highscores/HighscoreTypes.cs | 2 +- .../Core}/Highscores/HighscoreUserDto.cs | 2 +- .../Core}/Highscores/IHighscoreManager.cs | 2 +- .../Lib => src/Core}/HttpAbstractions.cs | 2 +- .../Core}/KvInMemoryStore/IKvInMemoryStore.cs | 2 +- .../Core}/KvInMemoryStore/KvInMemoryStore.cs | 2 +- .../Lib => src/Core}/Levels/ILevelCalc.cs | 2 +- .../Lib => src/Core}/Levels/LevelCalc.cs | 2 +- .../Core}/Localization/ITranslationHandler.cs | 2 +- .../Localization/TranslationGuildContext.cs | 2 +- .../Core}/Localization/TranslationHandler.cs | 6 +- .../Core}/Localization/Translations.yml | 0 .../Lib => src/Core}/Logger/DiscordLogger.cs | 2 +- .../Lib => src/Core}/Logger/GeekbotLogger.cs | 2 +- .../Lib => src/Core}/Logger/IDiscordLogger.cs | 2 +- .../Lib => src/Core}/Logger/IGeekbotLogger.cs | 2 +- .../Lib => src/Core}/Logger/LogDto.cs | 2 +- .../Lib => src/Core}/Logger/LogSource.cs | 2 +- .../Lib => src/Core}/Logger/LoggerFactory.cs | 2 +- .../Lib => src/Core}/Logger/MessageDto.cs | 2 +- .../Core}/Logger/SimpleConextConverter.cs | 2 +- .../Lib => src/Core}/MalClient/IMalClient.cs | 2 +- .../Lib => src/Core}/MalClient/MalClient.cs | 6 +- .../Core}/Media/FortunesProvider.cs | 6 +- .../Core}/Media/IFortunesProvider.cs | 2 +- .../Lib => src/Core}/Media/IMediaProvider.cs | 2 +- .../Lib => src/Core}/Media/MediaProvider.cs | 10 ++- .../Lib => src/Core}/Media/MediaType.cs | 2 +- .../Core}/Polyfills/UserPolyfillDto.cs | 4 +- .../IRandomNumberGenerator.cs | 2 +- .../RandomNumberGenerator.cs | 2 +- .../ReactionListener/IReactionListener.cs | 2 +- .../ReactionListener/ReactionListener.cs | 8 +-- .../Lib => src/Core}/RunParameters.cs | 2 +- .../Core}/UserRepository/IUserRepository.cs | 4 +- .../Core}/UserRepository/UserRepository.cs | 11 ++- .../Core}/WikipediaClient/IWikipediaClient.cs | 4 +- .../Core}/WikipediaClient/Page/PageApiUrls.cs | 2 +- .../Page/PageContentUrlCollection.cs | 2 +- .../WikipediaClient/Page/PageContentUrls.cs | 2 +- .../WikipediaClient/Page/PageCoordinates.cs | 2 +- .../Core}/WikipediaClient/Page/PageImage.cs | 2 +- .../WikipediaClient/Page/PageNamespace.cs | 2 +- .../Core}/WikipediaClient/Page/PagePreview.cs | 2 +- .../Core}/WikipediaClient/Page/PageTitles.cs | 2 +- .../Core}/WikipediaClient/Page/PageTypes.cs | 2 +- .../Core}/WikipediaClient/WikipediaClient.cs | 4 +- {Geekbot.net/WebApi => src/Web}/ApiError.cs | 2 +- .../Callback/CallbackController.cs | 4 +- .../Callback/CallbackTokenResponseDto.cs | 2 +- .../Controllers/Commands/CommandController.cs | 2 +- .../Web}/Controllers/Commands/CommandDto.cs | 2 +- .../Controllers/Commands/CommandParamDto.cs | 2 +- .../Highscores/HighscoreController.cs | 4 +- .../HighscoreControllerPostBodyDto.cs | 4 +- .../HighscoreControllerReponseBody.cs | 4 +- .../Web}/Controllers/Status/ApiStatusDto.cs | 2 +- .../Controllers/Status/StatusController.cs | 4 +- .../Web}/Logging/AspLogProvider.cs | 4 +- .../WebApi => src/Web}/Logging/AspLogger.cs | 4 +- src/Web/Web.csproj | 26 +++++++ .../WebApi => src/Web}/WebApiStartup.cs | 16 ++--- 197 files changed, 542 insertions(+), 498 deletions(-) delete mode 100755 Geekbot.net/Geekbot.net.csproj rename Tests/{Lib => Core}/Converters/EmojiConverter.test.cs (94%) rename Tests/{Lib => Core}/DiceParser/DiceParser.test.cs (97%) rename Tests/{Lib => Core}/DiceParser/SingleDie.test.cs (97%) rename Tests/{Lib => Core}/Levels/LevelCalc.test.cs (91%) rename Tests/{Lib => Core}/Localization/TranslationGuildContext.test.cs (95%) rename Tests/{Lib => Core}/Localization/Translations.test.cs (91%) create mode 100644 src/Bot/Bot.csproj rename {Geekbot.net => src/Bot}/Commands/Admin/Admin.cs (95%) rename {Geekbot.net => src/Bot}/Commands/Admin/Mod.cs (87%) rename {Geekbot.net => src/Bot}/Commands/Admin/Owner/Owner.cs (93%) rename {Geekbot.net => src/Bot}/Commands/Admin/Role.cs (96%) rename {Geekbot.net => src/Bot}/Commands/Games/Pokedex.cs (93%) rename {Geekbot.net => src/Bot}/Commands/Games/Roll/Roll.cs (92%) rename {Geekbot.net => src/Bot}/Commands/Games/Roll/RollTimeout.cs (74%) rename {Geekbot.net => src/Bot}/Commands/Integrations/LolMmr/LolMmr.cs (92%) rename {Geekbot.net => src/Bot}/Commands/Integrations/LolMmr/LolMmrDto.cs (76%) rename {Geekbot.net => src/Bot}/Commands/Integrations/LolMmr/LolMrrInfoDto.cs (71%) rename {Geekbot.net => src/Bot}/Commands/Integrations/MagicTheGathering.cs (93%) rename {Geekbot.net => src/Bot}/Commands/Integrations/Mal.cs (94%) rename {Geekbot.net => src/Bot}/Commands/Integrations/UbranDictionary/UrbanDictListItemDto.cs (81%) rename {Geekbot.net => src/Bot}/Commands/Integrations/UbranDictionary/UrbanDictResponseDto.cs (73%) rename {Geekbot.net => src/Bot}/Commands/Integrations/UbranDictionary/UrbanDictionary.cs (91%) rename {Geekbot.net => src/Bot}/Commands/Integrations/Wikipedia.cs (92%) rename {Geekbot.net => src/Bot}/Commands/Integrations/Youtube.cs (91%) rename {Geekbot.net => src/Bot}/Commands/Randomness/BenedictCumberbatchNameGenerator.cs (95%) rename {Geekbot.net => src/Bot}/Commands/Randomness/Cat/Cat.cs (86%) rename {Geekbot.net => src/Bot}/Commands/Randomness/Cat/CatResponseDto.cs (62%) rename {Geekbot.net => src/Bot}/Commands/Randomness/Chuck/ChuckNorrisJokeResponseDto.cs (64%) rename {Geekbot.net => src/Bot}/Commands/Randomness/Chuck/ChuckNorrisJokes.cs (87%) rename {Geekbot.net => src/Bot}/Commands/Randomness/Dad/DadJokeResponseDto.cs (63%) rename {Geekbot.net => src/Bot}/Commands/Randomness/Dad/DadJokes.cs (85%) rename {Geekbot.net => src/Bot}/Commands/Randomness/Dog/Dog.cs (86%) rename {Geekbot.net => src/Bot}/Commands/Randomness/Dog/DogResponseDto.cs (61%) rename {Geekbot.net => src/Bot}/Commands/Randomness/EightBall.cs (95%) rename {Geekbot.net => src/Bot}/Commands/Randomness/Fortune.cs (83%) rename {Geekbot.net => src/Bot}/Commands/Randomness/Gdq.cs (88%) rename {Geekbot.net => src/Bot}/Commands/Randomness/Greetings/GreetingBaseDto.cs (84%) rename {Geekbot.net => src/Bot}/Commands/Randomness/Greetings/GreetingDto.cs (80%) rename {Geekbot.net => src/Bot}/Commands/Randomness/Greetings/Greetings.cs (90%) rename {Geekbot.net => src/Bot}/Commands/Randomness/Kanye/Kanye.cs (87%) rename {Geekbot.net => src/Bot}/Commands/Randomness/Kanye/KanyeResponseDto.cs (73%) rename {Geekbot.net => src/Bot}/Commands/Randomness/RandomAnimals.cs (97%) rename {Geekbot.net => src/Bot}/Commands/Randomness/Ship.cs (91%) rename {Geekbot.net => src/Bot}/Commands/Randomness/Slap.cs (93%) rename {Geekbot.net => src/Bot}/Commands/Rpg/Cookies.cs (92%) rename {Geekbot.net => src/Bot}/Commands/User/GuildInfo.cs (87%) rename {Geekbot.net => src/Bot}/Commands/User/Karma.cs (95%) rename {Geekbot.net => src/Bot}/Commands/User/Ranking/Rank.cs (90%) rename {Geekbot.net => src/Bot}/Commands/User/Stats.cs (94%) rename {Geekbot.net => src/Bot}/Commands/Utils/AvatarGetter.cs (88%) rename {Geekbot.net => src/Bot}/Commands/Utils/Changelog/Changelog.cs (92%) rename {Geekbot.net => src/Bot}/Commands/Utils/Changelog/CommitAuthorDto.cs (77%) rename {Geekbot.net => src/Bot}/Commands/Utils/Changelog/CommitDto.cs (62%) rename {Geekbot.net => src/Bot}/Commands/Utils/Changelog/CommitInfoDto.cs (71%) rename {Geekbot.net => src/Bot}/Commands/Utils/Choose.cs (88%) rename {Geekbot.net => src/Bot}/Commands/Utils/Corona/CoronaStats.cs (94%) rename {Geekbot.net => src/Bot}/Commands/Utils/Corona/CoronaSummaryDto.cs (82%) rename {Geekbot.net => src/Bot}/Commands/Utils/Dice.cs (97%) rename {Geekbot.net => src/Bot}/Commands/Utils/Emojify.cs (88%) rename {Geekbot.net => src/Bot}/Commands/Utils/Help.cs (93%) rename {Geekbot.net => src/Bot}/Commands/Utils/Info.cs (92%) rename {Geekbot.net => src/Bot}/Commands/Utils/Lmgtfy.cs (92%) rename {Geekbot.net => src/Bot}/Commands/Utils/Ping.cs (89%) rename {Geekbot.net => src/Bot}/Commands/Utils/Quote/Quote.cs (97%) rename {Geekbot.net => src/Bot}/Commands/Utils/Quote/QuoteObjectDto.cs (81%) rename {Geekbot.net => src/Bot}/Handlers/CommandHandler.cs (96%) rename {Geekbot.net => src/Bot}/Handlers/MessageDeletedHandler.cs (93%) rename {Geekbot.net => src/Bot}/Handlers/ReactionHandler.cs (94%) rename {Geekbot.net => src/Bot}/Handlers/StatsHandler.cs (92%) rename {Geekbot.net => src/Bot}/Handlers/UserHandler.cs (95%) rename {Geekbot.net => src/Bot}/Logs/.keep (100%) mode change 100755 => 100644 rename {Geekbot.net => src/Bot}/Program.cs (91%) mode change 100755 => 100644 rename {Geekbot.net => src/Bot}/Storage/croissant (100%) rename {Geekbot.net => src/Bot}/Storage/dab (100%) rename {Geekbot.net => src/Bot}/Storage/fortunes (100%) rename {Geekbot.net => src/Bot}/Storage/foxes (100%) rename {Geekbot.net => src/Bot}/Storage/pandas (100%) rename {Geekbot.net => src/Bot}/Storage/penguins (100%) rename {Geekbot.net => src/Bot}/Storage/pumpkin (100%) rename {Geekbot.net => src/Bot}/Storage/squirrel (100%) rename {Geekbot.net => src/Bot}/Storage/turtles (100%) rename {Geekbot.net => src/Bot}/derp.ico (100%) rename {Geekbot.net/Lib => src/Core}/CommandPreconditions/DisableInDirectMessageAttribute.cs (92%) rename {Geekbot.net/Lib => src/Core}/Constants.cs (72%) rename {Geekbot.net/Lib => src/Core}/Converters/EmojiConverter.cs (96%) rename {Geekbot.net/Lib => src/Core}/Converters/IEmojiConverter.cs (74%) rename {Geekbot.net/Lib => src/Core}/Converters/IMtgManaConverter.cs (67%) rename {Geekbot.net/Lib => src/Core}/Converters/MtgManaConverter.cs (96%) create mode 100644 src/Core/Core.csproj rename {Geekbot.net => src/Core}/Database/DatabaseContext.cs (91%) rename {Geekbot.net => src/Core}/Database/DatabaseInitializer.cs (93%) rename {Geekbot.net => src/Core}/Database/InMemoryDatabase.cs (89%) rename {Geekbot.net => src/Core}/Database/LoggingAdapter/NpgsqlLoggingAdapter.cs (93%) rename {Geekbot.net => src/Core}/Database/LoggingAdapter/NpgsqlLoggingProviderAdapter.cs (82%) rename {Geekbot.net => src/Core}/Database/Models/CookiesModel.cs (87%) rename {Geekbot.net => src/Core}/Database/Models/GlobalsModel.cs (85%) rename {Geekbot.net => src/Core}/Database/Models/GuildSettingsModel.cs (90%) rename {Geekbot.net => src/Core}/Database/Models/KarmaModel.cs (87%) rename {Geekbot.net => src/Core}/Database/Models/MessagesModel.cs (85%) rename {Geekbot.net => src/Core}/Database/Models/QuoteModel.cs (93%) rename {Geekbot.net => src/Core}/Database/Models/ReactionListenerModel.cs (91%) rename {Geekbot.net => src/Core}/Database/Models/RoleSelfServiceModel.cs (85%) rename {Geekbot.net => src/Core}/Database/Models/RollsModel.cs (85%) rename {Geekbot.net => src/Core}/Database/Models/ShipsModel.cs (84%) rename {Geekbot.net => src/Core}/Database/Models/SlapsModel.cs (86%) rename {Geekbot.net => src/Core}/Database/Models/UserModel.cs (84%) rename {Geekbot.net => src/Core}/Database/SqlConnectionString.cs (94%) rename {Geekbot.net => src/Core}/Database/SqlDatabase.cs (90%) rename {Geekbot.net/Lib => src/Core}/DiceParser/DiceException.cs (84%) rename {Geekbot.net/Lib => src/Core}/DiceParser/DiceInput.cs (88%) rename {Geekbot.net/Lib => src/Core}/DiceParser/DiceInputOptions.cs (71%) rename {Geekbot.net/Lib => src/Core}/DiceParser/DiceParser.cs (97%) rename {Geekbot.net/Lib => src/Core}/DiceParser/DieAdvantageType.cs (73%) rename {Geekbot.net/Lib => src/Core}/DiceParser/DieResult.cs (96%) rename {Geekbot.net/Lib => src/Core}/DiceParser/IDiceParser.cs (70%) rename {Geekbot.net/Lib => src/Core}/DiceParser/SingleDie.cs (94%) rename {Geekbot.net/Lib => src/Core}/ErrorHandling/ErrorHandler.cs (97%) rename {Geekbot.net/Lib => src/Core}/ErrorHandling/IErrorHandler.cs (86%) rename {Geekbot.net/Lib => src/Core}/Extensions/DbSetExtensions.cs (94%) rename {Geekbot.net/Lib => src/Core}/Extensions/EmbedBuilderExtensions.cs (86%) rename {Geekbot.net/Lib => src/Core}/Extensions/IntExtensions.cs (87%) rename {Geekbot.net/Lib => src/Core}/Extensions/LongExtensions.cs (79%) rename {Geekbot.net/Lib => src/Core}/Extensions/StringExtensions.cs (83%) rename {Geekbot.net/Lib => src/Core}/Extensions/UlongExtensions.cs (79%) rename {Geekbot.net/Lib => src/Core}/GeekbotExitCode.cs (88%) rename {Geekbot.net/Lib => src/Core}/GlobalSettings/GlobalSettings.cs (91%) rename {Geekbot.net/Lib => src/Core}/GlobalSettings/IGlobalSettings.cs (72%) rename {Geekbot.net/Lib => src/Core}/GuildSettingsManager/GuildSettingsManager.cs (93%) rename {Geekbot.net/Lib => src/Core}/GuildSettingsManager/IGuildSettingsManager.cs (73%) rename {Geekbot.net/Lib => src/Core}/Highscores/HighscoreListEmptyException.cs (86%) rename {Geekbot.net/Lib => src/Core}/Highscores/HighscoreManager.cs (93%) rename {Geekbot.net/Lib => src/Core}/Highscores/HighscoreTypes.cs (69%) rename {Geekbot.net/Lib => src/Core}/Highscores/HighscoreUserDto.cs (82%) rename {Geekbot.net/Lib => src/Core}/Highscores/IHighscoreManager.cs (89%) rename {Geekbot.net/Lib => src/Core}/HttpAbstractions.cs (97%) rename {Geekbot.net/Lib => src/Core}/KvInMemoryStore/IKvInMemoryStore.cs (80%) rename {Geekbot.net/Lib => src/Core}/KvInMemoryStore/KvInMemoryStore.cs (93%) rename {Geekbot.net/Lib => src/Core}/Levels/ILevelCalc.cs (67%) rename {Geekbot.net/Lib => src/Core}/Levels/LevelCalc.cs (95%) rename {Geekbot.net/Lib => src/Core}/Localization/ITranslationHandler.cs (91%) rename {Geekbot.net/Lib => src/Core}/Localization/TranslationGuildContext.cs (95%) rename {Geekbot.net/Lib => src/Core}/Localization/TranslationHandler.cs (96%) rename {Geekbot.net/Lib => src/Core}/Localization/Translations.yml (100%) rename {Geekbot.net/Lib => src/Core}/Logger/DiscordLogger.cs (95%) rename {Geekbot.net/Lib => src/Core}/Logger/GeekbotLogger.cs (96%) rename {Geekbot.net/Lib => src/Core}/Logger/IDiscordLogger.cs (76%) rename {Geekbot.net/Lib => src/Core}/Logger/IGeekbotLogger.cs (92%) rename {Geekbot.net/Lib => src/Core}/Logger/LogDto.cs (88%) rename {Geekbot.net/Lib => src/Core}/Logger/LogSource.cs (87%) rename {Geekbot.net/Lib => src/Core}/Logger/LoggerFactory.cs (98%) rename {Geekbot.net/Lib => src/Core}/Logger/MessageDto.cs (92%) rename {Geekbot.net/Lib => src/Core}/Logger/SimpleConextConverter.cs (96%) rename {Geekbot.net/Lib => src/Core}/MalClient/IMalClient.cs (83%) rename {Geekbot.net/Lib => src/Core}/MalClient/MalClient.cs (92%) rename {Geekbot.net/Lib => src/Core}/Media/FortunesProvider.cs (85%) rename {Geekbot.net/Lib => src/Core}/Media/IFortunesProvider.cs (68%) rename {Geekbot.net/Lib => src/Core}/Media/IMediaProvider.cs (69%) rename {Geekbot.net/Lib => src/Core}/Media/MediaProvider.cs (92%) rename {Geekbot.net/Lib => src/Core}/Media/MediaType.cs (84%) rename {Geekbot.net/Lib => src/Core}/Polyfills/UserPolyfillDto.cs (90%) rename {Geekbot.net/Lib => src/Core}/RandomNumberGenerator/IRandomNumberGenerator.cs (67%) rename {Geekbot.net/Lib => src/Core}/RandomNumberGenerator/RandomNumberGenerator.cs (92%) rename {Geekbot.net/Lib => src/Core}/ReactionListener/IReactionListener.cs (89%) rename {Geekbot.net/Lib => src/Core}/ReactionListener/ReactionListener.cs (92%) rename {Geekbot.net/Lib => src/Core}/RunParameters.cs (97%) rename {Geekbot.net/Lib => src/Core}/UserRepository/IUserRepository.cs (68%) rename {Geekbot.net/Lib => src/Core}/UserRepository/UserRepository.cs (88%) rename {Geekbot.net/Lib => src/Core}/WikipediaClient/IWikipediaClient.cs (63%) rename {Geekbot.net/Lib => src/Core}/WikipediaClient/Page/PageApiUrls.cs (84%) rename {Geekbot.net/Lib => src/Core}/WikipediaClient/Page/PageContentUrlCollection.cs (73%) rename {Geekbot.net/Lib => src/Core}/WikipediaClient/Page/PageContentUrls.cs (79%) rename {Geekbot.net/Lib => src/Core}/WikipediaClient/Page/PageCoordinates.cs (68%) rename {Geekbot.net/Lib => src/Core}/WikipediaClient/Page/PageImage.cs (76%) rename {Geekbot.net/Lib => src/Core}/WikipediaClient/Page/PageNamespace.cs (68%) rename {Geekbot.net/Lib => src/Core}/WikipediaClient/Page/PagePreview.cs (94%) rename {Geekbot.net/Lib => src/Core}/WikipediaClient/Page/PageTitles.cs (76%) rename {Geekbot.net/Lib => src/Core}/WikipediaClient/Page/PageTypes.cs (84%) rename {Geekbot.net/Lib => src/Core}/WikipediaClient/WikipediaClient.cs (87%) rename {Geekbot.net/WebApi => src/Web}/ApiError.cs (70%) rename {Geekbot.net/WebApi => src/Web}/Controllers/Callback/CallbackController.cs (96%) rename {Geekbot.net/WebApi => src/Web}/Controllers/Callback/CallbackTokenResponseDto.cs (85%) rename {Geekbot.net/WebApi => src/Web}/Controllers/Commands/CommandController.cs (93%) rename {Geekbot.net/WebApi => src/Web}/Controllers/Commands/CommandDto.cs (83%) rename {Geekbot.net/WebApi => src/Web}/Controllers/Commands/CommandParamDto.cs (74%) rename {Geekbot.net/WebApi => src/Web}/Controllers/Highscores/HighscoreController.cs (92%) rename {Geekbot.net/WebApi => src/Web}/Controllers/Highscores/HighscoreControllerPostBodyDto.cs (76%) rename {Geekbot.net/WebApi => src/Web}/Controllers/Highscores/HighscoreControllerReponseBody.cs (66%) rename {Geekbot.net/WebApi => src/Web}/Controllers/Status/ApiStatusDto.cs (75%) rename {Geekbot.net/WebApi => src/Web}/Controllers/Status/StatusController.cs (89%) rename {Geekbot.net/WebApi => src/Web}/Logging/AspLogProvider.cs (88%) rename {Geekbot.net/WebApi => src/Web}/Logging/AspLogger.cs (95%) create mode 100644 src/Web/Web.csproj rename {Geekbot.net/WebApi => src/Web}/WebApiStartup.cs (86%) diff --git a/.gitignore b/.gitignore index 5db124b..066c4b4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,10 @@ -*/bin/ -*/obj/ -Geekbot.net/tmp/ +/*/**/bin +/*/**/obj +src/Bot/tmp/ +src/Bot/Logs/* +!/src/Bot/Logs/.keep .vs/ -UpgradeLog.htm .idea .vscode -Geekbot.net/Logs/* -!/Geekbot.net/Logs/.keep Geekbot.net.sln.DotSettings.user -Geekbot.net/temp/ -app \ No newline at end of file +app diff --git a/Geekbot.net.sln b/Geekbot.net.sln index 5011ad4..5dc5e4b 100644 --- a/Geekbot.net.sln +++ b/Geekbot.net.sln @@ -3,24 +3,36 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 2013 VisualStudioVersion = 12.0.0.0 MinimumVisualStudioVersion = 10.0.0.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Geekbot.net", "Geekbot.net/Geekbot.net.csproj", "{FDCB3D92-E7B5-47BB-A9B5-CFAEFA57CDB4}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{4CAF5F02-EFFE-4FDA-BD44-EEADDBA9600E}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Core", "src\Core\Core.csproj", "{47671723-52A9-4668-BBC5-2BA76AE3B288}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Web", "src\Web\Web.csproj", "{0A63D5DC-6325-4F53-8ED2-9843239B76CC}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bot", "src\Bot\Bot.csproj", "{DBF79896-9F7F-443D-B336-155E276DFF16}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {FDCB3D92-E7B5-47BB-A9B5-CFAEFA57CDB4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {FDCB3D92-E7B5-47BB-A9B5-CFAEFA57CDB4}.Debug|Any CPU.Build.0 = Debug|Any CPU - {FDCB3D92-E7B5-47BB-A9B5-CFAEFA57CDB4}.Release|Any CPU.ActiveCfg = Release|Any CPU - {FDCB3D92-E7B5-47BB-A9B5-CFAEFA57CDB4}.Release|Any CPU.Build.0 = Release|Any CPU {4CAF5F02-EFFE-4FDA-BD44-EEADDBA9600E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {4CAF5F02-EFFE-4FDA-BD44-EEADDBA9600E}.Debug|Any CPU.Build.0 = Debug|Any CPU {4CAF5F02-EFFE-4FDA-BD44-EEADDBA9600E}.Release|Any CPU.ActiveCfg = Release|Any CPU {4CAF5F02-EFFE-4FDA-BD44-EEADDBA9600E}.Release|Any CPU.Build.0 = Release|Any CPU + {47671723-52A9-4668-BBC5-2BA76AE3B288}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {47671723-52A9-4668-BBC5-2BA76AE3B288}.Debug|Any CPU.Build.0 = Debug|Any CPU + {47671723-52A9-4668-BBC5-2BA76AE3B288}.Release|Any CPU.ActiveCfg = Release|Any CPU + {47671723-52A9-4668-BBC5-2BA76AE3B288}.Release|Any CPU.Build.0 = Release|Any CPU + {0A63D5DC-6325-4F53-8ED2-9843239B76CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0A63D5DC-6325-4F53-8ED2-9843239B76CC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0A63D5DC-6325-4F53-8ED2-9843239B76CC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0A63D5DC-6325-4F53-8ED2-9843239B76CC}.Release|Any CPU.Build.0 = Release|Any CPU + {DBF79896-9F7F-443D-B336-155E276DFF16}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DBF79896-9F7F-443D-B336-155E276DFF16}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DBF79896-9F7F-443D-B336-155E276DFF16}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DBF79896-9F7F-443D-B336-155E276DFF16}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Geekbot.net/Geekbot.net.csproj b/Geekbot.net/Geekbot.net.csproj deleted file mode 100755 index 006e6cb..0000000 --- a/Geekbot.net/Geekbot.net.csproj +++ /dev/null @@ -1,64 +0,0 @@ - - - Exe - net5.0 - win-x64;linux-x64 - derp.ico - $(VersionSuffix) - $(VersionSuffix) - 0.0.0-DEV - Pizza and Coffee Studios - Pizza and Coffee Studios - A Discord bot - https://github.com/pizzaandcoffee/Geekbot.net - NU1701 - git - https://geekbot.pizzaandcoffee.rocks - - - true - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - PreserveNewest - - - Always - - - - - - \ No newline at end of file diff --git a/Tests/Lib/Converters/EmojiConverter.test.cs b/Tests/Core/Converters/EmojiConverter.test.cs similarity index 94% rename from Tests/Lib/Converters/EmojiConverter.test.cs rename to Tests/Core/Converters/EmojiConverter.test.cs index ad37824..f58d669 100644 --- a/Tests/Lib/Converters/EmojiConverter.test.cs +++ b/Tests/Core/Converters/EmojiConverter.test.cs @@ -1,7 +1,7 @@ -using Geekbot.net.Lib.Converters; +using Geekbot.Core.Converters; using Xunit; -namespace Tests.Lib.Converters +namespace Tests.Core.Converters { public class EmojiConverterTest { diff --git a/Tests/Lib/DiceParser/DiceParser.test.cs b/Tests/Core/DiceParser/DiceParser.test.cs similarity index 97% rename from Tests/Lib/DiceParser/DiceParser.test.cs rename to Tests/Core/DiceParser/DiceParser.test.cs index 48a362c..8a9163a 100644 --- a/Tests/Lib/DiceParser/DiceParser.test.cs +++ b/Tests/Core/DiceParser/DiceParser.test.cs @@ -1,11 +1,10 @@ using System.Collections.Generic; using System.Text.Json; -using Geekbot.net.Lib.DiceParser; -using Geekbot.net.Lib.RandomNumberGenerator; +using Geekbot.Core.DiceParser; +using Geekbot.Core.RandomNumberGenerator; using Xunit; -using YamlDotNet.Serialization; -namespace Tests.Lib.DiceParser +namespace Tests.Core.DiceParser { public class DiceParserTest { @@ -200,7 +199,7 @@ namespace Tests.Lib.DiceParser [Theory, MemberData(nameof(DiceParserTestData))] public void DiceParserTestFunc(string testName, DiceParserTestDto testData) { - var parser = new Geekbot.net.Lib.DiceParser.DiceParser(_randomNumberGenerator); + var parser = new Geekbot.Core.DiceParser.DiceParser(_randomNumberGenerator); var result = parser.Parse(testData.Input); Assert.Equal(JsonSerializer.Serialize(result), JsonSerializer.Serialize(testData.Expected)); diff --git a/Tests/Lib/DiceParser/SingleDie.test.cs b/Tests/Core/DiceParser/SingleDie.test.cs similarity index 97% rename from Tests/Lib/DiceParser/SingleDie.test.cs rename to Tests/Core/DiceParser/SingleDie.test.cs index 2309f98..813a33b 100644 --- a/Tests/Lib/DiceParser/SingleDie.test.cs +++ b/Tests/Core/DiceParser/SingleDie.test.cs @@ -1,8 +1,8 @@ -using Geekbot.net.Lib.DiceParser; -using Geekbot.net.Lib.RandomNumberGenerator; +using Geekbot.Core.DiceParser; +using Geekbot.Core.RandomNumberGenerator; using Xunit; -namespace Tests.Lib.DiceParser +namespace Tests.Core.DiceParser { public class SingleDieTest { diff --git a/Tests/Lib/Levels/LevelCalc.test.cs b/Tests/Core/Levels/LevelCalc.test.cs similarity index 91% rename from Tests/Lib/Levels/LevelCalc.test.cs rename to Tests/Core/Levels/LevelCalc.test.cs index 9b4b97c..02db6f5 100644 --- a/Tests/Lib/Levels/LevelCalc.test.cs +++ b/Tests/Core/Levels/LevelCalc.test.cs @@ -1,8 +1,7 @@ -using System.Collections.Generic; -using Geekbot.net.Lib.Levels; +using Geekbot.Core.Levels; using Xunit; -namespace Tests.Lib.Levels +namespace Tests.Core.Levels { public class LevelCalcTest { diff --git a/Tests/Lib/Localization/TranslationGuildContext.test.cs b/Tests/Core/Localization/TranslationGuildContext.test.cs similarity index 95% rename from Tests/Lib/Localization/TranslationGuildContext.test.cs rename to Tests/Core/Localization/TranslationGuildContext.test.cs index aeea0e1..4aaca16 100644 --- a/Tests/Lib/Localization/TranslationGuildContext.test.cs +++ b/Tests/Core/Localization/TranslationGuildContext.test.cs @@ -1,10 +1,10 @@ using System; using System.Collections.Generic; -using Geekbot.net.Lib.Localization; +using Geekbot.Core.Localization; using Moq; using Xunit; -namespace Tests.Lib.Localization +namespace Tests.Core.Localization { public class TranslationGuildContext_test { diff --git a/Tests/Lib/Localization/Translations.test.cs b/Tests/Core/Localization/Translations.test.cs similarity index 91% rename from Tests/Lib/Localization/Translations.test.cs rename to Tests/Core/Localization/Translations.test.cs index 736ed83..d8a0879 100644 --- a/Tests/Lib/Localization/Translations.test.cs +++ b/Tests/Core/Localization/Translations.test.cs @@ -6,7 +6,7 @@ using Xunit; using YamlDotNet.Core; using YamlDotNet.Serialization; -namespace Tests.Lib.Localization +namespace Tests.Core.Localization { public class Translations_test { @@ -14,7 +14,7 @@ namespace Tests.Lib.Localization public void TranslationsYamlIsValid() { // Read the file - var translationFile = File.ReadAllText(Path.GetFullPath("./../../../../Geekbot.net/Lib/Localization/Translations.yml")); + var translationFile = File.ReadAllText(Path.GetFullPath("./../../../../src/Core/Localization/Translations.yml")); // Deserialize var input = new StringReader(translationFile); diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index eaf88a3..b8d1fc4 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -15,6 +15,6 @@ - + \ No newline at end of file diff --git a/src/Bot/Bot.csproj b/src/Bot/Bot.csproj new file mode 100644 index 0000000..c7889ae --- /dev/null +++ b/src/Bot/Bot.csproj @@ -0,0 +1,43 @@ + + + Exe + net5.0 + win-x64;linux-x64 + derp.ico + $(VersionSuffix) + Geekbot.Bot + Geekbot + $(VersionSuffix) + 0.0.0-DEV + Pizza and Coffee Studios + Pizza and Coffee Studios + A Discord bot + https://github.com/pizzaandcoffee/Geekbot.net + NU1701 + git + https://geekbot.pizzaandcoffee.rocks + + + true + + + + + + + + + + + + + + + PreserveNewest + + + + + + + diff --git a/Geekbot.net/Commands/Admin/Admin.cs b/src/Bot/Commands/Admin/Admin.cs similarity index 95% rename from Geekbot.net/Commands/Admin/Admin.cs rename to src/Bot/Commands/Admin/Admin.cs index 9120971..f6c4210 100644 --- a/Geekbot.net/Commands/Admin/Admin.cs +++ b/src/Bot/Commands/Admin/Admin.cs @@ -4,13 +4,13 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Discord.WebSocket; -using Geekbot.net.Lib.CommandPreconditions; -using Geekbot.net.Lib.ErrorHandling; -using Geekbot.net.Lib.Extensions; -using Geekbot.net.Lib.GuildSettingsManager; -using Geekbot.net.Lib.Localization; +using Geekbot.Core.CommandPreconditions; +using Geekbot.Core.ErrorHandling; +using Geekbot.Core.Extensions; +using Geekbot.Core.GuildSettingsManager; +using Geekbot.Core.Localization; -namespace Geekbot.net.Commands.Admin +namespace Geekbot.Bot.Commands.Admin { [Group("admin")] [RequireUserPermission(GuildPermission.Administrator)] diff --git a/Geekbot.net/Commands/Admin/Mod.cs b/src/Bot/Commands/Admin/Mod.cs similarity index 87% rename from Geekbot.net/Commands/Admin/Mod.cs rename to src/Bot/Commands/Admin/Mod.cs index b0114a8..b642680 100644 --- a/Geekbot.net/Commands/Admin/Mod.cs +++ b/src/Bot/Commands/Admin/Mod.cs @@ -2,10 +2,10 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Lib.CommandPreconditions; -using Geekbot.net.Lib.ErrorHandling; +using Geekbot.Core.CommandPreconditions; +using Geekbot.Core.ErrorHandling; -namespace Geekbot.net.Commands.Admin +namespace Geekbot.Bot.Commands.Admin { [Group("mod")] [RequireUserPermission(GuildPermission.KickMembers)] diff --git a/Geekbot.net/Commands/Admin/Owner/Owner.cs b/src/Bot/Commands/Admin/Owner/Owner.cs similarity index 93% rename from Geekbot.net/Commands/Admin/Owner/Owner.cs rename to src/Bot/Commands/Admin/Owner/Owner.cs index 996d848..5fa3976 100644 --- a/Geekbot.net/Commands/Admin/Owner/Owner.cs +++ b/src/Bot/Commands/Admin/Owner/Owner.cs @@ -3,12 +3,12 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Discord.WebSocket; -using Geekbot.net.Lib.ErrorHandling; -using Geekbot.net.Lib.GlobalSettings; -using Geekbot.net.Lib.Logger; -using Geekbot.net.Lib.UserRepository; +using Geekbot.Core.ErrorHandling; +using Geekbot.Core.GlobalSettings; +using Geekbot.Core.Logger; +using Geekbot.Core.UserRepository; -namespace Geekbot.net.Commands.Admin.Owner +namespace Geekbot.Bot.Commands.Admin.Owner { [Group("owner")] [RequireOwner] diff --git a/Geekbot.net/Commands/Admin/Role.cs b/src/Bot/Commands/Admin/Role.cs similarity index 96% rename from Geekbot.net/Commands/Admin/Role.cs rename to src/Bot/Commands/Admin/Role.cs index 6d4557d..e997fa8 100644 --- a/Geekbot.net/Commands/Admin/Role.cs +++ b/src/Bot/Commands/Admin/Role.cs @@ -5,15 +5,15 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Discord.Net; -using Geekbot.net.Database; -using Geekbot.net.Database.Models; -using Geekbot.net.Lib.CommandPreconditions; -using Geekbot.net.Lib.ErrorHandling; -using Geekbot.net.Lib.Extensions; -using Geekbot.net.Lib.Localization; -using Geekbot.net.Lib.ReactionListener; +using Geekbot.Core.CommandPreconditions; +using Geekbot.Core.Database; +using Geekbot.Core.Database.Models; +using Geekbot.Core.ErrorHandling; +using Geekbot.Core.Extensions; +using Geekbot.Core.Localization; +using Geekbot.Core.ReactionListener; -namespace Geekbot.net.Commands.Admin +namespace Geekbot.Bot.Commands.Admin { [Group("role")] [DisableInDirectMessage] diff --git a/Geekbot.net/Commands/Games/Pokedex.cs b/src/Bot/Commands/Games/Pokedex.cs similarity index 93% rename from Geekbot.net/Commands/Games/Pokedex.cs rename to src/Bot/Commands/Games/Pokedex.cs index a0d2ae0..426761c 100644 --- a/Geekbot.net/Commands/Games/Pokedex.cs +++ b/src/Bot/Commands/Games/Pokedex.cs @@ -3,11 +3,11 @@ using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Lib.ErrorHandling; -using Geekbot.net.Lib.Extensions; +using Geekbot.Core.ErrorHandling; +using Geekbot.Core.Extensions; using PokeAPI; -namespace Geekbot.net.Commands.Games +namespace Geekbot.Bot.Commands.Games { public class Pokedex : ModuleBase { diff --git a/Geekbot.net/Commands/Games/Roll/Roll.cs b/src/Bot/Commands/Games/Roll/Roll.cs similarity index 92% rename from Geekbot.net/Commands/Games/Roll/Roll.cs rename to src/Bot/Commands/Games/Roll/Roll.cs index 1c89f19..6241ba1 100644 --- a/Geekbot.net/Commands/Games/Roll/Roll.cs +++ b/src/Bot/Commands/Games/Roll/Roll.cs @@ -2,15 +2,15 @@ using System.Linq; using System.Threading.Tasks; using Discord.Commands; -using Geekbot.net.Database; -using Geekbot.net.Database.Models; -using Geekbot.net.Lib.ErrorHandling; -using Geekbot.net.Lib.Extensions; -using Geekbot.net.Lib.KvInMemoryStore; -using Geekbot.net.Lib.Localization; -using Geekbot.net.Lib.RandomNumberGenerator; +using Geekbot.Core.Database; +using Geekbot.Core.Database.Models; +using Geekbot.Core.ErrorHandling; +using Geekbot.Core.Extensions; +using Geekbot.Core.KvInMemoryStore; +using Geekbot.Core.Localization; +using Geekbot.Core.RandomNumberGenerator; -namespace Geekbot.net.Commands.Games.Roll +namespace Geekbot.Bot.Commands.Games.Roll { public class Roll : ModuleBase { diff --git a/Geekbot.net/Commands/Games/Roll/RollTimeout.cs b/src/Bot/Commands/Games/Roll/RollTimeout.cs similarity index 74% rename from Geekbot.net/Commands/Games/Roll/RollTimeout.cs rename to src/Bot/Commands/Games/Roll/RollTimeout.cs index 81a520f..c53101a 100644 --- a/Geekbot.net/Commands/Games/Roll/RollTimeout.cs +++ b/src/Bot/Commands/Games/Roll/RollTimeout.cs @@ -1,6 +1,6 @@ using System; -namespace Geekbot.net.Commands.Games.Roll +namespace Geekbot.Bot.Commands.Games.Roll { public class RollTimeout { diff --git a/Geekbot.net/Commands/Integrations/LolMmr/LolMmr.cs b/src/Bot/Commands/Integrations/LolMmr/LolMmr.cs similarity index 92% rename from Geekbot.net/Commands/Integrations/LolMmr/LolMmr.cs rename to src/Bot/Commands/Integrations/LolMmr/LolMmr.cs index 5de7d63..4e75034 100644 --- a/Geekbot.net/Commands/Integrations/LolMmr/LolMmr.cs +++ b/src/Bot/Commands/Integrations/LolMmr/LolMmr.cs @@ -5,10 +5,10 @@ using System.Text; using System.Threading.Tasks; using System.Web; using Discord.Commands; -using Geekbot.net.Lib; -using Geekbot.net.Lib.ErrorHandling; +using Geekbot.Core; +using Geekbot.Core.ErrorHandling; -namespace Geekbot.net.Commands.Integrations.LolMmr +namespace Geekbot.Bot.Commands.Integrations.LolMmr { public class LolMmr : ModuleBase { diff --git a/Geekbot.net/Commands/Integrations/LolMmr/LolMmrDto.cs b/src/Bot/Commands/Integrations/LolMmr/LolMmrDto.cs similarity index 76% rename from Geekbot.net/Commands/Integrations/LolMmr/LolMmrDto.cs rename to src/Bot/Commands/Integrations/LolMmr/LolMmrDto.cs index 4a9887d..51d4c85 100644 --- a/Geekbot.net/Commands/Integrations/LolMmr/LolMmrDto.cs +++ b/src/Bot/Commands/Integrations/LolMmr/LolMmrDto.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Commands.Integrations.LolMmr +namespace Geekbot.Bot.Commands.Integrations.LolMmr { public class LolMmrDto { diff --git a/Geekbot.net/Commands/Integrations/LolMmr/LolMrrInfoDto.cs b/src/Bot/Commands/Integrations/LolMmr/LolMrrInfoDto.cs similarity index 71% rename from Geekbot.net/Commands/Integrations/LolMmr/LolMrrInfoDto.cs rename to src/Bot/Commands/Integrations/LolMmr/LolMrrInfoDto.cs index 55804fd..18b096a 100644 --- a/Geekbot.net/Commands/Integrations/LolMmr/LolMrrInfoDto.cs +++ b/src/Bot/Commands/Integrations/LolMmr/LolMrrInfoDto.cs @@ -1,7 +1,6 @@ -using System; using Newtonsoft.Json; -namespace Geekbot.net.Commands.Integrations.LolMmr +namespace Geekbot.Bot.Commands.Integrations.LolMmr { public class LolMrrInfoDto { diff --git a/Geekbot.net/Commands/Integrations/MagicTheGathering.cs b/src/Bot/Commands/Integrations/MagicTheGathering.cs similarity index 93% rename from Geekbot.net/Commands/Integrations/MagicTheGathering.cs rename to src/Bot/Commands/Integrations/MagicTheGathering.cs index f9e006c..66a87fe 100644 --- a/Geekbot.net/Commands/Integrations/MagicTheGathering.cs +++ b/src/Bot/Commands/Integrations/MagicTheGathering.cs @@ -4,12 +4,12 @@ using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Lib.Converters; -using Geekbot.net.Lib.ErrorHandling; -using Geekbot.net.Lib.Extensions; +using Geekbot.Core.Converters; +using Geekbot.Core.ErrorHandling; +using Geekbot.Core.Extensions; using MtgApiManager.Lib.Service; -namespace Geekbot.net.Commands.Integrations +namespace Geekbot.Bot.Commands.Integrations { public class MagicTheGathering : ModuleBase { diff --git a/Geekbot.net/Commands/Integrations/Mal.cs b/src/Bot/Commands/Integrations/Mal.cs similarity index 94% rename from Geekbot.net/Commands/Integrations/Mal.cs rename to src/Bot/Commands/Integrations/Mal.cs index f8aeb95..ae30493 100644 --- a/Geekbot.net/Commands/Integrations/Mal.cs +++ b/src/Bot/Commands/Integrations/Mal.cs @@ -4,11 +4,11 @@ using System.Web; using System.Xml; using Discord; using Discord.Commands; -using Geekbot.net.Lib.ErrorHandling; -using Geekbot.net.Lib.Extensions; -using Geekbot.net.Lib.MalClient; +using Geekbot.Core.ErrorHandling; +using Geekbot.Core.Extensions; +using Geekbot.Core.MalClient; -namespace Geekbot.net.Commands.Integrations +namespace Geekbot.Bot.Commands.Integrations { public class Mal : ModuleBase { diff --git a/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictListItemDto.cs b/src/Bot/Commands/Integrations/UbranDictionary/UrbanDictListItemDto.cs similarity index 81% rename from Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictListItemDto.cs rename to src/Bot/Commands/Integrations/UbranDictionary/UrbanDictListItemDto.cs index e98885b..0aee35f 100644 --- a/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictListItemDto.cs +++ b/src/Bot/Commands/Integrations/UbranDictionary/UrbanDictListItemDto.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Commands.Integrations.UbranDictionary +namespace Geekbot.Bot.Commands.Integrations.UbranDictionary { internal class UrbanListItemDto { diff --git a/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictResponseDto.cs b/src/Bot/Commands/Integrations/UbranDictionary/UrbanDictResponseDto.cs similarity index 73% rename from Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictResponseDto.cs rename to src/Bot/Commands/Integrations/UbranDictionary/UrbanDictResponseDto.cs index 2c3e014..1846601 100644 --- a/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictResponseDto.cs +++ b/src/Bot/Commands/Integrations/UbranDictionary/UrbanDictResponseDto.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; -namespace Geekbot.net.Commands.Integrations.UbranDictionary +namespace Geekbot.Bot.Commands.Integrations.UbranDictionary { internal class UrbanResponseDto { diff --git a/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs b/src/Bot/Commands/Integrations/UbranDictionary/UrbanDictionary.cs similarity index 91% rename from Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs rename to src/Bot/Commands/Integrations/UbranDictionary/UrbanDictionary.cs index 882caa5..bc1f478 100644 --- a/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs +++ b/src/Bot/Commands/Integrations/UbranDictionary/UrbanDictionary.cs @@ -3,11 +3,11 @@ using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Lib; -using Geekbot.net.Lib.ErrorHandling; -using Geekbot.net.Lib.Extensions; +using Geekbot.Core; +using Geekbot.Core.ErrorHandling; +using Geekbot.Core.Extensions; -namespace Geekbot.net.Commands.Integrations.UbranDictionary +namespace Geekbot.Bot.Commands.Integrations.UbranDictionary { public class UrbanDictionary : ModuleBase { diff --git a/Geekbot.net/Commands/Integrations/Wikipedia.cs b/src/Bot/Commands/Integrations/Wikipedia.cs similarity index 92% rename from Geekbot.net/Commands/Integrations/Wikipedia.cs rename to src/Bot/Commands/Integrations/Wikipedia.cs index 93f68fd..709f974 100644 --- a/Geekbot.net/Commands/Integrations/Wikipedia.cs +++ b/src/Bot/Commands/Integrations/Wikipedia.cs @@ -5,14 +5,14 @@ using System.Text; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Database; -using Geekbot.net.Lib.ErrorHandling; -using Geekbot.net.Lib.Extensions; -using Geekbot.net.Lib.WikipediaClient; -using Geekbot.net.Lib.WikipediaClient.Page; +using Geekbot.Core.Database; +using Geekbot.Core.ErrorHandling; +using Geekbot.Core.Extensions; +using Geekbot.Core.WikipediaClient; +using Geekbot.Core.WikipediaClient.Page; using HtmlAgilityPack; -namespace Geekbot.net.Commands.Integrations +namespace Geekbot.Bot.Commands.Integrations { public class Wikipedia : ModuleBase { diff --git a/Geekbot.net/Commands/Integrations/Youtube.cs b/src/Bot/Commands/Integrations/Youtube.cs similarity index 91% rename from Geekbot.net/Commands/Integrations/Youtube.cs rename to src/Bot/Commands/Integrations/Youtube.cs index 1672bab..60ebdaa 100644 --- a/Geekbot.net/Commands/Integrations/Youtube.cs +++ b/src/Bot/Commands/Integrations/Youtube.cs @@ -1,12 +1,12 @@ using System; using System.Threading.Tasks; using Discord.Commands; -using Geekbot.net.Lib.ErrorHandling; -using Geekbot.net.Lib.GlobalSettings; +using Geekbot.Core.ErrorHandling; +using Geekbot.Core.GlobalSettings; using Google.Apis.Services; using Google.Apis.YouTube.v3; -namespace Geekbot.net.Commands.Integrations +namespace Geekbot.Bot.Commands.Integrations { public class Youtube : ModuleBase { diff --git a/Geekbot.net/Commands/Randomness/BenedictCumberbatchNameGenerator.cs b/src/Bot/Commands/Randomness/BenedictCumberbatchNameGenerator.cs similarity index 95% rename from Geekbot.net/Commands/Randomness/BenedictCumberbatchNameGenerator.cs rename to src/Bot/Commands/Randomness/BenedictCumberbatchNameGenerator.cs index 838dff2..995c921 100644 --- a/Geekbot.net/Commands/Randomness/BenedictCumberbatchNameGenerator.cs +++ b/src/Bot/Commands/Randomness/BenedictCumberbatchNameGenerator.cs @@ -2,10 +2,10 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; using Discord.Commands; -using Geekbot.net.Lib.ErrorHandling; -using Geekbot.net.Lib.RandomNumberGenerator; +using Geekbot.Core.ErrorHandling; +using Geekbot.Core.RandomNumberGenerator; -namespace Geekbot.net.Commands.Randomness +namespace Geekbot.Bot.Commands.Randomness { public class BenedictCumberbatchNameGenerator : ModuleBase { diff --git a/Geekbot.net/Commands/Randomness/Cat/Cat.cs b/src/Bot/Commands/Randomness/Cat/Cat.cs similarity index 86% rename from Geekbot.net/Commands/Randomness/Cat/Cat.cs rename to src/Bot/Commands/Randomness/Cat/Cat.cs index 7cd7b19..7bd7e57 100644 --- a/Geekbot.net/Commands/Randomness/Cat/Cat.cs +++ b/src/Bot/Commands/Randomness/Cat/Cat.cs @@ -2,10 +2,10 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Lib; -using Geekbot.net.Lib.ErrorHandling; +using Geekbot.Core; +using Geekbot.Core.ErrorHandling; -namespace Geekbot.net.Commands.Randomness.Cat +namespace Geekbot.Bot.Commands.Randomness.Cat { public class Cat : ModuleBase { diff --git a/Geekbot.net/Commands/Randomness/Cat/CatResponseDto.cs b/src/Bot/Commands/Randomness/Cat/CatResponseDto.cs similarity index 62% rename from Geekbot.net/Commands/Randomness/Cat/CatResponseDto.cs rename to src/Bot/Commands/Randomness/Cat/CatResponseDto.cs index 05ebf2b..febb66f 100644 --- a/Geekbot.net/Commands/Randomness/Cat/CatResponseDto.cs +++ b/src/Bot/Commands/Randomness/Cat/CatResponseDto.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Commands.Randomness.Cat +namespace Geekbot.Bot.Commands.Randomness.Cat { internal class CatResponseDto { diff --git a/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokeResponseDto.cs b/src/Bot/Commands/Randomness/Chuck/ChuckNorrisJokeResponseDto.cs similarity index 64% rename from Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokeResponseDto.cs rename to src/Bot/Commands/Randomness/Chuck/ChuckNorrisJokeResponseDto.cs index 8d513b8..99d9493 100644 --- a/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokeResponseDto.cs +++ b/src/Bot/Commands/Randomness/Chuck/ChuckNorrisJokeResponseDto.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Commands.Randomness.Chuck +namespace Geekbot.Bot.Commands.Randomness.Chuck { internal class ChuckNorrisJokeResponseDto { diff --git a/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs b/src/Bot/Commands/Randomness/Chuck/ChuckNorrisJokes.cs similarity index 87% rename from Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs rename to src/Bot/Commands/Randomness/Chuck/ChuckNorrisJokes.cs index 95dcfd4..be328b4 100644 --- a/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs +++ b/src/Bot/Commands/Randomness/Chuck/ChuckNorrisJokes.cs @@ -2,10 +2,10 @@ using System.Net.Http; using System.Threading.Tasks; using Discord.Commands; -using Geekbot.net.Lib; -using Geekbot.net.Lib.ErrorHandling; +using Geekbot.Core; +using Geekbot.Core.ErrorHandling; -namespace Geekbot.net.Commands.Randomness.Chuck +namespace Geekbot.Bot.Commands.Randomness.Chuck { public class ChuckNorrisJokes : ModuleBase { diff --git a/Geekbot.net/Commands/Randomness/Dad/DadJokeResponseDto.cs b/src/Bot/Commands/Randomness/Dad/DadJokeResponseDto.cs similarity index 63% rename from Geekbot.net/Commands/Randomness/Dad/DadJokeResponseDto.cs rename to src/Bot/Commands/Randomness/Dad/DadJokeResponseDto.cs index 262eee9..2d72bdd 100644 --- a/Geekbot.net/Commands/Randomness/Dad/DadJokeResponseDto.cs +++ b/src/Bot/Commands/Randomness/Dad/DadJokeResponseDto.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Commands.Randomness.Dad +namespace Geekbot.Bot.Commands.Randomness.Dad { internal class DadJokeResponseDto { diff --git a/Geekbot.net/Commands/Randomness/Dad/DadJokes.cs b/src/Bot/Commands/Randomness/Dad/DadJokes.cs similarity index 85% rename from Geekbot.net/Commands/Randomness/Dad/DadJokes.cs rename to src/Bot/Commands/Randomness/Dad/DadJokes.cs index 7aabff3..136650c 100644 --- a/Geekbot.net/Commands/Randomness/Dad/DadJokes.cs +++ b/src/Bot/Commands/Randomness/Dad/DadJokes.cs @@ -1,10 +1,10 @@ using System; using System.Threading.Tasks; using Discord.Commands; -using Geekbot.net.Lib; -using Geekbot.net.Lib.ErrorHandling; +using Geekbot.Core; +using Geekbot.Core.ErrorHandling; -namespace Geekbot.net.Commands.Randomness.Dad +namespace Geekbot.Bot.Commands.Randomness.Dad { public class DadJokes : ModuleBase { diff --git a/Geekbot.net/Commands/Randomness/Dog/Dog.cs b/src/Bot/Commands/Randomness/Dog/Dog.cs similarity index 86% rename from Geekbot.net/Commands/Randomness/Dog/Dog.cs rename to src/Bot/Commands/Randomness/Dog/Dog.cs index b3d603b..2a8a3f1 100644 --- a/Geekbot.net/Commands/Randomness/Dog/Dog.cs +++ b/src/Bot/Commands/Randomness/Dog/Dog.cs @@ -2,10 +2,10 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Lib; -using Geekbot.net.Lib.ErrorHandling; +using Geekbot.Core; +using Geekbot.Core.ErrorHandling; -namespace Geekbot.net.Commands.Randomness.Dog +namespace Geekbot.Bot.Commands.Randomness.Dog { public class Dog : ModuleBase { diff --git a/Geekbot.net/Commands/Randomness/Dog/DogResponseDto.cs b/src/Bot/Commands/Randomness/Dog/DogResponseDto.cs similarity index 61% rename from Geekbot.net/Commands/Randomness/Dog/DogResponseDto.cs rename to src/Bot/Commands/Randomness/Dog/DogResponseDto.cs index 1fc1a82..473c1ce 100644 --- a/Geekbot.net/Commands/Randomness/Dog/DogResponseDto.cs +++ b/src/Bot/Commands/Randomness/Dog/DogResponseDto.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Commands.Randomness.Dog +namespace Geekbot.Bot.Commands.Randomness.Dog { internal class DogResponseDto { diff --git a/Geekbot.net/Commands/Randomness/EightBall.cs b/src/Bot/Commands/Randomness/EightBall.cs similarity index 95% rename from Geekbot.net/Commands/Randomness/EightBall.cs rename to src/Bot/Commands/Randomness/EightBall.cs index d07451d..e1a3a71 100644 --- a/Geekbot.net/Commands/Randomness/EightBall.cs +++ b/src/Bot/Commands/Randomness/EightBall.cs @@ -2,9 +2,9 @@ using System.Collections.Generic; using System.Threading.Tasks; using Discord.Commands; -using Geekbot.net.Lib.ErrorHandling; +using Geekbot.Core.ErrorHandling; -namespace Geekbot.net.Commands.Randomness +namespace Geekbot.Bot.Commands.Randomness { public class EightBall : ModuleBase { diff --git a/Geekbot.net/Commands/Randomness/Fortune.cs b/src/Bot/Commands/Randomness/Fortune.cs similarity index 83% rename from Geekbot.net/Commands/Randomness/Fortune.cs rename to src/Bot/Commands/Randomness/Fortune.cs index d4f6b5f..cc31536 100644 --- a/Geekbot.net/Commands/Randomness/Fortune.cs +++ b/src/Bot/Commands/Randomness/Fortune.cs @@ -1,8 +1,8 @@ using System.Threading.Tasks; using Discord.Commands; -using Geekbot.net.Lib.Media; +using Geekbot.Core.Media; -namespace Geekbot.net.Commands.Randomness +namespace Geekbot.Bot.Commands.Randomness { public class Fortune : ModuleBase { diff --git a/Geekbot.net/Commands/Randomness/Gdq.cs b/src/Bot/Commands/Randomness/Gdq.cs similarity index 88% rename from Geekbot.net/Commands/Randomness/Gdq.cs rename to src/Bot/Commands/Randomness/Gdq.cs index 4c16bed..c6d9fa8 100644 --- a/Geekbot.net/Commands/Randomness/Gdq.cs +++ b/src/Bot/Commands/Randomness/Gdq.cs @@ -2,9 +2,9 @@ using System.Net; using System.Threading.Tasks; using Discord.Commands; -using Geekbot.net.Lib.ErrorHandling; +using Geekbot.Core.ErrorHandling; -namespace Geekbot.net.Commands.Randomness +namespace Geekbot.Bot.Commands.Randomness { public class Gdq : ModuleBase { diff --git a/Geekbot.net/Commands/Randomness/Greetings/GreetingBaseDto.cs b/src/Bot/Commands/Randomness/Greetings/GreetingBaseDto.cs similarity index 84% rename from Geekbot.net/Commands/Randomness/Greetings/GreetingBaseDto.cs rename to src/Bot/Commands/Randomness/Greetings/GreetingBaseDto.cs index 7f4e02f..ae0274b 100644 --- a/Geekbot.net/Commands/Randomness/Greetings/GreetingBaseDto.cs +++ b/src/Bot/Commands/Randomness/Greetings/GreetingBaseDto.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Commands.Randomness.Greetings +namespace Geekbot.Bot.Commands.Randomness.Greetings { public class GreetingBaseDto { diff --git a/Geekbot.net/Commands/Randomness/Greetings/GreetingDto.cs b/src/Bot/Commands/Randomness/Greetings/GreetingDto.cs similarity index 80% rename from Geekbot.net/Commands/Randomness/Greetings/GreetingDto.cs rename to src/Bot/Commands/Randomness/Greetings/GreetingDto.cs index c967885..679e544 100644 --- a/Geekbot.net/Commands/Randomness/Greetings/GreetingDto.cs +++ b/src/Bot/Commands/Randomness/Greetings/GreetingDto.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Commands.Randomness.Greetings +namespace Geekbot.Bot.Commands.Randomness.Greetings { public class GreetingDto { diff --git a/Geekbot.net/Commands/Randomness/Greetings/Greetings.cs b/src/Bot/Commands/Randomness/Greetings/Greetings.cs similarity index 90% rename from Geekbot.net/Commands/Randomness/Greetings/Greetings.cs rename to src/Bot/Commands/Randomness/Greetings/Greetings.cs index 85e9152..cd69010 100644 --- a/Geekbot.net/Commands/Randomness/Greetings/Greetings.cs +++ b/src/Bot/Commands/Randomness/Greetings/Greetings.cs @@ -2,11 +2,11 @@ using System; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Lib; -using Geekbot.net.Lib.ErrorHandling; -using Geekbot.net.Lib.Extensions; +using Geekbot.Core; +using Geekbot.Core.ErrorHandling; +using Geekbot.Core.Extensions; -namespace Geekbot.net.Commands.Randomness.Greetings +namespace Geekbot.Bot.Commands.Randomness.Greetings { public class Greetings : ModuleBase { diff --git a/Geekbot.net/Commands/Randomness/Kanye/Kanye.cs b/src/Bot/Commands/Randomness/Kanye/Kanye.cs similarity index 87% rename from Geekbot.net/Commands/Randomness/Kanye/Kanye.cs rename to src/Bot/Commands/Randomness/Kanye/Kanye.cs index 2aca31d..6ad67cb 100644 --- a/Geekbot.net/Commands/Randomness/Kanye/Kanye.cs +++ b/src/Bot/Commands/Randomness/Kanye/Kanye.cs @@ -1,10 +1,10 @@ using System; using System.Threading.Tasks; using Discord.Commands; -using Geekbot.net.Lib; -using Geekbot.net.Lib.ErrorHandling; +using Geekbot.Core; +using Geekbot.Core.ErrorHandling; -namespace Geekbot.net.Commands.Randomness.Kanye +namespace Geekbot.Bot.Commands.Randomness.Kanye { public class Kanye : ModuleBase { diff --git a/Geekbot.net/Commands/Randomness/Kanye/KanyeResponseDto.cs b/src/Bot/Commands/Randomness/Kanye/KanyeResponseDto.cs similarity index 73% rename from Geekbot.net/Commands/Randomness/Kanye/KanyeResponseDto.cs rename to src/Bot/Commands/Randomness/Kanye/KanyeResponseDto.cs index ff74f37..8ca248e 100644 --- a/Geekbot.net/Commands/Randomness/Kanye/KanyeResponseDto.cs +++ b/src/Bot/Commands/Randomness/Kanye/KanyeResponseDto.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Commands.Randomness.Kanye +namespace Geekbot.Bot.Commands.Randomness.Kanye { public class KanyeResponseDto { diff --git a/Geekbot.net/Commands/Randomness/RandomAnimals.cs b/src/Bot/Commands/Randomness/RandomAnimals.cs similarity index 97% rename from Geekbot.net/Commands/Randomness/RandomAnimals.cs rename to src/Bot/Commands/Randomness/RandomAnimals.cs index 47b6ea6..b9c1bdd 100644 --- a/Geekbot.net/Commands/Randomness/RandomAnimals.cs +++ b/src/Bot/Commands/Randomness/RandomAnimals.cs @@ -1,9 +1,9 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Lib.Media; +using Geekbot.Core.Media; -namespace Geekbot.net.Commands.Randomness +namespace Geekbot.Bot.Commands.Randomness { public class RandomAnimals : ModuleBase { diff --git a/Geekbot.net/Commands/Randomness/Ship.cs b/src/Bot/Commands/Randomness/Ship.cs similarity index 91% rename from Geekbot.net/Commands/Randomness/Ship.cs rename to src/Bot/Commands/Randomness/Ship.cs index d5386d4..d5d9eea 100644 --- a/Geekbot.net/Commands/Randomness/Ship.cs +++ b/src/Bot/Commands/Randomness/Ship.cs @@ -3,14 +3,14 @@ using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Database; -using Geekbot.net.Database.Models; -using Geekbot.net.Lib.ErrorHandling; -using Geekbot.net.Lib.Extensions; -using Geekbot.net.Lib.Localization; -using Geekbot.net.Lib.RandomNumberGenerator; +using Geekbot.Core.Database; +using Geekbot.Core.Database.Models; +using Geekbot.Core.ErrorHandling; +using Geekbot.Core.Extensions; +using Geekbot.Core.Localization; +using Geekbot.Core.RandomNumberGenerator; -namespace Geekbot.net.Commands.Randomness +namespace Geekbot.Bot.Commands.Randomness { public class Ship : ModuleBase { diff --git a/Geekbot.net/Commands/Randomness/Slap.cs b/src/Bot/Commands/Randomness/Slap.cs similarity index 93% rename from Geekbot.net/Commands/Randomness/Slap.cs rename to src/Bot/Commands/Randomness/Slap.cs index 20e83c3..b512e73 100644 --- a/Geekbot.net/Commands/Randomness/Slap.cs +++ b/src/Bot/Commands/Randomness/Slap.cs @@ -4,12 +4,12 @@ using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Database; -using Geekbot.net.Database.Models; -using Geekbot.net.Lib.ErrorHandling; -using Geekbot.net.Lib.Extensions; +using Geekbot.Core.Database; +using Geekbot.Core.Database.Models; +using Geekbot.Core.ErrorHandling; +using Geekbot.Core.Extensions; -namespace Geekbot.net.Commands.Randomness +namespace Geekbot.Bot.Commands.Randomness { public class Slap : ModuleBase { diff --git a/Geekbot.net/Commands/Rpg/Cookies.cs b/src/Bot/Commands/Rpg/Cookies.cs similarity index 92% rename from Geekbot.net/Commands/Rpg/Cookies.cs rename to src/Bot/Commands/Rpg/Cookies.cs index b1a2d7d..f581105 100644 --- a/Geekbot.net/Commands/Rpg/Cookies.cs +++ b/src/Bot/Commands/Rpg/Cookies.cs @@ -3,15 +3,15 @@ using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Database; -using Geekbot.net.Database.Models; -using Geekbot.net.Lib.CommandPreconditions; -using Geekbot.net.Lib.ErrorHandling; -using Geekbot.net.Lib.Extensions; -using Geekbot.net.Lib.Localization; -using Geekbot.net.Lib.RandomNumberGenerator; +using Geekbot.Core.CommandPreconditions; +using Geekbot.Core.Database; +using Geekbot.Core.Database.Models; +using Geekbot.Core.ErrorHandling; +using Geekbot.Core.Extensions; +using Geekbot.Core.Localization; +using Geekbot.Core.RandomNumberGenerator; -namespace Geekbot.net.Commands.Rpg +namespace Geekbot.Bot.Commands.Rpg { [DisableInDirectMessage] [Group("cookies")] diff --git a/Geekbot.net/Commands/User/GuildInfo.cs b/src/Bot/Commands/User/GuildInfo.cs similarity index 87% rename from Geekbot.net/Commands/User/GuildInfo.cs rename to src/Bot/Commands/User/GuildInfo.cs index 6c8b941..c596186 100644 --- a/Geekbot.net/Commands/User/GuildInfo.cs +++ b/src/Bot/Commands/User/GuildInfo.cs @@ -3,13 +3,13 @@ using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Database; -using Geekbot.net.Lib.CommandPreconditions; -using Geekbot.net.Lib.ErrorHandling; -using Geekbot.net.Lib.Extensions; -using Geekbot.net.Lib.Levels; +using Geekbot.Core.CommandPreconditions; +using Geekbot.Core.Database; +using Geekbot.Core.ErrorHandling; +using Geekbot.Core.Extensions; +using Geekbot.Core.Levels; -namespace Geekbot.net.Commands.User +namespace Geekbot.Bot.Commands.User { public class GuildInfo : ModuleBase { diff --git a/Geekbot.net/Commands/User/Karma.cs b/src/Bot/Commands/User/Karma.cs similarity index 95% rename from Geekbot.net/Commands/User/Karma.cs rename to src/Bot/Commands/User/Karma.cs index bf85c91..3efc381 100644 --- a/Geekbot.net/Commands/User/Karma.cs +++ b/src/Bot/Commands/User/Karma.cs @@ -3,14 +3,14 @@ using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Database; -using Geekbot.net.Database.Models; -using Geekbot.net.Lib.CommandPreconditions; -using Geekbot.net.Lib.ErrorHandling; -using Geekbot.net.Lib.Extensions; -using Geekbot.net.Lib.Localization; +using Geekbot.Core.CommandPreconditions; +using Geekbot.Core.Database; +using Geekbot.Core.Database.Models; +using Geekbot.Core.ErrorHandling; +using Geekbot.Core.Extensions; +using Geekbot.Core.Localization; -namespace Geekbot.net.Commands.User +namespace Geekbot.Bot.Commands.User { [DisableInDirectMessage] public class Karma : ModuleBase diff --git a/Geekbot.net/Commands/User/Ranking/Rank.cs b/src/Bot/Commands/User/Ranking/Rank.cs similarity index 90% rename from Geekbot.net/Commands/User/Ranking/Rank.cs rename to src/Bot/Commands/User/Ranking/Rank.cs index 9021b4e..ae1bec9 100644 --- a/Geekbot.net/Commands/User/Ranking/Rank.cs +++ b/src/Bot/Commands/User/Ranking/Rank.cs @@ -4,16 +4,16 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Discord.Commands; -using Geekbot.net.Database; -using Geekbot.net.Lib.CommandPreconditions; -using Geekbot.net.Lib.Converters; -using Geekbot.net.Lib.ErrorHandling; -using Geekbot.net.Lib.Extensions; -using Geekbot.net.Lib.Highscores; -using Geekbot.net.Lib.Localization; -using Geekbot.net.Lib.UserRepository; +using Geekbot.Core.CommandPreconditions; +using Geekbot.Core.Converters; +using Geekbot.Core.Database; +using Geekbot.Core.ErrorHandling; +using Geekbot.Core.Extensions; +using Geekbot.Core.Highscores; +using Geekbot.Core.Localization; +using Geekbot.Core.UserRepository; -namespace Geekbot.net.Commands.User.Ranking +namespace Geekbot.Bot.Commands.User.Ranking { public class Rank : ModuleBase { diff --git a/Geekbot.net/Commands/User/Stats.cs b/src/Bot/Commands/User/Stats.cs similarity index 94% rename from Geekbot.net/Commands/User/Stats.cs rename to src/Bot/Commands/User/Stats.cs index 08b69d2..70f6247 100644 --- a/Geekbot.net/Commands/User/Stats.cs +++ b/src/Bot/Commands/User/Stats.cs @@ -3,13 +3,13 @@ using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Database; -using Geekbot.net.Lib.CommandPreconditions; -using Geekbot.net.Lib.ErrorHandling; -using Geekbot.net.Lib.Extensions; -using Geekbot.net.Lib.Levels; +using Geekbot.Core.CommandPreconditions; +using Geekbot.Core.Database; +using Geekbot.Core.ErrorHandling; +using Geekbot.Core.Extensions; +using Geekbot.Core.Levels; -namespace Geekbot.net.Commands.User +namespace Geekbot.Bot.Commands.User { public class Stats : ModuleBase { diff --git a/Geekbot.net/Commands/Utils/AvatarGetter.cs b/src/Bot/Commands/Utils/AvatarGetter.cs similarity index 88% rename from Geekbot.net/Commands/Utils/AvatarGetter.cs rename to src/Bot/Commands/Utils/AvatarGetter.cs index 142b3e3..6585a75 100644 --- a/Geekbot.net/Commands/Utils/AvatarGetter.cs +++ b/src/Bot/Commands/Utils/AvatarGetter.cs @@ -2,9 +2,9 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Lib.ErrorHandling; +using Geekbot.Core.ErrorHandling; -namespace Geekbot.net.Commands.Utils +namespace Geekbot.Bot.Commands.Utils { public class AvatarGetter : ModuleBase { diff --git a/Geekbot.net/Commands/Utils/Changelog/Changelog.cs b/src/Bot/Commands/Utils/Changelog/Changelog.cs similarity index 92% rename from Geekbot.net/Commands/Utils/Changelog/Changelog.cs rename to src/Bot/Commands/Utils/Changelog/Changelog.cs index c217619..92d5ddd 100644 --- a/Geekbot.net/Commands/Utils/Changelog/Changelog.cs +++ b/src/Bot/Commands/Utils/Changelog/Changelog.cs @@ -6,10 +6,10 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Discord.WebSocket; -using Geekbot.net.Lib; -using Geekbot.net.Lib.ErrorHandling; +using Geekbot.Core; +using Geekbot.Core.ErrorHandling; -namespace Geekbot.net.Commands.Utils.Changelog +namespace Geekbot.Bot.Commands.Utils.Changelog { public class Changelog : ModuleBase { diff --git a/Geekbot.net/Commands/Utils/Changelog/CommitAuthorDto.cs b/src/Bot/Commands/Utils/Changelog/CommitAuthorDto.cs similarity index 77% rename from Geekbot.net/Commands/Utils/Changelog/CommitAuthorDto.cs rename to src/Bot/Commands/Utils/Changelog/CommitAuthorDto.cs index 8debd77..7cabece 100644 --- a/Geekbot.net/Commands/Utils/Changelog/CommitAuthorDto.cs +++ b/src/Bot/Commands/Utils/Changelog/CommitAuthorDto.cs @@ -1,6 +1,6 @@ using System; -namespace Geekbot.net.Commands.Utils.Changelog +namespace Geekbot.Bot.Commands.Utils.Changelog { public class CommitAuthorDto { diff --git a/Geekbot.net/Commands/Utils/Changelog/CommitDto.cs b/src/Bot/Commands/Utils/Changelog/CommitDto.cs similarity index 62% rename from Geekbot.net/Commands/Utils/Changelog/CommitDto.cs rename to src/Bot/Commands/Utils/Changelog/CommitDto.cs index 3379697..a534730 100644 --- a/Geekbot.net/Commands/Utils/Changelog/CommitDto.cs +++ b/src/Bot/Commands/Utils/Changelog/CommitDto.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Commands.Utils.Changelog +namespace Geekbot.Bot.Commands.Utils.Changelog { public class CommitDto { diff --git a/Geekbot.net/Commands/Utils/Changelog/CommitInfoDto.cs b/src/Bot/Commands/Utils/Changelog/CommitInfoDto.cs similarity index 71% rename from Geekbot.net/Commands/Utils/Changelog/CommitInfoDto.cs rename to src/Bot/Commands/Utils/Changelog/CommitInfoDto.cs index 9008343..d6f806e 100644 --- a/Geekbot.net/Commands/Utils/Changelog/CommitInfoDto.cs +++ b/src/Bot/Commands/Utils/Changelog/CommitInfoDto.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Commands.Utils.Changelog +namespace Geekbot.Bot.Commands.Utils.Changelog { public class CommitInfoDto { diff --git a/Geekbot.net/Commands/Utils/Choose.cs b/src/Bot/Commands/Utils/Choose.cs similarity index 88% rename from Geekbot.net/Commands/Utils/Choose.cs rename to src/Bot/Commands/Utils/Choose.cs index 62069bd..d245bd2 100644 --- a/Geekbot.net/Commands/Utils/Choose.cs +++ b/src/Bot/Commands/Utils/Choose.cs @@ -1,10 +1,10 @@ using System; using System.Threading.Tasks; using Discord.Commands; -using Geekbot.net.Lib.ErrorHandling; -using Geekbot.net.Lib.Localization; +using Geekbot.Core.ErrorHandling; +using Geekbot.Core.Localization; -namespace Geekbot.net.Commands.Utils +namespace Geekbot.Bot.Commands.Utils { public class Choose : ModuleBase { diff --git a/Geekbot.net/Commands/Utils/Corona/CoronaStats.cs b/src/Bot/Commands/Utils/Corona/CoronaStats.cs similarity index 94% rename from Geekbot.net/Commands/Utils/Corona/CoronaStats.cs rename to src/Bot/Commands/Utils/Corona/CoronaStats.cs index 4be8195..3f9f3ea 100644 --- a/Geekbot.net/Commands/Utils/Corona/CoronaStats.cs +++ b/src/Bot/Commands/Utils/Corona/CoronaStats.cs @@ -1,13 +1,12 @@ using System; -using System.Text; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Lib; -using Geekbot.net.Lib.ErrorHandling; -using Geekbot.net.Lib.Extensions; +using Geekbot.Core; +using Geekbot.Core.ErrorHandling; +using Geekbot.Core.Extensions; -namespace Geekbot.net.Commands.Utils.Corona +namespace Geekbot.Bot.Commands.Utils.Corona { public class CoronaStats : ModuleBase { diff --git a/Geekbot.net/Commands/Utils/Corona/CoronaSummaryDto.cs b/src/Bot/Commands/Utils/Corona/CoronaSummaryDto.cs similarity index 82% rename from Geekbot.net/Commands/Utils/Corona/CoronaSummaryDto.cs rename to src/Bot/Commands/Utils/Corona/CoronaSummaryDto.cs index 5639249..3f6a820 100644 --- a/Geekbot.net/Commands/Utils/Corona/CoronaSummaryDto.cs +++ b/src/Bot/Commands/Utils/Corona/CoronaSummaryDto.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Commands.Utils.Corona +namespace Geekbot.Bot.Commands.Utils.Corona { public class CoronaSummaryDto { diff --git a/Geekbot.net/Commands/Utils/Dice.cs b/src/Bot/Commands/Utils/Dice.cs similarity index 97% rename from Geekbot.net/Commands/Utils/Dice.cs rename to src/Bot/Commands/Utils/Dice.cs index 2cd2e19..0668493 100644 --- a/Geekbot.net/Commands/Utils/Dice.cs +++ b/src/Bot/Commands/Utils/Dice.cs @@ -3,10 +3,10 @@ using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using Discord.Commands; -using Geekbot.net.Lib.DiceParser; -using Geekbot.net.Lib.ErrorHandling; +using Geekbot.Core.DiceParser; +using Geekbot.Core.ErrorHandling; -namespace Geekbot.net.Commands.Utils +namespace Geekbot.Bot.Commands.Utils { public class Dice : ModuleBase { diff --git a/Geekbot.net/Commands/Utils/Emojify.cs b/src/Bot/Commands/Utils/Emojify.cs similarity index 88% rename from Geekbot.net/Commands/Utils/Emojify.cs rename to src/Bot/Commands/Utils/Emojify.cs index 5355aff..c8328ee 100644 --- a/Geekbot.net/Commands/Utils/Emojify.cs +++ b/src/Bot/Commands/Utils/Emojify.cs @@ -1,10 +1,10 @@ using System; using System.Threading.Tasks; using Discord.Commands; -using Geekbot.net.Lib.Converters; -using Geekbot.net.Lib.ErrorHandling; +using Geekbot.Core.Converters; +using Geekbot.Core.ErrorHandling; -namespace Geekbot.net.Commands.Utils +namespace Geekbot.Bot.Commands.Utils { public class Emojify : ModuleBase { diff --git a/Geekbot.net/Commands/Utils/Help.cs b/src/Bot/Commands/Utils/Help.cs similarity index 93% rename from Geekbot.net/Commands/Utils/Help.cs rename to src/Bot/Commands/Utils/Help.cs index a6f0084..58630fe 100644 --- a/Geekbot.net/Commands/Utils/Help.cs +++ b/src/Bot/Commands/Utils/Help.cs @@ -3,9 +3,9 @@ using System.Text; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Lib.ErrorHandling; +using Geekbot.Core.ErrorHandling; -namespace Geekbot.net.Commands.Utils +namespace Geekbot.Bot.Commands.Utils { public class Help : ModuleBase { diff --git a/Geekbot.net/Commands/Utils/Info.cs b/src/Bot/Commands/Utils/Info.cs similarity index 92% rename from Geekbot.net/Commands/Utils/Info.cs rename to src/Bot/Commands/Utils/Info.cs index b1d5403..663dd07 100644 --- a/Geekbot.net/Commands/Utils/Info.cs +++ b/src/Bot/Commands/Utils/Info.cs @@ -5,11 +5,11 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Discord.WebSocket; -using Geekbot.net.Lib; -using Geekbot.net.Lib.ErrorHandling; -using Geekbot.net.Lib.Extensions; +using Geekbot.Core; +using Geekbot.Core.ErrorHandling; +using Geekbot.Core.Extensions; -namespace Geekbot.net.Commands.Utils +namespace Geekbot.Bot.Commands.Utils { public class Info : ModuleBase { diff --git a/Geekbot.net/Commands/Utils/Lmgtfy.cs b/src/Bot/Commands/Utils/Lmgtfy.cs similarity index 92% rename from Geekbot.net/Commands/Utils/Lmgtfy.cs rename to src/Bot/Commands/Utils/Lmgtfy.cs index 9976dad..6063cf9 100644 --- a/Geekbot.net/Commands/Utils/Lmgtfy.cs +++ b/src/Bot/Commands/Utils/Lmgtfy.cs @@ -2,9 +2,9 @@ using System; using System.Threading.Tasks; using System.Web; using Discord.Commands; -using Geekbot.net.Lib.ErrorHandling; +using Geekbot.Core.ErrorHandling; -namespace Geekbot.net.Commands.Utils +namespace Geekbot.Bot.Commands.Utils { public class Lmgtfy : ModuleBase { diff --git a/Geekbot.net/Commands/Utils/Ping.cs b/src/Bot/Commands/Utils/Ping.cs similarity index 89% rename from Geekbot.net/Commands/Utils/Ping.cs rename to src/Bot/Commands/Utils/Ping.cs index 18cf9cb..d4faa53 100644 --- a/Geekbot.net/Commands/Utils/Ping.cs +++ b/src/Bot/Commands/Utils/Ping.cs @@ -1,7 +1,7 @@ using System.Threading.Tasks; using Discord.Commands; -namespace Geekbot.net.Commands.Utils +namespace Geekbot.Bot.Commands.Utils { public class Ping : ModuleBase { diff --git a/Geekbot.net/Commands/Utils/Quote/Quote.cs b/src/Bot/Commands/Utils/Quote/Quote.cs similarity index 97% rename from Geekbot.net/Commands/Utils/Quote/Quote.cs rename to src/Bot/Commands/Utils/Quote/Quote.cs index 7af8a4c..4022753 100644 --- a/Geekbot.net/Commands/Utils/Quote/Quote.cs +++ b/src/Bot/Commands/Utils/Quote/Quote.cs @@ -3,16 +3,16 @@ using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; -using Geekbot.net.Database; -using Geekbot.net.Database.Models; -using Geekbot.net.Lib.CommandPreconditions; -using Geekbot.net.Lib.ErrorHandling; -using Geekbot.net.Lib.Extensions; -using Geekbot.net.Lib.Localization; -using Geekbot.net.Lib.Polyfills; -using Geekbot.net.Lib.RandomNumberGenerator; +using Geekbot.Core.CommandPreconditions; +using Geekbot.Core.Database; +using Geekbot.Core.Database.Models; +using Geekbot.Core.ErrorHandling; +using Geekbot.Core.Extensions; +using Geekbot.Core.Localization; +using Geekbot.Core.Polyfills; +using Geekbot.Core.RandomNumberGenerator; -namespace Geekbot.net.Commands.Utils.Quote +namespace Geekbot.Bot.Commands.Utils.Quote { [Group("quote")] [DisableInDirectMessage] diff --git a/Geekbot.net/Commands/Utils/Quote/QuoteObjectDto.cs b/src/Bot/Commands/Utils/Quote/QuoteObjectDto.cs similarity index 81% rename from Geekbot.net/Commands/Utils/Quote/QuoteObjectDto.cs rename to src/Bot/Commands/Utils/Quote/QuoteObjectDto.cs index a37ff76..32b65cf 100644 --- a/Geekbot.net/Commands/Utils/Quote/QuoteObjectDto.cs +++ b/src/Bot/Commands/Utils/Quote/QuoteObjectDto.cs @@ -1,6 +1,6 @@ using System; -namespace Geekbot.net.Commands.Utils.Quote +namespace Geekbot.Bot.Commands.Utils.Quote { internal class QuoteObjectDto { diff --git a/Geekbot.net/Handlers/CommandHandler.cs b/src/Bot/Handlers/CommandHandler.cs similarity index 96% rename from Geekbot.net/Handlers/CommandHandler.cs rename to src/Bot/Handlers/CommandHandler.cs index b6a3d9f..52e9a52 100644 --- a/Geekbot.net/Handlers/CommandHandler.cs +++ b/src/Bot/Handlers/CommandHandler.cs @@ -5,12 +5,12 @@ using Discord; using Discord.Commands; using Discord.Rest; using Discord.WebSocket; -using Geekbot.net.Database; -using Geekbot.net.Database.Models; -using Geekbot.net.Lib.GuildSettingsManager; -using Geekbot.net.Lib.Logger; +using Geekbot.Core.Database; +using Geekbot.Core.Database.Models; +using Geekbot.Core.GuildSettingsManager; +using Geekbot.Core.Logger; -namespace Geekbot.net.Handlers +namespace Geekbot.Bot.Handlers { public class CommandHandler { diff --git a/Geekbot.net/Handlers/MessageDeletedHandler.cs b/src/Bot/Handlers/MessageDeletedHandler.cs similarity index 93% rename from Geekbot.net/Handlers/MessageDeletedHandler.cs rename to src/Bot/Handlers/MessageDeletedHandler.cs index 5595cff..d0377f7 100644 --- a/Geekbot.net/Handlers/MessageDeletedHandler.cs +++ b/src/Bot/Handlers/MessageDeletedHandler.cs @@ -4,11 +4,11 @@ using System.Text; using System.Threading.Tasks; using Discord; using Discord.WebSocket; -using Geekbot.net.Database; -using Geekbot.net.Lib.Extensions; -using Geekbot.net.Lib.Logger; +using Geekbot.Core.Database; +using Geekbot.Core.Extensions; +using Geekbot.Core.Logger; -namespace Geekbot.net.Handlers +namespace Geekbot.Bot.Handlers { public class MessageDeletedHandler { diff --git a/Geekbot.net/Handlers/ReactionHandler.cs b/src/Bot/Handlers/ReactionHandler.cs similarity index 94% rename from Geekbot.net/Handlers/ReactionHandler.cs rename to src/Bot/Handlers/ReactionHandler.cs index e2355a8..66af3e8 100644 --- a/Geekbot.net/Handlers/ReactionHandler.cs +++ b/src/Bot/Handlers/ReactionHandler.cs @@ -1,9 +1,9 @@ using System.Threading.Tasks; using Discord; using Discord.WebSocket; -using Geekbot.net.Lib.ReactionListener; +using Geekbot.Core.ReactionListener; -namespace Geekbot.net.Handlers +namespace Geekbot.Bot.Handlers { public class ReactionHandler { diff --git a/Geekbot.net/Handlers/StatsHandler.cs b/src/Bot/Handlers/StatsHandler.cs similarity index 92% rename from Geekbot.net/Handlers/StatsHandler.cs rename to src/Bot/Handlers/StatsHandler.cs index 56c0894..197bbb8 100644 --- a/Geekbot.net/Handlers/StatsHandler.cs +++ b/src/Bot/Handlers/StatsHandler.cs @@ -1,13 +1,13 @@ using System; using System.Threading.Tasks; using Discord.WebSocket; -using Geekbot.net.Database; -using Geekbot.net.Database.Models; -using Geekbot.net.Lib.Extensions; -using Geekbot.net.Lib.Logger; +using Geekbot.Core.Database; +using Geekbot.Core.Database.Models; +using Geekbot.Core.Extensions; +using Geekbot.Core.Logger; using Microsoft.EntityFrameworkCore; -namespace Geekbot.net.Handlers +namespace Geekbot.Bot.Handlers { public class StatsHandler { diff --git a/Geekbot.net/Handlers/UserHandler.cs b/src/Bot/Handlers/UserHandler.cs similarity index 95% rename from Geekbot.net/Handlers/UserHandler.cs rename to src/Bot/Handlers/UserHandler.cs index fb26d70..a22d0e1 100644 --- a/Geekbot.net/Handlers/UserHandler.cs +++ b/src/Bot/Handlers/UserHandler.cs @@ -4,12 +4,12 @@ using System.Threading.Tasks; using Discord; using Discord.Rest; using Discord.WebSocket; -using Geekbot.net.Database; -using Geekbot.net.Lib.Extensions; -using Geekbot.net.Lib.Logger; -using Geekbot.net.Lib.UserRepository; +using Geekbot.Core.Database; +using Geekbot.Core.Extensions; +using Geekbot.Core.Logger; +using Geekbot.Core.UserRepository; -namespace Geekbot.net.Handlers +namespace Geekbot.Bot.Handlers { public class UserHandler { diff --git a/Geekbot.net/Logs/.keep b/src/Bot/Logs/.keep old mode 100755 new mode 100644 similarity index 100% rename from Geekbot.net/Logs/.keep rename to src/Bot/Logs/.keep diff --git a/Geekbot.net/Program.cs b/src/Bot/Program.cs old mode 100755 new mode 100644 similarity index 91% rename from Geekbot.net/Program.cs rename to src/Bot/Program.cs index c55f390..04b1450 --- a/Geekbot.net/Program.cs +++ b/src/Bot/Program.cs @@ -6,30 +6,30 @@ using CommandLine; using Discord; using Discord.Commands; using Discord.WebSocket; -using Geekbot.net.Database; -using Geekbot.net.Handlers; -using Geekbot.net.Lib; -using Geekbot.net.Lib.Converters; -using Geekbot.net.Lib.DiceParser; -using Geekbot.net.Lib.ErrorHandling; -using Geekbot.net.Lib.GlobalSettings; -using Geekbot.net.Lib.GuildSettingsManager; -using Geekbot.net.Lib.Highscores; -using Geekbot.net.Lib.KvInMemoryStore; -using Geekbot.net.Lib.Levels; -using Geekbot.net.Lib.Localization; -using Geekbot.net.Lib.Logger; -using Geekbot.net.Lib.MalClient; -using Geekbot.net.Lib.Media; -using Geekbot.net.Lib.RandomNumberGenerator; -using Geekbot.net.Lib.ReactionListener; -using Geekbot.net.Lib.UserRepository; -using Geekbot.net.Lib.WikipediaClient; -using Geekbot.net.WebApi; +using Geekbot.Bot.Handlers; +using Geekbot.Core; +using Geekbot.Core.Converters; +using Geekbot.Core.Database; +using Geekbot.Core.DiceParser; +using Geekbot.Core.ErrorHandling; +using Geekbot.Core.GlobalSettings; +using Geekbot.Core.GuildSettingsManager; +using Geekbot.Core.Highscores; +using Geekbot.Core.KvInMemoryStore; +using Geekbot.Core.Levels; +using Geekbot.Core.Localization; +using Geekbot.Core.Logger; +using Geekbot.Core.MalClient; +using Geekbot.Core.Media; +using Geekbot.Core.RandomNumberGenerator; +using Geekbot.Core.ReactionListener; +using Geekbot.Core.UserRepository; +using Geekbot.Core.WikipediaClient; +using Geekbot.Web; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; -namespace Geekbot.net +namespace Geekbot.Bot { internal class Program { diff --git a/Geekbot.net/Storage/croissant b/src/Bot/Storage/croissant similarity index 100% rename from Geekbot.net/Storage/croissant rename to src/Bot/Storage/croissant diff --git a/Geekbot.net/Storage/dab b/src/Bot/Storage/dab similarity index 100% rename from Geekbot.net/Storage/dab rename to src/Bot/Storage/dab diff --git a/Geekbot.net/Storage/fortunes b/src/Bot/Storage/fortunes similarity index 100% rename from Geekbot.net/Storage/fortunes rename to src/Bot/Storage/fortunes diff --git a/Geekbot.net/Storage/foxes b/src/Bot/Storage/foxes similarity index 100% rename from Geekbot.net/Storage/foxes rename to src/Bot/Storage/foxes diff --git a/Geekbot.net/Storage/pandas b/src/Bot/Storage/pandas similarity index 100% rename from Geekbot.net/Storage/pandas rename to src/Bot/Storage/pandas diff --git a/Geekbot.net/Storage/penguins b/src/Bot/Storage/penguins similarity index 100% rename from Geekbot.net/Storage/penguins rename to src/Bot/Storage/penguins diff --git a/Geekbot.net/Storage/pumpkin b/src/Bot/Storage/pumpkin similarity index 100% rename from Geekbot.net/Storage/pumpkin rename to src/Bot/Storage/pumpkin diff --git a/Geekbot.net/Storage/squirrel b/src/Bot/Storage/squirrel similarity index 100% rename from Geekbot.net/Storage/squirrel rename to src/Bot/Storage/squirrel diff --git a/Geekbot.net/Storage/turtles b/src/Bot/Storage/turtles similarity index 100% rename from Geekbot.net/Storage/turtles rename to src/Bot/Storage/turtles diff --git a/Geekbot.net/derp.ico b/src/Bot/derp.ico similarity index 100% rename from Geekbot.net/derp.ico rename to src/Bot/derp.ico diff --git a/Geekbot.net/Lib/CommandPreconditions/DisableInDirectMessageAttribute.cs b/src/Core/CommandPreconditions/DisableInDirectMessageAttribute.cs similarity index 92% rename from Geekbot.net/Lib/CommandPreconditions/DisableInDirectMessageAttribute.cs rename to src/Core/CommandPreconditions/DisableInDirectMessageAttribute.cs index 2bb5cfd..835e0b3 100644 --- a/Geekbot.net/Lib/CommandPreconditions/DisableInDirectMessageAttribute.cs +++ b/src/Core/CommandPreconditions/DisableInDirectMessageAttribute.cs @@ -2,7 +2,7 @@ using System; using System.Threading.Tasks; using Discord.Commands; -namespace Geekbot.net.Lib.CommandPreconditions +namespace Geekbot.Core.CommandPreconditions { [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)] public class DisableInDirectMessageAttribute : PreconditionAttribute diff --git a/Geekbot.net/Lib/Constants.cs b/src/Core/Constants.cs similarity index 72% rename from Geekbot.net/Lib/Constants.cs rename to src/Core/Constants.cs index 4abee4e..4dd08b9 100644 --- a/Geekbot.net/Lib/Constants.cs +++ b/src/Core/Constants.cs @@ -1,6 +1,6 @@ using System.Reflection; -namespace Geekbot.net.Lib +namespace Geekbot.Core { public static class Constants { @@ -8,7 +8,7 @@ namespace Geekbot.net.Lib public static string BotVersion() { - return typeof(Program).Assembly.GetCustomAttribute().InformationalVersion; + return typeof(Constants).Assembly.GetCustomAttribute().InformationalVersion; } public static string LibraryVersion() diff --git a/Geekbot.net/Lib/Converters/EmojiConverter.cs b/src/Core/Converters/EmojiConverter.cs similarity index 96% rename from Geekbot.net/Lib/Converters/EmojiConverter.cs rename to src/Core/Converters/EmojiConverter.cs index 06f1aa8..327aab8 100644 --- a/Geekbot.net/Lib/Converters/EmojiConverter.cs +++ b/src/Core/Converters/EmojiConverter.cs @@ -1,7 +1,7 @@ using System.Collections; using System.Text; -namespace Geekbot.net.Lib.Converters +namespace Geekbot.Core.Converters { public class EmojiConverter : IEmojiConverter { diff --git a/Geekbot.net/Lib/Converters/IEmojiConverter.cs b/src/Core/Converters/IEmojiConverter.cs similarity index 74% rename from Geekbot.net/Lib/Converters/IEmojiConverter.cs rename to src/Core/Converters/IEmojiConverter.cs index b0f666f..79ca0a7 100644 --- a/Geekbot.net/Lib/Converters/IEmojiConverter.cs +++ b/src/Core/Converters/IEmojiConverter.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Lib.Converters +namespace Geekbot.Core.Converters { public interface IEmojiConverter { diff --git a/Geekbot.net/Lib/Converters/IMtgManaConverter.cs b/src/Core/Converters/IMtgManaConverter.cs similarity index 67% rename from Geekbot.net/Lib/Converters/IMtgManaConverter.cs rename to src/Core/Converters/IMtgManaConverter.cs index d558f09..0dd3034 100644 --- a/Geekbot.net/Lib/Converters/IMtgManaConverter.cs +++ b/src/Core/Converters/IMtgManaConverter.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Lib.Converters +namespace Geekbot.Core.Converters { public interface IMtgManaConverter { diff --git a/Geekbot.net/Lib/Converters/MtgManaConverter.cs b/src/Core/Converters/MtgManaConverter.cs similarity index 96% rename from Geekbot.net/Lib/Converters/MtgManaConverter.cs rename to src/Core/Converters/MtgManaConverter.cs index cfd893b..aa6b74c 100644 --- a/Geekbot.net/Lib/Converters/MtgManaConverter.cs +++ b/src/Core/Converters/MtgManaConverter.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using System.Text.RegularExpressions; -namespace Geekbot.net.Lib.Converters +namespace Geekbot.Core.Converters { public class MtgManaConverter : IMtgManaConverter { diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj new file mode 100644 index 0000000..fcbce09 --- /dev/null +++ b/src/Core/Core.csproj @@ -0,0 +1,40 @@ + + + + net5.0 + $(VersionSuffix) + $(VersionSuffix) + 0.0.0-DEV + Geekbot.Core + Geekbot.Core + NU1701 + + + + + + + + + + + + + + + + + + + + + + + + + + PreserveNewest + + + + diff --git a/Geekbot.net/Database/DatabaseContext.cs b/src/Core/Database/DatabaseContext.cs similarity index 91% rename from Geekbot.net/Database/DatabaseContext.cs rename to src/Core/Database/DatabaseContext.cs index 505486f..93d46d3 100644 --- a/Geekbot.net/Database/DatabaseContext.cs +++ b/src/Core/Database/DatabaseContext.cs @@ -1,7 +1,7 @@ -using Geekbot.net.Database.Models; +using Geekbot.Core.Database.Models; using Microsoft.EntityFrameworkCore; -namespace Geekbot.net.Database +namespace Geekbot.Core.Database { public class DatabaseContext : DbContext { diff --git a/Geekbot.net/Database/DatabaseInitializer.cs b/src/Core/Database/DatabaseInitializer.cs similarity index 93% rename from Geekbot.net/Database/DatabaseInitializer.cs rename to src/Core/Database/DatabaseInitializer.cs index 9f60d8a..37f07bc 100644 --- a/Geekbot.net/Database/DatabaseInitializer.cs +++ b/src/Core/Database/DatabaseInitializer.cs @@ -1,10 +1,9 @@ using System; -using Geekbot.net.Database.LoggingAdapter; -using Geekbot.net.Lib; -using Geekbot.net.Lib.Logger; +using Geekbot.Core.Database.LoggingAdapter; +using Geekbot.Core.Logger; using Npgsql.Logging; -namespace Geekbot.net.Database +namespace Geekbot.Core.Database { public class DatabaseInitializer { diff --git a/Geekbot.net/Database/InMemoryDatabase.cs b/src/Core/Database/InMemoryDatabase.cs similarity index 89% rename from Geekbot.net/Database/InMemoryDatabase.cs rename to src/Core/Database/InMemoryDatabase.cs index 2b2cfd0..1077aea 100644 --- a/Geekbot.net/Database/InMemoryDatabase.cs +++ b/src/Core/Database/InMemoryDatabase.cs @@ -1,6 +1,6 @@ using Microsoft.EntityFrameworkCore; -namespace Geekbot.net.Database +namespace Geekbot.Core.Database { public class InMemoryDatabase : DatabaseContext { diff --git a/Geekbot.net/Database/LoggingAdapter/NpgsqlLoggingAdapter.cs b/src/Core/Database/LoggingAdapter/NpgsqlLoggingAdapter.cs similarity index 93% rename from Geekbot.net/Database/LoggingAdapter/NpgsqlLoggingAdapter.cs rename to src/Core/Database/LoggingAdapter/NpgsqlLoggingAdapter.cs index ceebd56..8a46c0d 100644 --- a/Geekbot.net/Database/LoggingAdapter/NpgsqlLoggingAdapter.cs +++ b/src/Core/Database/LoggingAdapter/NpgsqlLoggingAdapter.cs @@ -1,10 +1,9 @@ using System; -using Geekbot.net.Lib; -using Geekbot.net.Lib.Logger; +using Geekbot.Core.Logger; using Npgsql.Logging; using LogLevel = NLog.LogLevel; -namespace Geekbot.net.Database.LoggingAdapter +namespace Geekbot.Core.Database.LoggingAdapter { public class NpgsqlLoggingAdapter : NpgsqlLogger { diff --git a/Geekbot.net/Database/LoggingAdapter/NpgsqlLoggingProviderAdapter.cs b/src/Core/Database/LoggingAdapter/NpgsqlLoggingProviderAdapter.cs similarity index 82% rename from Geekbot.net/Database/LoggingAdapter/NpgsqlLoggingProviderAdapter.cs rename to src/Core/Database/LoggingAdapter/NpgsqlLoggingProviderAdapter.cs index efe82a3..c4f76d7 100644 --- a/Geekbot.net/Database/LoggingAdapter/NpgsqlLoggingProviderAdapter.cs +++ b/src/Core/Database/LoggingAdapter/NpgsqlLoggingProviderAdapter.cs @@ -1,8 +1,7 @@ -using Geekbot.net.Lib; -using Geekbot.net.Lib.Logger; +using Geekbot.Core.Logger; using Npgsql.Logging; -namespace Geekbot.net.Database.LoggingAdapter +namespace Geekbot.Core.Database.LoggingAdapter { public class NpgsqlLoggingProviderAdapter : INpgsqlLoggingProvider { diff --git a/Geekbot.net/Database/Models/CookiesModel.cs b/src/Core/Database/Models/CookiesModel.cs similarity index 87% rename from Geekbot.net/Database/Models/CookiesModel.cs rename to src/Core/Database/Models/CookiesModel.cs index 228f3b0..6ec71f9 100644 --- a/Geekbot.net/Database/Models/CookiesModel.cs +++ b/src/Core/Database/Models/CookiesModel.cs @@ -1,7 +1,7 @@ using System; using System.ComponentModel.DataAnnotations; -namespace Geekbot.net.Database.Models +namespace Geekbot.Core.Database.Models { public class CookiesModel { diff --git a/Geekbot.net/Database/Models/GlobalsModel.cs b/src/Core/Database/Models/GlobalsModel.cs similarity index 85% rename from Geekbot.net/Database/Models/GlobalsModel.cs rename to src/Core/Database/Models/GlobalsModel.cs index 63261fc..af084f9 100644 --- a/Geekbot.net/Database/Models/GlobalsModel.cs +++ b/src/Core/Database/Models/GlobalsModel.cs @@ -1,6 +1,6 @@ using System.ComponentModel.DataAnnotations; -namespace Geekbot.net.Database.Models +namespace Geekbot.Core.Database.Models { public class GlobalsModel { diff --git a/Geekbot.net/Database/Models/GuildSettingsModel.cs b/src/Core/Database/Models/GuildSettingsModel.cs similarity index 90% rename from Geekbot.net/Database/Models/GuildSettingsModel.cs rename to src/Core/Database/Models/GuildSettingsModel.cs index ebb6dc4..80655f7 100644 --- a/Geekbot.net/Database/Models/GuildSettingsModel.cs +++ b/src/Core/Database/Models/GuildSettingsModel.cs @@ -1,6 +1,6 @@ using System.ComponentModel.DataAnnotations; -namespace Geekbot.net.Database.Models +namespace Geekbot.Core.Database.Models { public class GuildSettingsModel { diff --git a/Geekbot.net/Database/Models/KarmaModel.cs b/src/Core/Database/Models/KarmaModel.cs similarity index 87% rename from Geekbot.net/Database/Models/KarmaModel.cs rename to src/Core/Database/Models/KarmaModel.cs index 7d5f533..2ec64f0 100644 --- a/Geekbot.net/Database/Models/KarmaModel.cs +++ b/src/Core/Database/Models/KarmaModel.cs @@ -1,7 +1,7 @@ using System; using System.ComponentModel.DataAnnotations; -namespace Geekbot.net.Database.Models +namespace Geekbot.Core.Database.Models { public class KarmaModel { diff --git a/Geekbot.net/Database/Models/MessagesModel.cs b/src/Core/Database/Models/MessagesModel.cs similarity index 85% rename from Geekbot.net/Database/Models/MessagesModel.cs rename to src/Core/Database/Models/MessagesModel.cs index 31332e8..87731d8 100644 --- a/Geekbot.net/Database/Models/MessagesModel.cs +++ b/src/Core/Database/Models/MessagesModel.cs @@ -1,6 +1,6 @@ using System.ComponentModel.DataAnnotations; -namespace Geekbot.net.Database.Models +namespace Geekbot.Core.Database.Models { public class MessagesModel { diff --git a/Geekbot.net/Database/Models/QuoteModel.cs b/src/Core/Database/Models/QuoteModel.cs similarity index 93% rename from Geekbot.net/Database/Models/QuoteModel.cs rename to src/Core/Database/Models/QuoteModel.cs index 98e8b21..3d356d2 100644 --- a/Geekbot.net/Database/Models/QuoteModel.cs +++ b/src/Core/Database/Models/QuoteModel.cs @@ -1,7 +1,7 @@ using System; using System.ComponentModel.DataAnnotations; -namespace Geekbot.net.Database.Models +namespace Geekbot.Core.Database.Models { public class QuoteModel { diff --git a/Geekbot.net/Database/Models/ReactionListenerModel.cs b/src/Core/Database/Models/ReactionListenerModel.cs similarity index 91% rename from Geekbot.net/Database/Models/ReactionListenerModel.cs rename to src/Core/Database/Models/ReactionListenerModel.cs index 05ab5b3..5ec28be 100644 --- a/Geekbot.net/Database/Models/ReactionListenerModel.cs +++ b/src/Core/Database/Models/ReactionListenerModel.cs @@ -1,6 +1,6 @@ using System.ComponentModel.DataAnnotations; -namespace Geekbot.net.Database.Models +namespace Geekbot.Core.Database.Models { public class ReactionListenerModel { diff --git a/Geekbot.net/Database/Models/RoleSelfServiceModel.cs b/src/Core/Database/Models/RoleSelfServiceModel.cs similarity index 85% rename from Geekbot.net/Database/Models/RoleSelfServiceModel.cs rename to src/Core/Database/Models/RoleSelfServiceModel.cs index de8c341..4aa92c4 100644 --- a/Geekbot.net/Database/Models/RoleSelfServiceModel.cs +++ b/src/Core/Database/Models/RoleSelfServiceModel.cs @@ -1,6 +1,6 @@ using System.ComponentModel.DataAnnotations; -namespace Geekbot.net.Database.Models +namespace Geekbot.Core.Database.Models { public class RoleSelfServiceModel { diff --git a/Geekbot.net/Database/Models/RollsModel.cs b/src/Core/Database/Models/RollsModel.cs similarity index 85% rename from Geekbot.net/Database/Models/RollsModel.cs rename to src/Core/Database/Models/RollsModel.cs index de9b82e..aa6613d 100644 --- a/Geekbot.net/Database/Models/RollsModel.cs +++ b/src/Core/Database/Models/RollsModel.cs @@ -1,6 +1,6 @@ using System.ComponentModel.DataAnnotations; -namespace Geekbot.net.Database.Models +namespace Geekbot.Core.Database.Models { public class RollsModel { diff --git a/Geekbot.net/Database/Models/ShipsModel.cs b/src/Core/Database/Models/ShipsModel.cs similarity index 84% rename from Geekbot.net/Database/Models/ShipsModel.cs rename to src/Core/Database/Models/ShipsModel.cs index d8156b0..23cf367 100644 --- a/Geekbot.net/Database/Models/ShipsModel.cs +++ b/src/Core/Database/Models/ShipsModel.cs @@ -1,6 +1,6 @@ using System.ComponentModel.DataAnnotations; -namespace Geekbot.net.Database.Models +namespace Geekbot.Core.Database.Models { public class ShipsModel { diff --git a/Geekbot.net/Database/Models/SlapsModel.cs b/src/Core/Database/Models/SlapsModel.cs similarity index 86% rename from Geekbot.net/Database/Models/SlapsModel.cs rename to src/Core/Database/Models/SlapsModel.cs index 6c402aa..09026c8 100644 --- a/Geekbot.net/Database/Models/SlapsModel.cs +++ b/src/Core/Database/Models/SlapsModel.cs @@ -1,6 +1,6 @@ using System.ComponentModel.DataAnnotations; -namespace Geekbot.net.Database.Models +namespace Geekbot.Core.Database.Models { public class SlapsModel { diff --git a/Geekbot.net/Database/Models/UserModel.cs b/src/Core/Database/Models/UserModel.cs similarity index 84% rename from Geekbot.net/Database/Models/UserModel.cs rename to src/Core/Database/Models/UserModel.cs index 43d3a9a..aa6281d 100644 --- a/Geekbot.net/Database/Models/UserModel.cs +++ b/src/Core/Database/Models/UserModel.cs @@ -1,8 +1,7 @@ using System; -using System.Collections.Generic; using System.ComponentModel.DataAnnotations; -namespace Geekbot.net.Database.Models +namespace Geekbot.Core.Database.Models { public class UserModel { diff --git a/Geekbot.net/Database/SqlConnectionString.cs b/src/Core/Database/SqlConnectionString.cs similarity index 94% rename from Geekbot.net/Database/SqlConnectionString.cs rename to src/Core/Database/SqlConnectionString.cs index 61804cf..ca2858d 100644 --- a/Geekbot.net/Database/SqlConnectionString.cs +++ b/src/Core/Database/SqlConnectionString.cs @@ -1,6 +1,6 @@ using System.Text; -namespace Geekbot.net.Database +namespace Geekbot.Core.Database { public class SqlConnectionString { diff --git a/Geekbot.net/Database/SqlDatabase.cs b/src/Core/Database/SqlDatabase.cs similarity index 90% rename from Geekbot.net/Database/SqlDatabase.cs rename to src/Core/Database/SqlDatabase.cs index e6d03d4..8a4d195 100644 --- a/Geekbot.net/Database/SqlDatabase.cs +++ b/src/Core/Database/SqlDatabase.cs @@ -1,6 +1,6 @@ using Microsoft.EntityFrameworkCore; -namespace Geekbot.net.Database +namespace Geekbot.Core.Database { public class SqlDatabase : DatabaseContext { diff --git a/Geekbot.net/Lib/DiceParser/DiceException.cs b/src/Core/DiceParser/DiceException.cs similarity index 84% rename from Geekbot.net/Lib/DiceParser/DiceException.cs rename to src/Core/DiceParser/DiceException.cs index d2a66ad..6c6c29c 100644 --- a/Geekbot.net/Lib/DiceParser/DiceException.cs +++ b/src/Core/DiceParser/DiceException.cs @@ -1,6 +1,6 @@ using System; -namespace Geekbot.net.Lib.DiceParser +namespace Geekbot.Core.DiceParser { public class DiceException : Exception { diff --git a/Geekbot.net/Lib/DiceParser/DiceInput.cs b/src/Core/DiceParser/DiceInput.cs similarity index 88% rename from Geekbot.net/Lib/DiceParser/DiceInput.cs rename to src/Core/DiceParser/DiceInput.cs index baca42c..60f0bd2 100644 --- a/Geekbot.net/Lib/DiceParser/DiceInput.cs +++ b/src/Core/DiceParser/DiceInput.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; -namespace Geekbot.net.Lib.DiceParser +namespace Geekbot.Core.DiceParser { public class DiceInput { diff --git a/Geekbot.net/Lib/DiceParser/DiceInputOptions.cs b/src/Core/DiceParser/DiceInputOptions.cs similarity index 71% rename from Geekbot.net/Lib/DiceParser/DiceInputOptions.cs rename to src/Core/DiceParser/DiceInputOptions.cs index 41f2def..5606a5e 100644 --- a/Geekbot.net/Lib/DiceParser/DiceInputOptions.cs +++ b/src/Core/DiceParser/DiceInputOptions.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Lib.DiceParser +namespace Geekbot.Core.DiceParser { public struct DiceInputOptions { diff --git a/Geekbot.net/Lib/DiceParser/DiceParser.cs b/src/Core/DiceParser/DiceParser.cs similarity index 97% rename from Geekbot.net/Lib/DiceParser/DiceParser.cs rename to src/Core/DiceParser/DiceParser.cs index 7f90865..b0f6a88 100644 --- a/Geekbot.net/Lib/DiceParser/DiceParser.cs +++ b/src/Core/DiceParser/DiceParser.cs @@ -1,9 +1,9 @@ using System; using System.Linq; using System.Text.RegularExpressions; -using Geekbot.net.Lib.RandomNumberGenerator; +using Geekbot.Core.RandomNumberGenerator; -namespace Geekbot.net.Lib.DiceParser +namespace Geekbot.Core.DiceParser { public class DiceParser : IDiceParser { diff --git a/Geekbot.net/Lib/DiceParser/DieAdvantageType.cs b/src/Core/DiceParser/DieAdvantageType.cs similarity index 73% rename from Geekbot.net/Lib/DiceParser/DieAdvantageType.cs rename to src/Core/DiceParser/DieAdvantageType.cs index 91818cb..9bd7486 100644 --- a/Geekbot.net/Lib/DiceParser/DieAdvantageType.cs +++ b/src/Core/DiceParser/DieAdvantageType.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Lib.DiceParser +namespace Geekbot.Core.DiceParser { public enum DieAdvantageType { diff --git a/Geekbot.net/Lib/DiceParser/DieResult.cs b/src/Core/DiceParser/DieResult.cs similarity index 96% rename from Geekbot.net/Lib/DiceParser/DieResult.cs rename to src/Core/DiceParser/DieResult.cs index 2f99fb6..aa309c0 100644 --- a/Geekbot.net/Lib/DiceParser/DieResult.cs +++ b/src/Core/DiceParser/DieResult.cs @@ -1,6 +1,6 @@ using System; -namespace Geekbot.net.Lib.DiceParser +namespace Geekbot.Core.DiceParser { public class DieResult { diff --git a/Geekbot.net/Lib/DiceParser/IDiceParser.cs b/src/Core/DiceParser/IDiceParser.cs similarity index 70% rename from Geekbot.net/Lib/DiceParser/IDiceParser.cs rename to src/Core/DiceParser/IDiceParser.cs index 608b379..ab1ebd3 100644 --- a/Geekbot.net/Lib/DiceParser/IDiceParser.cs +++ b/src/Core/DiceParser/IDiceParser.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Lib.DiceParser +namespace Geekbot.Core.DiceParser { public interface IDiceParser { diff --git a/Geekbot.net/Lib/DiceParser/SingleDie.cs b/src/Core/DiceParser/SingleDie.cs similarity index 94% rename from Geekbot.net/Lib/DiceParser/SingleDie.cs rename to src/Core/DiceParser/SingleDie.cs index 582fe29..f13dbcd 100644 --- a/Geekbot.net/Lib/DiceParser/SingleDie.cs +++ b/src/Core/DiceParser/SingleDie.cs @@ -1,8 +1,8 @@ using System.Collections.Generic; -using Geekbot.net.Lib.Extensions; -using Geekbot.net.Lib.RandomNumberGenerator; +using Geekbot.Core.Extensions; +using Geekbot.Core.RandomNumberGenerator; -namespace Geekbot.net.Lib.DiceParser +namespace Geekbot.Core.DiceParser { public class SingleDie { diff --git a/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs b/src/Core/ErrorHandling/ErrorHandler.cs similarity index 97% rename from Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs rename to src/Core/ErrorHandling/ErrorHandler.cs index cda9129..02402f2 100644 --- a/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs +++ b/src/Core/ErrorHandling/ErrorHandler.cs @@ -3,13 +3,13 @@ using System.Net; using System.Threading.Tasks; using Discord.Commands; using Discord.Net; -using Geekbot.net.Lib.Localization; -using Geekbot.net.Lib.Logger; +using Geekbot.Core.Localization; +using Geekbot.Core.Logger; using SharpRaven; using SharpRaven.Data; using Exception = System.Exception; -namespace Geekbot.net.Lib.ErrorHandling +namespace Geekbot.Core.ErrorHandling { public class ErrorHandler : IErrorHandler { diff --git a/Geekbot.net/Lib/ErrorHandling/IErrorHandler.cs b/src/Core/ErrorHandling/IErrorHandler.cs similarity index 86% rename from Geekbot.net/Lib/ErrorHandling/IErrorHandler.cs rename to src/Core/ErrorHandling/IErrorHandler.cs index 05e0d8e..c012b82 100644 --- a/Geekbot.net/Lib/ErrorHandling/IErrorHandler.cs +++ b/src/Core/ErrorHandling/IErrorHandler.cs @@ -3,7 +3,7 @@ using System.Threading.Tasks; using Discord.Commands; using Discord.Net; -namespace Geekbot.net.Lib.ErrorHandling +namespace Geekbot.Core.ErrorHandling { public interface IErrorHandler { diff --git a/Geekbot.net/Lib/Extensions/DbSetExtensions.cs b/src/Core/Extensions/DbSetExtensions.cs similarity index 94% rename from Geekbot.net/Lib/Extensions/DbSetExtensions.cs rename to src/Core/Extensions/DbSetExtensions.cs index e08b2d4..3ccffe9 100644 --- a/Geekbot.net/Lib/Extensions/DbSetExtensions.cs +++ b/src/Core/Extensions/DbSetExtensions.cs @@ -5,7 +5,7 @@ using System.Linq.Expressions; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; -namespace Geekbot.net.Lib.Extensions +namespace Geekbot.Core.Extensions { public static class DbSetExtensions { diff --git a/Geekbot.net/Lib/Extensions/EmbedBuilderExtensions.cs b/src/Core/Extensions/EmbedBuilderExtensions.cs similarity index 86% rename from Geekbot.net/Lib/Extensions/EmbedBuilderExtensions.cs rename to src/Core/Extensions/EmbedBuilderExtensions.cs index 246be44..c546306 100644 --- a/Geekbot.net/Lib/Extensions/EmbedBuilderExtensions.cs +++ b/src/Core/Extensions/EmbedBuilderExtensions.cs @@ -1,6 +1,6 @@ using Discord; -namespace Geekbot.net.Lib.Extensions +namespace Geekbot.Core.Extensions { public static class EmbedBuilderExtensions { diff --git a/Geekbot.net/Lib/Extensions/IntExtensions.cs b/src/Core/Extensions/IntExtensions.cs similarity index 87% rename from Geekbot.net/Lib/Extensions/IntExtensions.cs rename to src/Core/Extensions/IntExtensions.cs index ed0fc10..dac03e7 100644 --- a/Geekbot.net/Lib/Extensions/IntExtensions.cs +++ b/src/Core/Extensions/IntExtensions.cs @@ -1,6 +1,6 @@ using System; -namespace Geekbot.net.Lib.Extensions +namespace Geekbot.Core.Extensions { public static class IntExtensions { diff --git a/Geekbot.net/Lib/Extensions/LongExtensions.cs b/src/Core/Extensions/LongExtensions.cs similarity index 79% rename from Geekbot.net/Lib/Extensions/LongExtensions.cs rename to src/Core/Extensions/LongExtensions.cs index 1bcdd9b..087910e 100644 --- a/Geekbot.net/Lib/Extensions/LongExtensions.cs +++ b/src/Core/Extensions/LongExtensions.cs @@ -1,6 +1,6 @@ using System; -namespace Geekbot.net.Lib.Extensions +namespace Geekbot.Core.Extensions { public static class LongExtensions { diff --git a/Geekbot.net/Lib/Extensions/StringExtensions.cs b/src/Core/Extensions/StringExtensions.cs similarity index 83% rename from Geekbot.net/Lib/Extensions/StringExtensions.cs rename to src/Core/Extensions/StringExtensions.cs index 9ec1dfe..9affad1 100644 --- a/Geekbot.net/Lib/Extensions/StringExtensions.cs +++ b/src/Core/Extensions/StringExtensions.cs @@ -1,6 +1,6 @@ using System.Linq; -namespace Geekbot.net.Lib.Extensions +namespace Geekbot.Core.Extensions { public static class StringExtensions { diff --git a/Geekbot.net/Lib/Extensions/UlongExtensions.cs b/src/Core/Extensions/UlongExtensions.cs similarity index 79% rename from Geekbot.net/Lib/Extensions/UlongExtensions.cs rename to src/Core/Extensions/UlongExtensions.cs index 8fa2a67..295f317 100644 --- a/Geekbot.net/Lib/Extensions/UlongExtensions.cs +++ b/src/Core/Extensions/UlongExtensions.cs @@ -1,6 +1,6 @@ using System; -namespace Geekbot.net.Lib.Extensions +namespace Geekbot.Core.Extensions { public static class UlongExtensions { diff --git a/Geekbot.net/Lib/GeekbotExitCode.cs b/src/Core/GeekbotExitCode.cs similarity index 88% rename from Geekbot.net/Lib/GeekbotExitCode.cs rename to src/Core/GeekbotExitCode.cs index 4d91d18..5598f48 100644 --- a/Geekbot.net/Lib/GeekbotExitCode.cs +++ b/src/Core/GeekbotExitCode.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Lib +namespace Geekbot.Core { public enum GeekbotExitCode { diff --git a/Geekbot.net/Lib/GlobalSettings/GlobalSettings.cs b/src/Core/GlobalSettings/GlobalSettings.cs similarity index 91% rename from Geekbot.net/Lib/GlobalSettings/GlobalSettings.cs rename to src/Core/GlobalSettings/GlobalSettings.cs index 685a1e5..a000f8b 100644 --- a/Geekbot.net/Lib/GlobalSettings/GlobalSettings.cs +++ b/src/Core/GlobalSettings/GlobalSettings.cs @@ -1,10 +1,10 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using Geekbot.net.Database; -using Geekbot.net.Database.Models; +using Geekbot.Core.Database; +using Geekbot.Core.Database.Models; -namespace Geekbot.net.Lib.GlobalSettings +namespace Geekbot.Core.GlobalSettings { public class GlobalSettings : IGlobalSettings { diff --git a/Geekbot.net/Lib/GlobalSettings/IGlobalSettings.cs b/src/Core/GlobalSettings/IGlobalSettings.cs similarity index 72% rename from Geekbot.net/Lib/GlobalSettings/IGlobalSettings.cs rename to src/Core/GlobalSettings/IGlobalSettings.cs index 4833215..082b3dc 100644 --- a/Geekbot.net/Lib/GlobalSettings/IGlobalSettings.cs +++ b/src/Core/GlobalSettings/IGlobalSettings.cs @@ -1,7 +1,7 @@ using System.Threading.Tasks; -using Geekbot.net.Database.Models; +using Geekbot.Core.Database.Models; -namespace Geekbot.net.Lib.GlobalSettings +namespace Geekbot.Core.GlobalSettings { public interface IGlobalSettings { diff --git a/Geekbot.net/Lib/GuildSettingsManager/GuildSettingsManager.cs b/src/Core/GuildSettingsManager/GuildSettingsManager.cs similarity index 93% rename from Geekbot.net/Lib/GuildSettingsManager/GuildSettingsManager.cs rename to src/Core/GuildSettingsManager/GuildSettingsManager.cs index bfa7556..565563f 100644 --- a/Geekbot.net/Lib/GuildSettingsManager/GuildSettingsManager.cs +++ b/src/Core/GuildSettingsManager/GuildSettingsManager.cs @@ -1,11 +1,11 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using Geekbot.net.Database; -using Geekbot.net.Database.Models; -using Geekbot.net.Lib.Extensions; +using Geekbot.Core.Database; +using Geekbot.Core.Database.Models; +using Geekbot.Core.Extensions; -namespace Geekbot.net.Lib.GuildSettingsManager +namespace Geekbot.Core.GuildSettingsManager { public class GuildSettingsManager : IGuildSettingsManager { diff --git a/Geekbot.net/Lib/GuildSettingsManager/IGuildSettingsManager.cs b/src/Core/GuildSettingsManager/IGuildSettingsManager.cs similarity index 73% rename from Geekbot.net/Lib/GuildSettingsManager/IGuildSettingsManager.cs rename to src/Core/GuildSettingsManager/IGuildSettingsManager.cs index a45b263..d34eb73 100644 --- a/Geekbot.net/Lib/GuildSettingsManager/IGuildSettingsManager.cs +++ b/src/Core/GuildSettingsManager/IGuildSettingsManager.cs @@ -1,7 +1,7 @@ using System.Threading.Tasks; -using Geekbot.net.Database.Models; +using Geekbot.Core.Database.Models; -namespace Geekbot.net.Lib.GuildSettingsManager +namespace Geekbot.Core.GuildSettingsManager { public interface IGuildSettingsManager { diff --git a/Geekbot.net/Lib/Highscores/HighscoreListEmptyException.cs b/src/Core/Highscores/HighscoreListEmptyException.cs similarity index 86% rename from Geekbot.net/Lib/Highscores/HighscoreListEmptyException.cs rename to src/Core/Highscores/HighscoreListEmptyException.cs index 8c05ca9..5fc08ee 100644 --- a/Geekbot.net/Lib/Highscores/HighscoreListEmptyException.cs +++ b/src/Core/Highscores/HighscoreListEmptyException.cs @@ -1,6 +1,6 @@ using System; -namespace Geekbot.net.Lib.Highscores +namespace Geekbot.Core.Highscores { public class HighscoreListEmptyException : Exception { diff --git a/Geekbot.net/Lib/Highscores/HighscoreManager.cs b/src/Core/Highscores/HighscoreManager.cs similarity index 93% rename from Geekbot.net/Lib/Highscores/HighscoreManager.cs rename to src/Core/Highscores/HighscoreManager.cs index 8218532..ac8580b 100644 --- a/Geekbot.net/Lib/Highscores/HighscoreManager.cs +++ b/src/Core/Highscores/HighscoreManager.cs @@ -1,10 +1,10 @@ using System.Collections.Generic; using System.Linq; -using Geekbot.net.Database; -using Geekbot.net.Lib.Extensions; -using Geekbot.net.Lib.UserRepository; +using Geekbot.Core.Database; +using Geekbot.Core.Extensions; +using Geekbot.Core.UserRepository; -namespace Geekbot.net.Lib.Highscores +namespace Geekbot.Core.Highscores { public class HighscoreManager : IHighscoreManager { diff --git a/Geekbot.net/Lib/Highscores/HighscoreTypes.cs b/src/Core/Highscores/HighscoreTypes.cs similarity index 69% rename from Geekbot.net/Lib/Highscores/HighscoreTypes.cs rename to src/Core/Highscores/HighscoreTypes.cs index adf5698..b577642 100644 --- a/Geekbot.net/Lib/Highscores/HighscoreTypes.cs +++ b/src/Core/Highscores/HighscoreTypes.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Lib.Highscores +namespace Geekbot.Core.Highscores { public enum HighscoreTypes { diff --git a/Geekbot.net/Lib/Highscores/HighscoreUserDto.cs b/src/Core/Highscores/HighscoreUserDto.cs similarity index 82% rename from Geekbot.net/Lib/Highscores/HighscoreUserDto.cs rename to src/Core/Highscores/HighscoreUserDto.cs index 7abb352..58e1897 100644 --- a/Geekbot.net/Lib/Highscores/HighscoreUserDto.cs +++ b/src/Core/Highscores/HighscoreUserDto.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Lib.Highscores +namespace Geekbot.Core.Highscores { public class HighscoreUserDto { diff --git a/Geekbot.net/Lib/Highscores/IHighscoreManager.cs b/src/Core/Highscores/IHighscoreManager.cs similarity index 89% rename from Geekbot.net/Lib/Highscores/IHighscoreManager.cs rename to src/Core/Highscores/IHighscoreManager.cs index a09b07e..83ba2da 100644 --- a/Geekbot.net/Lib/Highscores/IHighscoreManager.cs +++ b/src/Core/Highscores/IHighscoreManager.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; -namespace Geekbot.net.Lib.Highscores +namespace Geekbot.Core.Highscores { public interface IHighscoreManager { diff --git a/Geekbot.net/Lib/HttpAbstractions.cs b/src/Core/HttpAbstractions.cs similarity index 97% rename from Geekbot.net/Lib/HttpAbstractions.cs rename to src/Core/HttpAbstractions.cs index 2cde4cc..4fa287b 100644 --- a/Geekbot.net/Lib/HttpAbstractions.cs +++ b/src/Core/HttpAbstractions.cs @@ -4,7 +4,7 @@ using System.Net.Http.Headers; using System.Threading.Tasks; using Newtonsoft.Json; -namespace Geekbot.net.Lib +namespace Geekbot.Core { public static class HttpAbstractions { diff --git a/Geekbot.net/Lib/KvInMemoryStore/IKvInMemoryStore.cs b/src/Core/KvInMemoryStore/IKvInMemoryStore.cs similarity index 80% rename from Geekbot.net/Lib/KvInMemoryStore/IKvInMemoryStore.cs rename to src/Core/KvInMemoryStore/IKvInMemoryStore.cs index 88f3863..6281f4b 100644 --- a/Geekbot.net/Lib/KvInMemoryStore/IKvInMemoryStore.cs +++ b/src/Core/KvInMemoryStore/IKvInMemoryStore.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Lib.KvInMemoryStore +namespace Geekbot.Core.KvInMemoryStore { public interface IKvInMemoryStore { diff --git a/Geekbot.net/Lib/KvInMemoryStore/KvInMemoryStore.cs b/src/Core/KvInMemoryStore/KvInMemoryStore.cs similarity index 93% rename from Geekbot.net/Lib/KvInMemoryStore/KvInMemoryStore.cs rename to src/Core/KvInMemoryStore/KvInMemoryStore.cs index 33b7155..4a50546 100644 --- a/Geekbot.net/Lib/KvInMemoryStore/KvInMemoryStore.cs +++ b/src/Core/KvInMemoryStore/KvInMemoryStore.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; -namespace Geekbot.net.Lib.KvInMemoryStore +namespace Geekbot.Core.KvInMemoryStore { public class KvInInMemoryStore : IKvInMemoryStore { diff --git a/Geekbot.net/Lib/Levels/ILevelCalc.cs b/src/Core/Levels/ILevelCalc.cs similarity index 67% rename from Geekbot.net/Lib/Levels/ILevelCalc.cs rename to src/Core/Levels/ILevelCalc.cs index cc488e7..17413e6 100644 --- a/Geekbot.net/Lib/Levels/ILevelCalc.cs +++ b/src/Core/Levels/ILevelCalc.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Lib.Levels +namespace Geekbot.Core.Levels { public interface ILevelCalc { diff --git a/Geekbot.net/Lib/Levels/LevelCalc.cs b/src/Core/Levels/LevelCalc.cs similarity index 95% rename from Geekbot.net/Lib/Levels/LevelCalc.cs rename to src/Core/Levels/LevelCalc.cs index 74747b7..8203f97 100644 --- a/Geekbot.net/Lib/Levels/LevelCalc.cs +++ b/src/Core/Levels/LevelCalc.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Linq; -namespace Geekbot.net.Lib.Levels +namespace Geekbot.Core.Levels { public class LevelCalc : ILevelCalc { diff --git a/Geekbot.net/Lib/Localization/ITranslationHandler.cs b/src/Core/Localization/ITranslationHandler.cs similarity index 91% rename from Geekbot.net/Lib/Localization/ITranslationHandler.cs rename to src/Core/Localization/ITranslationHandler.cs index 9cd0680..74b3e88 100644 --- a/Geekbot.net/Lib/Localization/ITranslationHandler.cs +++ b/src/Core/Localization/ITranslationHandler.cs @@ -2,7 +2,7 @@ using System.Threading.Tasks; using Discord.Commands; -namespace Geekbot.net.Lib.Localization +namespace Geekbot.Core.Localization { public interface ITranslationHandler { diff --git a/Geekbot.net/Lib/Localization/TranslationGuildContext.cs b/src/Core/Localization/TranslationGuildContext.cs similarity index 95% rename from Geekbot.net/Lib/Localization/TranslationGuildContext.cs rename to src/Core/Localization/TranslationGuildContext.cs index fb65aa6..25685da 100644 --- a/Geekbot.net/Lib/Localization/TranslationGuildContext.cs +++ b/src/Core/Localization/TranslationGuildContext.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Text; using System.Threading.Tasks; -namespace Geekbot.net.Lib.Localization +namespace Geekbot.Core.Localization { public class TranslationGuildContext { diff --git a/Geekbot.net/Lib/Localization/TranslationHandler.cs b/src/Core/Localization/TranslationHandler.cs similarity index 96% rename from Geekbot.net/Lib/Localization/TranslationHandler.cs rename to src/Core/Localization/TranslationHandler.cs index 6166e6a..e38d9a1 100644 --- a/Geekbot.net/Lib/Localization/TranslationHandler.cs +++ b/src/Core/Localization/TranslationHandler.cs @@ -4,12 +4,12 @@ using System.IO; using System.Linq; using System.Threading.Tasks; using Discord.Commands; -using Geekbot.net.Lib.GuildSettingsManager; -using Geekbot.net.Lib.Logger; +using Geekbot.Core.GuildSettingsManager; +using Geekbot.Core.Logger; using YamlDotNet.Core; using YamlDotNet.Serialization; -namespace Geekbot.net.Lib.Localization +namespace Geekbot.Core.Localization { public class TranslationHandler : ITranslationHandler { diff --git a/Geekbot.net/Lib/Localization/Translations.yml b/src/Core/Localization/Translations.yml similarity index 100% rename from Geekbot.net/Lib/Localization/Translations.yml rename to src/Core/Localization/Translations.yml diff --git a/Geekbot.net/Lib/Logger/DiscordLogger.cs b/src/Core/Logger/DiscordLogger.cs similarity index 95% rename from Geekbot.net/Lib/Logger/DiscordLogger.cs rename to src/Core/Logger/DiscordLogger.cs index 688e26b..f6fb95a 100644 --- a/Geekbot.net/Lib/Logger/DiscordLogger.cs +++ b/src/Core/Logger/DiscordLogger.cs @@ -2,7 +2,7 @@ using System.Threading.Tasks; using Discord; -namespace Geekbot.net.Lib.Logger +namespace Geekbot.Core.Logger { public class DiscordLogger : IDiscordLogger { diff --git a/Geekbot.net/Lib/Logger/GeekbotLogger.cs b/src/Core/Logger/GeekbotLogger.cs similarity index 96% rename from Geekbot.net/Lib/Logger/GeekbotLogger.cs rename to src/Core/Logger/GeekbotLogger.cs index e2a3714..8d4a8eb 100644 --- a/Geekbot.net/Lib/Logger/GeekbotLogger.cs +++ b/src/Core/Logger/GeekbotLogger.cs @@ -1,7 +1,7 @@ using System; using Newtonsoft.Json; -namespace Geekbot.net.Lib.Logger +namespace Geekbot.Core.Logger { public class GeekbotLogger : IGeekbotLogger { diff --git a/Geekbot.net/Lib/Logger/IDiscordLogger.cs b/src/Core/Logger/IDiscordLogger.cs similarity index 76% rename from Geekbot.net/Lib/Logger/IDiscordLogger.cs rename to src/Core/Logger/IDiscordLogger.cs index fdc8c7f..c127e0c 100644 --- a/Geekbot.net/Lib/Logger/IDiscordLogger.cs +++ b/src/Core/Logger/IDiscordLogger.cs @@ -1,7 +1,7 @@ using System.Threading.Tasks; using Discord; -namespace Geekbot.net.Lib.Logger +namespace Geekbot.Core.Logger { public interface IDiscordLogger { diff --git a/Geekbot.net/Lib/Logger/IGeekbotLogger.cs b/src/Core/Logger/IGeekbotLogger.cs similarity index 92% rename from Geekbot.net/Lib/Logger/IGeekbotLogger.cs rename to src/Core/Logger/IGeekbotLogger.cs index 944524a..1363629 100644 --- a/Geekbot.net/Lib/Logger/IGeekbotLogger.cs +++ b/src/Core/Logger/IGeekbotLogger.cs @@ -1,6 +1,6 @@ using System; -namespace Geekbot.net.Lib.Logger +namespace Geekbot.Core.Logger { public interface IGeekbotLogger { diff --git a/Geekbot.net/Lib/Logger/LogDto.cs b/src/Core/Logger/LogDto.cs similarity index 88% rename from Geekbot.net/Lib/Logger/LogDto.cs rename to src/Core/Logger/LogDto.cs index 8ccfcdf..9db0c65 100644 --- a/Geekbot.net/Lib/Logger/LogDto.cs +++ b/src/Core/Logger/LogDto.cs @@ -1,6 +1,6 @@ using System; -namespace Geekbot.net.Lib.Logger +namespace Geekbot.Core.Logger { public class GeekbotLoggerObject { diff --git a/Geekbot.net/Lib/Logger/LogSource.cs b/src/Core/Logger/LogSource.cs similarity index 87% rename from Geekbot.net/Lib/Logger/LogSource.cs rename to src/Core/Logger/LogSource.cs index 48ad7d0..4518327 100644 --- a/Geekbot.net/Lib/Logger/LogSource.cs +++ b/src/Core/Logger/LogSource.cs @@ -1,7 +1,7 @@ using Newtonsoft.Json; using Newtonsoft.Json.Converters; -namespace Geekbot.net.Lib.Logger +namespace Geekbot.Core.Logger { [JsonConverter(typeof(StringEnumConverter))] public enum LogSource diff --git a/Geekbot.net/Lib/Logger/LoggerFactory.cs b/src/Core/Logger/LoggerFactory.cs similarity index 98% rename from Geekbot.net/Lib/Logger/LoggerFactory.cs rename to src/Core/Logger/LoggerFactory.cs index e31686e..bc129fc 100644 --- a/Geekbot.net/Lib/Logger/LoggerFactory.cs +++ b/src/Core/Logger/LoggerFactory.cs @@ -5,7 +5,7 @@ using NLog.Config; using NLog.Targets; using SumoLogic.Logging.NLog; -namespace Geekbot.net.Lib.Logger +namespace Geekbot.Core.Logger { public class LoggerFactory { diff --git a/Geekbot.net/Lib/Logger/MessageDto.cs b/src/Core/Logger/MessageDto.cs similarity index 92% rename from Geekbot.net/Lib/Logger/MessageDto.cs rename to src/Core/Logger/MessageDto.cs index 011acf3..039024e 100644 --- a/Geekbot.net/Lib/Logger/MessageDto.cs +++ b/src/Core/Logger/MessageDto.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Lib.Logger +namespace Geekbot.Core.Logger { public class MessageDto { diff --git a/Geekbot.net/Lib/Logger/SimpleConextConverter.cs b/src/Core/Logger/SimpleConextConverter.cs similarity index 96% rename from Geekbot.net/Lib/Logger/SimpleConextConverter.cs rename to src/Core/Logger/SimpleConextConverter.cs index 1eef12c..23cee40 100644 --- a/Geekbot.net/Lib/Logger/SimpleConextConverter.cs +++ b/src/Core/Logger/SimpleConextConverter.cs @@ -1,7 +1,7 @@ using Discord.Commands; using Discord.WebSocket; -namespace Geekbot.net.Lib.Logger +namespace Geekbot.Core.Logger { public class SimpleConextConverter { diff --git a/Geekbot.net/Lib/MalClient/IMalClient.cs b/src/Core/MalClient/IMalClient.cs similarity index 83% rename from Geekbot.net/Lib/MalClient/IMalClient.cs rename to src/Core/MalClient/IMalClient.cs index 6aaab05..70851c1 100644 --- a/Geekbot.net/Lib/MalClient/IMalClient.cs +++ b/src/Core/MalClient/IMalClient.cs @@ -1,7 +1,7 @@ using System.Threading.Tasks; using MyAnimeListSharp.Core; -namespace Geekbot.net.Lib.MalClient +namespace Geekbot.Core.MalClient { public interface IMalClient { diff --git a/Geekbot.net/Lib/MalClient/MalClient.cs b/src/Core/MalClient/MalClient.cs similarity index 92% rename from Geekbot.net/Lib/MalClient/MalClient.cs rename to src/Core/MalClient/MalClient.cs index 3f121ea..3f5f165 100644 --- a/Geekbot.net/Lib/MalClient/MalClient.cs +++ b/src/Core/MalClient/MalClient.cs @@ -1,11 +1,11 @@ using System.Threading.Tasks; -using Geekbot.net.Lib.GlobalSettings; -using Geekbot.net.Lib.Logger; +using Geekbot.Core.GlobalSettings; +using Geekbot.Core.Logger; using MyAnimeListSharp.Auth; using MyAnimeListSharp.Core; using MyAnimeListSharp.Facade.Async; -namespace Geekbot.net.Lib.MalClient +namespace Geekbot.Core.MalClient { public class MalClient : IMalClient { diff --git a/Geekbot.net/Lib/Media/FortunesProvider.cs b/src/Core/Media/FortunesProvider.cs similarity index 85% rename from Geekbot.net/Lib/Media/FortunesProvider.cs rename to src/Core/Media/FortunesProvider.cs index 56b5ed6..1b0d87a 100644 --- a/Geekbot.net/Lib/Media/FortunesProvider.cs +++ b/src/Core/Media/FortunesProvider.cs @@ -1,10 +1,10 @@ using System; using System.IO; -using Geekbot.net.Lib.Logger; +using Geekbot.Core.Logger; -namespace Geekbot.net.Lib.Media +namespace Geekbot.Core.Media { - internal class FortunesProvider : IFortunesProvider + public class FortunesProvider : IFortunesProvider { private readonly string[] _fortuneArray; private readonly int _totalFortunes; diff --git a/Geekbot.net/Lib/Media/IFortunesProvider.cs b/src/Core/Media/IFortunesProvider.cs similarity index 68% rename from Geekbot.net/Lib/Media/IFortunesProvider.cs rename to src/Core/Media/IFortunesProvider.cs index c82e45c..a4ef0d9 100644 --- a/Geekbot.net/Lib/Media/IFortunesProvider.cs +++ b/src/Core/Media/IFortunesProvider.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Lib.Media +namespace Geekbot.Core.Media { public interface IFortunesProvider { diff --git a/Geekbot.net/Lib/Media/IMediaProvider.cs b/src/Core/Media/IMediaProvider.cs similarity index 69% rename from Geekbot.net/Lib/Media/IMediaProvider.cs rename to src/Core/Media/IMediaProvider.cs index 63c0f41..03e32cf 100644 --- a/Geekbot.net/Lib/Media/IMediaProvider.cs +++ b/src/Core/Media/IMediaProvider.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Lib.Media +namespace Geekbot.Core.Media { public interface IMediaProvider { diff --git a/Geekbot.net/Lib/Media/MediaProvider.cs b/src/Core/Media/MediaProvider.cs similarity index 92% rename from Geekbot.net/Lib/Media/MediaProvider.cs rename to src/Core/Media/MediaProvider.cs index 5a9a055..4c72ee0 100644 --- a/Geekbot.net/Lib/Media/MediaProvider.cs +++ b/src/Core/Media/MediaProvider.cs @@ -1,10 +1,8 @@ -using System; -using System.Collections.Generic; -using System.IO; -using Geekbot.net.Lib.Logger; -using Geekbot.net.Lib.RandomNumberGenerator; +using System.IO; +using Geekbot.Core.Logger; +using Geekbot.Core.RandomNumberGenerator; -namespace Geekbot.net.Lib.Media +namespace Geekbot.Core.Media { public class MediaProvider : IMediaProvider { diff --git a/Geekbot.net/Lib/Media/MediaType.cs b/src/Core/Media/MediaType.cs similarity index 84% rename from Geekbot.net/Lib/Media/MediaType.cs rename to src/Core/Media/MediaType.cs index aee317b..fa30164 100644 --- a/Geekbot.net/Lib/Media/MediaType.cs +++ b/src/Core/Media/MediaType.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Lib.Media +namespace Geekbot.Core.Media { public enum MediaType { diff --git a/Geekbot.net/Lib/Polyfills/UserPolyfillDto.cs b/src/Core/Polyfills/UserPolyfillDto.cs similarity index 90% rename from Geekbot.net/Lib/Polyfills/UserPolyfillDto.cs rename to src/Core/Polyfills/UserPolyfillDto.cs index 32ea663..3a8f43f 100644 --- a/Geekbot.net/Lib/Polyfills/UserPolyfillDto.cs +++ b/src/Core/Polyfills/UserPolyfillDto.cs @@ -3,9 +3,9 @@ using System.Collections.Immutable; using System.Threading.Tasks; using Discord; -namespace Geekbot.net.Lib.Polyfills +namespace Geekbot.Core.Polyfills { - internal class UserPolyfillDto : IUser + public class UserPolyfillDto : IUser { public ulong Id { get; set; } public DateTimeOffset CreatedAt { get; set; } diff --git a/Geekbot.net/Lib/RandomNumberGenerator/IRandomNumberGenerator.cs b/src/Core/RandomNumberGenerator/IRandomNumberGenerator.cs similarity index 67% rename from Geekbot.net/Lib/RandomNumberGenerator/IRandomNumberGenerator.cs rename to src/Core/RandomNumberGenerator/IRandomNumberGenerator.cs index 32c6285..f817248 100644 --- a/Geekbot.net/Lib/RandomNumberGenerator/IRandomNumberGenerator.cs +++ b/src/Core/RandomNumberGenerator/IRandomNumberGenerator.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Lib.RandomNumberGenerator +namespace Geekbot.Core.RandomNumberGenerator { public interface IRandomNumberGenerator { diff --git a/Geekbot.net/Lib/RandomNumberGenerator/RandomNumberGenerator.cs b/src/Core/RandomNumberGenerator/RandomNumberGenerator.cs similarity index 92% rename from Geekbot.net/Lib/RandomNumberGenerator/RandomNumberGenerator.cs rename to src/Core/RandomNumberGenerator/RandomNumberGenerator.cs index 79b4620..63381b5 100644 --- a/Geekbot.net/Lib/RandomNumberGenerator/RandomNumberGenerator.cs +++ b/src/Core/RandomNumberGenerator/RandomNumberGenerator.cs @@ -1,7 +1,7 @@ using System; using System.Security.Cryptography; -namespace Geekbot.net.Lib.RandomNumberGenerator +namespace Geekbot.Core.RandomNumberGenerator { public class RandomNumberGenerator : IRandomNumberGenerator { diff --git a/Geekbot.net/Lib/ReactionListener/IReactionListener.cs b/src/Core/ReactionListener/IReactionListener.cs similarity index 89% rename from Geekbot.net/Lib/ReactionListener/IReactionListener.cs rename to src/Core/ReactionListener/IReactionListener.cs index b2ab9fb..4909d68 100644 --- a/Geekbot.net/Lib/ReactionListener/IReactionListener.cs +++ b/src/Core/ReactionListener/IReactionListener.cs @@ -2,7 +2,7 @@ using Discord; using Discord.WebSocket; -namespace Geekbot.net.Lib.ReactionListener +namespace Geekbot.Core.ReactionListener { public interface IReactionListener { diff --git a/Geekbot.net/Lib/ReactionListener/ReactionListener.cs b/src/Core/ReactionListener/ReactionListener.cs similarity index 92% rename from Geekbot.net/Lib/ReactionListener/ReactionListener.cs rename to src/Core/ReactionListener/ReactionListener.cs index bd335af..84367c9 100644 --- a/Geekbot.net/Lib/ReactionListener/ReactionListener.cs +++ b/src/Core/ReactionListener/ReactionListener.cs @@ -2,11 +2,11 @@ using System.Threading.Tasks; using Discord; using Discord.WebSocket; -using Geekbot.net.Database; -using Geekbot.net.Database.Models; -using Geekbot.net.Lib.Extensions; +using Geekbot.Core.Database; +using Geekbot.Core.Database.Models; +using Geekbot.Core.Extensions; -namespace Geekbot.net.Lib.ReactionListener +namespace Geekbot.Core.ReactionListener { public class ReactionListener : IReactionListener { diff --git a/Geekbot.net/Lib/RunParameters.cs b/src/Core/RunParameters.cs similarity index 97% rename from Geekbot.net/Lib/RunParameters.cs rename to src/Core/RunParameters.cs index eb8c078..3210587 100644 --- a/Geekbot.net/Lib/RunParameters.cs +++ b/src/Core/RunParameters.cs @@ -1,7 +1,7 @@ using System; using CommandLine; -namespace Geekbot.net.Lib +namespace Geekbot.Core { public class RunParameters { diff --git a/Geekbot.net/Lib/UserRepository/IUserRepository.cs b/src/Core/UserRepository/IUserRepository.cs similarity index 68% rename from Geekbot.net/Lib/UserRepository/IUserRepository.cs rename to src/Core/UserRepository/IUserRepository.cs index b2f9a1f..32598f6 100644 --- a/Geekbot.net/Lib/UserRepository/IUserRepository.cs +++ b/src/Core/UserRepository/IUserRepository.cs @@ -1,8 +1,8 @@ using System.Threading.Tasks; using Discord.WebSocket; -using Geekbot.net.Database.Models; +using Geekbot.Core.Database.Models; -namespace Geekbot.net.Lib.UserRepository +namespace Geekbot.Core.UserRepository { public interface IUserRepository { diff --git a/Geekbot.net/Lib/UserRepository/UserRepository.cs b/src/Core/UserRepository/UserRepository.cs similarity index 88% rename from Geekbot.net/Lib/UserRepository/UserRepository.cs rename to src/Core/UserRepository/UserRepository.cs index 41e8e73..94d5df4 100644 --- a/Geekbot.net/Lib/UserRepository/UserRepository.cs +++ b/src/Core/UserRepository/UserRepository.cs @@ -1,14 +1,13 @@ using System; -using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Discord.WebSocket; -using Geekbot.net.Database; -using Geekbot.net.Database.Models; -using Geekbot.net.Lib.Extensions; -using Geekbot.net.Lib.Logger; +using Geekbot.Core.Database; +using Geekbot.Core.Database.Models; +using Geekbot.Core.Extensions; +using Geekbot.Core.Logger; -namespace Geekbot.net.Lib.UserRepository +namespace Geekbot.Core.UserRepository { public class UserRepository : IUserRepository { diff --git a/Geekbot.net/Lib/WikipediaClient/IWikipediaClient.cs b/src/Core/WikipediaClient/IWikipediaClient.cs similarity index 63% rename from Geekbot.net/Lib/WikipediaClient/IWikipediaClient.cs rename to src/Core/WikipediaClient/IWikipediaClient.cs index c6cdf3d..fdb1ae1 100644 --- a/Geekbot.net/Lib/WikipediaClient/IWikipediaClient.cs +++ b/src/Core/WikipediaClient/IWikipediaClient.cs @@ -1,7 +1,7 @@ using System.Threading.Tasks; -using Geekbot.net.Lib.WikipediaClient.Page; +using Geekbot.Core.WikipediaClient.Page; -namespace Geekbot.net.Lib.WikipediaClient +namespace Geekbot.Core.WikipediaClient { public interface IWikipediaClient { diff --git a/Geekbot.net/Lib/WikipediaClient/Page/PageApiUrls.cs b/src/Core/WikipediaClient/Page/PageApiUrls.cs similarity index 84% rename from Geekbot.net/Lib/WikipediaClient/Page/PageApiUrls.cs rename to src/Core/WikipediaClient/Page/PageApiUrls.cs index 803f4d7..6e3a1b6 100644 --- a/Geekbot.net/Lib/WikipediaClient/Page/PageApiUrls.cs +++ b/src/Core/WikipediaClient/Page/PageApiUrls.cs @@ -1,6 +1,6 @@ using System; -namespace Geekbot.net.Lib.WikipediaClient.Page +namespace Geekbot.Core.WikipediaClient.Page { public class PageApiUrls { diff --git a/Geekbot.net/Lib/WikipediaClient/Page/PageContentUrlCollection.cs b/src/Core/WikipediaClient/Page/PageContentUrlCollection.cs similarity index 73% rename from Geekbot.net/Lib/WikipediaClient/Page/PageContentUrlCollection.cs rename to src/Core/WikipediaClient/Page/PageContentUrlCollection.cs index 614aaa7..3b567a7 100644 --- a/Geekbot.net/Lib/WikipediaClient/Page/PageContentUrlCollection.cs +++ b/src/Core/WikipediaClient/Page/PageContentUrlCollection.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Lib.WikipediaClient.Page +namespace Geekbot.Core.WikipediaClient.Page { public class PageContentUrlCollection { diff --git a/Geekbot.net/Lib/WikipediaClient/Page/PageContentUrls.cs b/src/Core/WikipediaClient/Page/PageContentUrls.cs similarity index 79% rename from Geekbot.net/Lib/WikipediaClient/Page/PageContentUrls.cs rename to src/Core/WikipediaClient/Page/PageContentUrls.cs index faf7bb5..ca30b43 100644 --- a/Geekbot.net/Lib/WikipediaClient/Page/PageContentUrls.cs +++ b/src/Core/WikipediaClient/Page/PageContentUrls.cs @@ -1,6 +1,6 @@ using System; -namespace Geekbot.net.Lib.WikipediaClient.Page +namespace Geekbot.Core.WikipediaClient.Page { public class PageContentUrls { diff --git a/Geekbot.net/Lib/WikipediaClient/Page/PageCoordinates.cs b/src/Core/WikipediaClient/Page/PageCoordinates.cs similarity index 68% rename from Geekbot.net/Lib/WikipediaClient/Page/PageCoordinates.cs rename to src/Core/WikipediaClient/Page/PageCoordinates.cs index ce67f9b..52c31a4 100644 --- a/Geekbot.net/Lib/WikipediaClient/Page/PageCoordinates.cs +++ b/src/Core/WikipediaClient/Page/PageCoordinates.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Lib.WikipediaClient.Page +namespace Geekbot.Core.WikipediaClient.Page { public class PageCoordinates { diff --git a/Geekbot.net/Lib/WikipediaClient/Page/PageImage.cs b/src/Core/WikipediaClient/Page/PageImage.cs similarity index 76% rename from Geekbot.net/Lib/WikipediaClient/Page/PageImage.cs rename to src/Core/WikipediaClient/Page/PageImage.cs index cdcc2a9..8c4fb82 100644 --- a/Geekbot.net/Lib/WikipediaClient/Page/PageImage.cs +++ b/src/Core/WikipediaClient/Page/PageImage.cs @@ -1,6 +1,6 @@ using System; -namespace Geekbot.net.Lib.WikipediaClient.Page +namespace Geekbot.Core.WikipediaClient.Page { public class PageImage { diff --git a/Geekbot.net/Lib/WikipediaClient/Page/PageNamespace.cs b/src/Core/WikipediaClient/Page/PageNamespace.cs similarity index 68% rename from Geekbot.net/Lib/WikipediaClient/Page/PageNamespace.cs rename to src/Core/WikipediaClient/Page/PageNamespace.cs index 798e6b9..0691ac3 100644 --- a/Geekbot.net/Lib/WikipediaClient/Page/PageNamespace.cs +++ b/src/Core/WikipediaClient/Page/PageNamespace.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Lib.WikipediaClient.Page +namespace Geekbot.Core.WikipediaClient.Page { public class PageNamespace { diff --git a/Geekbot.net/Lib/WikipediaClient/Page/PagePreview.cs b/src/Core/WikipediaClient/Page/PagePreview.cs similarity index 94% rename from Geekbot.net/Lib/WikipediaClient/Page/PagePreview.cs rename to src/Core/WikipediaClient/Page/PagePreview.cs index 37434d3..1c1749d 100644 --- a/Geekbot.net/Lib/WikipediaClient/Page/PagePreview.cs +++ b/src/Core/WikipediaClient/Page/PagePreview.cs @@ -2,7 +2,7 @@ using Newtonsoft.Json; using Newtonsoft.Json.Converters; -namespace Geekbot.net.Lib.WikipediaClient.Page +namespace Geekbot.Core.WikipediaClient.Page { public class PagePreview { diff --git a/Geekbot.net/Lib/WikipediaClient/Page/PageTitles.cs b/src/Core/WikipediaClient/Page/PageTitles.cs similarity index 76% rename from Geekbot.net/Lib/WikipediaClient/Page/PageTitles.cs rename to src/Core/WikipediaClient/Page/PageTitles.cs index dcc2cea..ad83b27 100644 --- a/Geekbot.net/Lib/WikipediaClient/Page/PageTitles.cs +++ b/src/Core/WikipediaClient/Page/PageTitles.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.Lib.WikipediaClient.Page +namespace Geekbot.Core.WikipediaClient.Page { public class PageTitles { diff --git a/Geekbot.net/Lib/WikipediaClient/Page/PageTypes.cs b/src/Core/WikipediaClient/Page/PageTypes.cs similarity index 84% rename from Geekbot.net/Lib/WikipediaClient/Page/PageTypes.cs rename to src/Core/WikipediaClient/Page/PageTypes.cs index 63cd4d2..8bc9f64 100644 --- a/Geekbot.net/Lib/WikipediaClient/Page/PageTypes.cs +++ b/src/Core/WikipediaClient/Page/PageTypes.cs @@ -1,6 +1,6 @@ using System.Runtime.Serialization; -namespace Geekbot.net.Lib.WikipediaClient.Page +namespace Geekbot.Core.WikipediaClient.Page { public enum PageTypes { diff --git a/Geekbot.net/Lib/WikipediaClient/WikipediaClient.cs b/src/Core/WikipediaClient/WikipediaClient.cs similarity index 87% rename from Geekbot.net/Lib/WikipediaClient/WikipediaClient.cs rename to src/Core/WikipediaClient/WikipediaClient.cs index ed4902a..f577d92 100644 --- a/Geekbot.net/Lib/WikipediaClient/WikipediaClient.cs +++ b/src/Core/WikipediaClient/WikipediaClient.cs @@ -1,9 +1,9 @@ using System.Net.Http; using System.Threading.Tasks; -using Geekbot.net.Lib.WikipediaClient.Page; +using Geekbot.Core.WikipediaClient.Page; using Newtonsoft.Json; -namespace Geekbot.net.Lib.WikipediaClient +namespace Geekbot.Core.WikipediaClient { public class WikipediaClient : IWikipediaClient { diff --git a/Geekbot.net/WebApi/ApiError.cs b/src/Web/ApiError.cs similarity index 70% rename from Geekbot.net/WebApi/ApiError.cs rename to src/Web/ApiError.cs index 182518e..3708d2c 100644 --- a/Geekbot.net/WebApi/ApiError.cs +++ b/src/Web/ApiError.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.WebApi +namespace Geekbot.Web { public class ApiError { diff --git a/Geekbot.net/WebApi/Controllers/Callback/CallbackController.cs b/src/Web/Controllers/Callback/CallbackController.cs similarity index 96% rename from Geekbot.net/WebApi/Controllers/Callback/CallbackController.cs rename to src/Web/Controllers/Callback/CallbackController.cs index 78fb38d..b3927bc 100644 --- a/Geekbot.net/WebApi/Controllers/Callback/CallbackController.cs +++ b/src/Web/Controllers/Callback/CallbackController.cs @@ -4,11 +4,11 @@ using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; using Discord.WebSocket; -using Geekbot.net.Lib.GlobalSettings; +using Geekbot.Core.GlobalSettings; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; -namespace Geekbot.net.WebApi.Controllers.Callback +namespace Geekbot.Web.Controllers.Callback { public class CallbackController : Controller { diff --git a/Geekbot.net/WebApi/Controllers/Callback/CallbackTokenResponseDto.cs b/src/Web/Controllers/Callback/CallbackTokenResponseDto.cs similarity index 85% rename from Geekbot.net/WebApi/Controllers/Callback/CallbackTokenResponseDto.cs rename to src/Web/Controllers/Callback/CallbackTokenResponseDto.cs index 3c81592..37d2c32 100644 --- a/Geekbot.net/WebApi/Controllers/Callback/CallbackTokenResponseDto.cs +++ b/src/Web/Controllers/Callback/CallbackTokenResponseDto.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.WebApi.Controllers.Callback +namespace Geekbot.Web.Controllers.Callback { public class CallbackTokenResponseDto { diff --git a/Geekbot.net/WebApi/Controllers/Commands/CommandController.cs b/src/Web/Controllers/Commands/CommandController.cs similarity index 93% rename from Geekbot.net/WebApi/Controllers/Commands/CommandController.cs rename to src/Web/Controllers/Commands/CommandController.cs index 74f6e3c..e6c73e3 100644 --- a/Geekbot.net/WebApi/Controllers/Commands/CommandController.cs +++ b/src/Web/Controllers/Commands/CommandController.cs @@ -3,7 +3,7 @@ using Discord.Commands; using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Mvc; -namespace Geekbot.net.WebApi.Controllers.Commands +namespace Geekbot.Web.Controllers.Commands { [EnableCors("AllowSpecificOrigin")] public class CommandController : Controller diff --git a/Geekbot.net/WebApi/Controllers/Commands/CommandDto.cs b/src/Web/Controllers/Commands/CommandDto.cs similarity index 83% rename from Geekbot.net/WebApi/Controllers/Commands/CommandDto.cs rename to src/Web/Controllers/Commands/CommandDto.cs index 981d0f2..7183a75 100644 --- a/Geekbot.net/WebApi/Controllers/Commands/CommandDto.cs +++ b/src/Web/Controllers/Commands/CommandDto.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; -namespace Geekbot.net.WebApi.Controllers.Commands +namespace Geekbot.Web.Controllers.Commands { public class CommandDto { diff --git a/Geekbot.net/WebApi/Controllers/Commands/CommandParamDto.cs b/src/Web/Controllers/Commands/CommandParamDto.cs similarity index 74% rename from Geekbot.net/WebApi/Controllers/Commands/CommandParamDto.cs rename to src/Web/Controllers/Commands/CommandParamDto.cs index 5f7519d..77c9da6 100644 --- a/Geekbot.net/WebApi/Controllers/Commands/CommandParamDto.cs +++ b/src/Web/Controllers/Commands/CommandParamDto.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.WebApi.Controllers.Commands +namespace Geekbot.Web.Controllers.Commands { public class CommandParamDto { diff --git a/Geekbot.net/WebApi/Controllers/Highscores/HighscoreController.cs b/src/Web/Controllers/Highscores/HighscoreController.cs similarity index 92% rename from Geekbot.net/WebApi/Controllers/Highscores/HighscoreController.cs rename to src/Web/Controllers/Highscores/HighscoreController.cs index 3cccb0e..e990431 100644 --- a/Geekbot.net/WebApi/Controllers/Highscores/HighscoreController.cs +++ b/src/Web/Controllers/Highscores/HighscoreController.cs @@ -1,9 +1,9 @@ using System.Collections.Generic; -using Geekbot.net.Lib.Highscores; +using Geekbot.Core.Highscores; using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Mvc; -namespace Geekbot.net.WebApi.Controllers.Highscores +namespace Geekbot.Web.Controllers.Highscores { [EnableCors("AllowSpecificOrigin")] public class HighscoreController : Controller diff --git a/Geekbot.net/WebApi/Controllers/Highscores/HighscoreControllerPostBodyDto.cs b/src/Web/Controllers/Highscores/HighscoreControllerPostBodyDto.cs similarity index 76% rename from Geekbot.net/WebApi/Controllers/Highscores/HighscoreControllerPostBodyDto.cs rename to src/Web/Controllers/Highscores/HighscoreControllerPostBodyDto.cs index 6e7aec3..c3fe6a8 100644 --- a/Geekbot.net/WebApi/Controllers/Highscores/HighscoreControllerPostBodyDto.cs +++ b/src/Web/Controllers/Highscores/HighscoreControllerPostBodyDto.cs @@ -1,7 +1,7 @@ using System.ComponentModel.DataAnnotations; -using Geekbot.net.Lib.Highscores; +using Geekbot.Core.Highscores; -namespace Geekbot.net.WebApi.Controllers.Highscores +namespace Geekbot.Web.Controllers.Highscores { public class HighscoreControllerPostBodyDto { diff --git a/Geekbot.net/WebApi/Controllers/Highscores/HighscoreControllerReponseBody.cs b/src/Web/Controllers/Highscores/HighscoreControllerReponseBody.cs similarity index 66% rename from Geekbot.net/WebApi/Controllers/Highscores/HighscoreControllerReponseBody.cs rename to src/Web/Controllers/Highscores/HighscoreControllerReponseBody.cs index 0e59880..3cea17f 100644 --- a/Geekbot.net/WebApi/Controllers/Highscores/HighscoreControllerReponseBody.cs +++ b/src/Web/Controllers/Highscores/HighscoreControllerReponseBody.cs @@ -1,6 +1,6 @@ -using Geekbot.net.Lib.Highscores; +using Geekbot.Core.Highscores; -namespace Geekbot.net.WebApi.Controllers.Highscores +namespace Geekbot.Web.Controllers.Highscores { public class HighscoreControllerReponseBody { diff --git a/Geekbot.net/WebApi/Controllers/Status/ApiStatusDto.cs b/src/Web/Controllers/Status/ApiStatusDto.cs similarity index 75% rename from Geekbot.net/WebApi/Controllers/Status/ApiStatusDto.cs rename to src/Web/Controllers/Status/ApiStatusDto.cs index 0d5e6ad..cb2e5c7 100644 --- a/Geekbot.net/WebApi/Controllers/Status/ApiStatusDto.cs +++ b/src/Web/Controllers/Status/ApiStatusDto.cs @@ -1,4 +1,4 @@ -namespace Geekbot.net.WebApi.Controllers.Status +namespace Geekbot.Web.Controllers.Status { public class ApiStatusDto { diff --git a/Geekbot.net/WebApi/Controllers/Status/StatusController.cs b/src/Web/Controllers/Status/StatusController.cs similarity index 89% rename from Geekbot.net/WebApi/Controllers/Status/StatusController.cs rename to src/Web/Controllers/Status/StatusController.cs index c437e64..881462a 100644 --- a/Geekbot.net/WebApi/Controllers/Status/StatusController.cs +++ b/src/Web/Controllers/Status/StatusController.cs @@ -1,9 +1,9 @@ using System.Globalization; -using Geekbot.net.Lib; +using Geekbot.Core; using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Mvc; -namespace Geekbot.net.WebApi.Controllers.Status +namespace Geekbot.Web.Controllers.Status { [EnableCors("AllowSpecificOrigin")] public class StatusController : Controller diff --git a/Geekbot.net/WebApi/Logging/AspLogProvider.cs b/src/Web/Logging/AspLogProvider.cs similarity index 88% rename from Geekbot.net/WebApi/Logging/AspLogProvider.cs rename to src/Web/Logging/AspLogProvider.cs index d35872d..25fdace 100644 --- a/Geekbot.net/WebApi/Logging/AspLogProvider.cs +++ b/src/Web/Logging/AspLogProvider.cs @@ -1,8 +1,8 @@ using System.Collections.Concurrent; -using Geekbot.net.Lib.Logger; +using Geekbot.Core.Logger; using Microsoft.Extensions.Logging; -namespace Geekbot.net.WebApi.Logging +namespace Geekbot.Web.Logging { public class AspLogProvider : ILoggerProvider { diff --git a/Geekbot.net/WebApi/Logging/AspLogger.cs b/src/Web/Logging/AspLogger.cs similarity index 95% rename from Geekbot.net/WebApi/Logging/AspLogger.cs rename to src/Web/Logging/AspLogger.cs index 5899b26..c18a7f3 100644 --- a/Geekbot.net/WebApi/Logging/AspLogger.cs +++ b/src/Web/Logging/AspLogger.cs @@ -1,8 +1,8 @@ using System; -using Geekbot.net.Lib.Logger; +using Geekbot.Core.Logger; using Microsoft.Extensions.Logging; -namespace Geekbot.net.WebApi.Logging +namespace Geekbot.Web.Logging { public class AspLogger : ILogger { diff --git a/src/Web/Web.csproj b/src/Web/Web.csproj new file mode 100644 index 0000000..429165b --- /dev/null +++ b/src/Web/Web.csproj @@ -0,0 +1,26 @@ + + + + net5.0 + $(VersionSuffix) + $(VersionSuffix) + 0.0.0-DEV + Geekbot.Web + Geekbot.Web + NU1701 + + + + + + + + + + + + + + + + diff --git a/Geekbot.net/WebApi/WebApiStartup.cs b/src/Web/WebApiStartup.cs similarity index 86% rename from Geekbot.net/WebApi/WebApiStartup.cs rename to src/Web/WebApiStartup.cs index 7d3adc9..9aeedf9 100644 --- a/Geekbot.net/WebApi/WebApiStartup.cs +++ b/src/Web/WebApiStartup.cs @@ -2,19 +2,19 @@ using System.Reflection; using Discord.Commands; using Discord.WebSocket; -using Geekbot.net.Database; -using Geekbot.net.Lib; -using Geekbot.net.Lib.GlobalSettings; -using Geekbot.net.Lib.Highscores; -using Geekbot.net.Lib.Logger; -using Geekbot.net.WebApi.Logging; +using Geekbot.Core; +using Geekbot.Core.Database; +using Geekbot.Core.GlobalSettings; +using Geekbot.Core.Highscores; +using Geekbot.Core.Logger; +using Geekbot.Web.Logging; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -namespace Geekbot.net.WebApi +namespace Geekbot.Web { public static class WebApiStartup { @@ -51,7 +51,7 @@ namespace Geekbot.net.WebApi logging.SetMinimumLevel(LogLevel.Debug); logging.AddProvider(new AspLogProvider(logger)); }) - .UseSetting(WebHostDefaults.ApplicationKey, typeof(Program).GetTypeInfo().Assembly.FullName) + .UseSetting(WebHostDefaults.ApplicationKey, typeof(WebApiStartup).GetTypeInfo().Assembly.FullName) .Build().Run(); } } From 61ce14a61df972b644bcc214fb3ceae7cb4ef996 Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 8 Aug 2020 22:33:02 +0200 Subject: [PATCH 261/443] Change .gitlab-ci and dockerfile to fit the new project structure --- .gitlab-ci.yml | 4 ++-- Dockerfile | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 3d362ce..410e380 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -5,7 +5,7 @@ stages: - ops variables: - VERSION: 4.2.0-$CI_COMMIT_SHORT_SHA + VERSION: 4.3.0-$CI_COMMIT_SHORT_SHA IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG Build: @@ -18,7 +18,7 @@ Build: script: - dotnet restore - dotnet test Tests - - dotnet publish --version-suffix $VERSION -r linux-x64 -c Release -o ./app ./Geekbot.net/ + - dotnet publish --version-suffix $VERSION -r linux-x64 -c Release -o ./app ./src/Bot/ Package: stage: docker diff --git a/Dockerfile b/Dockerfile index 822471d..479dd40 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,4 +4,4 @@ COPY ./app /app/ EXPOSE 12995/tcp WORKDIR /app -ENTRYPOINT ./Geekbot.net +ENTRYPOINT ./Geekbot From 97ad54df9e34c24638310158c941040827fc2250 Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 13 Aug 2020 17:22:18 +0200 Subject: [PATCH 262/443] Rename the folder Tests to tests --- Geekbot.net.sln | 2 +- {Tests => tests}/Core/Converters/EmojiConverter.test.cs | 0 {Tests => tests}/Core/DiceParser/DiceParser.test.cs | 0 {Tests => tests}/Core/DiceParser/SingleDie.test.cs | 0 {Tests => tests}/Core/Levels/LevelCalc.test.cs | 0 .../Core/Localization/TranslationGuildContext.test.cs | 0 {Tests => tests}/Core/Localization/Translations.test.cs | 0 {Tests => tests}/TestData.cs | 0 {Tests => tests}/Tests.csproj | 0 9 files changed, 1 insertion(+), 1 deletion(-) rename {Tests => tests}/Core/Converters/EmojiConverter.test.cs (100%) rename {Tests => tests}/Core/DiceParser/DiceParser.test.cs (100%) rename {Tests => tests}/Core/DiceParser/SingleDie.test.cs (100%) rename {Tests => tests}/Core/Levels/LevelCalc.test.cs (100%) rename {Tests => tests}/Core/Localization/TranslationGuildContext.test.cs (100%) rename {Tests => tests}/Core/Localization/Translations.test.cs (100%) rename {Tests => tests}/TestData.cs (100%) rename {Tests => tests}/Tests.csproj (100%) diff --git a/Geekbot.net.sln b/Geekbot.net.sln index 5dc5e4b..4a5b912 100644 --- a/Geekbot.net.sln +++ b/Geekbot.net.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 2013 VisualStudioVersion = 12.0.0.0 MinimumVisualStudioVersion = 10.0.0.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{4CAF5F02-EFFE-4FDA-BD44-EEADDBA9600E}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "tests\Tests.csproj", "{4CAF5F02-EFFE-4FDA-BD44-EEADDBA9600E}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Core", "src\Core\Core.csproj", "{47671723-52A9-4668-BBC5-2BA76AE3B288}" EndProject diff --git a/Tests/Core/Converters/EmojiConverter.test.cs b/tests/Core/Converters/EmojiConverter.test.cs similarity index 100% rename from Tests/Core/Converters/EmojiConverter.test.cs rename to tests/Core/Converters/EmojiConverter.test.cs diff --git a/Tests/Core/DiceParser/DiceParser.test.cs b/tests/Core/DiceParser/DiceParser.test.cs similarity index 100% rename from Tests/Core/DiceParser/DiceParser.test.cs rename to tests/Core/DiceParser/DiceParser.test.cs diff --git a/Tests/Core/DiceParser/SingleDie.test.cs b/tests/Core/DiceParser/SingleDie.test.cs similarity index 100% rename from Tests/Core/DiceParser/SingleDie.test.cs rename to tests/Core/DiceParser/SingleDie.test.cs diff --git a/Tests/Core/Levels/LevelCalc.test.cs b/tests/Core/Levels/LevelCalc.test.cs similarity index 100% rename from Tests/Core/Levels/LevelCalc.test.cs rename to tests/Core/Levels/LevelCalc.test.cs diff --git a/Tests/Core/Localization/TranslationGuildContext.test.cs b/tests/Core/Localization/TranslationGuildContext.test.cs similarity index 100% rename from Tests/Core/Localization/TranslationGuildContext.test.cs rename to tests/Core/Localization/TranslationGuildContext.test.cs diff --git a/Tests/Core/Localization/Translations.test.cs b/tests/Core/Localization/Translations.test.cs similarity index 100% rename from Tests/Core/Localization/Translations.test.cs rename to tests/Core/Localization/Translations.test.cs diff --git a/Tests/TestData.cs b/tests/TestData.cs similarity index 100% rename from Tests/TestData.cs rename to tests/TestData.cs diff --git a/Tests/Tests.csproj b/tests/Tests.csproj similarity index 100% rename from Tests/Tests.csproj rename to tests/Tests.csproj From bd117e25956b5a5e4f822c42793014600182d59f Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 13 Aug 2020 17:42:33 +0200 Subject: [PATCH 263/443] Fix Translations file path --- src/Core/Localization/TranslationHandler.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core/Localization/TranslationHandler.cs b/src/Core/Localization/TranslationHandler.cs index e38d9a1..c7d35e9 100644 --- a/src/Core/Localization/TranslationHandler.cs +++ b/src/Core/Localization/TranslationHandler.cs @@ -32,7 +32,7 @@ namespace Geekbot.Core.Localization try { // Read the file - var translationFile = File.ReadAllText(Path.GetFullPath("./Lib/Localization/Translations.yml")); + var translationFile = File.ReadAllText(Path.GetFullPath("./Localization/Translations.yml")); // Deserialize var input = new StringReader(translationFile); From d68ce459ef5e9e54af3aff113c46df483ea98aa2 Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 13 Aug 2020 17:47:28 +0200 Subject: [PATCH 264/443] Fix CI Pipeline: tests should be executed from the tests folder (with a lowercase t) --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 410e380..8efd1d1 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -17,7 +17,7 @@ Build: - app script: - dotnet restore - - dotnet test Tests + - dotnet test tests - dotnet publish --version-suffix $VERSION -r linux-x64 -c Release -o ./app ./src/Bot/ Package: From 7942308059ada47dbd14cff9a7b630d00c624344 Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 13 Aug 2020 18:22:09 +0200 Subject: [PATCH 265/443] Alias "!quote save" with "!quote add" --- src/Bot/Commands/Utils/Quote/Quote.cs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Bot/Commands/Utils/Quote/Quote.cs b/src/Bot/Commands/Utils/Quote/Quote.cs index 4022753..9fe1431 100644 --- a/src/Bot/Commands/Utils/Quote/Quote.cs +++ b/src/Bot/Commands/Utils/Quote/Quote.cs @@ -58,9 +58,10 @@ namespace Geekbot.Bot.Commands.Utils.Quote } } - [Command("save")] - [Summary("Save a quote from the last sent message by @user")] - public async Task SaveQuote([Summary("@someone")] IUser user) + [Command("add")] + [Alias("save")] + [Summary("Add a quote from the last sent message by @user")] + public async Task AddQuote([Summary("@someone")] IUser user) { try { @@ -94,9 +95,10 @@ namespace Geekbot.Bot.Commands.Utils.Quote } } - [Command("save")] - [Summary("Save a quote from a message id")] - public async Task SaveQuote([Summary("message-ID")] ulong messageId) + [Command("add")] + [Alias("save")] + [Summary("Add a quote from a message id")] + public async Task AddQuote([Summary("message-ID")] ulong messageId) { try { From ad086a5e0c43d0b7d1181e29d10b5d1e190f6d8e Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 13 Aug 2020 19:51:56 +0200 Subject: [PATCH 266/443] Make it possible to create quotes from message links --- src/Bot/Commands/Utils/Quote/MessageLink.cs | 47 ++++++++++ src/Bot/Commands/Utils/Quote/Quote.cs | 95 ++++++++++++++++++++- src/Core/Localization/Translations.yml | 6 ++ 3 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 src/Bot/Commands/Utils/Quote/MessageLink.cs diff --git a/src/Bot/Commands/Utils/Quote/MessageLink.cs b/src/Bot/Commands/Utils/Quote/MessageLink.cs new file mode 100644 index 0000000..d789d15 --- /dev/null +++ b/src/Bot/Commands/Utils/Quote/MessageLink.cs @@ -0,0 +1,47 @@ +using System; +using System.Data; +using System.Text.RegularExpressions; + +namespace Geekbot.Bot.Commands.Utils.Quote +{ + public class MessageLink + { + public readonly static Regex re = new Regex( + @"https:\/\/(canary|ptb)?.discord(app)?.com\/channels\/(?\d{16,20})\/(?\d{16,20})\/(?\d{16,20})", + RegexOptions.Compiled | RegexOptions.IgnoreCase, + new TimeSpan(0, 0, 2)); + + public ulong GuildId { get; set; } + public ulong ChannelId { get; set; } + public ulong MessageId { get; set; } + + public MessageLink(string url) + { + var matches = re.Matches(url); + + foreach (Match match in matches) + { + foreach (Group matchGroup in match.Groups) + { + switch (matchGroup.Name) + { + case "GuildId": + GuildId = ulong.Parse(matchGroup.Value); + break; + case "ChannelId": + ChannelId = ulong.Parse(matchGroup.Value); + break; + case "MessageId": + MessageId = ulong.Parse(matchGroup.Value); + break; + } + } + } + } + + public static bool IsValid(string link) + { + return re.IsMatch(link); + } + } +} \ No newline at end of file diff --git a/src/Bot/Commands/Utils/Quote/Quote.cs b/src/Bot/Commands/Utils/Quote/Quote.cs index 9fe1431..1e49a84 100644 --- a/src/Bot/Commands/Utils/Quote/Quote.cs +++ b/src/Bot/Commands/Utils/Quote/Quote.cs @@ -82,7 +82,7 @@ namespace Geekbot.Bot.Commands.Utils.Quote if (lastMessage == null) return; var quote = CreateQuoteObject(lastMessage); - _database.Quotes.Add(quote); + await _database.Quotes.AddAsync(quote); await _database.SaveChangesAsync(); var embed = QuoteBuilder(quote); @@ -117,7 +117,61 @@ namespace Geekbot.Bot.Commands.Utils.Quote } var quote = CreateQuoteObject(message); - _database.Quotes.Add(quote); + await _database.Quotes.AddAsync(quote); + await _database.SaveChangesAsync(); + + var embed = QuoteBuilder(quote); + await ReplyAsync(transContext.GetString("QuoteAdded"), false, embed.Build()); + } + catch (Exception e) + { + await _errorHandler.HandleCommandException(e, Context, + "I couldn't find a message with that id :disappointed:"); + } + } + + [Command("add")] + [Alias("save")] + [Summary("Add a quote from a message link")] + public async Task AddQuote([Summary("message-link")] string messageLink) + { + try + { + var transContext = await _translationHandler.GetGuildContext(Context); + + if (!MessageLink.IsValid(messageLink)) + { + await ReplyAsync(transContext.GetString("NotAValidMessageLink")); + return; + } + + var link = new MessageLink(messageLink); + if (link.GuildId != Context.Guild.Id) + { + await ReplyAsync(transContext.GetString("OnlyQuoteFromSameServer")); + return; + } + + var channel = link.ChannelId == Context.Channel.Id + ? Context.Channel + : await Context.Guild.GetTextChannelAsync(link.ChannelId); + + var message = await channel.GetMessageAsync(link.MessageId); + + if (message.Author.Id == Context.Message.Author.Id) + { + await ReplyAsync(transContext.GetString("CannotSaveOwnQuotes")); + return; + } + + if (message.Author.IsBot) + { + await ReplyAsync(transContext.GetString("CannotQuoteBots")); + return; + } + + var quote = CreateQuoteObject(message); + await _database.Quotes.AddAsync(quote); await _database.SaveChangesAsync(); var embed = QuoteBuilder(quote); @@ -167,6 +221,43 @@ namespace Geekbot.Bot.Commands.Utils.Quote } } + [Command("make")] + [Summary("Create a quote from a message link")] + public async Task ReturnSpecifiedQuote([Summary("message-link")] string messageLink) + { + try + { + var transContext = await _translationHandler.GetGuildContext(Context); + + if (!MessageLink.IsValid(messageLink)) + { + await ReplyAsync(transContext.GetString("NotAValidMessageLink")); + return; + } + + var link = new MessageLink(messageLink); + if (link.GuildId != Context.Guild.Id) + { + await ReplyAsync(transContext.GetString("OnlyQuoteFromSameServer")); + return; + } + + var channel = link.ChannelId == Context.Channel.Id + ? Context.Channel + : await Context.Guild.GetTextChannelAsync(link.ChannelId); + + var message = await channel.GetMessageAsync(link.MessageId); + var quote = CreateQuoteObject(message); + var embed = QuoteBuilder(quote); + await ReplyAsync("", false, embed.Build()); + } + catch (Exception e) + { + await _errorHandler.HandleCommandException(e, Context, + "I couldn't find that message :disappointed:"); + } + } + [Command("remove")] [RequireUserPermission(GuildPermission.ManageMessages)] [Summary("Remove a quote (user needs the 'ManageMessages' permission)")] diff --git a/src/Core/Localization/Translations.yml b/src/Core/Localization/Translations.yml index 3c59977..fbb55bf 100644 --- a/src/Core/Localization/Translations.yml +++ b/src/Core/Localization/Translations.yml @@ -169,6 +169,12 @@ quote: MostQuotesPerson: EN: "Most quoted person" CHDE: "Meist quoteti person" + NotAValidMessageLink: + EN: "That is not a valid message link" + CHDE: "Das isch kei korrete nachrichtelink" + OnlyQuoteFromSameServer: + EN: "You can only quote messages from the same server" + CHDE: "Du chasch numme nachrichte vom gliche server quote" rank: InvalidType: EN: "Valid types are '`messages`' '`karma`', '`rolls`' and '`cookies`'" From 726ee77ed4fb3e1fb6933ba2d421be96aaba0888 Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 13 Aug 2020 20:04:54 +0200 Subject: [PATCH 267/443] Fix bug where message links from discord stable would be considered invalid --- src/Bot/Commands/Utils/Quote/MessageLink.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bot/Commands/Utils/Quote/MessageLink.cs b/src/Bot/Commands/Utils/Quote/MessageLink.cs index d789d15..dff1273 100644 --- a/src/Bot/Commands/Utils/Quote/MessageLink.cs +++ b/src/Bot/Commands/Utils/Quote/MessageLink.cs @@ -7,7 +7,7 @@ namespace Geekbot.Bot.Commands.Utils.Quote public class MessageLink { public readonly static Regex re = new Regex( - @"https:\/\/(canary|ptb)?.discord(app)?.com\/channels\/(?\d{16,20})\/(?\d{16,20})\/(?\d{16,20})", + @"https:\/\/((canary|ptb)\.)?discord(app)?.com\/channels\/(?\d{16,20})\/(?\d{16,20})\/(?\d{16,20})", RegexOptions.Compiled | RegexOptions.IgnoreCase, new TimeSpan(0, 0, 2)); From 187fd6a04f95c59044c9369de4ef145576d64982 Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 13 Aug 2020 22:46:53 +0200 Subject: [PATCH 268/443] Remove code duplication from !quote --- src/Bot/Commands/Utils/Quote/Quote.cs | 288 ++++++++++---------------- 1 file changed, 109 insertions(+), 179 deletions(-) diff --git a/src/Bot/Commands/Utils/Quote/Quote.cs b/src/Bot/Commands/Utils/Quote/Quote.cs index 1e49a84..a731273 100644 --- a/src/Bot/Commands/Utils/Quote/Quote.cs +++ b/src/Bot/Commands/Utils/Quote/Quote.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; +using Geekbot.Core; using Geekbot.Core.CommandPreconditions; using Geekbot.Core.Database; using Geekbot.Core.Database.Models; @@ -22,6 +23,7 @@ namespace Geekbot.Bot.Commands.Utils.Quote private readonly DatabaseContext _database; private readonly IRandomNumberGenerator _randomNumberGenerator; private readonly ITranslationHandler _translationHandler; + private readonly bool _isDev; public Quote(IErrorHandler errorHandler, DatabaseContext database, IRandomNumberGenerator randomNumberGenerator, ITranslationHandler translationHandler) { @@ -29,6 +31,8 @@ namespace Geekbot.Bot.Commands.Utils.Quote _database = database; _randomNumberGenerator = randomNumberGenerator; _translationHandler = translationHandler; + // to remove restrictions when developing + _isDev = Constants.BotVersion() == "0.0.0-DEV"; } [Command] @@ -63,36 +67,15 @@ namespace Geekbot.Bot.Commands.Utils.Quote [Summary("Add a quote from the last sent message by @user")] public async Task AddQuote([Summary("@someone")] IUser user) { - try - { - var transContext = await _translationHandler.GetGuildContext(Context); - if (user.Id == Context.Message.Author.Id) - { - await ReplyAsync(transContext.GetString("CannotSaveOwnQuotes")); - return; - } - - if (user.IsBot) - { - await ReplyAsync(transContext.GetString("CannotQuoteBots")); - return; - } - - var lastMessage = await GetLastMessageByUser(user); - if (lastMessage == null) return; - - var quote = CreateQuoteObject(lastMessage); - await _database.Quotes.AddAsync(quote); - await _database.SaveChangesAsync(); - - var embed = QuoteBuilder(quote); - await ReplyAsync(transContext.GetString("QuoteAdded"), false, embed.Build()); - } - catch (Exception e) - { - await _errorHandler.HandleCommandException(e, Context, - "I counldn't find a quote from that user :disappointed:"); - } + await QuoteFromMention(user, true); + } + + [Command("make")] + [Alias("preview")] + [Summary("Preview a quote from the last sent message by @user")] + public async Task ReturnSpecifiedQuote([Summary("@someone")] IUser user) + { + await QuoteFromMention(user, false); } [Command("add")] @@ -100,34 +83,15 @@ namespace Geekbot.Bot.Commands.Utils.Quote [Summary("Add a quote from a message id")] public async Task AddQuote([Summary("message-ID")] ulong messageId) { - try - { - var transContext = await _translationHandler.GetGuildContext(Context); - var message = await Context.Channel.GetMessageAsync(messageId); - if (message.Author.Id == Context.Message.Author.Id) - { - await ReplyAsync(transContext.GetString("CannotSaveOwnQuotes")); - return; - } - - if (message.Author.IsBot) - { - await ReplyAsync(transContext.GetString("CannotQuoteBots")); - return; - } - - var quote = CreateQuoteObject(message); - await _database.Quotes.AddAsync(quote); - await _database.SaveChangesAsync(); - - var embed = QuoteBuilder(quote); - await ReplyAsync(transContext.GetString("QuoteAdded"), false, embed.Build()); - } - catch (Exception e) - { - await _errorHandler.HandleCommandException(e, Context, - "I couldn't find a message with that id :disappointed:"); - } + await QuoteFromMessageId(messageId, true); + } + + [Command("make")] + [Alias("preview")] + [Summary("Preview a quote from a message id")] + public async Task ReturnSpecifiedQuote([Summary("message-ID")] ulong messageId) + { + await QuoteFromMessageId(messageId, false); } [Command("add")] @@ -135,127 +99,15 @@ namespace Geekbot.Bot.Commands.Utils.Quote [Summary("Add a quote from a message link")] public async Task AddQuote([Summary("message-link")] string messageLink) { - try - { - var transContext = await _translationHandler.GetGuildContext(Context); - - if (!MessageLink.IsValid(messageLink)) - { - await ReplyAsync(transContext.GetString("NotAValidMessageLink")); - return; - } - - var link = new MessageLink(messageLink); - if (link.GuildId != Context.Guild.Id) - { - await ReplyAsync(transContext.GetString("OnlyQuoteFromSameServer")); - return; - } - - var channel = link.ChannelId == Context.Channel.Id - ? Context.Channel - : await Context.Guild.GetTextChannelAsync(link.ChannelId); - - var message = await channel.GetMessageAsync(link.MessageId); - - if (message.Author.Id == Context.Message.Author.Id) - { - await ReplyAsync(transContext.GetString("CannotSaveOwnQuotes")); - return; - } - - if (message.Author.IsBot) - { - await ReplyAsync(transContext.GetString("CannotQuoteBots")); - return; - } - - var quote = CreateQuoteObject(message); - await _database.Quotes.AddAsync(quote); - await _database.SaveChangesAsync(); - - var embed = QuoteBuilder(quote); - await ReplyAsync(transContext.GetString("QuoteAdded"), false, embed.Build()); - } - catch (Exception e) - { - await _errorHandler.HandleCommandException(e, Context, - "I couldn't find a message with that id :disappointed:"); - } - } - - [Command("make")] - [Summary("Create a quote from the last sent message by @user")] - public async Task ReturnSpecifiedQuote([Summary("@someone")] IUser user) - { - try - { - var lastMessage = await GetLastMessageByUser(user); - if (lastMessage == null) return; - var quote = CreateQuoteObject(lastMessage); - var embed = QuoteBuilder(quote); - await ReplyAsync("", false, embed.Build()); - } - catch (Exception e) - { - await _errorHandler.HandleCommandException(e, Context, - "I counldn't find a quote from that user :disappointed:"); - } - } - - [Command("make")] - [Summary("Create a quote from a message id")] - public async Task ReturnSpecifiedQuote([Summary("message-ID")] ulong messageId) - { - try - { - var message = await Context.Channel.GetMessageAsync(messageId); - var quote = CreateQuoteObject(message); - var embed = QuoteBuilder(quote); - await ReplyAsync("", false, embed.Build()); - } - catch (Exception e) - { - await _errorHandler.HandleCommandException(e, Context, - "I couldn't find a message with that id :disappointed:"); - } + await QuoteFromMessageLink(messageLink, true); } [Command("make")] - [Summary("Create a quote from a message link")] + [Alias("preview")] + [Summary("Preview a quote from a message link")] public async Task ReturnSpecifiedQuote([Summary("message-link")] string messageLink) { - try - { - var transContext = await _translationHandler.GetGuildContext(Context); - - if (!MessageLink.IsValid(messageLink)) - { - await ReplyAsync(transContext.GetString("NotAValidMessageLink")); - return; - } - - var link = new MessageLink(messageLink); - if (link.GuildId != Context.Guild.Id) - { - await ReplyAsync(transContext.GetString("OnlyQuoteFromSameServer")); - return; - } - - var channel = link.ChannelId == Context.Channel.Id - ? Context.Channel - : await Context.Guild.GetTextChannelAsync(link.ChannelId); - - var message = await channel.GetMessageAsync(link.MessageId); - var quote = CreateQuoteObject(message); - var embed = QuoteBuilder(quote); - await ReplyAsync("", false, embed.Build()); - } - catch (Exception e) - { - await _errorHandler.HandleCommandException(e, Context, - "I couldn't find that message :disappointed:"); - } + await QuoteFromMessageLink(messageLink, false); } [Command("remove")] @@ -340,22 +192,100 @@ namespace Geekbot.Bot.Commands.Utils.Quote } } - private async Task GetLastMessageByUser(IUser user) + private async Task QuoteFromMention(IUser user, bool saveToDb) { try { + var transContext = await _translationHandler.GetGuildContext(Context); + var list = Context.Channel.GetMessagesAsync().Flatten(); - return await list.FirstOrDefaultAsync(msg => + var message = await list.FirstOrDefaultAsync(msg => msg.Author.Id == user.Id && msg.Embeds.Count == 0 && msg.Id != Context.Message.Id && !msg.Content.ToLower().StartsWith("!")); + if (message == null) return; + + await ProcessQuote(message, saveToDb, transContext); } - catch + catch (Exception e) { - await ReplyAsync($"No quoteable message have been sent by {user.Username} in this channel"); - return null; + await _errorHandler.HandleCommandException(e, Context, $"No quoteable messages have been sent by {user.Username} in this channel"); } + + } + + private async Task QuoteFromMessageId(ulong messageId, bool saveToDb) + { + try + { + var transContext = await _translationHandler.GetGuildContext(Context); + var message = await Context.Channel.GetMessageAsync(messageId); + + await ProcessQuote(message, saveToDb, transContext); + } + catch (Exception e) + { + await _errorHandler.HandleCommandException(e, Context, "I couldn't find a message with that id :disappointed:"); + } + } + + private async Task QuoteFromMessageLink(string messageLink, bool saveToDb) + { + try + { + var transContext = await _translationHandler.GetGuildContext(Context); + + if (!MessageLink.IsValid(messageLink)) + { + await ReplyAsync(transContext.GetString("NotAValidMessageLink")); + return; + } + + var link = new MessageLink(messageLink); + if (link.GuildId != Context.Guild.Id) + { + await ReplyAsync(transContext.GetString("OnlyQuoteFromSameServer")); + return; + } + + var channel = link.ChannelId == Context.Channel.Id + ? Context.Channel + : await Context.Guild.GetTextChannelAsync(link.ChannelId); + + var message = await channel.GetMessageAsync(link.MessageId); + + await ProcessQuote(message, saveToDb, transContext); + } + catch (Exception e) + { + await _errorHandler.HandleCommandException(e, Context, "I couldn't find that message :disappointed:"); + } + } + + private async Task ProcessQuote(IMessage message, bool saveToDb, TranslationGuildContext transContext) + { + if (message.Author.Id == Context.Message.Author.Id && saveToDb && !_isDev) + { + await ReplyAsync(transContext.GetString("CannotSaveOwnQuotes")); + return; + } + + if (message.Author.IsBot && saveToDb && !_isDev) + { + await ReplyAsync(transContext.GetString("CannotQuoteBots")); + return; + } + + var quote = CreateQuoteObject(message); + if (saveToDb) + { + await _database.Quotes.AddAsync(quote); + await _database.SaveChangesAsync(); + } + + var embed = QuoteBuilder(quote); + await ReplyAsync(saveToDb ? transContext.GetString("QuoteAdded") : string.Empty, false, embed.Build()); } private EmbedBuilder QuoteBuilder(QuoteModel quote) From 12388fd7d04faff06c43371c922df337b5a4dead Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 14 Aug 2020 00:12:14 +0200 Subject: [PATCH 269/443] Curate Media Files --- src/Bot/Storage/croissant | 13 +++---------- src/Bot/Storage/foxes | 31 +++++++++++-------------------- src/Bot/Storage/pandas | 5 +++-- src/Bot/Storage/penguins | 22 ++++++++++++---------- src/Bot/Storage/pumpkin | 23 +++++++---------------- src/Bot/Storage/squirrel | 28 ++++------------------------ src/Bot/Storage/turtles | 13 ++++++------- 7 files changed, 46 insertions(+), 89 deletions(-) diff --git a/src/Bot/Storage/croissant b/src/Bot/Storage/croissant index 281b790..6b4897f 100644 --- a/src/Bot/Storage/croissant +++ b/src/Bot/Storage/croissant @@ -3,15 +3,8 @@ http://www.bakespace.com/images/large/5d79070cf21b2f33c3a1dd4336cb27d2.jpeg http://food.fnr.sndimg.com/content/dam/images/food/fullset/2015/5/7/1/SD1B43_croissants-recipe_s4x3.jpg.rend.hgtvcom.616.462.suffix/1431052139248.jpeg http://img.taste.com.au/u-Bwjfm_/taste/2016/11/mini-croissants-with-3-fillings-14692-1.jpeg https://media.newyorker.com/photos/590974702179605b11ad8096/16:9/w_1200,h_630,c_limit/Gopnik-TheMurkyMeaningsofStraightenedOutCroissants.jpg -http://bt.static-redmouse.ch/sites/bielertagblatt.ch/files/styles/bt_article_showroom_landscape/hash/84/c9/84c9aed08415265911ec05c46d25d3ef.jpg?itok=hP5PnHaT -https://www.dermann.at/wp-content/uploads/Schokocroissant_HPBild_1400x900px.jpeg -https://www.bettybossi.ch/static/rezepte/x/bb_bkxx060101_0360a_x.jpg -http://www.engel-beck.ch/uploads/pics/tete-de-moine-gipfel-.jpg https://storage.cpstatic.ch/storage/og_image/laugengipfel--425319.jpg -https://www.backhaus-kutzer.de/fileadmin/templates/Resources/Public/img/produkte/suesses-gebaeck/Milchhoernchen.png -https://www.kuechengoetter.de/uploads/media/1000x524/00/36390-vanillekipferl-0.jpg?v=1-0 https://c1.staticflickr.com/3/2835/10874180753_2b2916e3ce_b.jpg -http://www.mistercool.ch/wp-content/uploads/2017/02/Gipfel-mit-Cerealien-7168.png -https://scontent-sea1-1.cdninstagram.com/t51.2885-15/s480x480/e35/c40.0.999.999/15099604_105396696611384_2866237281000226816_n.jpg?ig_cache_key=MTM4MzQxOTU1MDc5NjUxNzcwMA%3D%3D.2.c -http://www.lecrobag.de/wp-content/uploads/2014/03/Wurst_2014_l.jpg -https://www.thecookierookie.com/wp-content/uploads/2017/02/sheet-pan-chocolate-croissants-collage1.jpeg \ No newline at end of file +https://www.spatz-dessert.ch/image_upload/Laugengipfel.jpg +http://www.baeckerei-meier.ch/images/p005_1_03.png +http://i.huffpost.com/gen/1278175/thumbs/o-CROISSANT-facebook.jpg \ No newline at end of file diff --git a/src/Bot/Storage/foxes b/src/Bot/Storage/foxes index 020c1cf..52fd9d2 100644 --- a/src/Bot/Storage/foxes +++ b/src/Bot/Storage/foxes @@ -1,29 +1,20 @@ https://i.ytimg.com/vi/qF6OOGuT_hI/maxresdefault.jpg -https://www.hd-wallpapersdownload.com/script/bulk-upload/desktop-funny-fox-wallpaper.jpg -http://moziru.com/images/drawn-fox-funny-18.jpg https://static.tumblr.com/bb34d8f163098ad1daafcffbdbb03975/rk23uap/Nwwp0rmi2/tumblr_static_tumblr_static__640.jpg -https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQoHUFOnZ3wJ2kT1skNdztFXXSvpU8bEoGS1alNZiuyLXvGJhcY -http://childrenstorytales.com/wp-content/uploads/2011/03/how-to-draw-a-red-fox-in-the-snow.jpg https://www.popsci.com/sites/popsci.com/files/styles/1000_1x_/public/import/2013/images/2013/09/redfoxyawn.jpg?itok=yRkSVe8T https://hdqwalls.com/wallpapers/wild-fox-art.jpg -https://ae01.alicdn.com/kf/HTB1Q9dpLpXXXXbhXpXXq6xXFXXXl/new-cute-fox-toy-lifelike-soft-long-yellow-fox-doll-gift-about-73cm.jpg_640x640.jpg https://i.imgur.com/ktK9yXX.jpg -https://res.cloudinary.com/teepublic/image/private/s--yTx2ncFA--/t_Preview/b_rgb:c8e0ec,c_limit,f_auto,h_313,q_90,w_313/v1506478249/production/designs/1932607_0 http://4.bp.blogspot.com/-Hz-o_KYj3Xk/Vlm2mwbztjI/AAAAAAAA8Ss/jbH5ovjmC9A/s1600/ScreenShot5502.jpg -https://i.pinimg.com/originals/1e/d5/2f/1ed52f70873a95ac02fa074e48edfb71.jpg -https://i.imgur.com/2vCrtap.jpg -https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSfukWGu_IBaDeJOMBqOhVAwsDfqEPw0BFpCn5_-Iyr_xjd7zi9 -https://cdn.pixabay.com/photo/2017/01/31/18/36/animal-2026297_960_720.png -https://i.pinimg.com/originals/e2/63/67/e26367a0844633b2a697b0a9d69e8cc9.jpg -https://i.ebayimg.com/images/g/BvkAAOSwqxdTqrip/s-l300.jpg -https://res.cloudinary.com/teepublic/image/private/s--1R53bger--/t_Preview/b_rgb:eae0c7,c_limit,f_jpg,h_630,q_90,w_630/v1481013120/production/designs/914528_1.jpg -https://i.pinimg.com/originals/97/fe/69/97fe698462afde7b4209ccefeecbce71.jpg -https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT6G0ch6g-wG1TuDJ6BbkOFelMNnkgFXC6CxOw7qSNjoFkx-BCe https://wallpaperscraft.com/image/fox_forest_grass_117190_540x960.jpg -https://image.freepik.com/free-vector/cartoon-flat-illustration-funny-cute-fox_6317-1174.jpg https://orig00.deviantart.net/2feb/f/2013/137/a/f/fox_and_curious_squirrel_by_tamarar-d65ju8d.jpg -https://res.cloudinary.com/teepublic/image/private/s--dICeNmBx--/t_Preview/b_rgb:6e2229,c_limit,f_jpg,h_630,q_90,w_630/v1505243196/production/designs/1890493_1.jpg -https://vignette.wikia.nocookie.net/puppyinmypocketfanon/images/4/49/L-Baby-Fox.jpg/revision/latest?cb=20130421001806 -http://7-themes.com/data_images/out/69/7009194-fox-puppy.jpg http://www.tehcute.com/pics/201401/little-fox-big.jpg -https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR6QXB1APLdUsyzO39kPvhnC9cOvcwzEtsxown9QjWilWppia2mwg \ No newline at end of file +https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR6QXB1APLdUsyzO39kPvhnC9cOvcwzEtsxown9QjWilWppia2mwg +https://www.wildlifeaid.org.uk/wp-content/uploads/2016/03/FP9_July09.jpg +https://upload.wikimedia.org/wikipedia/commons/thumb/b/bb/Vulpes_vulpes_ssp_fulvus_6568085.jpg/1200px-Vulpes_vulpes_ssp_fulvus_6568085.jpg +http://images.hellogiggles.com/uploads/2017/06/10023347/fox1.jpg +https://i.ytimg.com/vi/mtroFou8Xb4/maxresdefault.jpg +http://wallpapers-best.com/uploads/posts/2015-09/20_fox.jpg +https://www.whats-your-sign.com/wp-content/uploads/2018/02/RedFoxSymbolism4.jpg +https://cdn.zmescience.com/wp-content/uploads/2016/09/8505162700_11394c3f6a_b.jpg +http://wallpapers-best.com/uploads/posts/2015-09/18_fox.jpg +https://s.abcnews.com/images/General/red-fox-new-jersey-gty-jt-191119_hpMain_16x9_992.jpg +https://i.ytimg.com/vi/ClNRWL_9L8s/maxresdefault.jpg \ No newline at end of file diff --git a/src/Bot/Storage/pandas b/src/Bot/Storage/pandas index 9d5046c..6c6d725 100644 --- a/src/Bot/Storage/pandas +++ b/src/Bot/Storage/pandas @@ -4,8 +4,9 @@ https://nationalzoo.si.edu/sites/default/files/styles/slide_1400x700/public/supp https://media4.s-nbcnews.com/j/newscms/2016_36/1685951/ss-160826-twip-05_8cf6d4cb83758449fd400c7c3d71aa1f.nbcnews-ux-2880-1000.jpg https://ichef-1.bbci.co.uk/news/660/cpsprodpb/169F6/production/_91026629_gettyimages-519508400.jpg https://cdn.history.com/sites/2/2017/03/GettyImages-157278376.jpg -https://www.pandasinternational.org/wptemp/wp-content/uploads/2012/10/slider1.jpg https://tctechcrunch2011.files.wordpress.com/2015/11/panda.jpg http://www.nationalgeographic.com/content/dam/magazine/rights-exempt/2016/08/departments/panda-mania-12.jpg http://animals.sandiegozoo.org/sites/default/files/2016-09/panda1_10.jpg -http://kids.nationalgeographic.com/content/dam/kids/photos/animals/Mammals/A-G/giant-panda-eating.adapt.945.1.jpg \ No newline at end of file +http://kids.nationalgeographic.com/content/dam/kids/photos/animals/Mammals/A-G/giant-panda-eating.adapt.945.1.jpg +https://static.independent.co.uk/s3fs-public/thumbnails/image/2015/10/08/15/Hong-Kong-pandas.jpg +https://3sn4dm1qd6i72l8a4r2ig7fl-wpengine.netdna-ssl.com/wp-content/uploads/2016/11/panda_lunlun_ZA_2083-b.jpg \ No newline at end of file diff --git a/src/Bot/Storage/penguins b/src/Bot/Storage/penguins index d38d853..95aa70d 100644 --- a/src/Bot/Storage/penguins +++ b/src/Bot/Storage/penguins @@ -1,13 +1,15 @@ -https://i.ytimg.com/vi/Qr6sULJnu2o/maxresdefault.jpg -https://www.apex-expeditions.com/wp-content/uploads/2015/08/newzealandSlider_Macquarie_ElephantSealKingPenguins_GRiehle_1366x601.jpg https://www.birdlife.org/sites/default/files/styles/1600/public/slide.jpg?itok=HRhQfA1S http://experimentexchange.com/wp-content/uploads/2016/07/penguins-fact.jpg -http://images.mentalfloss.com/sites/default/files/styles/mf_image_16x9/public/istock-511366776.jpg?itok=cWhdWNZ8&resize=1100x619 -https://www.thevaporplace.ch/media/catalog/product/cache/1/thumbnail/800x800/9df78eab33525d08d6e5fb8d27136e95/a/t/atopack_penguin-15.jpg -https://www.superfastbusiness.com/wp-content/uploads/2015/10/real-time-penguin-algorithm-featured.jpg http://www.antarctica.gov.au/__data/assets/image/0011/147737/varieties/antarctic.jpg -https://vignette.wikia.nocookie.net/robloxcreepypasta/images/1/11/AAEAAQAAAAAAAAdkAAAAJDc3YzkyYjJhLTYyZjctNDY2Mi04M2VjLTg4NjY4ZjgwYzRmNg.png/revision/latest?cb=20180207200526 -https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR3xV0lhpZuhT8Nmm6LaITsppZ7VfWcWXuyu2cPHrlv_dt_M92K5g -http://goboiano.com/wp-content/uploads/2017/04/Penguin-Kemeno-Friends-Waifu.jpg -https://cdn.yoast.com/app/uploads/2015/10/Penguins_1200x628.png -https://images.justwatch.com/backdrop/8611153/s1440/pingu \ No newline at end of file +https://images.justwatch.com/backdrop/8611153/s1440/pingu +http://4.bp.blogspot.com/-VhmPPCcZnwA/TWR303DSAuI/AAAAAAAAABU/eSSokmd376s/s1600/2-Penguins-penguins-4234010-1280-1024.jpg +https://media.glamour.com/photos/56959e35d9dab9ff41b308a0/master/pass/inspired-2015-02-gentoo-penguin-main.jpg +https://indiansciencejournal.files.wordpress.com/2012/04/emperor-penguin-credit-british-antarctic-survey.jpg +https://fthmb.tqn.com/7cd9Q3LSapEShHq2mKvQgSPr_tc=/2250x1500/filters:fill(auto,1)/149267744-56a008755f9b58eba4ae8f46.jpg +https://blogs.voanews.com/science-world/files/2014/07/11240219084_941dfbf66e_b.jpg +https://blogs.biomedcentral.com/bmcseriesblog/wp-content/uploads/sites/9/2015/11/IMG_5391-2.jpg +https://i2-prod.mirror.co.uk/incoming/article11682518.ece/ALTERNATES/s615/Emperor-penguins-on-ice.jpg +https://www.gannett-cdn.com/presto/2019/04/15/PPHX/8dfe0433-c22c-4458-9ba8-3aab866774f8-Penguins5cad7bfd107a4.jpg?crop=5759,3224,x0,y0&width=3200&height=1680&fit=bounds +http://4.bp.blogspot.com/_FNQgkfCwYxs/S82jBAxMVEI/AAAAAAAAAns/_3lAuJhUfcs/s1600/311785583_af8f2d1ea7_o.jpg +http://wallsdesk.com/wp-content/uploads/2017/01/Pictures-of-Penguin-.jpg +http://2.bp.blogspot.com/_W90V87w3sr8/TP3RPYwrrjI/AAAAAAAAAXk/riN0GwRwhFM/s1600/leap-of-faith-adelie-penguin-pictures.jpg \ No newline at end of file diff --git a/src/Bot/Storage/pumpkin b/src/Bot/Storage/pumpkin index 4b8e6f2..3652397 100644 --- a/src/Bot/Storage/pumpkin +++ b/src/Bot/Storage/pumpkin @@ -1,23 +1,14 @@ https://i.pinimg.com/736x/0a/a7/8a/0aa78af25e114836e1a42585fb7b09ed--funny-pumpkins-pumkin-carving.jpg http://wdy.h-cdn.co/assets/16/31/980x1470/gallery-1470321728-shot-two-021.jpg -https://i.pinimg.com/736x/6c/62/bf/6c62bfa73a19ffd9fc6f2d720d5e9764--cool-pumpkin-carving-carving-pumpkins.jpg http://images6.fanpop.com/image/photos/38900000/Jack-o-Lantern-halloween-38991566-500-415.jpg http://ghk.h-cdn.co/assets/15/37/1441834730-pumpkin-carve-2.jpg http://diy.sndimg.com/content/dam/images/diy/fullset/2011/7/26/1/iStock-10761186_halloween-pumpkin-in-garden_s4x3.jpg.rend.hgtvcom.966.725.suffix/1420851319631.jpeg -http://ghk.h-cdn.co/assets/cm/15/11/54ffe537af882-snail-pumpkin-de.jpg https://www.digsdigs.com/photos/2009/10/100-halloween-pumpkin-carving-ideas-12.jpg -http://diy.sndimg.com/content/dam/images/diy/fullset/2010/6/4/0/CI-Kyle-Nishioka_big-teeth-Jack-O-Lantern_s4x3.jpg.rend.hgtvcom.966.725.suffix/1420699522718.jpeg -https://twistedsifter.files.wordpress.com/2011/10/most-amazing-pumpkin-carving-ray-villafane-10.jpg?w=521&h=739 -https://i.pinimg.com/736x/09/c4/b1/09c4b187b266c1f65332294f66009944--funny-pumpkins-halloween-pumpkins.jpg -http://www.evilmilk.com/pictures/The_Pumpkin_Man.jpg -http://cache.lovethispic.com/uploaded_images/blogs/13-Funny-Pumpkin-Carvings-5773-9.JPG -http://ihappyhalloweenpictures.com/wp-content/uploads/2016/10/funny-halloween-pumpkin.jpg -http://www.smallhomelove.com/wp-content/uploads/2012/08/leg-eating-pumpkin.jpg -https://cdn.shopify.com/s/files/1/0773/6789/articles/Halloween_Feature_8ff7a7c4-2cb3-4584-a85f-5d4d1e6ca26e.jpg?v=1476211360 -http://4vector.com/i/free-vector-pumpkin-boy-color-version-clip-art_107714_Pumpkin_Boy_Color_Version_clip_art_hight.png https://i.pinimg.com/736x/59/8a/0f/598a0fbf789631b76c1ffd4443194d8e--halloween-pumpkins-fall-halloween.jpg -https://i.pinimg.com/originals/8f/86/f9/8f86f95457467872b371ba697d341961.jpg -http://nerdist.com/wp-content/uploads/2015/08/taleshalloween1.jpg -http://www.designbolts.com/wp-content/uploads/2014/09/Scary-Pumpkin_Grin_stencil-Ideas.jpg -http://vignette2.wikia.nocookie.net/scoobydoo/images/7/75/Pumpkin_monsters_%28Witch%27s_Ghost%29.png/revision/latest?cb=20140520070213 -https://taholtorf.files.wordpress.com/2013/10/36307-1920x1280.jpg +http://i.huffpost.com/gen/1405530/images/o-PUMPKINS-facebook.jpg +https://www.reviewjournal.com/wp-content/uploads/2016/10/web1_thinkstockphotos-491684958_7239666.jpg +https://img.sunset02.com/sites/default/files/1494265591/pumpkins-growing-on-farm-getty-sun-0517.jpg +https://toronto.citynews.ca/wp-content/blogs.dir/sites/10/2015/10/06/pumpkin-patch.jpg +http://i.huffpost.com/gen/3494726/images/o-PUMPKIN-facebook.jpg +https://servingjoy.com/wp-content/uploads/2014/12/Beautiful-autumn-halloween-pumpkins.jpg +https://www.history.com/.image/t_share/MTU3ODc5MDg2NDI4OTg4NzQ1/still-life-of-a-jack-o-lantern.jpg \ No newline at end of file diff --git a/src/Bot/Storage/squirrel b/src/Bot/Storage/squirrel index 2216465..91fd240 100644 --- a/src/Bot/Storage/squirrel +++ b/src/Bot/Storage/squirrel @@ -1,45 +1,25 @@ -http://orig14.deviantart.net/6016/f/2010/035/c/b/first_squirrel_assassin_by_shotokanteddy.jpg -https://thumbs-prod.si-cdn.com/eoEYA_2Hau4795uKoecUZZgz-3w=/800x600/filters:no_upscale()/https://public-media.smithsonianmag.com/filer/52/f9/52f93262-c29b-4a4f-b031-0c7ad145ed5f/42-33051942.jpg +https://public-media.smithsonianmag.com/filer/52/f9/52f93262-c29b-4a4f-b031-0c7ad145ed5f/42-33051942.jpg http://images5.fanpop.com/image/photos/30700000/Squirrel-squirrels-30710732-400-300.jpg -https://www.lovethegarden.com/sites/default/files/files/Red%20%26%20Grey%20Squirrel%20picture%20side%20by%20side-LR.jpg http://i.dailymail.co.uk/i/pix/2016/02/24/16/158F7E7C000005DC-3462228-image-a-65_1456331226865.jpg http://2.bp.blogspot.com/-egfnMhUb8tg/T_dAIu1m6cI/AAAAAAAAPPU/v4x9q4WqWl8/s640/cute-squirrel-hey-watcha-thinkin-about.jpg https://upload.wikimedia.org/wikipedia/commons/thumb/1/1c/Squirrel_posing.jpg/287px-Squirrel_posing.jpg https://i.pinimg.com/736x/51/db/9b/51db9bad4a87d445d321923c7d56b501--red-squirrel-animal-kingdom.jpg -https://metrouk2.files.wordpress.com/2016/10/ad_223291521.jpg?w=620&h=949&crop=1 -http://www.redsquirrelsunited.org.uk/wp-content/uploads/2016/07/layer-slider.jpg -http://images.mentalfloss.com/sites/default/files/squirrel-hero.jpg?resize=1100x740 https://i.pinimg.com/736x/ce/9c/59/ce9c5990b193046400d98724595cdaf3--red-squirrel-chipmunks.jpg https://www.brooklynpaper.com/assets/photos/40/30/dtg-squirrel-attacks-prospect-park-patrons-2017-07-28-bk01_z.jpg -http://www.freakingnews.com/pictures/16000/Squirrel-Shark-16467.jpg -http://img09.deviantart.net/5c1c/i/2013/138/0/6/barbarian_squirel_by_coucoucmoa-d64r9m4.jpg https://i.pinimg.com/736x/b4/5c/0d/b45c0d00b1a57e9f84f27f13cb019001--baby-squirrel-red-squirrel.jpg https://i.pinimg.com/736x/0f/75/87/0f7587bb613ab524763afe8c9a532e5c--cute-squirrel-squirrels.jpg http://cdn.images.express.co.uk/img/dynamic/128/590x/Grey-squirrel-828838.jpg http://www.lovethispic.com/uploaded_images/79964-Squirrel-Smelling-A-Flower.jpg https://i.pinimg.com/736x/23/d5/f9/23d5f9868f7d76c79c49bef53ae08f7f--squirrel-funny-red-squirrel.jpg -http://stories.barkpost.com/wp-content/uploads/2016/01/squirrel-3-copy.jpg https://i.ytimg.com/vi/pzUs0DdzK3Y/hqdefault.jpg -https://www.askideas.com/media/41/I-Swear-It-Wasnt-Me-Funny-Squirrel-Meme-Picture-For-Facebook.jpg -https://i.pinimg.com/736x/2d/54/d8/2d54d8d2a9b3ab9d3e78544b75afd88e--funny-animal-pictures-humorous-pictures.jpg -http://www.funny-animalpictures.com/media/content/items/images/funnysquirrels0012_O.jpg -http://funny-pics.co/wp-content/uploads/funny-squirrel-and-coffee-picture.jpg -https://pbs.twimg.com/media/Bi4Ij6CIgAAgEdZ.jpg -http://www.funnyjunksite.com/pictures/wp-content/uploads/2015/06/Funny-Superman-Squirrels.jpg -https://i.pinimg.com/736x/bf/35/00/bf3500104f8394909d116259d1f0575e--funny-squirrel-squirrel-girl.jpg -http://quotespill.com/wp-content/uploads/2017/07/Squirrel-Meme-Draw-me-like-one-of-your-french-squirrrels-min.jpg https://i.pinimg.com/736x/e2/16/bb/e216bba53f80fc8e0111d371e9850159--funny-squirrels-cute-squirrel.jpg https://i.pinimg.com/736x/52/43/c9/5243c93377245be1f686218c266d775c--funny-squirrel-baby-squirrel.jpg https://i.pinimg.com/736x/0c/be/1d/0cbe1da8ad2c0cf3882a806b6fd88965--cute-pictures-funny-animal-pictures.jpg -https://i.pinimg.com/736x/e5/08/67/e508670aa00ca3c896eccb81c4f6e2a8--funny-squirrel-baby-squirrel.jpg https://i.pinimg.com/736x/1c/7d/4f/1c7d4f067a10066aad802ce5ac468d71--group-boards-a-squirrel.jpg -http://funny-pics.co/wp-content/uploads/funny-squirrel-on-a-branch.jpg -http://loldamn.com/wp-content/uploads/2016/06/funny-squirrel-playing-water-bending.jpg -https://cdn.trendhunterstatic.com/thumbs/squirrel-photography.jpeg https://i.pinimg.com/736x/d6/42/12/d64212cc6221916db4173962bf6c131a--cute-squirrel-baby-squirrel.jpg -https://i.pinimg.com/236x/10/13/58/101358f2afc2c7d6b6a668046e7b8382--funny-animal-pictures-funny-animals.jpg https://i.pinimg.com/736x/da/0d/fe/da0dfe93bb26887795f906e8fa97d68e--secret-squirrel-cute-squirrel.jpg http://2.bp.blogspot.com/-HLieBqEuQoM/UDkRmeyzB5I/AAAAAAAABHs/RtsEynn5t6Y/s1600/hd-squirrel-wallpaper-with-a-brown-squirrel-eating-watermelon-wallpapers-backgrounds-pictures-photos.jpg -http://www.city-data.com/forum/members/brenda-starz-328928-albums-brenda-s-funny-squirrel-comment-pic-s-pic5075-punk-squirrels.jpg http://img15.deviantart.net/9c50/i/2011/213/c/9/just_taking_it_easy_by_lou_in_canada-d42do3d.jpg -http://3.bp.blogspot.com/-AwsSk76R2Is/USQa3-dszKI/AAAAAAAABUQ/KF_F8HbtP1U/w1200-h630-p-k-no-nu/crazySquirrel.jpg +https://insider.si.edu/wp-content/uploads/2018/01/Chipmunk-Photo-Mark-Rounds.jpg +https://media.mnn.com/assets/images/2014/12/gray-squirrel-uc-berkeley.jpg.1080x0_q100_crop-scale.jpg +https://citywildlife.org/wp-content/uploads/Juvenile-squirrel.jpg \ No newline at end of file diff --git a/src/Bot/Storage/turtles b/src/Bot/Storage/turtles index 9dbbf72..aa0fbcf 100644 --- a/src/Bot/Storage/turtles +++ b/src/Bot/Storage/turtles @@ -1,21 +1,20 @@ https://i.guim.co.uk/img/media/6b9be13031738e642f93f9271f3592044726a9b1/0_0_2863_1610/2863.jpg?w=640&h=360&q=55&auto=format&usm=12&fit=max&s=85f3b33cc158b5aa120c143dae1916ed http://cf.ltkcdn.net/small-pets/images/std/212089-676x450-Turtle-feeding-on-leaf.jpg -https://static1.squarespace.com/static/5369465be4b0507a1fd05af0/53767a6be4b0ad0822345e52/57e40ba4893fc031e05a018f/1498243318058/solvin.jpg?format=1500w https://c402277.ssl.cf1.rackcdn.com/photos/419/images/story_full_width/HI_287338Hero.jpg?1433950119 https://www.cdc.gov/salmonella/agbeni-08-17/images/turtle.jpg https://cdn.arstechnica.net/wp-content/uploads/2017/08/GettyImages-524757168.jpg http://pmdvod.nationalgeographic.com/NG_Video/595/319/4504517_098_05_TOS_thumbnail_640x360_636296259676.jpg -http://cdn1.arkive.org/media/7D/7D46329A-6ED2-4F08-909E-7B596417994A/Presentation.Large/Big-headed-turtle-close-up.jpg http://s7d2.scene7.com/is/image/PetSmart/ARTHMB-CleaningYourTortoiseOrTurtlesHabitat-20160818?$AR1104$ https://fthmb.tqn.com/9VGWzK_GWlvrjxtdFPX6EJxOq24=/960x0/filters:no_upscale()/133605352-56a2bce53df78cf7727960db.jpg -https://i.imgur.com/46QmzgF.jpg https://www.wildgratitude.com/wp-content/uploads/2015/07/turtle-spirit-animal1.jpg http://www.backwaterreptiles.com/images/turtles/red-eared-slider-turtle-for-sale.jpg -https://i.pinimg.com/736x/f1/f4/13/f1f413d6d07912be6080c08b186630ac--happy-turtle-funny-stuff.jpg -http://www.dupageforest.org/uploadedImages/Content/District_News/Nature_Stories/2016/Snapping%20Turtle%20Scott%20Plantier%20STP4793.jpg http://turtlebackzoo.com/wp-content/uploads/2016/07/exhibit-headers_0008_SOUTH-AMERICA-600x400.jpg -https://i.ytimg.com/vi/_YfYHFM3Das/maxresdefault.jpg https://i.pinimg.com/736x/dd/4e/7f/dd4e7f2f921ac28b1d5a59174d477131--cute-baby-sea-turtles-adorable-turtles.jpg http://kids.nationalgeographic.com/content/dam/kids/photos/animals/Reptiles/A-G/green-sea-turtle-closeup-underwater.adapt.945.1.jpg -https://i.ytimg.com/vi/p4Jj9QZFJvw/hqdefault.jpg https://fthmb.tqn.com/nirxHkH3jBAe74ife6fJJu6k6q8=/2121x1414/filters:fill(auto,1)/Red-eared-sliders-GettyImages-617946009-58fae8835f9b581d59a5bab6.jpg +http://assets.worldwildlife.org/photos/167/images/original/MID_225023-circle-hawksbill-turtle.jpg?1345565600 +https://seaturtles.org/wp-content/uploads/2013/11/GRN-honuAnitaWintner2.jpg +https://images2.minutemediacdn.com/image/upload/c_crop,h_2549,w_4536,x_0,y_237/v1560186367/shape/mentalfloss/istock-687398754.jpg?itok=QsiF5yHP +https://c402277.ssl.cf1.rackcdn.com/photos/13028/images/story_full_width/seaturtle_spring2017.jpg?1485359391 +https://i2.wp.com/rangerrick.org/wp-content/uploads/2018/03/Turtle-Tale-RR-Jr-June-July-2017.jpg?fit=1156%2C650&ssl=1 +https://boyslifeorg.files.wordpress.com/2019/07/greenseaturtle.jpg \ No newline at end of file From 90af781c7ba5f484ba34ef714c84aab1d455f9d2 Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 14 Aug 2020 03:30:54 +0200 Subject: [PATCH 270/443] Start Using resource files (.resx) for translations. Create GeekbotCommandBase to reduce command boilerplate. Convert admin, choose, cookies, karma, quote, rank, roll and ship to the new localization method. --- src/Bot/Bot.csproj | 88 ++++++++++ src/Bot/Commands/Admin/Admin.cs | 71 +++++--- src/Bot/Commands/Games/Roll/Roll.cs | 23 ++- src/Bot/Commands/Randomness/Ship.cs | 29 ++-- src/Bot/Commands/Rpg/Cookies.cs | 38 ++--- src/Bot/Commands/User/Karma.cs | 47 +++--- src/Bot/Commands/User/Ranking/Rank.cs | 40 ++--- src/Bot/Commands/Utils/Choose.cs | 15 +- src/Bot/Commands/Utils/Quote/Quote.cs | 61 +++---- src/Bot/Localization/Admin.Designer.cs | 81 ++++++++++ src/Bot/Localization/Admin.de-ch.resx | 20 +++ src/Bot/Localization/Admin.resx | 27 ++++ src/Bot/Localization/Choose.Designer.cs | 72 +++++++++ src/Bot/Localization/Choose.de-ch.resx | 17 ++ src/Bot/Localization/Choose.resx | 24 +++ src/Bot/Localization/Cookies.Designer.cs | 126 +++++++++++++++ src/Bot/Localization/Cookies.de-ch.resx | 35 ++++ src/Bot/Localization/Cookies.resx | 42 +++++ src/Bot/Localization/Internal.Designer.cs | 126 +++++++++++++++ src/Bot/Localization/Internal.de-ch.resx | 35 ++++ src/Bot/Localization/Internal.resx | 42 +++++ src/Bot/Localization/Karma.Designer.cs | 135 ++++++++++++++++ src/Bot/Localization/Karma.de-ch.resx | 38 +++++ src/Bot/Localization/Karma.resx | 45 ++++++ src/Bot/Localization/Quote.Designer.cs | 162 +++++++++++++++++++ src/Bot/Localization/Quote.de-ch.resx | 47 ++++++ src/Bot/Localization/Quote.resx | 54 +++++++ src/Bot/Localization/Rank.Designer.cs | 108 +++++++++++++ src/Bot/Localization/Rank.de-ch.resx | 29 ++++ src/Bot/Localization/Rank.resx | 36 +++++ src/Bot/Localization/Roll.Designer.cs | 99 ++++++++++++ src/Bot/Localization/Roll.de-ch.resx | 26 +++ src/Bot/Localization/Roll.resx | 33 ++++ src/Bot/Localization/Ship.Designer.cs | 117 ++++++++++++++ src/Bot/Localization/Ship.de-ch.resx | 32 ++++ src/Bot/Localization/Ship.resx | 39 +++++ src/Core/GeekbotCommandBase.cs | 27 ++++ src/Core/Localization/ITranslationHandler.cs | 1 + src/Core/Localization/TranslationHandler.cs | 16 +- src/Core/Localization/Translations.yml | 159 +----------------- 40 files changed, 1935 insertions(+), 327 deletions(-) create mode 100644 src/Bot/Localization/Admin.Designer.cs create mode 100644 src/Bot/Localization/Admin.de-ch.resx create mode 100644 src/Bot/Localization/Admin.resx create mode 100644 src/Bot/Localization/Choose.Designer.cs create mode 100644 src/Bot/Localization/Choose.de-ch.resx create mode 100644 src/Bot/Localization/Choose.resx create mode 100644 src/Bot/Localization/Cookies.Designer.cs create mode 100644 src/Bot/Localization/Cookies.de-ch.resx create mode 100644 src/Bot/Localization/Cookies.resx create mode 100644 src/Bot/Localization/Internal.Designer.cs create mode 100644 src/Bot/Localization/Internal.de-ch.resx create mode 100644 src/Bot/Localization/Internal.resx create mode 100644 src/Bot/Localization/Karma.Designer.cs create mode 100644 src/Bot/Localization/Karma.de-ch.resx create mode 100644 src/Bot/Localization/Karma.resx create mode 100644 src/Bot/Localization/Quote.Designer.cs create mode 100644 src/Bot/Localization/Quote.de-ch.resx create mode 100644 src/Bot/Localization/Quote.resx create mode 100644 src/Bot/Localization/Rank.Designer.cs create mode 100644 src/Bot/Localization/Rank.de-ch.resx create mode 100644 src/Bot/Localization/Rank.resx create mode 100644 src/Bot/Localization/Roll.Designer.cs create mode 100644 src/Bot/Localization/Roll.de-ch.resx create mode 100644 src/Bot/Localization/Roll.resx create mode 100644 src/Bot/Localization/Ship.Designer.cs create mode 100644 src/Bot/Localization/Ship.de-ch.resx create mode 100644 src/Bot/Localization/Ship.resx create mode 100644 src/Core/GeekbotCommandBase.cs diff --git a/src/Bot/Bot.csproj b/src/Bot/Bot.csproj index c7889ae..601766a 100644 --- a/src/Bot/Bot.csproj +++ b/src/Bot/Bot.csproj @@ -40,4 +40,92 @@ + + + ResXFileCodeGenerator + Ship.Designer.cs + + + ResXFileCodeGenerator + Rank.Designer.cs + + + ResXFileCodeGenerator + Karma.Designer.cs + + + ResXFileCodeGenerator + Internal.Designer.cs + + + ResXFileCodeGenerator + Cookies.Designer.cs + + + ResXFileCodeGenerator + Roll.Designer.cs + + + ResXFileCodeGenerator + Choose.Designer.cs + + + ResXFileCodeGenerator + Admin.Designer.cs + + + ResXFileCodeGenerator + Quote.Designer.cs + + + + + True + True + ship.resx + + + True + True + Rank.resx + + + Ship.resx + + + True + True + Karma.resx + + + True + True + Internal.resx + + + True + True + Cookies.resx + + + True + True + Roll.resx + + + True + True + Choose.resx + + + True + True + Admin.resx + + + True + True + Quote.resx + + diff --git a/src/Bot/Commands/Admin/Admin.cs b/src/Bot/Commands/Admin/Admin.cs index f6c4210..2627f86 100644 --- a/src/Bot/Commands/Admin/Admin.cs +++ b/src/Bot/Commands/Admin/Admin.cs @@ -1,9 +1,15 @@ using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Resources; using System.Text; +using System.Threading; using System.Threading.Tasks; using Discord; using Discord.Commands; using Discord.WebSocket; +using Geekbot.Core; using Geekbot.Core.CommandPreconditions; using Geekbot.Core.ErrorHandling; using Geekbot.Core.Extensions; @@ -15,19 +21,15 @@ namespace Geekbot.Bot.Commands.Admin [Group("admin")] [RequireUserPermission(GuildPermission.Administrator)] [DisableInDirectMessage] - public class Admin : ModuleBase + public class Admin : GeekbotCommandBase { private readonly DiscordSocketClient _client; - private readonly IErrorHandler _errorHandler; private readonly IGuildSettingsManager _guildSettingsManager; - private readonly ITranslationHandler _translation; - public Admin(DiscordSocketClient client, IErrorHandler errorHandler, IGuildSettingsManager guildSettingsManager, ITranslationHandler translationHandler) + public Admin(DiscordSocketClient client, IErrorHandler errorHandler, IGuildSettingsManager guildSettingsManager, ITranslationHandler translationHandler) : base(errorHandler, translationHandler) { _client = client; - _errorHandler = errorHandler; _guildSettingsManager = guildSettingsManager; - _translation = translationHandler; } [Command("welcome", RunMode = RunMode.Async)] @@ -60,7 +62,7 @@ namespace Geekbot.Bot.Commands.Admin } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context, "That channel doesn't seem to exist or i don't have write permissions"); + await ErrorHandler.HandleCommandException(e, Context, "That channel doesn't seem to exist or i don't have write permissions"); } } @@ -84,7 +86,7 @@ namespace Geekbot.Bot.Commands.Admin } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context, "That channel doesn't seem to exist or i don't have write permissions"); + await ErrorHandler.HandleCommandException(e, Context, "That channel doesn't seem to exist or i don't have write permissions"); } } @@ -107,7 +109,7 @@ namespace Geekbot.Bot.Commands.Admin } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } @@ -130,7 +132,7 @@ namespace Geekbot.Bot.Commands.Admin } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } @@ -141,24 +143,30 @@ namespace Geekbot.Bot.Commands.Admin try { var language = languageRaw.ToUpper(); - var success = await _translation.SetLanguage(Context.Guild.Id, language); + var success = await Translations.SetLanguage(Context.Guild.Id, language); if (success) { var guild = _guildSettingsManager.GetSettings(Context.Guild.Id); guild.Language = language; await _guildSettingsManager.UpdateSettings(guild); - var transContext = await _translation.GetGuildContext(Context); - await ReplyAsync(transContext.GetString("NewLanguageSet")); + if (language.ToLower() == "chde") + { + Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("de-ch"); + } + + await ReplyAsync(Localization.Admin.NewLanguageSet); return; } - await ReplyAsync( - $"That doesn't seem to be a supported language\r\nSupported Languages are {string.Join(", ", _translation.SupportedLanguages)}"); + var available = new List(); + available.Add("en-GB"); // default + available.AddRange(GetAvailableCultures().Select(culture => culture.Name)); + await ReplyAsync($"That doesn't seem to be a supported language\nSupported Languages are {string.Join(", ", available)}"); } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } @@ -177,7 +185,7 @@ namespace Geekbot.Bot.Commands.Admin } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } @@ -194,7 +202,7 @@ namespace Geekbot.Bot.Commands.Admin } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } @@ -211,7 +219,7 @@ namespace Geekbot.Bot.Commands.Admin } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } @@ -230,5 +238,30 @@ namespace Geekbot.Bot.Commands.Admin return null; } } + + private IEnumerable GetAvailableCultures() + { + var result = new List(); + var rm = new ResourceManager(typeof(Localization.Admin)); + var cultures = CultureInfo.GetCultures(CultureTypes.AllCultures); + foreach (var culture in cultures) + { + try + { + if (culture.Equals(CultureInfo.InvariantCulture)) continue; //do not use "==", won't work + + var rs = rm.GetResourceSet(culture, true, false); + if (rs != null) + { + result.Add(culture); + } + } + catch (CultureNotFoundException) + { + //NOP + } + } + return result; + } } } \ No newline at end of file diff --git a/src/Bot/Commands/Games/Roll/Roll.cs b/src/Bot/Commands/Games/Roll/Roll.cs index 6241ba1..244edbe 100644 --- a/src/Bot/Commands/Games/Roll/Roll.cs +++ b/src/Bot/Commands/Games/Roll/Roll.cs @@ -2,6 +2,7 @@ using System.Linq; using System.Threading.Tasks; using Discord.Commands; +using Geekbot.Core; using Geekbot.Core.Database; using Geekbot.Core.Database.Models; using Geekbot.Core.ErrorHandling; @@ -12,21 +13,17 @@ using Geekbot.Core.RandomNumberGenerator; namespace Geekbot.Bot.Commands.Games.Roll { - public class Roll : ModuleBase + public class Roll : GeekbotCommandBase { - private readonly IErrorHandler _errorHandler; private readonly IKvInMemoryStore _kvInMemoryStore; - private readonly ITranslationHandler _translation; private readonly DatabaseContext _database; private readonly IRandomNumberGenerator _randomNumberGenerator; - public Roll(IKvInMemoryStore kvInMemoryStore,IErrorHandler errorHandler, ITranslationHandler translation, DatabaseContext database, IRandomNumberGenerator randomNumberGenerator) + public Roll(IKvInMemoryStore kvInMemoryStore,IErrorHandler errorHandler, ITranslationHandler translation, DatabaseContext database, IRandomNumberGenerator randomNumberGenerator) : base(errorHandler, translation) { _kvInMemoryStore = kvInMemoryStore; - _translation = translation; _database = database; _randomNumberGenerator = randomNumberGenerator; - _errorHandler = errorHandler; } [Command("roll", RunMode = RunMode.Async)] @@ -37,7 +34,7 @@ namespace Geekbot.Bot.Commands.Games.Roll { var number = _randomNumberGenerator.Next(1, 100); int.TryParse(stuff, out var guess); - var transContext = await _translation.GetGuildContext(Context); + var transContext = await Translations.GetGuildContext(Context); if (guess <= 100 && guess > 0) { var kvKey = $"{Context.Guild.Id}:{Context.User.Id}:RollsPrevious"; @@ -46,8 +43,8 @@ namespace Geekbot.Bot.Commands.Games.Roll if (prevRoll?.LastGuess == guess && prevRoll?.GuessedOn.AddDays(1) > DateTime.Now) { - await ReplyAsync(transContext.GetString( - "NoPrevGuess", + await ReplyAsync(string.Format( + Localization.Roll.NoPrevGuess, Context.Message.Author.Mention, transContext.FormatDateTimeAsRemaining(prevRoll.GuessedOn.AddDays(1)))); return; @@ -55,10 +52,10 @@ namespace Geekbot.Bot.Commands.Games.Roll _kvInMemoryStore.Set(kvKey, new RollTimeout { LastGuess = guess, GuessedOn = DateTime.Now }); - await ReplyAsync(transContext.GetString("Rolled", Context.Message.Author.Mention, number, guess)); + await ReplyAsync(string.Format(Localization.Roll.Rolled, Context.Message.Author.Mention, number, guess)); if (guess == number) { - await ReplyAsync(transContext.GetString("Gratz", Context.Message.Author)); + await ReplyAsync(string.Format(Localization.Roll.Gratz, Context.Message.Author)); var user = await GetUser(Context.User.Id); user.Rolls += 1; _database.Rolls.Update(user); @@ -67,12 +64,12 @@ namespace Geekbot.Bot.Commands.Games.Roll } else { - await ReplyAsync(transContext.GetString("RolledNoGuess", Context.Message.Author.Mention, number)); + await ReplyAsync(string.Format(Localization.Roll.RolledNoGuess, Context.Message.Author.Mention, number)); } } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } diff --git a/src/Bot/Commands/Randomness/Ship.cs b/src/Bot/Commands/Randomness/Ship.cs index d5d9eea..78f3c99 100644 --- a/src/Bot/Commands/Randomness/Ship.cs +++ b/src/Bot/Commands/Randomness/Ship.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; +using Geekbot.Core; using Geekbot.Core.Database; using Geekbot.Core.Database.Models; using Geekbot.Core.ErrorHandling; @@ -12,19 +13,15 @@ using Geekbot.Core.RandomNumberGenerator; namespace Geekbot.Bot.Commands.Randomness { - public class Ship : ModuleBase + public class Ship : GeekbotCommandBase { - private readonly IErrorHandler _errorHandler; private readonly IRandomNumberGenerator _randomNumberGenerator; - private readonly ITranslationHandler _translation; private readonly DatabaseContext _database; - public Ship(DatabaseContext database, IErrorHandler errorHandler, IRandomNumberGenerator randomNumberGenerator, ITranslationHandler translation) + public Ship(DatabaseContext database, IErrorHandler errorHandler, IRandomNumberGenerator randomNumberGenerator, ITranslationHandler translations) : base(errorHandler, translations) { _database = database; - _errorHandler = errorHandler; _randomNumberGenerator = randomNumberGenerator; - _translation = translation; } [Command("Ship", RunMode = RunMode.Async)] @@ -58,28 +55,26 @@ namespace Geekbot.Bot.Commands.Randomness shippingRate = dbval.Strength; } - var transContext = await _translation.GetGuildContext(Context); - - var reply = $":heartpulse: **{transContext.GetString("Matchmaking")}** :heartpulse:\r\n"; + var reply = $":heartpulse: **{Localization.Ship.Matchmaking}** :heartpulse:\r\n"; reply += $":two_hearts: {user1.Mention} :heart: {user2.Mention} :two_hearts:\r\n"; - reply += $"0% [{BlockCounter(shippingRate)}] 100% - {DeterminateSuccess(shippingRate, transContext)}"; + reply += $"0% [{BlockCounter(shippingRate)}] 100% - {DeterminateSuccess(shippingRate)}"; await ReplyAsync(reply); } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } - private string DeterminateSuccess(int rate, TranslationGuildContext transContext) + private string DeterminateSuccess(int rate) { return (rate / 20) switch { - 0 => transContext.GetString("NotGonnaToHappen"), - 1 => transContext.GetString("NotSuchAGoodIdea"), - 2 => transContext.GetString("ThereMightBeAChance"), - 3 => transContext.GetString("CouldWork"), - 4 => transContext.GetString("ItsAMatch"), + 0 => Localization.Ship.NotGoingToHappen, + 1 => Localization.Ship.NotSuchAGoodIdea, + 2 => Localization.Ship.ThereMightBeAChance, + 3 => Localization.Ship.CouldWork, + 4 => Localization.Ship.ItsAMatch, _ => "nope" }; } diff --git a/src/Bot/Commands/Rpg/Cookies.cs b/src/Bot/Commands/Rpg/Cookies.cs index f581105..570f08a 100644 --- a/src/Bot/Commands/Rpg/Cookies.cs +++ b/src/Bot/Commands/Rpg/Cookies.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; +using Geekbot.Core; using Geekbot.Core.CommandPreconditions; using Geekbot.Core.Database; using Geekbot.Core.Database.Models; @@ -16,18 +17,14 @@ namespace Geekbot.Bot.Commands.Rpg [DisableInDirectMessage] [Group("cookies")] [Alias("cookie")] - public class Cookies : ModuleBase + public class Cookies : GeekbotCommandBase { private readonly DatabaseContext _database; - private readonly IErrorHandler _errorHandler; - private readonly ITranslationHandler _translation; private readonly IRandomNumberGenerator _randomNumberGenerator; - public Cookies(DatabaseContext database, IErrorHandler errorHandler, ITranslationHandler translation , IRandomNumberGenerator randomNumberGenerator) + public Cookies(DatabaseContext database, IErrorHandler errorHandler, ITranslationHandler translations , IRandomNumberGenerator randomNumberGenerator) : base(errorHandler, translations) { _database = database; - _errorHandler = errorHandler; - _translation = translation; _randomNumberGenerator = randomNumberGenerator; } @@ -37,23 +34,23 @@ namespace Geekbot.Bot.Commands.Rpg { try { - var transContext = await _translation.GetGuildContext(Context); + var transContext = await Translations.GetGuildContext(Context); var actor = await GetUser(Context.User.Id); if (actor.LastPayout.Value.AddDays(1).Date > DateTime.Now.Date) { var formatedWaitTime = transContext.FormatDateTimeAsRemaining(DateTimeOffset.Now.AddDays(1).Date); - await ReplyAsync(transContext.GetString("WaitForMoreCookies", formatedWaitTime)); + await ReplyAsync(string.Format(Localization.Cookies.WaitForMoreCookies, formatedWaitTime)); return; } actor.Cookies += 10; actor.LastPayout = DateTimeOffset.Now; await SetUser(actor); - await ReplyAsync(transContext.GetString("GetCookies", 10, actor.Cookies)); + await ReplyAsync(string.Format(Localization.Cookies.GetCookies, 10, actor.Cookies)); } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } @@ -63,13 +60,12 @@ namespace Geekbot.Bot.Commands.Rpg { try { - var transContext = await _translation.GetGuildContext(Context); var actor = await GetUser(Context.User.Id); - await ReplyAsync(transContext.GetString("InYourJar", actor.Cookies)); + await ReplyAsync(string.Format(Localization.Cookies.InYourJar, actor.Cookies)); } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } @@ -79,12 +75,11 @@ namespace Geekbot.Bot.Commands.Rpg { try { - var transContext = await _translation.GetGuildContext(Context); var giver = await GetUser(Context.User.Id); if (giver.Cookies < amount) { - await ReplyAsync(transContext.GetString("NotEnoughToGive")); + await ReplyAsync(Localization.Cookies.NotEnoughToGive); return; } @@ -96,11 +91,11 @@ namespace Geekbot.Bot.Commands.Rpg await SetUser(giver); await SetUser(taker); - await ReplyAsync(transContext.GetString("Given", amount, user.Username)); + await ReplyAsync(string.Format(Localization.Cookies.Given, amount, user.Username)); } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } @@ -110,12 +105,11 @@ namespace Geekbot.Bot.Commands.Rpg { try { - var transContext = await _translation.GetGuildContext(Context); var actor = await GetUser(Context.User.Id); if (actor.Cookies < 5) { - await ReplyAsync(transContext.GetString("NotEnoughCookiesToEat")); + await ReplyAsync(Localization.Cookies.NotEnoughCookiesToEat); return; } @@ -124,14 +118,14 @@ namespace Geekbot.Bot.Commands.Rpg await SetUser(actor); - await ReplyAsync(transContext.GetString("AteCookies", amount, actor.Cookies)); + await ReplyAsync(string.Format(Localization.Cookies.AteCookies, amount, actor.Cookies)); } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } - + private async Task GetUser(ulong userId) { var user = _database.Cookies.FirstOrDefault(u =>u.GuildId.Equals(Context.Guild.Id.AsLong()) && u.UserId.Equals(userId.AsLong())) ?? await CreateNewRow(userId); diff --git a/src/Bot/Commands/User/Karma.cs b/src/Bot/Commands/User/Karma.cs index 3efc381..41a401c 100644 --- a/src/Bot/Commands/User/Karma.cs +++ b/src/Bot/Commands/User/Karma.cs @@ -1,8 +1,11 @@ using System; +using System.Globalization; using System.Linq; +using System.Threading; using System.Threading.Tasks; using Discord; using Discord.Commands; +using Geekbot.Core; using Geekbot.Core.CommandPreconditions; using Geekbot.Core.Database; using Geekbot.Core.Database.Models; @@ -13,17 +16,15 @@ using Geekbot.Core.Localization; namespace Geekbot.Bot.Commands.User { [DisableInDirectMessage] - public class Karma : ModuleBase + public class Karma : GeekbotCommandBase { - private readonly IErrorHandler _errorHandler; private readonly DatabaseContext _database; - private readonly ITranslationHandler _translation; + private readonly ITranslationHandler _translations; - public Karma(DatabaseContext database, IErrorHandler errorHandler, ITranslationHandler translation) + public Karma(DatabaseContext database, IErrorHandler errorHandler, ITranslationHandler translations) : base(errorHandler, translations) { _database = database; - _errorHandler = errorHandler; - _translation = translation; + _translations = translations; } [Command("good", RunMode = RunMode.Async)] @@ -32,16 +33,17 @@ namespace Geekbot.Bot.Commands.User { try { - var transContext = await _translation.GetGuildContext(Context); + var transContext = await _translations.GetGuildContext(Context); + var actor = await GetUser(Context.User.Id); if (user.Id == Context.User.Id) { - await ReplyAsync(transContext.GetString("CannotChangeOwn", Context.User.Username)); + await ReplyAsync(string.Format(Localization.Karma.CannotChangeOwnUp, Context.User.Username)); } else if (TimeoutFinished(actor.TimeOut)) { var formatedWaitTime = transContext.FormatDateTimeAsRemaining(actor.TimeOut.AddMinutes(3)); - await ReplyAsync(transContext.GetString("WaitUntill", Context.User.Username, formatedWaitTime)); + await ReplyAsync(string.Format(Localization.Karma.WaitUntill, Context.User.Username, formatedWaitTime)); } else { @@ -60,16 +62,16 @@ namespace Geekbot.Bot.Commands.User .WithName(user.Username)); eb.WithColor(new Color(138, 219, 146)); - eb.Title = transContext.GetString("Increased"); - eb.AddInlineField(transContext.GetString("By"), Context.User.Username); - eb.AddInlineField(transContext.GetString("Amount"), "+1"); - eb.AddInlineField(transContext.GetString("Current"), target.Karma); + eb.Title = Localization.Karma.Increased; + eb.AddInlineField(Localization.Karma.By, Context.User.Username); + eb.AddInlineField(Localization.Karma.Amount, "+1"); + eb.AddInlineField(Localization.Karma.Current, target.Karma); await ReplyAsync("", false, eb.Build()); } } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } @@ -79,16 +81,17 @@ namespace Geekbot.Bot.Commands.User { try { - var transContext = await _translation.GetGuildContext(Context); + var transContext = await _translations.GetGuildContext(Context); + var actor = await GetUser(Context.User.Id); if (user.Id == Context.User.Id) { - await ReplyAsync(transContext.GetString("CannotChangeOwn", Context.User.Username)); + await ReplyAsync(string.Format(Localization.Karma.CannotChangeOwnDown, Context.User.Username)); } else if (TimeoutFinished(actor.TimeOut)) { var formatedWaitTime = transContext.FormatDateTimeAsRemaining(actor.TimeOut.AddMinutes(3)); - await ReplyAsync(transContext.GetString("WaitUntill", Context.User.Username, formatedWaitTime)); + await ReplyAsync(string.Format(Localization.Karma.WaitUntill, Context.User.Username, formatedWaitTime)); } else { @@ -107,16 +110,16 @@ namespace Geekbot.Bot.Commands.User .WithName(user.Username)); eb.WithColor(new Color(138, 219, 146)); - eb.Title = transContext.GetString("Decreased"); - eb.AddInlineField(transContext.GetString("By"), Context.User.Username); - eb.AddInlineField(transContext.GetString("Amount"), "-1"); - eb.AddInlineField(transContext.GetString("Current"), target.Karma); + eb.Title = Localization.Karma.Decreased; + eb.AddInlineField(Localization.Karma.By, Context.User.Username); + eb.AddInlineField(Localization.Karma.Amount, "-1"); + eb.AddInlineField(Localization.Karma.Current, target.Karma); await ReplyAsync("", false, eb.Build()); } } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } diff --git a/src/Bot/Commands/User/Ranking/Rank.cs b/src/Bot/Commands/User/Ranking/Rank.cs index ae1bec9..d73aacc 100644 --- a/src/Bot/Commands/User/Ranking/Rank.cs +++ b/src/Bot/Commands/User/Ranking/Rank.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Discord.Commands; +using Geekbot.Core; using Geekbot.Core.CommandPreconditions; using Geekbot.Core.Converters; using Geekbot.Core.Database; @@ -11,28 +12,20 @@ using Geekbot.Core.ErrorHandling; using Geekbot.Core.Extensions; using Geekbot.Core.Highscores; using Geekbot.Core.Localization; -using Geekbot.Core.UserRepository; namespace Geekbot.Bot.Commands.User.Ranking { - public class Rank : ModuleBase + public class Rank : GeekbotCommandBase { private readonly IEmojiConverter _emojiConverter; private readonly IHighscoreManager _highscoreManager; - private readonly ITranslationHandler _translationHandler; - private readonly IErrorHandler _errorHandler; private readonly DatabaseContext _database; - private readonly IUserRepository _userRepository; - public Rank(DatabaseContext database, IErrorHandler errorHandler, IUserRepository userRepository, - IEmojiConverter emojiConverter, IHighscoreManager highscoreManager, ITranslationHandler translationHandler) + public Rank(DatabaseContext database, IErrorHandler errorHandler, IEmojiConverter emojiConverter, IHighscoreManager highscoreManager, ITranslationHandler translations): base(errorHandler, translations) { _database = database; - _errorHandler = errorHandler; - _userRepository = userRepository; _emojiConverter = emojiConverter; _highscoreManager = highscoreManager; - _translationHandler = translationHandler; } [Command("rank", RunMode = RunMode.Async)] @@ -42,7 +35,6 @@ namespace Geekbot.Bot.Commands.User.Ranking { try { - var transContext = await _translationHandler.GetGuildContext(Context); HighscoreTypes type; try { @@ -51,14 +43,14 @@ namespace Geekbot.Bot.Commands.User.Ranking } catch { - await ReplyAsync(transContext.GetString("InvalidType")); + await ReplyAsync(Localization.Rank.InvalidType); return; } var replyBuilder = new StringBuilder(); if (amount > 20) { - await ReplyAsync(transContext.GetString("LimitingTo20Warning")); + await ReplyAsync(Localization.Rank.LimitingTo20Warning); amount = 20; } @@ -70,7 +62,7 @@ namespace Geekbot.Bot.Commands.User.Ranking } catch (HighscoreListEmptyException) { - await ReplyAsync(transContext.GetString("NoTypeFoundForServer", type)); + await ReplyAsync(string.Format(Localization.Rank.NoTypeFoundForServer, type)); return; } @@ -85,22 +77,24 @@ namespace Geekbot.Bot.Commands.User.Ranking var failedToRetrieveUser = highscoreUsers.Any(e => string.IsNullOrEmpty(e.Key.Username)); - if (failedToRetrieveUser) replyBuilder.AppendLine(transContext.GetString("FailedToResolveAllUsernames")); - replyBuilder.AppendLine(transContext.GetString("HighscoresFor", type.ToString().CapitalizeFirst(), Context.Guild.Name)); + if (failedToRetrieveUser) replyBuilder.AppendLine(Localization.Rank.FailedToResolveAllUsernames).AppendLine(); + + replyBuilder.AppendLine(string.Format(Localization.Rank.HighscoresFor, type.ToString().CapitalizeFirst(), Context.Guild.Name)); + var highscorePlace = 1; - foreach (var user in highscoreUsers) + foreach (var (user, value) in highscoreUsers) { replyBuilder.Append(highscorePlace < 11 ? $"{_emojiConverter.NumberToEmoji(highscorePlace)} " : $"`{highscorePlace}.` "); - replyBuilder.Append(user.Key.Username != null - ? $"**{user.Key.Username}#{user.Key.Discriminator}**" - : $"**{user.Key.Id}**"); + replyBuilder.Append(user.Username != null + ? $"**{user.Username}#{user.Discriminator}**" + : $"**{user.Id}**"); replyBuilder.Append(type == HighscoreTypes.messages - ? $" - {user.Value} {type} - {Math.Round((double) (100 * user.Value) / guildMessages, 2)}%\n" - : $" - {user.Value} {type}\n"); + ? $" - {value} {type} - {Math.Round((double) (100 * value) / guildMessages, 2)}%\n" + : $" - {value} {type}\n"); highscorePlace++; } @@ -109,7 +103,7 @@ namespace Geekbot.Bot.Commands.User.Ranking } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } } diff --git a/src/Bot/Commands/Utils/Choose.cs b/src/Bot/Commands/Utils/Choose.cs index d245bd2..ea239d7 100644 --- a/src/Bot/Commands/Utils/Choose.cs +++ b/src/Bot/Commands/Utils/Choose.cs @@ -1,20 +1,16 @@ using System; using System.Threading.Tasks; using Discord.Commands; +using Geekbot.Core; using Geekbot.Core.ErrorHandling; using Geekbot.Core.Localization; namespace Geekbot.Bot.Commands.Utils { - public class Choose : ModuleBase + public class Choose : GeekbotCommandBase { - private readonly IErrorHandler _errorHandler; - private readonly ITranslationHandler _translation; - - public Choose(IErrorHandler errorHandler, ITranslationHandler translation) + public Choose(IErrorHandler errorHandler, ITranslationHandler translation) : base(errorHandler, translation) { - _errorHandler = errorHandler; - _translation = translation; } [Command("choose", RunMode = RunMode.Async)] @@ -24,14 +20,13 @@ namespace Geekbot.Bot.Commands.Utils { try { - var transContext = await _translation.GetGuildContext(Context); var choicesArray = choices.Split(';'); var choice = new Random().Next(choicesArray.Length); - await ReplyAsync(transContext.GetString("Choice", choicesArray[choice].Trim())); + await ReplyAsync(string.Format(Localization.Choose.Choice, choicesArray[choice].Trim())); } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } } diff --git a/src/Bot/Commands/Utils/Quote/Quote.cs b/src/Bot/Commands/Utils/Quote/Quote.cs index a731273..5bb23a9 100644 --- a/src/Bot/Commands/Utils/Quote/Quote.cs +++ b/src/Bot/Commands/Utils/Quote/Quote.cs @@ -17,20 +17,16 @@ namespace Geekbot.Bot.Commands.Utils.Quote { [Group("quote")] [DisableInDirectMessage] - public class Quote : ModuleBase + public class Quote : GeekbotCommandBase { - private readonly IErrorHandler _errorHandler; private readonly DatabaseContext _database; private readonly IRandomNumberGenerator _randomNumberGenerator; - private readonly ITranslationHandler _translationHandler; private readonly bool _isDev; - public Quote(IErrorHandler errorHandler, DatabaseContext database, IRandomNumberGenerator randomNumberGenerator, ITranslationHandler translationHandler) + public Quote(IErrorHandler errorHandler, DatabaseContext database, IRandomNumberGenerator randomNumberGenerator, ITranslationHandler translationHandler) : base(errorHandler, translationHandler) { - _errorHandler = errorHandler; _database = database; _randomNumberGenerator = randomNumberGenerator; - _translationHandler = translationHandler; // to remove restrictions when developing _isDev = Constants.BotVersion() == "0.0.0-DEV"; } @@ -45,8 +41,7 @@ namespace Geekbot.Bot.Commands.Utils.Quote if (!s.Any()) { - var transContext = await _translationHandler.GetGuildContext(Context); - await ReplyAsync(transContext.GetString("NoQuotesFound")); + await ReplyAsync(Localization.Quote.NoQuotesFound); return; } @@ -58,7 +53,7 @@ namespace Geekbot.Bot.Commands.Utils.Quote } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context, "Whoops, seems like the quote was to edgy to return"); + await ErrorHandler.HandleCommandException(e, Context, "Whoops, seems like the quote was to edgy to return"); } } @@ -117,23 +112,22 @@ namespace Geekbot.Bot.Commands.Utils.Quote { try { - var transContext = await _translationHandler.GetGuildContext(Context); var quote = _database.Quotes.Where(e => e.GuildId == Context.Guild.Id.AsLong() && e.InternalId == id)?.FirstOrDefault(); if (quote != null) { _database.Quotes.Remove(quote); await _database.SaveChangesAsync(); var embed = QuoteBuilder(quote); - await ReplyAsync(transContext.GetString("Removed", id), false, embed.Build()); + await ReplyAsync(string.Format(Localization.Quote.Removed, id), false, embed.Build()); } else { - await ReplyAsync(transContext.GetString("NotFoundWithId")); + await ReplyAsync(Localization.Quote.NotFoundWithId); } } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context, "I couldn't find a quote with that id :disappointed:"); + await ErrorHandler.HandleCommandException(e, Context, "I couldn't find a quote with that id :disappointed:"); } } @@ -144,12 +138,11 @@ namespace Geekbot.Bot.Commands.Utils.Quote try { // setup - var transContext = await _translationHandler.GetGuildContext(Context); var eb = new EmbedBuilder(); eb.Author = new EmbedAuthorBuilder() { IconUrl = Context.Guild.IconUrl, - Name = $"{Context.Guild.Name} - {transContext.GetString("QuoteStats")}" + Name = $"{Context.Guild.Name} - {Localization.Quote.QuoteStats}" }; // gather data @@ -157,7 +150,7 @@ namespace Geekbot.Bot.Commands.Utils.Quote if (totalQuotes == 0) { // no quotes, no stats, end of the road - await ReplyAsync(transContext.GetString("NoQuotesFound")); + await ReplyAsync(Localization.Quote.NoQuotesFound); return; } @@ -176,8 +169,8 @@ namespace Geekbot.Bot.Commands.Utils.Quote .OrderBy(row => row.year); // add data to the embed - eb.AddField(transContext.GetString("MostQuotesPerson"), $"{mostQuotedPersonUser.Username} ({mostQuotedPerson.amount})"); - eb.AddInlineField(transContext.GetString("TotalQuotes"), totalQuotes); + eb.AddField(Localization.Quote.MostQuotesPerson, $"{mostQuotedPersonUser.Username} ({mostQuotedPerson.amount})"); + eb.AddInlineField(Localization.Quote.TotalQuotes, totalQuotes); foreach (var year in quotesByYear) { @@ -188,7 +181,7 @@ namespace Geekbot.Bot.Commands.Utils.Quote } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } @@ -196,8 +189,6 @@ namespace Geekbot.Bot.Commands.Utils.Quote { try { - var transContext = await _translationHandler.GetGuildContext(Context); - var list = Context.Channel.GetMessagesAsync().Flatten(); var message = await list.FirstOrDefaultAsync(msg => msg.Author.Id == user.Id && @@ -206,11 +197,11 @@ namespace Geekbot.Bot.Commands.Utils.Quote !msg.Content.ToLower().StartsWith("!")); if (message == null) return; - await ProcessQuote(message, saveToDb, transContext); + await ProcessQuote(message, saveToDb); } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context, $"No quoteable messages have been sent by {user.Username} in this channel"); + await ErrorHandler.HandleCommandException(e, Context, $"No quoteable messages have been sent by {user.Username} in this channel"); } } @@ -219,14 +210,14 @@ namespace Geekbot.Bot.Commands.Utils.Quote { try { - var transContext = await _translationHandler.GetGuildContext(Context); + // var transContext = await _translationHandler.GetGuildContext(Context); var message = await Context.Channel.GetMessageAsync(messageId); - await ProcessQuote(message, saveToDb, transContext); + await ProcessQuote(message, saveToDb); } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context, "I couldn't find a message with that id :disappointed:"); + await ErrorHandler.HandleCommandException(e, Context, "I couldn't find a message with that id :disappointed:"); } } @@ -234,18 +225,18 @@ namespace Geekbot.Bot.Commands.Utils.Quote { try { - var transContext = await _translationHandler.GetGuildContext(Context); + // var transContext = await _translationHandler.GetGuildContext(Context); if (!MessageLink.IsValid(messageLink)) { - await ReplyAsync(transContext.GetString("NotAValidMessageLink")); + await ReplyAsync(Localization.Quote.NotAValidMessageLink); return; } var link = new MessageLink(messageLink); if (link.GuildId != Context.Guild.Id) { - await ReplyAsync(transContext.GetString("OnlyQuoteFromSameServer")); + await ReplyAsync(Localization.Quote.OnlyQuoteFromSameServer); return; } @@ -255,25 +246,25 @@ namespace Geekbot.Bot.Commands.Utils.Quote var message = await channel.GetMessageAsync(link.MessageId); - await ProcessQuote(message, saveToDb, transContext); + await ProcessQuote(message, saveToDb); } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context, "I couldn't find that message :disappointed:"); + await ErrorHandler.HandleCommandException(e, Context, "I couldn't find that message :disappointed:"); } } - private async Task ProcessQuote(IMessage message, bool saveToDb, TranslationGuildContext transContext) + private async Task ProcessQuote(IMessage message, bool saveToDb) { if (message.Author.Id == Context.Message.Author.Id && saveToDb && !_isDev) { - await ReplyAsync(transContext.GetString("CannotSaveOwnQuotes")); + await ReplyAsync(Localization.Quote.CannotSaveOwnQuotes); return; } if (message.Author.IsBot && saveToDb && !_isDev) { - await ReplyAsync(transContext.GetString("CannotQuoteBots")); + await ReplyAsync(Localization.Quote.CannotQuoteBots); return; } @@ -285,7 +276,7 @@ namespace Geekbot.Bot.Commands.Utils.Quote } var embed = QuoteBuilder(quote); - await ReplyAsync(saveToDb ? transContext.GetString("QuoteAdded") : string.Empty, false, embed.Build()); + await ReplyAsync(saveToDb ? Localization.Quote.QuoteAdded : string.Empty, false, embed.Build()); } private EmbedBuilder QuoteBuilder(QuoteModel quote) diff --git a/src/Bot/Localization/Admin.Designer.cs b/src/Bot/Localization/Admin.Designer.cs new file mode 100644 index 0000000..c55adf1 --- /dev/null +++ b/src/Bot/Localization/Admin.Designer.cs @@ -0,0 +1,81 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Geekbot.Bot.Localization { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Admin { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Admin() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Geekbot.Bot.Localization.Admin", typeof(Admin).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to I'm talking english. + /// + internal static string GetLanguage { + get { + return ResourceManager.GetString("GetLanguage", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to I will reply in english from now on. + /// + internal static string NewLanguageSet { + get { + return ResourceManager.GetString("NewLanguageSet", resourceCulture); + } + } + } +} diff --git a/src/Bot/Localization/Admin.de-ch.resx b/src/Bot/Localization/Admin.de-ch.resx new file mode 100644 index 0000000..b89939b --- /dev/null +++ b/src/Bot/Localization/Admin.de-ch.resx @@ -0,0 +1,20 @@ + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + I werd ab jetzt uf schwiizerdüütsch antworte, äuuä + + + I red schwiizerdüütsch + + \ No newline at end of file diff --git a/src/Bot/Localization/Admin.resx b/src/Bot/Localization/Admin.resx new file mode 100644 index 0000000..6fd2c62 --- /dev/null +++ b/src/Bot/Localization/Admin.resx @@ -0,0 +1,27 @@ + + + + + + + + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + I will reply in english from now on + + + I'm talking english + + \ No newline at end of file diff --git a/src/Bot/Localization/Choose.Designer.cs b/src/Bot/Localization/Choose.Designer.cs new file mode 100644 index 0000000..1319140 --- /dev/null +++ b/src/Bot/Localization/Choose.Designer.cs @@ -0,0 +1,72 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Geekbot.Bot.Localization { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Choose { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Choose() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Geekbot.Bot.Localization.Choose", typeof(Choose).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to I Choose **{0}**. + /// + internal static string Choice { + get { + return ResourceManager.GetString("Choice", resourceCulture); + } + } + } +} diff --git a/src/Bot/Localization/Choose.de-ch.resx b/src/Bot/Localization/Choose.de-ch.resx new file mode 100644 index 0000000..eedc39b --- /dev/null +++ b/src/Bot/Localization/Choose.de-ch.resx @@ -0,0 +1,17 @@ + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + I nimme **{0}** + + \ No newline at end of file diff --git a/src/Bot/Localization/Choose.resx b/src/Bot/Localization/Choose.resx new file mode 100644 index 0000000..052177b --- /dev/null +++ b/src/Bot/Localization/Choose.resx @@ -0,0 +1,24 @@ + + + + + + + + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + I Choose **{0}** + + \ No newline at end of file diff --git a/src/Bot/Localization/Cookies.Designer.cs b/src/Bot/Localization/Cookies.Designer.cs new file mode 100644 index 0000000..5f2528f --- /dev/null +++ b/src/Bot/Localization/Cookies.Designer.cs @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Geekbot.Bot.Localization { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Cookies { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Cookies() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Geekbot.Bot.Localization.Cookies", typeof(Cookies).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to You ate {0} cookies, you've only got {1} cookies left. + /// + internal static string AteCookies { + get { + return ResourceManager.GetString("AteCookies", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to You got {0} cookies, there are now {1} cookies in you cookie jar. + /// + internal static string GetCookies { + get { + return ResourceManager.GetString("GetCookies", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to You gave {0} cookies to {1}. + /// + internal static string Given { + get { + return ResourceManager.GetString("Given", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to There are {0} cookies in you cookie jar. + /// + internal static string InYourJar { + get { + return ResourceManager.GetString("InYourJar", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Your cookie jar looks almost empty, you should probably not eat a cookie. + /// + internal static string NotEnoughCookiesToEat { + get { + return ResourceManager.GetString("NotEnoughCookiesToEat", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to You don't have enough cookies. + /// + internal static string NotEnoughToGive { + get { + return ResourceManager.GetString("NotEnoughToGive", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to You already got cookies today, you can have more cookies in {0}. + /// + internal static string WaitForMoreCookies { + get { + return ResourceManager.GetString("WaitForMoreCookies", resourceCulture); + } + } + } +} diff --git a/src/Bot/Localization/Cookies.de-ch.resx b/src/Bot/Localization/Cookies.de-ch.resx new file mode 100644 index 0000000..b53b588 --- /dev/null +++ b/src/Bot/Localization/Cookies.de-ch.resx @@ -0,0 +1,35 @@ + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Du häsch {0} guetzli becho, du häsch jetzt {1} guetzli ih dr büchse + + + Du hesch scho guetzli becho hüt, du chasch meh ha in {0} + + + Es hät {0} guetzli ih dineri büchs + + + Du hesch {1} {0} guetzli geh + + + Du hesch nid gnueg guetzli + + + Du hesch chuum no guetzli ih dineri büchs, du sötsch warschinli keini esse + + + Du hesch {0} guetzli gesse und hesch jezt no {1} übrig + + \ No newline at end of file diff --git a/src/Bot/Localization/Cookies.resx b/src/Bot/Localization/Cookies.resx new file mode 100644 index 0000000..53207fa --- /dev/null +++ b/src/Bot/Localization/Cookies.resx @@ -0,0 +1,42 @@ + + + + + + + + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + You got {0} cookies, there are now {1} cookies in you cookie jar + + + You already got cookies today, you can have more cookies in {0} + + + There are {0} cookies in you cookie jar + + + You gave {0} cookies to {1} + + + You don't have enough cookies + + + Your cookie jar looks almost empty, you should probably not eat a cookie + + + You ate {0} cookies, you've only got {1} cookies left + + \ No newline at end of file diff --git a/src/Bot/Localization/Internal.Designer.cs b/src/Bot/Localization/Internal.Designer.cs new file mode 100644 index 0000000..e7c18af --- /dev/null +++ b/src/Bot/Localization/Internal.Designer.cs @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Geekbot.Bot.Localization { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Internal { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Internal() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Geekbot.Bot.Localization.Internal", typeof(Internal).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to and. + /// + internal static string And { + get { + return ResourceManager.GetString("And", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to day|days. + /// + internal static string Days { + get { + return ResourceManager.GetString("Days", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to hour|hours. + /// + internal static string Hours { + get { + return ResourceManager.GetString("Hours", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Seems like i don't have enough permission to that :confused:. + /// + internal static string Http403 { + get { + return ResourceManager.GetString("Http403", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to minute|minutes. + /// + internal static string Minutes { + get { + return ResourceManager.GetString("Minutes", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to second|seconds. + /// + internal static string Seconds { + get { + return ResourceManager.GetString("Seconds", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Something went wrong :confused:. + /// + internal static string SomethingWentWrong { + get { + return ResourceManager.GetString("SomethingWentWrong", resourceCulture); + } + } + } +} diff --git a/src/Bot/Localization/Internal.de-ch.resx b/src/Bot/Localization/Internal.de-ch.resx new file mode 100644 index 0000000..568bb1f --- /dev/null +++ b/src/Bot/Localization/Internal.de-ch.resx @@ -0,0 +1,35 @@ + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Öppis isch schief gange :confused: + + + Gseht danach us das ich nid gnueg recht han zum das mache :confused: + + + tag|täg + + + stund|stunde + + + minute|minute + + + sekunde|sekunde + + + und + + \ No newline at end of file diff --git a/src/Bot/Localization/Internal.resx b/src/Bot/Localization/Internal.resx new file mode 100644 index 0000000..a771d6b --- /dev/null +++ b/src/Bot/Localization/Internal.resx @@ -0,0 +1,42 @@ + + + + + + + + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Something went wrong :confused: + + + Seems like i don't have enough permission to that :confused: + + + day|days + + + hour|hours + + + minute|minutes + + + second|seconds + + + and + + \ No newline at end of file diff --git a/src/Bot/Localization/Karma.Designer.cs b/src/Bot/Localization/Karma.Designer.cs new file mode 100644 index 0000000..4191fa2 --- /dev/null +++ b/src/Bot/Localization/Karma.Designer.cs @@ -0,0 +1,135 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Geekbot.Bot.Localization { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Karma { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Karma() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Geekbot.Bot.Localization.Karma", typeof(Karma).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to Amount. + /// + internal static string Amount { + get { + return ResourceManager.GetString("Amount", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to By. + /// + internal static string By { + get { + return ResourceManager.GetString("By", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Sorry {0}, but you can't lower your own karma. + /// + internal static string CannotChangeOwnDown { + get { + return ResourceManager.GetString("CannotChangeOwnDown", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Sorry {0}, but you can't give yourself karma. + /// + internal static string CannotChangeOwnUp { + get { + return ResourceManager.GetString("CannotChangeOwnUp", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Current. + /// + internal static string Current { + get { + return ResourceManager.GetString("Current", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Karma lowered. + /// + internal static string Decreased { + get { + return ResourceManager.GetString("Decreased", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Gained Karma. + /// + internal static string Increased { + get { + return ResourceManager.GetString("Increased", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Sorry {0}, but you have to wait {1} before you can give karma again.... + /// + internal static string WaitUntill { + get { + return ResourceManager.GetString("WaitUntill", resourceCulture); + } + } + } +} diff --git a/src/Bot/Localization/Karma.de-ch.resx b/src/Bot/Localization/Karma.de-ch.resx new file mode 100644 index 0000000..294c105 --- /dev/null +++ b/src/Bot/Localization/Karma.de-ch.resx @@ -0,0 +1,38 @@ + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Sorry {0}, aber du chasch dr selber kei karma geh + + + Sorry {0}, aber du musch no {1} warte bisch d wieder karma chasch geh... + + + Karma becho + + + Vo + + + Mengi + + + Jetzt + + + Sorry {0}, aber du chasch dr din eigete karma nid weg neh + + + Karma gsenkt + + \ No newline at end of file diff --git a/src/Bot/Localization/Karma.resx b/src/Bot/Localization/Karma.resx new file mode 100644 index 0000000..3a8fe5a --- /dev/null +++ b/src/Bot/Localization/Karma.resx @@ -0,0 +1,45 @@ + + + + + + + + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Sorry {0}, but you can't give yourself karma + + + Sorry {0}, but you have to wait {1} before you can give karma again... + + + Gained Karma + + + By + + + Amount + + + Current + + + Sorry {0}, but you can't lower your own karma + + + Karma lowered + + \ No newline at end of file diff --git a/src/Bot/Localization/Quote.Designer.cs b/src/Bot/Localization/Quote.Designer.cs new file mode 100644 index 0000000..342c181 --- /dev/null +++ b/src/Bot/Localization/Quote.Designer.cs @@ -0,0 +1,162 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Geekbot.Bot.Localization { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Quote { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Quote() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Geekbot.Bot.Localization.Quote", typeof(Quote).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to You can't save quotes by a bot.... + /// + internal static string CannotQuoteBots { + get { + return ResourceManager.GetString("CannotQuoteBots", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to You can't save your own quotes.... + /// + internal static string CannotSaveOwnQuotes { + get { + return ResourceManager.GetString("CannotSaveOwnQuotes", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Most quoted person. + /// + internal static string MostQuotesPerson { + get { + return ResourceManager.GetString("MostQuotesPerson", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to This server doesn't seem to have any quotes yet. You can add a quote with `!quote save @user` or `!quote save <messageId>`. + /// + internal static string NoQuotesFound { + get { + return ResourceManager.GetString("NoQuotesFound", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to That is not a valid message link. + /// + internal static string NotAValidMessageLink { + get { + return ResourceManager.GetString("NotAValidMessageLink", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to I couldn't find a quote with that ID :disappointed:. + /// + internal static string NotFoundWithId { + get { + return ResourceManager.GetString("NotFoundWithId", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to You can only quote messages from the same server. + /// + internal static string OnlyQuoteFromSameServer { + get { + return ResourceManager.GetString("OnlyQuoteFromSameServer", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to **Quote Added**. + /// + internal static string QuoteAdded { + get { + return ResourceManager.GetString("QuoteAdded", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Quote Stats. + /// + internal static string QuoteStats { + get { + return ResourceManager.GetString("QuoteStats", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to **Removed #{0}**. + /// + internal static string Removed { + get { + return ResourceManager.GetString("Removed", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Total. + /// + internal static string TotalQuotes { + get { + return ResourceManager.GetString("TotalQuotes", resourceCulture); + } + } + } +} diff --git a/src/Bot/Localization/Quote.de-ch.resx b/src/Bot/Localization/Quote.de-ch.resx new file mode 100644 index 0000000..99fd959 --- /dev/null +++ b/src/Bot/Localization/Quote.de-ch.resx @@ -0,0 +1,47 @@ + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Dä server het no kei quotes. Du chasch quotes hinzuefüege mit `!quote save @user` oder `!quote save <messageId>` + + + Du chasch kei quotes vo dir selber speichere... + + + Du chasch kei quotes vomne bot speichere... + + + **Quote hinzugfüegt** + + + **#{0} glöscht** + + + Ich chan kei quote finde mit därri ID :disappointed: + + + Quote statistike + + + Total + + + Meist quoteti person + + + Das isch kei korrete nachrichtelink + + + Du chasch numme nachrichte vom gliche server quote + + \ No newline at end of file diff --git a/src/Bot/Localization/Quote.resx b/src/Bot/Localization/Quote.resx new file mode 100644 index 0000000..b51d79c --- /dev/null +++ b/src/Bot/Localization/Quote.resx @@ -0,0 +1,54 @@ + + + + + + + + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + This server doesn't seem to have any quotes yet. You can add a quote with `!quote save @user` or `!quote save <messageId>` + + + You can't save your own quotes... + + + You can't save quotes by a bot... + + + **Quote Added** + + + **Removed #{0}** + + + I couldn't find a quote with that ID :disappointed: + + + Quote Stats + + + Total + + + Most quoted person + + + That is not a valid message link + + + You can only quote messages from the same server + + \ No newline at end of file diff --git a/src/Bot/Localization/Rank.Designer.cs b/src/Bot/Localization/Rank.Designer.cs new file mode 100644 index 0000000..23f5e16 --- /dev/null +++ b/src/Bot/Localization/Rank.Designer.cs @@ -0,0 +1,108 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Geekbot.Bot.Localization { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Rank { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Rank() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Geekbot.Bot.Localization.Rank", typeof(Rank).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to :warning: I couldn't find all usernames. Maybe they left the server?. + /// + internal static string FailedToResolveAllUsernames { + get { + return ResourceManager.GetString("FailedToResolveAllUsernames", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to :bar_chart: **{0} Highscore for {1}**. + /// + internal static string HighscoresFor { + get { + return ResourceManager.GetString("HighscoresFor", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Valid types are '`messages`' '`karma`', '`rolls`' and '`cookies`'. + /// + internal static string InvalidType { + get { + return ResourceManager.GetString("InvalidType", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to :warning: Limiting to 20. + /// + internal static string LimitingTo20Warning { + get { + return ResourceManager.GetString("LimitingTo20Warning", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to No {0} found on this server. + /// + internal static string NoTypeFoundForServer { + get { + return ResourceManager.GetString("NoTypeFoundForServer", resourceCulture); + } + } + } +} diff --git a/src/Bot/Localization/Rank.de-ch.resx b/src/Bot/Localization/Rank.de-ch.resx new file mode 100644 index 0000000..0b22fe4 --- /dev/null +++ b/src/Bot/Localization/Rank.de-ch.resx @@ -0,0 +1,29 @@ + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + :warning: Limitiert uf 20 + + + Kei {0} gfunde für dä server + + + :warning: Ich han nid alli benutzername gfunde. villiicht hend sie de server verlah? + + + :bar_chart: **{0} Highscore für {1}** + + + Gültigi paramenter sind '`messages`' '`karma`', '`rolls`' und '`cookies` + + \ No newline at end of file diff --git a/src/Bot/Localization/Rank.resx b/src/Bot/Localization/Rank.resx new file mode 100644 index 0000000..9598cf8 --- /dev/null +++ b/src/Bot/Localization/Rank.resx @@ -0,0 +1,36 @@ + + + + + + + + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Valid types are '`messages`' '`karma`', '`rolls`' and '`cookies`' + + + :warning: Limiting to 20 + + + No {0} found on this server + + + :warning: I couldn't find all usernames. Maybe they left the server? + + + :bar_chart: **{0} Highscore for {1}** + + \ No newline at end of file diff --git a/src/Bot/Localization/Roll.Designer.cs b/src/Bot/Localization/Roll.Designer.cs new file mode 100644 index 0000000..fda0536 --- /dev/null +++ b/src/Bot/Localization/Roll.Designer.cs @@ -0,0 +1,99 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Geekbot.Bot.Localization { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Roll { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Roll() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Geekbot.Bot.Localization.Roll", typeof(Roll).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to Congratulations {0}, your guess was correct!. + /// + internal static string Gratz { + get { + return ResourceManager.GetString("Gratz", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to :red_circle: {0}, you can't guess the same number again, guess another number or wait {1}. + /// + internal static string NoPrevGuess { + get { + return ResourceManager.GetString("NoPrevGuess", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to {0}, you rolled {1}, your guess was {2}. + /// + internal static string Rolled { + get { + return ResourceManager.GetString("Rolled", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to {0}, you rolled {1}. + /// + internal static string RolledNoGuess { + get { + return ResourceManager.GetString("RolledNoGuess", resourceCulture); + } + } + } +} diff --git a/src/Bot/Localization/Roll.de-ch.resx b/src/Bot/Localization/Roll.de-ch.resx new file mode 100644 index 0000000..ba73316 --- /dev/null +++ b/src/Bot/Localization/Roll.de-ch.resx @@ -0,0 +1,26 @@ + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + {0}, du hesch {1} grollt und hesch {2} grate + + + Gratuliere {0}, du hesch richtig grate! + + + {0}, du hesch {1} grollt + + + :red_circle: {0}, du chasch nid nomol es gliche rate, rate öppis anders oder warte {1} + + \ No newline at end of file diff --git a/src/Bot/Localization/Roll.resx b/src/Bot/Localization/Roll.resx new file mode 100644 index 0000000..822abb6 --- /dev/null +++ b/src/Bot/Localization/Roll.resx @@ -0,0 +1,33 @@ + + + + + + + + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + {0}, you rolled {1}, your guess was {2} + + + Congratulations {0}, your guess was correct! + + + {0}, you rolled {1} + + + :red_circle: {0}, you can't guess the same number again, guess another number or wait {1} + + \ No newline at end of file diff --git a/src/Bot/Localization/Ship.Designer.cs b/src/Bot/Localization/Ship.Designer.cs new file mode 100644 index 0000000..d959693 --- /dev/null +++ b/src/Bot/Localization/Ship.Designer.cs @@ -0,0 +1,117 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Geekbot.Bot.Localization { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Ship { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Ship() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Geekbot.Bot.Localization.Ship", typeof(Ship).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to Almost a match. + /// + internal static string CouldWork { + get { + return ResourceManager.GetString("CouldWork", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to It's a match. + /// + internal static string ItsAMatch { + get { + return ResourceManager.GetString("ItsAMatch", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Matchmaking. + /// + internal static string Matchmaking { + get { + return ResourceManager.GetString("Matchmaking", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Not going happen. + /// + internal static string NotGoingToHappen { + get { + return ResourceManager.GetString("NotGoingToHappen", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Not such a good idea. + /// + internal static string NotSuchAGoodIdea { + get { + return ResourceManager.GetString("NotSuchAGoodIdea", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to There might be a chance. + /// + internal static string ThereMightBeAChance { + get { + return ResourceManager.GetString("ThereMightBeAChance", resourceCulture); + } + } + } +} diff --git a/src/Bot/Localization/Ship.de-ch.resx b/src/Bot/Localization/Ship.de-ch.resx new file mode 100644 index 0000000..ec2880b --- /dev/null +++ b/src/Bot/Localization/Ship.de-ch.resx @@ -0,0 +1,32 @@ + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Verkupple + + + Wird nöd klappe + + + Nöd so ä gueti idee + + + Es gid eventuel ä chance + + + Fasch en match + + + Es isch es traumpaar + + \ No newline at end of file diff --git a/src/Bot/Localization/Ship.resx b/src/Bot/Localization/Ship.resx new file mode 100644 index 0000000..611040f --- /dev/null +++ b/src/Bot/Localization/Ship.resx @@ -0,0 +1,39 @@ + + + + + + + + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Matchmaking + + + Not going happen + + + Not such a good idea + + + There might be a chance + + + Almost a match + + + It's a match + + \ No newline at end of file diff --git a/src/Core/GeekbotCommandBase.cs b/src/Core/GeekbotCommandBase.cs new file mode 100644 index 0000000..8df5265 --- /dev/null +++ b/src/Core/GeekbotCommandBase.cs @@ -0,0 +1,27 @@ +using System.Globalization; +using System.Threading; +using Discord.Commands; +using Geekbot.Core.ErrorHandling; +using Geekbot.Core.Localization; + +namespace Geekbot.Core +{ + public class GeekbotCommandBase : ModuleBase + { + protected readonly IErrorHandler ErrorHandler; + protected readonly ITranslationHandler Translations; + + protected GeekbotCommandBase(IErrorHandler errorHandler, ITranslationHandler translations) + { + ErrorHandler = errorHandler; + Translations = translations; + } + + protected override void BeforeExecute(CommandInfo command) + { + base.BeforeExecute(command); + var language = Translations.GetServerLanguage(Context.Guild.Id); + Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(language == "CHDE" ? "de-ch" : language); + } + } +} \ No newline at end of file diff --git a/src/Core/Localization/ITranslationHandler.cs b/src/Core/Localization/ITranslationHandler.cs index 74b3e88..1cc06fa 100644 --- a/src/Core/Localization/ITranslationHandler.cs +++ b/src/Core/Localization/ITranslationHandler.cs @@ -12,5 +12,6 @@ namespace Geekbot.Core.Localization Task GetGuildContext(ICommandContext context); Task SetLanguage(ulong guildId, string language); List SupportedLanguages { get; } + string GetServerLanguage(ulong guildId); } } \ No newline at end of file diff --git a/src/Core/Localization/TranslationHandler.cs b/src/Core/Localization/TranslationHandler.cs index c7d35e9..1a46ada 100644 --- a/src/Core/Localization/TranslationHandler.cs +++ b/src/Core/Localization/TranslationHandler.cs @@ -89,7 +89,7 @@ namespace Geekbot.Core.Localization } } - private Task GetServerLanguage(ulong guildId) + public string GetServerLanguage(ulong guildId) { try { @@ -99,7 +99,7 @@ namespace Geekbot.Core.Localization lang = _serverLanguages[guildId]; if (!string.IsNullOrEmpty(lang)) { - return Task.FromResult(lang); + return lang; } throw new Exception(); } @@ -107,19 +107,19 @@ namespace Geekbot.Core.Localization { lang = _guildSettingsManager.GetSettings(guildId, false)?.Language ?? "EN"; _serverLanguages[guildId] = lang; - return Task.FromResult(lang); + return lang; } } catch (Exception e) { _logger.Error(LogSource.Geekbot, "Could not get guild language", e); - return Task.FromResult("EN"); + return "EN"; } } public async Task GetString(ulong guildId, string command, string stringName) { - var serverLang = await GetServerLanguage(guildId); + var serverLang = GetServerLanguage(guildId); return GetString(serverLang, command, stringName); } @@ -140,7 +140,7 @@ namespace Geekbot.Core.Localization try { var command = context.Message.Content.Split(' ').First().TrimStart('!').ToLower(); - var serverLanguage = await GetServerLanguage(context.Guild?.Id ?? 0); + var serverLanguage = GetServerLanguage(context.Guild?.Id ?? 0); return _translations[serverLanguage][command]; } catch (Exception e) @@ -153,7 +153,7 @@ namespace Geekbot.Core.Localization public async Task GetGuildContext(ICommandContext context) { var dict = await GetDict(context); - var language = await GetServerLanguage(context.Guild?.Id ?? 0); + var language = GetServerLanguage(context.Guild?.Id ?? 0); return new TranslationGuildContext(this, language, dict); } @@ -161,7 +161,7 @@ namespace Geekbot.Core.Localization { try { - var serverLanguage = await GetServerLanguage(context.Guild?.Id ?? 0); + var serverLanguage = GetServerLanguage(context.Guild?.Id ?? 0); return _translations[serverLanguage][command]; } catch (Exception e) diff --git a/src/Core/Localization/Translations.yml b/src/Core/Localization/Translations.yml index fbb55bf..678755d 100644 --- a/src/Core/Localization/Translations.yml +++ b/src/Core/Localization/Translations.yml @@ -15,13 +15,6 @@ dateTime: And: EN: "and" CHDE: "und" -admin: - NewLanguageSet: - EN: "I will reply in english from now on" - CHDE: "I werd ab jetzt uf schwiizerdüütsch antworte, äuuä" - GetLanguage: - EN: "I'm talking english" - CHDE: "I red schwiizerdüütsch" errorHandler: SomethingWentWrong: EN: "Something went wrong :confused:" @@ -30,86 +23,6 @@ httpErrors: 403: EN: "Seems like i don't have enough permission to that :confused:" CHDE: "Gseht danach us das ich nid gnueg recht han zum das mache :confused:" -choose: - Choice: - EN: "I Choose **{0}**" - CHDE: "I nimme **{0}**" -good: - CannotChangeOwn: - EN: "Sorry {0}, but you can't give yourself karma" - CHDE: "Sorry {0}, aber du chasch dr selber kei karma geh" - WaitUntill: - EN: "Sorry {0}, but you have to wait {1} before you can give karma again..." - CHDE: "Sorry {0}, aber du musch no {1} warte bisch d wieder karma chasch geh..." - Increased: - EN: "Karma gained" - CHDE: "Karma becho" - By: - EN: "By" - CHDE: "Vo" - Amount: - EN: "Amount" - CHDE: "Mengi" - Current: - EN: "Current" - CHDE: "Jetzt" -bad: - CannotChangeOwn: - EN: "Sorry {0}, but you can't lower your own karma" - CHDE: "Sorry {0}, aber du chasch dr din eigete karma nid weg neh" - WaitUntill: - EN: "Sorry {0}, but you have to wait {1} before you can lower karma again..." - CHDE: "Sorry {0}, aber du musch no {1} warte bisch d wieder karma chasch senke..." - Decreased: - EN: "Karma lowered" - CHDE: "Karma gsenkt" - By: - EN: "By" - CHDE: "Vo" - Amount: - EN: "Amount" - CHDE: "Mengi" - Current: - EN: "Current" - CHDE: "Jetzt" -roll: - Rolled: - EN: "{0}, you rolled {1}, your guess was {2}" - CHDE: "{0}, du hesch {1} grollt und hesch {2} grate" - Gratz: - EN: "Congratulations {0}, your guess was correct!" - CHDE: "Gratuliere {0}, du hesch richtig grate!" - RolledNoGuess: - EN: "{0}, you rolled {1}" - CHDE: "{0}, du hesch {1} grollt" - NoPrevGuess: - EN: ":red_circle: {0}, you can't guess the same number again, guess another number or wait {1}" - CHDE: ":red_circle: {0}, du chasch nid nomol es gliche rate, rate öppis anders oder warte {1}" -cookies: &cookiesAlias - GetCookies: - EN: "You got {0} cookies, there are now {1} cookies in you cookie jar" - CHDE: "Du häsch {0} guetzli becho, du häsch jetzt {1} guetzli ih dr büchse" - WaitForMoreCookies: - EN: "You already got cookies today, you can have more cookies in {0}" - CHDE: "Du hesch scho guetzli becho hüt, du chasch meh ha in {0}" - InYourJar: - EN: "There are {0} cookies in you cookie jar" - CHDE: "Es hät {0} guetzli ih dineri büchs" - Given: - EN: "You gave {0} cookies to {1}" - CHDE: "Du hesch {1} {0} guetzli geh" - NotEnoughToGive: - EN: "You don't have enough cookies" - CHDE: "Du hesch nid gnueg guetzli" - NotEnoughCookiesToEat: - EN: "Your cookie jar looks almost empty, you should probably not eat a cookie" - CHDE: "Du hesch chuum no guetzli ih dineri büchs, du sötsch warschinli keini esse" - AteCookies: - EN: "You ate {0} cookies, you've only got {1} cookies left" - CHDE: "Du hesch {0} guetzli gesse und hesch jezt no {1} übrig" -cookie: - # because command aliases are to hard to deal with... - <<: *cookiesAlias role: NoRolesConfigured: EN: "There are no roles configured for this server" @@ -140,74 +53,4 @@ role: CHDE: "{0} isch zur whitelist hinzuegfüegt" RemovedRoleFromWhitelist: EN: "Removed {0} from the whitelist" - CHDE: "{0} isch vo dr whitelist glöscht" -quote: - NoQuotesFound: - EN: "This server doesn't seem to have any quotes yet. You can add a quote with `!quote save @user` or `!quote save `" - CHDE: "Dä server het no kei quotes. Du chasch quotes hinzuefüege mit `!quote save @user` oder `!quote save `" - CannotSaveOwnQuotes: - EN: "You can't save your own quotes..." - CHDE: "Du chasch kei quotes vo dir selber speichere..." - CannotQuoteBots: - EN: "You can't save quotes by a bot..." - CHDE: "Du chasch kei quotes vomne bot speichere..." - QuoteAdded: - EN: "**Quote Added**" - CHDE: "**Quote hinzugfüegt**" - Removed: - EN: "**Removed #{0}**" - CHDE: "**#{0} glöscht**" - NotFoundWithId: - EN: "I couldn't find a quote with that ID :disappointed:" - CHDE: "Ich chan kei quote finde mit därri ID :disappointed:" - QuoteStats: - EN: "Quote Stats" - CHDE: "Quote statistike" - TotalQuotes: - EN: "Total" - CHDE: "Total" - MostQuotesPerson: - EN: "Most quoted person" - CHDE: "Meist quoteti person" - NotAValidMessageLink: - EN: "That is not a valid message link" - CHDE: "Das isch kei korrete nachrichtelink" - OnlyQuoteFromSameServer: - EN: "You can only quote messages from the same server" - CHDE: "Du chasch numme nachrichte vom gliche server quote" -rank: - InvalidType: - EN: "Valid types are '`messages`' '`karma`', '`rolls`' and '`cookies`'" - CHDE: "Gültigi paramenter sind '`messages`' '`karma`', '`rolls`' und '`cookies`'" - LimitingTo20Warning: - EN: ":warning: Limiting to 20\n" - CHDE: ":warning: Limitiert uf 20\n" - NoTypeFoundForServer: - EN: "No {0} found on this server" - CHDE: "Kei {0} gfunde für dä server" - FailedToResolveAllUsernames: - EN: ":warning: I couldn't find all usernames. Maybe they left the server?\n" - CHDE: ":warning: Ich han nid alli benutzername gfunde. villiicht hend sie de server verlah?\n" - HighscoresFor: - EN: ":bar_chart: **{0} Highscore for {1}**" - CHDE: ":bar_chart: **{0} Highscore für {1}**" -ship: - Matchmaking: - EN: "Matchmaking" - CHDE: "Verkupple" - NotGonnaToHappen: - EN: "Not gonna happen" - CHDE: "Wird nöd klappe" - NotSuchAGoodIdea: - EN: "Not such a good idea" - CHDE: "Nöd so ä gueti idee" - ThereMightBeAChance: - EN: "There might be a chance" - CHDE: "Es gid eventuel ä chance" - CouldWork: - EN: "Almost a match" - CHDE: "Fasch en match" - ItsAMatch: - EN: "It's a match" - CHDE: "Es isch es traumpaar" - \ No newline at end of file + CHDE: "{0} isch vo dr whitelist glöscht" \ No newline at end of file From 078c884df73c668aab75f916cd175deaaac7bd4c Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 14 Aug 2020 18:11:52 +0200 Subject: [PATCH 271/443] Convert Role command to new localization method --- src/Bot/Bot.csproj | 9 ++ src/Bot/Commands/Admin/Role.cs | 52 ++++----- src/Bot/Localization/Role.Designer.cs | 153 +++++++++++++++++++++++++ src/Bot/Localization/Role.de-ch.resx | 44 +++++++ src/Bot/Localization/Role.resx | 51 +++++++++ src/Core/Localization/Translations.yml | 33 +----- 6 files changed, 280 insertions(+), 62 deletions(-) create mode 100644 src/Bot/Localization/Role.Designer.cs create mode 100644 src/Bot/Localization/Role.de-ch.resx create mode 100644 src/Bot/Localization/Role.resx diff --git a/src/Bot/Bot.csproj b/src/Bot/Bot.csproj index 601766a..2d3cd2f 100644 --- a/src/Bot/Bot.csproj +++ b/src/Bot/Bot.csproj @@ -77,6 +77,10 @@ ResXFileCodeGenerator Quote.Designer.cs + + ResXFileCodeGenerator + Role.Designer.cs + @@ -127,5 +131,10 @@ True Quote.resx + + True + True + Role.resx + diff --git a/src/Bot/Commands/Admin/Role.cs b/src/Bot/Commands/Admin/Role.cs index e997fa8..c8459dd 100644 --- a/src/Bot/Commands/Admin/Role.cs +++ b/src/Bot/Commands/Admin/Role.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Discord.Net; +using Geekbot.Core; using Geekbot.Core.CommandPreconditions; using Geekbot.Core.Database; using Geekbot.Core.Database.Models; @@ -17,19 +18,15 @@ namespace Geekbot.Bot.Commands.Admin { [Group("role")] [DisableInDirectMessage] - public class Role : ModuleBase + public class Role : GeekbotCommandBase { private readonly DatabaseContext _database; - private readonly IErrorHandler _errorHandler; private readonly IReactionListener _reactionListener; - private readonly ITranslationHandler _translationHandler; - public Role(DatabaseContext database, IErrorHandler errorHandler, IReactionListener reactionListener, ITranslationHandler translationHandler) + public Role(DatabaseContext database, IErrorHandler errorHandler, IReactionListener reactionListener, ITranslationHandler translationHandler) : base(errorHandler, translationHandler) { _database = database; - _errorHandler = errorHandler; _reactionListener = reactionListener; - _translationHandler = translationHandler; } [Command(RunMode = RunMode.Async)] @@ -38,23 +35,22 @@ namespace Geekbot.Bot.Commands.Admin { try { - var transContext = await _translationHandler.GetGuildContext(Context); var roles = _database.RoleSelfService.Where(g => g.GuildId.Equals(Context.Guild.Id.AsLong())).ToList(); if (roles.Count == 0) { - await ReplyAsync(transContext.GetString("NoRolesConfigured")); + await ReplyAsync(Localization.Role.NoRolesConfigured); return; } var sb = new StringBuilder(); - sb.AppendLine(transContext.GetString("ListHeader", Context.Guild.Name)); - sb.AppendLine(transContext.GetString("ListInstruction")); + sb.AppendLine(string.Format(Localization.Role.ListHeader, Context.Guild.Name)); + sb.AppendLine(Localization.Role.ListInstruction); foreach (var role in roles) sb.AppendLine($"- {role.WhiteListName}"); await ReplyAsync(sb.ToString()); } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } @@ -64,7 +60,6 @@ namespace Geekbot.Bot.Commands.Admin { try { - var transContext = await _translationHandler.GetGuildContext(Context); var roleName = roleNameRaw.ToLower(); var roleFromDb = _database.RoleSelfService.FirstOrDefault(e => e.GuildId.Equals(Context.Guild.Id.AsLong()) && e.WhiteListName.Equals(roleName)); @@ -74,31 +69,31 @@ namespace Geekbot.Bot.Commands.Admin var role = Context.Guild.Roles.First(r => r.Id == roleFromDb.RoleId.AsUlong()); if (role == null) { - await ReplyAsync(transContext.GetString("RoleNotFound")); + await ReplyAsync(Localization.Role.RoleNotFound); return; } if (guildUser.RoleIds.Contains(role.Id)) { await guildUser.RemoveRoleAsync(role); - await ReplyAsync(transContext.GetString("RemovedUserFromRole", role.Name)); + await ReplyAsync(string.Format(Localization.Role.RemovedUserFromRole, role.Name)); return; } await guildUser.AddRoleAsync(role); - await ReplyAsync(transContext.GetString("AddedUserFromRole", role.Name)); + await ReplyAsync(string.Format(Localization.Role.AddedUserFromRole, role.Name)); return; } - await ReplyAsync(transContext.GetString("RoleNotFound")); + await ReplyAsync(Localization.Role.RoleNotFound); } catch (HttpException e) { - await _errorHandler.HandleHttpException(e, Context); + await ErrorHandler.HandleHttpException(e, Context); } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } @@ -109,10 +104,9 @@ namespace Geekbot.Bot.Commands.Admin { try { - var transContext = await _translationHandler.GetGuildContext(Context); if (role.IsManaged) { - await ReplyAsync(transContext.GetString("CannotAddManagedRole")); + await ReplyAsync(Localization.Role.CannotAddManagedRole); return; } @@ -122,7 +116,7 @@ namespace Geekbot.Bot.Commands.Admin || role.Permissions.BanMembers || role.Permissions.KickMembers) { - await ReplyAsync(transContext.GetString("CannotAddDangerousRole")); + await ReplyAsync(Localization.Role.CannotAddDangerousRole); return; } @@ -133,11 +127,11 @@ namespace Geekbot.Bot.Commands.Admin WhiteListName = roleName }); await _database.SaveChangesAsync(); - await ReplyAsync(transContext.GetString("AddedRoleToWhitelist", role.Name)); + await ReplyAsync(string.Format(Localization.Role.AddedRoleToWhitelist, role.Name)); } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } @@ -148,22 +142,21 @@ namespace Geekbot.Bot.Commands.Admin { try { - var transContext = await _translationHandler.GetGuildContext(Context); var roleFromDb = _database.RoleSelfService.FirstOrDefault(e => e.GuildId.Equals(Context.Guild.Id.AsLong()) && e.WhiteListName.Equals(roleName)); if (roleFromDb != null) { _database.RoleSelfService.Remove(roleFromDb); await _database.SaveChangesAsync(); - await ReplyAsync(transContext.GetString("RemovedRoleFromWhitelist", roleName)); + await ReplyAsync(string.Format(Localization.Role.RemovedRoleFromWhitelist, roleName)); return; } - await ReplyAsync(transContext.GetString("RoleNotFound")); + await ReplyAsync(Localization.Role.RoleNotFound); } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } @@ -182,14 +175,13 @@ namespace Geekbot.Bot.Commands.Admin await _reactionListener.AddRoleToListener(messageId, Context.Guild.Id, emoji, role); await Context.Message.DeleteAsync(); } - catch (HttpException e) + catch (HttpException) { await Context.Channel.SendMessageAsync("Custom emojis from other servers are not supported"); - Console.WriteLine(e); } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } } diff --git a/src/Bot/Localization/Role.Designer.cs b/src/Bot/Localization/Role.Designer.cs new file mode 100644 index 0000000..9128e3d --- /dev/null +++ b/src/Bot/Localization/Role.Designer.cs @@ -0,0 +1,153 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Geekbot.Bot.Localization { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Role { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Role() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Geekbot.Bot.Localization.Role", typeof(Role).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to Added {0} to the whitelist. + /// + internal static string AddedRoleToWhitelist { + get { + return ResourceManager.GetString("AddedRoleToWhitelist", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Added you to {0}. + /// + internal static string AddedUserFromRole { + get { + return ResourceManager.GetString("AddedUserFromRole", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to You cannot add that role to self service because it contains one or more dangerous permissions. + /// + internal static string CannotAddDangerousRole { + get { + return ResourceManager.GetString("CannotAddDangerousRole", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to You can't add a role that is managed by discord. + /// + internal static string CannotAddManagedRole { + get { + return ResourceManager.GetString("CannotAddManagedRole", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to **Self Service Roles on {0}**. + /// + internal static string ListHeader { + get { + return ResourceManager.GetString("ListHeader", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to To get a role, use `!role [name]`. + /// + internal static string ListInstruction { + get { + return ResourceManager.GetString("ListInstruction", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to There are no roles configured for this server. + /// + internal static string NoRolesConfigured { + get { + return ResourceManager.GetString("NoRolesConfigured", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Removed {0} from the whitelist. + /// + internal static string RemovedRoleFromWhitelist { + get { + return ResourceManager.GetString("RemovedRoleFromWhitelist", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Removed you from {0}. + /// + internal static string RemovedUserFromRole { + get { + return ResourceManager.GetString("RemovedUserFromRole", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to That role doesn't exist or is not on the whitelist. + /// + internal static string RoleNotFound { + get { + return ResourceManager.GetString("RoleNotFound", resourceCulture); + } + } + } +} diff --git a/src/Bot/Localization/Role.de-ch.resx b/src/Bot/Localization/Role.de-ch.resx new file mode 100644 index 0000000..b0b3259 --- /dev/null +++ b/src/Bot/Localization/Role.de-ch.resx @@ -0,0 +1,44 @@ + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Es sind kei rolle für dä server konfiguriert + + + **Self Service Rollene uf {0}** + + + Zum ä rolle becho, schriib `!role [name]` + + + Die rolle gids nid or isch nid uf dr whitelist + + + Han di entfernt vo {0} + + + Han di hinzue gfüegt zu {0} + + + Du chasch kei rolle hinzuefüge wo verwalted wird vo discord + + + Du chasch die rolle nid hinzuefüge will er ein oder mehreri gföhrlichi berechtigunge het + + + {0} isch zur whitelist hinzuegfüegt + + + {0} isch vo dr whitelist glöscht + + \ No newline at end of file diff --git a/src/Bot/Localization/Role.resx b/src/Bot/Localization/Role.resx new file mode 100644 index 0000000..63b70d0 --- /dev/null +++ b/src/Bot/Localization/Role.resx @@ -0,0 +1,51 @@ + + + + + + + + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + There are no roles configured for this server + + + **Self Service Roles on {0}** + + + To get a role, use `!role [name]` + + + That role doesn't exist or is not on the whitelist + + + Removed you from {0} + + + Added you to {0} + + + You can't add a role that is managed by discord + + + You cannot add that role to self service because it contains one or more dangerous permissions + + + Added {0} to the whitelist + + + Removed {0} from the whitelist + + \ No newline at end of file diff --git a/src/Core/Localization/Translations.yml b/src/Core/Localization/Translations.yml index 678755d..dc18c2b 100644 --- a/src/Core/Localization/Translations.yml +++ b/src/Core/Localization/Translations.yml @@ -22,35 +22,4 @@ errorHandler: httpErrors: 403: EN: "Seems like i don't have enough permission to that :confused:" - CHDE: "Gseht danach us das ich nid gnueg recht han zum das mache :confused:" -role: - NoRolesConfigured: - EN: "There are no roles configured for this server" - CHDE: "Es sind kei rolle für dä server konfiguriert" - ListHeader: - EN: "**Self Service Roles on {0}**" - CHDE: "**Self Service Rollene uf {0}**" - ListInstruction: - EN: "To get a role, use `!role [name]`" - CHDE: "Zum ä rolle becho, schriib `!role [name]`" - RoleNotFound: - EN: "That role doesn't exist or is not on the whitelist" - CHDE: "Die rolle gids nid or isch nid uf dr whitelist" - RemovedUserFromRole: - EN: "Removed you from {0}" - CHDE: "Han di entfernt vo {0}" - AddedUserFromRole: - EN: "Added you to {0}" - CHDE: "Han di hinzue gfüegt zu {0}" - CannotAddManagedRole: - EN: "You can't add a role that is managed by discord" - CHDE: "Du chasch kei rolle hinzuefüge wo verwalted wird vo discord" - CannotAddDangerousRole: - EN: "You cannot add that role to self service because it contains one or more dangerous permissions" - CHDE: "Du chasch die rolle nid hinzuefüge will er ein oder mehreri gföhrlichi berechtigunge het" - AddedRoleToWhitelist: - EN: "Added {0} to the whitelist" - CHDE: "{0} isch zur whitelist hinzuegfüegt" - RemovedRoleFromWhitelist: - EN: "Removed {0} from the whitelist" - CHDE: "{0} isch vo dr whitelist glöscht" \ No newline at end of file + CHDE: "Gseht danach us das ich nid gnueg recht han zum das mache :confused:" \ No newline at end of file From 33829e91bc2df8c1852c0c32d91c97dcc9f7c71f Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 14 Aug 2020 23:15:11 +0200 Subject: [PATCH 272/443] Delete the TranslationHandler and the old translations file. Refactor GeekbotCommandBase to get the server language from guild settings. Create DateLocalization to create a localized relative time remaining string. --- src/Bot/Commands/Admin/Admin.cs | 87 ++++---- src/Bot/Commands/Admin/Owner/Owner.cs | 19 +- src/Bot/Commands/Admin/Role.cs | 14 +- src/Bot/Commands/Games/Roll/Roll.cs | 19 +- src/Bot/Commands/Randomness/Ship.cs | 4 +- src/Bot/Commands/Rpg/Cookies.cs | 11 +- src/Bot/Commands/User/Karma.cs | 17 +- src/Bot/Commands/User/Ranking/Rank.cs | 13 +- src/Bot/Commands/Utils/Choose.cs | 4 +- src/Bot/Commands/Utils/Quote/Quote.cs | 5 +- src/Bot/Program.cs | 5 +- src/Bot/Utils/DateLocalization.cs | 49 +++++ src/Core/Core.csproj | 6 - src/Core/ErrorHandling/ErrorHandler.cs | 24 +-- src/Core/ErrorHandling/IErrorHandler.cs | 2 - src/Core/GeekbotCommandBase.cs | 13 +- src/Core/Localization/ITranslationHandler.cs | 17 -- .../Localization/TranslationGuildContext.cs | 90 -------- src/Core/Localization/TranslationHandler.cs | 194 ------------------ src/Core/Localization/Translations.yml | 25 --- .../TranslationGuildContext.test.cs | 71 ------- tests/Core/Localization/Translations.test.cs | 46 ----- 22 files changed, 156 insertions(+), 579 deletions(-) create mode 100644 src/Bot/Utils/DateLocalization.cs delete mode 100644 src/Core/Localization/ITranslationHandler.cs delete mode 100644 src/Core/Localization/TranslationGuildContext.cs delete mode 100644 src/Core/Localization/TranslationHandler.cs delete mode 100644 src/Core/Localization/Translations.yml delete mode 100644 tests/Core/Localization/TranslationGuildContext.test.cs delete mode 100644 tests/Core/Localization/Translations.test.cs diff --git a/src/Bot/Commands/Admin/Admin.cs b/src/Bot/Commands/Admin/Admin.cs index 2627f86..60ba8ac 100644 --- a/src/Bot/Commands/Admin/Admin.cs +++ b/src/Bot/Commands/Admin/Admin.cs @@ -14,7 +14,6 @@ using Geekbot.Core.CommandPreconditions; using Geekbot.Core.ErrorHandling; using Geekbot.Core.Extensions; using Geekbot.Core.GuildSettingsManager; -using Geekbot.Core.Localization; namespace Geekbot.Bot.Commands.Admin { @@ -24,21 +23,18 @@ namespace Geekbot.Bot.Commands.Admin public class Admin : GeekbotCommandBase { private readonly DiscordSocketClient _client; - private readonly IGuildSettingsManager _guildSettingsManager; - public Admin(DiscordSocketClient client, IErrorHandler errorHandler, IGuildSettingsManager guildSettingsManager, ITranslationHandler translationHandler) : base(errorHandler, translationHandler) + public Admin(DiscordSocketClient client, IErrorHandler errorHandler, IGuildSettingsManager guildSettingsManager) : base(errorHandler, guildSettingsManager) { _client = client; - _guildSettingsManager = guildSettingsManager; } [Command("welcome", RunMode = RunMode.Async)] [Summary("Set a Welcome Message (use '$user' to mention the new joined user).")] public async Task SetWelcomeMessage([Remainder, Summary("message")] string welcomeMessage) { - var guild = _guildSettingsManager.GetSettings(Context.Guild.Id); - guild.WelcomeMessage = welcomeMessage; - await _guildSettingsManager.UpdateSettings(guild); + GuildSettings.WelcomeMessage = welcomeMessage; + await GuildSettingsManager.UpdateSettings(GuildSettings); var formatedMessage = welcomeMessage.Replace("$user", Context.User.Mention); await ReplyAsync($"Welcome message has been changed\r\nHere is an example of how it would look:\r\n{formatedMessage}"); @@ -52,9 +48,8 @@ namespace Geekbot.Bot.Commands.Admin { var m = await channel.SendMessageAsync("..."); - var guild = _guildSettingsManager.GetSettings(Context.Guild.Id); - guild.WelcomeChannel = channel.Id.AsLong(); - await _guildSettingsManager.UpdateSettings(guild); + GuildSettings.WelcomeChannel = channel.Id.AsLong(); + await GuildSettingsManager.UpdateSettings(GuildSettings); await m.DeleteAsync(); @@ -74,9 +69,8 @@ namespace Geekbot.Bot.Commands.Admin { var m = await channel.SendMessageAsync("verifying..."); - var guild = _guildSettingsManager.GetSettings(Context.Guild.Id); - guild.ModChannel = channel.Id.AsLong(); - await _guildSettingsManager.UpdateSettings(guild); + GuildSettings.ModChannel = channel.Id.AsLong(); + await GuildSettingsManager.UpdateSettings(GuildSettings); var sb = new StringBuilder(); sb.AppendLine("Successfully saved mod channel, you can now do the following"); @@ -96,13 +90,12 @@ namespace Geekbot.Bot.Commands.Admin { try { - var guild = _guildSettingsManager.GetSettings(Context.Guild.Id); - var modChannel = await GetModChannel(guild.ModChannel.AsUlong()); + var modChannel = await GetModChannel(GuildSettings.ModChannel.AsUlong()); if (modChannel == null) return; - guild.ShowLeave = !guild.ShowLeave; - await _guildSettingsManager.UpdateSettings(guild); - await modChannel.SendMessageAsync(guild.ShowLeave + GuildSettings.ShowLeave = !GuildSettings.ShowLeave; + await GuildSettingsManager.UpdateSettings(GuildSettings); + await modChannel.SendMessageAsync(GuildSettings.ShowLeave ? "Saved - now sending messages here when someone leaves" : "Saved - stopping sending messages here when someone leaves" ); @@ -119,13 +112,12 @@ namespace Geekbot.Bot.Commands.Admin { try { - var guild = _guildSettingsManager.GetSettings(Context.Guild.Id); - var modChannel = await GetModChannel(guild.ModChannel.AsUlong()); + var modChannel = await GetModChannel(GuildSettings.ModChannel.AsUlong()); if (modChannel == null) return; - guild.ShowDelete = !guild.ShowDelete; - await _guildSettingsManager.UpdateSettings(guild); - await modChannel.SendMessageAsync(guild.ShowDelete + GuildSettings.ShowDelete = !GuildSettings.ShowDelete; + await GuildSettingsManager.UpdateSettings(GuildSettings); + await modChannel.SendMessageAsync(GuildSettings.ShowDelete ? "Saved - now sending messages here when someone deletes a message" : "Saved - stopping sending messages here when someone deletes a message" ); @@ -138,31 +130,25 @@ namespace Geekbot.Bot.Commands.Admin [Command("setlang", RunMode = RunMode.Async)] [Summary("Change the bots language")] - public async Task SetLanguage([Summary("language")] string languageRaw) + public async Task SetLanguage([Summary("language")] string language) { try { - var language = languageRaw.ToUpper(); - var success = await Translations.SetLanguage(Context.Guild.Id, language); - if (success) + var availableLanguages = new List(); + availableLanguages.Add("en-GB"); // default + availableLanguages.AddRange(GetAvailableCultures().Select(culture => culture.Name)); + if (availableLanguages.Contains(language)) { - var guild = _guildSettingsManager.GetSettings(Context.Guild.Id); - guild.Language = language; - await _guildSettingsManager.UpdateSettings(guild); + GuildSettings.Language = language; + await GuildSettingsManager.UpdateSettings(GuildSettings); + + Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(language.ToLower() == "chde" ? "de-CH" : language); - if (language.ToLower() == "chde") - { - Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("de-ch"); - } - await ReplyAsync(Localization.Admin.NewLanguageSet); return; } - - var available = new List(); - available.Add("en-GB"); // default - available.AddRange(GetAvailableCultures().Select(culture => culture.Name)); - await ReplyAsync($"That doesn't seem to be a supported language\nSupported Languages are {string.Join(", ", available)}"); + + await ReplyAsync($"That doesn't seem to be a supported language\nSupported Languages are {string.Join(", ", availableLanguages)}"); } catch (Exception e) { @@ -177,9 +163,8 @@ namespace Geekbot.Bot.Commands.Admin try { var language = languageRaw.ToLower(); - var guild = _guildSettingsManager.GetSettings(Context.Guild.Id); - guild.WikiLang = language; - await _guildSettingsManager.UpdateSettings(guild); + GuildSettings.WikiLang = language; + await GuildSettingsManager.UpdateSettings(GuildSettings); await ReplyAsync($"Now using the {language} wikipedia"); } @@ -195,10 +180,10 @@ namespace Geekbot.Bot.Commands.Admin { try { - var guild = _guildSettingsManager.GetSettings(Context.Guild.Id); - guild.Ping = !guild.Ping; - await _guildSettingsManager.UpdateSettings(guild); - await ReplyAsync(guild.Ping ? "i will reply to ping now" : "No more pongs..."); + // var guild = _guildSettingsManager.GetSettings(Context.Guild.Id); + GuildSettings.Ping = !GuildSettings.Ping; + await GuildSettingsManager.UpdateSettings(GuildSettings); + await ReplyAsync(GuildSettings.Ping ? "i will reply to ping now" : "No more pongs..."); } catch (Exception e) { @@ -212,10 +197,10 @@ namespace Geekbot.Bot.Commands.Admin { try { - var guild = _guildSettingsManager.GetSettings(Context.Guild.Id); - guild.Hui = !guild.Hui; - await _guildSettingsManager.UpdateSettings(guild); - await ReplyAsync(guild.Hui ? "i will reply to hui now" : "No more hui's..."); + // var guild = _guildSettingsManager.GetSettings(Context.Guild.Id); + GuildSettings.Hui = !GuildSettings.Hui; + await GuildSettingsManager.UpdateSettings(GuildSettings); + await ReplyAsync(GuildSettings.Hui ? "i will reply to hui now" : "No more hui's..."); } catch (Exception e) { diff --git a/src/Bot/Commands/Admin/Owner/Owner.cs b/src/Bot/Commands/Admin/Owner/Owner.cs index 5fa3976..64292e9 100644 --- a/src/Bot/Commands/Admin/Owner/Owner.cs +++ b/src/Bot/Commands/Admin/Owner/Owner.cs @@ -3,8 +3,10 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; using Discord.WebSocket; +using Geekbot.Core; using Geekbot.Core.ErrorHandling; using Geekbot.Core.GlobalSettings; +using Geekbot.Core.GuildSettingsManager; using Geekbot.Core.Logger; using Geekbot.Core.UserRepository; @@ -12,20 +14,19 @@ namespace Geekbot.Bot.Commands.Admin.Owner { [Group("owner")] [RequireOwner] - public class Owner : ModuleBase + public class Owner : GeekbotCommandBase { private readonly DiscordSocketClient _client; - private readonly IErrorHandler _errorHandler; private readonly IGlobalSettings _globalSettings; private readonly IGeekbotLogger _logger; private readonly IUserRepository _userRepository; - public Owner(DiscordSocketClient client, IGeekbotLogger logger, IUserRepository userRepositry, IErrorHandler errorHandler, IGlobalSettings globalSettings) + public Owner(DiscordSocketClient client, IGeekbotLogger logger, IUserRepository userRepositry, IErrorHandler errorHandler, IGlobalSettings globalSettings, + IGuildSettingsManager guildSettingsManager) : base(errorHandler, guildSettingsManager) { _client = client; _logger = logger; _userRepository = userRepositry; - _errorHandler = errorHandler; _globalSettings = globalSettings; } @@ -73,7 +74,7 @@ namespace Geekbot.Bot.Commands.Admin.Owner } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context, + await ErrorHandler.HandleCommandException(e, Context, "Couldn't complete User Repository, see console for more info"); } } @@ -89,10 +90,10 @@ namespace Geekbot.Bot.Commands.Admin.Owner } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } - + [Command("refreshuser", RunMode = RunMode.Async)] [Summary("Refresh a user in the user cache")] public async Task PopUserRepoCommand([Summary("user-id")] ulong userId) @@ -105,7 +106,7 @@ namespace Geekbot.Bot.Commands.Admin.Owner } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } @@ -119,7 +120,7 @@ namespace Geekbot.Bot.Commands.Admin.Owner } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } } diff --git a/src/Bot/Commands/Admin/Role.cs b/src/Bot/Commands/Admin/Role.cs index c8459dd..efec7bd 100644 --- a/src/Bot/Commands/Admin/Role.cs +++ b/src/Bot/Commands/Admin/Role.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using System.Net; using System.Text; using System.Threading.Tasks; using Discord; @@ -11,7 +12,7 @@ using Geekbot.Core.Database; using Geekbot.Core.Database.Models; using Geekbot.Core.ErrorHandling; using Geekbot.Core.Extensions; -using Geekbot.Core.Localization; +using Geekbot.Core.GuildSettingsManager; using Geekbot.Core.ReactionListener; namespace Geekbot.Bot.Commands.Admin @@ -23,7 +24,7 @@ namespace Geekbot.Bot.Commands.Admin private readonly DatabaseContext _database; private readonly IReactionListener _reactionListener; - public Role(DatabaseContext database, IErrorHandler errorHandler, IReactionListener reactionListener, ITranslationHandler translationHandler) : base(errorHandler, translationHandler) + public Role(DatabaseContext database, IErrorHandler errorHandler, IReactionListener reactionListener, IGuildSettingsManager guildSettingsManager) : base(errorHandler, guildSettingsManager) { _database = database; _reactionListener = reactionListener; @@ -89,7 +90,14 @@ namespace Geekbot.Bot.Commands.Admin } catch (HttpException e) { - await ErrorHandler.HandleHttpException(e, Context); + if (e.HttpCode == HttpStatusCode.Forbidden) + { + await ReplyAsync(Localization.Internal.Http403); + } + else + { + await ErrorHandler.HandleCommandException(e, Context); + } } catch (Exception e) { diff --git a/src/Bot/Commands/Games/Roll/Roll.cs b/src/Bot/Commands/Games/Roll/Roll.cs index 244edbe..6ef9322 100644 --- a/src/Bot/Commands/Games/Roll/Roll.cs +++ b/src/Bot/Commands/Games/Roll/Roll.cs @@ -2,13 +2,14 @@ using System.Linq; using System.Threading.Tasks; using Discord.Commands; +using Geekbot.Bot.Utils; using Geekbot.Core; using Geekbot.Core.Database; using Geekbot.Core.Database.Models; using Geekbot.Core.ErrorHandling; using Geekbot.Core.Extensions; +using Geekbot.Core.GuildSettingsManager; using Geekbot.Core.KvInMemoryStore; -using Geekbot.Core.Localization; using Geekbot.Core.RandomNumberGenerator; namespace Geekbot.Bot.Commands.Games.Roll @@ -19,7 +20,8 @@ namespace Geekbot.Bot.Commands.Games.Roll private readonly DatabaseContext _database; private readonly IRandomNumberGenerator _randomNumberGenerator; - public Roll(IKvInMemoryStore kvInMemoryStore,IErrorHandler errorHandler, ITranslationHandler translation, DatabaseContext database, IRandomNumberGenerator randomNumberGenerator) : base(errorHandler, translation) + public Roll(IKvInMemoryStore kvInMemoryStore, IErrorHandler errorHandler, DatabaseContext database, IRandomNumberGenerator randomNumberGenerator, IGuildSettingsManager guildSettingsManager) + : base(errorHandler, guildSettingsManager) { _kvInMemoryStore = kvInMemoryStore; _database = database; @@ -34,10 +36,9 @@ namespace Geekbot.Bot.Commands.Games.Roll { var number = _randomNumberGenerator.Next(1, 100); int.TryParse(stuff, out var guess); - var transContext = await Translations.GetGuildContext(Context); if (guess <= 100 && guess > 0) { - var kvKey = $"{Context.Guild.Id}:{Context.User.Id}:RollsPrevious"; + var kvKey = $"{Context?.Guild?.Id ?? 0}:{Context.User.Id}:RollsPrevious"; var prevRoll = _kvInMemoryStore.Get(kvKey); @@ -46,11 +47,11 @@ namespace Geekbot.Bot.Commands.Games.Roll await ReplyAsync(string.Format( Localization.Roll.NoPrevGuess, Context.Message.Author.Mention, - transContext.FormatDateTimeAsRemaining(prevRoll.GuessedOn.AddDays(1)))); + DateLocalization.FormatDateTimeAsRemaining(prevRoll.GuessedOn.AddDays(1)))); return; } - _kvInMemoryStore.Set(kvKey, new RollTimeout { LastGuess = guess, GuessedOn = DateTime.Now }); + _kvInMemoryStore.Set(kvKey, new RollTimeout {LastGuess = guess, GuessedOn = DateTime.Now}); await ReplyAsync(string.Format(Localization.Roll.Rolled, Context.Message.Author.Mention, number, guess)); if (guess == number) @@ -72,13 +73,13 @@ namespace Geekbot.Bot.Commands.Games.Roll await ErrorHandler.HandleCommandException(e, Context); } } - + private async Task GetUser(ulong userId) { - var user = _database.Rolls.FirstOrDefault(u =>u.GuildId.Equals(Context.Guild.Id.AsLong()) && u.UserId.Equals(userId.AsLong())) ?? await CreateNewRow(userId); + var user = _database.Rolls.FirstOrDefault(u => u.GuildId.Equals(Context.Guild.Id.AsLong()) && u.UserId.Equals(userId.AsLong())) ?? await CreateNewRow(userId); return user; } - + private async Task CreateNewRow(ulong userId) { var user = new RollsModel() diff --git a/src/Bot/Commands/Randomness/Ship.cs b/src/Bot/Commands/Randomness/Ship.cs index 78f3c99..f48713e 100644 --- a/src/Bot/Commands/Randomness/Ship.cs +++ b/src/Bot/Commands/Randomness/Ship.cs @@ -8,7 +8,7 @@ using Geekbot.Core.Database; using Geekbot.Core.Database.Models; using Geekbot.Core.ErrorHandling; using Geekbot.Core.Extensions; -using Geekbot.Core.Localization; +using Geekbot.Core.GuildSettingsManager; using Geekbot.Core.RandomNumberGenerator; namespace Geekbot.Bot.Commands.Randomness @@ -18,7 +18,7 @@ namespace Geekbot.Bot.Commands.Randomness private readonly IRandomNumberGenerator _randomNumberGenerator; private readonly DatabaseContext _database; - public Ship(DatabaseContext database, IErrorHandler errorHandler, IRandomNumberGenerator randomNumberGenerator, ITranslationHandler translations) : base(errorHandler, translations) + public Ship(DatabaseContext database, IErrorHandler errorHandler, IRandomNumberGenerator randomNumberGenerator, IGuildSettingsManager guildSettingsManager) : base(errorHandler, guildSettingsManager) { _database = database; _randomNumberGenerator = randomNumberGenerator; diff --git a/src/Bot/Commands/Rpg/Cookies.cs b/src/Bot/Commands/Rpg/Cookies.cs index 570f08a..a51d652 100644 --- a/src/Bot/Commands/Rpg/Cookies.cs +++ b/src/Bot/Commands/Rpg/Cookies.cs @@ -3,13 +3,14 @@ using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; +using Geekbot.Bot.Utils; using Geekbot.Core; using Geekbot.Core.CommandPreconditions; using Geekbot.Core.Database; using Geekbot.Core.Database.Models; using Geekbot.Core.ErrorHandling; using Geekbot.Core.Extensions; -using Geekbot.Core.Localization; +using Geekbot.Core.GuildSettingsManager; using Geekbot.Core.RandomNumberGenerator; namespace Geekbot.Bot.Commands.Rpg @@ -22,7 +23,8 @@ namespace Geekbot.Bot.Commands.Rpg private readonly DatabaseContext _database; private readonly IRandomNumberGenerator _randomNumberGenerator; - public Cookies(DatabaseContext database, IErrorHandler errorHandler, ITranslationHandler translations , IRandomNumberGenerator randomNumberGenerator) : base(errorHandler, translations) + public Cookies(DatabaseContext database, IErrorHandler errorHandler, IRandomNumberGenerator randomNumberGenerator, IGuildSettingsManager guildSettingsManager) + : base(errorHandler, guildSettingsManager) { _database = database; _randomNumberGenerator = randomNumberGenerator; @@ -34,12 +36,11 @@ namespace Geekbot.Bot.Commands.Rpg { try { - var transContext = await Translations.GetGuildContext(Context); var actor = await GetUser(Context.User.Id); if (actor.LastPayout.Value.AddDays(1).Date > DateTime.Now.Date) { - var formatedWaitTime = transContext.FormatDateTimeAsRemaining(DateTimeOffset.Now.AddDays(1).Date); - await ReplyAsync(string.Format(Localization.Cookies.WaitForMoreCookies, formatedWaitTime)); + var formattedWaitTime = DateLocalization.FormatDateTimeAsRemaining(DateTimeOffset.Now.AddDays(1).Date); + await ReplyAsync(string.Format(Localization.Cookies.WaitForMoreCookies, formattedWaitTime)); return; } actor.Cookies += 10; diff --git a/src/Bot/Commands/User/Karma.cs b/src/Bot/Commands/User/Karma.cs index 41a401c..4778bce 100644 --- a/src/Bot/Commands/User/Karma.cs +++ b/src/Bot/Commands/User/Karma.cs @@ -1,17 +1,16 @@ using System; -using System.Globalization; using System.Linq; -using System.Threading; using System.Threading.Tasks; using Discord; using Discord.Commands; +using Geekbot.Bot.Utils; using Geekbot.Core; using Geekbot.Core.CommandPreconditions; using Geekbot.Core.Database; using Geekbot.Core.Database.Models; using Geekbot.Core.ErrorHandling; using Geekbot.Core.Extensions; -using Geekbot.Core.Localization; +using Geekbot.Core.GuildSettingsManager; namespace Geekbot.Bot.Commands.User { @@ -19,12 +18,10 @@ namespace Geekbot.Bot.Commands.User public class Karma : GeekbotCommandBase { private readonly DatabaseContext _database; - private readonly ITranslationHandler _translations; - public Karma(DatabaseContext database, IErrorHandler errorHandler, ITranslationHandler translations) : base(errorHandler, translations) + public Karma(DatabaseContext database, IErrorHandler errorHandler, IGuildSettingsManager guildSettingsManager) : base(errorHandler, guildSettingsManager) { _database = database; - _translations = translations; } [Command("good", RunMode = RunMode.Async)] @@ -33,8 +30,6 @@ namespace Geekbot.Bot.Commands.User { try { - var transContext = await _translations.GetGuildContext(Context); - var actor = await GetUser(Context.User.Id); if (user.Id == Context.User.Id) { @@ -42,7 +37,7 @@ namespace Geekbot.Bot.Commands.User } else if (TimeoutFinished(actor.TimeOut)) { - var formatedWaitTime = transContext.FormatDateTimeAsRemaining(actor.TimeOut.AddMinutes(3)); + var formatedWaitTime = DateLocalization.FormatDateTimeAsRemaining(actor.TimeOut.AddMinutes(3)); await ReplyAsync(string.Format(Localization.Karma.WaitUntill, Context.User.Username, formatedWaitTime)); } else @@ -81,8 +76,6 @@ namespace Geekbot.Bot.Commands.User { try { - var transContext = await _translations.GetGuildContext(Context); - var actor = await GetUser(Context.User.Id); if (user.Id == Context.User.Id) { @@ -90,7 +83,7 @@ namespace Geekbot.Bot.Commands.User } else if (TimeoutFinished(actor.TimeOut)) { - var formatedWaitTime = transContext.FormatDateTimeAsRemaining(actor.TimeOut.AddMinutes(3)); + var formatedWaitTime = DateLocalization.FormatDateTimeAsRemaining(actor.TimeOut.AddMinutes(3)); await ReplyAsync(string.Format(Localization.Karma.WaitUntill, Context.User.Username, formatedWaitTime)); } else diff --git a/src/Bot/Commands/User/Ranking/Rank.cs b/src/Bot/Commands/User/Ranking/Rank.cs index d73aacc..20749a8 100644 --- a/src/Bot/Commands/User/Ranking/Rank.cs +++ b/src/Bot/Commands/User/Ranking/Rank.cs @@ -10,8 +10,8 @@ using Geekbot.Core.Converters; using Geekbot.Core.Database; using Geekbot.Core.ErrorHandling; using Geekbot.Core.Extensions; +using Geekbot.Core.GuildSettingsManager; using Geekbot.Core.Highscores; -using Geekbot.Core.Localization; namespace Geekbot.Bot.Commands.User.Ranking { @@ -21,7 +21,8 @@ namespace Geekbot.Bot.Commands.User.Ranking private readonly IHighscoreManager _highscoreManager; private readonly DatabaseContext _database; - public Rank(DatabaseContext database, IErrorHandler errorHandler, IEmojiConverter emojiConverter, IHighscoreManager highscoreManager, ITranslationHandler translations): base(errorHandler, translations) + public Rank(DatabaseContext database, IErrorHandler errorHandler, IEmojiConverter emojiConverter, IHighscoreManager highscoreManager, IGuildSettingsManager guildSettingsManager) + : base(errorHandler, guildSettingsManager) { _database = database; _emojiConverter = emojiConverter; @@ -53,7 +54,7 @@ namespace Geekbot.Bot.Commands.User.Ranking await ReplyAsync(Localization.Rank.LimitingTo20Warning); amount = 20; } - + var guildId = Context.Guild.Id; Dictionary highscoreUsers; try @@ -78,9 +79,9 @@ namespace Geekbot.Bot.Commands.User.Ranking var failedToRetrieveUser = highscoreUsers.Any(e => string.IsNullOrEmpty(e.Key.Username)); if (failedToRetrieveUser) replyBuilder.AppendLine(Localization.Rank.FailedToResolveAllUsernames).AppendLine(); - + replyBuilder.AppendLine(string.Format(Localization.Rank.HighscoresFor, type.ToString().CapitalizeFirst(), Context.Guild.Name)); - + var highscorePlace = 1; foreach (var (user, value) in highscoreUsers) { @@ -91,7 +92,7 @@ namespace Geekbot.Bot.Commands.User.Ranking replyBuilder.Append(user.Username != null ? $"**{user.Username}#{user.Discriminator}**" : $"**{user.Id}**"); - + replyBuilder.Append(type == HighscoreTypes.messages ? $" - {value} {type} - {Math.Round((double) (100 * value) / guildMessages, 2)}%\n" : $" - {value} {type}\n"); diff --git a/src/Bot/Commands/Utils/Choose.cs b/src/Bot/Commands/Utils/Choose.cs index ea239d7..731bee6 100644 --- a/src/Bot/Commands/Utils/Choose.cs +++ b/src/Bot/Commands/Utils/Choose.cs @@ -3,13 +3,13 @@ using System.Threading.Tasks; using Discord.Commands; using Geekbot.Core; using Geekbot.Core.ErrorHandling; -using Geekbot.Core.Localization; +using Geekbot.Core.GuildSettingsManager; namespace Geekbot.Bot.Commands.Utils { public class Choose : GeekbotCommandBase { - public Choose(IErrorHandler errorHandler, ITranslationHandler translation) : base(errorHandler, translation) + public Choose(IErrorHandler errorHandler, IGuildSettingsManager guildSettingsManager) : base(errorHandler, guildSettingsManager) { } diff --git a/src/Bot/Commands/Utils/Quote/Quote.cs b/src/Bot/Commands/Utils/Quote/Quote.cs index 5bb23a9..a7d3ff1 100644 --- a/src/Bot/Commands/Utils/Quote/Quote.cs +++ b/src/Bot/Commands/Utils/Quote/Quote.cs @@ -9,7 +9,7 @@ using Geekbot.Core.Database; using Geekbot.Core.Database.Models; using Geekbot.Core.ErrorHandling; using Geekbot.Core.Extensions; -using Geekbot.Core.Localization; +using Geekbot.Core.GuildSettingsManager; using Geekbot.Core.Polyfills; using Geekbot.Core.RandomNumberGenerator; @@ -23,7 +23,8 @@ namespace Geekbot.Bot.Commands.Utils.Quote private readonly IRandomNumberGenerator _randomNumberGenerator; private readonly bool _isDev; - public Quote(IErrorHandler errorHandler, DatabaseContext database, IRandomNumberGenerator randomNumberGenerator, ITranslationHandler translationHandler) : base(errorHandler, translationHandler) + public Quote(IErrorHandler errorHandler, DatabaseContext database, IRandomNumberGenerator randomNumberGenerator, IGuildSettingsManager guildSettingsManager) + : base(errorHandler, guildSettingsManager) { _database = database; _randomNumberGenerator = randomNumberGenerator; diff --git a/src/Bot/Program.cs b/src/Bot/Program.cs index 04b1450..5a4b456 100644 --- a/src/Bot/Program.cs +++ b/src/Bot/Program.cs @@ -17,7 +17,6 @@ using Geekbot.Core.GuildSettingsManager; using Geekbot.Core.Highscores; using Geekbot.Core.KvInMemoryStore; using Geekbot.Core.Levels; -using Geekbot.Core.Localization; using Geekbot.Core.Logger; using Geekbot.Core.MalClient; using Geekbot.Core.Media; @@ -168,8 +167,7 @@ namespace Geekbot.Bot var randomNumberGenerator = new RandomNumberGenerator(); var mediaProvider = new MediaProvider(_logger, randomNumberGenerator); var kvMemoryStore = new KvInInMemoryStore(); - var translationHandler = new TranslationHandler(_logger, _guildSettingsManager); - var errorHandler = new ErrorHandler(_logger, translationHandler, _runParameters); + var errorHandler = new ErrorHandler(_logger, _runParameters, () => Localization.Internal.SomethingWentWrong); var diceParser = new DiceParser(randomNumberGenerator); services.AddSingleton(_userRepository); @@ -186,7 +184,6 @@ namespace Geekbot.Bot services.AddSingleton(_globalSettings); services.AddSingleton(errorHandler); services.AddSingleton(diceParser); - services.AddSingleton(translationHandler); services.AddSingleton(_reactionListener); services.AddSingleton(_guildSettingsManager); services.AddSingleton(_client); diff --git a/src/Bot/Utils/DateLocalization.cs b/src/Bot/Utils/DateLocalization.cs new file mode 100644 index 0000000..eea40fb --- /dev/null +++ b/src/Bot/Utils/DateLocalization.cs @@ -0,0 +1,49 @@ +using System; +using System.Text; + +namespace Geekbot.Bot.Utils +{ + public class DateLocalization + { + public static string FormatDateTimeAsRemaining(DateTimeOffset dateTime) + { + var remaining = dateTime - DateTimeOffset.Now; + const string formattable = "{0} {1}"; + var sb = new StringBuilder(); + + if (remaining.Days > 0) + { + sb.AppendFormat(formattable, remaining.Days, GetSingularOrPlural(remaining.Days, Localization.Internal.Days)); + } + + if (remaining.Hours > 0) + { + if (sb.Length > 0) sb.Append(", "); + sb.AppendFormat(formattable, remaining.Hours, GetSingularOrPlural(remaining.Hours, Localization.Internal.Hours)); + } + + if (remaining.Minutes > 0) + { + if (sb.Length > 0) sb.Append(", "); + sb.AppendFormat(formattable, remaining.Minutes, GetSingularOrPlural(remaining.Minutes, Localization.Internal.Minutes)); + } + + if (remaining.Seconds > 0) + { + if (sb.Length > 0) + { + sb.AppendFormat(" {0} ", Localization.Internal.And); + } + sb.AppendFormat(formattable, remaining.Seconds, GetSingularOrPlural(remaining.Seconds, Localization.Internal.Seconds)); + } + + return sb.ToString().Trim(); + } + + private static string GetSingularOrPlural(int number, string rawString) + { + var versions = rawString.Split('|'); + return number == 1 ? versions[0] : versions[1]; + } + } +} \ No newline at end of file diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index fcbce09..fd44a95 100644 --- a/src/Core/Core.csproj +++ b/src/Core/Core.csproj @@ -31,10 +31,4 @@ - - - PreserveNewest - - - diff --git a/src/Core/ErrorHandling/ErrorHandler.cs b/src/Core/ErrorHandling/ErrorHandler.cs index 02402f2..47ed22d 100644 --- a/src/Core/ErrorHandling/ErrorHandler.cs +++ b/src/Core/ErrorHandling/ErrorHandler.cs @@ -1,9 +1,6 @@ using System; -using System.Net; using System.Threading.Tasks; using Discord.Commands; -using Discord.Net; -using Geekbot.Core.Localization; using Geekbot.Core.Logger; using SharpRaven; using SharpRaven.Data; @@ -14,14 +11,14 @@ namespace Geekbot.Core.ErrorHandling public class ErrorHandler : IErrorHandler { private readonly IGeekbotLogger _logger; - private readonly ITranslationHandler _translation; + private readonly Func _getDefaultErrorText; private readonly IRavenClient _raven; private readonly bool _errorsInChat; - public ErrorHandler(IGeekbotLogger logger, ITranslationHandler translation, RunParameters runParameters) + public ErrorHandler(IGeekbotLogger logger, RunParameters runParameters, Func getDefaultErrorText) { _logger = logger; - _translation = translation; + _getDefaultErrorText = getDefaultErrorText; _errorsInChat = runParameters.ExposeErrors; var sentryDsn = runParameters.SentryEndpoint; @@ -40,7 +37,9 @@ namespace Geekbot.Core.ErrorHandling { try { - var errorString = errorMessage == "def" ? await _translation.GetString(context.Guild?.Id ?? 0, "errorHandler", "SomethingWentWrong") : errorMessage; + var errorString = errorMessage == "def" + ? _getDefaultErrorText() + : errorMessage; var errorObj = SimpleConextConverter.ConvertContext(context); if (e.Message.Contains("50007")) return; if (e.Message.Contains("50013")) return; @@ -76,17 +75,6 @@ namespace Geekbot.Core.ErrorHandling } } - public async Task HandleHttpException(HttpException e, ICommandContext context) - { - var errorStrings = await _translation.GetDict(context, "httpErrors"); - switch(e.HttpCode) - { - case HttpStatusCode.Forbidden: - await context.Channel.SendMessageAsync(errorStrings["403"]); - break; - } - } - private void ReportExternal(Exception e, MessageDto errorObj) { if (_raven == null) return; diff --git a/src/Core/ErrorHandling/IErrorHandler.cs b/src/Core/ErrorHandling/IErrorHandler.cs index c012b82..d0e1d20 100644 --- a/src/Core/ErrorHandling/IErrorHandler.cs +++ b/src/Core/ErrorHandling/IErrorHandler.cs @@ -1,13 +1,11 @@ using System; using System.Threading.Tasks; using Discord.Commands; -using Discord.Net; namespace Geekbot.Core.ErrorHandling { public interface IErrorHandler { Task HandleCommandException(Exception e, ICommandContext context, string errorMessage = "def"); - Task HandleHttpException(HttpException e, ICommandContext context); } } \ No newline at end of file diff --git a/src/Core/GeekbotCommandBase.cs b/src/Core/GeekbotCommandBase.cs index 8df5265..43ced95 100644 --- a/src/Core/GeekbotCommandBase.cs +++ b/src/Core/GeekbotCommandBase.cs @@ -1,26 +1,29 @@ using System.Globalization; using System.Threading; using Discord.Commands; +using Geekbot.Core.Database.Models; using Geekbot.Core.ErrorHandling; -using Geekbot.Core.Localization; +using Geekbot.Core.GuildSettingsManager; namespace Geekbot.Core { public class GeekbotCommandBase : ModuleBase { + protected readonly IGuildSettingsManager GuildSettingsManager; + protected GuildSettingsModel GuildSettings; protected readonly IErrorHandler ErrorHandler; - protected readonly ITranslationHandler Translations; - protected GeekbotCommandBase(IErrorHandler errorHandler, ITranslationHandler translations) + protected GeekbotCommandBase(IErrorHandler errorHandler, IGuildSettingsManager guildSettingsManager) { + GuildSettingsManager = guildSettingsManager; ErrorHandler = errorHandler; - Translations = translations; } protected override void BeforeExecute(CommandInfo command) { base.BeforeExecute(command); - var language = Translations.GetServerLanguage(Context.Guild.Id); + GuildSettings = GuildSettingsManager.GetSettings(Context?.Guild?.Id ?? 0); + var language = GuildSettings.Language; Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(language == "CHDE" ? "de-ch" : language); } } diff --git a/src/Core/Localization/ITranslationHandler.cs b/src/Core/Localization/ITranslationHandler.cs deleted file mode 100644 index 1cc06fa..0000000 --- a/src/Core/Localization/ITranslationHandler.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System.Collections.Generic; -using System.Threading.Tasks; -using Discord.Commands; - -namespace Geekbot.Core.Localization -{ - public interface ITranslationHandler - { - Task GetString(ulong guildId, string command, string stringName); - string GetString(string language, string command, string stringName); - Task> GetDict(ICommandContext context, string command); - Task GetGuildContext(ICommandContext context); - Task SetLanguage(ulong guildId, string language); - List SupportedLanguages { get; } - string GetServerLanguage(ulong guildId); - } -} \ No newline at end of file diff --git a/src/Core/Localization/TranslationGuildContext.cs b/src/Core/Localization/TranslationGuildContext.cs deleted file mode 100644 index 25685da..0000000 --- a/src/Core/Localization/TranslationGuildContext.cs +++ /dev/null @@ -1,90 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading.Tasks; - -namespace Geekbot.Core.Localization -{ - public class TranslationGuildContext - { - public ITranslationHandler TranslationHandler { get; } - public string Language { get; } - public Dictionary Dict { get; } - - public TranslationGuildContext(ITranslationHandler translationHandler, string language, Dictionary dict) - { - TranslationHandler = translationHandler; - Language = language; - Dict = dict; - } - - public string GetString(string stringToFormat, params object[] args) - { - return string.Format(Dict[stringToFormat] ?? "", args); - } - - public string FormatDateTimeAsRemaining(DateTimeOffset dateTime) - { - var remaining = dateTime - DateTimeOffset.Now; - const string formattable = "{0} {1}"; - var sb = new StringBuilder(); - - if (remaining.Days > 0) - { - var s = GetTimeString(TimeTypes.Days); - sb.AppendFormat(formattable, remaining.Days, GetSingOrPlur(remaining.Days, s)); - } - - if (remaining.Hours > 0) - { - if (sb.Length > 0) sb.Append(", "); - var s = GetTimeString(TimeTypes.Hours); - sb.AppendFormat(formattable, remaining.Hours, GetSingOrPlur(remaining.Hours, s)); - } - - if (remaining.Minutes > 0) - { - if (sb.Length > 0) sb.Append(", "); - var s = GetTimeString(TimeTypes.Minutes); - sb.AppendFormat(formattable, remaining.Minutes, GetSingOrPlur(remaining.Minutes, s)); - } - - if (remaining.Seconds > 0) - { - if (sb.Length > 0) - { - var and = TranslationHandler.GetString(Language, "dateTime", "And"); - sb.AppendFormat(" {0} ", and); - } - var s = GetTimeString(TimeTypes.Seconds); - sb.AppendFormat(formattable, remaining.Seconds, GetSingOrPlur(remaining.Seconds, s)); - } - - return sb.ToString().Trim(); - } - - public Task SetLanguage(ulong guildId, string language) - { - return TranslationHandler.SetLanguage(guildId, language); - } - - private string GetTimeString(TimeTypes type) - { - return TranslationHandler.GetString(Language, "dateTime", type.ToString()); - } - - private string GetSingOrPlur(int number, string rawString) - { - var versions = rawString.Split('|'); - return number == 1 ? versions[0] : versions[1]; - } - - private enum TimeTypes - { - Days, - Hours, - Minutes, - Seconds - } - } -} \ No newline at end of file diff --git a/src/Core/Localization/TranslationHandler.cs b/src/Core/Localization/TranslationHandler.cs deleted file mode 100644 index 1a46ada..0000000 --- a/src/Core/Localization/TranslationHandler.cs +++ /dev/null @@ -1,194 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading.Tasks; -using Discord.Commands; -using Geekbot.Core.GuildSettingsManager; -using Geekbot.Core.Logger; -using YamlDotNet.Core; -using YamlDotNet.Serialization; - -namespace Geekbot.Core.Localization -{ - public class TranslationHandler : ITranslationHandler - { - private readonly IGeekbotLogger _logger; - private readonly IGuildSettingsManager _guildSettingsManager; - private readonly Dictionary _serverLanguages; - private Dictionary>> _translations; - - public TranslationHandler(IGeekbotLogger logger, IGuildSettingsManager guildSettingsManager) - { - _logger = logger; - _guildSettingsManager = guildSettingsManager; - _logger.Information(LogSource.Geekbot, "Loading Translations"); - LoadTranslations(); - _serverLanguages = new Dictionary(); - } - - private void LoadTranslations() - { - try - { - // Read the file - var translationFile = File.ReadAllText(Path.GetFullPath("./Localization/Translations.yml")); - - // Deserialize - var input = new StringReader(translationFile); - var mergingParser = new MergingParser(new Parser(input)); - var deserializer = new DeserializerBuilder().Build(); - var rawTranslations = deserializer.Deserialize>>>(mergingParser); - - // Sort - var sortedPerLanguage = new Dictionary>>(); - foreach (var command in rawTranslations) - { - foreach (var str in command.Value) - { - foreach (var lang in str.Value) - { - if (!sortedPerLanguage.ContainsKey(lang.Key)) - { - var commandDict = new Dictionary>(); - var strDict = new Dictionary - { - {str.Key, lang.Value} - }; - commandDict.Add(command.Key, strDict); - sortedPerLanguage.Add(lang.Key, commandDict); - } - if (!sortedPerLanguage[lang.Key].ContainsKey(command.Key)) - { - var strDict = new Dictionary - { - {str.Key, lang.Value} - }; - sortedPerLanguage[lang.Key].Add(command.Key, strDict); - } - if (!sortedPerLanguage[lang.Key][command.Key].ContainsKey(str.Key)) - { - sortedPerLanguage[lang.Key][command.Key].Add(str.Key, lang.Value); - } - } - } - } - _translations = sortedPerLanguage; - - // Find Languages - SupportedLanguages = new List(); - foreach (var lang in sortedPerLanguage) - { - SupportedLanguages.Add(lang.Key); - } - } - catch (Exception e) - { - _logger.Error(LogSource.Geekbot, "Failed to load Translations", e); - Environment.Exit(GeekbotExitCode.TranslationsFailed.GetHashCode()); - } - } - - public string GetServerLanguage(ulong guildId) - { - try - { - string lang; - try - { - lang = _serverLanguages[guildId]; - if (!string.IsNullOrEmpty(lang)) - { - return lang; - } - throw new Exception(); - } - catch - { - lang = _guildSettingsManager.GetSettings(guildId, false)?.Language ?? "EN"; - _serverLanguages[guildId] = lang; - return lang; - } - } - catch (Exception e) - { - _logger.Error(LogSource.Geekbot, "Could not get guild language", e); - return "EN"; - } - } - - public async Task GetString(ulong guildId, string command, string stringName) - { - var serverLang = GetServerLanguage(guildId); - return GetString(serverLang, command, stringName); - } - - public string GetString(string language, string command, string stringName) - { - var translation = _translations[language][command][stringName]; - if (!string.IsNullOrWhiteSpace(translation)) return translation; - translation = _translations[command][stringName]["EN"]; - if (string.IsNullOrWhiteSpace(translation)) - { - _logger.Warning(LogSource.Geekbot, $"No translation found for {command} - {stringName}"); - } - return translation; - } - - private async Task> GetDict(ICommandContext context) - { - try - { - var command = context.Message.Content.Split(' ').First().TrimStart('!').ToLower(); - var serverLanguage = GetServerLanguage(context.Guild?.Id ?? 0); - return _translations[serverLanguage][command]; - } - catch (Exception e) - { - _logger.Error(LogSource.Geekbot, "No translations for command found", e); - return new Dictionary(); - } - } - - public async Task GetGuildContext(ICommandContext context) - { - var dict = await GetDict(context); - var language = GetServerLanguage(context.Guild?.Id ?? 0); - return new TranslationGuildContext(this, language, dict); - } - - public async Task> GetDict(ICommandContext context, string command) - { - try - { - var serverLanguage = GetServerLanguage(context.Guild?.Id ?? 0); - return _translations[serverLanguage][command]; - } - catch (Exception e) - { - _logger.Error(LogSource.Geekbot, "No translations for command found", e); - return new Dictionary(); - } - } - - public async Task SetLanguage(ulong guildId, string language) - { - try - { - if (!SupportedLanguages.Contains(language)) return false; - var guild = _guildSettingsManager.GetSettings(guildId); - guild.Language = language; - await _guildSettingsManager.UpdateSettings(guild); - _serverLanguages[guildId] = language; - return true; - } - catch (Exception e) - { - _logger.Error(LogSource.Geekbot, "Error while changing language", e); - return false; - } - } - - public List SupportedLanguages { get; private set; } - } -} \ No newline at end of file diff --git a/src/Core/Localization/Translations.yml b/src/Core/Localization/Translations.yml deleted file mode 100644 index dc18c2b..0000000 --- a/src/Core/Localization/Translations.yml +++ /dev/null @@ -1,25 +0,0 @@ ---- -dateTime: - Days: - EN: "day|days" - CHDE: "tag|täg" - Hours: - EN: "hour|hours" - CHDE: "stund|stunde" - Minutes: - EN: "minute|minutes" - CHDE: "minute|minute" - Seconds: - EN: "second|seconds" - CHDE: "sekunde|sekunde" - And: - EN: "and" - CHDE: "und" -errorHandler: - SomethingWentWrong: - EN: "Something went wrong :confused:" - CHDE: "Öppis isch schief gange :confused:" -httpErrors: - 403: - EN: "Seems like i don't have enough permission to that :confused:" - CHDE: "Gseht danach us das ich nid gnueg recht han zum das mache :confused:" \ No newline at end of file diff --git a/tests/Core/Localization/TranslationGuildContext.test.cs b/tests/Core/Localization/TranslationGuildContext.test.cs deleted file mode 100644 index 4aaca16..0000000 --- a/tests/Core/Localization/TranslationGuildContext.test.cs +++ /dev/null @@ -1,71 +0,0 @@ -using System; -using System.Collections.Generic; -using Geekbot.Core.Localization; -using Moq; -using Xunit; - -namespace Tests.Core.Localization -{ - public class TranslationGuildContext_test - { - public class FormatDateTimeAsRemainingTestDto - { - public DateTimeOffset DateTime { get; set; } - public string Expected { get; set; } - } - - public static TestData FormatDateTimeAsRemainingData => - new TestData - { - { - "Wait for days", - new FormatDateTimeAsRemainingTestDto - { - DateTime = DateTimeOffset.Now.AddDays(5), - Expected = "4 days, 23 hours, 59 minutes and 59 seconds" - } - }, - { - "Wait for minutes", - new FormatDateTimeAsRemainingTestDto - { - DateTime = DateTimeOffset.Now.AddMinutes(5), - Expected = "4 minutes and 59 seconds" - } - }, - { - "Wait for seconds", - new FormatDateTimeAsRemainingTestDto - { - DateTime = DateTimeOffset.Now.AddSeconds(5), - Expected = "4 seconds" - } - } - }; - - [Theory, MemberData(nameof(FormatDateTimeAsRemainingData))] - public void FormatDateTimeAsRemaining(string testName, FormatDateTimeAsRemainingTestDto testData) - { - var translationHandlerMock = new Mock(MockBehavior.Loose); - translationHandlerMock - .Setup(thm => thm.GetString("EN", "dateTime", "Days")) - .Returns("day|days"); - translationHandlerMock - .Setup(thm => thm.GetString("EN", "dateTime", "Hours")) - .Returns("hour|hours"); - translationHandlerMock - .Setup(thm => thm.GetString("EN", "dateTime", "Minutes")) - .Returns("minute|minutes"); - translationHandlerMock - .Setup(thm => thm.GetString("EN", "dateTime", "Seconds")) - .Returns("second|seconds"); - translationHandlerMock - .Setup(thm => thm.GetString("EN", "dateTime", "And")) - .Returns("and"); - - var context = new TranslationGuildContext(translationHandlerMock.Object, "EN", new Dictionary()); - var result = context.FormatDateTimeAsRemaining(testData.DateTime); - Assert.Equal(result, testData.Expected); - } - } -} \ No newline at end of file diff --git a/tests/Core/Localization/Translations.test.cs b/tests/Core/Localization/Translations.test.cs deleted file mode 100644 index d8a0879..0000000 --- a/tests/Core/Localization/Translations.test.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System.Collections.Generic; -using System.IO; -using System.Linq; -using FluentAssertions; -using Xunit; -using YamlDotNet.Core; -using YamlDotNet.Serialization; - -namespace Tests.Core.Localization -{ - public class Translations_test - { - [Fact] - public void TranslationsYamlIsValid() - { - // Read the file - var translationFile = File.ReadAllText(Path.GetFullPath("./../../../../src/Core/Localization/Translations.yml")); - - // Deserialize - var input = new StringReader(translationFile); - var mergingParser = new MergingParser(new Parser(input)); - var deserializer = new DeserializerBuilder().Build(); - var rawTranslations = deserializer.Deserialize>>>(mergingParser); - - // These languages must be supported - var supportedLanguages = new List - { - "EN", - "CHDE" - }; - - // Iterate every single key to make sure it's populated - foreach (var command in rawTranslations) - { - foreach (var str in command.Value) - { - str.Value.Select(e => e.Key).ToList().Should().BeEquivalentTo(supportedLanguages, str.Key); - foreach (var lang in str.Value) - { - lang.Value.Should().NotBeNullOrEmpty($"{command.Key} / {str.Key} / {lang.Key}"); - } - } - } - } - } -} \ No newline at end of file From 60e36daaec4a587e48d716e307c9652d7b064723 Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 14 Aug 2020 23:34:02 +0200 Subject: [PATCH 273/443] Translate !stats --- src/Bot/Bot.csproj | 9 ++ src/Bot/Commands/User/Stats.cs | 26 ++--- src/Bot/Localization/Stats.Designer.cs | 135 +++++++++++++++++++++++++ src/Bot/Localization/Stats.de-ch.resx | 38 +++++++ src/Bot/Localization/Stats.resx | 45 +++++++++ 5 files changed, 240 insertions(+), 13 deletions(-) create mode 100644 src/Bot/Localization/Stats.Designer.cs create mode 100644 src/Bot/Localization/Stats.de-ch.resx create mode 100644 src/Bot/Localization/Stats.resx diff --git a/src/Bot/Bot.csproj b/src/Bot/Bot.csproj index 2d3cd2f..49f0b16 100644 --- a/src/Bot/Bot.csproj +++ b/src/Bot/Bot.csproj @@ -81,6 +81,10 @@ ResXFileCodeGenerator Role.Designer.cs + + ResXFileCodeGenerator + Stats.Designer.cs + @@ -136,5 +140,10 @@ True Role.resx + + True + True + Stats.resx + diff --git a/src/Bot/Commands/User/Stats.cs b/src/Bot/Commands/User/Stats.cs index 70f6247..a2a54a2 100644 --- a/src/Bot/Commands/User/Stats.cs +++ b/src/Bot/Commands/User/Stats.cs @@ -3,24 +3,24 @@ using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; +using Geekbot.Core; using Geekbot.Core.CommandPreconditions; using Geekbot.Core.Database; using Geekbot.Core.ErrorHandling; using Geekbot.Core.Extensions; +using Geekbot.Core.GuildSettingsManager; using Geekbot.Core.Levels; namespace Geekbot.Bot.Commands.User { - public class Stats : ModuleBase + public class Stats : GeekbotCommandBase { - private readonly IErrorHandler _errorHandler; private readonly ILevelCalc _levelCalc; private readonly DatabaseContext _database; - public Stats(DatabaseContext database, IErrorHandler errorHandler, ILevelCalc levelCalc) + public Stats(DatabaseContext database, IErrorHandler errorHandler, ILevelCalc levelCalc, IGuildSettingsManager guildSettingsManager) : base(errorHandler, guildSettingsManager) { _database = database; - _errorHandler = errorHandler; _levelCalc = levelCalc; } @@ -67,23 +67,23 @@ namespace Geekbot.Bot.Commands.User e.GuildId.Equals(Context.Guild.Id.AsLong()) && e.UserId.Equals(userInfo.Id.AsLong())); - eb.AddInlineField("Discordian Since", + eb.AddInlineField(Localization.Stats.OnDiscordSince, $"{createdAt.Day}.{createdAt.Month}.{createdAt.Year} ({age} days)") - .AddInlineField("Joined Server", + .AddInlineField(Localization.Stats.JoinedServer, $"{joinedAt.Day}.{joinedAt.Month}.{joinedAt.Year} ({joinedDayAgo} days)") - .AddInlineField("Karma", karma?.Karma ?? 0) - .AddInlineField("Level", level) - .AddInlineField("Messages Sent", messages) - .AddInlineField("Server Total", $"{percent}%"); + .AddInlineField(Localization.Stats.Karma, karma?.Karma ?? 0) + .AddInlineField(Localization.Stats.Level, level) + .AddInlineField(Localization.Stats.MessagesSent, messages) + .AddInlineField(Localization.Stats.ServerTotal, $"{percent}%"); - if (correctRolls != null) eb.AddInlineField("Guessed Rolls", correctRolls.Rolls); - if (cookies > 0) eb.AddInlineField("Cookies", cookies); + if (correctRolls != null) eb.AddInlineField(Localization.Stats.GuessedRolls, correctRolls.Rolls); + if (cookies > 0) eb.AddInlineField(Localization.Stats.Cookies, cookies); await ReplyAsync("", false, eb.Build()); } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } } diff --git a/src/Bot/Localization/Stats.Designer.cs b/src/Bot/Localization/Stats.Designer.cs new file mode 100644 index 0000000..d05f937 --- /dev/null +++ b/src/Bot/Localization/Stats.Designer.cs @@ -0,0 +1,135 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Geekbot.Bot.Localization { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Stats { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Stats() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Geekbot.Bot.Localization.Stats", typeof(Stats).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to Cookies. + /// + internal static string Cookies { + get { + return ResourceManager.GetString("Cookies", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Guessed Rolls. + /// + internal static string GuessedRolls { + get { + return ResourceManager.GetString("GuessedRolls", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Joined Server. + /// + internal static string JoinedServer { + get { + return ResourceManager.GetString("JoinedServer", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Karma. + /// + internal static string Karma { + get { + return ResourceManager.GetString("Karma", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Level. + /// + internal static string Level { + get { + return ResourceManager.GetString("Level", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Messages Sent. + /// + internal static string MessagesSent { + get { + return ResourceManager.GetString("MessagesSent", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to On Discord Since. + /// + internal static string OnDiscordSince { + get { + return ResourceManager.GetString("OnDiscordSince", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Server Total. + /// + internal static string ServerTotal { + get { + return ResourceManager.GetString("ServerTotal", resourceCulture); + } + } + } +} diff --git a/src/Bot/Localization/Stats.de-ch.resx b/src/Bot/Localization/Stats.de-ch.resx new file mode 100644 index 0000000..ab44a2e --- /dev/null +++ b/src/Bot/Localization/Stats.de-ch.resx @@ -0,0 +1,38 @@ + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Server Total + + + Uf Discord siit + + + Nachrichte versendet + + + Level + + + Karma + + + Server Bitrette + + + Grateni Rolls + + + Guetzli + + \ No newline at end of file diff --git a/src/Bot/Localization/Stats.resx b/src/Bot/Localization/Stats.resx new file mode 100644 index 0000000..3b8303a --- /dev/null +++ b/src/Bot/Localization/Stats.resx @@ -0,0 +1,45 @@ + + + + + + + + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + On Discord Since + + + Joined Server + + + Karma + + + Level + + + Messages Sent + + + Server Total + + + Guessed Rolls + + + Cookies + + \ No newline at end of file From a78e92d230acbb90322bc572a603ce57f122d79d Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 15 Aug 2020 00:40:59 +0200 Subject: [PATCH 274/443] Remove YamlDotNet --- src/Core/Core.csproj | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index fd44a95..7f278cf 100644 --- a/src/Core/Core.csproj +++ b/src/Core/Core.csproj @@ -28,7 +28,6 @@ - From 3c4a5c638b887833c6414170c0e2455ea365d838 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 26 Aug 2020 23:15:57 +0200 Subject: [PATCH 275/443] Upgrade Microsoft.Extensions.* to .NET5 preview 8 --- src/Core/Core.csproj | 6 +++--- tests/Tests.csproj | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index 7f278cf..28729a7 100644 --- a/src/Core/Core.csproj +++ b/src/Core/Core.csproj @@ -18,9 +18,9 @@ - - - + + + diff --git a/tests/Tests.csproj b/tests/Tests.csproj index b8d1fc4..75a5f9c 100644 --- a/tests/Tests.csproj +++ b/tests/Tests.csproj @@ -12,7 +12,6 @@ - From 546b5450e7d10b325461bf7cfea1d750de5c2270 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 26 Aug 2020 23:28:03 +0200 Subject: [PATCH 276/443] Deal with MTG Gatherer downtime --- src/Bot/Commands/Integrations/MagicTheGathering.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Bot/Commands/Integrations/MagicTheGathering.cs b/src/Bot/Commands/Integrations/MagicTheGathering.cs index 66a87fe..b179d6e 100644 --- a/src/Bot/Commands/Integrations/MagicTheGathering.cs +++ b/src/Bot/Commands/Integrations/MagicTheGathering.cs @@ -28,7 +28,7 @@ namespace Geekbot.Bot.Commands.Integrations { try { - var message = await Context.Channel.SendMessageAsync($":mag: Looking up\"{cardName}\", please wait..."); + var message = await Context.Channel.SendMessageAsync($":mag: Looking up \"{cardName}\", please wait..."); var service = new CardService(); var result = service @@ -36,7 +36,15 @@ namespace Geekbot.Bot.Commands.Integrations // fewer cards less risk of deserialization problems, don't need more than one anyways... .Where(x => x.PageSize, 1); - var card = result.All().Value.FirstOrDefault(); + var cards = await result.AllAsync(); + if (!cards.IsSuccess) + { + await message.ModifyAsync(properties => properties.Content = $":warning: The Gatherer reacted in an unexpected way: {cards.Exception.Message}"); + return; + } + + var card = cards.Value.FirstOrDefault(); + if (card == null) { await message.ModifyAsync(properties => properties.Content = ":red_circle: I couldn't find a card with that name..."); From 8246c7a862128f7161aa798eac26fe6716db9ae8 Mon Sep 17 00:00:00 2001 From: runebaas Date: Mon, 31 Aug 2020 18:46:00 +0200 Subject: [PATCH 277/443] When json logging is enabled, log it to the console without color or additional timestamp, also log the messages with the correct log level. Remove the logs folder. --- src/Bot/Logs/.keep | 0 src/Core/Logger/GeekbotLogger.cs | 49 ++++++++++---------------------- src/Core/Logger/LoggerFactory.cs | 44 ++++++++++++++-------------- 3 files changed, 36 insertions(+), 57 deletions(-) delete mode 100644 src/Bot/Logs/.keep diff --git a/src/Bot/Logs/.keep b/src/Bot/Logs/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/src/Core/Logger/GeekbotLogger.cs b/src/Core/Logger/GeekbotLogger.cs index 8d4a8eb..652faef 100644 --- a/src/Core/Logger/GeekbotLogger.cs +++ b/src/Core/Logger/GeekbotLogger.cs @@ -20,44 +20,25 @@ namespace Geekbot.Core.Logger }; Information(LogSource.Geekbot, "Using GeekbotLogger"); } - + public void Trace(LogSource source, string message, object extra = null) - { - _logger.Trace(CreateLogString("Trace", source, message, null, extra)); - } - + => _logger.Trace(CreateLogString("Trace", source, message, null, extra)); + public void Debug(LogSource source, string message, object extra = null) - { - if (_logAsJson) _logger.Info(CreateLogString("Debug", source, message, null, extra)); - else _logger.Debug(CreateLogString("Debug", source, message, null, extra)); - } - + => _logger.Debug(CreateLogString("Debug", source, message, null, extra)); + public void Information(LogSource source, string message, object extra = null) - { - _logger.Info(CreateLogString("Information", source, message, null, extra)); - } - + => _logger.Info(CreateLogString("Information", source, message, null, extra)); + public void Warning(LogSource source, string message, Exception stackTrace = null, object extra = null) - { - if (_logAsJson) _logger.Info(CreateLogString("Warning", source, message, stackTrace, extra)); - else _logger.Warn(CreateLogString("Warning", source, message, stackTrace, extra)); - } - + => _logger.Warn(CreateLogString("Warning", source, message, stackTrace, extra)); + public void Error(LogSource source, string message, Exception stackTrace, object extra = null) - { - if (_logAsJson) _logger.Info(CreateLogString("Error", source, message, stackTrace, extra)); - else _logger.Error(stackTrace, CreateLogString("Error", source, message, stackTrace, extra)); - } + => _logger.Error(stackTrace, CreateLogString("Error", source, message, stackTrace, extra)); - public NLog.Logger GetNLogger() - { - return _logger; - } + public NLog.Logger GetNLogger() => _logger; - public bool LogAsJson() - { - return _logAsJson; - } + public bool LogAsJson() => _logAsJson; private string CreateLogString(string type, LogSource source, string message, Exception stackTrace = null, object extra = null) { @@ -74,10 +55,10 @@ namespace Geekbot.Core.Logger }; return JsonConvert.SerializeObject(logObject, Formatting.None, _serializerSettings); } - + if (source != LogSource.Message) return $"[{source}] - {message}"; - - var m = (MessageDto) extra; + + var m = (MessageDto) extra; return $"[{source}] - [{m?.Guild.Name} - {m?.Channel.Name}] {m?.User.Name}: {m?.Message.Content}"; } } diff --git a/src/Core/Logger/LoggerFactory.cs b/src/Core/Logger/LoggerFactory.cs index bc129fc..bf3d926 100644 --- a/src/Core/Logger/LoggerFactory.cs +++ b/src/Core/Logger/LoggerFactory.cs @@ -12,12 +12,13 @@ namespace Geekbot.Core.Logger public static NLog.Logger CreateNLog(RunParameters runParameters) { var config = new LoggingConfiguration(); + var minLevel = runParameters.Verbose ? LogLevel.Trace : LogLevel.Info; if (!string.IsNullOrEmpty(runParameters.SumologicEndpoint)) { Console.WriteLine("Logging Geekbot Logs to Sumologic"); config.LoggingRules.Add( - new LoggingRule("*", LogLevel.Debug, LogLevel.Fatal, + new LoggingRule("*", minLevel, LogLevel.Fatal, new SumoLogicTarget() { Url = runParameters.SumologicEndpoint, @@ -27,11 +28,23 @@ namespace Geekbot.Core.Logger OptimizeBufferReuse = true, Name = "Geekbot" }) - ); + ); + } + else if (runParameters.LogJson) + { + config.LoggingRules.Add( + new LoggingRule("*", minLevel, LogLevel.Fatal, + new ConsoleTarget + { + Name = "Console", + Encoding = Encoding.UTF8, + Layout = "${message}" + } + ) + ); } else { - var minLevel = runParameters.Verbose ? LogLevel.Trace : LogLevel.Info; config.LoggingRules.Add( new LoggingRule("*", minLevel, LogLevel.Fatal, new ColoredConsoleTarget @@ -39,27 +52,12 @@ namespace Geekbot.Core.Logger Name = "Console", Encoding = Encoding.UTF8, Layout = "[${longdate} ${level:format=FirstCharacter}] ${message} ${exception:format=toString}" - }) - ); - - config.LoggingRules.Add( - new LoggingRule("*", minLevel, LogLevel.Fatal, - new FileTarget - { - Name = "File", - Layout = "[${longdate} ${level}] ${message}", - Encoding = Encoding.UTF8, - LineEnding = LineEndingMode.Default, - MaxArchiveFiles = 30, - ArchiveNumbering = ArchiveNumberingMode.Date, - ArchiveEvery = FileArchivePeriod.Day, - ArchiveFileName = "./Logs/Archive/{#####}.log", - FileName = "./Logs/Geekbot.log" - }) - ); + } + ) + ); } - - var loggerConfig = new LogFactory { Configuration = config }; + + var loggerConfig = new LogFactory {Configuration = config}; return loggerConfig.GetCurrentClassLogger(); } } From ce1153a0e2c843b91d5ed716a4ee460554449023 Mon Sep 17 00:00:00 2001 From: runebaas Date: Mon, 31 Aug 2020 19:29:03 +0200 Subject: [PATCH 278/443] Upgrade Entity Framework to preview 8 --- src/Core/Core.csproj | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index 28729a7..5ad67b3 100644 --- a/src/Core/Core.csproj +++ b/src/Core/Core.csproj @@ -13,11 +13,11 @@ - - - - - + + + + + @@ -25,7 +25,7 @@ - + From 81373b76144f9acf231c76e75c564d19a86d12c1 Mon Sep 17 00:00:00 2001 From: runebaas Date: Mon, 31 Aug 2020 21:50:01 +0200 Subject: [PATCH 279/443] Make the ErrorHandler more resilient in case an exception is thrown during the error handling --- src/Core/ErrorHandling/ErrorHandler.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Core/ErrorHandling/ErrorHandler.cs b/src/Core/ErrorHandling/ErrorHandler.cs index 47ed22d..60d97e9 100644 --- a/src/Core/ErrorHandling/ErrorHandler.cs +++ b/src/Core/ErrorHandling/ErrorHandler.cs @@ -70,8 +70,14 @@ namespace Geekbot.Core.ErrorHandling } catch (Exception ex) { - await context.Channel.SendMessageAsync("Something went really really wrong here"); - _logger.Error(LogSource.Geekbot, "Errorception", ex); + try + { + await context.Channel.SendMessageAsync("Something went really really wrong here"); + } + finally + { + _logger.Error(LogSource.Geekbot, "Errorception", ex); + } } } From f71349b3780145ded0f685eaa5efe74ea34bac2a Mon Sep 17 00:00:00 2001 From: runebaas Date: Tue, 8 Sep 2020 20:20:09 +0200 Subject: [PATCH 280/443] Reduce logging by making the user repo update message a debug log message --- src/Core/UserRepository/UserRepository.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core/UserRepository/UserRepository.cs b/src/Core/UserRepository/UserRepository.cs index 94d5df4..31fe2e2 100644 --- a/src/Core/UserRepository/UserRepository.cs +++ b/src/Core/UserRepository/UserRepository.cs @@ -48,7 +48,7 @@ namespace Geekbot.Core.UserRepository await _database.SaveChangesAsync(); - _logger.Information(LogSource.UserRepository, "Updated User", savedUser); + _logger.Debug(LogSource.UserRepository, "Updated User", savedUser); await Task.Delay(500); return true; } From d3c284102b5c674e5eb5def62fe08c6d250f126e Mon Sep 17 00:00:00 2001 From: runebaas Date: Tue, 15 Sep 2020 00:14:27 +0200 Subject: [PATCH 281/443] Add an api endpoint to shutdown the bot from the browser --- src/Core/GeekbotExitCode.cs | 1 + src/Web/Controllers/KillController.cs | 64 +++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 src/Web/Controllers/KillController.cs diff --git a/src/Core/GeekbotExitCode.cs b/src/Core/GeekbotExitCode.cs index 5598f48..51006e1 100644 --- a/src/Core/GeekbotExitCode.cs +++ b/src/Core/GeekbotExitCode.cs @@ -8,6 +8,7 @@ // Geekbot Internals TranslationsFailed = 201, + KilledByApiCall = 210, // Dependent Services /* 301 not in use anymore (redis) */ diff --git a/src/Web/Controllers/KillController.cs b/src/Web/Controllers/KillController.cs new file mode 100644 index 0000000..2492b0d --- /dev/null +++ b/src/Web/Controllers/KillController.cs @@ -0,0 +1,64 @@ +using System; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using Geekbot.Core; +using Geekbot.Core.GlobalSettings; +using Microsoft.AspNetCore.Mvc; + +namespace Geekbot.Web.Controllers +{ + /* + * Sometimes for some unknown reason command responses may become incredibly slow. + * Because i don't have time to debug it and may not always know directly when it happens, + * i want to give some trusted people the possibility to restart the bot. + * The kill code must be set manually in the db, it should end it `---1' + * + * ToDo: Actually fix the underlying issue + */ + public class KillController : Controller + { + private readonly IGlobalSettings _globalSettings; + private const string KillCodeDbKey = "KillCode"; + + public KillController(IGlobalSettings globalSettings) + { + _globalSettings = globalSettings; + } + + [HttpGet("/v1/kill/{providedKillCode}")] + public async Task KillTheBot([FromRoute] string providedKillCode) + { + var killCode = _globalSettings.GetKey(KillCodeDbKey); + if (providedKillCode != killCode) + { + return Unauthorized(new ApiError { Message = $"Go Away ({GetKillCodeInt(killCode)})" }); + } + + await UpdateKillCode(killCode); +#pragma warning disable CS4014 + Kill(); +#pragma warning restore CS4014 + return Ok(); + } + + private int GetKillCodeInt(string code) + { + var group = Regex.Match(code, @".+(\-\-\-(?\d+))").Groups["int"]; + return group.Success ? int.Parse(group.Value) : 0; + } + + private async Task UpdateKillCode(string oldCode) + { + var newKillCodeInt = new Random().Next(1, 100); + var newCode = oldCode.Replace($"---{GetKillCodeInt(oldCode)}", $"---{newKillCodeInt}"); + await _globalSettings.SetKey(KillCodeDbKey, newCode); + } + + private async Task Kill() + { + // wait a second so the http response can still be sent successfully + await Task.Delay(1000); + Environment.Exit(GeekbotExitCode.KilledByApiCall.GetHashCode()); + } + } +} \ No newline at end of file From cbe2aa23c955d01dcf31455a45cf448f922d95dc Mon Sep 17 00:00:00 2001 From: runebaas Date: Tue, 15 Sep 2020 23:25:40 +0200 Subject: [PATCH 282/443] Make sure that examples in the !urban command are not longer than 1024 characters. --- .../Integrations/UbranDictionary/UrbanDictionary.cs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/Bot/Commands/Integrations/UbranDictionary/UrbanDictionary.cs b/src/Bot/Commands/Integrations/UbranDictionary/UrbanDictionary.cs index bc1f478..72bc8cd 100644 --- a/src/Bot/Commands/Integrations/UbranDictionary/UrbanDictionary.cs +++ b/src/Bot/Commands/Integrations/UbranDictionary/UrbanDictionary.cs @@ -33,12 +33,6 @@ namespace Geekbot.Bot.Commands.Integrations.UbranDictionary var definition = definitions.List.First(e => !string.IsNullOrWhiteSpace(e.Example)); - var description = definition.Definition; - if (description.Length > 1801) - { - description = description.Substring(0, 1800) + " [...]"; - } - var eb = new EmbedBuilder(); eb.WithAuthor(new EmbedAuthorBuilder { @@ -46,8 +40,11 @@ namespace Geekbot.Bot.Commands.Integrations.UbranDictionary Url = definition.Permalink }); eb.WithColor(new Color(239, 255, 0)); - if (!string.IsNullOrEmpty(definition.Definition)) eb.Description = description; - if (!string.IsNullOrEmpty(definition.Example)) eb.AddField("Example", definition.Example ?? "(no example given...)"); + + static string ShortenIfToLong(string str, int maxLength) => str.Length > maxLength ? $"{str.Substring(0, maxLength - 5)}[...]" : str; + + if (!string.IsNullOrEmpty(definition.Definition)) eb.Description = ShortenIfToLong(definition.Definition, 1800); + if (!string.IsNullOrEmpty(definition.Example)) eb.AddField("Example", ShortenIfToLong(definition.Example, 1024)); if (!string.IsNullOrEmpty(definition.ThumbsUp)) eb.AddInlineField("Upvotes", definition.ThumbsUp); if (!string.IsNullOrEmpty(definition.ThumbsDown)) eb.AddInlineField("Downvotes", definition.ThumbsDown); if (definitions.Tags?.Length > 0) eb.AddField("Tags", string.Join(", ", definitions.Tags)); From 482a74839ac6edb07bbcd784df46e2f58755ace8 Mon Sep 17 00:00:00 2001 From: runebaas Date: Tue, 22 Sep 2020 12:24:12 +0200 Subject: [PATCH 283/443] Fix some swiss german grammar (never heard of it either) in the !quote command --- src/Bot/Localization/Karma.de-ch.resx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bot/Localization/Karma.de-ch.resx b/src/Bot/Localization/Karma.de-ch.resx index 294c105..5605f8b 100644 --- a/src/Bot/Localization/Karma.de-ch.resx +++ b/src/Bot/Localization/Karma.de-ch.resx @@ -30,7 +30,7 @@ Jetzt - Sorry {0}, aber du chasch dr din eigete karma nid weg neh + Sorry {0}, aber du chasch dis eigete karma nid senke Karma gsenkt From b743539c749dd19fe74fa6d6633cd2f7a3be9006 Mon Sep 17 00:00:00 2001 From: runebaas Date: Tue, 22 Sep 2020 13:06:57 +0200 Subject: [PATCH 284/443] Upgrade to .net5 rc1 and fix all breaking changes in the web api since .net core 2.2 --- .gitlab-ci.yml | 2 +- Dockerfile | 2 +- src/Bot/Bot.csproj | 1 - src/Core/Core.csproj | 18 +++++++++--------- src/Web/Web.csproj | 11 +---------- src/Web/WebApiStartup.cs | 22 +++++++++++++++------- 6 files changed, 27 insertions(+), 29 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 8efd1d1..c4d8b15 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -10,7 +10,7 @@ variables: Build: stage: build - image: mcr.microsoft.com/dotnet/core/sdk:5.0-focal + image: mcr.microsoft.com/dotnet/sdk:5.0 artifacts: expire_in: 1h paths: diff --git a/Dockerfile b/Dockerfile index 479dd40..245e06b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM mcr.microsoft.com/dotnet/core/aspnet:5.0-focal +FROM mcr.microsoft.com/dotnet/aspnet:5.0 COPY ./app /app/ diff --git a/src/Bot/Bot.csproj b/src/Bot/Bot.csproj index 49f0b16..60f6473 100644 --- a/src/Bot/Bot.csproj +++ b/src/Bot/Bot.csproj @@ -22,7 +22,6 @@ - diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index 5ad67b3..7b273fd 100644 --- a/src/Core/Core.csproj +++ b/src/Core/Core.csproj @@ -13,19 +13,19 @@ - - - - - - - - + + + + + + + + - + diff --git a/src/Web/Web.csproj b/src/Web/Web.csproj index 429165b..5080a3d 100644 --- a/src/Web/Web.csproj +++ b/src/Web/Web.csproj @@ -1,4 +1,4 @@ - + net5.0 @@ -10,15 +10,6 @@ NU1701 - - - - - - - - - diff --git a/src/Web/WebApiStartup.cs b/src/Web/WebApiStartup.cs index 9aeedf9..0c18886 100644 --- a/src/Web/WebApiStartup.cs +++ b/src/Web/WebApiStartup.cs @@ -18,6 +18,9 @@ namespace Geekbot.Web { public static class WebApiStartup { + // Using the "Microsoft.NET.Sdk.Web" SDK requires a static main function... + public static void Main() {} + public static void StartWebApi(IGeekbotLogger logger, RunParameters runParameters, CommandService commandService, DatabaseContext databaseContext, DiscordSocketClient client, IGlobalSettings globalSettings, IHighscoreManager highscoreManager) { @@ -28,22 +31,27 @@ namespace Geekbot.Web }) .ConfigureServices(services => { - services.AddMvc(); - services.AddSingleton(commandService); - services.AddSingleton(databaseContext); - services.AddSingleton(client); - services.AddSingleton(globalSettings); - services.AddSingleton(highscoreManager); + services.AddControllers(); services.AddCors(options => { options.AddPolicy("AllowSpecificOrigin", builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()); }); + + services.AddSingleton(commandService); + services.AddSingleton(databaseContext); + services.AddSingleton(client); + services.AddSingleton(globalSettings); + services.AddSingleton(highscoreManager); }) .Configure(app => { - app.UseMvc(); + app.UseRouting(); app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().Build()); + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); }) .ConfigureLogging(logging => { From ae9b9caeb90451c860e5be7fe19e51f81267c9f8 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 23 Sep 2020 16:05:43 +0200 Subject: [PATCH 285/443] Add random.org for random number generation. --- src/Bot/Commands/Utils/Quote/Quote.cs | 2 +- src/Bot/Program.cs | 2 +- src/Core/Core.csproj | 1 + .../RandomNumberGenerator.cs | 57 +++++++++++++++++-- tests/Core/DiceParser/DiceParser.test.cs | 6 +- tests/Core/DiceParser/SingleDie.test.cs | 6 +- 6 files changed, 62 insertions(+), 12 deletions(-) diff --git a/src/Bot/Commands/Utils/Quote/Quote.cs b/src/Bot/Commands/Utils/Quote/Quote.cs index a7d3ff1..6f4cd1c 100644 --- a/src/Bot/Commands/Utils/Quote/Quote.cs +++ b/src/Bot/Commands/Utils/Quote/Quote.cs @@ -46,7 +46,7 @@ namespace Geekbot.Bot.Commands.Utils.Quote return; } - var random = _randomNumberGenerator.Next(0, s.Count); + var random = _randomNumberGenerator.Next(0, s.Count - 1); var quote = s[random]; var embed = QuoteBuilder(quote); diff --git a/src/Bot/Program.cs b/src/Bot/Program.cs index 5a4b456..5994f8c 100644 --- a/src/Bot/Program.cs +++ b/src/Bot/Program.cs @@ -164,7 +164,7 @@ namespace Geekbot.Bot var emojiConverter = new EmojiConverter(); var mtgManaConverter = new MtgManaConverter(); var wikipediaClient = new WikipediaClient(); - var randomNumberGenerator = new RandomNumberGenerator(); + var randomNumberGenerator = new RandomNumberGenerator(_globalSettings); var mediaProvider = new MediaProvider(_logger, randomNumberGenerator); var kvMemoryStore = new KvInInMemoryStore(); var errorHandler = new ErrorHandler(_logger, _runParameters, () => Localization.Internal.SomethingWentWrong); diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index 7b273fd..0b3ff44 100644 --- a/src/Core/Core.csproj +++ b/src/Core/Core.csproj @@ -11,6 +11,7 @@ + diff --git a/src/Core/RandomNumberGenerator/RandomNumberGenerator.cs b/src/Core/RandomNumberGenerator/RandomNumberGenerator.cs index 63381b5..db990a7 100644 --- a/src/Core/RandomNumberGenerator/RandomNumberGenerator.cs +++ b/src/Core/RandomNumberGenerator/RandomNumberGenerator.cs @@ -1,25 +1,69 @@ using System; using System.Security.Cryptography; +using Anemonis.RandomOrg; +using Geekbot.Core.GlobalSettings; namespace Geekbot.Core.RandomNumberGenerator { public class RandomNumberGenerator : IRandomNumberGenerator { - readonly RNGCryptoServiceProvider csp; + private readonly RNGCryptoServiceProvider csp; + private readonly bool _canUseRandomOrg; + private readonly RandomOrgClient _randomOrgClient; - public RandomNumberGenerator() + public RandomNumberGenerator(IGlobalSettings globalSettings) { csp = new RNGCryptoServiceProvider(); + + var randomOrgApiKey = globalSettings.GetKey("RandomOrgApiKey"); + if (!string.IsNullOrEmpty(randomOrgApiKey)) + { + _canUseRandomOrg = true; + _randomOrgClient = new RandomOrgClient(randomOrgApiKey); + } } - public int Next(int minValue, int maxExclusiveValue) + public int Next(int minValue, int maxInclusiveValue) { - if (minValue >= maxExclusiveValue) + if (minValue == maxInclusiveValue) + { + return maxInclusiveValue; + } + + if (minValue >= maxInclusiveValue) { throw new ArgumentOutOfRangeException("minValue must be lower than maxExclusiveValue"); } + + if (_canUseRandomOrg) + { + try + { + return GetFromRandomOrg(minValue, maxInclusiveValue); + } + catch + { + // ignore + } + } - var diff = (long)maxExclusiveValue - minValue; + return GetFromCrypto(minValue, maxInclusiveValue); + } + + private int GetFromRandomOrg(int minValue, int maxInclusiveValue) + { + return _randomOrgClient + .GenerateIntegersAsync(1, minValue, maxInclusiveValue, false) + .Result + .Random + .Data[0]; + } + + private int GetFromCrypto(int minValue, int maxInclusiveValue) + { + var maxExclusiveValue = maxInclusiveValue + 1; + + var diff = (long) maxExclusiveValue - minValue; var upperBound = uint.MaxValue / diff * diff; uint ui; @@ -27,7 +71,8 @@ namespace Geekbot.Core.RandomNumberGenerator { ui = GetRandomUInt(); } while (ui >= upperBound); - return (int)(minValue + (ui % diff)); + + return (int) (minValue + (ui % diff)); } private uint GetRandomUInt() diff --git a/tests/Core/DiceParser/DiceParser.test.cs b/tests/Core/DiceParser/DiceParser.test.cs index 8a9163a..ed9425f 100644 --- a/tests/Core/DiceParser/DiceParser.test.cs +++ b/tests/Core/DiceParser/DiceParser.test.cs @@ -1,15 +1,17 @@ using System.Collections.Generic; using System.Text.Json; using Geekbot.Core.DiceParser; +using Geekbot.Core.GlobalSettings; using Geekbot.Core.RandomNumberGenerator; +using Moq; using Xunit; namespace Tests.Core.DiceParser { public class DiceParserTest { - private static readonly RandomNumberGenerator _randomNumberGenerator = new RandomNumberGenerator(); - + private static readonly RandomNumberGenerator _randomNumberGenerator = new RandomNumberGenerator(new Mock().Object); + public struct DiceParserTestDto { public string Input { get; set; } diff --git a/tests/Core/DiceParser/SingleDie.test.cs b/tests/Core/DiceParser/SingleDie.test.cs index 813a33b..5173902 100644 --- a/tests/Core/DiceParser/SingleDie.test.cs +++ b/tests/Core/DiceParser/SingleDie.test.cs @@ -1,5 +1,7 @@ using Geekbot.Core.DiceParser; +using Geekbot.Core.GlobalSettings; using Geekbot.Core.RandomNumberGenerator; +using Moq; using Xunit; namespace Tests.Core.DiceParser @@ -43,7 +45,7 @@ namespace Tests.Core.DiceParser [Theory, MemberData(nameof(SingleDieNameTestData))] public void SingleDieNameTestFunc(string testName, SingleDieNameTestDto testData) { - var die = new SingleDie(new RandomNumberGenerator()) {AdvantageType = testData.AdvantageType}; + var die = new SingleDie(new RandomNumberGenerator(new Mock().Object)) {AdvantageType = testData.AdvantageType}; Assert.Equal(die.DiceName, testData.Expected); } @@ -106,7 +108,7 @@ namespace Tests.Core.DiceParser [Theory, MemberData(nameof(SingleDieValidationTestData))] public void SingleDieValidationTestFunc(string testName, SingleDieValidationTestDto testData) { - var die = new SingleDie(new RandomNumberGenerator()) + var die = new SingleDie(new RandomNumberGenerator(new Mock().Object)) { Amount = testData.Amount, Sides = testData.Sides From 58bd4d17d0ecaa09ca0d64219c601cae021a6163 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 23 Sep 2020 16:28:50 +0200 Subject: [PATCH 286/443] Optimize the !quote database query --- src/Bot/Commands/Utils/Quote/Quote.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Bot/Commands/Utils/Quote/Quote.cs b/src/Bot/Commands/Utils/Quote/Quote.cs index 6f4cd1c..db6b1ac 100644 --- a/src/Bot/Commands/Utils/Quote/Quote.cs +++ b/src/Bot/Commands/Utils/Quote/Quote.cs @@ -33,23 +33,23 @@ namespace Geekbot.Bot.Commands.Utils.Quote } [Command] - [Summary("Return a random quoute from the database")] + [Summary("Return a random quote from the database")] public async Task GetRandomQuote() { try { - var s = _database.Quotes.Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong())).ToList(); + var totalQuotes = await _database.Quotes.CountAsync(e => e.GuildId.Equals(Context.Guild.Id.AsLong())); - if (!s.Any()) + if (totalQuotes == 0) { await ReplyAsync(Localization.Quote.NoQuotesFound); return; } - var random = _randomNumberGenerator.Next(0, s.Count - 1); - var quote = s[random]; - - var embed = QuoteBuilder(quote); + var random = _randomNumberGenerator.Next(0, totalQuotes - 1); + var quote = _database.Quotes.Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong())).Skip(random).Take(1); + + var embed = QuoteBuilder(quote.FirstOrDefault()); await ReplyAsync("", false, embed.Build()); } catch (Exception e) From 216188f61fc05bdd255980f076d44f2904539d51 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 23 Sep 2020 16:46:13 +0200 Subject: [PATCH 287/443] Add a deprecation warning for quoting by message ID --- src/Bot/Commands/Utils/Quote/Quote.cs | 15 +++++++++------ src/Bot/Localization/Quote.Designer.cs | 9 +++++++++ src/Bot/Localization/Quote.de-ch.resx | 3 +++ src/Bot/Localization/Quote.resx | 3 +++ 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/Bot/Commands/Utils/Quote/Quote.cs b/src/Bot/Commands/Utils/Quote/Quote.cs index db6b1ac..0b197cd 100644 --- a/src/Bot/Commands/Utils/Quote/Quote.cs +++ b/src/Bot/Commands/Utils/Quote/Quote.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using System.Text; using System.Threading.Tasks; using Discord; using Discord.Commands; @@ -211,10 +212,9 @@ namespace Geekbot.Bot.Commands.Utils.Quote { try { - // var transContext = await _translationHandler.GetGuildContext(Context); var message = await Context.Channel.GetMessageAsync(messageId); - await ProcessQuote(message, saveToDb); + await ProcessQuote(message, saveToDb, true); } catch (Exception e) { @@ -226,8 +226,6 @@ namespace Geekbot.Bot.Commands.Utils.Quote { try { - // var transContext = await _translationHandler.GetGuildContext(Context); - if (!MessageLink.IsValid(messageLink)) { await ReplyAsync(Localization.Quote.NotAValidMessageLink); @@ -255,7 +253,7 @@ namespace Geekbot.Bot.Commands.Utils.Quote } } - private async Task ProcessQuote(IMessage message, bool saveToDb) + private async Task ProcessQuote(IMessage message, bool saveToDb, bool showMessageIdWarning = false) { if (message.Author.Id == Context.Message.Author.Id && saveToDb && !_isDev) { @@ -277,7 +275,12 @@ namespace Geekbot.Bot.Commands.Utils.Quote } var embed = QuoteBuilder(quote); - await ReplyAsync(saveToDb ? Localization.Quote.QuoteAdded : string.Empty, false, embed.Build()); + + var sb = new StringBuilder(); + if (saveToDb) sb.AppendLine(Localization.Quote.QuoteAdded); + if (showMessageIdWarning) sb.AppendLine(Localization.Quote.MessageIdDeprecation); + + await ReplyAsync(sb.ToString(), false, embed.Build()); } private EmbedBuilder QuoteBuilder(QuoteModel quote) diff --git a/src/Bot/Localization/Quote.Designer.cs b/src/Bot/Localization/Quote.Designer.cs index 342c181..d0926ae 100644 --- a/src/Bot/Localization/Quote.Designer.cs +++ b/src/Bot/Localization/Quote.Designer.cs @@ -78,6 +78,15 @@ namespace Geekbot.Bot.Localization { } } + /// + /// Looks up a localized string similar to :warning: Creating quotes by message ID is deprecated in favour of message links and will be removed on 1 December 2020. + /// + internal static string MessageIdDeprecation { + get { + return ResourceManager.GetString("MessageIdDeprecation", resourceCulture); + } + } + /// /// Looks up a localized string similar to Most quoted person. /// diff --git a/src/Bot/Localization/Quote.de-ch.resx b/src/Bot/Localization/Quote.de-ch.resx index 99fd959..c7e3b8b 100644 --- a/src/Bot/Localization/Quote.de-ch.resx +++ b/src/Bot/Localization/Quote.de-ch.resx @@ -44,4 +44,7 @@ Du chasch numme nachrichte vom gliche server quote + + :warning: Es mache vo quotes mit message-IDs isch zgunste vo message-links veraltet und wird am 1. dezember 2020 entfernt + \ No newline at end of file diff --git a/src/Bot/Localization/Quote.resx b/src/Bot/Localization/Quote.resx index b51d79c..215f0ea 100644 --- a/src/Bot/Localization/Quote.resx +++ b/src/Bot/Localization/Quote.resx @@ -51,4 +51,7 @@ You can only quote messages from the same server + + :warning: Creating quotes by message ID is deprecated in favour of message links and will be removed on 1 December 2020 + \ No newline at end of file From 7ef0b6a3195a379f06ca7f33056131dbd192aea5 Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 24 Sep 2020 12:21:21 +0200 Subject: [PATCH 288/443] Swap the my anime list wrapper for the !anime and !manga commands --- src/Bot/Bot.csproj | 2 +- src/Bot/Commands/Integrations/Mal.cs | 116 +++++++++++---------------- src/Bot/Program.cs | 3 - src/Core/Core.csproj | 1 - src/Core/MalClient/IMalClient.cs | 12 --- src/Core/MalClient/MalClient.cs | 63 --------------- 6 files changed, 47 insertions(+), 150 deletions(-) delete mode 100644 src/Core/MalClient/IMalClient.cs delete mode 100644 src/Core/MalClient/MalClient.cs diff --git a/src/Bot/Bot.csproj b/src/Bot/Bot.csproj index 60f6473..4cd5b39 100644 --- a/src/Bot/Bot.csproj +++ b/src/Bot/Bot.csproj @@ -24,8 +24,8 @@ + - diff --git a/src/Bot/Commands/Integrations/Mal.cs b/src/Bot/Commands/Integrations/Mal.cs index ae30493..ab8c021 100644 --- a/src/Bot/Commands/Integrations/Mal.cs +++ b/src/Bot/Commands/Integrations/Mal.cs @@ -1,23 +1,23 @@ using System; +using System.Linq; using System.Threading.Tasks; using System.Web; -using System.Xml; using Discord; using Discord.Commands; using Geekbot.Core.ErrorHandling; using Geekbot.Core.Extensions; -using Geekbot.Core.MalClient; +using JikanDotNet; namespace Geekbot.Bot.Commands.Integrations { public class Mal : ModuleBase { private readonly IErrorHandler _errorHandler; - private readonly IMalClient _malClient; + private readonly IJikan _client; - public Mal(IMalClient malClient, IErrorHandler errorHandler) + public Mal(IErrorHandler errorHandler) { - _malClient = malClient; + _client = new Jikan(); _errorHandler = errorHandler; } @@ -27,46 +27,34 @@ namespace Geekbot.Bot.Commands.Integrations { try { - if (_malClient.IsLoggedIn()) + var results = await _client.SearchAnime(animeName); + var anime = results.Results.FirstOrDefault(); + if (anime != null) { - var anime = await _malClient.GetAnime(animeName); - if (anime != null) - { - var eb = new EmbedBuilder(); + var eb = new EmbedBuilder(); - var description = HttpUtility.HtmlDecode(anime.Synopsis) - .Replace("
", "") - .Replace("[i]", "*") - .Replace("[/i]", "*"); + var description = HttpUtility.HtmlDecode(anime.Description) + .Replace("
", "") + .Replace("[i]", "*") + .Replace("[/i]", "*"); - eb.Title = anime.Title; - eb.Description = description; - eb.ImageUrl = anime.Image; - eb.AddInlineField("Premiered", $"{anime.StartDate}"); - eb.AddInlineField("Ended", anime.EndDate == "0000-00-00" ? "???" : anime.EndDate); - eb.AddInlineField("Status", anime.Status); - eb.AddInlineField("Episodes", anime.Episodes); - eb.AddInlineField("MAL Score", anime.Score); - eb.AddInlineField("Type", anime.Type); - eb.AddField("MAL Link", $"https://myanimelist.net/anime/{anime.Id}"); + eb.Title = anime.Title; + eb.Description = description; + eb.ImageUrl = anime.ImageURL; + eb.AddInlineField("Premiered", $"{anime.StartDate.Value.ToShortDateString()}"); + eb.AddInlineField("Ended", anime.Airing ? "Present" : anime.EndDate.Value.ToShortDateString()); + eb.AddInlineField("Episodes", anime.Episodes); + eb.AddInlineField("MAL Score", anime.Score); + eb.AddInlineField("Type", anime.Type); + eb.AddField("MAL Link", $"https://myanimelist.net/anime/{anime.MalId}"); - await ReplyAsync("", false, eb.Build()); - } - else - { - await ReplyAsync("No anime found with that name..."); - } + await ReplyAsync("", false, eb.Build()); } else { - await ReplyAsync( - "Unfortunally i'm not connected to MyAnimeList.net, please tell my senpai to connect me"); + await ReplyAsync("No anime found with that name..."); } } - catch (XmlException e) - { - await _errorHandler.HandleCommandException(e, Context, "The MyAnimeList.net API refused to answer"); - } catch (Exception e) { await _errorHandler.HandleCommandException(e, Context); @@ -79,46 +67,34 @@ namespace Geekbot.Bot.Commands.Integrations { try { - if (_malClient.IsLoggedIn()) + var results = await _client.SearchManga(mangaName); + var manga = results.Results.FirstOrDefault(); + if (manga != null) { - var manga = await _malClient.GetManga(mangaName); - if (manga != null) - { - var eb = new EmbedBuilder(); - - var description = HttpUtility.HtmlDecode(manga.Synopsis) - .Replace("
", "") - .Replace("[i]", "*") - .Replace("[/i]", "*"); - - eb.Title = manga.Title; - eb.Description = description; - eb.ImageUrl = manga.Image; - eb.AddInlineField("Premiered", $"{manga.StartDate}"); - eb.AddInlineField("Ended", manga.EndDate == "0000-00-00" ? "???" : manga.EndDate); - eb.AddInlineField("Status", manga.Status); - eb.AddInlineField("Volumes", manga.Volumes); - eb.AddInlineField("Chapters", manga.Chapters); - eb.AddInlineField("MAL Score", manga.Score); - eb.AddField("MAL Link", $"https://myanimelist.net/manga/{manga.Id}"); - - await ReplyAsync("", false, eb.Build()); - } - else - { - await ReplyAsync("No manga found with that name..."); - } + var eb = new EmbedBuilder(); + + var description = HttpUtility.HtmlDecode(manga.Description) + .Replace("
", "") + .Replace("[i]", "*") + .Replace("[/i]", "*"); + + eb.Title = manga.Title; + eb.Description = description; + eb.ImageUrl = manga.ImageURL; + eb.AddInlineField("Premiered", $"{manga.StartDate.Value.ToShortDateString()}"); + eb.AddInlineField("Ended", manga.Publishing ? "Present" : manga.EndDate.Value.ToShortDateString()); + eb.AddInlineField("Volumes", manga.Volumes); + eb.AddInlineField("Chapters", manga.Chapters); + eb.AddInlineField("MAL Score", manga.Score); + eb.AddField("MAL Link", $"https://myanimelist.net/manga/{manga.MalId}"); + + await ReplyAsync("", false, eb.Build()); } else { - await ReplyAsync( - "Unfortunally i'm not connected to MyAnimeList.net, please tell my senpai to connect me"); + await ReplyAsync("No manga found with that name..."); } } - catch (XmlException e) - { - await _errorHandler.HandleCommandException(e, Context, "The MyAnimeList.net API refused to answer"); - } catch (Exception e) { await _errorHandler.HandleCommandException(e, Context); diff --git a/src/Bot/Program.cs b/src/Bot/Program.cs index 5994f8c..dee6ec3 100644 --- a/src/Bot/Program.cs +++ b/src/Bot/Program.cs @@ -18,7 +18,6 @@ using Geekbot.Core.Highscores; using Geekbot.Core.KvInMemoryStore; using Geekbot.Core.Levels; using Geekbot.Core.Logger; -using Geekbot.Core.MalClient; using Geekbot.Core.Media; using Geekbot.Core.RandomNumberGenerator; using Geekbot.Core.ReactionListener; @@ -159,7 +158,6 @@ namespace Geekbot.Bot _reactionListener = new ReactionListener(_databaseInitializer.Initialize()); _guildSettingsManager = new GuildSettingsManager(_databaseInitializer.Initialize()); var fortunes = new FortunesProvider(_logger); - var malClient = new MalClient(_globalSettings, _logger); var levelCalc = new LevelCalc(); var emojiConverter = new EmojiConverter(); var mtgManaConverter = new MtgManaConverter(); @@ -176,7 +174,6 @@ namespace Geekbot.Bot services.AddSingleton(emojiConverter); services.AddSingleton(fortunes); services.AddSingleton(mediaProvider); - services.AddSingleton(malClient); services.AddSingleton(mtgManaConverter); services.AddSingleton(wikipediaClient); services.AddSingleton(randomNumberGenerator); diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index 0b3ff44..780d0e2 100644 --- a/src/Core/Core.csproj +++ b/src/Core/Core.csproj @@ -22,7 +22,6 @@ - diff --git a/src/Core/MalClient/IMalClient.cs b/src/Core/MalClient/IMalClient.cs deleted file mode 100644 index 70851c1..0000000 --- a/src/Core/MalClient/IMalClient.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System.Threading.Tasks; -using MyAnimeListSharp.Core; - -namespace Geekbot.Core.MalClient -{ - public interface IMalClient - { - bool IsLoggedIn(); - Task GetAnime(string query); - Task GetManga(string query); - } -} \ No newline at end of file diff --git a/src/Core/MalClient/MalClient.cs b/src/Core/MalClient/MalClient.cs deleted file mode 100644 index 3f5f165..0000000 --- a/src/Core/MalClient/MalClient.cs +++ /dev/null @@ -1,63 +0,0 @@ -using System.Threading.Tasks; -using Geekbot.Core.GlobalSettings; -using Geekbot.Core.Logger; -using MyAnimeListSharp.Auth; -using MyAnimeListSharp.Core; -using MyAnimeListSharp.Facade.Async; - -namespace Geekbot.Core.MalClient -{ - public class MalClient : IMalClient - { - private readonly IGlobalSettings _globalSettings; - private readonly IGeekbotLogger _logger; - private ICredentialContext _credentials; - private AnimeSearchMethodsAsync _animeSearch; - private MangaSearchMethodsAsync _mangaSearch; - - public MalClient(IGlobalSettings globalSettings, IGeekbotLogger logger) - { - _globalSettings = globalSettings; - _logger = logger; - ReloadClient(); - } - - private bool ReloadClient() - { - var malCredentials = _globalSettings.GetKey("MalCredentials"); - if (!string.IsNullOrEmpty(malCredentials)) - { - var credSplit = malCredentials.Split('|'); - _credentials = new CredentialContext() - { - UserName = credSplit[0], - Password = credSplit[1] - }; - _animeSearch = new AnimeSearchMethodsAsync(_credentials); - _mangaSearch = new MangaSearchMethodsAsync(_credentials); - _logger.Debug(LogSource.Geekbot, "Logged in to MAL"); - return true; - } - _logger.Debug(LogSource.Geekbot, "No MAL Credentials Set!"); - return false; - - } - - public bool IsLoggedIn() - { - return _credentials != null; - } - - public async Task GetAnime(string query) - { - var response = await _animeSearch.SearchDeserializedAsync(query); - return response.Entries.Count == 0 ? null : response.Entries[0]; - } - - public async Task GetManga(string query) - { - var response = await _mangaSearch.SearchDeserializedAsync(query); - return response.Entries.Count == 0 ? null : response.Entries[0]; - } - } -} \ No newline at end of file From ed7748833aa499e8a19778f81a575e8a052c41f0 Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 24 Sep 2020 12:49:55 +0200 Subject: [PATCH 289/443] Format dates in the !anime and !manga commands with the correct culture info --- src/Bot/Commands/Integrations/Mal.cs | 31 +++++++++++++++++++--------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/Bot/Commands/Integrations/Mal.cs b/src/Bot/Commands/Integrations/Mal.cs index ab8c021..f44d04e 100644 --- a/src/Bot/Commands/Integrations/Mal.cs +++ b/src/Bot/Commands/Integrations/Mal.cs @@ -1,24 +1,25 @@ using System; using System.Linq; +using System.Threading; using System.Threading.Tasks; using System.Web; using Discord; using Discord.Commands; +using Geekbot.Core; using Geekbot.Core.ErrorHandling; using Geekbot.Core.Extensions; +using Geekbot.Core.GuildSettingsManager; using JikanDotNet; namespace Geekbot.Bot.Commands.Integrations { - public class Mal : ModuleBase + public class Mal : GeekbotCommandBase { - private readonly IErrorHandler _errorHandler; private readonly IJikan _client; - public Mal(IErrorHandler errorHandler) + public Mal(IErrorHandler errorHandler, IGuildSettingsManager guildSettingsManager) : base(errorHandler, guildSettingsManager) { _client = new Jikan(); - _errorHandler = errorHandler; } [Command("anime", RunMode = RunMode.Async)] @@ -41,8 +42,8 @@ namespace Geekbot.Bot.Commands.Integrations eb.Title = anime.Title; eb.Description = description; eb.ImageUrl = anime.ImageURL; - eb.AddInlineField("Premiered", $"{anime.StartDate.Value.ToShortDateString()}"); - eb.AddInlineField("Ended", anime.Airing ? "Present" : anime.EndDate.Value.ToShortDateString()); + eb.AddInlineField("Premiered", FormatDate(anime.StartDate)); + eb.AddInlineField("Ended", anime.Airing ? "Present" : FormatDate(anime.EndDate)); eb.AddInlineField("Episodes", anime.Episodes); eb.AddInlineField("MAL Score", anime.Score); eb.AddInlineField("Type", anime.Type); @@ -57,7 +58,7 @@ namespace Geekbot.Bot.Commands.Integrations } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } @@ -81,8 +82,8 @@ namespace Geekbot.Bot.Commands.Integrations eb.Title = manga.Title; eb.Description = description; eb.ImageUrl = manga.ImageURL; - eb.AddInlineField("Premiered", $"{manga.StartDate.Value.ToShortDateString()}"); - eb.AddInlineField("Ended", manga.Publishing ? "Present" : manga.EndDate.Value.ToShortDateString()); + eb.AddInlineField("Premiered", FormatDate(manga.StartDate)); + eb.AddInlineField("Ended", manga.Publishing ? "Present" : FormatDate(manga.EndDate)); eb.AddInlineField("Volumes", manga.Volumes); eb.AddInlineField("Chapters", manga.Chapters); eb.AddInlineField("MAL Score", manga.Score); @@ -97,8 +98,18 @@ namespace Geekbot.Bot.Commands.Integrations } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } + + private string FormatDate(DateTime? dateTime) + { + if (!dateTime.HasValue) + { + return DateTime.MinValue.ToString("d", Thread.CurrentThread.CurrentUICulture); + } + + return dateTime.Value.ToString("d", Thread.CurrentThread.CurrentUICulture); + } } } \ No newline at end of file From 41795aa13f0a806caf8cb7acb6e29d7653fb1d51 Mon Sep 17 00:00:00 2001 From: runebaas Date: Thu, 24 Sep 2020 18:29:21 +0200 Subject: [PATCH 290/443] Small wording tweaks to the !anime and !manga commands. They also no long need their description html decoded. --- src/Bot/Commands/Integrations/Mal.cs | 63 +++++++++++++--------------- 1 file changed, 28 insertions(+), 35 deletions(-) diff --git a/src/Bot/Commands/Integrations/Mal.cs b/src/Bot/Commands/Integrations/Mal.cs index f44d04e..ffc8dd7 100644 --- a/src/Bot/Commands/Integrations/Mal.cs +++ b/src/Bot/Commands/Integrations/Mal.cs @@ -2,7 +2,6 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; -using System.Web; using Discord; using Discord.Commands; using Geekbot.Core; @@ -32,22 +31,19 @@ namespace Geekbot.Bot.Commands.Integrations var anime = results.Results.FirstOrDefault(); if (anime != null) { - var eb = new EmbedBuilder(); - - var description = HttpUtility.HtmlDecode(anime.Description) - .Replace("
", "") - .Replace("[i]", "*") - .Replace("[/i]", "*"); - - eb.Title = anime.Title; - eb.Description = description; - eb.ImageUrl = anime.ImageURL; - eb.AddInlineField("Premiered", FormatDate(anime.StartDate)); - eb.AddInlineField("Ended", anime.Airing ? "Present" : FormatDate(anime.EndDate)); - eb.AddInlineField("Episodes", anime.Episodes); - eb.AddInlineField("MAL Score", anime.Score); - eb.AddInlineField("Type", anime.Type); - eb.AddField("MAL Link", $"https://myanimelist.net/anime/{anime.MalId}"); + var eb = new EmbedBuilder + { + Title = anime.Title, + Description = anime.Description, + ImageUrl = anime.ImageURL + }; + + eb.AddInlineField("Premiere", FormatDate(anime.StartDate)) + .AddInlineField("Ended", anime.Airing ? "-" : FormatDate(anime.EndDate)) + .AddInlineField("Episodes", anime.Episodes) + .AddInlineField("MAL Score", anime.Score) + .AddInlineField("Type", anime.Type) + .AddField("MAL Link", $"https://myanimelist.net/anime/{anime.MalId}"); await ReplyAsync("", false, eb.Build()); } @@ -72,23 +68,20 @@ namespace Geekbot.Bot.Commands.Integrations var manga = results.Results.FirstOrDefault(); if (manga != null) { - var eb = new EmbedBuilder(); - - var description = HttpUtility.HtmlDecode(manga.Description) - .Replace("
", "") - .Replace("[i]", "*") - .Replace("[/i]", "*"); - - eb.Title = manga.Title; - eb.Description = description; - eb.ImageUrl = manga.ImageURL; - eb.AddInlineField("Premiered", FormatDate(manga.StartDate)); - eb.AddInlineField("Ended", manga.Publishing ? "Present" : FormatDate(manga.EndDate)); - eb.AddInlineField("Volumes", manga.Volumes); - eb.AddInlineField("Chapters", manga.Chapters); - eb.AddInlineField("MAL Score", manga.Score); - eb.AddField("MAL Link", $"https://myanimelist.net/manga/{manga.MalId}"); - + var eb = new EmbedBuilder + { + Title = manga.Title, + Description = manga.Description, + ImageUrl = manga.ImageURL + }; + + eb.AddInlineField("Premiere", FormatDate(manga.StartDate)) + .AddInlineField("Ended", manga.Publishing ? "-" : FormatDate(manga.EndDate)) + .AddInlineField("Volumes", manga.Volumes) + .AddInlineField("Chapters", manga.Chapters) + .AddInlineField("MAL Score", manga.Score) + .AddField("MAL Link", $"https://myanimelist.net/manga/{manga.MalId}"); + await ReplyAsync("", false, eb.Build()); } else @@ -108,7 +101,7 @@ namespace Geekbot.Bot.Commands.Integrations { return DateTime.MinValue.ToString("d", Thread.CurrentThread.CurrentUICulture); } - + return dateTime.Value.ToString("d", Thread.CurrentThread.CurrentUICulture); } } From 28a5b9322e831517f87e8cb08d85bf6a364bcecd Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 9 Oct 2020 17:10:49 +0200 Subject: [PATCH 291/443] Allow Rune#0007 to bypass the ignored servers restriction --- src/Bot/Handlers/CommandHandler.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Bot/Handlers/CommandHandler.cs b/src/Bot/Handlers/CommandHandler.cs index 52e9a52..3c072fe 100644 --- a/src/Bot/Handlers/CommandHandler.cs +++ b/src/Bot/Handlers/CommandHandler.cs @@ -58,7 +58,8 @@ namespace Geekbot.Bot.Handlers _ => 0 // DM Channel }; - if (IsIgnoredGuild(guildId, message.Author.Id)) return Task.CompletedTask; + // temp hack, will fix later + if (IsIgnoredGuild(guildId, message.Author.Id) || messageParam.Author.Id == 93061333972455424) return Task.CompletedTask; var lowCaseMsg = message.ToString().ToLower(); if (ShouldHui(lowCaseMsg, guildId)) From fe51dfe540d9dfae1ff9a2d5c15067adf65c0942 Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 9 Oct 2020 17:25:53 +0200 Subject: [PATCH 292/443] Revert last commit --- src/Bot/Handlers/CommandHandler.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Bot/Handlers/CommandHandler.cs b/src/Bot/Handlers/CommandHandler.cs index 3c072fe..52e9a52 100644 --- a/src/Bot/Handlers/CommandHandler.cs +++ b/src/Bot/Handlers/CommandHandler.cs @@ -58,8 +58,7 @@ namespace Geekbot.Bot.Handlers _ => 0 // DM Channel }; - // temp hack, will fix later - if (IsIgnoredGuild(guildId, message.Author.Id) || messageParam.Author.Id == 93061333972455424) return Task.CompletedTask; + if (IsIgnoredGuild(guildId, message.Author.Id)) return Task.CompletedTask; var lowCaseMsg = message.ToString().ToLower(); if (ShouldHui(lowCaseMsg, guildId)) From ffab56d4a5e893aac0416ef75283fbb9f2b53129 Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 23 Oct 2020 21:46:59 +0200 Subject: [PATCH 293/443] Delete original message from user after using !emojify --- src/Bot/Commands/Utils/Emojify.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Bot/Commands/Utils/Emojify.cs b/src/Bot/Commands/Utils/Emojify.cs index c8328ee..8bb880e 100644 --- a/src/Bot/Commands/Utils/Emojify.cs +++ b/src/Bot/Commands/Utils/Emojify.cs @@ -32,6 +32,14 @@ namespace Geekbot.Bot.Commands.Utils await ReplyAsync($"{Context.User.Username}#{Context.User.Discriminator} said:"); await ReplyAsync(emojis); + try + { + await Context.Message.DeleteAsync(); + } + catch + { + // bot may not have enough permission, doesn't matter if it fails + } } catch (Exception e) { From 2fb815bc97356d274d7f7edc80d380f97d634019 Mon Sep 17 00:00:00 2001 From: runebaas Date: Mon, 2 Nov 2020 18:01:57 +0100 Subject: [PATCH 294/443] Don't use embeds for !dog --- src/Bot/Commands/Randomness/Dog/Dog.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Bot/Commands/Randomness/Dog/Dog.cs b/src/Bot/Commands/Randomness/Dog/Dog.cs index 2a8a3f1..051dbf3 100644 --- a/src/Bot/Commands/Randomness/Dog/Dog.cs +++ b/src/Bot/Commands/Randomness/Dog/Dog.cs @@ -23,11 +23,7 @@ namespace Geekbot.Bot.Commands.Randomness.Dog try { var response = await HttpAbstractions.Get(new Uri("http://random.dog/woof.json")); - var eb = new EmbedBuilder - { - ImageUrl = response.Url - }; - await ReplyAsync("", false, eb.Build()); + await ReplyAsync(response.Url); } catch (Exception e) { From 4c3b7044ce6543bc7cbfd9fc80fd71bee19a449d Mon Sep 17 00:00:00 2001 From: runebaas Date: Mon, 2 Nov 2020 18:33:05 +0100 Subject: [PATCH 295/443] Don't count 1 to many sides in the !dice command --- src/Core/DiceParser/SingleDie.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core/DiceParser/SingleDie.cs b/src/Core/DiceParser/SingleDie.cs index f13dbcd..4e0f474 100644 --- a/src/Core/DiceParser/SingleDie.cs +++ b/src/Core/DiceParser/SingleDie.cs @@ -32,7 +32,7 @@ namespace Geekbot.Core.DiceParser { var result = new DieResult { - Roll1 = _random.Next(1, Sides + 1), + Roll1 = _random.Next(1, Sides), AdvantageType = AdvantageType }; From 09dbeb97667d12adcfd9fab69fc14b2811a6abdb Mon Sep 17 00:00:00 2001 From: runebaas Date: Tue, 24 Nov 2020 21:40:33 +0100 Subject: [PATCH 296/443] Fix a stupid bug with the !avatar command --- src/Bot/Commands/Utils/AvatarGetter.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Bot/Commands/Utils/AvatarGetter.cs b/src/Bot/Commands/Utils/AvatarGetter.cs index 6585a75..2aa6b07 100644 --- a/src/Bot/Commands/Utils/AvatarGetter.cs +++ b/src/Bot/Commands/Utils/AvatarGetter.cs @@ -21,8 +21,8 @@ namespace Geekbot.Bot.Commands.Utils { try { - if (user == null) user = Context.User; - var url = user.GetAvatarUrl().Replace("128", "1024"); + user ??= Context.User; + var url = user.GetAvatarUrl(ImageFormat.Auto, 1024); await ReplyAsync(url); } catch (Exception e) From baf09e2f3807c9aa3fbd652f344399fee835eb89 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 25 Nov 2020 15:21:55 +0100 Subject: [PATCH 297/443] Remove the ability to create quotes from message ids --- src/Bot/Commands/Utils/Quote/Quote.cs | 35 ++------------------------ src/Bot/Localization/Quote.Designer.cs | 9 ------- src/Bot/Localization/Quote.de-ch.resx | 3 --- src/Bot/Localization/Quote.resx | 3 --- 4 files changed, 2 insertions(+), 48 deletions(-) diff --git a/src/Bot/Commands/Utils/Quote/Quote.cs b/src/Bot/Commands/Utils/Quote/Quote.cs index 0b197cd..a5f2e3c 100644 --- a/src/Bot/Commands/Utils/Quote/Quote.cs +++ b/src/Bot/Commands/Utils/Quote/Quote.cs @@ -74,22 +74,6 @@ namespace Geekbot.Bot.Commands.Utils.Quote { await QuoteFromMention(user, false); } - - [Command("add")] - [Alias("save")] - [Summary("Add a quote from a message id")] - public async Task AddQuote([Summary("message-ID")] ulong messageId) - { - await QuoteFromMessageId(messageId, true); - } - - [Command("make")] - [Alias("preview")] - [Summary("Preview a quote from a message id")] - public async Task ReturnSpecifiedQuote([Summary("message-ID")] ulong messageId) - { - await QuoteFromMessageId(messageId, false); - } [Command("add")] [Alias("save")] @@ -208,21 +192,7 @@ namespace Geekbot.Bot.Commands.Utils.Quote } - private async Task QuoteFromMessageId(ulong messageId, bool saveToDb) - { - try - { - var message = await Context.Channel.GetMessageAsync(messageId); - - await ProcessQuote(message, saveToDb, true); - } - catch (Exception e) - { - await ErrorHandler.HandleCommandException(e, Context, "I couldn't find a message with that id :disappointed:"); - } - } - - private async Task QuoteFromMessageLink(string messageLink, bool saveToDb) + private async Task QuoteFromMessageLink(string messageLink, bool saveToDb) { try { @@ -253,7 +223,7 @@ namespace Geekbot.Bot.Commands.Utils.Quote } } - private async Task ProcessQuote(IMessage message, bool saveToDb, bool showMessageIdWarning = false) + private async Task ProcessQuote(IMessage message, bool saveToDb) { if (message.Author.Id == Context.Message.Author.Id && saveToDb && !_isDev) { @@ -278,7 +248,6 @@ namespace Geekbot.Bot.Commands.Utils.Quote var sb = new StringBuilder(); if (saveToDb) sb.AppendLine(Localization.Quote.QuoteAdded); - if (showMessageIdWarning) sb.AppendLine(Localization.Quote.MessageIdDeprecation); await ReplyAsync(sb.ToString(), false, embed.Build()); } diff --git a/src/Bot/Localization/Quote.Designer.cs b/src/Bot/Localization/Quote.Designer.cs index d0926ae..342c181 100644 --- a/src/Bot/Localization/Quote.Designer.cs +++ b/src/Bot/Localization/Quote.Designer.cs @@ -78,15 +78,6 @@ namespace Geekbot.Bot.Localization { } } - /// - /// Looks up a localized string similar to :warning: Creating quotes by message ID is deprecated in favour of message links and will be removed on 1 December 2020. - /// - internal static string MessageIdDeprecation { - get { - return ResourceManager.GetString("MessageIdDeprecation", resourceCulture); - } - } - /// /// Looks up a localized string similar to Most quoted person. /// diff --git a/src/Bot/Localization/Quote.de-ch.resx b/src/Bot/Localization/Quote.de-ch.resx index c7e3b8b..99fd959 100644 --- a/src/Bot/Localization/Quote.de-ch.resx +++ b/src/Bot/Localization/Quote.de-ch.resx @@ -44,7 +44,4 @@ Du chasch numme nachrichte vom gliche server quote - - :warning: Es mache vo quotes mit message-IDs isch zgunste vo message-links veraltet und wird am 1. dezember 2020 entfernt - \ No newline at end of file diff --git a/src/Bot/Localization/Quote.resx b/src/Bot/Localization/Quote.resx index 215f0ea..b51d79c 100644 --- a/src/Bot/Localization/Quote.resx +++ b/src/Bot/Localization/Quote.resx @@ -51,7 +51,4 @@ You can only quote messages from the same server - - :warning: Creating quotes by message ID is deprecated in favour of message links and will be removed on 1 December 2020 - \ No newline at end of file From 714b0008bc480ebb1ac56f9f397d5591dc066df9 Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 11 Dec 2020 22:38:43 +0100 Subject: [PATCH 298/443] Allow a die to have 145 sides --- src/Core/DiceParser/SingleDie.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Core/DiceParser/SingleDie.cs b/src/Core/DiceParser/SingleDie.cs index 4e0f474..7c1ae41 100644 --- a/src/Core/DiceParser/SingleDie.cs +++ b/src/Core/DiceParser/SingleDie.cs @@ -63,9 +63,9 @@ namespace Geekbot.Core.DiceParser throw new DiceException("Die must have at least 2 sides") { DiceName = DiceName }; } - if (Sides > 144) + if (Sides > 145) { - throw new DiceException("Die can not have more than 144 sides") { DiceName = DiceName }; + throw new DiceException("Die can not have more than 145 sides") { DiceName = DiceName }; } } } From 7e792bd7821ada737086c3b8a5171cc82ffc55b4 Mon Sep 17 00:00:00 2001 From: runebaas Date: Tue, 29 Dec 2020 17:12:03 +0100 Subject: [PATCH 299/443] Fallback to user repo when retrieving a user via the discord gateway fails or times out --- src/Bot/Commands/Utils/Quote/Quote.cs | 16 ++++++++++++++-- src/Core/Polyfills/UserPolyfillDto.cs | 3 ++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Bot/Commands/Utils/Quote/Quote.cs b/src/Bot/Commands/Utils/Quote/Quote.cs index a5f2e3c..47986a0 100644 --- a/src/Bot/Commands/Utils/Quote/Quote.cs +++ b/src/Bot/Commands/Utils/Quote/Quote.cs @@ -13,6 +13,7 @@ using Geekbot.Core.Extensions; using Geekbot.Core.GuildSettingsManager; using Geekbot.Core.Polyfills; using Geekbot.Core.RandomNumberGenerator; +using Geekbot.Core.UserRepository; namespace Geekbot.Bot.Commands.Utils.Quote { @@ -22,13 +23,15 @@ namespace Geekbot.Bot.Commands.Utils.Quote { private readonly DatabaseContext _database; private readonly IRandomNumberGenerator _randomNumberGenerator; + private readonly IUserRepository _userRepository; private readonly bool _isDev; - public Quote(IErrorHandler errorHandler, DatabaseContext database, IRandomNumberGenerator randomNumberGenerator, IGuildSettingsManager guildSettingsManager) + public Quote(IErrorHandler errorHandler, DatabaseContext database, IRandomNumberGenerator randomNumberGenerator, IGuildSettingsManager guildSettingsManager, IUserRepository userRepository) : base(errorHandler, guildSettingsManager) { _database = database; _randomNumberGenerator = randomNumberGenerator; + _userRepository = userRepository; // to remove restrictions when developing _isDev = Constants.BotVersion() == "0.0.0-DEV"; } @@ -254,7 +257,16 @@ namespace Geekbot.Bot.Commands.Utils.Quote private EmbedBuilder QuoteBuilder(QuoteModel quote) { - var user = Context.Client.GetUserAsync(quote.UserId.AsUlong()).Result ?? new UserPolyfillDto { Username = "Unknown User" }; + var user = Context.Client.GetUserAsync(quote.UserId.AsUlong()).Result; + if (user == null) + { + var fallbackUserFromRepo = _userRepository.Get(quote.UserId.AsUlong()); + user = new UserPolyfillDto() + { + Username = fallbackUserFromRepo?.Username ?? "Unknown User", + AvatarUrl = fallbackUserFromRepo?.AvatarUrl + }; + } var eb = new EmbedBuilder(); eb.WithColor(new Color(143, 167, 232)); if (quote.InternalId == 0) diff --git a/src/Core/Polyfills/UserPolyfillDto.cs b/src/Core/Polyfills/UserPolyfillDto.cs index 3a8f43f..1d3a54b 100644 --- a/src/Core/Polyfills/UserPolyfillDto.cs +++ b/src/Core/Polyfills/UserPolyfillDto.cs @@ -14,6 +14,7 @@ namespace Geekbot.Core.Polyfills public UserStatus Status { get; set; } public IImmutableSet ActiveClients { get; } public string AvatarId { get; set; } + public string AvatarUrl { get; set; } public string Discriminator { get; set; } public ushort DiscriminatorValue { get; set; } public bool IsBot { get; set; } @@ -22,7 +23,7 @@ namespace Geekbot.Core.Polyfills public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128) { - return "https://discordapp.com/assets/6debd47ed13483642cf09e832ed0bc1b.png"; + return AvatarUrl ?? "https://discordapp.com/assets/6debd47ed13483642cf09e832ed0bc1b.png"; } public string GetDefaultAvatarUrl() From 29c0def713fb668cdbd1d3bc7a74d94e0738a23c Mon Sep 17 00:00:00 2001 From: runebaas Date: Tue, 29 Dec 2020 22:33:19 +0100 Subject: [PATCH 300/443] Start counting messages per quarter starting 1 january 2021 --- src/Bot/Handlers/StatsHandler.cs | 88 +++++++++++++++---- src/Core/Database/DatabaseContext.cs | 1 + .../Database/Models/MessageSeasonsModel.cs | 21 +++++ 3 files changed, 95 insertions(+), 15 deletions(-) create mode 100644 src/Core/Database/Models/MessageSeasonsModel.cs diff --git a/src/Bot/Handlers/StatsHandler.cs b/src/Bot/Handlers/StatsHandler.cs index 197bbb8..e5962a9 100644 --- a/src/Bot/Handlers/StatsHandler.cs +++ b/src/Bot/Handlers/StatsHandler.cs @@ -1,5 +1,7 @@ using System; +using System.Globalization; using System.Threading.Tasks; +using System.Timers; using Discord.WebSocket; using Geekbot.Core.Database; using Geekbot.Core.Database.Models; @@ -13,11 +15,28 @@ namespace Geekbot.Bot.Handlers { private readonly IGeekbotLogger _logger; private readonly DatabaseContext _database; + private string _season; + // ToDo: Clean up in 2021 + private bool _seasonsStarted; public StatsHandler(IGeekbotLogger logger, DatabaseContext database) { _logger = logger; _database = database; + _season = GetSeason(); + _seasonsStarted = DateTime.Now.Year == 2021; + + var timer = new Timer() + { + Enabled = true, + AutoReset = true, + Interval = TimeSpan.FromHours(1).TotalMilliseconds + }; + timer.Elapsed += (sender, args) => + { + _season = GetSeason(); + _seasonsStarted = DateTime.Now.Year == 2021; + }; } public async Task UpdateStats(SocketMessage message) @@ -32,23 +51,12 @@ namespace Geekbot.Bot.Handlers } var channel = (SocketGuildChannel) message.Channel; - - var rowId = await _database.Database.ExecuteSqlRawAsync( - "UPDATE \"Messages\" SET \"MessageCount\" = \"MessageCount\" + 1 WHERE \"GuildId\" = {0} AND \"UserId\" = {1}", - channel.Guild.Id.AsLong(), - message.Author.Id.AsLong() - ); - - if (rowId == 0) + await UpdateTotalTable(message, channel); + if (_seasonsStarted) { - await _database.Messages.AddAsync(new MessagesModel - { - UserId = message.Author.Id.AsLong(), - GuildId = channel.Guild.Id.AsLong(), - MessageCount = 1 - }); - await _database.SaveChangesAsync(); + await UpdateSeasonsTable(message, channel); } + if (message.Author.IsBot) return; _logger.Information(LogSource.Message, message.Content, SimpleConextConverter.ConvertSocketMessage(message)); @@ -58,5 +66,55 @@ namespace Geekbot.Bot.Handlers _logger.Error(LogSource.Message, "Could not process message stats", e); } } + + private async Task UpdateTotalTable(SocketMessage message, SocketGuildChannel channel) + { + var rowId = await _database.Database.ExecuteSqlRawAsync( + "UPDATE \"Messages\" SET \"MessageCount\" = \"MessageCount\" + 1 WHERE \"GuildId\" = {0} AND \"UserId\" = {1}", + channel.Guild.Id.AsLong(), + message.Author.Id.AsLong() + ); + + if (rowId == 0) + { + await _database.Messages.AddAsync(new MessagesModel + { + UserId = message.Author.Id.AsLong(), + GuildId = channel.Guild.Id.AsLong(), + MessageCount = 1 + }); + await _database.SaveChangesAsync(); + } + } + + private async Task UpdateSeasonsTable(SocketMessage message, SocketGuildChannel channel) + { + var rowId = await _database.Database.ExecuteSqlRawAsync( + "UPDATE \"MessagesSeasons\" SET \"MessageCount\" = \"MessageCount\" + 1 WHERE \"GuildId\" = {0} AND \"UserId\" = {1} AND \"Season\" = {2}", + channel.Guild.Id.AsLong(), + message.Author.Id.AsLong(), + _season + ); + + if (rowId == 0) + { + await _database.MessagesSeasons.AddAsync(new MessageSeasonsModel() + { + UserId = message.Author.Id.AsLong(), + GuildId = channel.Guild.Id.AsLong(), + Season = _season, + MessageCount = 1 + }); + await _database.SaveChangesAsync(); + } + } + + private static string GetSeason() + { + var now = DateTime.Now; + var year = (now.Year - 2000).ToString(CultureInfo.InvariantCulture); + var quarter = Math.Ceiling(now.Month / 3.0).ToString(CultureInfo.InvariantCulture); + return $"{year}Q{quarter}"; + } } } \ No newline at end of file diff --git a/src/Core/Database/DatabaseContext.cs b/src/Core/Database/DatabaseContext.cs index 93d46d3..14a1275 100644 --- a/src/Core/Database/DatabaseContext.cs +++ b/src/Core/Database/DatabaseContext.cs @@ -11,6 +11,7 @@ namespace Geekbot.Core.Database public DbSet Karma { get; set; } public DbSet Ships { get; set; } public DbSet Rolls { get; set; } + public DbSet MessagesSeasons { get; set; } public DbSet Messages { get; set; } public DbSet Slaps { get; set; } public DbSet Globals { get; set; } diff --git a/src/Core/Database/Models/MessageSeasonsModel.cs b/src/Core/Database/Models/MessageSeasonsModel.cs new file mode 100644 index 0000000..5a4252c --- /dev/null +++ b/src/Core/Database/Models/MessageSeasonsModel.cs @@ -0,0 +1,21 @@ +using System.ComponentModel.DataAnnotations; + +namespace Geekbot.Core.Database.Models +{ + public class MessageSeasonsModel + { + [Key] + public int Id { get; set; } + + [Required] + public long GuildId { get; set; } + + [Required] + public long UserId { get; set; } + + [Required] + public string Season { get; set; } + + public int MessageCount { get; set; } + } +} \ No newline at end of file From 29bb8035fe14f4f9a2b34c60f91548433fd82535 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Wed, 30 Dec 2020 23:17:00 +0100 Subject: [PATCH 301/443] add !rank seasons to the rank command --- src/Bot/Commands/User/Ranking/Rank.cs | 22 ++++++++++++++++++---- src/Bot/Handlers/StatsHandler.cs | 16 +++++----------- src/Core/Highscores/HighscoreManager.cs | 16 +++++++++++++++- src/Core/Highscores/HighscoreTypes.cs | 3 ++- src/Core/Highscores/IHighscoreManager.cs | 3 ++- src/Core/Highscores/SeasonsUtils.cs | 16 ++++++++++++++++ 6 files changed, 58 insertions(+), 18 deletions(-) create mode 100644 src/Core/Highscores/SeasonsUtils.cs diff --git a/src/Bot/Commands/User/Ranking/Rank.cs b/src/Bot/Commands/User/Ranking/Rank.cs index 20749a8..14a7650 100644 --- a/src/Bot/Commands/User/Ranking/Rank.cs +++ b/src/Bot/Commands/User/Ranking/Rank.cs @@ -30,9 +30,12 @@ namespace Geekbot.Bot.Commands.User.Ranking } [Command("rank", RunMode = RunMode.Async)] - [Summary("get user top 10 in messages or karma")] + [Summary("Get the highscore for various stats like message count, karma, correctly guessed roles, etc...")] [DisableInDirectMessage] - public async Task RankCmd([Summary("type")] string typeUnformated = "messages", [Summary("amount")] int amount = 10) + public async Task RankCmd( + [Summary("type")] string typeUnformated = "messages", + [Summary("amount")] int amount = 10, + [Summary("season")] string season = null) { try { @@ -59,7 +62,7 @@ namespace Geekbot.Bot.Commands.User.Ranking Dictionary highscoreUsers; try { - highscoreUsers = _highscoreManager.GetHighscoresWithUserData(type, guildId, amount); + highscoreUsers = _highscoreManager.GetHighscoresWithUserData(type, guildId, amount, season); } catch (HighscoreListEmptyException) { @@ -80,7 +83,18 @@ namespace Geekbot.Bot.Commands.User.Ranking if (failedToRetrieveUser) replyBuilder.AppendLine(Localization.Rank.FailedToResolveAllUsernames).AppendLine(); - replyBuilder.AppendLine(string.Format(Localization.Rank.HighscoresFor, type.ToString().CapitalizeFirst(), Context.Guild.Name)); + if (type == HighscoreTypes.seasons) + { + if (string.IsNullOrEmpty(season)) + { + season = SeasonsUtils.GetCurrentSeason(); + } + replyBuilder.AppendLine(string.Format(Localization.Rank.HighscoresFor, $"{type.ToString().CapitalizeFirst()} ({season})", Context.Guild.Name)); + } + else + { + replyBuilder.AppendLine(string.Format(Localization.Rank.HighscoresFor, type.ToString().CapitalizeFirst(), Context.Guild.Name)); + } var highscorePlace = 1; foreach (var (user, value) in highscoreUsers) diff --git a/src/Bot/Handlers/StatsHandler.cs b/src/Bot/Handlers/StatsHandler.cs index e5962a9..669be0f 100644 --- a/src/Bot/Handlers/StatsHandler.cs +++ b/src/Bot/Handlers/StatsHandler.cs @@ -1,11 +1,11 @@ using System; -using System.Globalization; using System.Threading.Tasks; using System.Timers; using Discord.WebSocket; using Geekbot.Core.Database; using Geekbot.Core.Database.Models; using Geekbot.Core.Extensions; +using Geekbot.Core.Highscores; using Geekbot.Core.Logger; using Microsoft.EntityFrameworkCore; @@ -23,7 +23,7 @@ namespace Geekbot.Bot.Handlers { _logger = logger; _database = database; - _season = GetSeason(); + _season = SeasonsUtils.GetCurrentSeason(); _seasonsStarted = DateTime.Now.Year == 2021; var timer = new Timer() @@ -34,7 +34,9 @@ namespace Geekbot.Bot.Handlers }; timer.Elapsed += (sender, args) => { - _season = GetSeason(); + var current = SeasonsUtils.GetCurrentSeason(); + if (current == _season) return; + _season = SeasonsUtils.GetCurrentSeason(); _seasonsStarted = DateTime.Now.Year == 2021; }; } @@ -108,13 +110,5 @@ namespace Geekbot.Bot.Handlers await _database.SaveChangesAsync(); } } - - private static string GetSeason() - { - var now = DateTime.Now; - var year = (now.Year - 2000).ToString(CultureInfo.InvariantCulture); - var quarter = Math.Ceiling(now.Month / 3.0).ToString(CultureInfo.InvariantCulture); - return $"{year}Q{quarter}"; - } } } \ No newline at end of file diff --git a/src/Core/Highscores/HighscoreManager.cs b/src/Core/Highscores/HighscoreManager.cs index ac8580b..7379e5e 100644 --- a/src/Core/Highscores/HighscoreManager.cs +++ b/src/Core/Highscores/HighscoreManager.cs @@ -18,7 +18,7 @@ namespace Geekbot.Core.Highscores } - public Dictionary GetHighscoresWithUserData(HighscoreTypes type, ulong guildId, int amount) + public Dictionary GetHighscoresWithUserData(HighscoreTypes type, ulong guildId, int amount, string season = null) { var list = type switch { @@ -26,6 +26,7 @@ namespace Geekbot.Core.Highscores HighscoreTypes.karma => GetKarmaList(guildId, amount), HighscoreTypes.rolls => GetRollsList(guildId, amount), HighscoreTypes.cookies => GetCookiesList(guildId, amount), + HighscoreTypes.seasons => GetMessageSeasonList(guildId, amount, season), _ => new Dictionary() }; @@ -75,6 +76,19 @@ namespace Geekbot.Core.Highscores .ToDictionary(key => key.UserId.AsUlong(), key => key.MessageCount); } + public Dictionary GetMessageSeasonList(ulong guildId, int amount, string season) + { + if (string.IsNullOrEmpty(season)) + { + season = SeasonsUtils.GetCurrentSeason(); + } + return _database.MessagesSeasons + .Where(k => k.GuildId.Equals(guildId.AsLong()) && k.Season.Equals(season)) + .OrderByDescending(o => o.MessageCount) + .Take(amount) + .ToDictionary(key => key.UserId.AsUlong(), key => key.MessageCount); + } + public Dictionary GetKarmaList(ulong guildId, int amount) { return _database.Karma diff --git a/src/Core/Highscores/HighscoreTypes.cs b/src/Core/Highscores/HighscoreTypes.cs index b577642..9901c98 100644 --- a/src/Core/Highscores/HighscoreTypes.cs +++ b/src/Core/Highscores/HighscoreTypes.cs @@ -5,6 +5,7 @@ messages, karma, rolls, - cookies + cookies, + seasons } } \ No newline at end of file diff --git a/src/Core/Highscores/IHighscoreManager.cs b/src/Core/Highscores/IHighscoreManager.cs index 83ba2da..9c2d6f0 100644 --- a/src/Core/Highscores/IHighscoreManager.cs +++ b/src/Core/Highscores/IHighscoreManager.cs @@ -4,8 +4,9 @@ namespace Geekbot.Core.Highscores { public interface IHighscoreManager { - Dictionary GetHighscoresWithUserData(HighscoreTypes type, ulong guildId, int amount); + Dictionary GetHighscoresWithUserData(HighscoreTypes type, ulong guildId, int amount, string season = null); Dictionary GetMessageList(ulong guildId, int amount); + Dictionary GetMessageSeasonList(ulong guildId, int amount, string season); Dictionary GetKarmaList(ulong guildId, int amount); Dictionary GetRollsList(ulong guildId, int amount); } diff --git a/src/Core/Highscores/SeasonsUtils.cs b/src/Core/Highscores/SeasonsUtils.cs new file mode 100644 index 0000000..d2a8e3e --- /dev/null +++ b/src/Core/Highscores/SeasonsUtils.cs @@ -0,0 +1,16 @@ +using System; +using System.Globalization; + +namespace Geekbot.Core.Highscores +{ + public class SeasonsUtils + { + public static string GetCurrentSeason() + { + var now = DateTime.Now; + var year = (now.Year - 2000).ToString(CultureInfo.InvariantCulture); + var quarter = Math.Ceiling(now.Month / 3.0).ToString(CultureInfo.InvariantCulture); + return $"{year}Q{quarter}"; + } + } +} \ No newline at end of file From 01f0d2f43b613d07b9a5be365c6274f2f9dbd983 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Wed, 30 Dec 2020 23:19:15 +0100 Subject: [PATCH 302/443] Check every 5 minutes if it's 2021 instead of every hour in the stats handler --- src/Bot/Handlers/StatsHandler.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bot/Handlers/StatsHandler.cs b/src/Bot/Handlers/StatsHandler.cs index 669be0f..137842c 100644 --- a/src/Bot/Handlers/StatsHandler.cs +++ b/src/Bot/Handlers/StatsHandler.cs @@ -30,7 +30,7 @@ namespace Geekbot.Bot.Handlers { Enabled = true, AutoReset = true, - Interval = TimeSpan.FromHours(1).TotalMilliseconds + Interval = TimeSpan.FromMinutes(5).TotalMilliseconds }; timer.Elapsed += (sender, args) => { From 17f62d76079f48c49e9943a6f11f73adb15970ca Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Wed, 30 Dec 2020 23:23:40 +0100 Subject: [PATCH 303/443] Ignore the discord bots server when updating stats --- src/Bot/Handlers/StatsHandler.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Bot/Handlers/StatsHandler.cs b/src/Bot/Handlers/StatsHandler.cs index 137842c..5ee7871 100644 --- a/src/Bot/Handlers/StatsHandler.cs +++ b/src/Bot/Handlers/StatsHandler.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using System.Timers; using Discord.WebSocket; @@ -25,7 +26,7 @@ namespace Geekbot.Bot.Handlers _database = database; _season = SeasonsUtils.GetCurrentSeason(); _seasonsStarted = DateTime.Now.Year == 2021; - + var timer = new Timer() { Enabled = true, @@ -53,6 +54,14 @@ namespace Geekbot.Bot.Handlers } var channel = (SocketGuildChannel) message.Channel; + + // ignore the discord bots server + // ToDo: create a clean solution for this... + if (channel.Guild.Id == 110373943822540800) + { + return; + } + await UpdateTotalTable(message, channel); if (_seasonsStarted) { From 8bdf2e96818c2ef2ad5acb6906ca8200e7851e77 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Wed, 30 Dec 2020 23:31:15 +0100 Subject: [PATCH 304/443] Add support for !rank quote --- src/Bot/Commands/Utils/Quote/Quote.cs | 4 ++-- src/Core/Highscores/HighscoreManager.cs | 12 ++++++++++++ src/Core/Highscores/HighscoreTypes.cs | 3 ++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Bot/Commands/Utils/Quote/Quote.cs b/src/Bot/Commands/Utils/Quote/Quote.cs index 47986a0..6a94309 100644 --- a/src/Bot/Commands/Utils/Quote/Quote.cs +++ b/src/Bot/Commands/Utils/Quote/Quote.cs @@ -147,8 +147,8 @@ namespace Geekbot.Bot.Commands.Utils.Quote .Where(row => row.GuildId == Context.Guild.Id.AsLong()) .GroupBy(row => row.UserId) .Select(row => new { userId = row.Key, amount = row.Count()}) - .OrderBy(row => row.amount) - .Last(); + .OrderByDescending(row => row.amount) + .First(); var mostQuotedPersonUser = Context.Client.GetUserAsync(mostQuotedPerson.userId.AsUlong()).Result ?? new UserPolyfillDto {Username = "Unknown User"}; var quotesByYear = _database.Quotes diff --git a/src/Core/Highscores/HighscoreManager.cs b/src/Core/Highscores/HighscoreManager.cs index 7379e5e..c839b39 100644 --- a/src/Core/Highscores/HighscoreManager.cs +++ b/src/Core/Highscores/HighscoreManager.cs @@ -27,6 +27,7 @@ namespace Geekbot.Core.Highscores HighscoreTypes.rolls => GetRollsList(guildId, amount), HighscoreTypes.cookies => GetCookiesList(guildId, amount), HighscoreTypes.seasons => GetMessageSeasonList(guildId, amount, season), + HighscoreTypes.quotes => GetQuotesList(guildId, amount), _ => new Dictionary() }; @@ -115,5 +116,16 @@ namespace Geekbot.Core.Highscores .Take(amount) .ToDictionary(key => key.UserId.AsUlong(), key => key.Cookies); } + + private Dictionary GetQuotesList(ulong guildId, int amount) + { + return _database.Quotes + .Where(row => row.GuildId == guildId.AsLong()) + .GroupBy(row => row.UserId) + .Select(row => new { userId = row.Key, amount = row.Count()}) + .OrderByDescending(row => row.amount) + .Take(amount) + .ToDictionary(key => key.userId.AsUlong(), key => key.amount); + } } } \ No newline at end of file diff --git a/src/Core/Highscores/HighscoreTypes.cs b/src/Core/Highscores/HighscoreTypes.cs index 9901c98..3aa396e 100644 --- a/src/Core/Highscores/HighscoreTypes.cs +++ b/src/Core/Highscores/HighscoreTypes.cs @@ -6,6 +6,7 @@ karma, rolls, cookies, - seasons + seasons, + quotes } } \ No newline at end of file From d477a4b056e209c4462fc369e311ec7044afb3e7 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Wed, 30 Dec 2020 23:33:27 +0100 Subject: [PATCH 305/443] Update the translations for !rank with the new rank types --- src/Bot/Localization/Rank.Designer.cs | 2 +- src/Bot/Localization/Rank.de-ch.resx | 2 +- src/Bot/Localization/Rank.resx | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Bot/Localization/Rank.Designer.cs b/src/Bot/Localization/Rank.Designer.cs index 23f5e16..6d54030 100644 --- a/src/Bot/Localization/Rank.Designer.cs +++ b/src/Bot/Localization/Rank.Designer.cs @@ -79,7 +79,7 @@ namespace Geekbot.Bot.Localization { } /// - /// Looks up a localized string similar to Valid types are '`messages`' '`karma`', '`rolls`' and '`cookies`'. + /// Looks up a localized string similar to Valid types are '`messages`' '`karma`', '`rolls`', '`cookies`', '`seasons`' and '`quotes`'. /// internal static string InvalidType { get { diff --git a/src/Bot/Localization/Rank.de-ch.resx b/src/Bot/Localization/Rank.de-ch.resx index 0b22fe4..743b8cd 100644 --- a/src/Bot/Localization/Rank.de-ch.resx +++ b/src/Bot/Localization/Rank.de-ch.resx @@ -24,6 +24,6 @@ :bar_chart: **{0} Highscore für {1}**
- Gültigi paramenter sind '`messages`' '`karma`', '`rolls`' und '`cookies` + Gültigi paramenter sind '`messages`' '`karma`', '`rolls`', '`cookies`', '`seasons`' und '`quotes`' \ No newline at end of file diff --git a/src/Bot/Localization/Rank.resx b/src/Bot/Localization/Rank.resx index 9598cf8..606a34e 100644 --- a/src/Bot/Localization/Rank.resx +++ b/src/Bot/Localization/Rank.resx @@ -19,7 +19,7 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - Valid types are '`messages`' '`karma`', '`rolls`' and '`cookies`' + Valid types are '`messages`' '`karma`', '`rolls`', '`cookies`', '`seasons`' and '`quotes`' :warning: Limiting to 20 From e495e2df17178bf0f444fe5a9bc0c73ad19dc920 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Fri, 1 Jan 2021 02:48:28 +0100 Subject: [PATCH 306/443] Use the correct unit when listing messages in a season --- src/Bot/Commands/User/Ranking/Rank.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Bot/Commands/User/Ranking/Rank.cs b/src/Bot/Commands/User/Ranking/Rank.cs index 14a7650..2f5f841 100644 --- a/src/Bot/Commands/User/Ranking/Rank.cs +++ b/src/Bot/Commands/User/Ranking/Rank.cs @@ -106,10 +106,13 @@ namespace Geekbot.Bot.Commands.User.Ranking replyBuilder.Append(user.Username != null ? $"**{user.Username}#{user.Discriminator}**" : $"**{user.Id}**"); - - replyBuilder.Append(type == HighscoreTypes.messages - ? $" - {value} {type} - {Math.Round((double) (100 * value) / guildMessages, 2)}%\n" - : $" - {value} {type}\n"); + + replyBuilder.Append(type switch + { + HighscoreTypes.messages => $" - {value} {HighscoreTypes.messages} - {Math.Round((double) (100 * value) / guildMessages, 2)}%\n", + HighscoreTypes.seasons => $" - {value} {HighscoreTypes.messages}\n", + _ => $" - {value} {type}\n" + }); highscorePlace++; } From 21303bfca81015af03e02d3a92f15e86f98d00ba Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 1 Jan 2021 18:06:40 +0100 Subject: [PATCH 307/443] Remove code in the stats handler that checks if 2021 has started --- src/Bot/Handlers/StatsHandler.cs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/Bot/Handlers/StatsHandler.cs b/src/Bot/Handlers/StatsHandler.cs index 5ee7871..d1384cd 100644 --- a/src/Bot/Handlers/StatsHandler.cs +++ b/src/Bot/Handlers/StatsHandler.cs @@ -17,15 +17,12 @@ namespace Geekbot.Bot.Handlers private readonly IGeekbotLogger _logger; private readonly DatabaseContext _database; private string _season; - // ToDo: Clean up in 2021 - private bool _seasonsStarted; public StatsHandler(IGeekbotLogger logger, DatabaseContext database) { _logger = logger; _database = database; _season = SeasonsUtils.GetCurrentSeason(); - _seasonsStarted = DateTime.Now.Year == 2021; var timer = new Timer() { @@ -38,7 +35,6 @@ namespace Geekbot.Bot.Handlers var current = SeasonsUtils.GetCurrentSeason(); if (current == _season) return; _season = SeasonsUtils.GetCurrentSeason(); - _seasonsStarted = DateTime.Now.Year == 2021; }; } @@ -62,11 +58,8 @@ namespace Geekbot.Bot.Handlers return; } - await UpdateTotalTable(message, channel); - if (_seasonsStarted) - { - await UpdateSeasonsTable(message, channel); - } + await UpdateTotalTable(message, channel); + await UpdateSeasonsTable(message, channel); if (message.Author.IsBot) return; From 04343352395a6f015e2acf919f64e5de2f5aca92 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Sun, 24 Jan 2021 22:15:15 +0100 Subject: [PATCH 308/443] Fix !corona by changing data source to covid19-api.org, added a country code parameter as well --- .../Corona/CoronaApiCountryResponseDto.cs | 19 +++++ src/Bot/Commands/Utils/Corona/CoronaStats.cs | 72 +++++++++++++++---- .../Commands/Utils/Corona/CoronaSummaryDto.cs | 9 --- .../Commands/Utils/Corona/CoronaTotalDto.cs | 9 +++ 4 files changed, 87 insertions(+), 22 deletions(-) create mode 100644 src/Bot/Commands/Utils/Corona/CoronaApiCountryResponseDto.cs delete mode 100644 src/Bot/Commands/Utils/Corona/CoronaSummaryDto.cs create mode 100644 src/Bot/Commands/Utils/Corona/CoronaTotalDto.cs diff --git a/src/Bot/Commands/Utils/Corona/CoronaApiCountryResponseDto.cs b/src/Bot/Commands/Utils/Corona/CoronaApiCountryResponseDto.cs new file mode 100644 index 0000000..84da7f0 --- /dev/null +++ b/src/Bot/Commands/Utils/Corona/CoronaApiCountryResponseDto.cs @@ -0,0 +1,19 @@ +using Newtonsoft.Json; + +namespace Geekbot.Bot.Commands.Utils.Corona +{ + public record CoronaApiCountryResponseDto + { + [JsonProperty("country")] + public string Country { get; init; } + + [JsonProperty("cases")] + public decimal Cases { get; init; } + + [JsonProperty("deaths")] + public decimal Deaths { get; init; } + + [JsonProperty("recovered")] + public decimal Recovered { get; init; } + } +} \ No newline at end of file diff --git a/src/Bot/Commands/Utils/Corona/CoronaStats.cs b/src/Bot/Commands/Utils/Corona/CoronaStats.cs index 3f9f3ea..5880030 100644 --- a/src/Bot/Commands/Utils/Corona/CoronaStats.cs +++ b/src/Bot/Commands/Utils/Corona/CoronaStats.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; @@ -11,7 +13,7 @@ namespace Geekbot.Bot.Commands.Utils.Corona public class CoronaStats : ModuleBase { private readonly IErrorHandler _errorHandler; - + public CoronaStats(IErrorHandler errorHandler) { _errorHandler = errorHandler; @@ -19,24 +21,30 @@ namespace Geekbot.Bot.Commands.Utils.Corona [Command("corona", RunMode = RunMode.Async)] [Summary("Get the latest worldwide corona statistics")] - public async Task Summary() + public async Task Summary([Summary("CountryCode")] string countryCode = null) { try { - var summary = await HttpAbstractions.Get(new Uri("https://api.covid19api.com/world/total")); - var activeCases = summary.TotalConfirmed - (summary.TotalRecovered + summary.TotalDeaths); + var summary = await GetCoronaInfo(countryCode); + if (summary == null) + { + await Context.Channel.SendMessageAsync($"`{countryCode}` is not a valid country code"); + return; + } + + var activeCases = summary.Cases - (summary.Recovered + summary.Deaths); - string CalculatePercentage(decimal i) => (i / summary.TotalConfirmed).ToString("#0.##%"); + string CalculatePercentage(decimal i) => (i / summary.Cases).ToString("#0.##%"); var activePercent = CalculatePercentage(activeCases); - var recoveredPercentage = CalculatePercentage(summary.TotalRecovered); - var deathsPercentage = CalculatePercentage(summary.TotalDeaths); + var recoveredPercentage = CalculatePercentage(summary.Recovered); + var deathsPercentage = CalculatePercentage(summary.Deaths); var numberFormat = "#,#"; - var totalFormatted = summary.TotalConfirmed.ToString(numberFormat); + var totalFormatted = summary.Cases.ToString(numberFormat); var activeFormatted = activeCases.ToString(numberFormat); - var recoveredFormatted = summary.TotalRecovered.ToString(numberFormat); - var deathsFormatted = summary.TotalDeaths.ToString(numberFormat); - + var recoveredFormatted = summary.Recovered.ToString(numberFormat); + var deathsFormatted = summary.Deaths.ToString(numberFormat); + var eb = new EmbedBuilder { Author = new EmbedAuthorBuilder @@ -46,7 +54,7 @@ namespace Geekbot.Bot.Commands.Utils.Corona }, Footer = new EmbedFooterBuilder { - Text = "Source: covid19api.com", + Text = "Source: covid19-api.org", }, Color = Color.Red }; @@ -54,7 +62,7 @@ namespace Geekbot.Bot.Commands.Utils.Corona eb.AddInlineField("Active", $"{activeFormatted} ({activePercent})"); eb.AddInlineField("Recovered", $"{recoveredFormatted} ({recoveredPercentage})"); eb.AddInlineField("Deaths", $"{deathsFormatted} ({deathsPercentage})"); - + await Context.Channel.SendMessageAsync(String.Empty, false, eb.Build()); } catch (Exception e) @@ -62,5 +70,43 @@ namespace Geekbot.Bot.Commands.Utils.Corona await _errorHandler.HandleCommandException(e, Context); } } + + private async Task GetCoronaInfo(string countryCode = null) + { + var allCountries = await HttpAbstractions.Get>(new Uri("https://covid19-api.org/api/status")); + + if (string.IsNullOrEmpty(countryCode)) + { + return allCountries.Aggregate( + new CoronaTotalDto(), + (accumulate, source) => + { + accumulate.Cases += source.Cases; + accumulate.Deaths += source.Deaths; + accumulate.Recovered += source.Recovered; + return accumulate; + } + ); + } + + if (countryCode.Length != 2) + { + return null; + } + + var upcasedCountryCode = countryCode.ToUpper(); + var countryStats = allCountries.Find(x => x.Country == upcasedCountryCode); + if (countryStats == null) + { + return null; + } + + return new CoronaTotalDto() + { + Cases = countryStats.Cases, + Deaths = countryStats.Deaths, + Recovered = countryStats.Recovered, + }; + } } } \ No newline at end of file diff --git a/src/Bot/Commands/Utils/Corona/CoronaSummaryDto.cs b/src/Bot/Commands/Utils/Corona/CoronaSummaryDto.cs deleted file mode 100644 index 3f6a820..0000000 --- a/src/Bot/Commands/Utils/Corona/CoronaSummaryDto.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace Geekbot.Bot.Commands.Utils.Corona -{ - public class CoronaSummaryDto - { - public decimal TotalConfirmed { get; set; } - public decimal TotalDeaths { get; set; } - public decimal TotalRecovered { get; set; } - } -} \ No newline at end of file diff --git a/src/Bot/Commands/Utils/Corona/CoronaTotalDto.cs b/src/Bot/Commands/Utils/Corona/CoronaTotalDto.cs new file mode 100644 index 0000000..c135927 --- /dev/null +++ b/src/Bot/Commands/Utils/Corona/CoronaTotalDto.cs @@ -0,0 +1,9 @@ +namespace Geekbot.Bot.Commands.Utils.Corona +{ + public record CoronaTotalDto + { + public decimal Cases { get; set; } + public decimal Deaths { get; set; } + public decimal Recovered { get; set; } + } +} \ No newline at end of file From 4fd62e9184145db1f76f33ebc3762c782e7fcde5 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Sun, 24 Jan 2021 22:20:59 +0100 Subject: [PATCH 309/443] Make sure that the build version suffix is not a number --- .gitlab-ci.yml | 4 ++-- src/Bot/Bot.csproj | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index c4d8b15..20b1c84 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -5,7 +5,7 @@ stages: - ops variables: - VERSION: 4.3.0-$CI_COMMIT_SHORT_SHA + VERSION: 4.3.0-V$CI_COMMIT_SHORT_SHA IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG Build: @@ -18,7 +18,7 @@ Build: script: - dotnet restore - dotnet test tests - - dotnet publish --version-suffix $VERSION -r linux-x64 -c Release -o ./app ./src/Bot/ + - dotnet publish --version-suffix "$VERSION" -r linux-x64 -c Release -o ./app ./src/Bot/ Package: stage: docker diff --git a/src/Bot/Bot.csproj b/src/Bot/Bot.csproj index 4cd5b39..ec32e8f 100644 --- a/src/Bot/Bot.csproj +++ b/src/Bot/Bot.csproj @@ -7,7 +7,7 @@ $(VersionSuffix) Geekbot.Bot Geekbot - $(VersionSuffix) + $(VersionSuffix) 0.0.0-DEV Pizza and Coffee Studios Pizza and Coffee Studios From bbb9b894227f6f136bddfa1f8b418166d623d82d Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Mon, 25 Jan 2021 00:48:42 +0100 Subject: [PATCH 310/443] Add Support for emoji flags in the emoji converter --- src/Core/Converters/EmojiConverter.cs | 166 ++++++++++++++++--------- src/Core/Converters/IEmojiConverter.cs | 1 + 2 files changed, 107 insertions(+), 60 deletions(-) diff --git a/src/Core/Converters/EmojiConverter.cs b/src/Core/Converters/EmojiConverter.cs index 327aab8..7527321 100644 --- a/src/Core/Converters/EmojiConverter.cs +++ b/src/Core/Converters/EmojiConverter.cs @@ -1,90 +1,136 @@ using System.Collections; +using System.Net.NetworkInformation; using System.Text; namespace Geekbot.Core.Converters { public class EmojiConverter : IEmojiConverter { + private readonly string[] NumberEmojiMap = + { + ":zero:", + ":one:", + ":two:", + ":three:", + ":four:", + ":five:", + ":six:", + ":seven:", + ":eight:", + ":nine:" + }; + public string NumberToEmoji(int number) { if (number == 10) { return "🔟"; } - var emojiMap = new[] - { - ":zero:", - ":one:", - ":two:", - ":three:", - ":four:", - ":five:", - ":six:", - ":seven:", - ":eight:", - ":nine:" - }; + var numbers = number.ToString().ToCharArray(); var returnString = new StringBuilder(); foreach (var n in numbers) { - returnString.Append(emojiMap[int.Parse(n.ToString())]); + returnString.Append(NumberEmojiMap[int.Parse(n.ToString())]); } return returnString.ToString(); } + private readonly Hashtable TextEmojiMap = new Hashtable + { + ['A'] = ":regional_indicator_a: ", + ['B'] = ":b: ", + ['C'] = ":regional_indicator_c: ", + ['D'] = ":regional_indicator_d: ", + ['E'] = ":regional_indicator_e: ", + ['F'] = ":regional_indicator_f: ", + ['G'] = ":regional_indicator_g: ", + ['H'] = ":regional_indicator_h: ", + ['I'] = ":regional_indicator_i: ", + ['J'] = ":regional_indicator_j: ", + ['K'] = ":regional_indicator_k: ", + ['L'] = ":regional_indicator_l: ", + ['M'] = ":regional_indicator_m: ", + ['N'] = ":regional_indicator_n: ", + ['O'] = ":regional_indicator_o: ", + ['P'] = ":regional_indicator_p: ", + ['Q'] = ":regional_indicator_q: ", + ['R'] = ":regional_indicator_r: ", + ['S'] = ":regional_indicator_s: ", + ['T'] = ":regional_indicator_t: ", + ['U'] = ":regional_indicator_u: ", + ['V'] = ":regional_indicator_v: ", + ['W'] = ":regional_indicator_w: ", + ['X'] = ":regional_indicator_x: ", + ['Y'] = ":regional_indicator_y: ", + ['Z'] = ":regional_indicator_z: ", + ['!'] = ":exclamation: ", + ['?'] = ":question: ", + ['#'] = ":hash: ", + ['*'] = ":star2: ", + ['+'] = ":heavy_plus_sign: ", + ['0'] = ":zero: ", + ['1'] = ":one: ", + ['2'] = ":two: ", + ['3'] = ":three: ", + ['4'] = ":four: ", + ['5'] = ":five: ", + ['6'] = ":six: ", + ['7'] = ":seven: ", + ['8'] = ":eight: ", + ['9'] = ":nine: ", + [' '] = " " + }; + public string TextToEmoji(string text) { - var emojiMap = new Hashtable - { - ['A'] = ":regional_indicator_a: ", - ['B'] = ":b: ", - ['C'] = ":regional_indicator_c: ", - ['D'] = ":regional_indicator_d: ", - ['E'] = ":regional_indicator_e: ", - ['F'] = ":regional_indicator_f: ", - ['G'] = ":regional_indicator_g: ", - ['H'] = ":regional_indicator_h: ", - ['I'] = ":regional_indicator_i: ", - ['J'] = ":regional_indicator_j: ", - ['K'] = ":regional_indicator_k: ", - ['L'] = ":regional_indicator_l: ", - ['M'] = ":regional_indicator_m: ", - ['N'] = ":regional_indicator_n: ", - ['O'] = ":regional_indicator_o: ", - ['P'] = ":regional_indicator_p: ", - ['Q'] = ":regional_indicator_q: ", - ['R'] = ":regional_indicator_r: ", - ['S'] = ":regional_indicator_s: ", - ['T'] = ":regional_indicator_t: ", - ['U'] = ":regional_indicator_u: ", - ['V'] = ":regional_indicator_v: ", - ['W'] = ":regional_indicator_w: ", - ['X'] = ":regional_indicator_x: ", - ['Y'] = ":regional_indicator_y: ", - ['Z'] = ":regional_indicator_z: ", - ['!'] = ":exclamation: ", - ['?'] = ":question: ", - ['#'] = ":hash: ", - ['*'] = ":star2: ", - ['+'] = ":heavy_plus_sign: ", - ['0'] = ":zero: ", - ['1'] = ":one: ", - ['2'] = ":two: ", - ['3'] = ":three: ", - ['4'] = ":four: ", - ['5'] = ":five: ", - ['6'] = ":six: ", - ['7'] = ":seven: ", - ['8'] = ":eight: ", - ['9'] = ":nine: ", - [' '] = " " - }; var letters = text.ToUpper().ToCharArray(); var returnString = new StringBuilder(); foreach (var n in letters) { - var emoji = emojiMap[n] ?? n; + var emoji = TextEmojiMap[n] ?? n; + returnString.Append(emoji); + } + return returnString.ToString(); + } + + private readonly Hashtable RegionalIndicatorMap = new Hashtable() + { + ['A'] = new Rune(0x1F1E6), + ['B'] = new Rune(0x1F1E7), + ['C'] = new Rune(0x1F1E8), + ['D'] = new Rune(0x1F1E9), + ['E'] = new Rune(0x1F1EA), + ['F'] = new Rune(0x1F1EB), + ['G'] = new Rune(0x1F1EC), + ['H'] = new Rune(0x1F1ED), + ['I'] = new Rune(0x1F1EE), + ['J'] = new Rune(0x1F1EF), + ['K'] = new Rune(0x1F1F0), + ['L'] = new Rune(0x1F1F1), + ['M'] = new Rune(0x1F1F2), + ['N'] = new Rune(0x1F1F3), + ['O'] = new Rune(0x1F1F4), + ['P'] = new Rune(0x1F1F5), + ['Q'] = new Rune(0x1F1F6), + ['R'] = new Rune(0x1F1F7), + ['S'] = new Rune(0x1F1F8), + ['T'] = new Rune(0x1F1F9), + ['U'] = new Rune(0x1F1FA), + ['V'] = new Rune(0x1F1FB), + ['W'] = new Rune(0x1F1FC), + ['X'] = new Rune(0x1F1FD), + ['Y'] = new Rune(0x1F1FE), + ['Z'] = new Rune(0x1F1FF) + }; + + public string CountryCodeToEmoji(string countryCode) + { + var letters = countryCode.ToUpper().ToCharArray(); + var returnString = new StringBuilder(); + foreach (var n in letters) + { + var emoji = RegionalIndicatorMap[n]; returnString.Append(emoji); } return returnString.ToString(); diff --git a/src/Core/Converters/IEmojiConverter.cs b/src/Core/Converters/IEmojiConverter.cs index 79ca0a7..0c364ea 100644 --- a/src/Core/Converters/IEmojiConverter.cs +++ b/src/Core/Converters/IEmojiConverter.cs @@ -4,5 +4,6 @@ { string NumberToEmoji(int number); string TextToEmoji(string text); + string CountryCodeToEmoji(string countryCode); } } \ No newline at end of file From 644d877e29adda6752be60578281572ca648138b Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Mon, 25 Jan 2021 00:49:18 +0100 Subject: [PATCH 311/443] Show country flag when !corona has a country code parameter --- src/Bot/Commands/Utils/Corona/CoronaStats.cs | 17 +++++++++++++++-- src/Bot/Commands/Utils/Corona/CoronaTotalDto.cs | 1 + 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Bot/Commands/Utils/Corona/CoronaStats.cs b/src/Bot/Commands/Utils/Corona/CoronaStats.cs index 5880030..15431d2 100644 --- a/src/Bot/Commands/Utils/Corona/CoronaStats.cs +++ b/src/Bot/Commands/Utils/Corona/CoronaStats.cs @@ -1,10 +1,12 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Text; using System.Threading.Tasks; using Discord; using Discord.Commands; using Geekbot.Core; +using Geekbot.Core.Converters; using Geekbot.Core.ErrorHandling; using Geekbot.Core.Extensions; @@ -13,10 +15,12 @@ namespace Geekbot.Bot.Commands.Utils.Corona public class CoronaStats : ModuleBase { private readonly IErrorHandler _errorHandler; + private readonly IEmojiConverter _emojiConverter; - public CoronaStats(IErrorHandler errorHandler) + public CoronaStats(IErrorHandler errorHandler, IEmojiConverter emojiConverter) { _errorHandler = errorHandler; + _emojiConverter = emojiConverter; } [Command("corona", RunMode = RunMode.Async)] @@ -45,11 +49,19 @@ namespace Geekbot.Bot.Commands.Utils.Corona var recoveredFormatted = summary.Recovered.ToString(numberFormat); var deathsFormatted = summary.Deaths.ToString(numberFormat); + var embedTitleBuilder = new StringBuilder(); + embedTitleBuilder.Append("Confirmed Corona Cases"); + if (!string.IsNullOrEmpty(summary.Country)) + { + embedTitleBuilder.Append(" - "); + embedTitleBuilder.Append(_emojiConverter.CountryCodeToEmoji(summary.Country)); + } + var eb = new EmbedBuilder { Author = new EmbedAuthorBuilder { - Name = "Confirmed Corona Cases", + Name = embedTitleBuilder.ToString(), IconUrl = "https://www.redcross.org/content/dam/icons/disasters/virus/Virus-1000x1000-R-Pl.png" }, Footer = new EmbedFooterBuilder @@ -103,6 +115,7 @@ namespace Geekbot.Bot.Commands.Utils.Corona return new CoronaTotalDto() { + Country = upcasedCountryCode, Cases = countryStats.Cases, Deaths = countryStats.Deaths, Recovered = countryStats.Recovered, diff --git a/src/Bot/Commands/Utils/Corona/CoronaTotalDto.cs b/src/Bot/Commands/Utils/Corona/CoronaTotalDto.cs index c135927..58e5ac9 100644 --- a/src/Bot/Commands/Utils/Corona/CoronaTotalDto.cs +++ b/src/Bot/Commands/Utils/Corona/CoronaTotalDto.cs @@ -2,6 +2,7 @@ namespace Geekbot.Bot.Commands.Utils.Corona { public record CoronaTotalDto { + public string Country { get; set; } public decimal Cases { get; set; } public decimal Deaths { get; set; } public decimal Recovered { get; set; } From eddd005d34159ab7890c2795707157d4bef5d6c4 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Mon, 25 Jan 2021 01:40:51 +0100 Subject: [PATCH 312/443] Add translations for !corona --- src/Bot/Bot.csproj | 9 ++ src/Bot/Commands/Utils/Corona/CoronaStats.cs | 21 ++-- src/Bot/Localization/Corona.Designer.cs | 117 +++++++++++++++++++ src/Bot/Localization/Corona.de-ch.resx | 32 +++++ src/Bot/Localization/Corona.resx | 39 +++++++ 5 files changed, 207 insertions(+), 11 deletions(-) create mode 100644 src/Bot/Localization/Corona.Designer.cs create mode 100644 src/Bot/Localization/Corona.de-ch.resx create mode 100644 src/Bot/Localization/Corona.resx diff --git a/src/Bot/Bot.csproj b/src/Bot/Bot.csproj index ec32e8f..6c6f56f 100644 --- a/src/Bot/Bot.csproj +++ b/src/Bot/Bot.csproj @@ -84,6 +84,10 @@ ResXFileCodeGenerator Stats.Designer.cs + + ResXFileCodeGenerator + Corona.Designer.cs +
@@ -144,5 +148,10 @@ True Stats.resx + + True + True + Corona.resx +
diff --git a/src/Bot/Commands/Utils/Corona/CoronaStats.cs b/src/Bot/Commands/Utils/Corona/CoronaStats.cs index 15431d2..e39be0a 100644 --- a/src/Bot/Commands/Utils/Corona/CoronaStats.cs +++ b/src/Bot/Commands/Utils/Corona/CoronaStats.cs @@ -9,17 +9,16 @@ using Geekbot.Core; using Geekbot.Core.Converters; using Geekbot.Core.ErrorHandling; using Geekbot.Core.Extensions; +using Geekbot.Core.GuildSettingsManager; namespace Geekbot.Bot.Commands.Utils.Corona { - public class CoronaStats : ModuleBase + public class CoronaStats : GeekbotCommandBase { - private readonly IErrorHandler _errorHandler; private readonly IEmojiConverter _emojiConverter; - public CoronaStats(IErrorHandler errorHandler, IEmojiConverter emojiConverter) + public CoronaStats(IErrorHandler errorHandler, IGuildSettingsManager guildSettingsManager, IEmojiConverter emojiConverter) : base(errorHandler, guildSettingsManager) { - _errorHandler = errorHandler; _emojiConverter = emojiConverter; } @@ -50,7 +49,7 @@ namespace Geekbot.Bot.Commands.Utils.Corona var deathsFormatted = summary.Deaths.ToString(numberFormat); var embedTitleBuilder = new StringBuilder(); - embedTitleBuilder.Append("Confirmed Corona Cases"); + embedTitleBuilder.Append(Localization.Corona.ConfirmedCases); if (!string.IsNullOrEmpty(summary.Country)) { embedTitleBuilder.Append(" - "); @@ -66,20 +65,20 @@ namespace Geekbot.Bot.Commands.Utils.Corona }, Footer = new EmbedFooterBuilder { - Text = "Source: covid19-api.org", + Text = $"{Localization.Corona.Source}: covid19-api.org", }, Color = Color.Red }; - eb.AddField("Total", totalFormatted); - eb.AddInlineField("Active", $"{activeFormatted} ({activePercent})"); - eb.AddInlineField("Recovered", $"{recoveredFormatted} ({recoveredPercentage})"); - eb.AddInlineField("Deaths", $"{deathsFormatted} ({deathsPercentage})"); + eb.AddField(Localization.Corona.Total, totalFormatted); + eb.AddInlineField(Localization.Corona.Active, $"{activeFormatted} ({activePercent})"); + eb.AddInlineField(Localization.Corona.Recovered, $"{recoveredFormatted} ({recoveredPercentage})"); + eb.AddInlineField(Localization.Corona.Deaths, $"{deathsFormatted} ({deathsPercentage})"); await Context.Channel.SendMessageAsync(String.Empty, false, eb.Build()); } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } diff --git a/src/Bot/Localization/Corona.Designer.cs b/src/Bot/Localization/Corona.Designer.cs new file mode 100644 index 0000000..b243bfc --- /dev/null +++ b/src/Bot/Localization/Corona.Designer.cs @@ -0,0 +1,117 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Geekbot.Bot.Localization { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Corona { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Corona() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Geekbot.Bot.Localization.Corona", typeof(Corona).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to Active. + /// + internal static string Active { + get { + return ResourceManager.GetString("Active", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Confirmed Corona Cases. + /// + internal static string ConfirmedCases { + get { + return ResourceManager.GetString("ConfirmedCases", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Deaths. + /// + internal static string Deaths { + get { + return ResourceManager.GetString("Deaths", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Recovered. + /// + internal static string Recovered { + get { + return ResourceManager.GetString("Recovered", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Source. + /// + internal static string Source { + get { + return ResourceManager.GetString("Source", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Total. + /// + internal static string Total { + get { + return ResourceManager.GetString("Total", resourceCulture); + } + } + } +} diff --git a/src/Bot/Localization/Corona.de-ch.resx b/src/Bot/Localization/Corona.de-ch.resx new file mode 100644 index 0000000..3c4180c --- /dev/null +++ b/src/Bot/Localization/Corona.de-ch.resx @@ -0,0 +1,32 @@ + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Bstätigti Corona Fallzahle + + + Total + + + Aktiv + + + Erholt + + + Gstorbe + + + Quelle + + \ No newline at end of file diff --git a/src/Bot/Localization/Corona.resx b/src/Bot/Localization/Corona.resx new file mode 100644 index 0000000..44bf85e --- /dev/null +++ b/src/Bot/Localization/Corona.resx @@ -0,0 +1,39 @@ + + + + + + + + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Confirmed Corona Cases + + + Total + + + Active + + + Recovered + + + Deaths + + + Source + + \ No newline at end of file From c1b8394e1b092d9d7b459f2969cad5ebb21e1b24 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Thu, 18 Mar 2021 12:06:41 +0100 Subject: [PATCH 313/443] Add a !neutral command for karma, it does nothing. --- src/Bot/Commands/User/Karma.cs | 41 ++++++++++++++++++++++++++ src/Bot/Localization/Karma.Designer.cs | 9 ++++++ src/Bot/Localization/Karma.de-ch.resx | 3 ++ src/Bot/Localization/Karma.resx | 3 ++ 4 files changed, 56 insertions(+) diff --git a/src/Bot/Commands/User/Karma.cs b/src/Bot/Commands/User/Karma.cs index 4778bce..3ba8650 100644 --- a/src/Bot/Commands/User/Karma.cs +++ b/src/Bot/Commands/User/Karma.cs @@ -115,6 +115,47 @@ namespace Geekbot.Bot.Commands.User await ErrorHandler.HandleCommandException(e, Context); } } + + [Command("neutral", RunMode = RunMode.Async)] + [Summary("Do nothing to someones karma")] + public async Task Neutral([Summary("@someone")] IUser user) + { + try + { + var actor = await GetUser(Context.User.Id); + if (user.Id == Context.User.Id) + { + await ReplyAsync(string.Format(Localization.Karma.CannotChangeOwnDown, Context.User.Username)); + return; + } + + if (TimeoutFinished(actor.TimeOut)) + { + var formatedWaitTime = DateLocalization.FormatDateTimeAsRemaining(actor.TimeOut.AddMinutes(3)); + await ReplyAsync(string.Format(Localization.Karma.WaitUntill, Context.User.Username, formatedWaitTime)); + return; + } + + var target = await GetUser(user.Id); + + var eb = new EmbedBuilder(); + eb.WithAuthor(new EmbedAuthorBuilder() + .WithIconUrl(user.GetAvatarUrl()) + .WithName(user.Username)); + + eb.WithColor(new Color(138, 219, 146)); + eb.Title = Localization.Karma.Neutral; + eb.AddInlineField(Localization.Karma.By, Context.User.Username); + eb.AddInlineField(Localization.Karma.Amount, "0"); + eb.AddInlineField(Localization.Karma.Current, target.Karma); + await ReplyAsync("", false, eb.Build()); + + } + catch (Exception e) + { + await ErrorHandler.HandleCommandException(e, Context); + } + } private bool TimeoutFinished(DateTimeOffset lastKarma) { diff --git a/src/Bot/Localization/Karma.Designer.cs b/src/Bot/Localization/Karma.Designer.cs index 4191fa2..71cab3b 100644 --- a/src/Bot/Localization/Karma.Designer.cs +++ b/src/Bot/Localization/Karma.Designer.cs @@ -123,6 +123,15 @@ namespace Geekbot.Bot.Localization { } } + /// + /// Looks up a localized string similar to Neutral Karma. + /// + internal static string Neutral { + get { + return ResourceManager.GetString("Neutral", resourceCulture); + } + } + /// /// Looks up a localized string similar to Sorry {0}, but you have to wait {1} before you can give karma again.... /// diff --git a/src/Bot/Localization/Karma.de-ch.resx b/src/Bot/Localization/Karma.de-ch.resx index 5605f8b..7b739b7 100644 --- a/src/Bot/Localization/Karma.de-ch.resx +++ b/src/Bot/Localization/Karma.de-ch.resx @@ -35,4 +35,7 @@ Karma gsenkt + + Neutral Karma + \ No newline at end of file diff --git a/src/Bot/Localization/Karma.resx b/src/Bot/Localization/Karma.resx index 3a8fe5a..2ebea1d 100644 --- a/src/Bot/Localization/Karma.resx +++ b/src/Bot/Localization/Karma.resx @@ -42,4 +42,7 @@ Karma lowered + + Neutral Karma + \ No newline at end of file From 6c142f41d3b16582cd5bcaa4dd871ca522b43698 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Thu, 18 Mar 2021 12:23:18 +0100 Subject: [PATCH 314/443] Upgrade discord.net --- src/Core/Core.csproj | 2 +- src/Core/Polyfills/UserPolyfillDto.cs | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index 780d0e2..0a4e989 100644 --- a/src/Core/Core.csproj +++ b/src/Core/Core.csproj @@ -13,7 +13,7 @@ - + diff --git a/src/Core/Polyfills/UserPolyfillDto.cs b/src/Core/Polyfills/UserPolyfillDto.cs index 1d3a54b..385a30d 100644 --- a/src/Core/Polyfills/UserPolyfillDto.cs +++ b/src/Core/Polyfills/UserPolyfillDto.cs @@ -13,6 +13,7 @@ namespace Geekbot.Core.Polyfills public IActivity Activity { get; } public UserStatus Status { get; set; } public IImmutableSet ActiveClients { get; } + public IImmutableList Activities { get; } public string AvatarId { get; set; } public string AvatarUrl { get; set; } public string Discriminator { get; set; } @@ -20,7 +21,8 @@ namespace Geekbot.Core.Polyfills public bool IsBot { get; set; } public bool IsWebhook { get; set; } public string Username { get; set; } - + public UserProperties? PublicFlags { get; } + public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128) { return AvatarUrl ?? "https://discordapp.com/assets/6debd47ed13483642cf09e832ed0bc1b.png"; From c77b501b6c6d6fd3492a881686fca4c801311921 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Thu, 18 Mar 2021 12:27:26 +0100 Subject: [PATCH 315/443] Fix message sent when user is trying give themselves !neutral karma --- src/Bot/Commands/User/Karma.cs | 2 +- src/Bot/Localization/Karma.Designer.cs | 9 +++++++++ src/Bot/Localization/Karma.de-ch.resx | 3 +++ src/Bot/Localization/Karma.resx | 3 +++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Bot/Commands/User/Karma.cs b/src/Bot/Commands/User/Karma.cs index 3ba8650..61e381e 100644 --- a/src/Bot/Commands/User/Karma.cs +++ b/src/Bot/Commands/User/Karma.cs @@ -125,7 +125,7 @@ namespace Geekbot.Bot.Commands.User var actor = await GetUser(Context.User.Id); if (user.Id == Context.User.Id) { - await ReplyAsync(string.Format(Localization.Karma.CannotChangeOwnDown, Context.User.Username)); + await ReplyAsync(string.Format(Localization.Karma.CannotChangeOwnSame, Context.User.Username)); return; } diff --git a/src/Bot/Localization/Karma.Designer.cs b/src/Bot/Localization/Karma.Designer.cs index 71cab3b..041fec4 100644 --- a/src/Bot/Localization/Karma.Designer.cs +++ b/src/Bot/Localization/Karma.Designer.cs @@ -87,6 +87,15 @@ namespace Geekbot.Bot.Localization { } } + /// + /// Looks up a localized string similar to Sorry {0}, but you can't give yourself neutral karma. + /// + internal static string CannotChangeOwnSame { + get { + return ResourceManager.GetString("CannotChangeOwnSame", resourceCulture); + } + } + /// /// Looks up a localized string similar to Sorry {0}, but you can't give yourself karma. /// diff --git a/src/Bot/Localization/Karma.de-ch.resx b/src/Bot/Localization/Karma.de-ch.resx index 7b739b7..5805a27 100644 --- a/src/Bot/Localization/Karma.de-ch.resx +++ b/src/Bot/Localization/Karma.de-ch.resx @@ -38,4 +38,7 @@ Neutral Karma + + Sorry {0}, aber du chasch dr selber kei neutrals karma geh + \ No newline at end of file diff --git a/src/Bot/Localization/Karma.resx b/src/Bot/Localization/Karma.resx index 2ebea1d..a16e14a 100644 --- a/src/Bot/Localization/Karma.resx +++ b/src/Bot/Localization/Karma.resx @@ -45,4 +45,7 @@ Neutral Karma + + Sorry {0}, but you can't give yourself neutral karma + \ No newline at end of file From d1d57ba7144e497c9fcf7991060e7bfcee6d5d6c Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Thu, 18 Mar 2021 23:53:56 +0100 Subject: [PATCH 316/443] Refactor karma commands --- src/Bot/Commands/User/Karma.cs | 190 --------------------- src/Bot/Commands/User/Karma/Karma.cs | 137 +++++++++++++++ src/Bot/Commands/User/Karma/KarmaChange.cs | 9 + 3 files changed, 146 insertions(+), 190 deletions(-) delete mode 100644 src/Bot/Commands/User/Karma.cs create mode 100644 src/Bot/Commands/User/Karma/Karma.cs create mode 100644 src/Bot/Commands/User/Karma/KarmaChange.cs diff --git a/src/Bot/Commands/User/Karma.cs b/src/Bot/Commands/User/Karma.cs deleted file mode 100644 index 61e381e..0000000 --- a/src/Bot/Commands/User/Karma.cs +++ /dev/null @@ -1,190 +0,0 @@ -using System; -using System.Linq; -using System.Threading.Tasks; -using Discord; -using Discord.Commands; -using Geekbot.Bot.Utils; -using Geekbot.Core; -using Geekbot.Core.CommandPreconditions; -using Geekbot.Core.Database; -using Geekbot.Core.Database.Models; -using Geekbot.Core.ErrorHandling; -using Geekbot.Core.Extensions; -using Geekbot.Core.GuildSettingsManager; - -namespace Geekbot.Bot.Commands.User -{ - [DisableInDirectMessage] - public class Karma : GeekbotCommandBase - { - private readonly DatabaseContext _database; - - public Karma(DatabaseContext database, IErrorHandler errorHandler, IGuildSettingsManager guildSettingsManager) : base(errorHandler, guildSettingsManager) - { - _database = database; - } - - [Command("good", RunMode = RunMode.Async)] - [Summary("Increase Someones Karma")] - public async Task Good([Summary("@someone")] IUser user) - { - try - { - var actor = await GetUser(Context.User.Id); - if (user.Id == Context.User.Id) - { - await ReplyAsync(string.Format(Localization.Karma.CannotChangeOwnUp, Context.User.Username)); - } - else if (TimeoutFinished(actor.TimeOut)) - { - var formatedWaitTime = DateLocalization.FormatDateTimeAsRemaining(actor.TimeOut.AddMinutes(3)); - await ReplyAsync(string.Format(Localization.Karma.WaitUntill, Context.User.Username, formatedWaitTime)); - } - else - { - var target = await GetUser(user.Id); - target.Karma += 1; - SetUser(target); - - actor.TimeOut = DateTimeOffset.Now; - SetUser(actor); - - await _database.SaveChangesAsync(); - - var eb = new EmbedBuilder(); - eb.WithAuthor(new EmbedAuthorBuilder() - .WithIconUrl(user.GetAvatarUrl()) - .WithName(user.Username)); - - eb.WithColor(new Color(138, 219, 146)); - eb.Title = Localization.Karma.Increased; - eb.AddInlineField(Localization.Karma.By, Context.User.Username); - eb.AddInlineField(Localization.Karma.Amount, "+1"); - eb.AddInlineField(Localization.Karma.Current, target.Karma); - await ReplyAsync("", false, eb.Build()); - } - } - catch (Exception e) - { - await ErrorHandler.HandleCommandException(e, Context); - } - } - - [Command("bad", RunMode = RunMode.Async)] - [Summary("Decrease Someones Karma")] - public async Task Bad([Summary("@someone")] IUser user) - { - try - { - var actor = await GetUser(Context.User.Id); - if (user.Id == Context.User.Id) - { - await ReplyAsync(string.Format(Localization.Karma.CannotChangeOwnDown, Context.User.Username)); - } - else if (TimeoutFinished(actor.TimeOut)) - { - var formatedWaitTime = DateLocalization.FormatDateTimeAsRemaining(actor.TimeOut.AddMinutes(3)); - await ReplyAsync(string.Format(Localization.Karma.WaitUntill, Context.User.Username, formatedWaitTime)); - } - else - { - var target = await GetUser(user.Id); - target.Karma -= 1; - SetUser(target); - - actor.TimeOut = DateTimeOffset.Now; - SetUser(actor); - - await _database.SaveChangesAsync(); - - var eb = new EmbedBuilder(); - eb.WithAuthor(new EmbedAuthorBuilder() - .WithIconUrl(user.GetAvatarUrl()) - .WithName(user.Username)); - - eb.WithColor(new Color(138, 219, 146)); - eb.Title = Localization.Karma.Decreased; - eb.AddInlineField(Localization.Karma.By, Context.User.Username); - eb.AddInlineField(Localization.Karma.Amount, "-1"); - eb.AddInlineField(Localization.Karma.Current, target.Karma); - await ReplyAsync("", false, eb.Build()); - } - } - catch (Exception e) - { - await ErrorHandler.HandleCommandException(e, Context); - } - } - - [Command("neutral", RunMode = RunMode.Async)] - [Summary("Do nothing to someones karma")] - public async Task Neutral([Summary("@someone")] IUser user) - { - try - { - var actor = await GetUser(Context.User.Id); - if (user.Id == Context.User.Id) - { - await ReplyAsync(string.Format(Localization.Karma.CannotChangeOwnSame, Context.User.Username)); - return; - } - - if (TimeoutFinished(actor.TimeOut)) - { - var formatedWaitTime = DateLocalization.FormatDateTimeAsRemaining(actor.TimeOut.AddMinutes(3)); - await ReplyAsync(string.Format(Localization.Karma.WaitUntill, Context.User.Username, formatedWaitTime)); - return; - } - - var target = await GetUser(user.Id); - - var eb = new EmbedBuilder(); - eb.WithAuthor(new EmbedAuthorBuilder() - .WithIconUrl(user.GetAvatarUrl()) - .WithName(user.Username)); - - eb.WithColor(new Color(138, 219, 146)); - eb.Title = Localization.Karma.Neutral; - eb.AddInlineField(Localization.Karma.By, Context.User.Username); - eb.AddInlineField(Localization.Karma.Amount, "0"); - eb.AddInlineField(Localization.Karma.Current, target.Karma); - await ReplyAsync("", false, eb.Build()); - - } - catch (Exception e) - { - await ErrorHandler.HandleCommandException(e, Context); - } - } - - private bool TimeoutFinished(DateTimeOffset lastKarma) - { - return lastKarma.AddMinutes(3) > DateTimeOffset.Now; - } - - private async Task GetUser(ulong userId) - { - var user = _database.Karma.FirstOrDefault(u =>u.GuildId.Equals(Context.Guild.Id.AsLong()) && u.UserId.Equals(userId.AsLong())) ?? await CreateNewRow(userId); - return user; - } - - private void SetUser(KarmaModel user) - { - _database.Karma.Update(user); - } - - private async Task CreateNewRow(ulong userId) - { - var user = new KarmaModel() - { - GuildId = Context.Guild.Id.AsLong(), - UserId = userId.AsLong(), - Karma = 0, - TimeOut = DateTimeOffset.MinValue - }; - var newUser = _database.Karma.Add(user).Entity; - await _database.SaveChangesAsync(); - return newUser; - } - } -} \ No newline at end of file diff --git a/src/Bot/Commands/User/Karma/Karma.cs b/src/Bot/Commands/User/Karma/Karma.cs new file mode 100644 index 0000000..a248422 --- /dev/null +++ b/src/Bot/Commands/User/Karma/Karma.cs @@ -0,0 +1,137 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using Discord; +using Discord.Commands; +using Geekbot.Bot.Utils; +using Geekbot.Core; +using Geekbot.Core.CommandPreconditions; +using Geekbot.Core.Database; +using Geekbot.Core.Database.Models; +using Geekbot.Core.ErrorHandling; +using Geekbot.Core.Extensions; +using Geekbot.Core.GuildSettingsManager; + +namespace Geekbot.Bot.Commands.User.Karma +{ + [DisableInDirectMessage] + public class Karma : GeekbotCommandBase + { + private readonly DatabaseContext _database; + + public Karma(DatabaseContext database, IErrorHandler errorHandler, IGuildSettingsManager guildSettingsManager) : base(errorHandler, guildSettingsManager) + { + _database = database; + } + + [Command("good", RunMode = RunMode.Async)] + [Summary("Increase Someones Karma")] + public async Task Good([Summary("@someone")] IUser user) + { + await ChangeKarma(user, KarmaChange.Up); + } + + [Command("bad", RunMode = RunMode.Async)] + [Summary("Decrease Someones Karma")] + public async Task Bad([Summary("@someone")] IUser user) + { + await ChangeKarma(user, KarmaChange.Down); + } + + [Command("neutral", RunMode = RunMode.Async)] + [Summary("Do nothing to someones Karma")] + public async Task Neutral([Summary("@someone")] IUser user) + { + await ChangeKarma(user, KarmaChange.Same); + } + + private async Task ChangeKarma(IUser user, KarmaChange change) + { + try + { + // Get the user + var actor = await GetUser(Context.User.Id); + + // Check if the user can change karma + if (user.Id == Context.User.Id) + { + var message = change switch + { + KarmaChange.Up => Localization.Karma.CannotChangeOwnUp, + KarmaChange.Same => Localization.Karma.CannotChangeOwnSame, + KarmaChange.Down => Localization.Karma.CannotChangeOwnDown, + _ => throw new ArgumentOutOfRangeException(nameof(change), change, null) + }; + await ReplyAsync(string.Format(message, Context.User.Username)); + return; + } + + if (actor.TimeOut.AddMinutes(3) > DateTimeOffset.Now) + { + var formatedWaitTime = DateLocalization.FormatDateTimeAsRemaining(actor.TimeOut.AddMinutes(3)); + await ReplyAsync(string.Format(Localization.Karma.WaitUntill, Context.User.Username, formatedWaitTime)); + return; + } + + // Get the values for the change direction + var (title, amount) = change switch + { + KarmaChange.Up => (Localization.Karma.Increased, 1), + KarmaChange.Same => (Localization.Karma.Neutral, 0), + KarmaChange.Down => (Localization.Karma.Decreased, -1), + _ => throw new ArgumentOutOfRangeException(nameof(change), change, null) + }; + + // Change it + var target = await GetUser(user.Id); + target.Karma += amount; + _database.Karma.Update(target); + + actor.TimeOut = DateTimeOffset.Now; + _database.Karma.Update(actor); + + await _database.SaveChangesAsync(); + + // Respond + var eb = new EmbedBuilder() + { + Author = new EmbedAuthorBuilder() + { + Name = user.Username, + IconUrl = user.GetAvatarUrl() + }, + Title = title, + Color = new Color(138, 219, 146) + }; + eb.AddInlineField(Localization.Karma.By, Context.User.Username); + eb.AddInlineField(Localization.Karma.Amount, amount.ToString()); + eb.AddInlineField(Localization.Karma.Current, target.Karma.ToString()); + await ReplyAsync("", false, eb.Build()); + } + catch (Exception e) + { + await ErrorHandler.HandleCommandException(e, Context); + } + } + + private async Task GetUser(ulong userId) + { + var user = _database.Karma.FirstOrDefault(u => u.GuildId.Equals(Context.Guild.Id.AsLong()) && u.UserId.Equals(userId.AsLong())) ?? await CreateNewRow(userId); + return user; + } + + private async Task CreateNewRow(ulong userId) + { + var user = new KarmaModel() + { + GuildId = Context.Guild.Id.AsLong(), + UserId = userId.AsLong(), + Karma = 0, + TimeOut = DateTimeOffset.MinValue + }; + var newUser = _database.Karma.Add(user).Entity; + await _database.SaveChangesAsync(); + return newUser; + } + } +} \ No newline at end of file diff --git a/src/Bot/Commands/User/Karma/KarmaChange.cs b/src/Bot/Commands/User/Karma/KarmaChange.cs new file mode 100644 index 0000000..01dafc1 --- /dev/null +++ b/src/Bot/Commands/User/Karma/KarmaChange.cs @@ -0,0 +1,9 @@ +namespace Geekbot.Bot.Commands.User.Karma +{ + public enum KarmaChange + { + Up, + Same, + Down + } +} \ No newline at end of file From 1c643285874df4a92b633cf3dd2c63a2559084f5 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Fri, 19 Mar 2021 00:21:35 +0100 Subject: [PATCH 317/443] Upgrade to .net6 preview --- .gitlab-ci.yml | 2 +- Dockerfile | 2 +- src/Bot/Bot.csproj | 2 +- src/Bot/Commands/Utils/Quote/Quote.cs | 1 + src/Core/Core.csproj | 26 ++++++++++++++++---------- src/Web/Web.csproj | 2 +- tests/Tests.csproj | 6 +++--- 7 files changed, 24 insertions(+), 17 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 20b1c84..967def1 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -10,7 +10,7 @@ variables: Build: stage: build - image: mcr.microsoft.com/dotnet/sdk:5.0 + image: mcr.microsoft.com/dotnet/sdk:6.0 artifacts: expire_in: 1h paths: diff --git a/Dockerfile b/Dockerfile index 245e06b..39529ff 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM mcr.microsoft.com/dotnet/aspnet:5.0 +FROM mcr.microsoft.com/dotnet/aspnet:6.0 COPY ./app /app/ diff --git a/src/Bot/Bot.csproj b/src/Bot/Bot.csproj index 6c6f56f..86ab9a3 100644 --- a/src/Bot/Bot.csproj +++ b/src/Bot/Bot.csproj @@ -1,7 +1,7 @@ Exe - net5.0 + net6.0 win-x64;linux-x64 derp.ico $(VersionSuffix) diff --git a/src/Bot/Commands/Utils/Quote/Quote.cs b/src/Bot/Commands/Utils/Quote/Quote.cs index 6a94309..e79663c 100644 --- a/src/Bot/Commands/Utils/Quote/Quote.cs +++ b/src/Bot/Commands/Utils/Quote/Quote.cs @@ -14,6 +14,7 @@ using Geekbot.Core.GuildSettingsManager; using Geekbot.Core.Polyfills; using Geekbot.Core.RandomNumberGenerator; using Geekbot.Core.UserRepository; +using Microsoft.EntityFrameworkCore; namespace Geekbot.Bot.Commands.Utils.Quote { diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index 0a4e989..474478e 100644 --- a/src/Core/Core.csproj +++ b/src/Core/Core.csproj @@ -1,7 +1,7 @@ - net5.0 + net6.0 $(VersionSuffix) $(VersionSuffix) 0.0.0-DEV @@ -14,18 +14,24 @@ - - - - - - - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + - + diff --git a/src/Web/Web.csproj b/src/Web/Web.csproj index 5080a3d..744cadf 100644 --- a/src/Web/Web.csproj +++ b/src/Web/Web.csproj @@ -1,7 +1,7 @@ - net5.0 + net6.0 $(VersionSuffix) $(VersionSuffix) 0.0.0-DEV diff --git a/tests/Tests.csproj b/tests/Tests.csproj index 75a5f9c..6b9b84f 100644 --- a/tests/Tests.csproj +++ b/tests/Tests.csproj @@ -1,13 +1,13 @@  - net5.0 + net6.0 false NU1701 xUnit1026 - - + + From f25c9250ec109485e05f366f32fb18f5f9c2b309 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Fri, 19 Mar 2021 01:10:22 +0100 Subject: [PATCH 318/443] Switch to an alpine container and single file, self-contained deployments --- .gitlab-ci.yml | 2 +- Dockerfile | 2 +- src/Bot/Bot.csproj | 2 +- src/Core/Core.csproj | 1 + src/Web/Web.csproj | 1 + tests/Tests.csproj | 1 + 6 files changed, 6 insertions(+), 3 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 967def1..f92ba14 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -18,7 +18,7 @@ Build: script: - dotnet restore - dotnet test tests - - dotnet publish --version-suffix "$VERSION" -r linux-x64 -c Release -o ./app ./src/Bot/ + - dotnet publish --version-suffix "$VERSION" -r linux-musl-x64 -p:PublishSingleFile=true --self-contained true -c Release -o ./app ./src/Bot/ Package: stage: docker diff --git a/Dockerfile b/Dockerfile index 39529ff..6e81339 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM mcr.microsoft.com/dotnet/aspnet:6.0 +FROM mcr.microsoft.com/dotnet/aspnet:6.0-alpine COPY ./app /app/ diff --git a/src/Bot/Bot.csproj b/src/Bot/Bot.csproj index 86ab9a3..9312ac1 100644 --- a/src/Bot/Bot.csproj +++ b/src/Bot/Bot.csproj @@ -2,7 +2,7 @@ Exe net6.0 - win-x64;linux-x64 + win10-x64;linux-x64;linux-musl-x64 derp.ico $(VersionSuffix) Geekbot.Bot diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index 474478e..17b4a26 100644 --- a/src/Core/Core.csproj +++ b/src/Core/Core.csproj @@ -2,6 +2,7 @@ net6.0 + win10-x64;linux-x64;linux-musl-x64 $(VersionSuffix) $(VersionSuffix) 0.0.0-DEV diff --git a/src/Web/Web.csproj b/src/Web/Web.csproj index 744cadf..d57c63c 100644 --- a/src/Web/Web.csproj +++ b/src/Web/Web.csproj @@ -2,6 +2,7 @@ net6.0 + win10-x64;linux-x64;linux-musl-x64 $(VersionSuffix) $(VersionSuffix) 0.0.0-DEV diff --git a/tests/Tests.csproj b/tests/Tests.csproj index 6b9b84f..4823e0b 100644 --- a/tests/Tests.csproj +++ b/tests/Tests.csproj @@ -1,6 +1,7 @@  net6.0 + win10-x64;linux-x64;linux-musl-x64 false NU1701 xUnit1026 From 52fe5bdec11dc2327f4703e83bd1b6eb7ff2c646 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Sat, 20 Mar 2021 04:06:10 +0100 Subject: [PATCH 319/443] Switch back to a debian container, alpine is missing locale info --- .gitlab-ci.yml | 2 +- Dockerfile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f92ba14..b0135b6 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -18,7 +18,7 @@ Build: script: - dotnet restore - dotnet test tests - - dotnet publish --version-suffix "$VERSION" -r linux-musl-x64 -p:PublishSingleFile=true --self-contained true -c Release -o ./app ./src/Bot/ + - dotnet publish --version-suffix "$VERSION" -r linux-x64 -p:PublishSingleFile=true --self-contained true -c Release -o ./app ./src/Bot/ Package: stage: docker diff --git a/Dockerfile b/Dockerfile index 6e81339..39529ff 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM mcr.microsoft.com/dotnet/aspnet:6.0-alpine +FROM mcr.microsoft.com/dotnet/aspnet:6.0 COPY ./app /app/ From 49870b6b91da13fdb7ec3b9b4a53696eafc2e058 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Sat, 20 Mar 2021 04:17:14 +0100 Subject: [PATCH 320/443] Stop using single-file deployments due to missing locale data --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b0135b6..967def1 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -18,7 +18,7 @@ Build: script: - dotnet restore - dotnet test tests - - dotnet publish --version-suffix "$VERSION" -r linux-x64 -p:PublishSingleFile=true --self-contained true -c Release -o ./app ./src/Bot/ + - dotnet publish --version-suffix "$VERSION" -r linux-x64 -c Release -o ./app ./src/Bot/ Package: stage: docker From 41e0a9f8d72c24766019a4e7844337cab62ce281 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Fri, 26 Mar 2021 00:11:15 +0100 Subject: [PATCH 321/443] Add !evergiven to see if the ship is still stuck in the suez canal --- src/Bot/Bot.csproj | 9 +++ src/Bot/Commands/Utils/Evergiven.cs | 50 +++++++++++++ src/Bot/Localization/Evergiven.Designer.cs | 81 ++++++++++++++++++++++ src/Bot/Localization/Evergiven.de-ch.resx | 20 ++++++ src/Bot/Localization/Evergiven.resx | 27 ++++++++ src/Bot/Utils/DateLocalization.cs | 6 +- 6 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 src/Bot/Commands/Utils/Evergiven.cs create mode 100644 src/Bot/Localization/Evergiven.Designer.cs create mode 100644 src/Bot/Localization/Evergiven.de-ch.resx create mode 100644 src/Bot/Localization/Evergiven.resx diff --git a/src/Bot/Bot.csproj b/src/Bot/Bot.csproj index 9312ac1..2b1cdae 100644 --- a/src/Bot/Bot.csproj +++ b/src/Bot/Bot.csproj @@ -88,6 +88,10 @@ ResXFileCodeGenerator Corona.Designer.cs + + ResXFileCodeGenerator + Evergiven.Designer.cs + @@ -153,5 +157,10 @@ True Corona.resx + + True + True + Evergiven.resx + diff --git a/src/Bot/Commands/Utils/Evergiven.cs b/src/Bot/Commands/Utils/Evergiven.cs new file mode 100644 index 0000000..663fb0a --- /dev/null +++ b/src/Bot/Commands/Utils/Evergiven.cs @@ -0,0 +1,50 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using Discord.Commands; +using Geekbot.Bot.Utils; +using Geekbot.Core; +using Geekbot.Core.ErrorHandling; +using Geekbot.Core.GuildSettingsManager; +using HtmlAgilityPack; + +namespace Geekbot.Bot.Commands.Utils +{ + public class Evergiven : GeekbotCommandBase + { + public Evergiven(IErrorHandler errorHandler, IGuildSettingsManager guildSettingsManager) : base(errorHandler, guildSettingsManager) + { + } + + [Command("evergiven", RunMode = RunMode.Async)] + [Summary("Check if the evergiven ship is still stuck in the suez canal")] + public async Task GetStatus() + { + try + { + var httpClient = HttpAbstractions.CreateDefaultClient(); + var response = await httpClient.GetAsync("https://istheshipstillstuck.com/"); + response.EnsureSuccessStatusCode(); + var stringResponse = await response.Content.ReadAsStringAsync(); + + var doc = new HtmlDocument(); + doc.LoadHtml(stringResponse); + var yesOrNoNode = doc.DocumentNode.SelectNodes("//a").FirstOrDefault(); + + if (yesOrNoNode?.InnerHtml == "Yes.") + { + var stuckSince = DateTime.Now - new DateTime(2021, 03, 23, 10, 39, 0); + var formatted = DateLocalization.FormatDateTimeAsRemaining(stuckSince); + await ReplyAsync(string.Format(Localization.Evergiven.StillStuck, formatted));// $"Ever Given is **still stuck** in the suez canal! It has been stuck for {formatted}"); + return; + } + + await ReplyAsync(Localization.Evergiven.NotStuckAnymore); + } + catch (Exception e) + { + await ErrorHandler.HandleCommandException(e, Context); + } + } + } +} \ No newline at end of file diff --git a/src/Bot/Localization/Evergiven.Designer.cs b/src/Bot/Localization/Evergiven.Designer.cs new file mode 100644 index 0000000..7f66fed --- /dev/null +++ b/src/Bot/Localization/Evergiven.Designer.cs @@ -0,0 +1,81 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Geekbot.Bot.Localization { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Evergiven { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Evergiven() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Geekbot.Bot.Localization.Evergiven", typeof(Evergiven).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to Seems like Ever Given has moved on, check https://istheshipstillstuck.com/. + /// + internal static string NotStuckAnymore { + get { + return ResourceManager.GetString("NotStuckAnymore", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Ever Given is **still stuck** in the suez canal! It has been stuck for {0}. + /// + internal static string StillStuck { + get { + return ResourceManager.GetString("StillStuck", resourceCulture); + } + } + } +} diff --git a/src/Bot/Localization/Evergiven.de-ch.resx b/src/Bot/Localization/Evergiven.de-ch.resx new file mode 100644 index 0000000..b1563fc --- /dev/null +++ b/src/Bot/Localization/Evergiven.de-ch.resx @@ -0,0 +1,20 @@ + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Ever Given **steckt immer no fescht** im Suez Kanal! Es isch derte siit {0} + + + Es gseht danach us das Ever Given wiiter gange isch, gsehne https://istheshipstillstuck.com/ + + \ No newline at end of file diff --git a/src/Bot/Localization/Evergiven.resx b/src/Bot/Localization/Evergiven.resx new file mode 100644 index 0000000..36fb012 --- /dev/null +++ b/src/Bot/Localization/Evergiven.resx @@ -0,0 +1,27 @@ + + + + + + + + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Seems like Ever Given has moved on, check https://istheshipstillstuck.com/ + + + Ever Given is **still stuck** in the suez canal! It has been stuck for {0} + + \ No newline at end of file diff --git a/src/Bot/Utils/DateLocalization.cs b/src/Bot/Utils/DateLocalization.cs index eea40fb..ee52d73 100644 --- a/src/Bot/Utils/DateLocalization.cs +++ b/src/Bot/Utils/DateLocalization.cs @@ -7,7 +7,11 @@ namespace Geekbot.Bot.Utils { public static string FormatDateTimeAsRemaining(DateTimeOffset dateTime) { - var remaining = dateTime - DateTimeOffset.Now; + return FormatDateTimeAsRemaining(dateTime - DateTimeOffset.Now); + } + + public static string FormatDateTimeAsRemaining(TimeSpan remaining) + { const string formattable = "{0} {1}"; var sb = new StringBuilder(); From 9ad39058ac71a2a1e19091dcc4d910d8fa92d70e Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Mon, 29 Mar 2021 11:39:50 +0200 Subject: [PATCH 322/443] Update the !evergiven command to directly reflect what istheshipstillstuck.com says --- src/Bot/Bot.csproj | 9 --- src/Bot/Commands/Utils/Evergiven.cs | 28 ++++++-- src/Bot/Localization/Evergiven.Designer.cs | 81 ---------------------- src/Bot/Localization/Evergiven.de-ch.resx | 20 ------ src/Bot/Localization/Evergiven.resx | 27 -------- 5 files changed, 21 insertions(+), 144 deletions(-) delete mode 100644 src/Bot/Localization/Evergiven.Designer.cs delete mode 100644 src/Bot/Localization/Evergiven.de-ch.resx delete mode 100644 src/Bot/Localization/Evergiven.resx diff --git a/src/Bot/Bot.csproj b/src/Bot/Bot.csproj index 2b1cdae..9312ac1 100644 --- a/src/Bot/Bot.csproj +++ b/src/Bot/Bot.csproj @@ -88,10 +88,6 @@ ResXFileCodeGenerator Corona.Designer.cs - - ResXFileCodeGenerator - Evergiven.Designer.cs -
@@ -157,10 +153,5 @@ True Corona.resx - - True - True - Evergiven.resx -
diff --git a/src/Bot/Commands/Utils/Evergiven.cs b/src/Bot/Commands/Utils/Evergiven.cs index 663fb0a..1115eaf 100644 --- a/src/Bot/Commands/Utils/Evergiven.cs +++ b/src/Bot/Commands/Utils/Evergiven.cs @@ -1,8 +1,9 @@ using System; using System.Linq; +using System.Text; using System.Threading.Tasks; +using System.Web; using Discord.Commands; -using Geekbot.Bot.Utils; using Geekbot.Core; using Geekbot.Core.ErrorHandling; using Geekbot.Core.GuildSettingsManager; @@ -29,17 +30,30 @@ namespace Geekbot.Bot.Commands.Utils var doc = new HtmlDocument(); doc.LoadHtml(stringResponse); - var yesOrNoNode = doc.DocumentNode.SelectNodes("//a").FirstOrDefault(); + var statusNode = doc.DocumentNode.SelectNodes("//a").FirstOrDefault(); - if (yesOrNoNode?.InnerHtml == "Yes.") + if (statusNode == null) { - var stuckSince = DateTime.Now - new DateTime(2021, 03, 23, 10, 39, 0); - var formatted = DateLocalization.FormatDateTimeAsRemaining(stuckSince); - await ReplyAsync(string.Format(Localization.Evergiven.StillStuck, formatted));// $"Ever Given is **still stuck** in the suez canal! It has been stuck for {formatted}"); + await ReplyAsync("Maybe, check "); return; } - await ReplyAsync(Localization.Evergiven.NotStuckAnymore); + var sb = new StringBuilder(); + + sb.Append($"Is that ship still stuck? {statusNode.InnerHtml}"); + if (statusNode.Attributes.Contains("href")) + { + sb.Append($" {statusNode.Attributes["href"].Value}"); + } + + var stuckTimer = doc.DocumentNode.SelectNodes("//p")?.First(node => node.Attributes.First(attr => attr.Name == "style")?.Value == "text-align:center"); + if (stuckTimer != null) + { + sb.AppendLine(); + sb.AppendLine(HttpUtility.HtmlDecode(stuckTimer.InnerText)); + } + + await ReplyAsync(sb.ToString()); } catch (Exception e) { diff --git a/src/Bot/Localization/Evergiven.Designer.cs b/src/Bot/Localization/Evergiven.Designer.cs deleted file mode 100644 index 7f66fed..0000000 --- a/src/Bot/Localization/Evergiven.Designer.cs +++ /dev/null @@ -1,81 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by a tool. -// Runtime Version:4.0.30319.42000 -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -namespace Geekbot.Bot.Localization { - using System; - - - /// - /// A strongly-typed resource class, for looking up localized strings, etc. - /// - // This class was auto-generated by the StronglyTypedResourceBuilder - // class via a tool like ResGen or Visual Studio. - // To add or remove a member, edit your .ResX file then rerun ResGen - // with the /str option, or rebuild your VS project. - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - internal class Evergiven { - - private static global::System.Resources.ResourceManager resourceMan; - - private static global::System.Globalization.CultureInfo resourceCulture; - - [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] - internal Evergiven() { - } - - /// - /// Returns the cached ResourceManager instance used by this class. - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Resources.ResourceManager ResourceManager { - get { - if (object.ReferenceEquals(resourceMan, null)) { - global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Geekbot.Bot.Localization.Evergiven", typeof(Evergiven).Assembly); - resourceMan = temp; - } - return resourceMan; - } - } - - /// - /// Overrides the current thread's CurrentUICulture property for all - /// resource lookups using this strongly typed resource class. - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Globalization.CultureInfo Culture { - get { - return resourceCulture; - } - set { - resourceCulture = value; - } - } - - /// - /// Looks up a localized string similar to Seems like Ever Given has moved on, check https://istheshipstillstuck.com/. - /// - internal static string NotStuckAnymore { - get { - return ResourceManager.GetString("NotStuckAnymore", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Ever Given is **still stuck** in the suez canal! It has been stuck for {0}. - /// - internal static string StillStuck { - get { - return ResourceManager.GetString("StillStuck", resourceCulture); - } - } - } -} diff --git a/src/Bot/Localization/Evergiven.de-ch.resx b/src/Bot/Localization/Evergiven.de-ch.resx deleted file mode 100644 index b1563fc..0000000 --- a/src/Bot/Localization/Evergiven.de-ch.resx +++ /dev/null @@ -1,20 +0,0 @@ - - - text/microsoft-resx - - - 1.3 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - Ever Given **steckt immer no fescht** im Suez Kanal! Es isch derte siit {0} - - - Es gseht danach us das Ever Given wiiter gange isch, gsehne https://istheshipstillstuck.com/ - - \ No newline at end of file diff --git a/src/Bot/Localization/Evergiven.resx b/src/Bot/Localization/Evergiven.resx deleted file mode 100644 index 36fb012..0000000 --- a/src/Bot/Localization/Evergiven.resx +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - - - - text/microsoft-resx - - - 1.3 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - Seems like Ever Given has moved on, check https://istheshipstillstuck.com/ - - - Ever Given is **still stuck** in the suez canal! It has been stuck for {0} - - \ No newline at end of file From 5b99ee951bc6ea45a09347f5840154b7a92f0258 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Mon, 29 Mar 2021 17:45:43 +0200 Subject: [PATCH 323/443] Evergiven is free once again --- src/Bot/Commands/Utils/Evergiven.cs | 31 ++++------------------------- 1 file changed, 4 insertions(+), 27 deletions(-) diff --git a/src/Bot/Commands/Utils/Evergiven.cs b/src/Bot/Commands/Utils/Evergiven.cs index 1115eaf..c7c4335 100644 --- a/src/Bot/Commands/Utils/Evergiven.cs +++ b/src/Bot/Commands/Utils/Evergiven.cs @@ -23,35 +23,12 @@ namespace Geekbot.Bot.Commands.Utils { try { - var httpClient = HttpAbstractions.CreateDefaultClient(); - var response = await httpClient.GetAsync("https://istheshipstillstuck.com/"); - response.EnsureSuccessStatusCode(); - var stringResponse = await response.Content.ReadAsStringAsync(); - - var doc = new HtmlDocument(); - doc.LoadHtml(stringResponse); - var statusNode = doc.DocumentNode.SelectNodes("//a").FirstOrDefault(); - - if (statusNode == null) - { - await ReplyAsync("Maybe, check "); - return; - } - var sb = new StringBuilder(); - sb.Append($"Is that ship still stuck? {statusNode.InnerHtml}"); - if (statusNode.Attributes.Contains("href")) - { - sb.Append($" {statusNode.Attributes["href"].Value}"); - } - - var stuckTimer = doc.DocumentNode.SelectNodes("//p")?.First(node => node.Attributes.First(attr => attr.Name == "style")?.Value == "text-align:center"); - if (stuckTimer != null) - { - sb.AppendLine(); - sb.AppendLine(HttpUtility.HtmlDecode(stuckTimer.InnerText)); - } + sb.AppendLine("Is that ship still stuck?"); + sb.AppendLine("**No!**"); + sb.AppendLine("It was stuck for 6 days, 3 hours and 38 minutes. It (probably) cost \"us\" $59 billion."); + sb.AppendLine("You can follow it here: "); await ReplyAsync(sb.ToString()); } From 153ce3dca4b408a32dbfbdd12bfbcfd8c02acf27 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Mon, 29 Mar 2021 19:02:31 +0200 Subject: [PATCH 324/443] Translate !8ball --- src/Bot/Bot.csproj | 9 + src/Bot/Commands/Randomness/EightBall.cs | 41 +--- src/Bot/Localization/EightBall.Designer.cs | 243 +++++++++++++++++++++ src/Bot/Localization/EightBall.de-ch.resx | 94 ++++++++ src/Bot/Localization/EightBall.resx | 200 +++++++++++++++++ 5 files changed, 558 insertions(+), 29 deletions(-) create mode 100644 src/Bot/Localization/EightBall.Designer.cs create mode 100644 src/Bot/Localization/EightBall.de-ch.resx create mode 100644 src/Bot/Localization/EightBall.resx diff --git a/src/Bot/Bot.csproj b/src/Bot/Bot.csproj index 9312ac1..cd04530 100644 --- a/src/Bot/Bot.csproj +++ b/src/Bot/Bot.csproj @@ -88,6 +88,10 @@ ResXFileCodeGenerator Corona.Designer.cs + + ResXFileCodeGenerator + EightBall.Designer.cs +
@@ -153,5 +157,10 @@ True Corona.resx + + True + True + EightBall.resx + diff --git a/src/Bot/Commands/Randomness/EightBall.cs b/src/Bot/Commands/Randomness/EightBall.cs index e1a3a71..207afe3 100644 --- a/src/Bot/Commands/Randomness/EightBall.cs +++ b/src/Bot/Commands/Randomness/EightBall.cs @@ -1,18 +1,18 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Threading.Tasks; using Discord.Commands; +using Geekbot.Core; using Geekbot.Core.ErrorHandling; +using Geekbot.Core.GuildSettingsManager; namespace Geekbot.Bot.Commands.Randomness { - public class EightBall : ModuleBase + public class EightBall : GeekbotCommandBase { - private readonly IErrorHandler _errorHandler; - - public EightBall(IErrorHandler errorHandler) + public EightBall(IErrorHandler errorHandler, IGuildSettingsManager guildSettingsManager) : base(errorHandler, guildSettingsManager) { - _errorHandler = errorHandler; } [Command("8ball", RunMode = RunMode.Async)] @@ -21,36 +21,19 @@ namespace Geekbot.Bot.Commands.Randomness { try { - var replies = new List + var enumerator = Localization.EightBall.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true).GetEnumerator(); + var replies = new List(); + while (enumerator.MoveNext()) { - "It is certain", - "It is decidedly so", - "Without a doubt", - "Yes, definitely", - "You may rely on it", - "As I see it, yes", - "Most likely", - "Outlook good", - "Yes", - "Signs point to yes", - "Reply hazy try again", - "Ask again later", - "Better not tell you now", - "Cannot predict now", - "Concentrate and ask again", - "Don't count on it", - "My reply is no", - "My sources say no", - "Outlook not so good", - "Very doubtful" - }; - + replies.Add(enumerator.Value?.ToString()); + } + var answer = new Random().Next(replies.Count); await ReplyAsync(replies[answer]); } catch (Exception e) { - await _errorHandler.HandleCommandException(e, Context); + await ErrorHandler.HandleCommandException(e, Context); } } } diff --git a/src/Bot/Localization/EightBall.Designer.cs b/src/Bot/Localization/EightBall.Designer.cs new file mode 100644 index 0000000..8214825 --- /dev/null +++ b/src/Bot/Localization/EightBall.Designer.cs @@ -0,0 +1,243 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Geekbot.Bot.Localization { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class EightBall { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal EightBall() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Geekbot.Bot.Localization.EightBall", typeof(EightBall).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to As I see it, yes. + /// + internal static string AsISeeItYes { + get { + return ResourceManager.GetString("AsISeeItYes", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Ask again later. + /// + internal static string AskAgainLater { + get { + return ResourceManager.GetString("AskAgainLater", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Better not tell you now. + /// + internal static string BetterNotTellYouNow { + get { + return ResourceManager.GetString("BetterNotTellYouNow", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Cannot predict now. + /// + internal static string CannotPredictNow { + get { + return ResourceManager.GetString("CannotPredictNow", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Concentrate and ask again. + /// + internal static string ConcentrateAndAskAgain { + get { + return ResourceManager.GetString("ConcentrateAndAskAgain", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Don't count on it. + /// + internal static string DontCountOnIt { + get { + return ResourceManager.GetString("DontCountOnIt", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to It is certain. + /// + internal static string ItIsCertain { + get { + return ResourceManager.GetString("ItIsCertain", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to It is decidedly so. + /// + internal static string ItIsDecidedlySo { + get { + return ResourceManager.GetString("ItIsDecidedlySo", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Most likely. + /// + internal static string MostLikely { + get { + return ResourceManager.GetString("MostLikely", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to My reply is no. + /// + internal static string MyReplyIsNo { + get { + return ResourceManager.GetString("MyReplyIsNo", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to My sources say no. + /// + internal static string MySourcesSayNo { + get { + return ResourceManager.GetString("MySourcesSayNo", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Outlook good. + /// + internal static string OutlookGood { + get { + return ResourceManager.GetString("OutlookGood", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Outlook not so good. + /// + internal static string OutlookNotSoGood { + get { + return ResourceManager.GetString("OutlookNotSoGood", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Reply hazy try again. + /// + internal static string ReplyHazyTryAgain { + get { + return ResourceManager.GetString("ReplyHazyTryAgain", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Signs point to yes. + /// + internal static string SignsPointToYes { + get { + return ResourceManager.GetString("SignsPointToYes", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Very doubtful. + /// + internal static string VeryDoubtful { + get { + return ResourceManager.GetString("VeryDoubtful", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Without a doubt. + /// + internal static string WithoutADoubt { + get { + return ResourceManager.GetString("WithoutADoubt", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Yes. + /// + internal static string Yes { + get { + return ResourceManager.GetString("Yes", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Yes, definitely. + /// + internal static string YesDefinitely { + get { + return ResourceManager.GetString("YesDefinitely", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to You may rely on it. + /// + internal static string YouMayRelyOnIt { + get { + return ResourceManager.GetString("YouMayRelyOnIt", resourceCulture); + } + } + } +} diff --git a/src/Bot/Localization/EightBall.de-ch.resx b/src/Bot/Localization/EightBall.de-ch.resx new file mode 100644 index 0000000..89d7a60 --- /dev/null +++ b/src/Bot/Localization/EightBall.de-ch.resx @@ -0,0 +1,94 @@ + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Es isch sicher + + + + So isch es entschiede worde + + + + Ohni zwifel + + + + Ja, absolut + + + + Chasch davo usgoh + + + + Wie ich es gsehn, ja + + + + Sehr waschinli + + + + Ussicht isch guet + + + + Ja + + + + Ahzeiche zeigend uf ja + + + + Antwort isch verschwumme, versuechs nomol + + + + Frög spöter nomol + + + + Segs dir jetzt besser nid + + + + Im mommnet chani das nid vorussege + + + + Konzentrier di und frog nomol + + + + Zähl nid druf + + + + Mini antwort isch nei + + + + Mini quellene seged nei + + + + Ussicht isch ned so guet + + + + Sehr froglich + + + \ No newline at end of file diff --git a/src/Bot/Localization/EightBall.resx b/src/Bot/Localization/EightBall.resx new file mode 100644 index 0000000..6eba369 --- /dev/null +++ b/src/Bot/Localization/EightBall.resx @@ -0,0 +1,200 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + It is certain + + + + It is decidedly so + + + + Without a doubt + + + + Yes, definitely + + + + You may rely on it + + + + As I see it, yes + + + + Most likely + + + + Outlook good + + + + Yes + + + + Signs point to yes + + + + Reply hazy try again + + + + Ask again later + + + + Better not tell you now + + + + Cannot predict now + + + + Concentrate and ask again + + + + Don't count on it + + + + My reply is no + + + + My sources say no + + + + Outlook not so good + + + + Very doubtful + + + \ No newline at end of file From 8bd8efa66a70f9769ee53b2d574ba03879b9a8cb Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Wed, 7 Apr 2021 22:49:13 +0200 Subject: [PATCH 325/443] Remove the !evergiven command --- src/Bot/Commands/Utils/Evergiven.cs | 41 ----------------------------- 1 file changed, 41 deletions(-) delete mode 100644 src/Bot/Commands/Utils/Evergiven.cs diff --git a/src/Bot/Commands/Utils/Evergiven.cs b/src/Bot/Commands/Utils/Evergiven.cs deleted file mode 100644 index c7c4335..0000000 --- a/src/Bot/Commands/Utils/Evergiven.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Web; -using Discord.Commands; -using Geekbot.Core; -using Geekbot.Core.ErrorHandling; -using Geekbot.Core.GuildSettingsManager; -using HtmlAgilityPack; - -namespace Geekbot.Bot.Commands.Utils -{ - public class Evergiven : GeekbotCommandBase - { - public Evergiven(IErrorHandler errorHandler, IGuildSettingsManager guildSettingsManager) : base(errorHandler, guildSettingsManager) - { - } - - [Command("evergiven", RunMode = RunMode.Async)] - [Summary("Check if the evergiven ship is still stuck in the suez canal")] - public async Task GetStatus() - { - try - { - var sb = new StringBuilder(); - - sb.AppendLine("Is that ship still stuck?"); - sb.AppendLine("**No!**"); - sb.AppendLine("It was stuck for 6 days, 3 hours and 38 minutes. It (probably) cost \"us\" $59 billion."); - sb.AppendLine("You can follow it here: "); - - await ReplyAsync(sb.ToString()); - } - catch (Exception e) - { - await ErrorHandler.HandleCommandException(e, Context); - } - } - } -} \ No newline at end of file From 5a50ba58206e4fcfd85a2f55af909be9d7291cdb Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Tue, 20 Apr 2021 22:25:33 +0200 Subject: [PATCH 326/443] Add total quotes of a user to !stats --- src/Bot/Commands/User/Stats.cs | 7 +++++-- src/Bot/Localization/Stats.Designer.cs | 18 ++++++++++++++++++ src/Bot/Localization/Stats.de-ch.resx | 6 ++++++ src/Bot/Localization/Stats.resx | 6 ++++++ 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/Bot/Commands/User/Stats.cs b/src/Bot/Commands/User/Stats.cs index a2a54a2..aad2657 100644 --- a/src/Bot/Commands/User/Stats.cs +++ b/src/Bot/Commands/User/Stats.cs @@ -54,6 +54,8 @@ namespace Geekbot.Bot.Commands.User ?.FirstOrDefault(e => e.GuildId.Equals(Context.Guild.Id.AsLong()) && e.UserId.Equals(userInfo.Id.AsLong())) ?.Cookies ?? 0; + var quotes = _database.Quotes.Count(e => e.GuildId.Equals(Context.Guild.Id.AsLong()) && e.UserId.Equals(userInfo.Id.AsLong())); + var eb = new EmbedBuilder(); eb.WithAuthor(new EmbedAuthorBuilder() .WithIconUrl(userInfo.GetAvatarUrl()) @@ -68,9 +70,9 @@ namespace Geekbot.Bot.Commands.User e.UserId.Equals(userInfo.Id.AsLong())); eb.AddInlineField(Localization.Stats.OnDiscordSince, - $"{createdAt.Day}.{createdAt.Month}.{createdAt.Year} ({age} days)") + $"{createdAt.Day}.{createdAt.Month}.{createdAt.Year} ({age} {Localization.Stats.Days})") .AddInlineField(Localization.Stats.JoinedServer, - $"{joinedAt.Day}.{joinedAt.Month}.{joinedAt.Year} ({joinedDayAgo} days)") + $"{joinedAt.Day}.{joinedAt.Month}.{joinedAt.Year} ({joinedDayAgo} {Localization.Stats.Days})") .AddInlineField(Localization.Stats.Karma, karma?.Karma ?? 0) .AddInlineField(Localization.Stats.Level, level) .AddInlineField(Localization.Stats.MessagesSent, messages) @@ -78,6 +80,7 @@ namespace Geekbot.Bot.Commands.User if (correctRolls != null) eb.AddInlineField(Localization.Stats.GuessedRolls, correctRolls.Rolls); if (cookies > 0) eb.AddInlineField(Localization.Stats.Cookies, cookies); + if (quotes > 0) eb.AddInlineField(Localization.Stats.Quotes, quotes); await ReplyAsync("", false, eb.Build()); } diff --git a/src/Bot/Localization/Stats.Designer.cs b/src/Bot/Localization/Stats.Designer.cs index d05f937..6f650e2 100644 --- a/src/Bot/Localization/Stats.Designer.cs +++ b/src/Bot/Localization/Stats.Designer.cs @@ -69,6 +69,15 @@ namespace Geekbot.Bot.Localization { } } + /// + /// Looks up a localized string similar to Days. + /// + internal static string Days { + get { + return ResourceManager.GetString("Days", resourceCulture); + } + } + /// /// Looks up a localized string similar to Guessed Rolls. /// @@ -123,6 +132,15 @@ namespace Geekbot.Bot.Localization { } } + /// + /// Looks up a localized string similar to Quotes. + /// + internal static string Quotes { + get { + return ResourceManager.GetString("Quotes", resourceCulture); + } + } + /// /// Looks up a localized string similar to Server Total. /// diff --git a/src/Bot/Localization/Stats.de-ch.resx b/src/Bot/Localization/Stats.de-ch.resx index ab44a2e..0af0477 100644 --- a/src/Bot/Localization/Stats.de-ch.resx +++ b/src/Bot/Localization/Stats.de-ch.resx @@ -35,4 +35,10 @@ Guetzli + + Täg + + + Quotes + \ No newline at end of file diff --git a/src/Bot/Localization/Stats.resx b/src/Bot/Localization/Stats.resx index 3b8303a..6eb3a92 100644 --- a/src/Bot/Localization/Stats.resx +++ b/src/Bot/Localization/Stats.resx @@ -42,4 +42,10 @@ Cookies + + Days + + + Quotes + \ No newline at end of file From 611b179d628aa56db4f5503c004814f1dde4bb3e Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Fri, 9 Jul 2021 20:21:17 +0200 Subject: [PATCH 327/443] Add a piece of low fat mozzarella to the !slap command --- src/Bot/Commands/Randomness/Slap.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Bot/Commands/Randomness/Slap.cs b/src/Bot/Commands/Randomness/Slap.cs index b512e73..7a858b1 100644 --- a/src/Bot/Commands/Randomness/Slap.cs +++ b/src/Bot/Commands/Randomness/Slap.cs @@ -76,7 +76,8 @@ namespace Geekbot.Bot.Commands.Randomness "teapot", "candle", "dictionary", - "powerless banhammer" + "powerless banhammer", + "piece of low fat mozzarella" }; await ReplyAsync($"{Context.User.Username} slapped {user.Username} with a {things[new Random().Next(things.Count - 1)]}"); @@ -127,4 +128,4 @@ namespace Geekbot.Bot.Commands.Randomness e.UserId.Equals(userId.AsLong())); } } -} \ No newline at end of file +} From 8fcc6291065e9becd2e5f9ada1955dba7e83ad99 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Fri, 9 Jul 2021 20:39:04 +0200 Subject: [PATCH 328/443] Disable MSBuildEnableWorkloadResolver in gitlab ci --- .gitlab-ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 967def1..fc1cd0e 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -7,6 +7,8 @@ stages: variables: VERSION: 4.3.0-V$CI_COMMIT_SHORT_SHA IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG + # ToDo: Remove when .net6 is released + MSBuildEnableWorkloadResolver: false Build: stage: build From 86068ecc443dd18d709dcf1f52d6d291ce06c500 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Fri, 9 Jul 2021 20:48:42 +0200 Subject: [PATCH 329/443] Disable self contained publishing for the test, web and bot dlls --- .gitlab-ci.yml | 2 +- src/Bot/Bot.csproj | 1 + src/Web/Web.csproj | 1 + tests/Tests.csproj | 1 + 4 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index fc1cd0e..7da242b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -8,7 +8,7 @@ variables: VERSION: 4.3.0-V$CI_COMMIT_SHORT_SHA IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG # ToDo: Remove when .net6 is released - MSBuildEnableWorkloadResolver: false + MSBuildEnableWorkloadResolver: 'false' Build: stage: build diff --git a/src/Bot/Bot.csproj b/src/Bot/Bot.csproj index cd04530..ff10a94 100644 --- a/src/Bot/Bot.csproj +++ b/src/Bot/Bot.csproj @@ -3,6 +3,7 @@ Exe net6.0 win10-x64;linux-x64;linux-musl-x64 + false derp.ico $(VersionSuffix) Geekbot.Bot diff --git a/src/Web/Web.csproj b/src/Web/Web.csproj index d57c63c..a9ffcc4 100644 --- a/src/Web/Web.csproj +++ b/src/Web/Web.csproj @@ -3,6 +3,7 @@ net6.0 win10-x64;linux-x64;linux-musl-x64 + false $(VersionSuffix) $(VersionSuffix) 0.0.0-DEV diff --git a/tests/Tests.csproj b/tests/Tests.csproj index 4823e0b..786164e 100644 --- a/tests/Tests.csproj +++ b/tests/Tests.csproj @@ -2,6 +2,7 @@ net6.0 win10-x64;linux-x64;linux-musl-x64 + false false NU1701 xUnit1026 From 8d037c786ea5b916add8b1219e1d3e2836ed91f6 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Sat, 10 Jul 2021 00:41:22 +0200 Subject: [PATCH 330/443] Reenable MSBuildEnableWorkloadResolver --- .gitlab-ci.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 7da242b..967def1 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -7,8 +7,6 @@ stages: variables: VERSION: 4.3.0-V$CI_COMMIT_SHORT_SHA IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG - # ToDo: Remove when .net6 is released - MSBuildEnableWorkloadResolver: 'false' Build: stage: build From 90668b6aac1bb728eba9d1a4b924743e3503a1d0 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Sat, 10 Jul 2021 00:49:03 +0200 Subject: [PATCH 331/443] Add padding at the end of things for the !slap command --- src/Bot/Commands/Randomness/Slap.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Bot/Commands/Randomness/Slap.cs b/src/Bot/Commands/Randomness/Slap.cs index 7a858b1..f5d21b8 100644 --- a/src/Bot/Commands/Randomness/Slap.cs +++ b/src/Bot/Commands/Randomness/Slap.cs @@ -77,10 +77,13 @@ namespace Geekbot.Bot.Commands.Randomness "candle", "dictionary", "powerless banhammer", - "piece of low fat mozzarella" + "piece of low fat mozzarella", + // For some reason it never picks the last one + // Adding this workaround, because i'm to lazy to actually fix it at the time of writing this + "padding" }; - await ReplyAsync($"{Context.User.Username} slapped {user.Username} with a {things[new Random().Next(things.Count - 1)]}"); + await ReplyAsync($"{Context.User.Username} slapped {user.Username} with a {things[new Random().Next(0, things.Count - 1)]}"); await UpdateRecieved(user.Id); await UpdateGiven(Context.User.Id); From 9a55d8447fef227f5b69790125651767bd92fb82 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Wed, 11 Aug 2021 17:58:06 +0200 Subject: [PATCH 332/443] Upgrade discord.net to version 2.4.0 --- src/Core/Core.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index 17b4a26..629233a 100644 --- a/src/Core/Core.csproj +++ b/src/Core/Core.csproj @@ -14,7 +14,7 @@ - + all From f22956368b3a165a3cbe9135ff829cffa6ba8187 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Wed, 11 Aug 2021 17:58:45 +0200 Subject: [PATCH 333/443] Remove !gdq --- src/Bot/Commands/Randomness/Gdq.cs | 36 ------------------------------ 1 file changed, 36 deletions(-) delete mode 100644 src/Bot/Commands/Randomness/Gdq.cs diff --git a/src/Bot/Commands/Randomness/Gdq.cs b/src/Bot/Commands/Randomness/Gdq.cs deleted file mode 100644 index c6d9fa8..0000000 --- a/src/Bot/Commands/Randomness/Gdq.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System; -using System.Net; -using System.Threading.Tasks; -using Discord.Commands; -using Geekbot.Core.ErrorHandling; - -namespace Geekbot.Bot.Commands.Randomness -{ - public class Gdq : ModuleBase - { - private readonly IErrorHandler _errorHandler; - - public Gdq(IErrorHandler errorHandler) - { - _errorHandler = errorHandler; - } - - [Command("gdq", RunMode = RunMode.Async)] - [Summary("Get a quote from the GDQ donation generator.")] - public async Task GetQuote() - { - try - { - using var client = new WebClient(); - var url = new Uri("http://taskinoz.com/gdq/api/"); - var response = client.DownloadString(url); - - await ReplyAsync(response); - } - catch (Exception e) - { - await _errorHandler.HandleCommandException(e, Context); - } - } - } -} \ No newline at end of file From 18ece35ffe88aaa8e5aa0204b499d72f5615ece4 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Wed, 11 Aug 2021 23:08:00 +0200 Subject: [PATCH 334/443] Remove System.Timers.Timer ambiguity for .net6-preview7 --- src/Bot/Handlers/StatsHandler.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Bot/Handlers/StatsHandler.cs b/src/Bot/Handlers/StatsHandler.cs index d1384cd..b089515 100644 --- a/src/Bot/Handlers/StatsHandler.cs +++ b/src/Bot/Handlers/StatsHandler.cs @@ -1,7 +1,5 @@ using System; -using System.Collections.Generic; using System.Threading.Tasks; -using System.Timers; using Discord.WebSocket; using Geekbot.Core.Database; using Geekbot.Core.Database.Models; @@ -24,7 +22,7 @@ namespace Geekbot.Bot.Handlers _database = database; _season = SeasonsUtils.GetCurrentSeason(); - var timer = new Timer() + var timer = new System.Timers.Timer() { Enabled = true, AutoReset = true, From e712403dd96d824688cc0077b5394397b5b233f2 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Fri, 17 Sep 2021 11:21:42 +0200 Subject: [PATCH 335/443] Upgrade Sumologic, jikan and HtmlAgilityPack --- src/Bot/Bot.csproj | 4 ++-- src/Core/Core.csproj | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Bot/Bot.csproj b/src/Bot/Bot.csproj index ff10a94..c64eecb 100644 --- a/src/Bot/Bot.csproj +++ b/src/Bot/Bot.csproj @@ -24,8 +24,8 @@ - - + + diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index 629233a..463c4e9 100644 --- a/src/Core/Core.csproj +++ b/src/Core/Core.csproj @@ -34,7 +34,7 @@ - + From f19ddb30b285352282722bc868f67827e029bddd Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Fri, 17 Sep 2021 11:23:20 +0200 Subject: [PATCH 336/443] Replace RNGCryptoServiceProvider with System.Security.Cryptography.RandomNumberGenerator --- src/Core/RandomNumberGenerator/RandomNumberGenerator.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Core/RandomNumberGenerator/RandomNumberGenerator.cs b/src/Core/RandomNumberGenerator/RandomNumberGenerator.cs index db990a7..1b7178b 100644 --- a/src/Core/RandomNumberGenerator/RandomNumberGenerator.cs +++ b/src/Core/RandomNumberGenerator/RandomNumberGenerator.cs @@ -1,5 +1,4 @@ using System; -using System.Security.Cryptography; using Anemonis.RandomOrg; using Geekbot.Core.GlobalSettings; @@ -7,13 +6,13 @@ namespace Geekbot.Core.RandomNumberGenerator { public class RandomNumberGenerator : IRandomNumberGenerator { - private readonly RNGCryptoServiceProvider csp; + private readonly System.Security.Cryptography.RandomNumberGenerator rng; private readonly bool _canUseRandomOrg; private readonly RandomOrgClient _randomOrgClient; public RandomNumberGenerator(IGlobalSettings globalSettings) { - csp = new RNGCryptoServiceProvider(); + rng = System.Security.Cryptography.RandomNumberGenerator.Create(); var randomOrgApiKey = globalSettings.GetKey("RandomOrgApiKey"); if (!string.IsNullOrEmpty(randomOrgApiKey)) @@ -32,7 +31,7 @@ namespace Geekbot.Core.RandomNumberGenerator if (minValue >= maxInclusiveValue) { - throw new ArgumentOutOfRangeException("minValue must be lower than maxExclusiveValue"); + throw new ArgumentOutOfRangeException("minValue", "must be lower than maxExclusiveValue"); } if (_canUseRandomOrg) @@ -84,7 +83,7 @@ namespace Geekbot.Core.RandomNumberGenerator private byte[] GenerateRandomBytes(int bytesNumber) { var buffer = new byte[bytesNumber]; - csp.GetBytes(buffer); + rng.GetBytes(buffer); return buffer; } } From 989057a0b06c034658bf83d73ea16a289d480803 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Fri, 17 Sep 2021 12:22:26 +0200 Subject: [PATCH 337/443] Migrate from RavenSharp to the SentrySDK --- src/Bot/Bot.csproj | 2 +- src/Core/Core.csproj | 2 +- src/Core/ErrorHandling/ErrorHandler.cs | 38 +++++++++++++------------- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/Bot/Bot.csproj b/src/Bot/Bot.csproj index c64eecb..998456c 100644 --- a/src/Bot/Bot.csproj +++ b/src/Bot/Bot.csproj @@ -29,7 +29,7 @@ - + diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index 463c4e9..dfce63b 100644 --- a/src/Core/Core.csproj +++ b/src/Core/Core.csproj @@ -33,7 +33,7 @@ - + diff --git a/src/Core/ErrorHandling/ErrorHandler.cs b/src/Core/ErrorHandling/ErrorHandler.cs index 60d97e9..a5ccc8e 100644 --- a/src/Core/ErrorHandling/ErrorHandler.cs +++ b/src/Core/ErrorHandling/ErrorHandler.cs @@ -1,9 +1,9 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using Discord.Commands; using Geekbot.Core.Logger; -using SharpRaven; -using SharpRaven.Data; +using Sentry; using Exception = System.Exception; namespace Geekbot.Core.ErrorHandling @@ -12,7 +12,6 @@ namespace Geekbot.Core.ErrorHandling { private readonly IGeekbotLogger _logger; private readonly Func _getDefaultErrorText; - private readonly IRavenClient _raven; private readonly bool _errorsInChat; public ErrorHandler(IGeekbotLogger logger, RunParameters runParameters, Func getDefaultErrorText) @@ -22,15 +21,15 @@ namespace Geekbot.Core.ErrorHandling _errorsInChat = runParameters.ExposeErrors; var sentryDsn = runParameters.SentryEndpoint; - if (!string.IsNullOrEmpty(sentryDsn)) + if (string.IsNullOrEmpty(sentryDsn)) return; + + SentrySdk.Init(o => { - _raven = new RavenClient(sentryDsn) { Release = Constants.BotVersion(), Environment = "Production" }; - _logger.Information(LogSource.Geekbot, $"Command Errors will be logged to Sentry: {sentryDsn}"); - } - else - { - _raven = null; - } + o.Dsn = sentryDsn; + o.Release = Constants.BotVersion(); + o.Environment = "Production"; + }); + _logger.Information(LogSource.Geekbot, $"Command Errors will be logged to Sentry: {sentryDsn}"); } public async Task HandleCommandException(Exception e, ICommandContext context, string errorMessage = "def") @@ -83,18 +82,19 @@ namespace Geekbot.Core.ErrorHandling private void ReportExternal(Exception e, MessageDto errorObj) { - if (_raven == null) return; + if (!SentrySdk.IsEnabled) return; + var sentryEvent = new SentryEvent(e) { - Tags = - { - ["discord_server"] = errorObj.Guild.Name, - ["discord_user"] = errorObj.User.Name - }, Message = errorObj.Message.Content, - Extra = errorObj }; - _raven.Capture(sentryEvent); + sentryEvent.SetTag("discord_server", errorObj.Guild.Name); + sentryEvent.SetExtra("Channel", errorObj.Channel); + sentryEvent.SetExtra("Guild", errorObj.Guild); + sentryEvent.SetExtra("Message", errorObj.Message); + sentryEvent.SetExtra("User", errorObj.User); + + SentrySdk.CaptureEvent(sentryEvent); } } } \ No newline at end of file From 5c507b026cc3f8bb94bc4cb8f92d77595c609a5c Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Fri, 17 Sep 2021 14:03:35 +0200 Subject: [PATCH 338/443] Remove random.org integration --- src/Bot/Program.cs | 2 +- src/Core/Core.csproj | 1 - .../RandomNumberGenerator.cs | 34 +------------------ tests/Core/DiceParser/DiceParser.test.cs | 2 +- tests/Core/DiceParser/SingleDie.test.cs | 4 +-- 5 files changed, 5 insertions(+), 38 deletions(-) diff --git a/src/Bot/Program.cs b/src/Bot/Program.cs index dee6ec3..3429fbc 100644 --- a/src/Bot/Program.cs +++ b/src/Bot/Program.cs @@ -162,7 +162,7 @@ namespace Geekbot.Bot var emojiConverter = new EmojiConverter(); var mtgManaConverter = new MtgManaConverter(); var wikipediaClient = new WikipediaClient(); - var randomNumberGenerator = new RandomNumberGenerator(_globalSettings); + var randomNumberGenerator = new RandomNumberGenerator(); var mediaProvider = new MediaProvider(_logger, randomNumberGenerator); var kvMemoryStore = new KvInInMemoryStore(); var errorHandler = new ErrorHandler(_logger, _runParameters, () => Localization.Internal.SomethingWentWrong); diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index dfce63b..6d1b89b 100644 --- a/src/Core/Core.csproj +++ b/src/Core/Core.csproj @@ -12,7 +12,6 @@ - diff --git a/src/Core/RandomNumberGenerator/RandomNumberGenerator.cs b/src/Core/RandomNumberGenerator/RandomNumberGenerator.cs index 1b7178b..7460677 100644 --- a/src/Core/RandomNumberGenerator/RandomNumberGenerator.cs +++ b/src/Core/RandomNumberGenerator/RandomNumberGenerator.cs @@ -1,25 +1,14 @@ using System; -using Anemonis.RandomOrg; -using Geekbot.Core.GlobalSettings; namespace Geekbot.Core.RandomNumberGenerator { public class RandomNumberGenerator : IRandomNumberGenerator { private readonly System.Security.Cryptography.RandomNumberGenerator rng; - private readonly bool _canUseRandomOrg; - private readonly RandomOrgClient _randomOrgClient; - public RandomNumberGenerator(IGlobalSettings globalSettings) + public RandomNumberGenerator() { rng = System.Security.Cryptography.RandomNumberGenerator.Create(); - - var randomOrgApiKey = globalSettings.GetKey("RandomOrgApiKey"); - if (!string.IsNullOrEmpty(randomOrgApiKey)) - { - _canUseRandomOrg = true; - _randomOrgClient = new RandomOrgClient(randomOrgApiKey); - } } public int Next(int minValue, int maxInclusiveValue) @@ -33,31 +22,10 @@ namespace Geekbot.Core.RandomNumberGenerator { throw new ArgumentOutOfRangeException("minValue", "must be lower than maxExclusiveValue"); } - - if (_canUseRandomOrg) - { - try - { - return GetFromRandomOrg(minValue, maxInclusiveValue); - } - catch - { - // ignore - } - } return GetFromCrypto(minValue, maxInclusiveValue); } - private int GetFromRandomOrg(int minValue, int maxInclusiveValue) - { - return _randomOrgClient - .GenerateIntegersAsync(1, minValue, maxInclusiveValue, false) - .Result - .Random - .Data[0]; - } - private int GetFromCrypto(int minValue, int maxInclusiveValue) { var maxExclusiveValue = maxInclusiveValue + 1; diff --git a/tests/Core/DiceParser/DiceParser.test.cs b/tests/Core/DiceParser/DiceParser.test.cs index ed9425f..fd9e5c5 100644 --- a/tests/Core/DiceParser/DiceParser.test.cs +++ b/tests/Core/DiceParser/DiceParser.test.cs @@ -10,7 +10,7 @@ namespace Tests.Core.DiceParser { public class DiceParserTest { - private static readonly RandomNumberGenerator _randomNumberGenerator = new RandomNumberGenerator(new Mock().Object); + private static readonly RandomNumberGenerator _randomNumberGenerator = new RandomNumberGenerator(); public struct DiceParserTestDto { diff --git a/tests/Core/DiceParser/SingleDie.test.cs b/tests/Core/DiceParser/SingleDie.test.cs index 5173902..fdba5e7 100644 --- a/tests/Core/DiceParser/SingleDie.test.cs +++ b/tests/Core/DiceParser/SingleDie.test.cs @@ -45,7 +45,7 @@ namespace Tests.Core.DiceParser [Theory, MemberData(nameof(SingleDieNameTestData))] public void SingleDieNameTestFunc(string testName, SingleDieNameTestDto testData) { - var die = new SingleDie(new RandomNumberGenerator(new Mock().Object)) {AdvantageType = testData.AdvantageType}; + var die = new SingleDie(new RandomNumberGenerator()) {AdvantageType = testData.AdvantageType}; Assert.Equal(die.DiceName, testData.Expected); } @@ -108,7 +108,7 @@ namespace Tests.Core.DiceParser [Theory, MemberData(nameof(SingleDieValidationTestData))] public void SingleDieValidationTestFunc(string testName, SingleDieValidationTestDto testData) { - var die = new SingleDie(new RandomNumberGenerator(new Mock().Object)) + var die = new SingleDie(new RandomNumberGenerator()) { Amount = testData.Amount, Sides = testData.Sides From 1f518e980c323b728e43b85d9f829738bd18754e Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Fri, 17 Sep 2021 14:04:30 +0200 Subject: [PATCH 339/443] Move Sentry SDK init to the main init process --- src/Bot/Program.cs | 20 +++++++++++++++++++- src/Core/ErrorHandling/ErrorHandler.cs | 12 ------------ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/Bot/Program.cs b/src/Bot/Program.cs index 3429fbc..4d7526b 100644 --- a/src/Bot/Program.cs +++ b/src/Bot/Program.cs @@ -26,6 +26,8 @@ using Geekbot.Core.WikipediaClient; using Geekbot.Web; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; +using Sentry; +using Constants = Geekbot.Core.Constants; namespace Geekbot.Bot { @@ -90,7 +92,9 @@ namespace Geekbot.Bot _logger.Information(LogSource.Api, "Starting Web API"); StartWebApi(); - + + RegisterSentry(); + _logger.Information(LogSource.Geekbot, "Done and ready for use"); await Task.Delay(-1); @@ -224,5 +228,19 @@ namespace Geekbot.Bot var highscoreManager = new HighscoreManager(_databaseInitializer.Initialize(), _userRepository); WebApiStartup.StartWebApi(_logger, _runParameters, _commands, _databaseInitializer.Initialize(), _client, _globalSettings, highscoreManager); } + + private void RegisterSentry() + { + var sentryDsn = _runParameters.SentryEndpoint; + if (string.IsNullOrEmpty(sentryDsn)) return; + SentrySdk.Init(o => + { + o.Dsn = sentryDsn; + o.Release = Constants.BotVersion(); + o.Environment = "Production"; + o.TracesSampleRate = 1.0; + }); + _logger.Information(LogSource.Geekbot, $"Command Errors will be logged to Sentry: {sentryDsn}"); + } } } \ No newline at end of file diff --git a/src/Core/ErrorHandling/ErrorHandler.cs b/src/Core/ErrorHandling/ErrorHandler.cs index a5ccc8e..0b55bd8 100644 --- a/src/Core/ErrorHandling/ErrorHandler.cs +++ b/src/Core/ErrorHandling/ErrorHandler.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Threading.Tasks; using Discord.Commands; using Geekbot.Core.Logger; @@ -19,17 +18,6 @@ namespace Geekbot.Core.ErrorHandling _logger = logger; _getDefaultErrorText = getDefaultErrorText; _errorsInChat = runParameters.ExposeErrors; - - var sentryDsn = runParameters.SentryEndpoint; - if (string.IsNullOrEmpty(sentryDsn)) return; - - SentrySdk.Init(o => - { - o.Dsn = sentryDsn; - o.Release = Constants.BotVersion(); - o.Environment = "Production"; - }); - _logger.Information(LogSource.Geekbot, $"Command Errors will be logged to Sentry: {sentryDsn}"); } public async Task HandleCommandException(Exception e, ICommandContext context, string errorMessage = "def") From 3299ac4eabafdb4c053ca0887aad73d0518c9635 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Fri, 17 Sep 2021 14:06:10 +0200 Subject: [PATCH 340/443] Add generic sentry tracing to the main command module --- src/Core/GeekbotCommandBase.cs | 40 +++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/Core/GeekbotCommandBase.cs b/src/Core/GeekbotCommandBase.cs index 43ced95..ae964e7 100644 --- a/src/Core/GeekbotCommandBase.cs +++ b/src/Core/GeekbotCommandBase.cs @@ -1,9 +1,13 @@ +using System.Collections.Generic; using System.Globalization; using System.Threading; +using System.Threading.Tasks; +using Discord; using Discord.Commands; using Geekbot.Core.Database.Models; using Geekbot.Core.ErrorHandling; using Geekbot.Core.GuildSettingsManager; +using Sentry; namespace Geekbot.Core { @@ -12,6 +16,7 @@ namespace Geekbot.Core protected readonly IGuildSettingsManager GuildSettingsManager; protected GuildSettingsModel GuildSettings; protected readonly IErrorHandler ErrorHandler; + protected ITransaction Transaction; protected GeekbotCommandBase(IErrorHandler errorHandler, IGuildSettingsManager guildSettingsManager) { @@ -22,9 +27,42 @@ namespace Geekbot.Core protected override void BeforeExecute(CommandInfo command) { base.BeforeExecute(command); + + // Transaction Setup + Transaction = SentrySdk.StartTransaction(new Transaction(command.Name, "Exec")); + Transaction.SetTags(new [] + { + new KeyValuePair("GuildId", Context.Guild.Id.ToString()), + new KeyValuePair("Guild", Context.Guild.Name), + }); + Transaction.User = new User() + { + Id = Context.User.Id.ToString(), + Username = Context.User.Username, + }; + + // Command Setup + var setupSpan = Transaction.StartChild("Setup"); + GuildSettings = GuildSettingsManager.GetSettings(Context?.Guild?.Id ?? 0); var language = GuildSettings.Language; - Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(language == "CHDE" ? "de-ch" : language); + Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(language); + + setupSpan.Finish(); + } + + protected override void AfterExecute(CommandInfo command) + { + base.AfterExecute(command); + Transaction.Finish(); + } + + protected override Task ReplyAsync(string message = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, AllowedMentions allowedMentions = null, MessageReference messageReference = null) + { + var replySpan = Transaction.StartChild("Reply"); + var msg = base.ReplyAsync(message, isTTS, embed, options, allowedMentions, messageReference); + replySpan.Finish(); + return msg; } } } \ No newline at end of file From aa826f962d8715f2579a0c9ee79bee8776c74a4e Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Fri, 17 Sep 2021 14:06:34 +0200 Subject: [PATCH 341/443] Add traces to the !roll command --- src/Bot/Commands/Games/Roll/Roll.cs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Bot/Commands/Games/Roll/Roll.cs b/src/Bot/Commands/Games/Roll/Roll.cs index 6ef9322..0361cbd 100644 --- a/src/Bot/Commands/Games/Roll/Roll.cs +++ b/src/Bot/Commands/Games/Roll/Roll.cs @@ -11,6 +11,7 @@ using Geekbot.Core.Extensions; using Geekbot.Core.GuildSettingsManager; using Geekbot.Core.KvInMemoryStore; using Geekbot.Core.RandomNumberGenerator; +using Sentry; namespace Geekbot.Bot.Commands.Games.Roll { @@ -34,43 +35,54 @@ namespace Geekbot.Bot.Commands.Games.Roll { try { + var inputSpan = Transaction.StartChild("CommandInput"); var number = _randomNumberGenerator.Next(1, 100); int.TryParse(stuff, out var guess); + inputSpan.Finish(); + if (guess <= 100 && guess > 0) { + var prevRollCheckSpan = Transaction.StartChild("PrevRollCheck"); var kvKey = $"{Context?.Guild?.Id ?? 0}:{Context.User.Id}:RollsPrevious"; - + var prevRoll = _kvInMemoryStore.Get(kvKey); - + if (prevRoll?.LastGuess == guess && prevRoll?.GuessedOn.AddDays(1) > DateTime.Now) { await ReplyAsync(string.Format( Localization.Roll.NoPrevGuess, Context.Message.Author.Mention, DateLocalization.FormatDateTimeAsRemaining(prevRoll.GuessedOn.AddDays(1)))); + Transaction.Status = SpanStatus.InvalidArgument; return; } _kvInMemoryStore.Set(kvKey, new RollTimeout {LastGuess = guess, GuessedOn = DateTime.Now}); + prevRollCheckSpan.Finish(); await ReplyAsync(string.Format(Localization.Roll.Rolled, Context.Message.Author.Mention, number, guess)); if (guess == number) { + var correctGuessSpan = Transaction.StartChild("CorrectGuess"); await ReplyAsync(string.Format(Localization.Roll.Gratz, Context.Message.Author)); var user = await GetUser(Context.User.Id); user.Rolls += 1; _database.Rolls.Update(user); await _database.SaveChangesAsync(); + correctGuessSpan.Finish(); } } else { await ReplyAsync(string.Format(Localization.Roll.RolledNoGuess, Context.Message.Author.Mention, number)); } + + Transaction.Status = SpanStatus.Ok; } catch (Exception e) { await ErrorHandler.HandleCommandException(e, Context); + Transaction.Status = SpanStatus.InternalError; } } From d708525a2f59711599311782d1c663e7f4f65441 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Fri, 17 Sep 2021 14:07:19 +0200 Subject: [PATCH 342/443] Add traces to the !quote command --- src/Bot/Commands/Utils/Quote/Quote.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Bot/Commands/Utils/Quote/Quote.cs b/src/Bot/Commands/Utils/Quote/Quote.cs index e79663c..fdf1030 100644 --- a/src/Bot/Commands/Utils/Quote/Quote.cs +++ b/src/Bot/Commands/Utils/Quote/Quote.cs @@ -15,6 +15,8 @@ using Geekbot.Core.Polyfills; using Geekbot.Core.RandomNumberGenerator; using Geekbot.Core.UserRepository; using Microsoft.EntityFrameworkCore; +using Sentry; +using Constants = Geekbot.Core.Constants; namespace Geekbot.Bot.Commands.Utils.Quote { @@ -43,23 +45,32 @@ namespace Geekbot.Bot.Commands.Utils.Quote { try { + var getTotalSpan = Transaction.StartChild("TotalQuotes"); var totalQuotes = await _database.Quotes.CountAsync(e => e.GuildId.Equals(Context.Guild.Id.AsLong())); + getTotalSpan.Finish(); if (totalQuotes == 0) { await ReplyAsync(Localization.Quote.NoQuotesFound); + Transaction.Status = SpanStatus.NotFound; return; } + var getQuoteFromDbSpan = Transaction.StartChild("GetQuoteFromDB"); var random = _randomNumberGenerator.Next(0, totalQuotes - 1); var quote = _database.Quotes.Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong())).Skip(random).Take(1); + getQuoteFromDbSpan.Finish(); + var replySpan = Transaction.StartChild("Reply"); var embed = QuoteBuilder(quote.FirstOrDefault()); await ReplyAsync("", false, embed.Build()); + replySpan.Finish(); + Transaction.Status = SpanStatus.Ok; } catch (Exception e) { await ErrorHandler.HandleCommandException(e, Context, "Whoops, seems like the quote was to edgy to return"); + Transaction.Status = SpanStatus.InternalError; } } From 833a8a0dd8dfea3fa29019584cf448c41c4cc469 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Fri, 17 Sep 2021 14:27:46 +0200 Subject: [PATCH 343/443] Split Transactions from the GeekbotCommandBase --- src/Core/GeekbotCommandBase.cs | 35 +----------------------- src/Core/TransactionModuleBase.cs | 45 +++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 34 deletions(-) create mode 100644 src/Core/TransactionModuleBase.cs diff --git a/src/Core/GeekbotCommandBase.cs b/src/Core/GeekbotCommandBase.cs index ae964e7..801c53c 100644 --- a/src/Core/GeekbotCommandBase.cs +++ b/src/Core/GeekbotCommandBase.cs @@ -1,22 +1,17 @@ -using System.Collections.Generic; using System.Globalization; using System.Threading; -using System.Threading.Tasks; -using Discord; using Discord.Commands; using Geekbot.Core.Database.Models; using Geekbot.Core.ErrorHandling; using Geekbot.Core.GuildSettingsManager; -using Sentry; namespace Geekbot.Core { - public class GeekbotCommandBase : ModuleBase + public class GeekbotCommandBase : TransactionModuleBase { protected readonly IGuildSettingsManager GuildSettingsManager; protected GuildSettingsModel GuildSettings; protected readonly IErrorHandler ErrorHandler; - protected ITransaction Transaction; protected GeekbotCommandBase(IErrorHandler errorHandler, IGuildSettingsManager guildSettingsManager) { @@ -28,20 +23,6 @@ namespace Geekbot.Core { base.BeforeExecute(command); - // Transaction Setup - Transaction = SentrySdk.StartTransaction(new Transaction(command.Name, "Exec")); - Transaction.SetTags(new [] - { - new KeyValuePair("GuildId", Context.Guild.Id.ToString()), - new KeyValuePair("Guild", Context.Guild.Name), - }); - Transaction.User = new User() - { - Id = Context.User.Id.ToString(), - Username = Context.User.Username, - }; - - // Command Setup var setupSpan = Transaction.StartChild("Setup"); GuildSettings = GuildSettingsManager.GetSettings(Context?.Guild?.Id ?? 0); @@ -50,19 +31,5 @@ namespace Geekbot.Core setupSpan.Finish(); } - - protected override void AfterExecute(CommandInfo command) - { - base.AfterExecute(command); - Transaction.Finish(); - } - - protected override Task ReplyAsync(string message = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, AllowedMentions allowedMentions = null, MessageReference messageReference = null) - { - var replySpan = Transaction.StartChild("Reply"); - var msg = base.ReplyAsync(message, isTTS, embed, options, allowedMentions, messageReference); - replySpan.Finish(); - return msg; - } } } \ No newline at end of file diff --git a/src/Core/TransactionModuleBase.cs b/src/Core/TransactionModuleBase.cs new file mode 100644 index 0000000..111ae18 --- /dev/null +++ b/src/Core/TransactionModuleBase.cs @@ -0,0 +1,45 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Discord; +using Discord.Commands; +using Sentry; + +namespace Geekbot.Core +{ + public class TransactionModuleBase : ModuleBase + { + protected ITransaction Transaction; + + protected override void BeforeExecute(CommandInfo command) + { + base.BeforeExecute(command); + + // Transaction Setup + Transaction = SentrySdk.StartTransaction(new Transaction(command.Name, "Exec")); + Transaction.SetTags(new [] + { + new KeyValuePair("GuildId", Context.Guild.Id.ToString()), + new KeyValuePair("Guild", Context.Guild.Name), + }); + Transaction.User = new User() + { + Id = Context.User.Id.ToString(), + Username = Context.User.Username, + }; + } + + protected override void AfterExecute(CommandInfo command) + { + base.AfterExecute(command); + Transaction.Finish(); + } + + protected override Task ReplyAsync(string message = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, AllowedMentions allowedMentions = null, MessageReference messageReference = null) + { + var replySpan = Transaction.StartChild("Reply"); + var msg = base.ReplyAsync(message, isTTS, embed, options, allowedMentions, messageReference); + replySpan.Finish(); + return msg; + } + } +} \ No newline at end of file From f02c30e66028930798a3146a188fc1f1ecf8f990 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Fri, 17 Sep 2021 14:30:50 +0200 Subject: [PATCH 344/443] Remove the mod command class --- src/Bot/Commands/Admin/Mod.cs | 38 ----------------------------------- 1 file changed, 38 deletions(-) delete mode 100644 src/Bot/Commands/Admin/Mod.cs diff --git a/src/Bot/Commands/Admin/Mod.cs b/src/Bot/Commands/Admin/Mod.cs deleted file mode 100644 index b642680..0000000 --- a/src/Bot/Commands/Admin/Mod.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System; -using System.Threading.Tasks; -using Discord; -using Discord.Commands; -using Geekbot.Core.CommandPreconditions; -using Geekbot.Core.ErrorHandling; - -namespace Geekbot.Bot.Commands.Admin -{ - [Group("mod")] - [RequireUserPermission(GuildPermission.KickMembers)] - [RequireUserPermission(GuildPermission.ManageMessages)] - [RequireUserPermission(GuildPermission.ManageRoles)] - [DisableInDirectMessage] - public class Mod : ModuleBase - { - private readonly IErrorHandler _errorHandler; - - public Mod(IErrorHandler errorHandler) - { - _errorHandler = errorHandler; - } - - [Command("namehistory", RunMode = RunMode.Async)] - [Summary("See past usernames of an user")] - public async Task UsernameHistory([Summary("@someone")] IUser user) - { - try - { - await Context.Channel.SendMessageAsync("This command has been removed due to low usage and excessively high database usage"); - } - catch (Exception e) - { - await _errorHandler.HandleCommandException(e, Context); - } - } - } -} \ No newline at end of file From a1f8d033c28e97b7963825f7492b423d21b62f2a Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Fri, 17 Sep 2021 14:31:24 +0200 Subject: [PATCH 345/443] Use the TransactionModuleBase for all commands that haven't implemented GeekbotCommandBase --- src/Bot/Commands/Games/Pokedex.cs | 3 ++- src/Bot/Commands/Integrations/LolMmr/LolMmr.cs | 2 +- src/Bot/Commands/Integrations/MagicTheGathering.cs | 3 ++- .../Commands/Integrations/UbranDictionary/UrbanDictionary.cs | 2 +- src/Bot/Commands/Integrations/Wikipedia.cs | 3 ++- src/Bot/Commands/Integrations/Youtube.cs | 3 ++- .../Commands/Randomness/BenedictCumberbatchNameGenerator.cs | 3 ++- src/Bot/Commands/Randomness/Cat/Cat.cs | 2 +- src/Bot/Commands/Randomness/Chuck/ChuckNorrisJokes.cs | 2 +- src/Bot/Commands/Randomness/Dad/DadJokes.cs | 2 +- src/Bot/Commands/Randomness/Dog/Dog.cs | 2 +- src/Bot/Commands/Randomness/Fortune.cs | 3 ++- src/Bot/Commands/Randomness/Greetings/Greetings.cs | 2 +- src/Bot/Commands/Randomness/Kanye/Kanye.cs | 2 +- src/Bot/Commands/Randomness/RandomAnimals.cs | 3 ++- src/Bot/Commands/Randomness/Slap.cs | 3 ++- src/Bot/Commands/User/GuildInfo.cs | 3 ++- src/Bot/Commands/Utils/AvatarGetter.cs | 3 ++- src/Bot/Commands/Utils/Changelog/Changelog.cs | 2 +- src/Bot/Commands/Utils/Dice.cs | 3 ++- src/Bot/Commands/Utils/Emojify.cs | 3 ++- src/Bot/Commands/Utils/Help.cs | 3 ++- src/Bot/Commands/Utils/Info.cs | 2 +- src/Bot/Commands/Utils/Lmgtfy.cs | 3 ++- src/Bot/Commands/Utils/Ping.cs | 3 ++- 25 files changed, 40 insertions(+), 25 deletions(-) diff --git a/src/Bot/Commands/Games/Pokedex.cs b/src/Bot/Commands/Games/Pokedex.cs index 426761c..325ee8c 100644 --- a/src/Bot/Commands/Games/Pokedex.cs +++ b/src/Bot/Commands/Games/Pokedex.cs @@ -3,13 +3,14 @@ using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; +using Geekbot.Core; using Geekbot.Core.ErrorHandling; using Geekbot.Core.Extensions; using PokeAPI; namespace Geekbot.Bot.Commands.Games { - public class Pokedex : ModuleBase + public class Pokedex : TransactionModuleBase { private readonly IErrorHandler _errorHandler; diff --git a/src/Bot/Commands/Integrations/LolMmr/LolMmr.cs b/src/Bot/Commands/Integrations/LolMmr/LolMmr.cs index 4e75034..3049617 100644 --- a/src/Bot/Commands/Integrations/LolMmr/LolMmr.cs +++ b/src/Bot/Commands/Integrations/LolMmr/LolMmr.cs @@ -10,7 +10,7 @@ using Geekbot.Core.ErrorHandling; namespace Geekbot.Bot.Commands.Integrations.LolMmr { - public class LolMmr : ModuleBase + public class LolMmr : TransactionModuleBase { private readonly IErrorHandler _errorHandler; diff --git a/src/Bot/Commands/Integrations/MagicTheGathering.cs b/src/Bot/Commands/Integrations/MagicTheGathering.cs index b179d6e..359b41e 100644 --- a/src/Bot/Commands/Integrations/MagicTheGathering.cs +++ b/src/Bot/Commands/Integrations/MagicTheGathering.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; +using Geekbot.Core; using Geekbot.Core.Converters; using Geekbot.Core.ErrorHandling; using Geekbot.Core.Extensions; @@ -11,7 +12,7 @@ using MtgApiManager.Lib.Service; namespace Geekbot.Bot.Commands.Integrations { - public class MagicTheGathering : ModuleBase + public class MagicTheGathering : TransactionModuleBase { private readonly IErrorHandler _errorHandler; private readonly IMtgManaConverter _manaConverter; diff --git a/src/Bot/Commands/Integrations/UbranDictionary/UrbanDictionary.cs b/src/Bot/Commands/Integrations/UbranDictionary/UrbanDictionary.cs index 72bc8cd..9060b91 100644 --- a/src/Bot/Commands/Integrations/UbranDictionary/UrbanDictionary.cs +++ b/src/Bot/Commands/Integrations/UbranDictionary/UrbanDictionary.cs @@ -9,7 +9,7 @@ using Geekbot.Core.Extensions; namespace Geekbot.Bot.Commands.Integrations.UbranDictionary { - public class UrbanDictionary : ModuleBase + public class UrbanDictionary : TransactionModuleBase { private readonly IErrorHandler _errorHandler; diff --git a/src/Bot/Commands/Integrations/Wikipedia.cs b/src/Bot/Commands/Integrations/Wikipedia.cs index 709f974..82f42a0 100644 --- a/src/Bot/Commands/Integrations/Wikipedia.cs +++ b/src/Bot/Commands/Integrations/Wikipedia.cs @@ -5,6 +5,7 @@ using System.Text; using System.Threading.Tasks; using Discord; using Discord.Commands; +using Geekbot.Core; using Geekbot.Core.Database; using Geekbot.Core.ErrorHandling; using Geekbot.Core.Extensions; @@ -14,7 +15,7 @@ using HtmlAgilityPack; namespace Geekbot.Bot.Commands.Integrations { - public class Wikipedia : ModuleBase + public class Wikipedia : TransactionModuleBase { private readonly IErrorHandler _errorHandler; private readonly IWikipediaClient _wikipediaClient; diff --git a/src/Bot/Commands/Integrations/Youtube.cs b/src/Bot/Commands/Integrations/Youtube.cs index 60ebdaa..74fcf52 100644 --- a/src/Bot/Commands/Integrations/Youtube.cs +++ b/src/Bot/Commands/Integrations/Youtube.cs @@ -1,6 +1,7 @@ using System; using System.Threading.Tasks; using Discord.Commands; +using Geekbot.Core; using Geekbot.Core.ErrorHandling; using Geekbot.Core.GlobalSettings; using Google.Apis.Services; @@ -8,7 +9,7 @@ using Google.Apis.YouTube.v3; namespace Geekbot.Bot.Commands.Integrations { - public class Youtube : ModuleBase + public class Youtube : TransactionModuleBase { private readonly IGlobalSettings _globalSettings; private readonly IErrorHandler _errorHandler; diff --git a/src/Bot/Commands/Randomness/BenedictCumberbatchNameGenerator.cs b/src/Bot/Commands/Randomness/BenedictCumberbatchNameGenerator.cs index 995c921..23187bd 100644 --- a/src/Bot/Commands/Randomness/BenedictCumberbatchNameGenerator.cs +++ b/src/Bot/Commands/Randomness/BenedictCumberbatchNameGenerator.cs @@ -2,12 +2,13 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; using Discord.Commands; +using Geekbot.Core; using Geekbot.Core.ErrorHandling; using Geekbot.Core.RandomNumberGenerator; namespace Geekbot.Bot.Commands.Randomness { - public class BenedictCumberbatchNameGenerator : ModuleBase + public class BenedictCumberbatchNameGenerator : TransactionModuleBase { private readonly IErrorHandler _errorHandler; private readonly IRandomNumberGenerator _randomNumberGenerator; diff --git a/src/Bot/Commands/Randomness/Cat/Cat.cs b/src/Bot/Commands/Randomness/Cat/Cat.cs index 7bd7e57..1198113 100644 --- a/src/Bot/Commands/Randomness/Cat/Cat.cs +++ b/src/Bot/Commands/Randomness/Cat/Cat.cs @@ -7,7 +7,7 @@ using Geekbot.Core.ErrorHandling; namespace Geekbot.Bot.Commands.Randomness.Cat { - public class Cat : ModuleBase + public class Cat : TransactionModuleBase { private readonly IErrorHandler _errorHandler; diff --git a/src/Bot/Commands/Randomness/Chuck/ChuckNorrisJokes.cs b/src/Bot/Commands/Randomness/Chuck/ChuckNorrisJokes.cs index be328b4..e2b1f16 100644 --- a/src/Bot/Commands/Randomness/Chuck/ChuckNorrisJokes.cs +++ b/src/Bot/Commands/Randomness/Chuck/ChuckNorrisJokes.cs @@ -7,7 +7,7 @@ using Geekbot.Core.ErrorHandling; namespace Geekbot.Bot.Commands.Randomness.Chuck { - public class ChuckNorrisJokes : ModuleBase + public class ChuckNorrisJokes : TransactionModuleBase { private readonly IErrorHandler _errorHandler; diff --git a/src/Bot/Commands/Randomness/Dad/DadJokes.cs b/src/Bot/Commands/Randomness/Dad/DadJokes.cs index 136650c..67f9679 100644 --- a/src/Bot/Commands/Randomness/Dad/DadJokes.cs +++ b/src/Bot/Commands/Randomness/Dad/DadJokes.cs @@ -6,7 +6,7 @@ using Geekbot.Core.ErrorHandling; namespace Geekbot.Bot.Commands.Randomness.Dad { - public class DadJokes : ModuleBase + public class DadJokes : TransactionModuleBase { private readonly IErrorHandler _errorHandler; diff --git a/src/Bot/Commands/Randomness/Dog/Dog.cs b/src/Bot/Commands/Randomness/Dog/Dog.cs index 051dbf3..39e57c7 100644 --- a/src/Bot/Commands/Randomness/Dog/Dog.cs +++ b/src/Bot/Commands/Randomness/Dog/Dog.cs @@ -7,7 +7,7 @@ using Geekbot.Core.ErrorHandling; namespace Geekbot.Bot.Commands.Randomness.Dog { - public class Dog : ModuleBase + public class Dog : TransactionModuleBase { private readonly IErrorHandler _errorHandler; diff --git a/src/Bot/Commands/Randomness/Fortune.cs b/src/Bot/Commands/Randomness/Fortune.cs index cc31536..1157603 100644 --- a/src/Bot/Commands/Randomness/Fortune.cs +++ b/src/Bot/Commands/Randomness/Fortune.cs @@ -1,10 +1,11 @@ using System.Threading.Tasks; using Discord.Commands; +using Geekbot.Core; using Geekbot.Core.Media; namespace Geekbot.Bot.Commands.Randomness { - public class Fortune : ModuleBase + public class Fortune : TransactionModuleBase { private readonly IFortunesProvider _fortunes; diff --git a/src/Bot/Commands/Randomness/Greetings/Greetings.cs b/src/Bot/Commands/Randomness/Greetings/Greetings.cs index cd69010..c32ee6d 100644 --- a/src/Bot/Commands/Randomness/Greetings/Greetings.cs +++ b/src/Bot/Commands/Randomness/Greetings/Greetings.cs @@ -8,7 +8,7 @@ using Geekbot.Core.Extensions; namespace Geekbot.Bot.Commands.Randomness.Greetings { - public class Greetings : ModuleBase + public class Greetings : TransactionModuleBase { private readonly IErrorHandler _errorHandler; diff --git a/src/Bot/Commands/Randomness/Kanye/Kanye.cs b/src/Bot/Commands/Randomness/Kanye/Kanye.cs index 6ad67cb..e5d2e95 100644 --- a/src/Bot/Commands/Randomness/Kanye/Kanye.cs +++ b/src/Bot/Commands/Randomness/Kanye/Kanye.cs @@ -6,7 +6,7 @@ using Geekbot.Core.ErrorHandling; namespace Geekbot.Bot.Commands.Randomness.Kanye { - public class Kanye : ModuleBase + public class Kanye : TransactionModuleBase { private readonly IErrorHandler _errorHandler; diff --git a/src/Bot/Commands/Randomness/RandomAnimals.cs b/src/Bot/Commands/Randomness/RandomAnimals.cs index b9c1bdd..5493485 100644 --- a/src/Bot/Commands/Randomness/RandomAnimals.cs +++ b/src/Bot/Commands/Randomness/RandomAnimals.cs @@ -1,11 +1,12 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; +using Geekbot.Core; using Geekbot.Core.Media; namespace Geekbot.Bot.Commands.Randomness { - public class RandomAnimals : ModuleBase + public class RandomAnimals : TransactionModuleBase { private readonly IMediaProvider _mediaProvider; diff --git a/src/Bot/Commands/Randomness/Slap.cs b/src/Bot/Commands/Randomness/Slap.cs index f5d21b8..c99c325 100644 --- a/src/Bot/Commands/Randomness/Slap.cs +++ b/src/Bot/Commands/Randomness/Slap.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; +using Geekbot.Core; using Geekbot.Core.Database; using Geekbot.Core.Database.Models; using Geekbot.Core.ErrorHandling; @@ -11,7 +12,7 @@ using Geekbot.Core.Extensions; namespace Geekbot.Bot.Commands.Randomness { - public class Slap : ModuleBase + public class Slap : TransactionModuleBase { private readonly IErrorHandler _errorHandler; private readonly DatabaseContext _database; diff --git a/src/Bot/Commands/User/GuildInfo.cs b/src/Bot/Commands/User/GuildInfo.cs index c596186..c7a3b27 100644 --- a/src/Bot/Commands/User/GuildInfo.cs +++ b/src/Bot/Commands/User/GuildInfo.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; +using Geekbot.Core; using Geekbot.Core.CommandPreconditions; using Geekbot.Core.Database; using Geekbot.Core.ErrorHandling; @@ -11,7 +12,7 @@ using Geekbot.Core.Levels; namespace Geekbot.Bot.Commands.User { - public class GuildInfo : ModuleBase + public class GuildInfo : TransactionModuleBase { private readonly IErrorHandler _errorHandler; private readonly DatabaseContext _database; diff --git a/src/Bot/Commands/Utils/AvatarGetter.cs b/src/Bot/Commands/Utils/AvatarGetter.cs index 2aa6b07..458eec8 100644 --- a/src/Bot/Commands/Utils/AvatarGetter.cs +++ b/src/Bot/Commands/Utils/AvatarGetter.cs @@ -2,11 +2,12 @@ using System.Threading.Tasks; using Discord; using Discord.Commands; +using Geekbot.Core; using Geekbot.Core.ErrorHandling; namespace Geekbot.Bot.Commands.Utils { - public class AvatarGetter : ModuleBase + public class AvatarGetter : TransactionModuleBase { private readonly IErrorHandler _errorHandler; diff --git a/src/Bot/Commands/Utils/Changelog/Changelog.cs b/src/Bot/Commands/Utils/Changelog/Changelog.cs index 92d5ddd..989ac0d 100644 --- a/src/Bot/Commands/Utils/Changelog/Changelog.cs +++ b/src/Bot/Commands/Utils/Changelog/Changelog.cs @@ -11,7 +11,7 @@ using Geekbot.Core.ErrorHandling; namespace Geekbot.Bot.Commands.Utils.Changelog { - public class Changelog : ModuleBase + public class Changelog : TransactionModuleBase { private readonly DiscordSocketClient _client; private readonly IErrorHandler _errorHandler; diff --git a/src/Bot/Commands/Utils/Dice.cs b/src/Bot/Commands/Utils/Dice.cs index 0668493..c57001f 100644 --- a/src/Bot/Commands/Utils/Dice.cs +++ b/src/Bot/Commands/Utils/Dice.cs @@ -3,12 +3,13 @@ using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using Discord.Commands; +using Geekbot.Core; using Geekbot.Core.DiceParser; using Geekbot.Core.ErrorHandling; namespace Geekbot.Bot.Commands.Utils { - public class Dice : ModuleBase + public class Dice : TransactionModuleBase { private readonly IErrorHandler _errorHandler; private readonly IDiceParser _diceParser; diff --git a/src/Bot/Commands/Utils/Emojify.cs b/src/Bot/Commands/Utils/Emojify.cs index 8bb880e..63b5456 100644 --- a/src/Bot/Commands/Utils/Emojify.cs +++ b/src/Bot/Commands/Utils/Emojify.cs @@ -1,12 +1,13 @@ using System; using System.Threading.Tasks; using Discord.Commands; +using Geekbot.Core; using Geekbot.Core.Converters; using Geekbot.Core.ErrorHandling; namespace Geekbot.Bot.Commands.Utils { - public class Emojify : ModuleBase + public class Emojify : TransactionModuleBase { private readonly IEmojiConverter _emojiConverter; private readonly IErrorHandler _errorHandler; diff --git a/src/Bot/Commands/Utils/Help.cs b/src/Bot/Commands/Utils/Help.cs index 58630fe..7377bfa 100644 --- a/src/Bot/Commands/Utils/Help.cs +++ b/src/Bot/Commands/Utils/Help.cs @@ -3,11 +3,12 @@ using System.Text; using System.Threading.Tasks; using Discord; using Discord.Commands; +using Geekbot.Core; using Geekbot.Core.ErrorHandling; namespace Geekbot.Bot.Commands.Utils { - public class Help : ModuleBase + public class Help : TransactionModuleBase { private readonly IErrorHandler _errorHandler; diff --git a/src/Bot/Commands/Utils/Info.cs b/src/Bot/Commands/Utils/Info.cs index 663dd07..912528d 100644 --- a/src/Bot/Commands/Utils/Info.cs +++ b/src/Bot/Commands/Utils/Info.cs @@ -11,7 +11,7 @@ using Geekbot.Core.Extensions; namespace Geekbot.Bot.Commands.Utils { - public class Info : ModuleBase + public class Info : TransactionModuleBase { private readonly DiscordSocketClient _client; private readonly CommandService _commands; diff --git a/src/Bot/Commands/Utils/Lmgtfy.cs b/src/Bot/Commands/Utils/Lmgtfy.cs index 6063cf9..76fa6fa 100644 --- a/src/Bot/Commands/Utils/Lmgtfy.cs +++ b/src/Bot/Commands/Utils/Lmgtfy.cs @@ -2,11 +2,12 @@ using System; using System.Threading.Tasks; using System.Web; using Discord.Commands; +using Geekbot.Core; using Geekbot.Core.ErrorHandling; namespace Geekbot.Bot.Commands.Utils { - public class Lmgtfy : ModuleBase + public class Lmgtfy : TransactionModuleBase { private readonly IErrorHandler _errorHandler; diff --git a/src/Bot/Commands/Utils/Ping.cs b/src/Bot/Commands/Utils/Ping.cs index d4faa53..ee751cd 100644 --- a/src/Bot/Commands/Utils/Ping.cs +++ b/src/Bot/Commands/Utils/Ping.cs @@ -1,9 +1,10 @@ using System.Threading.Tasks; using Discord.Commands; +using Geekbot.Core; namespace Geekbot.Bot.Commands.Utils { - public class Ping : ModuleBase + public class Ping : TransactionModuleBase { [Command("👀", RunMode = RunMode.Async)] [Summary("Look at the bot.")] From d16828077d5098a807027857ab5871d4c85668bf Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Fri, 17 Sep 2021 14:33:55 +0200 Subject: [PATCH 346/443] Set Transaction Status to OK by default --- src/Bot/Commands/Games/Roll/Roll.cs | 2 -- src/Bot/Commands/Utils/Quote/Quote.cs | 1 - src/Core/TransactionModuleBase.cs | 1 + 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Bot/Commands/Games/Roll/Roll.cs b/src/Bot/Commands/Games/Roll/Roll.cs index 0361cbd..172126b 100644 --- a/src/Bot/Commands/Games/Roll/Roll.cs +++ b/src/Bot/Commands/Games/Roll/Roll.cs @@ -76,8 +76,6 @@ namespace Geekbot.Bot.Commands.Games.Roll { await ReplyAsync(string.Format(Localization.Roll.RolledNoGuess, Context.Message.Author.Mention, number)); } - - Transaction.Status = SpanStatus.Ok; } catch (Exception e) { diff --git a/src/Bot/Commands/Utils/Quote/Quote.cs b/src/Bot/Commands/Utils/Quote/Quote.cs index fdf1030..8fbecbc 100644 --- a/src/Bot/Commands/Utils/Quote/Quote.cs +++ b/src/Bot/Commands/Utils/Quote/Quote.cs @@ -65,7 +65,6 @@ namespace Geekbot.Bot.Commands.Utils.Quote var embed = QuoteBuilder(quote.FirstOrDefault()); await ReplyAsync("", false, embed.Build()); replySpan.Finish(); - Transaction.Status = SpanStatus.Ok; } catch (Exception e) { diff --git a/src/Core/TransactionModuleBase.cs b/src/Core/TransactionModuleBase.cs index 111ae18..c35800b 100644 --- a/src/Core/TransactionModuleBase.cs +++ b/src/Core/TransactionModuleBase.cs @@ -26,6 +26,7 @@ namespace Geekbot.Core Id = Context.User.Id.ToString(), Username = Context.User.Username, }; + Transaction.Status = SpanStatus.Ok; } protected override void AfterExecute(CommandInfo command) From 0a9099a6d28ad99a5696f8c5548341ba666d1f00 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Fri, 17 Sep 2021 14:53:24 +0200 Subject: [PATCH 347/443] Move Sentry Init priority between the discord connection and dependency registration --- src/Bot/Program.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Bot/Program.cs b/src/Bot/Program.cs index 4d7526b..883d92b 100644 --- a/src/Bot/Program.cs +++ b/src/Bot/Program.cs @@ -85,6 +85,8 @@ namespace Geekbot.Bot await Login(); _logger.Information(LogSource.Geekbot, $"Now Connected as {_client.CurrentUser.Username} to {_client.Guilds.Count} Servers"); await _client.SetGameAsync(_globalSettings.GetKey("Game")); + + RegisterSentry(); _logger.Information(LogSource.Geekbot, "Loading Dependencies and Handlers"); RegisterDependencies(); @@ -93,8 +95,6 @@ namespace Geekbot.Bot _logger.Information(LogSource.Api, "Starting Web API"); StartWebApi(); - RegisterSentry(); - _logger.Information(LogSource.Geekbot, "Done and ready for use"); await Task.Delay(-1); From 3d117aebe1d8007ed644fcdfcc57d912cffcb140 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Fri, 17 Sep 2021 15:12:22 +0200 Subject: [PATCH 348/443] Split reply and quote embed building trace apart in !quote --- src/Bot/Commands/Utils/Quote/Quote.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Bot/Commands/Utils/Quote/Quote.cs b/src/Bot/Commands/Utils/Quote/Quote.cs index 8fbecbc..835cdcf 100644 --- a/src/Bot/Commands/Utils/Quote/Quote.cs +++ b/src/Bot/Commands/Utils/Quote/Quote.cs @@ -61,10 +61,10 @@ namespace Geekbot.Bot.Commands.Utils.Quote var quote = _database.Quotes.Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong())).Skip(random).Take(1); getQuoteFromDbSpan.Finish(); - var replySpan = Transaction.StartChild("Reply"); + var buildQuoteEmbedSpan = Transaction.StartChild("BuildQuoteEmbed"); var embed = QuoteBuilder(quote.FirstOrDefault()); + buildQuoteEmbedSpan.Finish(); await ReplyAsync("", false, embed.Build()); - replySpan.Finish(); } catch (Exception e) { From 954c6c2be30ef93c96f3fdef8f61f25cdac718f3 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Fri, 17 Sep 2021 15:55:13 +0200 Subject: [PATCH 349/443] Remove Guild ID from the tracing tags --- src/Core/TransactionModuleBase.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Core/TransactionModuleBase.cs b/src/Core/TransactionModuleBase.cs index c35800b..726c355 100644 --- a/src/Core/TransactionModuleBase.cs +++ b/src/Core/TransactionModuleBase.cs @@ -18,7 +18,6 @@ namespace Geekbot.Core Transaction = SentrySdk.StartTransaction(new Transaction(command.Name, "Exec")); Transaction.SetTags(new [] { - new KeyValuePair("GuildId", Context.Guild.Id.ToString()), new KeyValuePair("Guild", Context.Guild.Name), }); Transaction.User = new User() From 1b9d8732d5c9cb50c42e40312e719b6466a0500c Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Sun, 19 Sep 2021 00:57:10 +0200 Subject: [PATCH 350/443] Add tracing to the !quote embed builder --- src/Bot/Commands/Utils/Quote/Quote.cs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Bot/Commands/Utils/Quote/Quote.cs b/src/Bot/Commands/Utils/Quote/Quote.cs index 835cdcf..372bed7 100644 --- a/src/Bot/Commands/Utils/Quote/Quote.cs +++ b/src/Bot/Commands/Utils/Quote/Quote.cs @@ -268,29 +268,32 @@ namespace Geekbot.Bot.Commands.Utils.Quote private EmbedBuilder QuoteBuilder(QuoteModel quote) { + var getEmbedUserSpan = Transaction.StartChild("GetEmbedUser"); var user = Context.Client.GetUserAsync(quote.UserId.AsUlong()).Result; if (user == null) { + var getEmbedUserFromRepoSpan = Transaction.StartChild("GetEmbedUserFromRepo"); var fallbackUserFromRepo = _userRepository.Get(quote.UserId.AsUlong()); user = new UserPolyfillDto() { Username = fallbackUserFromRepo?.Username ?? "Unknown User", AvatarUrl = fallbackUserFromRepo?.AvatarUrl }; + getEmbedUserFromRepoSpan.Finish(); } + getEmbedUserSpan.Finish(); + + var embedBuilderSpan = Transaction.StartChild("EmbedBuilder"); var eb = new EmbedBuilder(); eb.WithColor(new Color(143, 167, 232)); - if (quote.InternalId == 0) - { - eb.Title = $"{user.Username} @ {quote.Time.Day}.{quote.Time.Month}.{quote.Time.Year}"; - } - else - { - eb.Title = $"#{quote.InternalId} | {user.Username} @ {quote.Time.Day}.{quote.Time.Month}.{quote.Time.Year}"; - } + eb.Title = quote.InternalId == 0 + ? $"{user.Username} @ {quote.Time.Day}.{quote.Time.Month}.{quote.Time.Year}" + : $"#{quote.InternalId} | {user.Username} @ {quote.Time.Day}.{quote.Time.Month}.{quote.Time.Year}"; eb.Description = quote.Quote; eb.ThumbnailUrl = user.GetAvatarUrl(); if (quote.Image != null) eb.ImageUrl = quote.Image; + embedBuilderSpan.Finish(); + return eb; } From 447c6d80422237959dfb7867118c37e888b1b243 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Sun, 19 Sep 2021 00:58:00 +0200 Subject: [PATCH 351/443] Remove a database call from !quote by delegating the randomness to the database. --- src/Bot/Commands/Utils/Quote/Quote.cs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/Bot/Commands/Utils/Quote/Quote.cs b/src/Bot/Commands/Utils/Quote/Quote.cs index 372bed7..e85c6fa 100644 --- a/src/Bot/Commands/Utils/Quote/Quote.cs +++ b/src/Bot/Commands/Utils/Quote/Quote.cs @@ -45,22 +45,17 @@ namespace Geekbot.Bot.Commands.Utils.Quote { try { - var getTotalSpan = Transaction.StartChild("TotalQuotes"); - var totalQuotes = await _database.Quotes.CountAsync(e => e.GuildId.Equals(Context.Guild.Id.AsLong())); - getTotalSpan.Finish(); - - if (totalQuotes == 0) + var getQuoteFromDbSpan = Transaction.StartChild("GetQuoteFromDB"); + var quote = _database.Quotes.FromSqlInterpolated($"select * from \"Quotes\" where \"GuildId\" = {Context.Guild.Id} order by random() limit 1"); + getQuoteFromDbSpan.Finish(); + + if (!quote.Any()) { await ReplyAsync(Localization.Quote.NoQuotesFound); Transaction.Status = SpanStatus.NotFound; return; } - var getQuoteFromDbSpan = Transaction.StartChild("GetQuoteFromDB"); - var random = _randomNumberGenerator.Next(0, totalQuotes - 1); - var quote = _database.Quotes.Where(e => e.GuildId.Equals(Context.Guild.Id.AsLong())).Skip(random).Take(1); - getQuoteFromDbSpan.Finish(); - var buildQuoteEmbedSpan = Transaction.StartChild("BuildQuoteEmbed"); var embed = QuoteBuilder(quote.FirstOrDefault()); buildQuoteEmbedSpan.Finish(); From 85d06b76e009b9542acba34d3bed6f2b1a328c8c Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Sun, 19 Sep 2021 16:06:11 +0200 Subject: [PATCH 352/443] Add --disable-gateway parameter to the run parameters to stop the bot from connecting to discord. Useful when working on the web-api --- src/Bot/Program.cs | 17 ++++++++++------- src/Core/RunParameters.cs | 3 +++ src/Web/WebApiStartup.cs | 8 +++++--- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/Bot/Program.cs b/src/Bot/Program.cs index 883d92b..31b67e0 100644 --- a/src/Bot/Program.cs +++ b/src/Bot/Program.cs @@ -80,17 +80,20 @@ namespace Geekbot.Bot var database = ConnectToDatabase(); _globalSettings = new GlobalSettings(database); - logger.Information(LogSource.Geekbot, "Connecting to Discord"); - SetupDiscordClient(); - await Login(); - _logger.Information(LogSource.Geekbot, $"Now Connected as {_client.CurrentUser.Username} to {_client.Guilds.Count} Servers"); - await _client.SetGameAsync(_globalSettings.GetKey("Game")); + if (!runParameters.DisableGateway) + { + logger.Information(LogSource.Geekbot, "Connecting to Discord"); + SetupDiscordClient(); + await Login(); + _logger.Information(LogSource.Geekbot, $"Now Connected as {_client.CurrentUser.Username} to {_client.Guilds.Count} Servers"); + await _client.SetGameAsync(_globalSettings.GetKey("Game")); + } RegisterSentry(); _logger.Information(LogSource.Geekbot, "Loading Dependencies and Handlers"); RegisterDependencies(); - await RegisterHandlers(); + if (!runParameters.DisableGateway) await RegisterHandlers(); _logger.Information(LogSource.Api, "Starting Web API"); StartWebApi(); @@ -187,9 +190,9 @@ namespace Geekbot.Bot services.AddSingleton(diceParser); services.AddSingleton(_reactionListener); services.AddSingleton(_guildSettingsManager); - services.AddSingleton(_client); services.AddTransient(e => new HighscoreManager(_databaseInitializer.Initialize(), _userRepository)); services.AddTransient(e => _databaseInitializer.Initialize()); + if (!_runParameters.DisableGateway) services.AddSingleton(_client); _servicesProvider = services.BuildServiceProvider(); } diff --git a/src/Core/RunParameters.cs b/src/Core/RunParameters.cs index 3210587..a886a98 100644 --- a/src/Core/RunParameters.cs +++ b/src/Core/RunParameters.cs @@ -20,6 +20,9 @@ namespace Geekbot.Core [Option('e', "expose-errors", HelpText = "Shows internal errors in the chat (default: false) (env: EXPOSE_ERRORS)")] public bool ExposeErrors { get; set; } = ParamFallback("EXPOSE_ERRORS", false); + + [Option("disable-gateway", HelpText = "Disables the Discord Gateway (default: false) (env: GATEWAY_DISABLE)")] + public bool DisableGateway { get; set; } = ParamFallback("GATEWAY_DISABLE", false); /************************************ * Database * diff --git a/src/Web/WebApiStartup.cs b/src/Web/WebApiStartup.cs index 0c18886..9c8a805 100644 --- a/src/Web/WebApiStartup.cs +++ b/src/Web/WebApiStartup.cs @@ -37,12 +37,14 @@ namespace Geekbot.Web options.AddPolicy("AllowSpecificOrigin", builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()); }); - - services.AddSingleton(commandService); + services.AddSingleton(databaseContext); - services.AddSingleton(client); services.AddSingleton(globalSettings); services.AddSingleton(highscoreManager); + + if (runParameters.DisableGateway) return; + services.AddSingleton(commandService); + services.AddSingleton(client); }) .Configure(app => { From d81fb2a3d977786079a69525f1d17357fb520ea0 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Sun, 19 Sep 2021 16:11:06 +0200 Subject: [PATCH 353/443] Add initial interaction support --- .../Interactions/InteractionController.cs | 90 +++++++++++++++++++ .../Model/ApplicationCommandOption.cs | 16 ++++ .../Interactions/Model/Interaction.cs | 22 +++++ .../Interactions/Model/InteractionData.cs | 14 +++ .../Interactions/Model/InteractionOption.cs | 12 +++ .../Model/InteractionResolvedData.cs | 15 ++++ .../Interactions/Model/InteractionResponse.cs | 13 +++ .../Model/InteractionResponseData.cs | 16 ++++ .../Model/InteractionResponseType.cs | 11 +++ .../Interactions/Model/InteractionType.cs | 9 ++ .../Model/MessageComponents/Component.cs | 7 ++ .../Interactions/Model/Resolved/Channel.cs | 12 +++ .../Model/Resolved/ChannelType.cs | 17 ++++ .../Interactions/Model/Resolved/Member.cs | 16 ++++ .../Interactions/Model/Resolved/RoleTag.cs | 9 ++ .../Interactions/Model/Resolved/Roles.cs | 15 ++++ .../Model/Resolved/ThreadMetadata.cs | 13 +++ .../Interactions/Model/Resolved/User.cs | 21 +++++ src/Web/Web.csproj | 4 + src/Web/WebApiStartup.cs | 5 +- 20 files changed, 336 insertions(+), 1 deletion(-) create mode 100644 src/Web/Controllers/Interactions/InteractionController.cs create mode 100644 src/Web/Controllers/Interactions/Model/ApplicationCommandOption.cs create mode 100644 src/Web/Controllers/Interactions/Model/Interaction.cs create mode 100644 src/Web/Controllers/Interactions/Model/InteractionData.cs create mode 100644 src/Web/Controllers/Interactions/Model/InteractionOption.cs create mode 100644 src/Web/Controllers/Interactions/Model/InteractionResolvedData.cs create mode 100644 src/Web/Controllers/Interactions/Model/InteractionResponse.cs create mode 100644 src/Web/Controllers/Interactions/Model/InteractionResponseData.cs create mode 100644 src/Web/Controllers/Interactions/Model/InteractionResponseType.cs create mode 100644 src/Web/Controllers/Interactions/Model/InteractionType.cs create mode 100644 src/Web/Controllers/Interactions/Model/MessageComponents/Component.cs create mode 100644 src/Web/Controllers/Interactions/Model/Resolved/Channel.cs create mode 100644 src/Web/Controllers/Interactions/Model/Resolved/ChannelType.cs create mode 100644 src/Web/Controllers/Interactions/Model/Resolved/Member.cs create mode 100644 src/Web/Controllers/Interactions/Model/Resolved/RoleTag.cs create mode 100644 src/Web/Controllers/Interactions/Model/Resolved/Roles.cs create mode 100644 src/Web/Controllers/Interactions/Model/Resolved/ThreadMetadata.cs create mode 100644 src/Web/Controllers/Interactions/Model/Resolved/User.cs diff --git a/src/Web/Controllers/Interactions/InteractionController.cs b/src/Web/Controllers/Interactions/InteractionController.cs new file mode 100644 index 0000000..3501c06 --- /dev/null +++ b/src/Web/Controllers/Interactions/InteractionController.cs @@ -0,0 +1,90 @@ +using System; +using System.IO; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; +using Geekbot.Core.GlobalSettings; +using Geekbot.Web.Controllers.Interactions.Model; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Sodium; + +namespace Geekbot.Web.Controllers.Interactions +{ + public class InteractionController : Controller + { + private readonly byte[] publicKeyBytes; + + public InteractionController(IGlobalSettings globalSettings) + { + var publicKey = globalSettings.GetKey("DiscordPublicKey"); + publicKeyBytes = Convert.FromHexString(publicKey.AsSpan()); + } + + [HttpPost] + [Route("/interactions")] + public async Task HandleInteraction( + [FromHeader(Name = "X-Signature-Ed25519")] string signature, + [FromHeader(Name = "X-Signature-Timestamp")] string timestamp + ) + { + if (string.IsNullOrEmpty(signature) || string.IsNullOrEmpty(timestamp)) + { + return BadRequest(); + } + + Request.EnableBuffering(); + if (!(await HasValidSignature(signature, timestamp))) + { + return Unauthorized(); + } + + if (Request.Body.CanSeek) Request.Body.Seek(0, SeekOrigin.Begin); + var interaction = await JsonSerializer.DeserializeAsync(Request.Body, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true }).ConfigureAwait(false); + if (interaction is null) throw new JsonException("Failed to deserialize JSON body"); + + return (interaction.Type, interaction.Version) switch + { + (InteractionType.Ping, 1) => Ping(), + (InteractionType.ApplicationCommand, 1) => ApplicationCommand(interaction), + (InteractionType.MessageComponent, 1) => MessageComponent(interaction), + _ => StatusCode(501) + }; + } + + private IActionResult Ping() + { + var response = new InteractionResponse() + { + Type = InteractionResponseType.Pong, + }; + return Ok(response); + } + + private IActionResult ApplicationCommand(Interaction interaction) + { + return StatusCode(501); + } + + private IActionResult MessageComponent(Interaction interaction) + { + return StatusCode(501); + } + + private async Task HasValidSignature(string signature, string timestamp) + { + var timestampBytes = Encoding.Default.GetBytes(timestamp); + var signatureBytes = Convert.FromHexString(signature.AsSpan()); + + var memoryStream = new MemoryStream(); + await Request.Body.CopyToAsync(memoryStream).ConfigureAwait(false); + var body = memoryStream.ToArray(); + + var timestampLength = timestampBytes.Length; + Array.Resize(ref timestampBytes, timestampLength + body.Length); + Array.Copy(body, 0, timestampBytes, timestampLength, body.Length); + + return PublicKeyAuth.VerifyDetached(signatureBytes, timestampBytes, publicKeyBytes); + } + } +} \ No newline at end of file diff --git a/src/Web/Controllers/Interactions/Model/ApplicationCommandOption.cs b/src/Web/Controllers/Interactions/Model/ApplicationCommandOption.cs new file mode 100644 index 0000000..794102b --- /dev/null +++ b/src/Web/Controllers/Interactions/Model/ApplicationCommandOption.cs @@ -0,0 +1,16 @@ +namespace Geekbot.Web.Controllers.Interactions.Model +{ + public enum ApplicationCommandOption + { + SubCommand = 1, + SubCommandGroup = 2, + String = 3, + Integer = 4, + Boolean = 5, + User = 6, + Channel = 7, + Role = 8, + Mentionable = 9, + Number = 10, + } +} \ No newline at end of file diff --git a/src/Web/Controllers/Interactions/Model/Interaction.cs b/src/Web/Controllers/Interactions/Model/Interaction.cs new file mode 100644 index 0000000..6045504 --- /dev/null +++ b/src/Web/Controllers/Interactions/Model/Interaction.cs @@ -0,0 +1,22 @@ +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Text.Json.Serialization; + +namespace Geekbot.Web.Controllers.Interactions.Model +{ + public record Interaction + { + public string Id { get; set; } + public string ApplicationId { get; init; } + [Required] + public InteractionType Type { get; set; } + public InteractionData Data { get; init; } + public string GuildId { get; init; } + public string Name { get; init; } + public string Description { get; init; } + public List Options { get; init; } + public bool DefaultPermission { get; init; } + [Required] + public int Version { get; set; } + } +} \ No newline at end of file diff --git a/src/Web/Controllers/Interactions/Model/InteractionData.cs b/src/Web/Controllers/Interactions/Model/InteractionData.cs new file mode 100644 index 0000000..cb0f594 --- /dev/null +++ b/src/Web/Controllers/Interactions/Model/InteractionData.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; + +namespace Geekbot.Web.Controllers.Interactions.Model +{ + public record InteractionData + { + public string Id { get; set; } + public string Name { get; set; } + public int Type { get; set;} + public InteractionResolvedData Resolved { get; set; } + public List Options { get; set; } + public string TargetId { get; set; } + } +} \ No newline at end of file diff --git a/src/Web/Controllers/Interactions/Model/InteractionOption.cs b/src/Web/Controllers/Interactions/Model/InteractionOption.cs new file mode 100644 index 0000000..c5acfa7 --- /dev/null +++ b/src/Web/Controllers/Interactions/Model/InteractionOption.cs @@ -0,0 +1,12 @@ +using System.Collections.Generic; + +namespace Geekbot.Web.Controllers.Interactions.Model +{ + public record InteractionOption + { + public string Name { get; set; } + public ApplicationCommandOption Type { get; set; } + public string Value { get; set; } + public List Options { get; set; } + } +} \ No newline at end of file diff --git a/src/Web/Controllers/Interactions/Model/InteractionResolvedData.cs b/src/Web/Controllers/Interactions/Model/InteractionResolvedData.cs new file mode 100644 index 0000000..ee90f48 --- /dev/null +++ b/src/Web/Controllers/Interactions/Model/InteractionResolvedData.cs @@ -0,0 +1,15 @@ +using System.Collections.Generic; +using Discord; +using Geekbot.Web.Controllers.Interactions.Model.Resolved; + +namespace Geekbot.Web.Controllers.Interactions.Model +{ + public class InteractionResolvedData + { + public Dictionary Users { get; set; } + public Dictionary Members { get; set; } + public Dictionary Roles { get; set; } + public Dictionary Channels { get; set; } + // public Dictionary Messages { get; set; } + } +} \ No newline at end of file diff --git a/src/Web/Controllers/Interactions/Model/InteractionResponse.cs b/src/Web/Controllers/Interactions/Model/InteractionResponse.cs new file mode 100644 index 0000000..c3a2a1e --- /dev/null +++ b/src/Web/Controllers/Interactions/Model/InteractionResponse.cs @@ -0,0 +1,13 @@ +using System.Text.Json.Serialization; + +namespace Geekbot.Web.Controllers.Interactions.Model +{ + public record InteractionResponse + { + [JsonPropertyName("type")] + public InteractionResponseType Type { get; set; } + + [JsonPropertyName("data")] + public InteractionData Data { get; set; } + } +} \ No newline at end of file diff --git a/src/Web/Controllers/Interactions/Model/InteractionResponseData.cs b/src/Web/Controllers/Interactions/Model/InteractionResponseData.cs new file mode 100644 index 0000000..146e812 --- /dev/null +++ b/src/Web/Controllers/Interactions/Model/InteractionResponseData.cs @@ -0,0 +1,16 @@ +using System.Collections.Generic; +using Discord; +using Geekbot.Web.Controllers.Interactions.Model.MessageComponents; + +namespace Geekbot.Web.Controllers.Interactions.Model +{ + public record InteractionResponseData + { + public bool Tts { get; set; } = false; + public string Content { get; set; } + public List Embeds { get; set; } + public AllowedMentions AllowedMentions { get; set; } + public int Flags { get; set; } + public List Components { get; set; } + } +} \ No newline at end of file diff --git a/src/Web/Controllers/Interactions/Model/InteractionResponseType.cs b/src/Web/Controllers/Interactions/Model/InteractionResponseType.cs new file mode 100644 index 0000000..5bc85dc --- /dev/null +++ b/src/Web/Controllers/Interactions/Model/InteractionResponseType.cs @@ -0,0 +1,11 @@ +namespace Geekbot.Web.Controllers.Interactions.Model +{ + public enum InteractionResponseType + { + Pong = 1, + ChannelMessageWithSource = 4, + DeferredChannelMessageWithSource = 5, + DeferredUpdateMessage = 6, + UpdateMessage = 7, + } +} \ No newline at end of file diff --git a/src/Web/Controllers/Interactions/Model/InteractionType.cs b/src/Web/Controllers/Interactions/Model/InteractionType.cs new file mode 100644 index 0000000..a8421c5 --- /dev/null +++ b/src/Web/Controllers/Interactions/Model/InteractionType.cs @@ -0,0 +1,9 @@ +namespace Geekbot.Web.Controllers.Interactions.Model +{ + public enum InteractionType + { + Ping = 1, + ApplicationCommand = 2, + MessageComponent = 3, + } +} \ No newline at end of file diff --git a/src/Web/Controllers/Interactions/Model/MessageComponents/Component.cs b/src/Web/Controllers/Interactions/Model/MessageComponents/Component.cs new file mode 100644 index 0000000..72097a4 --- /dev/null +++ b/src/Web/Controllers/Interactions/Model/MessageComponents/Component.cs @@ -0,0 +1,7 @@ +namespace Geekbot.Web.Controllers.Interactions.Model.MessageComponents +{ + public record Component + { + + } +} \ No newline at end of file diff --git a/src/Web/Controllers/Interactions/Model/Resolved/Channel.cs b/src/Web/Controllers/Interactions/Model/Resolved/Channel.cs new file mode 100644 index 0000000..4b280d9 --- /dev/null +++ b/src/Web/Controllers/Interactions/Model/Resolved/Channel.cs @@ -0,0 +1,12 @@ +namespace Geekbot.Web.Controllers.Interactions.Model.Resolved +{ + public record Channel + { + public string Id { get; set; } + public ChannelType Type { get; set; } + public string Name { get; set; } + public string ParentId { get; set; } + public ThreadMetadata ThreadMetadata { get; set; } + public string Permissions { get; set; } + } +} \ No newline at end of file diff --git a/src/Web/Controllers/Interactions/Model/Resolved/ChannelType.cs b/src/Web/Controllers/Interactions/Model/Resolved/ChannelType.cs new file mode 100644 index 0000000..1fbc0af --- /dev/null +++ b/src/Web/Controllers/Interactions/Model/Resolved/ChannelType.cs @@ -0,0 +1,17 @@ +namespace Geekbot.Web.Controllers.Interactions.Model.Resolved +{ + public enum ChannelType + { + GuildText = 0, + Dm = 1, + GuildVoice = 2, + GroupDm = 3, + GuildCategory = 4, + GuildNews = 5, + GuildStore = 6, + GuildNewsThread = 10, + GuildPublicThread = 11, + GuildPrivateThread = 12, + GuildStageVoice = 13, + } +} \ No newline at end of file diff --git a/src/Web/Controllers/Interactions/Model/Resolved/Member.cs b/src/Web/Controllers/Interactions/Model/Resolved/Member.cs new file mode 100644 index 0000000..e95a09a --- /dev/null +++ b/src/Web/Controllers/Interactions/Model/Resolved/Member.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; + +namespace Geekbot.Web.Controllers.Interactions.Model.Resolved +{ + public record Member + { + // public User User { get; set; } + public string Nick { get; set; } + public List Roles { get; set; } + public DateTime JoinedAt { get; set; } + public DateTime PremiumSince { get; set; } + public bool Pending { get; set; } + public string Permissions { get; set; } + } +} \ No newline at end of file diff --git a/src/Web/Controllers/Interactions/Model/Resolved/RoleTag.cs b/src/Web/Controllers/Interactions/Model/Resolved/RoleTag.cs new file mode 100644 index 0000000..4f35ae1 --- /dev/null +++ b/src/Web/Controllers/Interactions/Model/Resolved/RoleTag.cs @@ -0,0 +1,9 @@ +namespace Geekbot.Web.Controllers.Interactions.Model.Resolved +{ + public record RoleTag + { + public string BotId { get; set; } + public string IntegrationId { get; set; } + public bool PremiumSubscriber { get; set; } + } +} \ No newline at end of file diff --git a/src/Web/Controllers/Interactions/Model/Resolved/Roles.cs b/src/Web/Controllers/Interactions/Model/Resolved/Roles.cs new file mode 100644 index 0000000..aab5d7c --- /dev/null +++ b/src/Web/Controllers/Interactions/Model/Resolved/Roles.cs @@ -0,0 +1,15 @@ +namespace Geekbot.Web.Controllers.Interactions.Model.Resolved +{ + public record Roles + { + public string Id { get; set; } + public string Name { get; set; } + public int Color { get; set; } + public bool Hoist { get; set; } + public int Position { get; set; } + public string Permissions { get; set; } + public bool Managed { get; set; } + public bool Mentionable { get; set; } + public RoleTag Tags { get; set; } + } +} \ No newline at end of file diff --git a/src/Web/Controllers/Interactions/Model/Resolved/ThreadMetadata.cs b/src/Web/Controllers/Interactions/Model/Resolved/ThreadMetadata.cs new file mode 100644 index 0000000..e43ad89 --- /dev/null +++ b/src/Web/Controllers/Interactions/Model/Resolved/ThreadMetadata.cs @@ -0,0 +1,13 @@ +using System; + +namespace Geekbot.Web.Controllers.Interactions.Model.Resolved +{ + public record ThreadMetadata + { + public bool Archived { get; set; } + public int AutoArchiveDuration { get; set; } + public DateTime ArchiveTimestamp { get; set; } + public bool Locked { get; set; } + public bool Invitable { get; set; } + } +} \ No newline at end of file diff --git a/src/Web/Controllers/Interactions/Model/Resolved/User.cs b/src/Web/Controllers/Interactions/Model/Resolved/User.cs new file mode 100644 index 0000000..4ef1f8d --- /dev/null +++ b/src/Web/Controllers/Interactions/Model/Resolved/User.cs @@ -0,0 +1,21 @@ +namespace Geekbot.Web.Controllers.Interactions.Model.Resolved +{ + public record User + { + public string Id { get; set; } + public string Username { get; set; } + public string Discriminator { get; set; } + public string Avatar { get; set; } + public bool Bot { get; set; } + public bool System { get; set; } + public bool MfaEnabled { get; set; } + public string Banner { get; set; } + public int AccentColor { get; set; } + public string Locale { get; set; } + public bool Verified { get; set; } + public string Email { get; set; } + public int Flags { get; set; } + public int PremiumType { get; set; } + public int PublicFlags { get; set; } + } +} \ No newline at end of file diff --git a/src/Web/Web.csproj b/src/Web/Web.csproj index a9ffcc4..dc6b09a 100644 --- a/src/Web/Web.csproj +++ b/src/Web/Web.csproj @@ -16,4 +16,8 @@ + + + + diff --git a/src/Web/WebApiStartup.cs b/src/Web/WebApiStartup.cs index 9c8a805..34090d3 100644 --- a/src/Web/WebApiStartup.cs +++ b/src/Web/WebApiStartup.cs @@ -31,7 +31,10 @@ namespace Geekbot.Web }) .ConfigureServices(services => { - services.AddControllers(); + services.AddControllers().AddJsonOptions(options => + { + options.JsonSerializerOptions.PropertyNameCaseInsensitive = true; + }); services.AddCors(options => { options.AddPolicy("AllowSpecificOrigin", From 209887e237847fa9d49c1fd60d59e4e65a16adb7 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Sun, 19 Sep 2021 16:19:40 +0200 Subject: [PATCH 354/443] Add sentry support to the webapi --- src/Web/Web.csproj | 1 + src/Web/WebApiStartup.cs | 1 + 2 files changed, 2 insertions(+) diff --git a/src/Web/Web.csproj b/src/Web/Web.csproj index dc6b09a..48fcf86 100644 --- a/src/Web/Web.csproj +++ b/src/Web/Web.csproj @@ -17,6 +17,7 @@
+ diff --git a/src/Web/WebApiStartup.cs b/src/Web/WebApiStartup.cs index 34090d3..f62dd58 100644 --- a/src/Web/WebApiStartup.cs +++ b/src/Web/WebApiStartup.cs @@ -40,6 +40,7 @@ namespace Geekbot.Web options.AddPolicy("AllowSpecificOrigin", builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()); }); + services.AddSentry(); services.AddSingleton(databaseContext); services.AddSingleton(globalSettings); From 60547140ea5838106c58bd61952ae5bc11be671c Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Mon, 20 Sep 2021 01:28:26 +0200 Subject: [PATCH 355/443] Create all interaction models --- .../ApplicationCommand/Command.cs | 73 ++++++++++++ .../ApplicationCommand/CommandType.cs | 21 ++++ .../Interactions/ApplicationCommand/Option.cs | 51 ++++++++ .../ApplicationCommand/OptionChoice.cs | 17 +++ .../ApplicationCommand/OptionChoiceDouble.cs | 14 +++ .../ApplicationCommand/OptionChoiceInteger.cs | 14 +++ .../ApplicationCommand/OptionChoiceString.cs | 14 +++ .../ApplicationCommand/OptionType.cs | 38 ++++++ src/Core/Interactions/Embed/Embed.cs | 88 ++++++++++++++ src/Core/Interactions/Embed/EmbedAuthor.cs | 32 +++++ src/Core/Interactions/Embed/EmbedField.cs | 26 ++++ src/Core/Interactions/Embed/EmbedFooter.cs | 27 +++++ src/Core/Interactions/Embed/EmbedImage.cs | 32 +++++ src/Core/Interactions/Embed/EmbedProvider.cs | 20 ++++ src/Core/Interactions/Embed/EmbedThumbnail.cs | 33 ++++++ src/Core/Interactions/Embed/EmbedTypes.cs | 49 ++++++++ src/Core/Interactions/Embed/EmbedVideo.cs | 32 +++++ .../MessageComponents/ButtonStyle.cs | 31 +++++ .../MessageComponents/Component.cs | 111 ++++++++++++++++++ .../MessageComponents/ComponentEmoji.cs | 33 ++++++ .../MessageComponents/ComponentType.cs | 21 ++++ .../MessageComponents/SelectOption.cs | 38 ++++++ src/Core/Interactions/Request/Interaction.cs | 77 ++++++++++++ .../Interactions/Request/InteractionData.cs | 109 +++++++++++++++++ .../Interactions/Request/InteractionOption.cs | 34 ++++++ .../Request/InteractionResolvedData.cs | 24 ++++ .../Interactions/Request/InteractionType.cs | 10 ++ src/Core/Interactions/Resolved/Attachment.cs | 34 ++++++ src/Core/Interactions/Resolved/Channel.cs | 25 ++++ .../Interactions}/Resolved/ChannelType.cs | 2 +- src/Core/Interactions/Resolved/Emoji.cs | 32 +++++ src/Core/Interactions/Resolved/Member.cs | 27 +++++ src/Core/Interactions/Resolved/Message.cs | 103 ++++++++++++++++ .../Resolved/MessageInteraction.cs | 20 ++++ src/Core/Interactions/Resolved/MessageType.cs | 29 +++++ src/Core/Interactions/Resolved/Reaction.cs | 16 +++ src/Core/Interactions/Resolved/Role.cs | 34 ++++++ src/Core/Interactions/Resolved/RoleTag.cs | 16 +++ .../Interactions/Resolved/ThreadMetadata.cs | 23 ++++ src/Core/Interactions/Resolved/User.cs | 52 ++++++++ .../Response/InteractionResponse.cs | 23 ++++ .../Response/InteractionResponseData.cs | 51 ++++++++ .../Response/InteractionResponseType.cs | 34 ++++++ .../Model/ApplicationCommandOption.cs | 16 --- .../Interactions/Model/Interaction.cs | 22 ---- .../Interactions/Model/InteractionData.cs | 14 --- .../Interactions/Model/InteractionOption.cs | 12 -- .../Model/InteractionResolvedData.cs | 15 --- .../Interactions/Model/InteractionResponse.cs | 13 -- .../Model/InteractionResponseData.cs | 16 --- .../Model/InteractionResponseType.cs | 11 -- .../Interactions/Model/InteractionType.cs | 9 -- .../Model/MessageComponents/Component.cs | 7 -- .../Interactions/Model/Resolved/Channel.cs | 12 -- .../Interactions/Model/Resolved/Member.cs | 16 --- .../Interactions/Model/Resolved/RoleTag.cs | 9 -- .../Interactions/Model/Resolved/Roles.cs | 15 --- .../Model/Resolved/ThreadMetadata.cs | 13 -- .../Interactions/Model/Resolved/User.cs | 21 ---- 59 files changed, 1589 insertions(+), 222 deletions(-) create mode 100644 src/Core/Interactions/ApplicationCommand/Command.cs create mode 100644 src/Core/Interactions/ApplicationCommand/CommandType.cs create mode 100644 src/Core/Interactions/ApplicationCommand/Option.cs create mode 100644 src/Core/Interactions/ApplicationCommand/OptionChoice.cs create mode 100644 src/Core/Interactions/ApplicationCommand/OptionChoiceDouble.cs create mode 100644 src/Core/Interactions/ApplicationCommand/OptionChoiceInteger.cs create mode 100644 src/Core/Interactions/ApplicationCommand/OptionChoiceString.cs create mode 100644 src/Core/Interactions/ApplicationCommand/OptionType.cs create mode 100644 src/Core/Interactions/Embed/Embed.cs create mode 100644 src/Core/Interactions/Embed/EmbedAuthor.cs create mode 100644 src/Core/Interactions/Embed/EmbedField.cs create mode 100644 src/Core/Interactions/Embed/EmbedFooter.cs create mode 100644 src/Core/Interactions/Embed/EmbedImage.cs create mode 100644 src/Core/Interactions/Embed/EmbedProvider.cs create mode 100644 src/Core/Interactions/Embed/EmbedThumbnail.cs create mode 100644 src/Core/Interactions/Embed/EmbedTypes.cs create mode 100644 src/Core/Interactions/Embed/EmbedVideo.cs create mode 100644 src/Core/Interactions/MessageComponents/ButtonStyle.cs create mode 100644 src/Core/Interactions/MessageComponents/Component.cs create mode 100644 src/Core/Interactions/MessageComponents/ComponentEmoji.cs create mode 100644 src/Core/Interactions/MessageComponents/ComponentType.cs create mode 100644 src/Core/Interactions/MessageComponents/SelectOption.cs create mode 100644 src/Core/Interactions/Request/Interaction.cs create mode 100644 src/Core/Interactions/Request/InteractionData.cs create mode 100644 src/Core/Interactions/Request/InteractionOption.cs create mode 100644 src/Core/Interactions/Request/InteractionResolvedData.cs create mode 100644 src/Core/Interactions/Request/InteractionType.cs create mode 100644 src/Core/Interactions/Resolved/Attachment.cs create mode 100644 src/Core/Interactions/Resolved/Channel.cs rename src/{Web/Controllers/Interactions/Model => Core/Interactions}/Resolved/ChannelType.cs (80%) create mode 100644 src/Core/Interactions/Resolved/Emoji.cs create mode 100644 src/Core/Interactions/Resolved/Member.cs create mode 100644 src/Core/Interactions/Resolved/Message.cs create mode 100644 src/Core/Interactions/Resolved/MessageInteraction.cs create mode 100644 src/Core/Interactions/Resolved/MessageType.cs create mode 100644 src/Core/Interactions/Resolved/Reaction.cs create mode 100644 src/Core/Interactions/Resolved/Role.cs create mode 100644 src/Core/Interactions/Resolved/RoleTag.cs create mode 100644 src/Core/Interactions/Resolved/ThreadMetadata.cs create mode 100644 src/Core/Interactions/Resolved/User.cs create mode 100644 src/Core/Interactions/Response/InteractionResponse.cs create mode 100644 src/Core/Interactions/Response/InteractionResponseData.cs create mode 100644 src/Core/Interactions/Response/InteractionResponseType.cs delete mode 100644 src/Web/Controllers/Interactions/Model/ApplicationCommandOption.cs delete mode 100644 src/Web/Controllers/Interactions/Model/Interaction.cs delete mode 100644 src/Web/Controllers/Interactions/Model/InteractionData.cs delete mode 100644 src/Web/Controllers/Interactions/Model/InteractionOption.cs delete mode 100644 src/Web/Controllers/Interactions/Model/InteractionResolvedData.cs delete mode 100644 src/Web/Controllers/Interactions/Model/InteractionResponse.cs delete mode 100644 src/Web/Controllers/Interactions/Model/InteractionResponseData.cs delete mode 100644 src/Web/Controllers/Interactions/Model/InteractionResponseType.cs delete mode 100644 src/Web/Controllers/Interactions/Model/InteractionType.cs delete mode 100644 src/Web/Controllers/Interactions/Model/MessageComponents/Component.cs delete mode 100644 src/Web/Controllers/Interactions/Model/Resolved/Channel.cs delete mode 100644 src/Web/Controllers/Interactions/Model/Resolved/Member.cs delete mode 100644 src/Web/Controllers/Interactions/Model/Resolved/RoleTag.cs delete mode 100644 src/Web/Controllers/Interactions/Model/Resolved/Roles.cs delete mode 100644 src/Web/Controllers/Interactions/Model/Resolved/ThreadMetadata.cs delete mode 100644 src/Web/Controllers/Interactions/Model/Resolved/User.cs diff --git a/src/Core/Interactions/ApplicationCommand/Command.cs b/src/Core/Interactions/ApplicationCommand/Command.cs new file mode 100644 index 0000000..aaf1e74 --- /dev/null +++ b/src/Core/Interactions/ApplicationCommand/Command.cs @@ -0,0 +1,73 @@ +using System.Collections.Generic; +using System.Text.Json.Serialization; + +namespace Geekbot.Core.Interactions.ApplicationCommand +{ + /// + public record Command + { + /// + /// unique id of the command + /// + [JsonPropertyName("id")] + public string Id { get; set; } + + /// + /// the type of command, defaults 1 if not set + /// + [JsonPropertyName("type")] + public CommandType Type { get; set; } + + /// + /// unique id of the parent application + /// + [JsonPropertyName("application_id")] + public string ApplicationId { get; set; } + + /// + /// guild id of the command, if not global + /// + [JsonPropertyName("guild_id")] + public string GuildId { get; set; } + + /// + /// 1-32 character name + /// + /// + /// CHAT_INPUT command names and command option names must match the following regex ^[\w-]{1,32}$ with the unicode flag set. If there is a lowercase variant of any letters used, you must use those. + /// Characters with no lowercase variants and/or uncased letters are still allowed. USER and MESSAGE commands may be mixed case and can include spaces. + /// + [JsonPropertyName("name")] + public string Name { get; set; } + + /// + /// 1-100 character description for CHAT_INPUT commands, empty string for USER and MESSAGE commands + /// + /// + /// Exclusive: CHAT_INPUT + /// + [JsonPropertyName("description")] + public string Description { get; set; } + + /// + /// the parameters for the command, max 25 + /// + /// + /// Exclusive: CHAT_INPUT + /// + [JsonPropertyName("options")] + public List