npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-djs-handler

v5.0.0

Published

A module that makes your life easier when creating your bot by cleaning up your code!

Downloads

38

Readme

⚓ Simple Discord.js Handler

⚓ Tutorial

❓ Getting Started

To get started, you need to install NodeJs (recommended version) to ensure everything works during testing. For the code editor, I recommend Visual Studio Code.

❓ Create a Discord Application

Go to the Discord Developer Portal to create your application. Follow the instructions and keep the page open to retrieve the necessary information later.

❓ Installation

To install the module, run the following command (this is where NodeJs comes in handy):

npm install simple-djs-handler

❓ Bot Configuration

You need to initialize your main file (which we'll call main.js) with the following code:

const { BotClient } = require('simple-djs-handler');
const { GatewayIntentBits } = require('discord.js');

const client = new BotClient({
  token: 'YOUR_BOT_TOKEN',
  slashCommandsEnabled: true, // Required for the module to function properly!
  slashCommandsClientId: 'YOUR_CLIENT_ID',
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMembers,
    GatewayIntentBits.GuildMessages,
    // ... add other intents as needed
  ],
});

client.start();
  • Replace YOUR_BOT_TOKEN with your bot's token. For this, go to the Bot section on the Discord Developer Portal.
  • Replace YOUR_CLIENT_ID with your bot's ID, available in the General Information section.

Then, you can start your bot with the following command:

node main

The module will automatically create the commands and events folders.

❓ Event Configuration

Here's how to create a Ready.js file in the ./events/ folder (e.g., ./events/Ready.js):

const { BotEvent } = require('simple-djs-handler');
const { Events } = require('discord.js');

module.exports = new BotEvent({
    name: Events.ClientReady,
    once: true, // To execute the code only once
    execute(client) {        
      client.user.setActivity('Visual Studio Code');
    },
});

To handle slash commands, create the InteractionCreate.js file in the same ./events/ folder:

const { BotEvent } = require('simple-djs-handler');
const { Events } = require('discord.js');

module.exports = new BotEvent({
    name: Events.InteractionCreate,
    async execute(interaction) {
        if (!interaction.isChatInputCommand()) return;

        const command = interaction.client.commands.get(interaction.commandName);
        const client = interaction.client;

        if (!command) {
            console.log(`No matching command found for ${interaction.commandName}.`);
            return;
        }

        try {
            await command.execute(client, interaction);
        } catch (error) {
            console.log(`Error executing the command: ${error}`);
        }
    },
});

❓ Command Configuration

Here is an example structure for a command without options:

const { BotCommand } = require('simple-djs-handler');

module.exports = new BotCommand({
    name: 'simple',
    description: 'A simple example command without options',
    execute: async (client, interaction) => {
        interaction.reply({
            content: "This is a simple command!"
        });
    },
});

And an example of a command with options:

const { BotCommand } = require('simple-djs-handler');

module.exports = new BotCommand({
    name: 'example',
    description: 'An example command with options',
    options: [
        {
            name: 'example_option',
            description: 'An example option',
            type: 'STRING', // See the table below for other option types
            required: true, // false if the option is not mandatory
        },
        // ... add other options as needed
    ],
    execute: async (interaction) => {
        const stringOption = interaction.options.getString('example_option');
        // Your command logic here
    },
});

❓ Available Option Types

| Option | Method to Retrieve | | ------------------ | -------------------------------- | | STRING | getStringOption() | | USER | addUserOption() | | CHANNEL | addChannelOption() | | ROLE | addRoleOption() | | SUBCOMMAND | addSubcommand() | | SUB_COMMAND_GROUP| addSubcommandGroup() |


This simplified version of the document provides essential steps to set up a Discord.js bot using the simple-djs-handler module.