From 19d3aa7819a97155d597dbc05b3b75cabe76bc80 Mon Sep 17 00:00:00 2001 From: "Ean Milligan (Bastion)" Date: Sun, 29 Jan 2023 01:28:08 -0500 Subject: [PATCH] add delete command to reset a lfg channel --- db/populateDefaults.ts | 3 +- src/commands/_index.ts | 3 +- src/commands/delete.ts | 68 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 src/commands/delete.ts diff --git a/db/populateDefaults.ts b/db/populateDefaults.ts index 31f6dea..055a70d 100644 --- a/db/populateDefaults.ts +++ b/db/populateDefaults.ts @@ -4,9 +4,10 @@ import { dbClient } from '../src/db.ts'; console.log('Attempting to insert default actions into command_cnt'); const actions = [ - 'cmd-setup', + 'cmd-delete', 'cmd-info', 'cmd-report', + 'cmd-setup', ]; for (const action of actions) { await dbClient.execute('INSERT INTO command_cnt(command) values(?)', [action]).catch((e) => { diff --git a/src/commands/_index.ts b/src/commands/_index.ts index 679332b..d090a80 100644 --- a/src/commands/_index.ts +++ b/src/commands/_index.ts @@ -5,8 +5,9 @@ import utils from '../utils.ts'; import info from './info.ts'; import report from './report.ts'; import setup from './setup.ts'; +import deleteCmd from './delete.ts'; -export const commands: Array = [info, report, setup]; +export const commands: Array = [deleteCmd, info, report, setup]; export const createSlashCommands = async (bot: Bot) => { const globalCommands: MakeRequired[] = []; diff --git a/src/commands/delete.ts b/src/commands/delete.ts new file mode 100644 index 0000000..7a41ca4 --- /dev/null +++ b/src/commands/delete.ts @@ -0,0 +1,68 @@ +import config from '../../config.ts'; +import { ApplicationCommandFlags, ApplicationCommandTypes, Bot, Interaction, InteractionResponseTypes } from '../../deps.ts'; +import { failColor, somethingWentWrong, successColor } from '../commandUtils.ts'; +import { dbClient, lfgChannelSettings, queries } from '../db.ts'; +import { CommandDetails } from '../types/commandTypes.ts'; +import utils from '../utils.ts'; + +const details: CommandDetails = { + name: 'delete-lfg-channel', + description: `Removes all settings from ${config.name} related to this LFG channel. Events will not be deleted.`, + type: ApplicationCommandTypes.ChatInput, + defaultMemberPermissions: ['ADMINISTRATOR'], +}; + +const execute = async (bot: Bot, interaction: Interaction) => { + dbClient.execute(queries.callIncCnt('cmd-delete')).catch((e) => utils.commonLoggers.dbError('delete.ts', 'call sproc INC_CNT on', e)); + + if (interaction.guildId && interaction.channelId) { + if (!lfgChannelSettings.has(`${interaction.guildId}-${interaction.channelId}`)) { + // Cannot delete a lfg channel that has not been set up + bot.helpers.sendInteractionResponse(interaction.id, interaction.token, { + type: InteractionResponseTypes.ChannelMessageWithSource, + data: { + flags: ApplicationCommandFlags.Ephemeral, + embeds: [{ + color: failColor, + title: 'Unable to delete LFG channel.', + description: + 'This channel is already is not an LFG channel. If you need to setup the channel, please run `/setup` in this channel.\n\nThis will not harm any active events in this channel and simply resets the settings for this channel.', + }], + }, + }).catch((e: Error) => utils.commonLoggers.interactionSendError('delete.ts', interaction, e)); + return; + } + + // Remove it from the DB + let dbError = false; + await dbClient.execute('DELETE FROM guild_settings WHERE guildId = ? AND lfgChannelId = ?', [interaction.guildId, interaction.channelId]).catch((e) => { + utils.commonLoggers.dbError('delete.ts', 'delete guild/lfgChannel', e); + dbError = true; + }); + if (dbError) { + somethingWentWrong(bot, interaction, 'deleteDBDeleteFail'); + return; + } + lfgChannelSettings.delete(`${interaction.guildId}-${interaction.channelId}`); + + // Complete the interaction + bot.helpers.sendInteractionResponse(interaction.id, interaction.token, { + type: InteractionResponseTypes.ChannelMessageWithSource, + data: { + flags: ApplicationCommandFlags.Ephemeral, + embeds: [{ + color: successColor, + title: 'LFG Channel settings removed!', + description: `${config.name} has finished removing the settings for this channel. You may safely dismiss this message.`, + }], + }, + }).catch((e: Error) => utils.commonLoggers.interactionSendError('delete.ts', interaction, e)); + } else { + somethingWentWrong(bot, interaction, 'deleteMissingGuildIdChannelId'); + } +}; + +export default { + details, + execute, +};