Rohan Kumar
Rohan Kumar

Reputation: 67

Discord Bot is not taking answer from DM

I have been working on a bot which asks a question in DM but at the moment it's asking all the question in DM and taking answers from a channel. I want it to take the answer from DM.

const Discord = require('discord.js');
const client = new Discord.Client();
const prefix = '-';
const guildID = '';
const token = '';

//Ready Event
client.on('ready', () => {
  console.log('Application bot ready!')
});

//Message Event
client.on('message', async message => {
  //args
  const args = message.content.slice(prefix.length).trim().split(/ +/g);
  const command = args.shift().toLowerCase();

  //commands
  if (command === 'apply') {
    //Has to be in DMs
    if (message.channel.type != 'dm') {
      message.channel.send('Check Your DM');
      message.author.send('Application started!');

      //First Question
      await message.author.send('How old are you?');
      let answer = await message.channel.awaitMessages(answer => answer.author.id != client.user.id, {
        max: 1
      });
      const age = (answer.map(answers => answers.content).join());

      //Second Question
      await message.author.send('Whats your name?');
      answer = await message.channel.awaitMessages(answer => answer.author.id != client.user.id, {
        max: 1
      });
      const name = (answer.map(answers => answers.content).join());

      //Third Question
      await message.author.send('Where do you live?');
      answer = await message.channel.awaitMessages(answer => answer.author.id != client.user.id, {
        max: 1
      });
      const location = (answer.map(answers => answers.content).join());

      //Embed
      const embed = new Discord.MessageEmbed()
        .setAuthor(message.author.tag, message.author.avatarURL)
        .addField('Age', age)
        .addField('Name', name)
        .addField('Location', location)
        .setTimestamp()
        .setColor('RED');

      //Sending Embed
      const guild = client.guilds.cache.get(guildID);
      await guild.channels.cache.find(channel => channel.name === 'general').send(embed);

    }

  }
});

//Log In
client.login(token);

I tried changing message.channel.type to dm but then I don't receive questions in dm

Upvotes: 1

Views: 646

Answers (1)

Federico Grandi
Federico Grandi

Reputation: 6806

I think you should separate the part where you check the channel from the one where you send the questions.

The first thing you want to do is check whether the message comes from a DM: if that's the case then you're all set, if not you have to get the DM channel in order to receive the answers. You can use something like this:

if (message.channel.type != 'dm')
  message.channel.send('Check your DMs!')
let appChannel = (await message.author.send('Application started.')).channel

This way you have a variable called appChannel that stores the channel where you will ask questions and receive answers (which is the user DM channel). You can run the rest of the code using just that, here's an example with the first question:

await appChannel.send('How old are you?');
let answer = await appChannel.awaitMessages(answer => answer.author.id != client.user.id, { max: 1 });
const age = (answer.map(answers => answers.content).join());

Upvotes: 1

Related Questions