最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

命令超时

IT培训 admin 6浏览 0评论

命令超时

目前我有这个:

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

我想限制命令“,你好”的用法。我希望每次用户输入“,你好”时都会有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

所有帮助表示赞赏。谢谢,

回答如下:

您需要存储使用该命令的最后日期,然后相应地分叉流。要显示上次使用该命令的用户,您还需要使用时间戳存储该信息。

以下是基于您的示例:

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

命令超时

目前我有这个:

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

我想限制命令“,你好”的用法。我希望每次用户输入“,你好”时都会有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

所有帮助表示赞赏。谢谢,

回答如下:

您需要存储使用该命令的最后日期,然后相应地分叉流。要显示上次使用该命令的用户,您还需要使用时间戳存储该信息。

以下是基于您的示例:

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)

  1. 暂无评论