2017-12-27 10 views
2

Zur Zeit habe ich dies:Befehlszeitlimit | 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 

ich die Verwendung des Befehls möchte „hallo“ begrenzen. Ich möchte, dass es für jedes Mal, wenn ein Benutzer ", hallo" eingibt, eine Zeitüberschreitung von 10 Sekunden gibt. Und wenn ein Benutzer vor dieser Abklingzeit den Befehl eingibt, wird eine Nachricht gesendet, die sagt, wer den Befehl zuletzt benutzt hat und wie lange er noch in der Abklingzeit bleibt. Diese

ist, was ich will das Ergebnis aussehen:

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 

Alle Hilfe ist willkommen. Danke,

Antwort

1

Sie müssen das letzte Datum speichern, an dem der Befehl verwendet wurde, und dann den Datenfluss entsprechend verzweigen. Um auch anzuzeigen, wer zuletzt den Befehl verwendet hat, müssen Sie diese Informationen mit dem Zeitstempel speichern.

Hier ist ein Beispiel, basierend auf Ihrer Unterstützung:

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

} 

Diese Probe so überarbeitet wird, dass Befehle in einem einzelnen Objekt gespeichert und dynamisch geprüft. Dies macht eine switch-Anweisung überflüssig.

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

Ja, 'lastHelloCommandDate' sollte auf '0' initialisiert werden. Wenn es "undefiniert" ist (wie oben beschrieben), ist das Ergebnis "new Date() - undefined", was zu "NaN" führt (was nicht größer als eine Zahl ist). –