2.7.3 • Published 11 months ago

djs-modules.js v2.7.3

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

Introduction

djs-modules.js is a package that creates and load all commands from a folder. It supports Slash commands and Context menu commands, and it's very simple and easy to use.

Table of Contents

Requirements

  • discord.js v14.9.0 or above.
  • Node.js v16.9.0 or above.

It's recommended to use TypeScript instead of JavaScript for the typings. If you're new on TypeScript, click here.

Installation

npm install djs-modules.js
yarn add djs-modules.js

Example

Example bot
├─── commands
│       └─── Utility
│               └─── ping.ts
└─── index.ts

index.ts:

import { Client } from 'discord.js';
import { Handler } from 'djs-modules.js';

const client = new Client({
    intents: ['Guilds']
});

const config = {
    token: 'Your bot token',
    id: 'Your bot ID',
    owner_id: 'Your account ID'
};

interface options {
    owner_only?: boolean
};

export const handler = new Handler<client, options>(client, './commands/', {
    includesDir: true
});

handler.on('commandLoad', (data) => console.log('Successfully loaded new command: ' + data.name));

handler.load();

handler.on('chatInputCreate', async (interaction, collection) => {
    const command = collection.get(interaction.commandName);

    if (!command) return;

    if (command.options) {
        if (command.options?.owner_only && interaction.user.id !== config.owner_id) {
            await interaction.reply({
                content: 'You are not the developer of the bot!',
                ephemeral: true
            });

            return;
        };
    };

    try {
        command.run(client, interaction, interaction.options);
    } catch { };
});

(async () => {
    await handler.deploy({
        token: config.token,
        applicationId: config.id
    });
})();

client.login(config.token);

ping.ts:

import { SlashCommandBuilder } from 'discord.js';
import { handler } from '../../index';

export default new handler.command({
    structure: new SlashCommandBuilder()
        .setName('ping')
        .setDescription('Replies with pong!'),
    options: {
        owner_only: true
    },
    run: async (client, interaction) => {
        await interaction.reply({
            content: `Pong! ${client.ws.ping}`
        });
    }
});

Options

Type parameters:

ParameterTypeOptional?DefaultDescription
ExtendedClientextends Client--The Discord bot client.
ExtendedCustomOptionsobjectYes{ }The custom options for each command builder.

The constructor:

OptionTypeOptional?DefaultDescription
includesDirbooleanYesfalseReads and loads other directories that exists in the provided folder path, including files.
defaultListenerbooleanYesfalseTrigger the property run from command builder whenever someone used it's application command name. If you have required custom options, you must set this property to false.
skipFileIfAlreadyExistbooleanYesfalseSkips a file if it's application command structure name already exists in the Collection. If the property's value is false, the original key's value (in collection) will be overwrited with another new module data.

deploy() method:

OptionTypeOptional?DefaultDescription
tokenstring--The Discord bot token.
applicationIdstring--The Discord bot application ID.
RESTRESTOptionsYesundefinedThe REST options.
guildIdstringYesundefinedThe guild ID to load the application commands on it.

Events

EventDescription
commandSkipWhenever a command has been skipped by the loader.
commandLoadWhenever a command has been successfully loaded.
chatInputCreateWhenever a Discord user has used a slash command.
contextMenuCreateWhenever a Discord user has used a context menu command (user or message).
userContextMenuCreateWhenever a Discord user has used a user context menu command.
messageContextMenuCreateWhenever a Discord user has used a message context menu command.
deployStartWhenever REST for application commands has started loading.
deployFinishWhenever REST has successfully finished loading app. commands.
deployErrorWhenever REST has caught an error.

License (©)

The MIT License

Copyright (c) 2023 T.F.A

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

2.7.3

11 months ago

2.7.2

11 months ago

2.7.1

11 months ago

2.7.0

11 months ago

2.6.0

11 months ago

2.5.2

11 months ago

2.5.1

11 months ago

2.5.0

11 months ago

2.4.1

11 months ago

2.4.0

11 months ago

2.3.0

11 months ago

2.2.0

11 months ago

2.1.0

11 months ago

2.0.1

11 months ago

2.0.0

11 months ago

1.5.0

12 months ago

1.4.1

12 months ago

1.4.0

12 months ago

1.3.0

12 months ago

1.2.0

12 months ago

1.1.3

12 months ago

1.1.2

12 months ago

1.1.1

12 months ago

1.1.0

12 months ago

1.0.2

12 months ago

1.0.1

12 months ago

1.0.0

12 months ago