1 |
rakinar2 |
577 |
import { Guild, Snowflake, TextBasedChannel } from "discord.js"; |
2 |
|
|
import Client from "../core/Client"; |
3 |
|
|
import { logError } from "./logger"; |
4 |
|
|
|
5 |
|
|
export async function safeMemberFetch(guild: Guild, id: Snowflake) { |
6 |
|
|
try { |
7 |
|
|
return guild.members.cache.get(id) ?? (await guild.members.fetch(id)); |
8 |
|
|
} catch (e) { |
9 |
|
|
logError(e); |
10 |
|
|
return null; |
11 |
|
|
} |
12 |
|
|
} |
13 |
|
|
|
14 |
|
|
export async function safeUserFetch(client: Client, id: Snowflake) { |
15 |
|
|
try { |
16 |
|
|
return client.users.cache.get(id) ?? (await client.users.fetch(id)); |
17 |
|
|
} catch (e) { |
18 |
|
|
logError(e); |
19 |
|
|
return null; |
20 |
|
|
} |
21 |
|
|
} |
22 |
|
|
|
23 |
|
|
export async function safeChannelFetch(guild: Guild, id: Snowflake) { |
24 |
|
|
try { |
25 |
|
|
return guild.channels.cache.get(id) ?? (await guild.channels.fetch(id)); |
26 |
|
|
} catch (e) { |
27 |
|
|
logError(e); |
28 |
|
|
return null; |
29 |
|
|
} |
30 |
|
|
} |
31 |
|
|
|
32 |
|
|
export async function safeRoleFetch(guild: Guild, id: Snowflake) { |
33 |
|
|
try { |
34 |
|
|
return guild.roles.cache.get(id) ?? (await guild.roles.fetch(id)); |
35 |
|
|
} catch (e) { |
36 |
|
|
logError(e); |
37 |
|
|
return null; |
38 |
|
|
} |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
export async function safeMessageFetch(channel: TextBasedChannel, id: Snowflake) { |
42 |
|
|
try { |
43 |
|
|
return channel.messages.cache.get(id) ?? (await channel.messages.fetch(id)); |
44 |
|
|
} catch (e) { |
45 |
|
|
logError(e); |
46 |
|
|
return null; |
47 |
|
|
} |
48 |
|
|
} |