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.
Per the discord.js documentation, the property .channel
exists on Message
, which is of type TextChannel | DMChannel | GroupDMChannel
TextChannel | DMChannel | GroupDMChannel
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"
You can tell the bot to ignore a message if its a dm
client.on("message", msg => {
if (msg.type == "dm") {
return;
}
If you want to reply with a message you can do this:
client.on("message", msg => {
if (msg.type == "dm") {
msg.channel.send(`Sorry ${msg.author.username}, you can't do this`)
}
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.