diff --git a/src/commands/link.ts b/src/commands/link.ts index 7e576a4..ee65e6b 100644 --- a/src/commands/link.ts +++ b/src/commands/link.ts @@ -1,5 +1,5 @@ import { ApplicationIntegrationType, BitFieldResolvable, ChatInputCommandInteraction, Client, GuildBasedChannel, InteractionContextType, MessageFlags, MessageMentions, PermissionFlagsBits, SlashCommandBuilder, User } from 'discord.js'; -import { channelIsInStaffCategory, handleWhoIsInteraction, logCustomEvent } from '../utils'; +import { channelIsInStaffCategory, handleWhoIsInteraction, logCustomEvent, resolveUser } from '../utils'; import { Database } from '../shared/Database'; import { config } from '../config'; @@ -59,13 +59,7 @@ export default { const idToUse = matches ? matches.groups!.id : discordUserInput; - let user: User | null = null; - - try { - user = await client.users.fetch(idToUse); - } catch (e) { - return interaction.editReply('An error has occurred. Are you using the right format? You can provide a user ID (ex `324007235137044482`) or a user mention (you must have text previews enabled, Settings -> Chat -> Text Box).'); - } + const user = await resolveUser(client, idToUse); if (!user) return interaction.editReply("Couldn't find user."); diff --git a/src/commands/notes.ts b/src/commands/notes.ts index 0881cc4..3fb076c 100644 --- a/src/commands/notes.ts +++ b/src/commands/notes.ts @@ -1,6 +1,6 @@ import { ApplicationCommandOptionType, ApplicationIntegrationType, AutocompleteInteraction, ChatInputCommandInteraction, Client, GuildBasedChannel, InteractionContextType, MessageFlags, MessageMentions, PermissionFlagsBits, SlashCommandBuilder, User } from 'discord.js'; import { Database } from '../shared/Database'; -import { channelIsInStaffCategory, deferInteraction, logCustomEvent } from '../utils'; +import { channelIsInStaffCategory, deferInteraction, logCustomEvent, resolveUser } from '../utils'; import { getNoteMessage } from '../utils/note-utils'; const mentionRegex = new RegExp(MessageMentions.UsersPattern); @@ -95,13 +95,7 @@ export default { await deferInteraction(interaction); - let user: User | null = null; - - try { - user = await client.users.fetch(idToUse); - } catch (e) { - return interaction.editReply('An error has occurred. Are you using the right format? You can provide a user ID (ex `324007235137044482`) or a user mention (you must have text previews enabled, Settings -> Chat -> Text Box).'); - } + const user = await resolveUser(client, idToUse); if (!user) return interaction.editReply('User not found'); diff --git a/src/utils/discord-user-utils.ts b/src/utils/discord-user-utils.ts new file mode 100644 index 0000000..6aab086 --- /dev/null +++ b/src/utils/discord-user-utils.ts @@ -0,0 +1,13 @@ +import { Client, User } from 'discord.js'; + +export async function resolveUser(client: Client, value: string): Promise { + let user: User | null | undefined = null; + + try { + user = await client.users.fetch(value); + } catch (e) { + user = client.users.cache.find(u => u.username == value); + } + + return user; +} \ No newline at end of file diff --git a/src/utils/index.ts b/src/utils/index.ts index a884159..91601b5 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -5,9 +5,12 @@ export * from './ban-events'; export * from './ban-utils'; export * from './channel-utils'; export * from './commands'; +export * from './debug-utils'; +export * from './discord-user-utils'; export * from './e621-utils'; export * from './event-log-utils'; export * from './file-utils'; +export * from './github-user-utils'; export * from './interaction-utils'; export * from './message-utils'; export * from './ms-to-human';