Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

104
Vistas
How can I create a bot which replies something to random messages. im using an event handler

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

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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);
}

Demo

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda