在DM中删除Bot生成的消息

时间:2020-07-26 19:01:30

标签: javascript node.js discord.js

不久前,我从discord.js开始,当时正在开发命令处理程序-通常是在处理命令。 我意识到我的机器人也可以在DM上回复,因此我尝试了以下方法:

if (!msg.guild) {

msg.channel.send("I am not answering to to DMs, please respect that!");

}

我莫名其妙地错误地回答了我10条消息,如果我没有停止自动程序的话,它还会继续。

现在,我决定我甚至不希望机器人回复dm,它应该忽略它,但是现在我收到了他发送的10/20条消息,我无法摆脱。 谁能给我发送一行代码,以便我可以手动删除一条消息?我想复制他写给我的每封邮件的ID,这不是问题。

2 个答案:

答案 0 :(得分:0)

const User = client.users.cache.get("UserId") // Your Discord ID.
const MessageId = "ID"; // The ID of the message you want to delete.

User.dmChannel.messages.fetch(MessageId).then(m => m.delete()) // Fecthing the message and then deleting it.

答案 1 :(得分:0)

这可能很长,但是我注意到了这个问题,并且我的机器人中已经有一个功能可以将其DM删除给用户...。

deleteDMs(用户,resultsChannel){

this.#client.users.fetch( user.id )
.then( u => {

    u.createDM()
    .then( dmchannel => { 
        dmchannel.messages.fetch( { limit: 100 } )
        .then( messages => {

            let c = messages.size;
            messages = messages.filter( m => { return m.author.id === this.#client.user.id } );

            if( c !== messages.size ){

                resultsChannel.send( `No can do - ${u.toString()} must delete DMs to ${this.toString()}` +
                `  before ${this.toString()} can delete my DMs to ${u.toString()}. (Discord API quirk.)` );

            }else{

                c = messages.size;
                resultsChannel.send( `Deleting ${c} DMs.` );

                messages.forEach( msg => {

                    msg.delete()
                    .then( () => {

                        c --;
                        if( c === 0 ) resultsChannel.send( 'DMs deleted successfully.');

                    } ).catch( err => { resultsChannel.send( `Error occurred while deleting DMs.\n${err}` ) } );

                } );//end messages.forEach

            }//end if( c !===

        } ).catch( err => { resultsChannel.send( `Error occurred while fetching DMs.\n${err}` ) } );

    } ).catch( err => { resultsChannel.send( `Error occurred while resolving DM channel.\n${err}` ) } );

} ).catch( err => { resultsChannel.send( `Error occurred while fetching user.\n${err}` ) } );

} //结束deleteDMs