【问题标题】:Discord.js bot isn't clearing messages, but still sends the x amount of messages have been deleted messageDiscord.js 机器人没有清除消息,但仍然发送 x 条消息已被删除消息
【发布时间】:2021-03-06 13:04:33
【问题描述】:

我试图从我以前在同一服务器上使用不同命令名称创建的机器人复制这段代码,现在它没有删除消息,我不知道为什么不这样做。

bot.on('message', msg =>{
  if(!msg.author.bot && msg.content.startsWith(PREFIX))
  {
    let args = msg.content.substring(PREFIX.length).split(" ");

    switch(args[0])

    {
      case 'ClearMessages':
        if (msg.member.roles.cache.some(r=>["RNGesus"].includes(r.name)))
        {
          msg.channel.bulkDelete(args[1], true).catch(err=> {
            console.error(err)
            msg.channel.send("An error has occured while trying to delete messages")
          });
          msg.channel.send(`${args[1]} messages have been deleted`)
          .then(msg =>{
            msg.delete({timeout: 3000})
          })
          .catch(console.error)
        }
      break;

当机器人应该删除消息时,它也不会在控制台中提供任何错误消息。如果有人可以提供帮助,我将不胜感激,我对 discord.js 还很陌生,我的一位朋友帮助我做到了这一点。我试图向他们寻求帮助,但他们也想不通。

【问题讨论】:

    标签: discord.js


    【解决方案1】:

    TextChannel.bulkDelete() 的第一个参数应该是消息的数组/集合或要删除的消息数。显然,您试图提供一些要删除的消息,但实际上您提供的是一个 string

    您需要先使用unary plus operator (+)args[0] 转换为一个数字,然后使用Math.floor() 将其四舍五入为整数(或者如果您想更花哨和更高效,可以使用双精度bitwise NOT operator (~))。作为建议,您还应该检查以确保该号码有效。

    if (msg.member.roles.cache.some((r) => ['RNGesus'].includes(r.name))) {
     if (+args[1] < 2 || +args[1] > 100)
      return message.channel.send('The number must be at least 2 and at most 100');
     msg.channel.bulkDelete(Math.floor(+args[1]), true).catch((err) => {
      console.error(err);
      msg.channel.send('An error has occured while trying to delete messages');
     });
     msg.channel
      .send(`${args[1]} messages have been deleted`)
      .then((msg) => {
       msg.delete({ timeout: 3000 });
      })
      .catch(console.error);
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-13
      • 1970-01-01
      • 2020-10-26
      • 2021-07-23
      • 2018-04-29
      • 1970-01-01
      • 2018-11-25
      • 2019-03-01
      • 2020-05-17
      相关资源
      最近更新 更多