NoMoreAcronyms/commands/addPhrase.cjs

47 lines
1.6 KiB
JavaScript

2023-03-31 00:34:58 +07:00
const { SlashCommandBuilder } = require('discord.js');
var abbreviationKey = require("../abbreviation_key.json");
const fs = require('node:fs');
var path = require('node:path');;
//console.log(abbreviationKey);
2023-03-31 00:34:58 +07:00
module.exports = {
data: new SlashCommandBuilder()
.setName("add_phrase")
2023-04-01 11:10:21 +07:00
.setDescription("adds abbreviation to respond to")
2023-03-31 00:34:58 +07:00
.addStringOption( (option) =>
option.setName("abbreviation")
2023-04-01 11:10:21 +07:00
.setDescription("The abbreviation to target")
2023-03-31 00:34:58 +07:00
.setRequired(true)
)
.addStringOption( option =>
option.setName('phrase')
.setDescription("The phrase that the abbreviation shortens")
.setRequired(true)
),
2023-04-04 17:20:28 +07:00
async execute(interaction, client) {
2023-04-01 00:04:34 +07:00
var abbreviation = interaction.options.getString('abbreviation').toLowerCase();
2023-03-31 00:34:58 +07:00
var phrase = interaction.options.getString('phrase');
2023-04-01 00:01:32 +07:00
await interaction.reply('Adding abbreviation `' + abbreviation + "` to target list. This will complete to the phrase `" + phrase + "`");
2023-03-31 00:34:58 +07:00
addPhrase(abbreviation, phrase);
},
};
function addPhrase(abbrevation, phrase) {
console.log(abbrevation, phrase);
abbreviationKey.target_phrases[abbrevation] = phrase;
console.log(abbreviationKey);
// 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");
2023-03-31 12:24:31 +07:00
console.log("Added Phrase to list. JSON now contains:" + abbreviationKey);
2023-03-31 00:34:58 +07:00
} catch (err) {
console.error(err);
}
}