From 9be8140d4d7f2c6200af5cac0d9a3303440c3ee8 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Mon, 13 Dec 2021 20:29:45 +0100 Subject: [PATCH] Added .showembed and .showembed #channel which will show you embed json from the specified message --- CHANGELOG.md | 1 + .../Common/SmartText/SmartEmbedText.cs | 42 +++++++++++++++++++ src/NadekoBot/Modules/Utility/Utility.cs | 36 ++++++++++++++++ src/NadekoBot/data/aliases.yml | 4 +- .../data/strings/commands/commands.en-US.yml | 7 +++- 5 files changed, 88 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 688aad170..3cea4a8a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ Experimental changelog. Mostly based on [keepachangelog](https://keepachangelog. - `.remindl` and `.remindrm` commands now supports optional 'server' parameter for Administrators which allows them to delete any reminder created on the server - Added slots.currencyFontColor to gambling.yml - Added `.qexport` and `.qimport` commands which allow you to export and import quotes just like `.crsexport` +- Added `.showembed ` and `.showembed #channel ` which will show you embed json from the specified message ### Changed - `.at` and `.atl` commands reworked diff --git a/src/NadekoBot/Common/SmartText/SmartEmbedText.cs b/src/NadekoBot/Common/SmartText/SmartEmbedText.cs index 35c84a97f..8f7bc92a0 100644 --- a/src/NadekoBot/Common/SmartText/SmartEmbedText.cs +++ b/src/NadekoBot/Common/SmartText/SmartEmbedText.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using Discord; using NadekoBot.Extensions; using NadekoBot.Services; @@ -29,6 +30,47 @@ namespace NadekoBot (Footer != null && (!string.IsNullOrWhiteSpace(Footer.Text) || !string.IsNullOrWhiteSpace(Footer.IconUrl))) || (Fields != null && Fields.Length > 0); + public static SmartEmbedText FromEmbed(IEmbed eb, string plainText = null) + { + var set = new SmartEmbedText(); + + set.PlainText = plainText; + set.Title = eb.Title; + set.Description = eb.Description; + set.Url = eb.Url; + set.Thumbnail = eb.Thumbnail?.Url; + set.Image = eb.Image?.Url; + set.Author = eb.Author is EmbedAuthor ea + ? new() + { + Name = ea.Name, + Url = ea.Url, + IconUrl = ea.IconUrl + } + : null; + set.Footer = eb.Footer is EmbedFooter ef + ? new() + { + Text = ef.Text, + IconUrl = ef.IconUrl + } + : null; + + if (eb.Fields.Length > 0) + set.Fields = eb + .Fields + .Select(field => new SmartTextEmbedField() + { + Inline = field.Inline, + Name = field.Name, + Value = field.Value, + }) + .ToArray(); + + set.Color = eb.Color?.RawValue ?? 0; + return set; + } + public EmbedBuilder GetEmbed() { var embed = new EmbedBuilder() diff --git a/src/NadekoBot/Modules/Utility/Utility.cs b/src/NadekoBot/Modules/Utility/Utility.cs index eb184135e..4cf0f3ef6 100644 --- a/src/NadekoBot/Modules/Utility/Utility.cs +++ b/src/NadekoBot/Modules/Utility/Utility.cs @@ -362,7 +362,41 @@ namespace NadekoBot.Modules.Utility await ctx.Channel.EmbedAsync(embed); } + + [NadekoCommand, Aliases] + [RequireContext(ContextType.Guild)] + public Task ShowEmbed(ulong messageId) + => ShowEmbed((ITextChannel)ctx.Channel, messageId); + [NadekoCommand, Aliases] + [RequireContext(ContextType.Guild)] + public async Task ShowEmbed(ITextChannel ch, ulong messageId) + { + var user = (IGuildUser)ctx.User; + var perms = user.GetPermissions(ch); + if (!perms.ReadMessageHistory || !perms.ViewChannel) + { + await ReplyErrorLocalizedAsync(strs.insuf_perms_u); + return; + } + + var msg = await ch.GetMessageAsync(messageId); + if (msg is null) + { + await ReplyErrorLocalizedAsync(strs.msg_not_found); + return; + } + + var embed = msg.Embeds.FirstOrDefault(); + if (embed is null) + { + await ReplyErrorLocalizedAsync(strs.not_found); + return; + } + + var json = SmartEmbedText.FromEmbed(embed, msg.Content).ToJson(); + await SendConfirmAsync(Format.Sanitize(json).Replace("](", "]\\(")); + } [NadekoCommand, Aliases] [RequireContext(ContextType.Guild)] @@ -433,6 +467,8 @@ namespace NadekoBot.Modules.Utility New } + + // [NadekoCommand, Usage, Description, Aliases] // [RequireContext(ContextType.Guild)] // public async Task CreateMyInvite(CreateInviteType type = CreateInviteType.Any) diff --git a/src/NadekoBot/data/aliases.yml b/src/NadekoBot/data/aliases.yml index 7c14f0edd..30858f131 100644 --- a/src/NadekoBot/data/aliases.yml +++ b/src/NadekoBot/data/aliases.yml @@ -1270,4 +1270,6 @@ quotesexport: - qexport quotesimport: - quotesimport - - qimport \ No newline at end of file + - qimport +showembed: + - showembed \ No newline at end of file diff --git a/src/NadekoBot/data/strings/commands/commands.en-US.yml b/src/NadekoBot/data/strings/commands/commands.en-US.yml index b98b6cbd9..fbc6b7c1f 100644 --- a/src/NadekoBot/data/strings/commands/commands.en-US.yml +++ b/src/NadekoBot/data/strings/commands/commands.en-US.yml @@ -2152,4 +2152,9 @@ imageonlychannel: coordreload: desc: "Reloads coordinator config" args: - - "" \ No newline at end of file + - "" +showembed: + desc: "Prints the json equivalent of the embed of the message specified by its Id." + args: + - "820022733172121600" + - "#some-channel 820022733172121600" \ No newline at end of file