From 65094f39b22db3fbab07e8b44a2998c2073407fb Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Mon, 8 Oct 2018 16:50:23 +0200 Subject: [PATCH] Lots of work regarding using embeds instead of plain text for messages --- DeukBot4/DeukBot4.csproj | 5 +- .../Commands/GeneralCommands.cs | 65 ++++++++++++++----- .../Commands/ModeratorCommands.cs | 14 ++-- .../CommandHandler/Commands/OwnerCommands.cs | 10 +-- .../Commands/RolePermissionCommands.cs | 10 +-- .../CommandHandler/EmbedFactory.cs | 30 +++++++++ .../CommandHandler/HelpCommandGenerator.cs | 30 +++++---- .../RequestStructure/CommandRequest.cs | 16 ++++- .../CommandHandler/TagStorage.cs | 25 ++++--- DeukBot4/Program.cs | 1 + 10 files changed, 140 insertions(+), 66 deletions(-) create mode 100644 DeukBot4/MessageHandlers/CommandHandler/EmbedFactory.cs diff --git a/DeukBot4/DeukBot4.csproj b/DeukBot4/DeukBot4.csproj index e3acf92..5a714a9 100644 --- a/DeukBot4/DeukBot4.csproj +++ b/DeukBot4/DeukBot4.csproj @@ -5,10 +5,9 @@ 7.1 - - + - + \ No newline at end of file diff --git a/DeukBot4/MessageHandlers/CommandHandler/Commands/GeneralCommands.cs b/DeukBot4/MessageHandlers/CommandHandler/Commands/GeneralCommands.cs index b0542e4..321212a 100644 --- a/DeukBot4/MessageHandlers/CommandHandler/Commands/GeneralCommands.cs +++ b/DeukBot4/MessageHandlers/CommandHandler/Commands/GeneralCommands.cs @@ -1,5 +1,8 @@ using System; +using System.Collections.Generic; +using System.Linq; using System.Net; +using System.Threading; using System.Threading.Tasks; using System.Web; using DeukBot4.APIHandlers; @@ -8,6 +11,7 @@ using DeukBot4.MessageHandlers.CommandHandler.RequestStructure; using DeukBot4.MessageHandlers.Permissions; using DeukBot4.Utilities; using Discord; +using Discord.Rest; using Discord.WebSocket; namespace DeukBot4.MessageHandlers.CommandHandler @@ -20,29 +24,55 @@ namespace DeukBot4.MessageHandlers.CommandHandler [CommandHelp("Gives basic info on the bot", "Gives basic info on the bot")] public async Task Info(CommandRequest request) { - var embed = new EmbedBuilder + await request.SendSimpleEmbed("Deukbot", "A bot designed by Deukhoofd for use on the Epsilon server", new[] { - Author = new EmbedAuthorBuilder() + new EmbedFieldBuilder() { - Name = "DeukBot", - Url = "https://gitlab.com/Deukhoofd/DeukBot4/tree/master/DeukBot4/MessageHandlers/CommandHandler" + Name = "Software", Value = "Deukbot 4.0", IsInline = true }, - Color = Color.Gold, - Title = "Deukbot Info", - Description = "A bot designed by Deukhoofd for use on the Epsilon server", - Timestamp = DateTime.UtcNow - }; - embed.AddField("Software", "Deukbot 4.0", true); - embed.AddField("Creator", "Deukhoofd#7361", true); - - await request.SendMessageAsync("", embed: embed.Build()); + new EmbedFieldBuilder() + { + Name = "Creator", Value = "<@84372569012043776>", IsInline = true + }, + }); } [Command("ping", PermissionLevel.Everyone)] [CommandHelp("Simple Ping Pong Response", "Generates a simple Pong response when triggered")] public async Task Ping(CommandRequest request) { - await request.SendMessageAsync("Pong"); + var t1 = DateTimeOffset.UtcNow; + var eb = EmbedFactory.GetStandardEmbedBuilder(); + eb.Title = "Pong"; + eb.Description = "Pong"; + eb.Fields = new List() + { + new EmbedFieldBuilder() + { + Name = "Ping between Message and Command Handling", + Value = $"{(int)(t1 - request.OriginalMessage.CreatedAt).TotalMilliseconds} ms" + } + }; + eb.Footer = new EmbedFooterBuilder() + { + Text = "Ping can be off a bit due to Discord timestamping" + }; + + var t2 = DateTimeOffset.UtcNow; + var m = await request.SendMessageAsync("", embed: eb.Build()); + eb.Fields.Add(new EmbedFieldBuilder() + { + Name = "Time handling embed creation", + Value = $"{(t2 - t1).TotalMilliseconds} ms" + }); + eb.Fields.Add(new EmbedFieldBuilder() + { + Name = "Ping between Bot and Discord", + Value = $"{(int)(m.CreatedAt - t2).TotalMilliseconds} ms" + }); + + + m.ModifyAsync(properties => { properties.Embed = eb.Build(); }); } [Command("help", PermissionLevel.Everyone)] @@ -56,7 +86,8 @@ namespace DeukBot4.MessageHandlers.CommandHandler { if (request.Parameters.Length == 0) { - await request.SendMessageAsync(HelpCommandGenerator.GenerateFullHelp(request.RequestPermissions)); + await request.SendMessageAsync("", + embed: HelpCommandGenerator.GenerateFullHelp(request.RequestPermissions)); } else { @@ -72,14 +103,14 @@ namespace DeukBot4.MessageHandlers.CommandHandler [CommandParameters(ParameterMatcher.ParameterType.Remainder)] public async Task BotOpinion(CommandRequest request) { - await request.SendMessageAsync(BotOpinions.GetOpinion(request)); + await request.SendSimpleEmbed("Opinion", BotOpinions.GetOpinion(request)); } [Command("catfact", PermissionLevel.Everyone)] [CommandHelp("Returns a random cat fact", "Returns a random cat fact, powered by https://catfact.ninja/")] public async Task RandomCatFact(CommandRequest request) { - await request.SendMessageAsync(await CatFactsApi.GetRandomCatFact()); + await request.SendSimpleEmbed("Cat Fact", await CatFactsApi.GetRandomCatFact()); } [Command("catpic", PermissionLevel.Everyone)] diff --git a/DeukBot4/MessageHandlers/CommandHandler/Commands/ModeratorCommands.cs b/DeukBot4/MessageHandlers/CommandHandler/Commands/ModeratorCommands.cs index 42f9341..36337fd 100644 --- a/DeukBot4/MessageHandlers/CommandHandler/Commands/ModeratorCommands.cs +++ b/DeukBot4/MessageHandlers/CommandHandler/Commands/ModeratorCommands.cs @@ -31,7 +31,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler var user = await request.Parameters[0].AsDiscordUser(channel.Guild); if (user == null) { - await request.SendMessageAsync("I can't find that user on the server"); + await request.SendSimpleEmbed("Kick", "I can't find that user on the server"); return; } @@ -41,7 +41,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler // if the user has sufficient permissions, or is deukbot, warn the user that he's not allowed to do that, and stop if (userPermissions >= PermissionLevel.Helper || user.Id == Program.Client.CurrentUser.Id) { - await request.SendMessageAsync("You are not allowed to kick that user"); + await request.SendSimpleEmbed("Kick", "You are not allowed to kick that user"); return; } @@ -54,7 +54,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler // and kick await user.KickAsync(reason); - await request.SendMessageAsync($"User was kicked: {user.Username}"); + await request.SendSimpleEmbed("Kick", $"User was kicked: {user.Username}"); } [Command("ban", PermissionLevel.Moderator)] @@ -75,7 +75,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler var user = await request.Parameters[0].AsDiscordUser(channel.Guild); if (user == null) { - await request.SendMessageAsync("I can't find that user on the server"); + await request.SendSimpleEmbed("Ban", "I can't find that user on the server"); return; } @@ -85,7 +85,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler // if the user has sufficient permissions, or is deukbot, warn the user that he's not allowed to do that, and stop if (userPermissions >= PermissionLevel.Helper || user.Id == Program.Client.CurrentUser.Id) { - await request.SendMessageAsync("You are not allowed to ban that user"); + await request.SendSimpleEmbed("Ban", "You are not allowed to ban that user"); return; } @@ -139,7 +139,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler var user = await request.Parameters[0].AsDiscordUser(channel.Guild); if (user == null) { - await request.SendMessageAsync("I can't find that user on the server"); + await request.SendSimpleEmbed("Silence", "I can't find that user on the server"); return; } @@ -154,7 +154,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler var silencedRole = channel.Guild.GetRole(silencedRoleId); if (silencedRole == null) { - await request.SendMessageAsync( + await request.SendSimpleEmbed( "Silence", "Can't find the silenced role. Has it been deleted?"); return; } diff --git a/DeukBot4/MessageHandlers/CommandHandler/Commands/OwnerCommands.cs b/DeukBot4/MessageHandlers/CommandHandler/Commands/OwnerCommands.cs index 7348203..9885656 100644 --- a/DeukBot4/MessageHandlers/CommandHandler/Commands/OwnerCommands.cs +++ b/DeukBot4/MessageHandlers/CommandHandler/Commands/OwnerCommands.cs @@ -20,12 +20,12 @@ namespace DeukBot4.MessageHandlers.CommandHandler var channelId = request.Parameters[0].AsUlong(); if (!channelId.HasValue) { - await request.SendMessageAsync($"Invalid channel ID: {request.Parameters[0].AsString()}"); + await request.SendSimpleEmbed("Send", $"Invalid channel ID: {request.Parameters[0].AsString()}"); return; } if (!(Program.Client.GetChannel(channelId.Value) is IMessageChannel channelFind)) { - await request.SendMessageAsync($"Can't find channel: {channelId.Value}"); + await request.SendSimpleEmbed("Send", $"Can't find channel: {channelId.Value}"); return; } await channelFind.SendMessageAsync(request.Parameters[1].AsString()); @@ -40,21 +40,21 @@ namespace DeukBot4.MessageHandlers.CommandHandler var channelId = request.Parameters[0].AsUlong(); if (!channelId.HasValue) { - await request.SendMessageAsync($"Invalid channel ID: {request.Parameters[0].AsString()}"); + await request.SendSimpleEmbed("DM", $"Invalid channel ID: {request.Parameters[0].AsString()}"); return; } var user = Program.Client.GetUser(channelId.Value); if (user == null) { - await request.SendMessageAsync($"Can't find user: {channelId.Value}"); + await request.SendSimpleEmbed("DM", $"Can't find user: {channelId.Value}"); return; } var channel = await user.GetOrCreateDMChannelAsync(); if (channel == null) { - await request.SendMessageAsync($"Can't create channel"); + await request.SendSimpleEmbed("DM", $"Can't create channel"); return; } await channel.SendMessageAsync(request.Parameters[1].AsString()); diff --git a/DeukBot4/MessageHandlers/CommandHandler/Commands/RolePermissionCommands.cs b/DeukBot4/MessageHandlers/CommandHandler/Commands/RolePermissionCommands.cs index dfcd782..f4909f6 100644 --- a/DeukBot4/MessageHandlers/CommandHandler/Commands/RolePermissionCommands.cs +++ b/DeukBot4/MessageHandlers/CommandHandler/Commands/RolePermissionCommands.cs @@ -72,7 +72,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler if (request.Parameters.Length == 0) { - await request.SendMessageAsync( + await request.SendSimpleEmbed("Role Permission", $"You did not give a valid role ID. Use ``!roles`` to list all current server roles, along with their ids"); return; } @@ -80,7 +80,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler var roleId = request.Parameters[0].AsUlong(); if (!roleId.HasValue) { - await request.SendMessageAsync( + await request.SendSimpleEmbed("Role Permission", $"You did not give a valid role ID. Use ``!roles`` to list all current server roles, along with their ids"); return; } @@ -88,7 +88,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler var role = serverChannel.Guild.GetRole(roleId.Value); if (role == null) { - await request.SendMessageAsync("No role with that id exists on this server"); + await request.SendSimpleEmbed("Role Permission", "No role with that id exists on this server"); return; } @@ -111,7 +111,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler var val = request.Parameters[0].AsUlong(); if (!val.HasValue) { - await request.SendMessageAsync("Invalid role ID"); + await request.SendSimpleEmbed("Silenced Role", "Invalid role ID"); return; } @@ -121,7 +121,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler var role = channel.Guild.GetRole(val.Value); if (role == null) { - await request.SendMessageAsync("No role by that ID exists on the server"); + await request.SendSimpleEmbed("Silenced Role", "No role by that ID exists on the server"); return; } diff --git a/DeukBot4/MessageHandlers/CommandHandler/EmbedFactory.cs b/DeukBot4/MessageHandlers/CommandHandler/EmbedFactory.cs new file mode 100644 index 0000000..473f4a7 --- /dev/null +++ b/DeukBot4/MessageHandlers/CommandHandler/EmbedFactory.cs @@ -0,0 +1,30 @@ +using System; +using Discord; + +namespace DeukBot4.MessageHandlers.CommandHandler +{ + public static class EmbedFactory + { + private static EmbedAuthorBuilder Author; + + public static void Initialize(ISelfUser user) + { + Author = new EmbedAuthorBuilder() + { + Name = user.Username, + IconUrl = user.GetAvatarUrl(ImageFormat.Png, 128) + }; + } + + public static EmbedBuilder GetStandardEmbedBuilder() + { + var eb = new EmbedBuilder + { + Author = Author, + Color = Color.Gold, + Timestamp = DateTimeOffset.Now + }; + return eb; + } + } +} \ No newline at end of file diff --git a/DeukBot4/MessageHandlers/CommandHandler/HelpCommandGenerator.cs b/DeukBot4/MessageHandlers/CommandHandler/HelpCommandGenerator.cs index 0f67955..38357e4 100644 --- a/DeukBot4/MessageHandlers/CommandHandler/HelpCommandGenerator.cs +++ b/DeukBot4/MessageHandlers/CommandHandler/HelpCommandGenerator.cs @@ -7,9 +7,13 @@ namespace DeukBot4.MessageHandlers.CommandHandler { public static class HelpCommandGenerator { - public static string GenerateFullHelp(PermissionLevel level) + public static Embed GenerateFullHelp(PermissionLevel level) { var dic = new Dictionary>(); + var eb = EmbedFactory.GetStandardEmbedBuilder(); + eb.Title = "Help"; + eb.Description = "Below you will find a list of commands you are able to use. " + + "For a more detailed information on a command use ``!help {command}``"; foreach (var command in CommandHandler.Commands) { if (command.Value.Permission > level) @@ -25,17 +29,22 @@ namespace DeukBot4.MessageHandlers.CommandHandler } entry.Add(command.Value.Name, command.Value.ShortHelp); } - var sb = new StringBuilder(); foreach (var entry in dic) { - sb.Append($"**{entry.Key}**\n"); + var sectionSb = new StringBuilder(); foreach (var cmd in entry.Value) { - sb.Append($"``{cmd.Key}`` - {cmd.Value}\n"); + sectionSb.Append($"**{cmd.Key}** -- {cmd.Value}\n"); } + eb.Fields.Add(new EmbedFieldBuilder() + { + Name = entry.Key, + Value = sectionSb + + }); } - return sb.ToString(); + return eb.Build(); } public static Embed GenerateSpecificHelp(string command, PermissionLevel level) @@ -46,14 +55,9 @@ namespace DeukBot4.MessageHandlers.CommandHandler return null; if (!cmd.HasHelp) return null; - - var eb = new EmbedBuilder - { - Title = cmd.Name, - Description = cmd.LongHelp, - Color = Color.Gold, - }; - + var eb = EmbedFactory.GetStandardEmbedBuilder(); + eb.Title = cmd.Name; + eb.Description = cmd.LongHelp; return eb.Build(); } diff --git a/DeukBot4/MessageHandlers/CommandHandler/RequestStructure/CommandRequest.cs b/DeukBot4/MessageHandlers/CommandHandler/RequestStructure/CommandRequest.cs index d1541c5..08760c7 100644 --- a/DeukBot4/MessageHandlers/CommandHandler/RequestStructure/CommandRequest.cs +++ b/DeukBot4/MessageHandlers/CommandHandler/RequestStructure/CommandRequest.cs @@ -1,9 +1,11 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Text.RegularExpressions; using System.Threading.Tasks; using DeukBot4.MessageHandlers.Permissions; using Discord; +using Discord.Rest; using Discord.WebSocket; namespace DeukBot4.MessageHandlers.CommandHandler.RequestStructure @@ -32,11 +34,21 @@ namespace DeukBot4.MessageHandlers.CommandHandler.RequestStructure OK, UnknownCommand, Invalid, Forbidden, InvalidParameters } - public async Task SendMessageAsync(string text, bool isTTS = false, Embed embed = null) + public Task SendMessageAsync(string text, bool isTTS = false, Embed embed = null) { - await OriginalMessage.Channel.SendMessageAsync(text, isTTS, embed); + return OriginalMessage.Channel.SendMessageAsync(text, isTTS, embed); } + public Task SendSimpleEmbed(string title, string description, IEnumerable fields = null) + { + var eb = EmbedFactory.GetStandardEmbedBuilder(); + eb.Title = title; + eb.Description = description; + eb.Fields = fields == null ? new List() : fields.ToList(); + return OriginalMessage.Channel.SendMessageAsync("", embed: eb.Build()); + } + + public static async Task<(CommandRequest, RequestCode, object)> Create(SocketMessage message) { var originalMessage = message; diff --git a/DeukBot4/MessageHandlers/CommandHandler/TagStorage.cs b/DeukBot4/MessageHandlers/CommandHandler/TagStorage.cs index 517ea9c..79a45e5 100644 --- a/DeukBot4/MessageHandlers/CommandHandler/TagStorage.cs +++ b/DeukBot4/MessageHandlers/CommandHandler/TagStorage.cs @@ -151,11 +151,11 @@ namespace DeukBot4.MessageHandlers.CommandHandler var result = serverTags.AddTag(key, value); if (!result.success) { - cmd.SendMessageAsync("Something went wrong: " + result.result); + cmd.SendSimpleEmbed("Tag Creation", "Something went wrong: " + result.result); } else { - cmd.SendMessageAsync("Success! Added tag with key: " + key); + cmd.SendSimpleEmbed("Tag Creation","Success! Added tag with key: " + key); } } @@ -175,11 +175,11 @@ namespace DeukBot4.MessageHandlers.CommandHandler var result = serverTags.RemoveTag(key); if (!result.success) { - cmd.SendMessageAsync("Something went wrong: " + result.result); + cmd.SendSimpleEmbed("Tag Deletion","Something went wrong: " + result.result); } else { - cmd.SendMessageAsync("Success! Removed tag with key: " + key); + cmd.SendSimpleEmbed("Tag Deletion","Success! Removed tag with key: " + key); } } @@ -208,11 +208,11 @@ namespace DeukBot4.MessageHandlers.CommandHandler var result = serverTags.AddTag(keyword, rest); if (!result.success) { - cmd.SendMessageAsync("Something went wrong: " + result.result); + cmd.SendSimpleEmbed("Tag Creation", "Something went wrong: " + result.result); } else { - cmd.SendMessageAsync("Success! Added tag with key: " + keyword); + cmd.SendSimpleEmbed("Tag Creation","Success! Added tag with key: " + keyword); } return; } @@ -226,11 +226,11 @@ namespace DeukBot4.MessageHandlers.CommandHandler var result = serverTags.RemoveTag(keyword); if (!result.success) { - cmd.SendMessageAsync("Something went wrong: " + result.result); + cmd.SendSimpleEmbed("Tag Deletion","Something went wrong: " + result.result); } else { - cmd.SendMessageAsync("Success! Removed tag with key: " + keyword); + cmd.SendSimpleEmbed("Tag Deletion","Success! Removed tag with key: " + keyword); } return; } @@ -252,12 +252,9 @@ namespace DeukBot4.MessageHandlers.CommandHandler return; } var tags = serverTags.TagList(); - var eb = new EmbedBuilder - { - Title = "This Server Has The Following Tags:", - Description = tags.Join("\n"), - Color = Color.Gold, - }; + var eb = EmbedFactory.GetStandardEmbedBuilder(); + eb.Title = "This Server Has The Following Tags"; + eb.Description = tags.Join("\n"); cmd.OriginalMessage.Channel.SendMessageAsync("", embed: eb.Build()); } diff --git a/DeukBot4/Program.cs b/DeukBot4/Program.cs index e0b5d21..d1def0e 100644 --- a/DeukBot4/Program.cs +++ b/DeukBot4/Program.cs @@ -63,6 +63,7 @@ namespace DeukBot4 properties.Username = Settings.Username; properties.Avatar = new Image("avatar.png"); }); + EmbedFactory.Initialize(Client.CurrentUser); Console.WriteLine(("Joined Guilds: ")); foreach (var clientGuild in Client.Guilds) {