-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1df7f02
commit 623ce23
Showing
1 changed file
with
25 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |