简体   繁体   English

我的 Discord.JS 代码有一个奇怪的错误,你能帮我解决这个问题吗?

[英]My code for Discord.JS is having a strange error, can you please help me solve this?

For some reason when I type the purge command with a number more than one it gives the error 'DiscordAPIError: Unknown Message' (look at image for full error), can you please help solve this?出于某种原因,当我键入带有多个数字的清除命令时,它会给出错误“DiscordAPIError:未知消息”(查看图像以获取完整错误),您能帮忙解决这个问题吗? I suspect because the functions have already been declared so a second time around it gets confused.我怀疑是因为这些函数已经被声明了,所以第二次会感到困惑。

Image of error:错误图片:

在此处输入图像描述

By the way here is my code for the command:顺便说一下,这是我的命令代码:

 if (msg.content.startsWith("$purge")) {
        if (msg.content.match(/\d+/)) {
            if (isNaN(Number(msg.content.split(' ')[1])) == false) {
                channel = msg.channel
                msg.delete()
                for (f = 1; f <= Number(msg.content.split(' ')[1]); f++) {
                    msg.channel.messages.fetch().then(e => {
                        e.every(function (value) {
                            if (value.deleted == false) {
                                message = value;
                            }
                        })
                        message.delete()
                    })
                }
            }
            else {
                if (isNaN(Number(msg.content.split(' ')[1])) == true) {
                    msg.channel.send("Please include an amount of messages to purge in the right place")
                }
                
            }
        }
        else {
            if (!msg.content.match(/\d+/)) {
                msg.channel.send("Please include an amount of messages to purge")
            }
            
        }
        
        
    }

Edit: I realised what was happening, still don't know how to fix it withut using bulkDelete()编辑:我意识到发生了什么,仍然不知道如何在不使用 bulkDelete() 的情况下修复它

The problem is coming from this part:问题来自这部分:

msg.channel.messages.fetch().then((e) => {
  e.every(function (value) {
    if (value.deleted == false) {
      message = value;
    }
  });
  message.delete();
});

messages.fetch() returns a promise and when it's resolved a collection of messages (you named it e ). messages.fetch()返回一个 promise ,当它解析消息集合时(您将其命名为e )。 You try to check if every message has a deleted property and if it doesn't, you just assign the message to a (global?) message variable.您尝试检查每条消息是否都有已deleted的属性,如果没有,您只需将消息分配给(全局?) message变量。 And as you're working inside a for loop in an asynchronous way, the message you want to delete could be already deleted.当您以异步方式在 for 循环中工作时,您要删除的消息可能已经被删除。

There is a bulkDelete() method in discord.js though which you could use for your purge command. discord.js 中有一个bulkDelete()方法,您可以将其用于清除命令。 You just need to provide the number of messages you want to delete in the channel and it does the rest:您只需提供要在频道中删除的消息数量,它会执行 rest:

channel.bulkDelete(5)
  .then(messages => console.log(`Bulk deleted ${messages.size} messages`))
  .catch(console.error);

You could simplify your code like this:您可以像这样简化代码:

const prefix = '$';
// remove the prefix and split by spaces
const args = msg.content.slice(prefix.length).split(/ +/);
// remove the command from the args array
const command = args.shift().toLowerCase();

if (command === 'purge') {
  if (isNaN(args[0])) {
    return msg.channel.send(
      'Please include an amount of messages to purge in the right place',
    );
  }
  // add an extra to delete the last message with the purge command too
  const amount = Number(args[0]) > 100 ? 101 : Number(args[0]) + 1;

  msg.channel.bulkDelete(amount, true)
  .then((_message) => {
    console.log(`Bot cleared ${_message.size}messages`);
  });
}

channel.bulkDelete(5).then(messages => console.log( Bulk deleted ${messages.size} messages )).catch(console.error); channel.bulkDelete(5).then(messages => console.log( Bulk deleted ${messages.size} messages )).catch(console.error);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM