From 0b680d7fb22a46ca0d5a730f0f8ccc450b4a4eb0 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Thu, 28 Sep 2017 18:55:57 +0200 Subject: [PATCH] new Quote command and bugfixes --- Geekbot.net/Lib/ErrorHandler.cs | 8 +- Geekbot.net/Modules/AdminCmd.cs | 12 +-- Geekbot.net/Modules/Quote.cs | 163 ++++++++++++++++++++++++++++++++ Geekbot.net/Modules/UserInfo.cs | 2 +- 4 files changed, 176 insertions(+), 9 deletions(-) create mode 100644 Geekbot.net/Modules/Quote.cs diff --git a/Geekbot.net/Lib/ErrorHandler.cs b/Geekbot.net/Lib/ErrorHandler.cs index 249c837..5ded3a2 100644 --- a/Geekbot.net/Lib/ErrorHandler.cs +++ b/Geekbot.net/Lib/ErrorHandler.cs @@ -15,11 +15,15 @@ namespace Geekbot.net.Lib // this.botOwnerDmChannel = botOwnerDmChannel; } - public void HandleCommandException(Exception e, ICommandContext Context) + public void HandleCommandException(Exception e, ICommandContext Context, string errorMessage = "") { var errorMsg = $"Error Occured while executing \"{Context.Message.Content}\", executed by \"{Context.User.Username}\", complete message was \"{Context.Message}\""; logger.Error(e, errorMsg); + if (!string.IsNullOrEmpty(errorMessage)) + { + Context.Channel.SendMessageAsync(errorMessage); + } // await botOwnerDmChannel.SendMessageAsync($"{errorMsg}```{e.StackTrace}```"); // await Context.Channel.SendMessageAsync("Something went wrong..."); } @@ -27,6 +31,6 @@ namespace Geekbot.net.Lib public interface IErrorHandler { - void HandleCommandException(Exception e, ICommandContext Context); + void HandleCommandException(Exception e, ICommandContext Context, string errorMessage = ""); } } \ No newline at end of file diff --git a/Geekbot.net/Modules/AdminCmd.cs b/Geekbot.net/Modules/AdminCmd.cs index ac7ba2f..2229469 100644 --- a/Geekbot.net/Modules/AdminCmd.cs +++ b/Geekbot.net/Modules/AdminCmd.cs @@ -36,10 +36,10 @@ namespace Geekbot.net.Modules [Summary("Set the youtube api key")] public async Task SetYoutubeKey([Summary("API Key")] string key) { - var botOwner = redis.StringGet("botOwner"); - if (!Context.User.Id.ToString().Equals(botOwner.ToString())) + 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}"); + await ReplyAsync($"Sorry, only the botowner can do this ({botOwner.Username}#{botOwner.Discriminator})"); return; } @@ -51,10 +51,10 @@ namespace Geekbot.net.Modules [Summary("Set the game that the bot is playing")] public async Task SetGame([Remainder] [Summary("Game")] string key) { - var botOwner = redis.StringGet("botOwner"); - if (!Context.User.Id.ToString().Equals(botOwner.ToString())) + 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}"); + await ReplyAsync($"Sorry, only the botowner can do this ({botOwner.Username}#{botOwner.Discriminator})"); return; } diff --git a/Geekbot.net/Modules/Quote.cs b/Geekbot.net/Modules/Quote.cs new file mode 100644 index 0000000..107b036 --- /dev/null +++ b/Geekbot.net/Modules/Quote.cs @@ -0,0 +1,163 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Discord; +using Discord.Commands; +using Geekbot.net.Lib; +using Microsoft.Extensions.Configuration; +using Newtonsoft.Json; +using Serilog; +using StackExchange.Redis; + +namespace Geekbot.net.Modules +{ + [Group("quote")] + public class Quote : ModuleBase + { + private readonly IDatabase redis; + private readonly ILogger logger; + private readonly IErrorHandler errorHandler; + + public Quote(IDatabase redis, ILogger logger, IErrorHandler errorHandler) + { + this.redis = redis; + this.logger = logger; + this.errorHandler = errorHandler; + } + + [Command()] + [Summary("Return a random quoute from the database")] + public async Task getRandomQuote() + { + var randomQuote = redis.SetRandomMember($"{Context.Guild.Id}:Quotes"); + try + { + var quote = JsonConvert.DeserializeObject(randomQuote); + var embed = quoteBuilder(quote); + await ReplyAsync("", false, embed.Build()); + } + catch (Exception e) + { + errorHandler.HandleCommandException(e, Context, "Whoops, seems like the quote was to edgy to return"); + } + } + + [Command("save")] + [Summary("Save a quote from the last sent message by @user")] + public async Task saveQuote([Summary("@user")] IUser user) + { + try + { + var lastMessage = await getLastMessageByUser(user); + var quote = new QuoteObject() + { + userId = user.Id, + time = DateTime.Now, + quote = lastMessage.Content + }; + var quoteStore = JsonConvert.SerializeObject(quote); + redis.SetAdd($"{Context.Guild.Id}:Quotes", quoteStore); + await ReplyAsync("Quote Added"); + } + catch (Exception e) + { + errorHandler.HandleCommandException(e, Context, "I counldn't find a quote from that user :disappointed:"); + } + } + + [Command("save")] + [Summary("Save a quote from a message id")] + public async Task saveQuote([Summary("messageId")] ulong messageId) + { + try + { + var message = await Context.Channel.GetMessageAsync(messageId); + var quote = new QuoteObject() + { + userId = message.Author.Id, + time = DateTime.Now, + quote = message.Content + }; + var quoteStore = JsonConvert.SerializeObject(quote); + redis.SetAdd($"{Context.Guild.Id}:Quotes", quoteStore); + await ReplyAsync("Quote Added"); + } + catch (Exception e) + { + 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("@user")] IUser user) + { + try + { + var lastMessage = await getLastMessageByUser(user); + var quote = new QuoteObject() + { + userId = user.Id, + time = DateTime.Now, + quote = lastMessage.Content + }; + var embed = quoteBuilder(quote); + await ReplyAsync("", false, embed.Build()); + } + catch (Exception e) + { + 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("messageId")] ulong messageId) + { + try + { + var message = await Context.Channel.GetMessageAsync(messageId); + var quote = new QuoteObject() + { + userId = message.Author.Id, + time = DateTime.Now, + quote = message.Content + }; + var embed = quoteBuilder(quote); + await ReplyAsync("", false, embed.Build()); + } + catch (Exception e) + { + errorHandler.HandleCommandException(e, Context, "I couldn't find a message with that id :disappointed:"); + } + } + + private async Task getLastMessageByUser(IUser user) + { + Task> list = Context.Channel.GetMessagesAsync().Flatten(); + await list; + return list.Result.Where(msg => + msg.Author.Id == user.Id + && msg.Embeds.Count == 0 + && msg.Id != Context.Message.Id) + .First(); + } + + private EmbedBuilder quoteBuilder(QuoteObject quote) + { + var user = Context.Client.GetUserAsync(quote.userId).Result; + var eb = new EmbedBuilder(); + + eb.ThumbnailUrl = user.GetAvatarUrl(); + eb.AddField(quote.quote, $"-- {user.Username} - {quote.time.Day}.{quote.time.Month}.{quote.time.Year}"); + return eb; + } + } + + public class QuoteObject { + public ulong userId { get; set; } + public DateTime time { get; set; } + public string quote { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Modules/UserInfo.cs b/Geekbot.net/Modules/UserInfo.cs index 21fc618..4514b13 100644 --- a/Geekbot.net/Modules/UserInfo.cs +++ b/Geekbot.net/Modules/UserInfo.cs @@ -111,7 +111,7 @@ namespace Geekbot.net.Modules private string NumerToEmoji(int number) { - var emojis = new string[] {":one:", ":two:", ":three:", ":four:", ":five:", ":six", ":seven:", ":eight:", ":nine:", ":keycap_ten:"}; + var emojis = new string[] {":one:", ":two:", ":three:", ":four:", ":five:", ":six:", ":seven:", ":eight:", ":nine:", ":keycap_ten:"}; try { return emojis[number - 1];