From 45f289d071416955122785cf55cd111199223789 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Sat, 11 Nov 2017 16:20:26 +0100 Subject: [PATCH] Urban Dictionary command, small improvents in message handler, emojis are in embeds now, avatar command --- Geekbot.net/Commands/AvatarGetter.cs | 37 +++++++++++ Geekbot.net/Commands/Cat.cs | 10 +-- Geekbot.net/Commands/Dog.cs | 10 +-- Geekbot.net/Commands/Emojify.cs | 14 +++- Geekbot.net/Commands/UrbanDictionary.cs | 85 +++++++++++++++++++++++++ Geekbot.net/Handlers.cs | 36 +++++++---- Geekbot.net/Program.cs | 3 +- 7 files changed, 168 insertions(+), 27 deletions(-) create mode 100644 Geekbot.net/Commands/AvatarGetter.cs create mode 100644 Geekbot.net/Commands/UrbanDictionary.cs diff --git a/Geekbot.net/Commands/AvatarGetter.cs b/Geekbot.net/Commands/AvatarGetter.cs new file mode 100644 index 0000000..aa968db --- /dev/null +++ b/Geekbot.net/Commands/AvatarGetter.cs @@ -0,0 +1,37 @@ +using System; +using System.Threading.Tasks; +using Discord; +using Discord.Commands; +using Geekbot.net.Lib; + +namespace Geekbot.net.Commands +{ + public class AvatarGetter : ModuleBase + { + private readonly IErrorHandler _errorHandler; + + public AvatarGetter(IErrorHandler errorHandler) + { + _errorHandler = errorHandler; + } + + [Command("avatar", RunMode = RunMode.Async)] + [Remarks(CommandCategories.Randomness)] + [Summary("Get someones avatar")] + public async Task getAvatar([Remainder, Summary("user")] IUser user = null) + { + try + { + if (user == null) + { + user = Context.User; + } + await ReplyAsync(user.GetAvatarUrl()); + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + } + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/Cat.cs b/Geekbot.net/Commands/Cat.cs index 8e528d9..6b83122 100644 --- a/Geekbot.net/Commands/Cat.cs +++ b/Geekbot.net/Commands/Cat.cs @@ -46,10 +46,10 @@ namespace Geekbot.net.Commands _errorHandler.HandleCommandException(e, Context); } } - } - - public class CatResponse - { - public string file { get; set; } + + private class CatResponse + { + public string file { get; set; } + } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/Dog.cs b/Geekbot.net/Commands/Dog.cs index f0c8e52..6a19741 100644 --- a/Geekbot.net/Commands/Dog.cs +++ b/Geekbot.net/Commands/Dog.cs @@ -46,10 +46,10 @@ namespace Geekbot.net.Commands _errorHandler.HandleCommandException(e, Context); } } - } - - public class DogResponse - { - public string url { get; set; } + + private class DogResponse + { + 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 855bd65..7d8d102 100644 --- a/Geekbot.net/Commands/Emojify.cs +++ b/Geekbot.net/Commands/Emojify.cs @@ -1,6 +1,7 @@ using System; using System.Text; using System.Threading.Tasks; +using Discord; using Discord.Commands; using Geekbot.net.Lib; @@ -30,9 +31,16 @@ namespace Geekbot.net.Commands { await ReplyAsync("I can't take that much at once!"); return; - } - await ReplyAsync($"*{Context.User.Username}#{Context.User.Discriminator} said:*"); - await ReplyAsync(emojis); + } + var eb = new EmbedBuilder(); + eb.WithAuthor(new EmbedAuthorBuilder() + { + IconUrl = Context.User.GetAvatarUrl(), + Name = $"{Context.User.Username}#{Context.User.Discriminator}" + }); + eb.WithColor(new Color(59, 136, 195)); + eb.Description = emojis; + await ReplyAsync("", false, eb.Build()); } catch (Exception e) { diff --git a/Geekbot.net/Commands/UrbanDictionary.cs b/Geekbot.net/Commands/UrbanDictionary.cs new file mode 100644 index 0000000..ffd1394 --- /dev/null +++ b/Geekbot.net/Commands/UrbanDictionary.cs @@ -0,0 +1,85 @@ +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 Newtonsoft.Json; + +namespace Geekbot.net.Commands +{ + public class UrbanDictionary : ModuleBase + { + private readonly IErrorHandler _errorHandler; + + public UrbanDictionary(IErrorHandler errorHandler) + { + _errorHandler = errorHandler; + } + + [Command("urban", RunMode = RunMode.Async)] + [Remarks(CommandCategories.Randomness)] + [Summary("Lookup something on urban dictionary")] + public async Task urbanDefine([Remainder, Summary("word")] string word) + { + try + { + using (var client = new HttpClient()) + { + client.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 is not defined..."); + return; + } + var definition = definitions.list.OrderBy(e => e.thumbs_up).First(); + + var eb = new EmbedBuilder(); + eb.Title = definition.word; + eb.WithColor(new Color(239,255,0)); + eb.Description = definition.definition; + eb.AddField("Example", definition.example); + eb.AddInlineField("Upvotes", definition.thumbs_up); + eb.AddInlineField("Downvotes", definition.thumbs_down); + eb.WithFooter(new EmbedFooterBuilder() + { + Text = definition.permalink + }); + + await ReplyAsync("", false, eb.Build()); + } + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + } + } + + private class UrbanResponse + { + public string[] tags { get; set; } + public string result_type { 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; } + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index ba86e37..89fb61e 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Text; using System.Threading.Tasks; using Discord; @@ -37,8 +38,7 @@ namespace Geekbot.net { try { - var message = messageParam as SocketUserMessage; - if (message == null) return Task.CompletedTask; + if (!(messageParam is SocketUserMessage message)) return Task.CompletedTask; if (message.Author.IsBot) return Task.CompletedTask; var argPos = 0; var lowCaseMsg = message.ToString().ToLower(); @@ -65,18 +65,30 @@ namespace Geekbot.net } } - public Task UpdateStats(SocketMessage messsageParam) + public Task UpdateStats(SocketMessage message) { - var message = messsageParam; - if (message == null) return Task.CompletedTask; - - var channel = (SocketGuildChannel) message.Channel; - - _redis.HashIncrementAsync($"{channel.Guild.Id}:Messages", message.Author.Id.ToString()); - _redis.HashIncrementAsync($"{channel.Guild.Id}:Messages", 0.ToString()); + try + { + if (message == null) return Task.CompletedTask; + if (message.Channel.Name.StartsWith('@')) + { + _logger.Information( + $"[Message] DM-Channel - {message.Channel.Name} - {message.Content}"); + return Task.CompletedTask; + } + var channel = (SocketGuildChannel) message.Channel; - if (message.Author.IsBot) return Task.CompletedTask; - _logger.Information($"[Message] {channel.Guild.Name} - {message.Channel} - {message.Author.Username} - {message.Content}"); + _redis.HashIncrementAsync($"{channel.Guild.Id}:Messages", message.Author.Id.ToString()); + _redis.HashIncrementAsync($"{channel.Guild.Id}:Messages", 0.ToString()); + + if (message.Author.IsBot) return Task.CompletedTask; + _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) + { + _logger.Error(e, "Could not process message stats"); + } return Task.CompletedTask; } diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 7246dd4..d57a269 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -64,7 +64,6 @@ namespace Geekbot.net { LogLevel = LogSeverity.Verbose, MessageCacheSize = 1000 -// AlwaysDownloadUsers = true }); client.Log += DiscordLogger; commands = new CommandService(); @@ -149,7 +148,7 @@ namespace Geekbot.net if (isConneted) { await client.SetGameAsync(redis.StringGet("Game")); - logger.Information($"[Geekbot] Now Connected to {client.Guilds.Count} Servers"); + logger.Information($"[Geekbot] Now Connected as {client.CurrentUser.Username} to {client.Guilds.Count} Servers"); logger.Information("[Geekbot] Registering Stuff");