Files
hexerade/src/commands/utilities/name-sync.ts
T

63 lines
2.1 KiB
TypeScript

// External Imports
import { Command, CommandBuilder, CommandClient, CommandInteraction, Constants, Member, SlashCommand } from 'athena-prime';
// Internal Imports
import { Database } from '../../shared/Database';
import { E621User } from '../../types';
import { CreateDefaultEmbed, getE621User, SetSeverity } from '../../utils';
// ---------------
async function fetchUser(member: Member, id: number | null): Promise<E621User | null> {
const syncedIds = await Database.getE621Ids(member.id);
if (!id || !syncedIds.includes(id))
return await getE621User(syncedIds[0]);
else
return await getE621User(id);
}
@SlashCommand(
new CommandBuilder('name-sync', 'Sync your Discord username with your e621 username.')
.setIntegrationTypes(Constants.ApplicationIntegrationType.GuildInstall)
.setContexts(Constants.InteractionContextType.Guild)
.setCommandType(Constants.ApplicationCommandType.ChatInput)
.addNumberOption({ name: 'id', description: 'The ID of the user to sync your name to.', required: false, max_value: 16 })
)
class NameSyncCommand extends Command<CommandClient> {
cooldown = 360; // 5m.
async handleCommand(context: CommandClient<any, any>, interaction: CommandInteraction, data: any) {
if (!interaction.inGuild()) return;
await interaction.defer();
const id = interaction.getNumber('id');
const user = await fetchUser(interaction.member, id);
if (!user) {
await interaction.createMessage({
embeds: [{
...CreateDefaultEmbed(context),
...SetSeverity('error'),
description: 'I was unable to get a user entry for your account or provided id. Please try again, or contact an administrator.'
}]
});
return;
}
await interaction.member.edit({ nick: user.name }, 'Member requested a name-sync.');
await interaction.createMessage({
embeds: [{
...CreateDefaultEmbed(context),
...SetSeverity('success'),
description: `Successfully updated your display name. Hello \`${user.name}\`!`
}]
});
}
}
export default new NameSyncCommand('name-sync');