diff options
author | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2021-08-29 18:30:04 -0400 |
---|---|---|
committer | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2021-08-29 18:30:04 -0400 |
commit | a51dc607be54e600248c8c3c86f9881470ff4158 (patch) | |
tree | 6a3c6f5132fca493639790fb68b3368bc700990b /src/commands/config/settings.ts | |
parent | 0d71ac0234f7e71d60ae727a9f1db9ad66a47bde (diff) | |
download | tanzanite-a51dc607be54e600248c8c3c86f9881470ff4158.tar.gz tanzanite-a51dc607be54e600248c8c3c86f9881470ff4158.tar.bz2 tanzanite-a51dc607be54e600248c8c3c86f9881470ff4158.zip |
level image, fixes, revamped role command (still broken), continued working on settings command
Diffstat (limited to 'src/commands/config/settings.ts')
-rw-r--r-- | src/commands/config/settings.ts | 142 |
1 files changed, 131 insertions, 11 deletions
diff --git a/src/commands/config/settings.ts b/src/commands/config/settings.ts index a8070e2..8a5f290 100644 --- a/src/commands/config/settings.ts +++ b/src/commands/config/settings.ts @@ -1,4 +1,5 @@ -import { BushCommand, BushMessage, BushSlashMessage, guildSettingsObj, settingsArr } from '@lib'; +import { BushCommand, BushMessage, BushSlashMessage, GuildSettings, guildSettingsObj, settingsArr } from '@lib'; +import { ArgumentOptions, Flag } from 'discord-akairo'; import { Message, MessageActionRow, @@ -8,6 +9,7 @@ import { MessageOptions, MessageSelectMenu } from 'discord.js'; +import _ from 'lodash'; export default class SettingsCommand extends BushCommand { public constructor() { @@ -16,13 +18,15 @@ export default class SettingsCommand extends BushCommand { category: 'config', description: { content: 'Configure server options.', - usage: 'settings', + usage: `settings (${settingsArr.map((s) => `\`${s}\``).join(', ')}) (${['view', 'set', 'add', 'remove'].map( + (s) => `\`${s}\`` + )})`, examples: ['settings'] }, slash: true, slashOptions: settingsArr.map((setting) => { return { - name: util.camelToSnakeCase(setting), + name: _.snakeCase(setting), description: `Manage the server's ${guildSettingsObj[setting].name.toLowerCase()}`, type: 'SUB_COMMAND_GROUP', options: guildSettingsObj[setting].type.includes('-array') @@ -95,14 +99,122 @@ export default class SettingsCommand extends BushCommand { }); } - // *args(): any {} + // I make very readable code :) + *args(message: BushMessage): IterableIterator<ArgumentOptions | Flag> { + const setting = yield { + id: 'setting', + type: settingsArr, + prompt: { + start: `What setting would you like to see or change? You can choose one of the following: ${settingsArr + .map((s) => `\`${s}\``) + .join(', ')}`, + retry: `{error} Choose one of the following settings: ${settingsArr.map((s) => `\`${s}\``).join(', ')}`, + optional: message.util.parsed!.alias === 'settings' + } + }; + + const action = yield { + id: 'action', + type: guildSettingsObj[setting as unknown as GuildSettings].type.includes('-array') + ? ['view', 'add', 'remove'] + : ['view', 'set'], + prompt: { + start: `Would you like to ${util.oxford( + (guildSettingsObj[setting as unknown as GuildSettings].type.includes('-array') + ? ['view', 'add', 'remove'] + : ['view', 'set'] + ).map((a) => `\`${a}\``), + 'or' + )} the \`${setting}\` setting?`, + retry: `{error} Choose one of the following actions to perform on the \`${setting}\` setting: ${util.oxford( + (guildSettingsObj[setting as unknown as GuildSettings].type.includes('-array') + ? ['view', 'add', 'remove'] + : ['view', 'set'] + ).map((a) => `\`${a}\``), + 'or' + )}`, + optional: message.util.parsed!.alias === 'settings' + } + }; - public override async exec(message: BushMessage | BushSlashMessage, args: unknown): Promise<unknown> { - client.console.debugRaw(message.interaction); - client.console.debugRaw(args); + const value = + action === 'view' + ? undefined + : yield { + id: 'value', + type: 'string', + match: 'restContent', + prompt: { + start: `What would you like to ${action} ${ + (action as unknown as 'add' | 'remove' | 'set') === 'add' + ? `to the ${setting} setting` + : (action as unknown as 'remove' | 'set') === 'remove' + ? `from the ${setting} setting` + : `the ${setting} setting to` + }?`, + retry: `{error} You must choose a value to ${action} ${ + (action as unknown as 'add' | 'remove' | 'set') === 'add' + ? `to the ${setting} setting` + : (action as unknown as 'remove' | 'set') === 'remove' + ? `from the ${setting} setting` + : `the ${setting} setting to` + }.`, + optional: message.util.parsed!.alias === 'settings' + } + }; + + return { setting, action, value }; + } + + public override async exec( + message: BushMessage | BushSlashMessage, + args: { [x in GuildSettings | ('view' | 'set' | 'add' | 'remove') | ('setting' | 'action') | 'value']: string | undefined } + ): Promise<unknown> { if (!message.guild) return await message.util.reply(`${util.emojis.error} This command can only be used in servers.`); - const messageOptions = await this.generateMessageOptions(message); - const msg = (await message.util.reply(messageOptions)) as Message; + if (!message.member?.permissions.has('MANAGE_GUILD')) + return await message.util.reply( + `${util.emojis.error} You must have the **MANAGE_GUILD** permissions to run this command.` + ); + const setting = _.camelCase(args[settingsArr.find((s) => args[s]) ?? 'setting']) as GuildSettings | undefined; + const action = (args[ + (['view', 'set', 'add', 'remove'] as ('view' | 'set' | 'add' | 'remove')[]).find((a) => args[a]) ?? 'action' + ] ?? 'view') as 'view' | 'set' | 'add' | 'remove'; + const value = args.value; + + let msg; + + if (!setting || action === 'view') { + const messageOptions = await this.generateMessageOptions(message, setting ?? undefined); + msg = (await message.util.reply(messageOptions)) as Message; + } else { + if (!value) + return await message.util.reply( + `${util.emojis.error} You must choose a value to ${action} ${ + (action as unknown as 'add' | 'remove' | 'set') === 'add' + ? `to the ${setting} setting` + : (action as unknown as 'remove' | 'set') === 'remove' + ? `from the ${setting} setting` + : `the ${setting} setting to` + }` + ); + switch (action) { + case 'add': + case 'remove': { + const existing = (await message.guild.getSetting(setting)) as string[]; + const updated = util.addOrRemoveFromArray('add', existing, value); + await message.guild.setSetting(setting, updated); + const messageOptions = await this.generateMessageOptions(message); + msg = (await message.util.reply(messageOptions)) as Message; + break; + } + case 'set': { + await message.guild.setSetting(setting, value); + const messageOptions = await this.generateMessageOptions(message); + msg = (await message.util.reply(messageOptions)) as Message; + break; + } + } + } const collector = msg.createMessageComponentCollector({ channel: message.channel ?? undefined, guild: message.guild, @@ -121,6 +233,11 @@ export default class SettingsCommand extends BushCommand { await this.generateMessageOptions(message, interaction.values[0] as keyof typeof guildSettingsObj) ); } + case 'command_settingsBack': { + if (!interaction.isButton()) return; + + return interaction.update(await this.generateMessageOptions(message)); + } } } else { return await interaction?.deferUpdate().catch(() => undefined); @@ -130,7 +247,7 @@ export default class SettingsCommand extends BushCommand { public async generateMessageOptions( message: BushMessage | BushSlashMessage, - feature?: keyof typeof guildSettingsObj + feature?: undefined | keyof typeof guildSettingsObj ): Promise<MessageOptions> { if (!message.guild) throw new Error('message.guild is null'); const settingsEmbed = new MessageEmbed().setTitle(`${message.guild!.name}'s Settings`).setColor(util.colors.default); @@ -158,6 +275,7 @@ export default class SettingsCommand extends BushCommand { type: 'string' | 'channel' | 'channel-array' | 'role' | 'role-array' ): Promise<string> => { const feat = await message.guild!.getSetting(feature); + console.debug(feat); switch (type.replace('-array', '') as 'string' | 'channel' | 'role') { case 'string': { return Array.isArray(feat) @@ -172,6 +290,7 @@ export default class SettingsCommand extends BushCommand { } } }; + const components = new MessageActionRow().addComponents( new MessageButton().setStyle('PRIMARY').setCustomId('command_settingsBack').setLabel('Back') ); @@ -184,7 +303,8 @@ export default class SettingsCommand extends BushCommand { ); settingsEmbed.addField( guildSettingsObj[feature].name, - await generateCurrentValue(feature as 'string' | 'channel' | 'channel-array' | 'role' | 'role-array') + (await generateCurrentValue(feature as 'string' | 'channel' | 'channel-array' | 'role' | 'role-array')) || + '[No Value Set]' ); return { embeds: [settingsEmbed], components: [components] }; } |