Javascript不协调机器人冷却不工作

Javascript不协调机器人冷却不工作,javascript,discord,bots,Javascript,Discord,Bots,除了冷却时间,这个机器人工作得很好。如果有人键入命令,它会用其中一个随机句子进行响应。我想给机器人添加一个用户冷却时间,这样每个用户都必须等待,直到他们可以再次使用它。问题是,机器人的冷却部分是无用的,根本不起作用。有人能帮我吗?非常感谢您的详细回答 const Discord = require('discord.js'); const client = new Discord.Client(); const prefix = '!'; const used = new Map();

除了冷却时间,这个机器人工作得很好。如果有人键入命令,它会用其中一个随机句子进行响应。我想给机器人添加一个用户冷却时间,这样每个用户都必须等待,直到他们可以再次使用它。问题是,机器人的冷却部分是无用的,根本不起作用。有人能帮我吗?非常感谢您的详细回答

const Discord = require('discord.js');

const client = new Discord.Client();

const prefix = '!';

const used = new Map();

const talkedRecently = new Set();

client.once('ready', () => {
    console.log('Ready!');
});


  client.on("message", (message) => {
    if (message.content.startsWith("!random") && talkedRecently.has(msg.author.id)) {
    msg.channel.send("Wait 1 minute before getting typing this again. - " + msg.author);}
    else {
        let tomb=["something1","something2", "something3",];
  
  let i = tomb[Math.floor(Math.random()*tomb.length)];
      message.channel.send(i);
    }
            talkedRecently.add(msg.author.id);
        setTimeout(() => {
          // Removes the user from the set after a minute
          talkedRecently.delete(msg.author.id);
        }, 60000);
    )};
 

client.login(process.env.token);

一些细节:

第一个问题是由于您对
msg
的引用不存在,因此您的第二个条件永远不会通过。 看起来您试图做的是引用参数
message
,这将保存包含所需数据的对象

{
    author: {
        id: 'm5000'
    }
}
试试下面的方法

if (message.content.startsWith("!random") && talkedRecently.has(message.author.id)) {
   ... Your code here
}
第二个问题只是一个“Woops”,将来您可以研究ESLint,以便尽早发现这些问题


您似乎还错误地用
)}关闭了client.on()方法而不是
})

我对您的代码进行了整理,并对其进行了修改,使其更易于扩展。我还修改了,允许冷却自动添加到每个命令中

如果您想让代码变得更好,我强烈建议您通读

完整的解释可以在这个答案的底部找到

示范操场:

示范

代码
const Discord=require('Discord.js');
const client=new Discord.client();
常量前缀='!';
const cooldowns=新的Discord.Collection();
client.once('ready',()=>{
console.log('Ready!');
});
client.on('消息',(消息)=>{
if(message.author.bot | | |!message.content.startsWith(prefix))返回;
const[command,…args]=message.content.slice(prefix.length).split(/\s+/g);
如果(!cooldowns.has(命令)){
set(命令,newdiscord.Collection());
}
const now=Date.now();
const timestamps=cooldowns.get(命令);
常数冷却量=1*60*1000;
if(timestamps.has(message.author.id)){
const expirationTime=timestamps.get(message.author.id)+冷却量;
如果(现在<到期时间){
const timeLeft=(expirationTime-now)/1000;
返回message.reply(`Please wait${timeLeft.toFixed(1)}秒,然后再重新使用\`command}\`command.`);
}
}
timestamps.set(message.author.id,现在);
setTimeout(()=>timestamps.delete(message.author.id),冷却量);
开关(命令){
“随机”案例:
让坟墓=['something1','something2','something3';
设i=thume[Math.floor(Math.random()*thume.length)];
message.channel.send(一);
打破
违约:
message.reply(`command\`${command}\`未被识别。`);
}
});
client.login(process.env.token);
解释 创建一个集合来存储具有冷却时间的命令:

const cooldowns=newdiscord.Collection();
如果消息是由bot发送的,或者消息不是以前缀(
)开头,则消息处理程序将退出:

if(message.author.bot | |!message.content.startsWith(prefix))返回;
将消息拆分为命令和参数数组;这是通过使用
.slice(prefix.length)
删除开头的前缀,然后使用
.split(/\s+/g)
在每个空白处拆分消息来完成的:

const[command,…args]=message.content.slice(prefix.length).split(/\s+/g);
如果请求的命令不在冷却集合中,则添加该命令并将其值设置为新集合:

if(!cooldowns.has(命令)){
set(命令,newdiscord.Collection());
}
以毫秒为单位的当前时间放置在变量中:

const now=Date.now();
请求命令的集合放置在变量中;其中包含在1分钟内使用请求命令的所有用户:

const timestamps=cooldowns.get(命令);
默认冷却时间设置为1分钟:

const冷却量=1*60*1000;
这将检查用户是否是所请求命令集合的一部分;如果是,则计算他们可以再次使用该命令的时间,并检查是否已过过期时间;如果没有,则计算剩余时间,向用户发送消息,消息处理程序退出:

if(timestamps.has(message.author.id)){
const expirationTime=timestamps.get(message.author.id)+冷却量;
如果(现在<到期时间){
const timeLeft=(expirationTime-now)/1000;
返回message.reply(`Please wait${timeLeft.toFixed(1)}秒,然后再重新使用\`command}\`command.`);
}
}
如果用户在1分钟内未使用该命令,则会将其用户ID添加到集合中,并创建计时器,以便在一分钟后自动从集合中删除其用户ID:

timestamps.set(message.author.id,现在);
setTimeout(()=>timestamps.delete(message.author.id),冷却量);
最后,bot检查用户输入的命令并执行某些代码;如果无法识别该命令,将向用户发送一条消息:

开关(命令){
“随机”案例:
让坟墓=['something1','something2','something3';
设i=thume[Math.floor(Math.random()*thume.length)];
message.channel.send(一);
打破
违约:
message.reply(`command\`${command}\`未被识别。`);
}
工具书类 Discord.js()
JavaScript()