1 |
rakinar2 |
577 |
import DiscordClient from '../client/Client'; |
2 |
|
|
import { User, GuildMember, GuildChannel, Role, Guild } from 'discord.js'; |
3 |
|
|
|
4 |
|
|
export async function parseUser(client: DiscordClient, input: string): Promise <User | null> { |
5 |
|
|
if (!/^\<\@(\!)?(\d+)\>$/.test(input.trim())) { |
6 |
|
|
return null; |
7 |
|
|
} |
8 |
|
|
|
9 |
|
|
let user: User | null = null; |
10 |
|
|
|
11 |
|
|
try { |
12 |
|
|
user = await client.users.fetch(input.trim().substring(input.includes('!') ? 3 : 2, input.trim().length - 1)); |
13 |
|
|
} |
14 |
|
|
catch (e) { |
15 |
|
|
return null; |
16 |
|
|
} |
17 |
|
|
|
18 |
|
|
return user; |
19 |
|
|
} |
20 |
|
|
|
21 |
|
|
export async function parseMember(guild: Guild, input: string): Promise <GuildMember | null> { |
22 |
|
|
if (!/^\<\@(\!)?(\d+)\>$/.test(input.trim())) { |
23 |
|
|
return null; |
24 |
|
|
} |
25 |
|
|
|
26 |
|
|
let user: GuildMember | null = null; |
27 |
|
|
|
28 |
|
|
try { |
29 |
|
|
user = await guild.members.fetch(input.trim().substring(input.includes('!') ? 3 : 2, input.trim().length - 1)); |
30 |
|
|
} |
31 |
|
|
catch (e) { |
32 |
|
|
return null; |
33 |
|
|
} |
34 |
|
|
|
35 |
|
|
return user; |
36 |
|
|
} |
37 |
|
|
|
38 |
|
|
export async function parseChannel(guild: Guild, input: string): Promise <GuildChannel | null> { |
39 |
|
|
if (!/^\<\#(\d+)\>$/.test(input.trim())) { |
40 |
|
|
return null; |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
let channel: GuildChannel | null = null; |
44 |
|
|
|
45 |
|
|
try { |
46 |
|
|
channel = await guild.channels.fetch(input.trim().substring(2, input.trim().length - 1)); |
47 |
|
|
} |
48 |
|
|
catch (e) { |
49 |
|
|
return null; |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
return channel; |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
export async function parseRole(guild: Guild, input: string): Promise <Role | null> { |
56 |
|
|
if (!/^\<\@\&(\d+)\>$/.test(input.trim())) { |
57 |
|
|
return null; |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
let role: Role | null = null; |
61 |
|
|
|
62 |
|
|
try { |
63 |
|
|
role = await guild.roles.fetch(input.trim().substring(3, input.trim().length - 1)); |
64 |
|
|
} |
65 |
|
|
catch (e) { |
66 |
|
|
return null; |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
return role; |
70 |
|
|
} |