How to add buttons to embed pages discord.js

Viewed 1693

I've been trying to add buttons to my slash command to make embed pages. All the info I've been able to find has either just been on buttons (not buttons + slash commands) or needs to be done within the index file and not done with a command handler. I've tried adding an await to the interaction.reply but it sends an error that it needs to be within an async function. I'm not entirely sure how async/await works so I couldn't work much more on that if that would be the proper way to solve my issue. How would I collect the button interaction within a command handled file to edit the embeds on button click? This is the guide I used to create the initial command handler. Here's my current info.js file.

const { SlashCommandBuilder } = require("@discordjs/builders");
const Discord = require("discord.js");
const info = require("./info.json");

module.exports = {
  data: new SlashCommandBuilder()
    .setName("cinfo")
    .setDescription("Shows AniGame card info")
    .addStringOption(option =>
      option
        .setName("name")
        .setDescription("Enter the name of a card")
        .setRequired(false)
    ),
  async execute(interaction) {
    const msg = interaction.options.getString("name").toLowerCase();

    function cinfo(
      name,
      info,
      stats,
      talent,
      image,
      sr1,
      sr2,
      sr3,
      srt,
      ur1,
      ur2,
      ur3,
      urt
    ) {
      const base = new Discord.MessageEmbed()
        .setTitle(name)
        .setColor(0xffd1dc)
        .setDescription(info)
        .addField("__Card Stats__", stats)
        .addField("Talent", talent)
        .setImage(image);

      const sr = new Discord.MessageEmbed()
        .setTitle(name + " | Level 50")
        .setColor(0xffd1dc)
        .addField("__SR Evo 1__", sr1, true)
        .addField("__SR Evo 2__", sr2, true)
        .addField("__SR Evo 3__", sr3, true)
        .addField("Talent", srt)
        .setImage(image);

      const ur = new Discord.MessageEmbed()
        .setTitle(name + " | Level 60")
        .setColor(0xffd1dc)
        .addField("__UR Evo 1__", ur1, true)
        .addField("__UR Evo 2__", ur2, true)
        .addField("__UR Evo 3__", ur3, true)
        .addField("Talent", urt)
        .setImage(image);

      const row = new Discord.MessageActionRow().addComponents(
        new Discord.MessageButton()
          .setCustomId("backward")
          .setStyle("PRIMARY")
          .setEmoji("⬅️"),

        new Discord.MessageButton()
          .setCustomId("forward")
          .setStyle("PRIMARY")
          .setEmoji("➡️"),

        new Discord.MessageButton()
          .setCustomId("delete")
          .setStyle("DANGER")
          .setEmoji("️")
      );

      const pages = [base, sr, ur];

      interaction.reply({ embeds: [base], components: [row] });
      // await interaction.editReply()
        
    }
    
    // location 1
    if (
      msg === "itaru hashida" ||
      msg === "itaru" ||
      msg === "hashida"
    ) {
      cinfo(
        info.Itaru_Hashida[0],
        info.Itaru_Hashida[1],
        info.Itaru_Hashida[2],
        info.Itaru_Hashida[3],
        info.Itaru_Hashida[4],
        info.Itaru_Hashida[5],
        info.Itaru_Hashida[6],
        info.Itaru_Hashida[7],
        info.Itaru_Hashida[8],
        info.Itaru_Hashida[9],
        info.Itaru_Hashida[10],
        info.Itaru_Hashida[11],
        info.Itaru_Hashida[12]
      );
    }
  }
};
2 Answers
Related