From 705e0939994e511624347866dcee6a10c9f20d0f Mon Sep 17 00:00:00 2001 From: Keanu Date: Sat, 20 Mar 2021 13:27:57 +0100 Subject: [PATCH] Added message quoting. See https://is.gd/lpGqxj --- package-lock.json | 6 ++-- src/core/lib.ts | 1 + src/events/message.ts | 5 +++ src/modules/message_embed.ts | 60 ++++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 src/modules/message_embed.ts diff --git a/package-lock.json b/package-lock.json index 31bcc46..3eb82ca 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1182,9 +1182,9 @@ "integrity": "sha512-OdjXJxnCN1AvyLSzeKIgXTXxV+99ZuXl3Hpo9XpJAv9MBcHrrJOQ5kV7ypXOuQie+AmWG25hLbiKdwYTifzcfQ==" }, "typescript": { - "version": "3.9.7", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.9.7.tgz", - "integrity": "sha512-BLbiRkiBzAwsjut4x/dsibSTB6yWpwT5qWmC2OfuCg3GgVQCSgMs4vEctYPhsaGtd0AeuuHMkjZ2h2WG8MSzRw==", + "version": "4.2.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.2.3.tgz", + "integrity": "sha512-qOcYwxaByStAWrBf4x0fibwZvMRG+r4cQoTjbPtUlrWjBHbmCAww1i448U0GJ+3cNNEtebDteo/cHOR3xJ4wEw==", "dev": true }, "which": { diff --git a/src/core/lib.ts b/src/core/lib.ts index 4cadf4d..b35577f 100644 --- a/src/core/lib.ts +++ b/src/core/lib.ts @@ -556,6 +556,7 @@ export abstract class GenericStructure { public save(asynchronous = true) { const tag = this.__meta__; + /// @ts-ignore delete this.__meta__; FileManager.write(tag, this, asynchronous); this.__meta__ = tag; diff --git a/src/events/message.ts b/src/events/message.ts index f793971..bcb1343 100644 --- a/src/events/message.ts +++ b/src/events/message.ts @@ -4,6 +4,7 @@ import {hasPermission, getPermissionLevel, PermissionNames} from "../core/permis import {Permissions, Collection} from "discord.js"; import {getPrefix} from "../core/structures"; import $, {replyEventListeners} from "../core/lib"; +import quote from "../modules/message_embed"; // It's a rather hacky solution, but since there's no top-level await, I just have to make the loading conditional. let commands: Collection | null = null; @@ -31,6 +32,10 @@ export default new Event<"message">({ const clientUser = message.client.user; let usesBotSpecificPrefix = false; + if (!message.content.startsWith(prefix)) { + return quote(message); + } + // If the client user exists, check if it starts with the bot-specific prefix. if (clientUser) { // If the prefix starts with the bot-specific prefix, go off that instead (these two options must mutually exclude each other). diff --git a/src/modules/message_embed.ts b/src/modules/message_embed.ts new file mode 100644 index 0000000..b69e76e --- /dev/null +++ b/src/modules/message_embed.ts @@ -0,0 +1,60 @@ +import { client } from '..' +import { Message, TextChannel, APIMessage, MessageEmbed } from 'discord.js' +import { getPrefix } from '../core/structures' +import { DiscordAPIError } from 'discord.js' + +export default async function quote(message: Message) { + if (message.author.bot) return + // const message_link_regex = message.content.match(/(!)?https?:\/\/\w+\.com\/channels\/(\d+)\/(\d+)\/(\d+)/) + const message_link_regex = message.content.match(/([?)/) + + if (message_link_regex == null) return + const [, char, guildID, channelID, messageID] = message_link_regex + + if (char || message.content.startsWith(getPrefix(message.guild))) return + + try { + const channel = client.guilds.cache.get(guildID)?.channels.cache.get(channelID) as TextChannel + const link_message = await channel.messages.fetch(messageID) + + let rtmsg: string | APIMessage = '' + if (link_message.cleanContent) { + rtmsg = new APIMessage(message.channel as TextChannel, { + content: link_message.cleanContent, + disableMentions: 'all', + files: link_message.attachments.array() + }) + } + + const embeds = [ + ...link_message.embeds.filter(v => v.type == 'rich'), + ...link_message.attachments.values() + ] + + /// @ts-ignore + if (!link_message.cleanContent && embeds.empty) { + const Embed = new MessageEmbed() + .setDescription('🚫 The message is empty.') + return message.channel.send(Embed) + } + + const infoEmbed = new MessageEmbed() + .setAuthor( + link_message.author.username, + link_message.author.displayAvatarURL({format: 'png', dynamic: true, size: 4096})) + .setTimestamp(link_message.createdTimestamp) + .setDescription(`${link_message.cleanContent}\n\nSent in **${link_message.guild?.name}** | <#${link_message.channel.id}> ([link](https://discord.com/channels/${guildID}/${channelID}/${messageID}))`); + if (link_message.attachments.size !== 0) { + const image = link_message.attachments.first(); + /// @ts-ignore + infoEmbed.setImage(image.url); + } + + await message.channel.send(infoEmbed) + } catch (error) { + if (error instanceof DiscordAPIError) { + message.channel.send("I don't have access to this channel, or something else went wrong.") + } + return console.error(error) + } +} \ No newline at end of file