(rewrite): Began organising commands and polished /name-sync
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
import { Database } from '../../shared/Database';
|
||||
import { E621User } from '../../types';
|
||||
import { CreateDefaultEmbed, getE621User, SetSeverity } from '../../utils';
|
||||
|
||||
// External Imports
|
||||
import { Command, CommandBuilder, CommandClient, CommandInteraction, Constants, Member, SlashCommand } from 'athena-prime';
|
||||
|
||||
// ---------------
|
||||
|
||||
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 = 60;
|
||||
|
||||
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');
|
||||
Reference in New Issue
Block a user