Attempt to resolve usernames

This commit is contained in:
Tarrgon
2025-09-07 20:24:25 -04:00
parent a00f4a3830
commit c5868612cf
4 changed files with 20 additions and 16 deletions
+2 -8
View File
@@ -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.");
+2 -8
View File
@@ -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');
+13
View File
@@ -0,0 +1,13 @@
import { Client, User } from 'discord.js';
export async function resolveUser(client: Client, value: string): Promise<User | null | undefined> {
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;
}
+3
View File
@@ -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';