I'm using discord.js and I'm trying to make if a user sends a direct message to a bot, it will respond a simple reply such as "I'm sorry, You can not DM me. Please use the ticket system instead."
I tried using:
client.on('message', msg => {
if (msg.channel.type == "dm") {
msg.author.send(" blah blah example text");
return;
}
});
This yields no results.
I'm totally new to coding and I would appreciate the help.
Thanks
OK, now I solved this for good.
I was missing:
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "DIRECT_MESSAGES", "DIRECT_MESSAGE_REACTIONS", "DIRECT_MESSAGE_TYPING"], partials: ['CHANNEL',] })
(used this guide: https://github.com/discordjs/discord.js/issues/5516#issuecomment-985458524)
For the reply:
if (message.channel.type === "DM") {
return message.reply({ content: "TEXT" });
}
I should've used CAPITAL DM, Jesus Christ. Now it works like a charm.
Thanks everybody.
Your using the message event, so it needs a command to function.
if (message.content === "test")
, your command is in the String.
if (message.channel.type === "dm")
, and this detects what channel type is the user in for using the command.
return message.reply("")
, then it will send an error message in the user's dm because the command cannot be executed in dm's.
else
, if it doesn't detect a dm channel then it will ignore the error response.
client.on("message", message => {
if (message.content === "test") {
if (message.channel.type === "dm") {
return message.reply({ content: "This message is a dm for the user." });
} else {
return message.reply({ content: "This message is a message sent to channel." });
}
}
});