【问题标题】:Discord.js message user on react for applicationsDiscord.js 消息用户对应用程序做出反应
【发布时间】:2021-08-02 23:49:07
【问题描述】:

我想转换它,以便人们只需要做出反应并将应用程序发送给他们,我不知道如何在反应的东西上发送消息,所以如果有人可以帮助我,将不胜感激。

两个非常乐于助人的人帮助了我

@Skulaurun Mrusal@PerplexingParadox

谢谢! ????

client.on("message", async (message) => {
      // Don't reply to bots
  if (message.author.bot) return;

  if (message.content == "#req") {
         if(!message.member.hasPermission("MANAGE_MESSAGES"))
     {
        message.reply("You do not have permission to do that!");
        return;
     }
    // Perform raw API request and send a message with a button,
    // since it isn't supported natively in discord.js v12
    client.api.channels(message.channel.id).messages.post({
      data: {
        embeds: [reqEmbed],
        components: [
          {
            type: 1,
            components: [
              {
                type: 2,
                style:  4,
                label: "Apply",
                // Our button id, we can use that later to identify,
                // that the user has clicked this specific button
                custom_id: "send_application"
              }
            ]
          }
        ]
      }
    });
  }
});

// Channel id where the application will be sent
const applicationChannelId = "652099170835890177";

// Our questions the bot will ask the user
const questions = [
  "What is your In-Game Username?",
  "How long have you been drifting on FiveM?",
  "Do you use Controller or Keyboard?",
  "How much do you play weekly?",
  "Have you been in any other teams? If so, why did you leave?",
  "Short description about yourself and why you would like to be apart of Blind Spot? (Age, Country)"
];

// Function that will ask a GuildMember a question and returns a reply
async function askQuestion(member, question) {

  const message = await member.send(question);
  const reply = await message.channel.awaitMessages((m) => {
    return m.author.id === member.id;
  }, { time: 5 * 60000, max: 1 });

  return reply.first();

}

client.ws.on("INTERACTION_CREATE", async (interaction) => {

  // If component type is a button
  if (interaction.data.component_type === 2) {

    const guildId = interaction.guild_id;
    const userId = interaction.member.user.id;
    const buttonId = interaction.data.custom_id;
    const member = client.guilds.resolve(guildId).member(userId);

    if (buttonId == "send_application") {

      // Reply to an interaction, so we don't get "This interaction failed" error
      client.api.interactions(interaction.id, interaction.token).callback.post({
        data: {
          type: 4,
          data: {
            content: "I have started the application process in your DM's.",
            flags: 64 // make the message ephemeral
          }
        }
      });

      try {

        // Create our application, we will fill it later
        const application = new MessageEmbed()
          .setTitle("New Application")
          .setDescription(`This application was submitted by ${member.user.tag}`)
          .setColor("#ED4245");

        const cancel = () => member.send("Your application has been canceled.\n**If you would like to start your application again Click on the Apply button in** <#657393981851697153>");
        

        // Ask the user if he wants to continue
        const reply = await askQuestion(member,
          "Please fill in this form so we can proceed with your tryout.\n" +
          "**Type `yes` to continue or type `cancel` to cancel.**"
        );
  
        // If not cancel the process
        if (reply.content.toLowerCase() != "yes") {
          cancel(); return;
        }
  
        // Ask the user questions one by one and add them to application
        for (const question of questions) {
          const reply = await askQuestion(member, question);
          // The user can cancel the process anytime he wants
          if (reply.content.toLowerCase() == "cancel") {
            cancel(); return;
          }
          application.addField(question, reply);
        }
  
           await askQuestion(member,
          "Do you want your application to be submitted?\n" +
          "**Type `yes` to get your Application submitted and reviewed by Staff Members**"
        );
  
        // If not cancel the process
        if (reply.content.toLowerCase() != "yes") {
          cancel(); return;
        }
        // Send the filled application to the application channel
        client.channels.cache.get(applicationChannelId).send(application);

      } catch {
        // If the user took too long to respond an error will be thrown,
        // we can handle that case here.
        member.send(
          "You took too long to respond or Something went wrong, Please contact a Staff member\n" +
          "The process was canceled."
        );
      }

    }

  }

});

【问题讨论】:

标签: javascript discord discord.js


【解决方案1】:

您可以使用ClientmessageReactionAdd 事件。

client.on("messageReactionAdd", (reaction, user) => {

    if (!reaction.emoji.name === "?") return;

    // Check if the message is the right message we want users to react to
    // Obviously you need to enable partials for this to work
    if (reaction.message.id != "...") return;

    const member = reaction.message.guild.member(user);
    member.send("Here's your form!");
    // ... Rest of your code ...

});

请注意,这不适用于在机器人启动之前对发送的消息进行的反应。解决方案是启用Partial Structures(如果您正在处理部分数据,请不要忘记获取。)

或使用Message.createReactionCollector() 创建ReactionCollector

// ... The variable message defined somewhere ...

const collector = message.createReactionCollector((reaction, user) => {
    return reaction.emoji.name === "?";
});

collector.on("collect", (reaction, user) => {
    const member = message.guild.member(user);
    member.send("Here's your form!");
    // ... Rest of your code ...
});

也许在这种情况下使用按钮而不是反应会更好。要使用按钮创建消息,您可以执行原始 API 请求或使用第三方库,如 discord-buttons。以下解决方案适用于 discord.js v12。

client.on("message", async (message) => {

  // Don't reply to bots
  if (message.author.bot) return;

  if (message.content == "#createButton") {
    // Perform raw API request and send a message with a button,
    // since it isn't supported natively in discord.js v12
    client.api.channels(message.channel.id).messages.post({
      data: {
        content: "If you want to apply, click the button below.",
        components: [
          {
            type: 1,
            components: [
              {
                type: 2,
                style: 1,
                label: "Apply",
                // Our button id, we can use that later to identify,
                // that the user has clicked this specific button
                custom_id: "send_application"
              }
            ]
          }
        ]
      }
    });
  }

});

然后我们需要监听一个事件INTERACTION_CREATE,表明用户点击了我们的按钮。 (或其他一些交互触发了该事件,例如斜杠命令。)

// Channel id where the application will be sent
const applicationChannelId = "871527842180132895";

// Our questions the bot will ask the user
const questions = [
  "What is your In-Game Username?",
  "How long have you been drifting on FiveM?",
  "Do you use Controller or Keyboard?",
  "How much do you play weekly?",
  "Have you been in any other teams? If so, why did you leave?",
  "Short description about yourself and why you would like to be apart of Blind Spot? (Age, Country)"
];

// Function that will ask a GuildMember a question and returns a reply
async function askQuestion(member, question) {

  const message = await member.send(question);
  const reply = await message.channel.awaitMessages((m) => {
    return m.author.id === member.id;
  }, { time: 5 * 60000, max: 1 });

  return reply.first();

}

client.ws.on("INTERACTION_CREATE", async (interaction) => {

  // If component type is a button
  if (interaction.data.component_type === 2) {

    const guildId = interaction.guild_id;
    const userId = interaction.member.user.id;
    const buttonId = interaction.data.custom_id;
    const member = client.guilds.resolve(guildId).member(userId);

    if (buttonId == "send_application") {

      // Reply to an interaction, so we don't get "This interaction failed" error
      client.api.interactions(interaction.id, interaction.token).callback.post({
        data: {
          type: 4,
          data: {
            content: "I have started the application process in your DM's.",
            flags: 64 // make the message ephemeral
          }
        }
      });

      try {

        // Create our application, we will fill it later
        const application = new Discord.MessageEmbed()
          .setTitle("New Application")
          .setDescription(`This application was submitted by ${member.user.tag}`)
          .setColor("#ED4245");

        const cancel = () => member.send("Ok, I have cancelled this process.");

        // Ask the user if he wants to continue
        const reply = await askQuestion(member,
          "Please fill in this form so we can proceed with your tryout.\n" +
          "**Type `yes` to continue or type `cancel` to cancel.**"
        );
  
        // If not cancel the process
        if (reply.content.toLowerCase() != "yes") {
          cancel(); return;
        }
  
        // Ask the user questions one by one and add them to application
        for (const question of questions) {
          const reply = await askQuestion(member, question);
          // The user can cancel the process anytime he wants
          if (reply.content.toLowerCase() == "cancel") {
            cancel(); return;
          }
          application.addField(question, reply);
        }
  
        // Send the filled application to the application channel
        client.channels.cache.get(applicationChannelId).send(application);

      } catch {
        // If the user took too long to respond an error will be thrown,
        // we can handle that case here.
        member.send(
          "Something went wrong or you took too long to respond.\n" +
          "The process was cancelled."
        );
      }

    }

  }

});

【讨论】:

  • 我已经更新了我的答案,你可以看看。
  • 说实话,按钮的想法很棒,我什至不知道这是什么东西,感谢您的帮助,我真的很感激
  • 这是一个新功能,甚至在 discord.js v12 的稳定版本中都没有实现。但是 Discord API 支持它。例如,斜杠命令也是如此。
  • 很高兴我提供了帮助,如果我在代码中的某个地方搞砸了,请告诉我!我会更新我的答案并修复它。 (即使我测试过,你也永远不会知道。)
  • 你定义了Discord 吗?或者你只有MessageEmbed?您可以尝试将Discord.MessageEmbed 替换为MessageEmbed。如果这没有帮助,您可以删除 try/catch 并查看错误说明。或者从 catch 块将其记录到控制台。
猜你喜欢
  • 1970-01-01
  • 2018-04-03
  • 2021-06-24
  • 2021-05-14
  • 2021-07-02
  • 2021-03-31
  • 2021-04-02
  • 2020-05-17
  • 2021-05-26
相关资源
最近更新 更多