简体   繁体   中英

discord.js How to delete messages that contain blacklisted word in all channels

I am trying to make command that deletes messages from all channels that contain blacklisted word. I don't want command to activate itself but for example when I write.delete in bot commands channel it deletes messages containing banned word from all channels. With my code command does not delete messages. Here is my current code for this command:

module.exports = {
    name:'delete',
    description: 'deletes all messages containing blacklisted word',
    async execute(message, args){
        const delete = await message.channel.fetch();
        if(message.content.includes('bad-word')){
            message.delete();
        }
    }
}

Any help would be appreciated

Currently, your code is fetching the channel in which you sent the command (and the result is never used), and then it checks if your message, that triggered the command, contains "bad-word". It's not what you want. You should use the TextChannel's messages attribute. It will give you the channel's MessageManager . You can then use a loop to iterate though the messages and apply your check on each one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM