【问题标题】:how to dynamically loading and executing the commands, without message event (discord bot)如何在没有消息事件的情况下动态加载和执行命令(不和谐机器人)
【发布时间】:2021-07-14 19:00:27
【问题描述】:

我希望它启动我的功能, 那么现在会发生什么,它执行该功能,但它会无限次执行垃圾邮件...... data.ref 中的数据是一个数字,当它与数字匹配时,它会执行函数,但它只是偶尔..

我能做些什么来防止这种情况发生?超时功能不起作用..它仍然不断发送垃圾邮件

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

}

servertest.js 文件

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

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

MY CODE }

//更新

通常我会在 main.js 中添加(server.js 代码),因为我想保持代码干净,我正在使用 module.exports。

我完全理解如何处理事件.. 但是如何在不使用消息事件的情况下运行我的脚本,我想在接收到值后直接触发特定的 servertest.js .. 现在它只有在我使用!servertest 时才有效。 目标是让机器人在通道中通知我,根据它通过 socket.on 连接获得的值的变化,从我的 ANPR 和安全系统发送特定结果。

【问题讨论】:

    标签: node.js discord.js


    【解决方案1】:

    它会向您的servertest 命令发送垃圾邮件,因为我假设您在servertest 中发送消息。由于每次发送消息时都会执行servertest,因此会导致servertest 调用自身的永远循环。

    请使用 discord.js 指南 herehere 中显示的命令处理程序

    index.js(或您的入口文件)中,您应该加载所有命令:

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

    监听消息,解析消息,动态执行命令

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

    【讨论】:

    • 你好@Eden 我明白了,但现在它需要一个命令来执行。我希望它不使用命令并作为事件工作。就像当我从我的 socket.on 函数接收到一些数据时,它需要触发 !servertest 命令。
    • 您正在动态加载和执行命令。必须在事件中调用命令,因为命令只会从消息事件中触发。
    • 还有其他方法可以触发 server.js 吗?除了消息事件..我不想把代码放在我的 main.js 中,因为它太大了..而且它看起来不干净..@Eden
    • 您可以添加一个事件处理程序来将事件拆分到不同的文件中。或者您可以要求来自index.js 的客户端并将事件添加到另一个文件中。
    • 我不明白你想如何触发 server.js。这是一个应该在有人发送命令时运行的命令,对吧?为什么你不想用消息事件触发它?
    猜你喜欢
    • 2020-10-03
    • 2021-12-15
    • 2021-08-30
    • 2017-04-29
    • 2019-05-11
    • 2018-08-22
    • 1970-01-01
    • 2018-12-15
    • 2020-05-18
    相关资源
    最近更新 更多