Skip to content

Commit

Permalink
MessageLinkArgument: Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
networkException committed May 13, 2021
1 parent 1df7f02 commit 623ce23
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions arguments/messageLinkArgument.ts
Original file line number Diff line number Diff line change
@@ -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<Message> {
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]);
}
}

0 comments on commit 623ce23

Please sign in to comment.