Socket
Socket
Sign inDemoInstall

djs-builder

Package Overview
Dependencies
241
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    djs-builder

Discord.js bot builder. Supports Ts and Js.


Version published
Maintainers
1
Created

Readme

Source

Discord Bot Utilities

Welcome to the Discord Bot Utilities package! This package provides a set of utility classes/interfaces to simplify common tasks when developing Discord bots using the Discord.js library.

Table of Contents

  1. ButtonManager
  2. MenuManager
  3. PermissionChecker
  4. Starter
  5. Options

Starter

Description

Starter provides functionality for initializing and starting a Discord bot.

Usage

const { Starter } = require('djs-builder'); // cjs module .js

const { Client, GatewayIntentBits, Partials } = require('discord.js');

const intentsArray = Object.keys(GatewayIntentBits)
    .map((a) => GatewayIntentBits[a]);
const intents = intentsArray.reduce((acc, curr) => acc | curr, 0);

const client = new Client({
    intents,
    partials: Object.values(Partials),
});

// OR
import { Starter } from 'djs-builder'; // Ejs module .mjs or Ts

import { Client, GatewayIntentBits, Partials, PermissionFlagsBits } from 'discord.js';

const intentsArray = Object.keys(GatewayIntentBits)
    .map((a) => GatewayIntentBits[a as keyof typeof GatewayIntentBits]);
const intents = intentsArray.reduce((acc, curr) => acc | curr, 0);

const client = new Client({
    intents,
    partials: Object.values(Partials) as Partials[],
});

// Define starter options
const starterOptions = {
    bot: {
        token: 'YOUR_BOT_TOKEN', // [OPTIONAL] Discord bot token
        logs: {
            devLogs: {
                enable: true, // Required: Enable developer logs
                pathToWatch: '/path/to/watch', // Required: Path to watch for file changes
                webhookURL: 'https://your.webhook.url', // Required: Webhook URL for logging
                mention: '<@123456789012345678>' // [OPTIONAL] User ID to mention in logs
            },
            terminal: true // [OPTIONAL] Log messages to terminal
        },
        name: 'Your Bot Name', // [OPTIONAL] Bot name
        avatar: 'https://your.bot/avatar.png', // [OPTIONAL] Bot avatar URL or local path image
        banner: 'https://your.bot/banner.png', // [OPTIONAL] Bot banner URL or local path image
        BotInfo: {
            perms: ['SendMessages', 'BotMessages'], // [OPTIONAL] Bot permissions to work in any server
            serverId: '123456789012345678', // [OPTIONAL] Discord server ID
            botInvite: 'https://discord.com/invite/your-bot-invite', // [OPTIONAL] Bot invite URL
            serverInvite: 'https://discord.com/invite/your-server-invite', // [OPTIONAL] Server invite URL
            ownerId: '123456789012345678', // [OPTIONAL] Bot owner's user ID
            partners: ['partner1', 'partner2'] // [OPTIONAL] Bot partners
        },
        Status: {
            state: 'online', // Required: Bot presence state ['online', 'offline', 'dnd', 'idle', 'Streaming']
            activities: ['Game 1', 'Game 2'], // [OPTIONAL] Bot activities
            type: 0, // [OPTIONAL] Bot activity type
            delay: 60000 // [OPTIONAL] Activity rotation delay in milliseconds
        },
        Database: {
            mongo: {
                mongoURI: 'mongodb://localhost:27017', // Required: MongoDB connection URI
                dbName: 'your_database_name' // [OPTIONAL] MongoDB database name
            },
            verse: {
                adapterType: 'json', // Required: Database adapter type ['json', 'yaml', 'sql']
                path: '/path/to/json/file', // Required: Path to database file
                dev: {
                    enable: true, // Required: Enable development mode
                    logsPath: '/path/to/logs' // [OPTIONAL] Path to development logs
                },
                secure: { enable: false, secret:'your_encryption_key'} // [OPTIONAL] To secure Data File by encrypting it with secret key
            }
        }
    },
    slash: {
        path: './path/to/slash_commands', // Required: Path to slash commands
        global: true, // [OPTIONAL] Register slash commands globally
        serverId: '123456789012345678', // [OPTIONAL] Discord server ID
        logsId: '123456789012345678' // [OPTIONAL] Logs channel ID
    },
    prefix: {
        path: './path/to/prefixes', // Required: Path to prefix settings
        prefix: '!', // Required: Default bot prefix
        global: true, // [OPTIONAL] Use global prefix
        serverIds: ['123456789012345678'], // [OPTIONAL] Discord server IDs
        logsId: '123456789012345678' // [OPTIONAL] Logs channel ID
    },
    events: {
        path: './path/to/events', // Required: Path to event handlers
        recursive: true, // [OPTIONAL] Enable recursive event loading
        eventBlacklist: ['path/to the event/file.js', 'path/to the event/file.js'] // [OPTIONAL] Blacklisted events
    },
    anticrash: {
        enable: true, // Required: Enable anti-crash feature
        webhookURL: 'https://your.crash.webhook.url', // Required: Webhook URL for crash alerts
        mention: '<@123456789012345678>' // [OPTIONAL] User ID to mention in crash alerts
    }
};

// Define the starter instance
const bot = new Starter();

async function botStart() {
const botstarted = await bot.start(client, starterOptions);
    const mongodb = await botstarted.mongodb;
    const versedb = await botstarted.versedb;
    const slashSize = await botstarted.slashSize;
    const prefixSize = await botstarted.prefixSize;
    const eventSize = await botstarted.eventSize;
    return {
        getDb: mongodb,
        db: versedb,
        slashSize: slashSize,
        prefixSize: prefixSize,
        eventSize: eventSize
    }
}
module.exports = { botStart };
// Or
export { botStart };
  • Usage for the returned values from botStart() funtion:
const { botStart } = require('path/to/file/where botStart is exported from');
// Or
import { botStart } from 'path/to/file/where botStart is exported from';

async function test() {
    const client = await botStart();

    // Usage for mongoDb
    const db = await client.getDb;
    await db.collection('collectionName'); // See more usage at https://www.mongodb.com

    // Usage for verseDb
    const db = await client.db;
    await db.load('collectionName'); // See more usage at https://versedb.jedi-studio.com

    // Usage for slashSize/prefixSize/eventSize
    console.log(`loaded slash Commands: ${client.slashSize}`);
    console.log(`loaded prefix Commands: ${client.prefixSize}`);
    console.log(`loaded events: ${client.eventSize}`);
}

test()

ButtonManager

Description

ButtonManager is a utility class for managing the creation of Discord buttons.

Usage
const { ButtonManager } = require('djs-builder'); // Cjs module .js
// OR
import { ButtonManager } from 'djs-builder'; // Ejs module .mjs or Ts

// Define button data
const buttonsData = [
  {
    customId: 'button1',
    style: 'Primary',// style: Primary, Secondary, Link, Danger, Success
    label: 'Primary Button',
    emoji: '😃',  // Emoji for the button
    disabled: false,  // Whether the button is disabled
  },
  {
    customId: 'button2',
    style: 'Secondary',
    label: 'Secondary Button',
    emoji: '🚀',
    disabled: true,
  },
  {
    style: 'Link',
    label: 'Link Button',
    url: 'https://example.com',  // URL for link-style button
    emoji: '🔗',
  },
];

// Create an instance of ButtonManager
const actionRow = new ButtonManager(buttonsData);

// Buttons Row add it into components
const row = actionRow.ButtonBuild();

// Exmaple 
const message = await interaction.channel.send({ content: 'Here are some buttons:', components: [row] });
// OR
const message = await message.channel.send({ content: 'Here are some buttons:', components: [row] });

MenuManager

Description

MenuManager facilitates the creation of select menus (dropdown menus) in Discord.

Usage

const { MenuManager } = require('djs-builder'); // Cjs module .js
// OR
import { MenuManager } from 'djs-builder'; // Ejs module .mjs or ts

// Define select menu options
const selectMenuOptions = [
  { label: 'Option 1', value: 'option1', description: 'Description for Option 1', emoji: '🌟', default: true },
  { label: 'Option 2', value: 'option2', description: 'Description for Option 2', emoji: '🚀' },
  { label: 'Option 3', value: 'option3', description: 'Description for Option 3', emoji: '🔗' },
];

// Create an instance of SelectMenuManager
const selectMenuManager = new MenuManager(
  selectMenuOptions,
  'customSelectMenuId',  // Custom ID for the select menu
  'Select an option',    // Placeholder text for the select menu
  1,                      // Minimum number of selected values
  2,                       // Maximum number of selected values
  false                   // Disabled state for meny (true or false)
);

// Create a select menu with the specified options
const selectMenuRow = selectMenuManager.createSelectMenu();

// Define a message with the select menu
 message.reply({
  content: 'Please choose an option:',
  components: [selectMenuRow],
});

// Define a interaction with the select menu
 interaction.reply({
  content: 'Please choose an option:',
  components: [selectMenuRow],
});

PermissionChecker

Description:

PermissionChecker provides functionality for checking user permissions in a Discord guild.

Usage:

const { PermissionChecker } =  require('djs-builder'); // Cjs module .js
// OR
import { PermissionChecker } from 'djs-builder'; // Ejs module .mjs or ts

const perms = new PermissionChecker();

// Interaction case
const userId = interaction.user.id;
const guild = interaction.guild;
// Message Case
const guild = message.guild;
const memberId = message.author.id;

// Usage:

const permToCheck = ['ManageGuild', 'BanMembers']
perms.checker(userId, guild, permToCheck)

Options:

  • Cooldown in slash/prefix commands.
  • Owner, Usage, Description, and Category for prefix commands
  • Events has intializer, retryAttempts, execute once, execute for specific times, timeout, and name

Contributions

Contributions are welcome! If you have any suggestions, bug reports, or feature requests, feel free to contact us on discord.

Discord Banner

Keywords

FAQs

Last updated on 15 May 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc