NoMoreAcronyms/main.ts

129 lines
3.9 KiB
TypeScript

2023-03-31 00:34:58 +07:00
// Require the necessary discord.js classes
const { Client, Events, GatewayIntentBits, REST, Routes, Collection, FLAGS } = require('discord.js');
const Discord = require('discord.js');
const { clientId, guildId, token } = require('./key.json');
const fs = require('node:fs');
const path = require('node:path');
// Create a new client instance
const client = new Discord.Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
});
// When the client is ready, run this code (only once)
// We use 'c' for the event parameter to keep it separate from the already defined 'client'
client.once(Events.ClientReady, c => {
console.log(`Ready! Logged in as ${c.user.tag}`);
});
// Log in to Discord with your client's token
client.login(token);
// Retrieve commands
client.commands = new Collection();
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.ts'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
// Set a new item in the Collection with the key as the command name and the value as the exported module
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
} else {
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
}
});
// My code
var abbreviationKey = require("./abbreviation_key.json");
2023-03-31 12:10:42 +07:00
function arrayRotate(arr, reverse, amount) {
for (var i = 0; i < amount; i++) {
if (reverse) arr.unshift(arr.pop());
else arr.push(arr.shift());
return arr;
}
2023-03-31 00:34:58 +07:00
}
2023-03-31 12:10:42 +07:00
function matchAbbr(abbrTarget) {
for (var abbr in abbreviationKey.target_phrases) {
if (abbreviationKey.target_phrases[abbr] === abbrTarget) {
return abbr;
} else {
return "";
}
}
}
2023-03-31 00:34:58 +07:00
2023-03-31 12:10:42 +07:00
function replyMessage(message, correctedMessage) {
2023-03-31 12:16:32 +07:00
message.reply("Your message contains an acronym! Let me fix that for you: \n `" + correctedMessage + "`");
2023-03-31 12:10:42 +07:00
}
2023-03-31 00:34:58 +07:00
client.on('messageCreate', message => {
console.log(`${message.author.tag} in #${message.channel.name} sent: ${message.content}`);
if (message.author.bot) {
return;
}
2023-03-31 12:10:42 +07:00
var matchMessageArray = message.content.toLowerCase().split(/[ ,!@#$%^&*()]+/);
console.log(matchMessageArray);
for (let i = 0; i < matchMessageArray.length; ++i) {
if (abbreviationKey.target_phrases[matchMessageArray[i]] !== undefined) {
//Return key
var phrase = abbreviationKey.target_phrases[matchMessageArray[i]];
var abbr = matchAbbr(phrase);
console.log("Found abbreviation: " + abbr);
console.log("Phrase that matches used acronym: " + phrase);
var rebuildMessageArray = message.content.split(new RegExp(abbr, 'i'));
arrayRotate(rebuildMessageArray, true, 1);
rebuildMessageArray.unshift(phrase);
arrayRotate(rebuildMessageArray, true, 2);
console.log(rebuildMessageArray);
// Build into string and set to variable!
var correctedMessage = "";
for (let j = 0; j < rebuildMessageArray.length; j++) {
correctedMessage += rebuildMessageArray[j];
}
console.log(correctedMessage);
replyMessage(message, correctedMessage);
//break;
2023-03-31 00:34:58 +07:00
}
}
2023-03-31 12:10:42 +07:00
2023-03-31 00:34:58 +07:00
}
);