jmessore
jmessore

Reputation: 3

How do I make a discord bot check if it is being PMed and then respond to said PM with discord.js?

How do I get my bot to check if the user is DMing the bot, and then for the bot to respond to said message?

The code that I have played with so far, parts work fine such as "sendMessage and console.log and bot.channels.get etc, but it's getting the correct statement to run that specific section is the issue, the code:

// "Help" command for admin assistance
bot.on('message', (message) => {
if(message.channel.DMChannel) {
     // Check if the word sent is "help"
     if(message.content.toLowerCase() == 'help') {
        console.log('User ' + member.user.username + ' is requesting assitance. Now alerting staff members!');
        bot.channels.get("397707437781680130").send('**' + member.user.username + '**, is requesting staff assitance. Now alerting staff members!')
        bot.sendMessage('I helped you! A staff member will respond soon!');
     } else {
         bot.sendMessage('You can only ask for help by DM, please type "help" if you need assistance!');
     }
    }
});    

I'd appreciate pointers.

Upvotes: 0

Views: 3470

Answers (1)

Raymond Zhang
Raymond Zhang

Reputation: 730

Discord.js has a built in channel.type which you can use to check for a DM channel (also called PM Channel).

With this in mind, your code should look something like:

bot.on('message', (message) => {
   if(message.channel.type == "dm") {
      //what should happen on a dm
   } else {
      //what should happen if the channel is not a dm
   }
});    

Upvotes: 3

Related Questions