Lots of work regarding using embeds instead of plain text for messages

This commit is contained in:
Deukhoofd 2018-10-08 16:50:23 +02:00
parent b849c1b734
commit 65094f39b2
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
10 changed files with 140 additions and 66 deletions

View File

@ -5,10 +5,9 @@
<LangVersion>7.1</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Discord.Net" Version="2.0.0-beta" />
<PackageReference Include="Microsoft.NETCore.App" Version="2.1.2" />
<PackageReference Include="Discord.Net" Version="2.0.0-beta2-00999" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="Npgsql" Version="4.0.2" />
<PackageReference Include="Npgsql" Version="4.0.3" />
<PackageReference Include="StackExchange.Redis" Version="1.2.6" />
</ItemGroup>
</Project>

View File

@ -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<EmbedFieldBuilder>()
{
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)]

View File

@ -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;
}

View File

@ -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());

View File

@ -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;
}

View File

@ -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;
}
}
}

View File

@ -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<string, Dictionary<string, string>>();
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();
}

View File

@ -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<RestUserMessage> 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<RestUserMessage> SendSimpleEmbed(string title, string description, IEnumerable<EmbedFieldBuilder> fields = null)
{
var eb = EmbedFactory.GetStandardEmbedBuilder();
eb.Title = title;
eb.Description = description;
eb.Fields = fields == null ? new List<EmbedFieldBuilder>() : fields.ToList();
return OriginalMessage.Channel.SendMessageAsync("", embed: eb.Build());
}
public static async Task<(CommandRequest, RequestCode, object)> Create(SocketMessage message)
{
var originalMessage = message;

View File

@ -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());
}

View File

@ -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)
{