Added more functionality to the react command
This commit is contained in:
parent
5165c5ec4b
commit
5665a91af4
|
@ -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 <emote name> (<message ID / distance>)",
|
||||
usage: 'react <emotes...> (<distance / message ID / "Copy ID" / "Copy Message Link">)',
|
||||
async run($: CommonLibrary): Promise<any> {
|
||||
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/<Guild ID>/<Channel ID>/<Message ID> ("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();
|
||||
}
|
||||
// <Channel ID>-<Message ID> ("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();
|
||||
}
|
||||
// <Message ID>
|
||||
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();
|
||||
|
|
Loading…
Reference in New Issue