Discord.js deleteMessage() 不起作用

Discord.js deleteMessage() doesn't work

我正在使用 discord.js 创建一个 Discord 机器人,我想创建一个可以清除消息的命令。现在,我有这段代码(只有有趣的部分),但我不知道为什么它不起作用:

// Importing discord.js, creating bot and setting the prefix 
const Discord = require('discord.js');
const bot = new Discord.Client();
const prefix = "/";

// Array that stores all messages sent
messages = [];

bot.on('message', (message) => {

   // Store the new message in the messages array
   messages.push(message);

   // Split the command so that "/clear all" becames args["clear", "all"]
   var args = message.content.substring(prefix.length).split(" ");

   // If the command is "/clear all"
   if(args[0] == "clear" && args[1] == "all") {

      bot.deleteMessages(messages); // Code that doesn't work

      // Resets the array
      messages = [];

   }
}

// CONNECT !!!
bot.login('LOGING TOKEN HERE');

你能帮帮我吗?

我看到两个问题:

  1. messages数组总是空的;没有向数组添加项的代码,因此对 bot.deleteMessages 的调用将始终得到一个空数组;
  2. deleteMessages 似乎不是 Discord.Client 上可用的方法;

根据文档,我想你想要的是sweepMessages。该状态的描述:

Sweeps all text-based channels' messages and removes the ones older than the max message lifetime. If the message has been edited, the time of the edit is used rather than the time of the original message.

尝试将代码更改为调用 bot.sweepMessages(1);,我认为这会告诉客户端清除所有早于一秒的消息。

不使用 sweepMessages 的另一种方法是使用 fetchMessages:

let user = message.mentions.users.first();
let amount = !!parseInt(message.content.split(' ')[1]) ? parseInt(message.content.split(' ')[1]) : parseInt(message.content.split(' ')[2])
var prefix = '!'


if (message.content.startsWith(prefix + 'clear') && !amount) 
    return message.reply('Must specify an amount to clear!');
if (message.content.startsWith(prefix + 'clear') && !amount && !user) return message.reply('Must specify a user and amount, or just an amount, of messages to clear!');
    message.channel.fetchMessages({
        limit: amount,
    }).then((messages) => {
    if (user) {
        const filterBy = user ? user.id : bot.user.id;
        messages = messages.filter(m => m.author.id === filterBy).array().slice(0, amount);
    }
    message.channel.bulkDelete(messages).catch(error => console.log(error.stack));
});

这将允许用户在发送时使用命令 !clear [#] 删除该数量的消息。如果它是 运行 就像 !clear 你可以设置删除多少,没有指定的数量。

discord.js Documentation - TextChannel#fetchMessages

可以交换

bot.deleteMessages()

至:

messages.forEach(x => x.delete())

您应该改用 <TextChannel>.bulkDelete

示例:

msg.channel.bulkDelete(100).then(() => {
    msg.channel.send("Purged 100 messages.").then(m => m.delete(3000));
});

这会在每次调用此方法时删除频道中的 2 - 100 条消息,这样您就不会经常收到 429 (Too many Requests) Error,这可能会导致您的 token being revoked.

不是这样的。你应该先获取消息然后使用 bulkDelete 删除它们 , 这是一个简单的例子

// Normal Javascript
<Message>.channel.fetchMessages()
.then(messages => {
// Here you can use bulkDelete(101) to delete 100 messages instead of using fetchMessages and deleting only 50
<Message>.channel.bulkDelete(messages);
});
// ES6
let messages = await <Message>.channel.fetchMessages();
// Here you can use bulkDelete(101) to delete 100 messages instead of using fetchMessages and deleting only 50
await <Message>.channel.bulkDelete(messages);