0.1.8 • Published 3 years ago

slimecommands v0.1.8

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Slime Commands

SlimeCommands changed the usage.

Slime command Introduction

Now it uses semver, so the versions indicate what was made in the code. X.Y.Z, being X major changes, Y Minor changes, Z fixes in the code.. As version is now 0.1.5, this means that is the initial release in its 5th fixes in the code version.

Routing for using SlimeCommands

In order for it to work, you need to follow a specific routing system in your code.

You need to have:

  • Commands Folder
  • Events Folder

With the structure like so:

bot
│   index.js
│      
│
└───Commands
│   │
│   │    
│   └───context
│   │   │ 
│   │   │ pingContext.js
│   │   │
│   └───client(You can use the names you want)
│       │
│       │ping.js
│ 
└───Structures
    │   
    └───Events
        │   
        └───Client
            │ interactionCreate.js
            │ ready.js

Code inside the files

index.js

    const slimecmd = require('slimecommands');

    // After the client is defined
    client.commands = new Collection()
    module.exports = client;

    client.login(process.env.DC_TOKEN).then(() => {
        slimecmd.load(client)
    }).catch((err) => {
        console.error(err)
    });

Then you need to create the Events:

  • interactionCreate.js
  • ready.js

You can grab them from the official repository of my bot, Project: Pyro in github.

Project: Pyro, official github repository

Or make you own events.

I'll let you choose, so I let you here the code I made, take it, and modify it.

interactionCreate.js

const { EmbedBuilder, Collection, PermissionsBitField } = require('discord.js');
const ms = require('ms')
require('dotenv').config();
const cooldown = new Collection();

module.exports = {
    name: 'interactionCreate',
    on: true,
    async execute(interaction, client){
        const Command = client.commands.get(interaction.commandName);
        if(interaction.type == 4){
            if(Command.autocomplete) {
                const choices = []
                await Command.autocomplete(interaction, choices);
            }
        }

        if(!interaction.type == 2) return;

        if(!Command) return client.commands.delete(interaction.commandName)

        try {
            if(Command.cooldown) {
                if(cooldown.has(`slash-${Command.name}${interaction.user.id}`)){

                return interaction.reply({
                    content: `You're now in ${ms(`slash-${Command.name}${interaction.user.id}`) - Date.now()} cooldown!`
                })};

                if(Command.userPerms || Command.botPerms) {
                    if(!interaction.memberPermissions.has(PermissionsBitField.resolve(Command.userPerms || []))) {
                        const userPerms = new EmbedBuilder()
                        .setDescription(`🚫 ${interaction.user}, You don't have \`${Command.userPerms}\` permissions to use this command!`)
                        .setColor('Red')
                        return interaction.reply({embeds: [userPerms]})
                    }
                    if(!interaction.guild.members.cache.get(client.user.id).permissions.has(PermissionsBitField.resolve(Command.botPerms || []))) {
                        const botPerms = new EmbedBuilder()
                        .setDescription(`🚫 ${interaction.user}, I don't have \`${slashCommand.botPerms}\` permissions to use this command!`)
                        .setColor('Red')
                        return interaction.reply({embeds: [botPerms]})
                    }
                }

                await Command.run(client, interaction);
                cooldown.set(`slash-${Command.name}${interaction.user.id}`, Date.now() + Command.cooldown);
                setTimeout(() => {
                    cooldown.delete(`slash-${Command.name}${interaction.user.id}`)
                }, Command.cooldown)

            } else {
                if(Command.userPerms || Command.botPerms) {
                    if(!interaction.memberPermissions.has(PermissionsBitField.resolve(Command.userPerms || []))) {
                        const userPerms = new EmbedBuilder()
                        .setDescription(`🚫 ${interaction.user}, You don't have \`${Command.userPerms}\` permissions to use this command!`)
                        .setColor('Red')
                        return interaction.reply({embeds: [userPerms]})
                    }
                    if(!interaction.guild.members.cache.get(client.user.id).permissions.has(PermissionsBitField.resolve(Command.botPerms || []))) {
                        const botPerms = new EmbedBuilder()
                        .setDescription(`🚫 ${interaction.user}, I don't have \`${Command.botPerms}\` permissions to use this command!`)
                        .setColor('Red')
                        return interaction.reply({embeds: [botPerms]})
                    }
                }
                await Command.run(client, interaction)
            }

        } catch(err) {
            console.error(err)
        }
        }

    }

ready.js

const chalk = require('chalk')
const { Client } = require('discord.js');

module.exports = {
    name: "ready",
    on: true,
    /**
     * 
     * @param { Client } client
     * 
     */
    async execute(client) {
        console.log(chalk.green(`Client is now logged in`));
        client.user.setPresence({
            activities: [{
                name: "/help",
            }]
        })
    } 
} 

With that your bot should work if you downloaded this npm packge.

If not, feel free to contact me on github, if I can, I can give you a hand always ;).

Important information about SlimeCommands

SLIMECOMMANDS ONLY SUPPORTS SLASH COMMANDS AT THE MOMENT.

Remember that this is as barebones as it can be.

The discord.js version used to test this one, was the version 14 (Last version).

How to make a command with SlimeCommands

Now, I'll show you how to create a simple ping command with slimecommands.

You need to do this: ping.js

const { ApplicationCommandType } = require('discord.js');

module.exports = {
	name: 'ping',
	description: "Check bot's ping.",
	type: ApplicationCommandType.ChatInput,
	cooldown: 3000,
	run: async (client, interaction) => {
		interaction.reply({ content: `🏓 Pong! Latency: **${Math.round(client.ws.ping)} ms**` })
	}
};

The ApplicationCommandType is the type of command that discord defines in their application.

  • name, name of the command.
  • description, description of the command.
  • type, type of command that discord defined.
  • cooldown (optional), time to use the command again in miliseconds
  • run, actual function of the command

Run: is only a method of defining what the command will do, so doing:

run: async (client, interaction) => { //What the command will return }

We are telling the code to detect when the command is trigrerred in discord and what the command will do when that happens.

0.1.8

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago