删除版主的消息命令

时间:2017-08-04 21:05:33

标签: node.js macos bots discord discord.js

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

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

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)

3 个答案:

答案 0 :(得分:4)

对于初学者,您无法批量删除超过14天的邮件,因此如果您尝试清除包含超过14天邮件的频道,则无法批量删除,您必须将其删除一次一个(而/ for循环),我假设你知道这是在文档上。其次,message.channel.fetchMessages()会返回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.")})                        
        }
    }

});

答案 1 :(得分:1)

这是留言。而不是为味精。

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);
}

答案 2 :(得分:0)

这是非常简单的代码,如果您要在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/////////////