NoMoreAcronyms/commands/removePhrase.cjs

50 lines
1.7 KiB
JavaScript

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("remove_phrase")
.setDescription("removes abbreviation")
.addStringOption( (option) =>
option.setName("abbreviation")
.setDescription("The abbreviation to target")
.setRequired(true)
),
async execute(interaction) {
var abbreviation = interaction.options.getString('abbreviation').toLowerCase();
//var phrase = interaction.options.getString('phrase');
console.log("Command executed by " + interaction.member)
//(user.member.roles.cache.some(role => role.name == "Trusted Users"))
if (interaction.member == "294976590658666497")
{
await interaction.reply('Removing abbreviation `' + abbreviation + "` from the target list...");
removePhrase(abbreviation);
} else {
await interaction.reply('Sorry, but you are not authorized to run this command...');
}
},
};
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.target_phrases);
} catch (err) {
console.error(err);
}
}