Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为tmi.js integrated discord.js添加全局计时器_Javascript_Node.js_Discord.js_Twitch - Fatal编程技术网

Javascript 为tmi.js integrated discord.js添加全局计时器

Javascript 为tmi.js integrated discord.js添加全局计时器,javascript,node.js,discord.js,twitch,Javascript,Node.js,Discord.js,Twitch,我正在尝试做一个discord机器人,它会监听多个twitch聊天命令,然后使用tmi.js和discord.js在discord上运行它们。目前它正在工作,但我似乎无法为命令本身添加全局冷却以防止垃圾邮件。起初,我尝试在每个命令中添加一个cd计时器,但无法使其工作,因此决定尝试制作一个全局cd,但仍然无效。我做错什么了吗 twitch.on('message', (channel, tags, message, self) => { if(!message.startsWith(

我正在尝试做一个discord机器人,它会监听多个twitch聊天命令,然后使用tmi.js和discord.js在discord上运行它们。目前它正在工作,但我似乎无法为命令本身添加全局冷却以防止垃圾邮件。起初,我尝试在每个命令中添加一个cd计时器,但无法使其工作,因此决定尝试制作一个全局cd,但仍然无效。我做错什么了吗

twitch.on('message', (channel, tags, message, self) => {
    if(!message.startsWith(prefix) || self) return;
    const args = (message.slice(prefix.length).trim().split(/ +/));
    const commandName = args.shift().toLowerCase();
    
    if (!twitch.commands.has(commandName)) return;
    const command = twitch.commands.get(commandName);
}
    try {
        command.execute(bot, botChannel, vcChannel, isReady);
    } catch (error){
        console.error(error);
    }
        
});

为了更新,我基本上在这里从async Wait函数中获取了一份小册子: 然后,我对代码进行了如下修改:

const setAsyncTimeout = (cb, timeout = 0) => new Promise(resolve => {
    setTimeout(() => {
        cb();
        resolve();
    }, timeout);
});
const doStuffAsync = async () => {
    await setAsyncTimeout(() => {
        isReady = true;
        console.log(isReady);
    }, 10000);};

twitch.on('message', (channel, tags, message, self) => {
    if(!message.startsWith(prefix) || self) return;
    const args = (message.slice(prefix.length).trim().split(/ +/));
    const commandName = args.shift().toLowerCase();
    
    if (!twitch.commands.has(commandName)) return;
    if (isReady){
        const command = twitch.commands.get(commandName);
        isReady = false;
        try {
            command.execute(bot, botChannel, vcChannel);
        } catch (error){
            console.error(error);
        }
        doStuffAsync();
    }
});

现在似乎可以工作了,因为10秒对于机器人来说是一个足够长的时间,它可以在不引起超时的情况下正确地离开discord。我仍然愿意接受更好的优化建议

你可以实现一个限制每X毫秒调用次数的方法。我曾尝试在try-catch区域实现上述throttle函数,但它没有起作用。我错过什么了吗?