Tranq
Tranq

Reputation: 47

how to dynamically loading and executing the commands, without message event (discord bot)

i want that it start my function, So what happens now it executes the function but it spams the execution unlimited times... The data from data.ref is a number and when it matches the number it executes the function, but its only now and then..

what can i do to prevent this? a timeout function doesnt work.. it still keeps spamming

for (var i in config.Extra) {
        if (myData.ref == config.Extra[i].ref) {
        
            var Alert1Type = config.Extra[i].Alert1; // type of the device show
            var Alert1Name = config.Extra[i].name; // Custom name show
            
        

        
            console.log('test');
            test() //starts the function
         
            
        }
}

function test(){

    client.on('message', message => {
    client.commands.get('servertest').run(client, message); 
    return 
})

}

the servertest.js file

module.exports.help = {
        name: "servertest"
}

module.exports.run = async (client, message, args) => {

MY CODE }

//update

Normally i would add (server.js code) in my main.js, as i want to keep the code clean i am using module.exports.

i totally understand how to handle a event.. but how to let run my script without using a message event, i want to direct trigger that specific servertest.js after it receives a value.. right now it only works if i use !servertest.. the goal is to let the bot notify me in a channel to send specific results from my ANPR and security system, based on changes from a value it gets trough socket.on connection.

Upvotes: 0

Views: 951

Answers (1)

Eden
Eden

Reputation: 48

It spams your servertest command because I assume you are sending messages in the servertest. Since servertest gets executed every time a message gets sent, it results in a forever loop of servertest calling itself.

Please use the command handler shown in the discord.js guide here and here

In index.js (or your entry file), you should load all commands:

const fs = require('fs');
const Discord = require('discord.js');
const { prefix, token } = require('./config.json');

const client = new Discord.Client();
client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}

and listen to messages, parse them, and dynamically execute the command

client.on('message', message => {
    // return if the command was sent from a bot to prevent it from replying to itself
    if (!message.content.startsWith(prefix) || message.author.bot) return;
    
    // parsing the command
    const args = message.content.slice(prefix.length).trim().split(/ +/);
    const command = args.shift().toLowerCase();

    if (!client.commands.has(command)) return;

    try {
        // Find the command matching the name and execute it
        client.commands.get(command).execute(message, args);
    } catch (error) {
        console.error(error);
        message.reply('there was an error trying to execute that command!');
    }
});

Upvotes: 1

Related Questions