Skip to content

C1appy/Discord.js-v13-Button-Handler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

To get started make a new file in your handler file or wherever you wish this to be.

Paste this code below into said file

const { resolve } = require('path');
const { readdir } = require('fs').promises;
const chalk = require('chalk');
module.exports = (client) => {
  
  async function getFiles(dir) {
    const dirents = await readdir(dir, { withFileTypes: true });
    const files = await Promise.all(dirents.map((dirent) => {
      const res = resolve(dir, dirent.name);
      return dirent.isDirectory() ? getFiles(res) : res;
    }));
    return Array.prototype.concat(...files);
  }
  
  async function run(){
      const files = await getFiles("./Buttons-SelectMenus/Buttons/");
      const jsFiles = files.filter((f) => f.split(`.`).pop() === "js")
      if (jsFiles.length <= 0)
         return console.log(chalk.yellowBright.bold(`[Button-Handler] No loadable Buttons detected`));
          console.log(chalk.blueBright.bold(`[Button-Handler]: Loaded ${jsFiles.length} Buttons`))
      // console.log(chalk.magenta`Loading ${jsFiles.length} Buttons`);
      jsFiles.forEach((fileName, index) => {
          let props = require(fileName);
          // console.log(`[${`Loader`}] ${fileName} Buttons loaded (${index + 1})`);
          client.interactions.set(props.help.custom_id, props);
      });
  }
  run();

  client.on(`interactionCreate`, async (interaction) => {
    if (interaction.isButton()) {
      var cmd = client.interactions.get(interaction.customId);
      if (cmd) cmd.run(client, interaction);
    }
  });
};

After this is done go to your main file (normally index.js) and paste this somewhere

require(./Handlers/Buttons)(client);

Make sure to change ./Handlers/Buttons to the path where you have it

Once done make a new folder in your main directory named Buttons and then make folders/files for each button all you need to do is add your buttons customID into there and the code to be executed once triggered

To use this

module.exports.run = async (client, interaction) => {

            //the code to be executed goes here

}
module.exports.help = {
    values: `your button ID here`,
};

About

Button handler for discord.js v13+

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published