2 |
import { User, GuildMember, GuildChannel, Role, Guild } from 'discord.js'; |
import { User, GuildMember, GuildChannel, Role, Guild } from 'discord.js'; |
3 |
|
|
4 |
export async function parseUser(client: DiscordClient, input: string): Promise <User | null> { |
export async function parseUser(client: DiscordClient, input: string): Promise <User | null> { |
5 |
if (!/^\<\@(\d+)\>$/.test(input.trim())) { |
if (!/^\<\@(\!)?(\d+)\>$/.test(input.trim())) { |
6 |
return null; |
return null; |
7 |
} |
} |
8 |
|
|
9 |
let user: User | null = null; |
let user: User | null = null; |
10 |
|
|
11 |
try { |
try { |
12 |
user = await client.users.fetch(input.trim().substring(2, input.trim().length - 1)); |
user = await client.users.fetch(input.trim().substring(input.includes('!') ? 3 : 2, input.trim().length - 1)); |
13 |
} |
} |
14 |
catch (e) { |
catch (e) { |
15 |
return null; |
return null; |
19 |
} |
} |
20 |
|
|
21 |
export async function parseMember(guild: Guild, input: string): Promise <GuildMember | null> { |
export async function parseMember(guild: Guild, input: string): Promise <GuildMember | null> { |
22 |
if (!/^\<\@(\d+)\>$/.test(input.trim())) { |
if (!/^\<\@(\!)?(\d+)\>$/.test(input.trim())) { |
23 |
return null; |
return null; |
24 |
} |
} |
25 |
|
|
26 |
let user: GuildMember | null = null; |
let user: GuildMember | null = null; |
27 |
|
|
28 |
try { |
try { |
29 |
user = await guild.members.fetch(input.trim().substring(2, input.trim().length - 1)); |
user = await guild.members.fetch(input.trim().substring(input.includes('!') ? 3 : 2, input.trim().length - 1)); |
30 |
} |
} |
31 |
catch (e) { |
catch (e) { |
32 |
return null; |
return null; |