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

hybridcommands

v1.0.1

Published

This package has been created to help you in making a bot helping with the loaders of commands and introducing you in to the slashes!

Downloads

5

Readme

About

This package has been created to help you in making a bot helping with the loaders of commands and introducing you in to the slashes!

Installing the package

npm i hybridcommands
yarn add hybridcommands

Config.json

{
    "token": "DICORD_TOKEN",
    "clientId": "CLIENT_ID"
}

Starting

const { Settings, Context } = require('hybridcommands') //Calling all builders of the package
import { Client, Collection, REST, Routes } from 'discord.js' //Calling the class to build the discord bot and the collector
const { token, clientId } = require('path/of/your/config.json')

const client = new Client({
    intents: 32511 //Intents can be getted on discord's developers portal
}) //The client was been created

const settings = new Settings({
    path: './path/of/commands/', //The path to read the commands
    client: client, //The discord client of your bot
    clientId: clientId, //The id of your bot to add command slashes
    token: token, //The token of your bot
    debug: true, //Log when a command was readded succesfully, if you want no log that, set this parameter false
    collection: new Collection(), //Here is were the commands was be saved, is similar to a Map
    slashesBody: [],
    context: Context //You can make your customContext
})

client.on('ready', () => {
    console.log(`Logged as: ${client.user.tag}`)
});

settings.setCollection() //Loading the commands

const rest = new REST().setToken(settings.token);

(async () => {
	try {
		console.log(`Started refreshing ${settings.slashesBody.length} application (/) commands.`);

		// The put method is used to fully refresh all commands
		await rest.put(
			Routes.applicationCommands(settings.clientId),
			{ body: settings.slashesBody },
		);

		console.log(`Successfully reloaded application (/) commands.`);
	} catch (error) {
		// And of course, make sure you catch and log any errors!
		console.error(error);
	}
})()

client.login(token)

Command Example

import {CommandBuilder, DataBuilder, CustomDataBuilder,ParamsBuilder} from 'hybridcommands'

const data = new DataBuilder({
    name: "ping",
    aliases: [],
    description: "pong!"
})
const customData = new CustomDataBuilder({
    foo: "bar"
})
const params = new ParamsBuilder({
    slash: data.adapt(),
    params: []
})
module.exports['command'] = new CommandBuilder({
    data: data,
    custom: customData,
    normal: true,
    slash: true,
    code: async(ctx) => {
        ctx.send("Pong!")
    }
})

messageCreate Event

client.on('messageCreate', async(message) => {
    if(message.author.bot || message.channel.type === 'dm') return; //If the messages is from a DM Channel the bot ignore that
    let prefix = '!' //The prefix of your bot
    const args = message.content.slice(prefix?.length).trim().split(/ +/);
    const commandName = args?.shift()?.toLowerCase();
    if (!commandName) return;
    const command = settings.getCommand(commandName)
    if(!command) return;
    const ctx = new Context({
        message: message,
        type: "normal"
    })

    try {
        command.code(ctx)
    } catch (e) {
        console.log
    }
})

interactionCommand Event

client.on('interactionCommand', async(interaction) => {
    if(interaction.isChatInputCommand){
        const command = settings.getCommand(interaction.commandName)
        if(!command) return;
        const ctx = new Context({
            interaction: interaction,
            type: "slash"
        })

        try {
            command.code(ctx)
        } catch (e) {
            console.log
        }
    }
})

Index with Custom Context

Whereever you refer to the common Context you must change it to your CustomContext and export it from the index.

const { Settings, Context } = require('hybridcommands')
import { Client, Collection, REST, Routes } from 'discord.js'
const { token, clientId } = require('path/of/your/config.json')

const client = new Client({
    intents: 32511 
})

class CustomContext extends Context {
    constructor(options){ //Make sure that what is contained in the options also has what the common Context needs
        super({
            data: options.data,
            command: options.command,
            type: options.type
        })
        //Do anything, this is a Class
    }
    //Also, you can made custom functions for your custom context!
}

const settings = new Settings({
    path: './path/of/commands/', 
    client: client, 
    clientId: clientId, 
    token: token, 
    debug: true, 
    collection: new Collection(), 
    slashesBody: [],
    context: CustomContext
})

client.on('ready', () => {
    console.log(`Logged as: ${client.user.tag}`)
});

settings.setCollection()

const rest = new REST().setToken(settings.token);

(async () => {
	try {
		console.log(`Started refreshing ${settings.slashesBody.length} application (/) commands.`);

		await rest.put(
			Routes.applicationCommands(settings.clientId),
			{ body: settings.slashesBody },
		);

		console.log(`Successfully reloaded application (/) commands.`);
	} catch (error) {
		console.error(error);
	}
})()

client.login(token)

module.exports['CustomContext'] = CustomContext //We export the CustomContext