implement removal methods

This commit is contained in:
Baipyrus 2024-04-05 11:19:12 +02:00
parent d10dd2da00
commit 54dc8bd245

View File

@ -115,35 +115,47 @@ async function addResponse(interaction) {
await interaction.showModal(modal); await interaction.showModal(modal);
} }
/** @param {ChatInputCommandInteraction} interaction */
async function removeKeyword(interaction) {
const { options } = interaction;
// Get command options
const keyword = options.getString('name');
// Try deleting keyword from database
await Keywords.destroy({
where: {
guild: interaction.guildId,
name: keyword
}
});
}
/** @param {ChatInputCommandInteraction} interaction */ /** @param {ChatInputCommandInteraction} interaction */
async function removeResponse(interaction) { async function removeResponse(interaction) {
const { options } = interaction; const { options } = interaction;
// Get command options // Get command options
/** @type {'keyword'|'response'} */ const keyword = options.getString('keyword');
const type = options.getString('type');
const name = options.getString('name'); const name = options.getString('name');
switch (type) { // Find keyword in database
case 'keyword': /** @type {import('../../models/keywords.js').Keyword} */
// Try removing keyword const found = await Keywords.findOne({
await Keywords.destroy({
where: { where: {
guild: interaction.guildId, guild: interaction.guildId,
name name: keyword
} }
}); });
break;
case 'response': // Try deleting response from database
// Try removing response
await Responses.destroy({ await Responses.destroy({
where: { where: {
guild: interaction.guildId, keyword: found.id,
name name
} }
}); });
} }
}
/** @param {ChatInputCommandInteraction} interaction */ /** @param {ChatInputCommandInteraction} interaction */
async function listResponse(interaction) { async function listResponse(interaction) {
@ -364,7 +376,7 @@ export const data = new SlashCommandBuilder()
.setDescription('Deletes a keyword completely.') .setDescription('Deletes a keyword completely.')
.addStringOption((option) => .addStringOption((option) =>
option option
.setName('keyword') .setName('name')
.setDescription('The keyword to be deleted.') .setDescription('The keyword to be deleted.')
.setAutocomplete(true) .setAutocomplete(true)
.setRequired(true) .setRequired(true)
@ -472,14 +484,21 @@ export async function autocomplete(interaction) {
export async function execute(interaction) { export async function execute(interaction) {
const { options } = interaction; const { options } = interaction;
switch (options.getSubcommand()) { const command = options.getSubcommand();
const group = options.getSubcommandGroup();
const joined = group === null ? command : `${group} ${command}`;
switch (joined) {
case 'create': case 'create':
createResponse(interaction); createResponse(interaction);
break; break;
case 'add': case 'add':
addResponse(interaction); addResponse(interaction);
break; break;
case 'remove': case 'remove keyword':
removeKeyword(interaction);
break;
case 'remove response':
removeResponse(interaction); removeResponse(interaction);
break; break;
case 'list': case 'list':