2017-12-27 781 views
2

目前我有這樣的:命令超時| Discord.js

const Discord = require("discord.js"); 
const PREFIX = ","; 
const token = "my token"; 
var bot = new Discord.Client(); 
bot.on('ready',() => { 
    bot.on('message', message => { 
     if (!message.content.startsWith(PREFIX)) return; //if not command ignore message 

     var args = message.content.substring(PREFIX.length).split(" "); //splits commands so each word = pos in array 

     switch (args[0].toLowerCase()) { //not case-sensitive anymore 

      case "hello": 
       message.channel.send("hello"); 
       break; 

      //rest of the commands 

我想限制命令「你好」的用法。我希望每次用戶輸入「hello」時都會有10秒的超時時間。如果用戶在冷卻前輸入命令,它會發送一條消息,說明誰最後使用了該命令,以及冷卻時間還剩多長時間。

這就是我想要的結果看起來像:

User1:   ,hello 
Bot:    hello 

(After 1 second) 

User2:   ,hello 
Bot:   User1 has already used this command, please wait another 9 seconds to use it again 

(After 9 seconds) 

User 2:   ,hello 
Bot:   hello 

所有幫助表示讚賞。 謝謝,

回答

1

您需要存儲該命令的最後日期,然後相應地分流。爲了顯示最後一次使用該命令的人員,您需要將該信息與時間戳一起存儲。

下面是一個例子基於你:

const Discord = require("discord.js"); 
const PREFIX = ","; 
const token = "my token"; 
const bot = new Discord.Client(); 

let lastHelloCommandDate, lastHelloCommandUser; 

bot.on('ready',() => { 
    bot.on('message', message => { 
     if (!message.content.startsWith(PREFIX)) return; //if not command ignore message 

     var args = message.content.substring(PREFIX.length).split(" "); //splits commands so each word = pos in array 

     switch (args[0].toLowerCase()) { //not case-sensitive anymore 

      case "hello": 
       hello(message); 
       break; 

      //rest of the commands 
    }}}) 
}) 

function hello(message) { 
    const now = new Date(); 
    if (now - lastHelloCommandDate > 10 * 60 * 1000) { 
    // It's been more than 10 mins 
    message.channel.send("hello"); 
    lastHelloCommandDate = now; 
    lastHelloCommandUser = message.sender; 
    } else { 
    // It's been less than 10 mins 
    // send a direct message to the user 
    // i don't know if message.sender exists, check the api 
    message.sender.send(`Command last used by ${lastHelloCommandUser}`); 
    } 

} 

將該樣品再加工,使得命令存儲在一個單獨的對象和動態檢查。這消除了對switch語句的需要。

const Discord = require("discord.js"); 
const PREFIX = ","; 
const token = "my token"; 
const bot = new Discord.Client(); 

let lastHelloCommandDate, lastHelloCommandUser; 

bot.on('ready',() => { 
    bot.on('message', message => { 
     if (!message.content.startsWith(PREFIX)) return; //if not command ignore message 

     var args = message.content.substring(PREFIX.length).split(" "); //splits commands so each word = pos in array 
     const command = args[0].toLowerCase(); 

     if (!commands[command]) { 
      throw new Error(`Unknown command supplied: ${command}`); 
     } 
     commands[command](message); 
    }}}) 
}) 

const commands = { 
    hello: message => { 
    const now = new Date(); 
    if (now - lastHelloCommandDate > 10 * 60 * 1000) { 
     // It's been more than 10 mins 
     message.channel.send("hello"); 
     lastHelloCommandDate = now; 
     lastHelloCommandUser = message.sender; 
    } else { 
     // It's been less than 10 mins 
     // send a direct message to the user 
     // i don't know if message.sender exists, check the api 
     message.sender.send(`Command last used by ${lastHelloCommandUser}`); 
    } 
    } 
}; 
+0

是的,'lastHelloCommandDate'應該初始化爲'0'。如果它是'undefined'(如上所述),則結果爲'new Date() - undefined',這會導致'NaN'(不會超過任何數字)。 –