diff options
-rw-r--r-- | src/commands/info/pronouns.ts | 106 | ||||
-rw-r--r-- | src/config/example-options.ts | 1 | ||||
-rw-r--r-- | src/lib/extensions/Util.ts | 23 | ||||
-rw-r--r-- | src/listeners/client/syncslashcommands.ts | 8 |
4 files changed, 127 insertions, 11 deletions
diff --git a/src/commands/info/pronouns.ts b/src/commands/info/pronouns.ts new file mode 100644 index 0000000..07d210f --- /dev/null +++ b/src/commands/info/pronouns.ts @@ -0,0 +1,106 @@ +import { BotCommand } from '../../lib/extensions/BotCommand'; +import { User, Message, MessageEmbed } from 'discord.js'; +import got, { HTTPError } from 'got'; +import { CommandInteraction } from 'discord.js'; +import { ApplicationCommandOptionType } from 'discord-api-types'; +import { SlashCommandOption } from '../../lib/extensions/Util'; + +export const pronounMapping = { + unspecified: 'Unspecified', + hh: 'He/Him', + hi: 'He/It', + hs: 'He/She', + ht: 'He/They', + ih: 'It/Him', + ii: 'It/Its', + is: 'It/She', + it: 'It/They', + shh: 'She/He', + sh: 'She/Her', + si: 'She/It', + st: 'She/They', + th: 'They/He', + ti: 'They/It', + ts: 'They/She', + tt: 'They/Them', + any: 'Any pronouns', + other: 'Other pronouns', + ask: 'Ask me my pronouns', + avoid: 'Avoid pronouns, use my name' +}; +export type pronounsType = keyof typeof pronounMapping; + +export default class PronounsCommand extends BotCommand { + constructor() { + super('pronouns', { + aliases: ['pronouns', 'pronoun'], + category: 'utilities', + description: { + usage: 'pronouns <user>', + examples: ['pronouns IRONM00N'], + content: 'Finds the pronouns of a user using https://pronoundb.org.' + }, + args: [ + { + id: 'user', + type: 'user', + default: null + } + ], + clientPermissions: ['SEND_MESSAGES'], + slashCommandOptions: [ + { + type: ApplicationCommandOptionType.USER, + name: 'user', + description: 'The user to get pronouns for', + required: false + } + ], + slashEmphemeral: true // I'll add dynamic checking to this later + }); + } + async sendResponse(message: Message|CommandInteraction, user: User, author: boolean): Promise<void> { + try { + const apiRes: { pronouns: pronounsType } = await got.get(`https://pronoundb.org/api/v1/lookup?platform=discord&id=${user.id}`).json(); + if (message instanceof Message) { + message.reply( + new MessageEmbed({ + title: `${author ? 'Your' : `${user.tag}'s`} pronouns:`, + description: pronounMapping[apiRes.pronouns], + footer: { + text: 'Data provided by https://pronoundb.org/' + } + }) + ) + } else { + message.reply({ + embeds: [ + new MessageEmbed({ + title: `${author ? 'Your' : `${user.tag}'s`} pronouns:`, + description: pronounMapping[apiRes.pronouns], + footer: { + text: 'Data provided by https://pronoundb.org/' + } + }) + ] + }) + } + } catch (e) { + if (e instanceof HTTPError && e.response.statusCode === 404) { + if (author) { + await message.reply('You do not appear to have any pronouns set. Please go to https://pronoundb.org/ and set your pronouns.'); + } else { + await message.reply(`${user.tag} does not appear to have any pronouns set. Please tell them to go to https://pronoundb.org/ and set their pronouns.`); + } + } else throw e; + } + } + async exec(message: Message, { user }: { user?: User }): Promise<void> { + const u = user || message.author; + await this.sendResponse(message, u, u.id === message.author.id) + } + async execSlash(message: CommandInteraction, { user }: { user?: SlashCommandOption<void> }): Promise<void> { + const u = user?.user || message.user + await this.sendResponse(message, u, u.id === message.user.id) + } +}
\ No newline at end of file diff --git a/src/config/example-options.ts b/src/config/example-options.ts index 5bfb92f..f835321 100644 --- a/src/config/example-options.ts +++ b/src/config/example-options.ts @@ -11,6 +11,7 @@ export const owners = [ ]; export const prefix = '-' as string; export const dev = true as boolean; +export const devGuild = '695310188764332072' as string; export const channels = { log: 'id here', error: 'id here', diff --git a/src/lib/extensions/Util.ts b/src/lib/extensions/Util.ts index c99cb45..bd6823f 100644 --- a/src/lib/extensions/Util.ts +++ b/src/lib/extensions/Util.ts @@ -14,6 +14,7 @@ import { import { GuildChannel } from 'discord.js'; import { Role } from 'discord.js'; import chalk from 'chalk'; +import { Guild } from 'discord.js'; interface hastebinRes { key: string; @@ -244,9 +245,11 @@ export class Util extends ClientUtil { return apiRes.uuid.replace(/-/g, ''); } - public async syncSlashCommands(force = false): Promise<void> { + public async syncSlashCommands(force = false, guild?: string): Promise<void> { + let fetchedGuild: Guild + if (guild) fetchedGuild = this.client.guilds.cache.get(guild); try { - const registered = await this.client.application.commands.fetch(); + const registered = guild === undefined ? await this.client.application.commands.fetch() : await fetchedGuild.commands.fetch(); for (const [, registeredCommand] of registered) { if ( !this.client.commandHandler.modules.find( @@ -254,9 +257,9 @@ export class Util extends ClientUtil { )?.execSlash || force ) { - await this.client.application.commands.delete(registeredCommand.id); + guild === undefined ? await this.client.application.commands.delete(registeredCommand.id) : await fetchedGuild.commands.delete(registeredCommand.id); this.client.logger.verbose( - chalk`{red Deleted slash command ${registeredCommand.name}}` + chalk`{red Deleted slash command ${registeredCommand.name}${guild !== undefined ? ` in guild ${fetchedGuild.name}`:''}}` ); } } @@ -273,25 +276,25 @@ export class Util extends ClientUtil { if (found?.id && !force) { if (slashdata.description !== found.description) { - await this.client.application.commands.edit(found.id, slashdata); + guild === undefined ? await this.client.application.commands.edit(found.id, slashdata) : fetchedGuild.commands.edit(found.id, slashdata); this.client.logger.verbose( - chalk`{yellow Edited slash command ${botCommand.id}}` + chalk`{yellow Edited slash command ${botCommand.id}${guild !== undefined ? ` in guild ${fetchedGuild.name}`:''}}` ); } } else { - await this.client.application.commands.create(slashdata); + guild === undefined ? await this.client.application.commands.create(slashdata) : fetchedGuild.commands.create(slashdata); this.client.logger.verbose( - chalk`{green Created slash command ${botCommand.id}}` + chalk`{green Created slash command ${botCommand.id}${guild !== undefined ? ` in guild ${fetchedGuild.name}`:''}}` ); } } } - return this.client.logger.log(chalk.green('Slash commands registered')); + return this.client.logger.log(chalk.green(`Slash commands registered${guild !== undefined ? ` in guild ${fetchedGuild.name}`:''}`)); } catch (e) { console.log(chalk.red(e.stack)); return this.client.logger.error( - chalk`{red Slash commands not registered, see above error.}` + chalk`{red Slash commands not registered${guild !== undefined ? ` in guild ${fetchedGuild.name}`:''}, see above error.}` ); } } diff --git a/src/listeners/client/syncslashcommands.ts b/src/listeners/client/syncslashcommands.ts index 66f530f..da42185 100644 --- a/src/listeners/client/syncslashcommands.ts +++ b/src/listeners/client/syncslashcommands.ts @@ -8,6 +8,12 @@ export default class CreateSlashCommands extends BotListener { }); } async exec(): Promise<void> { - await this.client.util.syncSlashCommands(); + if (this.client.config.dev && this.client.config.devGuild) { + // Use guild slash commands for instant registration in dev + await this.client.util.syncSlashCommands(false, this.client.config.devGuild); + } else { + // Use global in production + await this.client.util.syncSlashCommands(); + } } } |