From 623ce233453594738dbf8a08d140a781fbfbfddb Mon Sep 17 00:00:00 2001 From: networkException Date: Fri, 14 May 2021 00:40:37 +0200 Subject: [PATCH] MessageLinkArgument: Initial commit --- arguments/messageLinkArgument.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 arguments/messageLinkArgument.ts diff --git a/arguments/messageLinkArgument.ts b/arguments/messageLinkArgument.ts new file mode 100644 index 0000000..89ef277 --- /dev/null +++ b/arguments/messageLinkArgument.ts @@ -0,0 +1,25 @@ +import { Channel, Guild, Message } from 'discord.js'; +import { client } from '..'; +import { CommandError } from '../commandError'; +import { SnowflakeBasedArgument } from './base/snowflakeBasedArgument'; + +export class MessageLinkArgument extends SnowflakeBasedArgument { + + public static async parse(input: string): Promise { + if (!input) + throw CommandError.syntax('MessageLinkArgument', 'No input provided'); + + const match: RegExpMatchArray = this.regex('MessageLinkArgument', /https:\/\/(?:canary\.|ptb\.)?discord\.com\/channels\/(\d*)\/(\d*)\/(\d*)/, input); + + if (!this.validateSnowflake(match[1]) || !this.validateSnowflake(match[2]) || !this.validateSnowflake(match[3])) + throw CommandError.syntax('MessageLinkArgument', 'One or more snowflakes in the link are invalid'); + + const guild: Guild = await client.guilds.fetch(match[1]); + const channel: Channel = await guild.channels.resolve(match[2]); + + if (!channel.isText()) + throw CommandError.semantic('MessageLinkArgument', 'Linked channel does not have the type text'); + + return channel.messages.fetch(match[3]); + } +} \ No newline at end of file