Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

128
Views
Discord bot that replys to a direct message

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

7 months ago · Juan Pablo Isaza
2 answers
Answer question

0

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.

7 months ago · Juan Pablo Isaza Report

0

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." });
    }
  }
});
7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs