清除来自特定成员 djs V13 的消息

Purge messages from specific member djs V13

您将如何清除来自特定用户的消息,并限制您可以清除的消息数量? 也需要使用斜杠命令

您要清除特定频道或整个服务器的消息吗?

我的第一个想法是获取频道对象并对其调用 awaitMessages 函数。它采用接受消息对象的过滤器回调参数。从那时起,您可以过滤消息,使其所有者与选定的用户相匹配(比方说 !purge 10 @user123)。因此,每条消息都会以所有者必须是@user123 的方式进行过滤。之后,您可以在获取消息对象时调用删除功能。

我将向您展示一种清除特定频道中用户消息的解决方案。

参考:

DiscordJS - Documentation - TextChannel.awaitMessages

DiscordJS - Documentation - Message.delete

// Could throw an exception at channel.awaitMessages or 
// message.delete so make sure to handle it how you prefer.
async function purgeMemberMessages(
  channel, 
  memberId, 
  limit, 
  timeout = 60000
) {

  // Get all messages posted by the given user with a limit
  let messagesCollection = await channel.awaitMessages({
    filter: m => m.author.id === memberId,
    max: limit,
    time: timeout
  });
  
  // Convert the collection to an array
  let messages = Array.from(messagesCollection.values());
  
  // Delete each message
  for (let message of messages) {
    await message.delete();
  }
  
}

// we will assume you have access to the channel object
// let channel = ...
// userId will hold the value which user inputs through a slash command
let userId = '123456789';
// limit will hold the value which user inputs through a slash command as well
let limit = 100;
await purgeMemberMessages(channel, userId, limit);