Nadim147c / Discord-handler

A automatic Discord.js typescript handler. It automatically handles slash commands, event and other discord interactions.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

⚠ This handler no longer supports windows

I recently archived this repository because it was really hard to make it work on windows. So, I dropped it's support for windows. In future, I might add support for windows. But current you have to use WSL 2 to work to work on it on windows.

Discord Handler

A complete discord.js typescript handler. Automatically register commands and subcommands.

Features

  • Dynamic handler. You don't have to use client.on in your index file.
  • Creates sub commands from directory name for handling them from different module.
  • Automatically register commands.
  • Handle other [Buttons, SelectMenu, Modal, ContextMenu] interactions.
  • Handle Message and Interaction triggers.


Installation

  • Use this command to clone the repository.
git clone https://github.com/3N147/Discord-handler.git
cd Discord-handler

Install all dependencies and start as dev. Using npm:

npm i
npm run dev

or using yarn:

yarn
yarn dev

Environment Variables

  • Create .env file and set those following values. Take a look in the example file.
    • DISCORD: Discord API Token
    • LOGIN: Discord Webhook URL for bot logging logs
    • ERROR: Discord Webhook URL for error logs
    • GUILDS: Discord Webhook URL for guild join or leave logs

Commands

  • All commands are categorized by folder in commands folder.
  • You just have to export a commands object using the Command Class.
  • run method will be executed when someone uses the command.
import { Command } from "../../../structures/Command"

export default new Command({
    data: {
        name: "command-name", // The name of the command
        description: "A description",
    },
    async run(interaction) {
        // Your code goes here
    },
})

Auto Complete

  • Autocomplete interaction are handled by autocomplete method. Learn More
Example
import { Command } from "../../../structures/Command"
import { ApplicationCommandOptionType } from "discord.js"

export default new Command({
    data: {
        name: "autocomplete",
        description: "autocomplete example",
        options: [
            {
                type: ApplicationCommandOptionType.String,
                name: "input",
                description: "Type anything for autocomplete.",
                autocomplete: true,
                required: true,
            },
        ],
    },
    async autocomplete(interaction, focused) {
        const choices = getChoicesSomeHow(focused)
        return choices
    },
    async run(command) {
        return
    },
})

Properties

  • You can set permissions property. It will automatically check permissions for you.
  • You can set custom timeout, dev or beta, defer and ephemeral property.
Example
import { Command } from "../../../structures/Command"

export default new Command({
    data: { name: "ping", description: "ping pong" },
    dev: true,
    beta: true,
    permissions: ["Speak"],
    deffer: true,
    ephemeral: true,
    timeout: 1000 * 5, // 5 seconds
    async autocomplete(interaction, focused) {},
    async run(command) {},
})

Methods

  • You get response, warn and error method for quickly replying to users.
Example
import { Command } from "../../../structures/Command"

export default new Command({
    data: { name: "ping", description: "ping pong" },
    async run(command) {
        command.response("Thanks for using me.")
        command.warn("You can't do that.", false, 5)
        command.error("User don't exists.", true)
    },
})

Subcommands and Files Structure

  • You can create a folder and put your commands folder in it to create subcommand and subcommand-group. Learn More
commands
|
|__ category-folder
    |__ command-file    "/ping"
|
|__ category-folder
    |__ command-folder
       |__ command-file   "/help commands"
|
|__ category-folder
    |__ command-folder
        |__ subcommand-group-folder
            |__ command-file    "/timeout user remove"

Interactions

All Button, Select Menu, Modal and Context Menu handlers are available in the interactions folder.

Note: Context Menu have two types in the context-menus (Users and Messages) folder.

Properties

  • Every interaction handler has permission property for auto permission checking.

Methods

  • You get response, warn and error method for quickly replying to users. (same as commands)

Note: autocomplete interactions are handled from command handlers.

Select Menus

selectmenuInteraction has different types. [String, User, Role, Channel, Mentionable] There're different classes for different types of select menu.

Files Structure

It uses glob to find all module on a particular folder.

EXAMPLE OF VALID SELECT MENU HANDLERS
select-menus
|
|__ folder
    |__ file
|
|__ file
|
|__ folder
    |__ folder
        |__ file

Custom Value

In Button, Select Menu and Modal Submit interaction there is customValue property.

Here is an example use case of this feature. This is a user filter.
import { Command } from "../../../structures/Command"

// Command handler
export default new Command({
    data: {name: "create-button", description: "create a cool button"},
    async run(interaction) {
        // customId = `${key}:${customValue}`
        const button = new ButtonBuilder()....setCustomId(`cool-button:${user.id}`)
        const components = [...(button)]
        interaction.reply({ components })
    },
})
import Button from "../../../structures/Button"

// Button handler
export default new Button({
    id: "cool-button", // key
    async run(interaction) {
        // customValue
        if (interaction.customValue !== interaction.user.id) interaction.warn("You can't use this button")
    },
})

Triggers

  • There are two type of triggers. (Reaction and Message)
  • Reaction trigger runs when someone reacts with the specified emoji.
  • Message triggers runs when someone includes specified word/prase in the Message Content.

Methods

  • You get response, warn and error method for quickly replying to users. (same as commands)

Logging

You can save logs using Discord Webhook.

  • Set your Discord Webhook URL as environment variable.
  • Check environment variable part in installation.

License

Distributed under MIT license by 3N147.

About

A automatic Discord.js typescript handler. It automatically handles slash commands, event and other discord interactions.

License:MIT License


Languages

Language:TypeScript 99.8%Language:Dockerfile 0.2%