Discord.js 消息用户对应用程序做出反应

时间:2021-08-01 20:12:35

标签: javascript discord discord.js

我想转换它,以便人们只需要做出反应并将应用程序发送给他们,我不知道如何在反应内容上发送消息,因此如果有人能帮助我,将不胜感激。< /p>

两个非常有帮助的人帮助了我

@Skulaurun Mrusal@PerplexingParadox

谢谢! ?

enter image description here

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."
        );
      }

    }

  }

});

1 个答案:

答案 0 :(得分: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(如果您正在处理部分数据,请不要忘记获取。)

或者使用 ReactionCollector 创建一个 Message.createReactionCollector()

// ... 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"
              }
            ]
          }
        ]
      }
    });
  }

});

enter image description here

然后我们需要监听一个事件 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."
        );
      }

    }

  }

});
相关问题