0.1.6 • Published 1 year ago

globcommands v0.1.6

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

What is GlobCommands?

GlobCommands is a new handler with some extra capabilities and features that will be expanding overtime for Discord.js v14 bots.

Important note: This handler is made entirely for TYPESCRIPT, JavaScript might not work properly.

What are the features of this handler?

  • Reload of events and commands
  • Logging
  • Database Accessing to PocketBase (It will be expanded in the future)
  • Custom methods for defining the client
  • Everything divided into files to better understanding of the code

How do I use GlobCommands?

Starting the bot

First, you will need to create a init file, could be name whatever you want, but would be the one starting the bot. In my case I'll name it as: bot.ts

bot.ts

import { GlobClient, DbEngine } from 'globcommands'
// The DBEngine is an Enum that comes with globcommands to help you define the dbengine supported by globcommands

export const client = new GlobClinet(
  YOUR_TOKEN,
  {
    intents: ['YOUR_INTENTS'],
    partials: ['YOUR_PARTIALS'],
  },
  YOUR_GUILDID[DB_ENGINE] // In case your testing // This one is not needed if you don't want to use a Database Engine of any sort
)

client.start()

With that the bot will start.

How do I create commands?

The commands have a simple sintax to use.

  1. Create a src folder
  2. Inside that src folder, create two new ones, commands and events.
  3. (Optional) You can separate the commands by categories creating them inside each folder individually.

Now, when we have this, let's create a new file inside the folder commands:

(Doesn't need to be like this, can be only ping.ts or whatever) ping.command.ts

module.exports = {
  name: 'ping', // Name of the command
  description: 'Simple ping command', // Description of the command
  // Run function
  run: async ({ interaction }) => {
    interaction.reply({ content: 'Pong!', ephemeral: true }) // Callback of the function
  },
}

This will not work until you create an event handling the interactions, so let's create the interactionCreate event.

interactionCreate.event.ts

import { CommandInteractionOptionResolver } from 'discord.js'
import { ExtendedInteraction } from 'globcommands'
import { client } from '../../bot'
module.exports = {
  event: 'interactionCreate',
  run: async (interaction) => {
    if (interaction.isCommand()) {
      const command = client.commands.get(interaction.commandName)
      if (!command) {
        return interaction.reply('This command is not found.')
      }
      if (command.developer && interaction.user.id != '372840998918684672') {
        return interaction.reply({
          content: 'You can not use this command, is a developer command.',
          ephemeral: true,
        })
      }
      command.run({
        args: interaction.options as CommandInteractionOptionResolver,
        client,
        interaction: interaction as ExtendedInteraction,
      })
    }
  },
}

and now the ready event, making use of the logger that comes with globcommands

ready.event.ts

import { logger } from 'globcommands'

module.exports = {
  event: 'ready',
  once: true, // Only run this one once, you can remove it to do it more than 1 time
  run: async () => {
    logger.info('✅ Bot is up and ready!', { service: 'Bot' })
  },
}

But Slimy, why not making a ESM as TypeScript works with that?

Well, this is interesting, during the development I encounter a thing.

We all know that ESM modules makes use of import {} from 'package', but in order to make the reloading capabilities I wanted to do in the handler, this didn't work. Why?

Let me explain: The import statement, by design, doesn't let you access the cache of each import, is unnaccesible and inmutable.

So instead I needed to use the require(), which has a function to access the cache and clean it to refresh the files imported.

That's why I don't make use of ESM modules in this case.

FAQ

I found a Bug!!

:O OH NO, Report it to me!

I want this feature in

Let me be bold as I can be, I don't take requests in this case, you will need to wait until I want to implement something else or your feature if I want to.

(This section can be expanded in the future.)