删除版主的消息命令

Delete message command for moderators

我正在开发 Discord 机器人,最近一直在阅读 API。我想为我的 bot 添加一个命令,它将完全清除文本通道,并且只允许具有管理消息权限的人。

到目前为止,这是我的代码:

client.on('message', function(message) {
    if (message.content == "^clear") {
        try {
            if (message.member.hasPermission("MANAGE_MESSAGES")) {
                messages = message.channel.fetchMessages();
                message.channel.bulkDelete(messages);
            }
        } catch(e) {
            message.channel.send("ERROR: ERROR CLEARING CHANNEL.");
            console.log(e);
        }
    }

});

我收到如下错误:

TypeError: The messages must be an Array, Collection, or number.
at TextChannel.bulkDelete (/Users/person/bot/node_modules/discord.js/src/structures/interfaces/TextBasedChannel.js:379:11)
at Client.<anonymous> (/Users/person/bpt/bot.js:29:33)
at emitOne (events.js:96:13)
at Client.emit (events.js:188:7)
at MessageCreateHandler.handle (/Users/person/bot/node_modules/discord.js/src/client/websocket/packets/handlers/MessageCreate.js:9:34)
at WebSocketPacketManager.handle (/Users/person/bot/node_modules/discord.js/src/client/websocket/packets/WebSocketPacketManager.js:102:65)
at WebSocketConnection.onPacket (/Users/person/bot/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:325:35)
at WebSocketConnection.onMessage (/Users/person/bot/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:288:17)
at WebSocket.onMessage (/Users/person/bot/node_modules/discord.js/node_modules/ws/lib/EventTarget.js:103:16)
at emitTwo (events.js:106:13)

首先,您不能批量删除超过 14 天的消息,因此如果您尝试清除包含超过 14 天消息的频道,批量删除将不起作用,您必须一次删除一个(while/for 循环),我假设您在文档中知道这一点。其次,message.channel.fetchMessages() returns a Promise 意味着它在您访问它时尚未准备好。您必须使用 async/await 或仅使用 .then。示例:

client.on('message', function(message) {
    if (message.content == "^clear") {
        if (message.member.hasPermission("MANAGE_MESSAGES")) {
            message.channel.fetchMessages()
               .then(function(list){
                    message.channel.bulkDelete(list);
                }, function(err){message.channel.send("ERROR: ERROR CLEARING CHANNEL.")})                        
        }
    }

});

这是非常简单的代码,如果您想要 message.channel.bulkDelete(输入您的自定义号码),因为这只会删除 5 条消息。但请记住,最大值为 100。

/////////////////Clear Chat//////////////////////////////////

client.on("message", message => {
  if(message.content === "/clearchat 5") {
    message.channel.bulkDelete(5)
    .then(messages => console.log(`Bulk deleted ${messages.size} messages`))
    .catch(console.error);
 message.channel.send("Chat cleared");
  }
})
///Make ur own trigger and edit the response/////////////

这是留言。而不是味精。

if(message.content.startsWith(prefix + "clear")){
    const amount = message.content.split(" ")[1];
    if(!amount)
    {
        message.reply(`<amount>`);
        return;
    }
     if(!message.member.hasPermission("MANAGE_MESSAGES"))
     {
        message.channel.send('You have no permissions to do that');
        return;
     }
    message.channel.bulkDelete(amount)
    //.then(messages => message.channel.send(`${messages.size} Messages deleted`))
.then(messages => message.channel.send(`Messages deleted by ${message.author.username}`))
.catch(console.error);
}