From 52c14205080e3dbf32587175063d319ebd821094 Mon Sep 17 00:00:00 2001 From: Alyxia Sother Date: Sat, 21 Aug 2021 12:31:47 +0200 Subject: [PATCH] Implement disabling of message embeds As requested by @Juby210 --- src/commands/system/admin.ts | 23 ++++++++++++ src/modules/messageEmbed.ts | 73 +++++++++++++++++++----------------- src/structures.ts | 2 + 3 files changed, 64 insertions(+), 34 deletions(-) diff --git a/src/commands/system/admin.ts b/src/commands/system/admin.ts index f32125a..2e2d579 100644 --- a/src/commands/system/admin.ts +++ b/src/commands/system/admin.ts @@ -61,6 +61,29 @@ export default new NamedCommand({ }) }) }), + messageembeds: new NamedCommand({ + description: "Enable or disable sending message previews.", + usage: "enable/disable", + run: "Please specify `enable` or `disable`.", + subcommands: { + true: new NamedCommand({ + description: "Enable sending of message previews.", + async run({send, guild}) { + Storage.getGuild(guild!.id).messageEmbeds = true; + Storage.save(); + send("Sending of message previews has been enabled."); + } + }), + false: new NamedCommand({ + description: "Disable sending of message previews.", + async run({send, guild}) { + Storage.getGuild(guild!.id).messageEmbeds = false; + Storage.save(); + send("Sending of message previews has been disabled."); + } + }) + } + }), autoroles: new NamedCommand({ description: "Configure your server's autoroles.", usage: "", diff --git a/src/modules/messageEmbed.ts b/src/modules/messageEmbed.ts index e09371b..050edf6 100644 --- a/src/modules/messageEmbed.ts +++ b/src/modules/messageEmbed.ts @@ -2,50 +2,55 @@ import {client} from "../index"; import {MessageEmbed} from "discord.js"; import {getPrefix} from "../structures"; import {getMessageByID} from "onion-lasers"; +import {Storage} from "../structures"; client.on("message", async (message) => { - // Only execute if the message is from a user and isn't a command. - if (message.content.startsWith(getPrefix(message.guild)) || message.author.bot) return; - const messageLink = extractFirstMessageLink(message.content); - if (!messageLink) return; - const [guildID, channelID, messageID] = messageLink; + const {messageEmbeds} = Storage.getGuild(message.guild!.id); - const linkMessage = await getMessageByID(channelID, messageID); + if (messageEmbeds) { + // Only execute if the message is from a user and isn't a command. + if (message.content.startsWith(getPrefix(message.guild)) || message.author.bot) return; + const messageLink = extractFirstMessageLink(message.content); + if (!messageLink) return; + const [guildID, channelID, messageID] = messageLink; - // If it's an invalid link (or the bot doesn't have access to it). - if (typeof linkMessage === "string") { - return message.channel.send("I don't have access to that channel!"); - } + const linkMessage = await getMessageByID(channelID, messageID); - const embeds = [ - ...linkMessage.embeds.filter((embed) => embed.type === "rich"), - ...linkMessage.attachments.values() - ]; + // If it's an invalid link (or the bot doesn't have access to it). + if (typeof linkMessage === "string") { + return message.channel.send("I don't have access to that channel!"); + } - if (!linkMessage.cleanContent && embeds.length === 0) { - return message.channel.send(new MessageEmbed().setDescription("🚫 The message is empty.")); - } + const embeds = [ + ...linkMessage.embeds.filter((embed) => embed.type === "rich"), + ...linkMessage.attachments.values() + ]; - if (linkMessage.cleanContent.length > 2048) { - return message.channel.send(new MessageEmbed().setDescription("🚫 This message is too long.")); - } + if (!linkMessage.cleanContent && embeds.length === 0) { + return message.channel.send(new MessageEmbed().setDescription("🚫 The message is empty.")); + } - const infoEmbed = new MessageEmbed() - .setAuthor( - linkMessage.author.username, - linkMessage.author.displayAvatarURL({format: "png", dynamic: true, size: 4096}) - ) - .setTimestamp(linkMessage.createdTimestamp) - .setDescription( - `${linkMessage.cleanContent}\n\nSent in **${linkMessage.guild?.name}** | <#${linkMessage.channel.id}> ([link](https://discord.com/channels/${guildID}/${channelID}/${messageID}))` - ); + if (linkMessage.cleanContent.length > 2048) { + return message.channel.send(new MessageEmbed().setDescription("🚫 This message is too long.")); + } - if (linkMessage.attachments.size !== 0) { - const image = linkMessage.attachments.first(); - infoEmbed.setImage(image!.url); - } + const infoEmbed = new MessageEmbed() + .setAuthor( + linkMessage.author.username, + linkMessage.author.displayAvatarURL({format: "png", dynamic: true, size: 4096}) + ) + .setTimestamp(linkMessage.createdTimestamp) + .setDescription( + `${linkMessage.cleanContent}\n\nSent in **${linkMessage.guild?.name}** | <#${linkMessage.channel.id}> ([link](https://discord.com/channels/${guildID}/${channelID}/${messageID}))` + ); - return await message.channel.send(infoEmbed); + if (linkMessage.attachments.size !== 0) { + const image = linkMessage.attachments.first(); + infoEmbed.setImage(image!.url); + } + + return await message.channel.send(infoEmbed); + } else return; }); export function extractFirstMessageLink(message: string): [string, string, string] | null { diff --git a/src/structures.ts b/src/structures.ts index c92f5fc..233f462 100644 --- a/src/structures.ts +++ b/src/structures.ts @@ -79,6 +79,7 @@ class Member { class Guild { public prefix: string | null; + public messageEmbeds: boolean | null; public welcomeType: "none" | "text" | "graphical"; public welcomeChannel: string | null; public welcomeMessage: string | null; @@ -90,6 +91,7 @@ class Guild { constructor(data?: GenericJSON) { this.prefix = select(data?.prefix, null, String); + this.messageEmbeds = select(data?.messageLinks, true, Boolean); this.welcomeChannel = select(data?.welcomeChannel, null, String); this.welcomeMessage = select(data?.welcomeMessage, null, String); this.autoRoles = select(data?.autoRoles, null, String, true);