首页 > 解决方案 > -UnhandledPromiseRejectionWarning: DiscordAPIError: Unknown Message-

问题描述

我对 discord.js 编程很陌生,所以请温柔一点。

这是我的机器人应该负责删除消息的代码。

有时在清除消息时,我会从问题的标题中得到一个错误。

我应该在某处使用 catch 块,如果是,在哪里?

bot.on('message', function(message){
    if(input.startsWith(prefix + 'CLR')){
        message.delete();
            var clrcommand = input.toString().split(' ');
            if(!clrcommand[1]){
                message.channel.send({embed:{
                    title:'<clr ?',
                    description:'Error! Please specify the number of messages to clear e.g. <clr 10',
                    color:0x2471A3
            }})
            return;
        }
        if(sender.id === 'xxxxxxxxxxxxxxxxxx'){
            nmb=Number(clrcommand[1]);
            message.channel.bulkDelete(nmb);
        }
        else{
            message.channel.send({embed:{
                title:'<clr '+clrcommand[1],
                description:'You do not have permissions to request the deletion of messages on this server!',
                color:0x2471A3
            }})
            return;
        }
    }
});

标签: node.jsdiscord.js

解决方案


通过删除message.delete();线,我设法解决了我的问题。感谢@Cannicide 的帮助。

以下是修改后的代码,添加了一些内容。也许它可以帮助某人。干杯。:)

bot.on('message', function(message){
    if(input.startsWith(prefix + 'CLR')){
        var clrcommand = input.toString().split(' ');
        if(!clrcommand[1]){
            message.channel.send({embed:{
                title:'<clr ?',
                description:'Error! Please specify the number of messages to clear e.g. <clr 10',
                color:0x2471A3
            }})
            return;
        }
        if((clrcommand[1] < 1) || (clrcommand[1] > 99)){
            message.channel.send({embed:{
                description:'You need to enter a number between 1 and 99!',
                color:0x2471A3
            }});
        }
        if(sender.id === 'xxxxxxxxxxxxxxxxxx'){
            nmb=Number(clrcommand[1]);
            message.channel.bulkDelete(nmb);
        }
        else{
            message.channel.send({embed:{
                title:'<clr '+clrcommand[1],
                description:'You do not have permissions to request the deletion of messages on this server!',
                color:0x2471A3
            }})
            return;
        }
    }
});

推荐阅读