【问题标题】:Private message commands in discord不和谐的私人消息命令
【发布时间】:2018-12-15 00:19:54
【问题描述】:

我正在使用 javascript 为 discord 机器人创建 RP 配置文件。我的对话从一个频道开始,然后转到与机器人的私人消息传递。第一个问题被问到,用户的回答存储在数据库中。这工作正常。

当我尝试在与机器人一起使用私人消息中的另一个命令移动到 RP 配置文件创建的下一步时,似乎出现了问题。它似乎没有注册正在使用的命令。甚至可以在与机器人的私人消息中使用命令吗?

我使用了与第一个问题相同的代码,改变了需要的内容,但没有任何内容应该破坏代码。它只是看起来甚至没有看到第二个命令,它存储在一个单独的命令文件中。我该怎么做?

module.exports.run = async (bot, message, args) => {
message.author.send(` SECOND QUESTION, **What is the age of your Brawler or Character?**`)
  .then((newmsg) => { //Now newmsg is the message you send to the bot
    newmsg.channel.awaitMessages(response => response.content, {
      max: 1,
      time: 300000,
      errors: ['time'],
    }).then((collected) => {
      newmsg.channel.send(`Your brawler's age is: **${collected.first().content}**

      If you are okay with this age, type !profilegender to continue the profile creation process!

      If you would like to edit your age, please type !profileage`)
        con.query(`UPDATE profile SET age = '${collected.first().content}' WHERE id = ${message.author.id}`);
        console.log("1 record updated!")
    }).catch(() => {
      newmsg.channel.send('Please submit an age for your character. To restart Profile creation, please type "!profilecreate" command in Profile Creation channel on the server.');
    });
  });
}

提前感谢您的宝贵时间!

编辑:这是机器人/客户端正在侦听消息的代码的一部分。

bot.on(`message`, async message => {
  if(message.author.bot) return;
  if(message.channel.type === "dm") return;

  con.query(`SELECT * FROM profile WHERE id = '${message.author.id}'`, (err, rows) => {
    if(err) throw err;

    var sql;

    if(rows.length < 1) {
      var sql = (`INSERT INTO profile (id, username) VALUES (${message.author.id}, '${message.author.tag}')`);
    } else {
      var sql = (`UPDATE profile SET username = '${message.author.tag}' WHERE id = ${message.author.id}`);
    };

    //con.query(sql, console.log);
    //if (err) throw err;
    //console.log("1 record inserted!");
  });

【问题讨论】:

  • 您确定在您的client.on("message") 内部没有任何东西可以阻止机器人识别命令(可能是检查前缀或特定频道或类似的东西。 ..)。你能发布你的代码的那部分吗?无论如何,通过 DM 使用命令是没有问题的,但是如果您的代码仅依赖于 TextChannels(而不是 DMChannels)可能会导致一些问题(通常很容易修复)
  • 这可能是问题所在。我什至没有考虑到这一点。我用那部分代码更新了 OP。它确实提到了带有返回的通道类型,因此当涉及到命令时,它似乎不会在消息中等待 DM 中的任何内容。我不确定如何解决这个问题。只需删除提及“dm”作为消息类型的行?
  • 是的,如果频道是 DM,该行告诉机器人退出该功能。请记住,如果您删除它,机器人将允许通过 DM 执行每个命令。如果这对你没问题,你可以删除它。如果您只想通过 DM 使用某些命令,您可能需要添加一个 if 检查,如果通道是 DM && 则返回该命令是不允许的
  • 我指定它需要在某个频道中用于我的其他几个命令,所以这应该不是问题。私人消息命令将非常少,并且所有其他命令将指定给服务器中的某些通道,因此根据我的需要指定它应该不会很麻烦。非常感谢。如果您想发布这些 cmets 的要点作为答案,以便我将其标记为已回答,那就太好了。
  • 完美!我刚刚添加了一个答案

标签: javascript command discord.js private-messaging


【解决方案1】:

cmets 的回答

在您的client.on("message") 内部有一个 if 检查,如果通道是 DMChannel,则退出该函数

if(message.channel.type === "dm") return;

为避免这种情况,只需删除此行:这样,机器人将执行命令而不管通道类型如何。如果您只想在某些通道中允许某些命令,您可以在client.on("message") 或命令本身的函数中执行此操作。

【讨论】:

    猜你喜欢
    • 2018-11-19
    • 2020-10-03
    • 2020-07-28
    • 2021-03-09
    • 1970-01-01
    • 2021-06-30
    • 2018-11-10
    • 2020-11-23
    • 2019-05-19
    相关资源
    最近更新 更多