So i want my bot on discord to have custom replies for example:
user: Hi
bot: Hello
user: ping
bot: pong
I want to make my bot do this but i'm using an event handler for all the commands i'm using and I don't want to use If(message.content ==... message.channel.send ".." in the messagecreate.js or the index.js file because I'm planning to use multiple messages like so and I'd like my code to be relatively clean and organized
so if there is any way I can move the 'if' statements to another messagereplies.js file and run it from there, please let me know
This is my messagecreat.js file
require('dotenv').config();
module.exports = async (Discord, client, message) => {
const prefix = (process.env.PREFIX);
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const cmd = args.shift().toLowerCase();
const command = client.commands.get(cmd) || client.commands.find(a => a.aliases && a.aliases.includes(cmd));
if (command) command.execute(client, message, args, Discord);
and this is my event handler
const fs = require('fs');
module.exports = (client, Discord) => {
const load_dir = (dirs) => {
const event_files = fs.readdirSync(`./events/${dirs}`).filter(file => file.endsWith('.js'))
for (const file of event_files) {
const event = require(`../events/${dirs}/${file}`);
const event_name = file.split('.')[0];
client.on(event_name, event.bind(null, Discord, client))
}
}
['client', 'guild'].forEach(e => load_dir(e));
}
Basically I just want a block in the messagecreate.js file or event_handler.js file that executes another file in another folder if a message is sent
Create a json file call it whatever and inside of it put your trigger phrase and its response like so, for this example, I'm calling it catchPhrase.json
{
"hi": "hello",
"ping": "pong",
"here": "there",
"nowhere": "everywhere",
"master": "slave",
"mac": "pc"
}
Then in your event listener you only need one if statement
require('dotenv').config();
module.exports = async (Discord, client, message) => {
const prefix = (process.env.PREFIX);
const catchPhrase = require('path/to/catchPhrase.json')
if (catchPhrase[message.content]) return message.reply(`${catchPhrase[message.content]}`)
const args = message.content.slice(prefix.length).split(/ +/);
const cmd = args.shift().toLowerCase();
const command = client.commands.get(cmd) || client.commands.find(a => a.aliases && a.aliases.includes(cmd));
if (command) command.execute(client, message, args, Discord);
}