0

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

insyri
  • 308
  • 2
  • 8
Avtester
  • 41
  • 4
  • Do you know if the first `if` goes through? Could you put a console.log inside to see? This could be a first step towards figuring out what's not working. Also make sure that your Discord settings are correct and that you can receive DMs. – Antoine Gagnon Dec 06 '21 at 00:47
  • I see that it doesnt go through. Any ideas to solve this? Nothing is printing on the console log – Avtester Dec 06 '21 at 00:57
  • Maybe try to `console.log(msg.channel.type)` before the if and see what value you should be checking for in DMs – Antoine Gagnon Dec 06 '21 at 01:03
  • What version of discord.js are you using? – MrMythical Dec 06 '21 at 01:11
  • Im using the latest one. I think its v13? – Avtester Dec 06 '21 at 01:12
  • I used console log - when interacting with the bot on DM - nothing happens. Seems the bot cant recieve DM? Although it is set as admin? – Avtester Dec 06 '21 at 01:13
  • Can the bot receive message events if it's not in a dm, like can it see a message in a guild? – insyri Dec 06 '21 at 01:16
  • It sees a guild text. I set for him a few commands and everything works. The console logs them as well. – Avtester Dec 06 '21 at 01:17
  • @Avtester maybe look at this issue: https://github.com/discordjs/discord.js/issues/5516#issuecomment-985458524 Seems similar. – Antoine Gagnon Dec 06 '21 at 01:19
  • 1
    @antonie ok I got some progress. Using these intents and partials, the console log now shows that the bot recieve dms. But it still cant send messages – Avtester Dec 06 '21 at 01:35

2 Answers2

4

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.

Avtester
  • 41
  • 4
-1

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." });
    }
  }
});
  • Oh, If i want the dm to be triggered without a command? what are my options? just an auto reply to any message – Avtester Dec 06 '21 at 02:14
  • @Avtester, Maybe you can try `message.content === args.join(" ")`. Replace it with the command. – AdventuresX0 Dec 06 '21 at 02:18
  • @Avtester, What do you mean by "return"? Does it not like execute? – AdventuresX0 Dec 06 '21 at 02:25
  • I replaced it with the message content == test, the console said "args is not defined" – Avtester Dec 06 '21 at 02:27
  • @Avtester, Oh wait I forgot you need to define the arguments, add this on top of the message event: `const prefix = "!";`, then add this inside the message event: `if (!message.content.startsWith(prefix) || message.author.bot) return; const args = message.content.slice(prefix.length).trim().split(/ +/); const command = args.shift().toLowerCase();` Make sure that the command is below the defining of the arguments. – AdventuresX0 Dec 06 '21 at 02:37
  • Hmm, this doesn't work either. I think my problem is that i want the bot to reply on user messaging him on dm, and not by command. – Avtester Dec 06 '21 at 02:45