【问题标题】:Application Bot Discord.js应用程序机器人 Discord.js
【发布时间】:2020-11-13 07:51:34
【问题描述】:

因此,此代码向用户发送 DM,向用户询问一些问题,然后我的目标是将应用程序发送回特定频道,不知道该怎么做,任何帮助我知道您必须使用频道 ID,但不确定如何编译所有答案,然后将它们发送回特定频道?


  let userApplications = {}

  bot.on("message", function(message) {
    if (message.author.equals(bot.user)) return;

    let authorId = message.author.id;

    if (message.content === "%apply") {
        console.log(`Apply begin for authorId ${authorId}`);
        // User is not already in a registration process
        if (!(authorId in userApplications)) {
            userApplications[authorId] = { "step" : 1}

            message.author.send("```We need to ask some questions so  we can know a litte bit about yourself```");
            message.author.send("```Application Started - Type '#Cancel' to cancel the application```");
            message.author.send("```Question 1: In-Game Name?```");
        }

    } else {

        if (message.channel.type === "dm" && authorId in userApplications) {
            let authorApplication = userApplications[authorId];

            if (authorApplication.step == 1 ) {
                authorApplication.answer1 = message.content;
                message.author.send("```Question 2: Age?```");
                authorApplication.step ++;
            }
            else if (authorApplication.step == 2) {
                authorApplication.answer2 = message.content;
                message.author.send("```Question 3: Timezone? NA, AU, EU, NZ, or Other? (If other, describe your timezone)```");
                authorApplication.step ++;
            }
            else if (authorApplication.step == 3) {
                authorApplication.answer3 = message.content;
                message.author.send("```Question 4: Do you have schematica?```");
                authorApplication.step ++;
            }

            else if (authorApplication.step == 4) {
                authorApplication.answer4 = message.content;
                message.author.send("```Thanks for your registration. Type %apply to register again```");
                //before deleting, you can send the answers to a specific channel by ID
                bot.channels.cache.get("616852008837709844")
                  .send(`${message.author.tag}\n${authorApplication.answer1}\n${authorApplication.answer2}\n${authorApplication.answer3}\n${authorApplication.answer4});
                delete userApplications[authorId];
            }
        }
    }
  });


}


module.exports.help = {
    name: "app"
}```

【问题讨论】:

  • 你想把答案发回用户最初写%apply的频道吗?还是转到特定频道供您观看?

标签: node.js discord.js


【解决方案1】:

您有一个良好的开端,因为您将所有用户的状态保存在 userApplications 对象中。因为您想保留用户的答案,所以您可以简单地使用此状态对象 - 将用户的答案塞入其中,并在需要时使用它们。

例如:

bot.on("message", function(message) {
  if (message.author.equals(bot.user)) return;

  let authorId = message.author.id;

  if (message.content === "%apply") {
      console.log(`Apply begin for authorId ${authorId}`);
      // User is not already in a registration process    
      if (!(authorId in userApplications)) {
          userApplications[authorId] = { "step" : 1}

          message.author.send("```We need to ask some questions so  we can know a litte bit about yourself```");
          message.author.send("```Application Started - Type '#Cancel' to cancel the application```");
          message.author.send("```Question 1: In-Game Name?```");
      }

  } else {

      if (message.channel.type === "dm" && authorId in userApplications) {
          let authorApplication = userApplications[authorId];

          if (authorApplication.step == 1 ) {
              authorApplication.answer1 = message.content;
              message.author.send("```Question 2: Age?```");
              authorApplication.step ++;
          }
          else if (authorApplication.step == 2) {
              authorApplication.answer2 = message.content;
              message.author.send("```Question 3: Timezone? NA, AU, EU, NZ, or Other? (If other, describe your timezone)```");
              authorApplication.step ++;
          }
          else if (authorApplication.step == 3) {
              authorApplication.answer3 = message.content;
              message.author.send("```Question 4: Do you have schematica?```");
              authorApplication.step ++;
          }

          else if (authorApplication.step == 4) {
              authorApplication.answer4 = message.content;
              message.author.send("```Thanks for your registration. Type %apply to register again```");
              //before deleting, you can send the answers to a specific channel by ID
              bot.channels.cache.get("CHANNEL_ID_HERE")
                .send(`${message.author.tag}\n${authorApplication.answer1}\n${authorApplication.answer2}\n${authorApplication.answer3}\n${authorApplication.answer4}`);
              delete userApplications[authorId];
          }
      }
  }
});

您可以对此进行许多简单、直接的改进 - 例如,将答案存储在数组中,而不是单独的属性中。但是在不知道你的经验水平的情况下,我不会介绍你可能不容易理解的代码。

【讨论】:

  • 试图运行你的代码,但它似乎没有工作
  • @JamesLewis 你有什么错误吗?您是否将 CHANNEL_ID_HERE 替换为所需的频道 ID?
  • 是的错误hastebin.com/unosixisad.js 这是我的全部代码。不知道为什么它不起作用。
  • @JamesLewis 我的网络阻止了该站点。您可以将错误编辑到问题中或将它们作为评论添加到此处吗?
  • @JamesLewis 抱歉,我刚刚发现代码中缺少一个 `。我想它至少导致了你的一个错误。在末尾的 .send 行上,模板文字没有结束 `。试试修改后的代码。
【解决方案2】:

  let userApplications = {}

  bot.on("message", function(message) {
    if (message.author.equals(bot.user)) return;

    let authorId = message.author.id;

    if (message.content === "%apply") {
        console.log(`Apply begin for authorId ${authorId}`);
        // User is not already in a registration process
        if (!(authorId in userApplications)) {
            userApplications[authorId] = { "step" : 1}

            message.author.send("```We need to ask some questions so  we can know a litte bit about yourself```");
            message.author.send("```Application Started - Type '#Cancel' to cancel the application```");
            message.author.send("```Question 1: In-Game Name?```");
        }

    } else {

        if (message.channel.type === "dm" && authorId in userApplications) {
            let authorApplication = userApplications[authorId];

            if (authorApplication.step == 1 ) {
                authorApplication.answer1 = message.content;
                message.author.send("```Question 2: Age?```");
                authorApplication.step ++;
            }
            else if (authorApplication.step == 2) {
                authorApplication.answer2 = message.content;
                message.author.send("```Question 3: Timezone? NA, AU, EU, NZ, or Other? (If other, describe your timezone)```");
                authorApplication.step ++;
            }
            else if (authorApplication.step == 3) {
                authorApplication.answer3 = message.content;
                message.author.send("```Question 4: Do you have schematica?```");
                authorApplication.step ++;
            }

            else if (authorApplication.step == 4) {
                authorApplication.answer4 = message.content;
                message.author.send("```Thanks for your registration. Type %apply to register again```");
                //before deleting, you can send the answers to a specific channel by ID
                bot.channels.cache.get("616852008837709844")
                  .send(`${message.author.tag}\n${authorApplication.answer1}\n${authorApplication.answer2}\n${authorApplication.answer3}\n${authorApplication.answer4});
                delete userApplications[authorId];
            }
        }
    }
  });


}


module.exports.help = {
    name: "app"
}```

【讨论】:

  • 这就是我的确切代码,我遇到了错误!
【解决方案3】:

我正在创建这个 discord.js 应用程序机器人,但我想为应用程序添加更多命令。例如,我想要一个命令 !apply1 和命令 !apply2。当我复制代码、再次粘贴、更改命令名称并键入命令时,它会发送两三个问题而不是一个。我应该改变什么?

如果您不明白并想查看我在说什么,请加入这个虚假的不和谐服务器:https://discord.gg/fVsQaa6(它每次发送 2 个问题,我想 1 接 1 发送。另外,它会将答案发送到他们两个的频道相同,我检查了频道ID是否正确。)

【讨论】:

    猜你喜欢
    • 2020-12-03
    • 2021-07-23
    • 2020-08-09
    • 2021-01-06
    • 2021-07-01
    • 2018-01-29
    • 2020-08-21
    • 2022-01-07
    • 2020-10-03
    相关资源
    最近更新 更多