【问题标题】:The command that im trying to use is not working. im not so familiar with making a discord bot but i understand it, but I can't solve this problem我试图使用的命令不起作用。我对制作不和谐机器人不太熟悉,但我理解,但我无法解决这个问题
【发布时间】:2020-09-09 01:25:37
【问题描述】:

谁能告诉我为什么该命令不起作用?当我编写前缀和命令时,它可以工作,但它会显示以下消息:

You did not specify a correct amount of time!

应该是这样,但如果我在它旁边写下任何其他数字以便我可以设置慢速模式,它就不会做任何事情。如果有人可以分析代码并告诉我问题,将不胜感激。是的,有一个令牌,但我删除了它,以免泄露。

const commandFiles = fs
  .readdirSync("./commands/")
  .filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
  const command = require(`./commands/${file}`);

  client.commands.set(command.name, command);
}

client.once("ready", () => {
  console.log("ChatSlowMode is Online!");
});

client.on("message", (message) => {
  if (message.content.startsWith(prefix) || message.author.bot) return;

  const args = message.content.slice(prefix.length).split();
  const command = args.shift().toLowerCase();

  if (command === "slowmode") {
    if (!message.member.hasPermission("MANAGE_CHANNELS"))
      return message.channel.send("You don't have access to this command!");
    if (!args[0])
      return message.channel.send(
        "You did not specify a correct amount of time!"
      );
    if (isNaN(args[0])) return message.channel.send("That is not a number!");
    if (args[0] > 21600)
      return message.channel.send(
        "Invalid Number! Number must be below 21600."
      );
    message.channel.setRateLimitPerUser(args[0]);
    message.channel.send(`Slowmode Set to **${args[0]}**`);
  }
});

client.login("");

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    首先,您不能在某些数字之外设置慢速模式。它们就像您在服务器的频道设置中进行设置一样。此部分如下所示:

    现在,如您所见,您只能将其设置为5s10s15s30s1m2m5m10m、@987654334 @、30m1h2h6h。当您将其设置为未指定的数量时,它将引发此错误:

    You did not specify a correct amount of time!
    

    您要做的是,检查 args 中的数字,并检查它是否数字。试试下面的代码:

    // Disable this if you're already using the command below.
    // This can cause `command` to be defined twice and return a constant value error.
    
    // const commandFiles = fs
    //     .readdirSync('./commands/')
    //     .filter(file => file.endsWith('.js'));
    // for(const file of commandFiles){
    //     const command = require(`./commands/${file}`);
    //     client.commands.set(command.name, command);
    // }
    
    client.on('ready', () => {
     // `client.on()` should be better
     console.log('ChatSlowMode is Online!');
    });
    
    client.on('message', (message) => {
     // Check if message doesn't starts with `prefix` instead, or it will stop your code.
     if (!message.content.startsWith(prefix) || message.author.bot) return;
    
     const command = message.content
      .slice(prefix.length)
      .toLowerCase()
      .split(' ')[0]
      .toLowerCase();
    
     const args = message.content
      .slice(prefix.length)
      .split(' ')
      .slice(1);
    
     if (command === 'slowmode') {
      if (!message.member.hasPermission('MANAGE_CHANNELS'))
       return message.channel.send("You don't have access to this command!");
    
      // Checks if `args[0]` doesn't exist or isn't a number.
      if (!args[0])
       return message.channel.send('You did not specify a correct amount of time!');
      if (isNaN(args[0])) return message.channel.send('That is not a number!');
    
      // Check if `args[0]` is a correct amount of time to set slowmode
      // Array `validNumbers` are an array consistent of numbers of time you can set slowmode with.
      const validNumbers = [
       5,
       10,
       15,
       30,
       60,
       120,
       300,
       600,
       900,
       1800,
       3600,
       7200,
       21600,
      ];
    
      // Check if `args[0]` parsed into an interger is included in `validNumbers`
      if (!validNumbers.includes(parseInt(args[0])))
       return message.channel.send('Invalid Number! Number must be below 21600.');
    
      // Set the slowmode
      message.channel.setRateLimitPerUser(args[0]);
    
      // Send the reply
      message.channel.send(`Slowmode Set to **${args[0]}**`);
     }
    });
    
    client.login('');
    

    如果您仍然不明白,请参阅这些参考资料:

    【讨论】:

    • 那么,例如,我不能将慢速模式设置为 35 分钟?
    • 不,你不能,因为 discord 不允许这样做。
    • 我在 validNumbers 中添加了 14 并尝试了它,它成功了。知道如何
    • 这应该是不可能的,你检查一下slowmode是否真的设置为14秒?不应该。
    • 是的,我已经设置好了,所以我可以设置为小于 21600 的任何数字
    猜你喜欢
    • 1970-01-01
    • 2021-08-11
    • 2020-12-06
    • 2021-02-25
    • 2023-03-20
    • 2022-11-03
    • 2019-12-29
    • 2020-10-17
    • 1970-01-01
    相关资源
    最近更新 更多