0

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.

1 Answer 1

1

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!');
    }
});
Sign up to request clarification or add additional context in comments.

6 Comments

Hello @Eden i understand that, but now it needs a command to execute. i want it to not use a command and work as an event. like when i receive some data from my socket.on function it needs to trigger the !servertest command.
You are dynamically loading and executing the commands. A command must be called within an event, because a command will only be triggered from a message event.
is there an other way to trigger that server.js? besides a message event.. i dont want to put the code in my main.js as its to big.. and it doesnt look clean.. @Eden
You can add an event handler to split up the events into different files. Or you can require the client from index.js and add the event in another file.
I don't understand how you want to trigger server.js. It is a command that is supposed to run when someone sends the command, right? Why would you not want to trigger it with a message event?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.