From 688bafe0aa040522712d4534b8e761042fc79a5f Mon Sep 17 00:00:00 2001 From: Caleb Fontenot Date: Sat, 1 Apr 2023 11:10:21 -0500 Subject: [PATCH] added removePhrase --- commands/addPhrase.ts | 4 ++-- commands/removePhrase.ts | 47 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 commands/removePhrase.ts diff --git a/commands/addPhrase.ts b/commands/addPhrase.ts index c43bc6d..615dcdd 100644 --- a/commands/addPhrase.ts +++ b/commands/addPhrase.ts @@ -7,10 +7,10 @@ console.log(abbreviationKey); module.exports = { data: new SlashCommandBuilder() .setName("add_phrase") - .setDescription("adds_abbreviation_to_respond_to") + .setDescription("adds abbreviation to respond to") .addStringOption( (option) => option.setName("abbreviation") - .setDescription("The_abbreviation_to_target") + .setDescription("The abbreviation to target") .setRequired(true) ) .addStringOption( option => diff --git a/commands/removePhrase.ts b/commands/removePhrase.ts new file mode 100644 index 0000000..496908f --- /dev/null +++ b/commands/removePhrase.ts @@ -0,0 +1,47 @@ +const { SlashCommandBuilder } = require('discord.js'); +var abbreviationKey = require("../abbreviation_key.json"); +const fs = require('node:fs'); +var path = require('node:path');; +console.log(abbreviationKey); + +module.exports = { + data: new SlashCommandBuilder() + .setName("add_phrase") + .setDescription("adds abbreviation to respond to") + .addStringOption( (option) => + option.setName("abbreviation") + .setDescription("The abbreviation to target") + .setRequired(true) + ) + .addStringOption( option => + option.setName('phrase') + .setDescription("The phrase that the abbreviation shortens") + .setRequired(true) + ), + async execute(interaction) { + var abbreviation = interaction.options.getString('abbreviation').toLowerCase(); + //var phrase = interaction.options.getString('phrase'); + await interaction.reply('Removing abbreviation `' + abbreviation + "` from the target list... `"); + removePhrase(abbreviation); + }, +}; + +function removePhrase(abbrevation) { + console.log(abbrevation); + delete abbreviationKey.target_phrases[abbrevation]; + console.log(abbreviationKey.target_phrases); + + // Write data to file + var jsonString = JSON.stringify(abbreviationKey); + try { + //console.log(fs.existsSync("../abbreviation_key.json")); + var jsonPath = path.join(__dirname, '..', 'abbreviation_key.json'); + fs.unlinkSync(jsonPath); + fs.writeFileSync(jsonPath, jsonString, { encoding: 'utf8' }, "\t"); + console.log("Removed abbreviation from list. JSON now contains:" + abbreviationKey); + } catch (err) { + console.error(err); + } + + +} \ No newline at end of file