How can I say my Discord bot doesn't react to direct messages, only in a server chat channel?

client.on('message', function(message) { // COMMAND CLEAR [+]
    if (message.content.startsWith('/dm ') && message.mentions.users.size){
      message.send("ERROR")
    } 
    if (message.content == ".c") {
        if (message.member.hasPermission("MANAGE_MESSAGES")) {
            message.channel.fetchMessages(1000)
               .then(function(list){
                    message.channel.bulkDelete(list);
                }, function(err){message.channel.send()})                 
        }
    }

}); // COMMAND CLEAR [-]

I just want the bot to only react to this command on Discord server channels, and not in direct messages.

728x90

1 Answers How can I say my Discord bot doesn't react to direct messages, only in a server chat channel?

Per the discord.js documentation, the property .channel exists on Message, which is of type TextChannel | DMChannel | GroupDMChannel.

So the best way to check the origin of a message is to check the instanceof the channel property:

client.on("message", message => {
   if (message instanceof TextChannel) {
      // Was posted in a text channel, and not a DM
   }
})

Note: you will need to import TextChannel from the module. Assuming you're using TypeScript (I've only ever used discord.js with typescript) it looks like this:

import { TextChannel } from "discord.js"

1 months ago