【问题标题】:Command Timeout | Discord.js命令超时 |不和谐.js
【发布时间】:2018-06-08 09:31:00
【问题描述】:

目前我有这个:

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

感谢所有帮助。 谢谢,

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案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}`);
        }
      }
    };
    

    【讨论】:

    • 是的,lastHelloCommandDate 应该初始化为0。如果是undefined(如上所述),则结果为new Date() - undefined,结果为NaN(不大于任何数字)。
    猜你喜欢
    • 2021-06-30
    • 2021-07-17
    • 2020-11-22
    • 1970-01-01
    • 2021-04-06
    • 2020-08-26
    • 2020-11-23
    • 2021-05-08
    • 1970-01-01
    相关资源
    最近更新 更多