From 5665a91af4a01400728ad97f52768b9a22cfe5db Mon Sep 17 00:00:00 2001 From: WatDuhHekBro <44940783+WatDuhHekBro@users.noreply.github.com> Date: Tue, 15 Dec 2020 03:22:32 -0600 Subject: [PATCH] Added more functionality to the react command --- src/commands/utilities/react.ts | 64 ++++++++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 5 deletions(-) diff --git a/src/commands/utilities/react.ts b/src/commands/utilities/react.ts index 1517147..035ae44 100644 --- a/src/commands/utilities/react.ts +++ b/src/commands/utilities/react.ts @@ -1,18 +1,72 @@ import Command from "../../core/command"; import {CommonLibrary} from "../../core/lib"; +import {Message, Channel, TextChannel} from "discord.js"; export default new Command({ description: "Reacts to the a previous message in your place. You have to react with the same emote before the bot removes that reaction.", - usage: "react ()", + usage: 'react ()', async run($: CommonLibrary): Promise { - let target; + let target: Message | undefined; let distance = 1; if ($.args.length >= 2) { - const last = $.args[$.args.length - 1]; + const last = $.args[$.args.length - 1]; // Because this is optional, do not .pop() unless you're sure it's a message link indicator. + const URLPattern = /^(?:https:\/\/discord.com\/channels\/(\d{17,19})\/(\d{17,19})\/(\d{17,19}))$/; + const copyIDPattern = /^(?:(\d{17,19})-(\d{17,19}))$/; - if (/\d{17,19}/g.test(last)) { + // https://discord.com/channels/// ("Copy Message Link" Button) + if (URLPattern.test(last)) { + const match = URLPattern.exec(last)!; + const guildID = match[1]; + const channelID = match[2]; + const messageID = match[3]; + let guild = $.guild; + let channel: Channel | undefined = $.channel; + + if (guild?.id !== guildID) { + try { + guild = await $.client.guilds.fetch(guildID); + } catch { + return $.channel.send(`\`${guildID}\` is an invalid guild ID!`); + } + } + + if (channel.id !== channelID) channel = guild.channels.cache.get(channelID); + if (!channel) return $.channel.send(`\`${channelID}\` is an invalid channel ID!`); + + if ($.message.id !== messageID) { + try { + target = await (channel as TextChannel).messages.fetch(messageID); + } catch { + return $.channel.send(`\`${messageID}\` is an invalid message ID!`); + } + } + + $.args.pop(); + } + // - ("Copy ID" Button) + else if (copyIDPattern.test(last)) { + const match = copyIDPattern.exec(last)!; + const channelID = match[1]; + const messageID = match[2]; + let channel: Channel | undefined = $.channel; + + if (channel.id !== channelID) channel = $.guild?.channels.cache.get(channelID); + if (!channel) return $.channel.send(`\`${channelID}\` is an invalid channel ID!`); + + if ($.message.id !== messageID) { + try { + target = await (channel as TextChannel).messages.fetch(messageID); + } catch { + return $.channel.send(`\`${messageID}\` is an invalid message ID!`); + } + } + + $.args.pop(); + } + // + else if (/^\d{17,19}$/.test(last)) { try { target = await $.channel.messages.fetch(last); } catch { @@ -22,7 +76,7 @@ export default new Command({ $.args.pop(); } // The entire string has to be a number for this to match. Prevents leaCheeseAmerican1 from triggering this. - else if (/^\d+$/g.test(last)) { + else if (/^\d+$/.test(last)) { distance = parseInt(last); if (distance >= 0 && distance <= 99) $.args.pop();