Discord Bot没有接受DM的答复

时间:2020-05-17 03:34:20

标签: javascript discord discord.js

我一直在研究一个机器人,该机器人在DM中问一个问题,但现在它正在问DM中的所有问题并从渠道中获取答案。我希望它从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);

我尝试将message.channel.type更改为dm,但是我没有在dm中收到问题

1 个答案:

答案 0 :(得分:1)

我认为您应该将检查频道的部分与发送问题的部分分开。

您要做的第一件事是检查消息是否来自DM:如果是这种情况,那么您就已经准备好了;否则,您必须获得DM频道才能接收答案。您可以使用以下内容:

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

这样,您就有了一个名为appChannel的变量,该变量存储了您要提问和接收答案的通道(这是用户DM通道)。您可以仅使用该代码来运行其余代码,这是第一个问题的示例:

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());