【问题标题】:Job Inquiry Bot for Discord.jsDiscord.js 的工作查询机器人
【发布时间】:2020-09-02 23:24:51
【问题描述】:

我正在尝试创建一个机器人,用户可以通过 DM 开始“工作”查询。 示例:

用户使用 !inquiry 向机器人发送消息,然后机器人询问有关工作的问题,例如它是个人项目还是公司,那么公司或个人项目是什么 twitter,然后它会询问什么类型的服务他们通过提供选项来请求,并且基于该选项,机器人将回复“请解释您对新 xxx 工作的想法”

然后,一旦用户回答了所有这些问题,机器人就会发送一个包含他们回答的嵌入内容。

我正在考虑使用 MessageCollector,但我不知道如何执行逻辑。我让机器人响应用户,所以我了解如何通过 DM 向用户发送消息。只是不太明白如何实现其余的。我需要一点推动力。

client.on("message", async (msg) => {
  if (!msg.content.startsWith(prefix) || msg.author.bot) return;

if (msg.channel.type === "dm") {
 const args = msg.content.slice(prefix.length).split(/ +/);
 const command = args.shift().toLowerCase();
 const discordUser = msg.author.tag;

if (command !== "order") {
  try {
    sendFailMessage(msg, "wrongCommand");
  } catch (e) {
    console.warn("Failed sending Fail Message");
  }
} else {
  msg.channel.startTyping(1);
  msg.author.send();

【问题讨论】:

  • 欢迎来到 Stack Overflow!到目前为止你有什么代码?
  • 我添加了我的代码。所以在此之后的其余部分我只是重新开始,因为它开始变得混乱

标签: javascript discord.js


【解决方案1】:

我之前做过类似的事情,所以如果你想要完整的代码和上下文:https://github.com/karizma/ravenxi/blob/master/commands/showcase/apply.js

但这里是根据您的上下文重写的: (注意它的定义没有优化,因为 idk 你的上下文代码,并且你为每次调用传递变量,但如果你知道 js 如果你要使用库,你应该知道,改进代码应该不会太难)

function sendNexQuestion(index, channel, questions, replys) {
    return channel.send(questions[index])
        .then(() => channel.awaitMessages(() => true, { max: 1, time: 30000, errors: ["time"] }))
        .then(reply => {
            const content = reply.first().content;
            if (content === prefix + "cancel") throw "Self-Cancel";
            if (content === prefix + "redo") {
                replys.length = 0;
                return sendNextQuestion(0, channel, questions, replys);
            }

            replys.push(content);
            return index >= questions.length - 1 ? new Promise(res => res(replys)) : sendNextQuestion(index + 1, channel, questions, replys);
        }).catch(err => {
            if (err === "Self-Cancel") {
                //user canceled
            }

            channel.send("Application Canceled");
        });
}

client.on("message", async (msg) => {
    if (!msg.content.startsWith(prefix) || msg.author.bot) return;
    if (msg.channel.type === "dm") {
        const args = msg.content.slice(prefix.length).split(/ +/);
        const command = args.shift().toLowerCase();
        const questions = ["Why do you want this?", "Question 2"];

        if (command === "inquiry") {
            sendNexQuestion(0, msg.channel, questions, []).then(replys => {
                //array of replys
            })
        }
    }
});

【讨论】:

  • 回答第一个问题后,我的申请被取消。我该如何解决这个问题?
  • 抱歉,我忘记回复了,您还有这个问题吗?
猜你喜欢
  • 2021-09-18
  • 2022-07-09
  • 2021-07-05
  • 2020-07-02
  • 2021-07-30
  • 2020-02-02
  • 2021-07-08
  • 2022-01-26
  • 2021-07-01
相关资源
最近更新 更多