Skip to content

AmandaDiscord/CommandManager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AmandaDiscord's CommandManager

Why?

This is generic enough and quite helpful in centralizing the info and logic when it comes down to handling interaction based commands from Discord. Specifically ChatInputCommands and ContextMenuCommands. This has the benefit of a reduction of references such as writing the declarative info you would send off to Discord and also where you'd declare the function to actually run the command.

How?

You'd construct a CommandManager instance and supply the paramGetter to the manager as well for when it's handling the commands. This runs for every command call after an interaction has been received through manager.handle

const { CommandManager, ChatInputCommand } = require("@amanda/commands")

const manager = new CommandManager(cmd => [new ChatInputCommand(cmd)])

You'd then add commands to the manager like so:

manager.assign([
	// ...commands here
])

When you get to writing the process function for each command, the arguments for the function will already be typed depending on what you supplied in the Array when building the CommandManager. Typically, you'd have the same params for each command, but they can be different. You may just have to overwrite the types. Alternatively, you can use multiple managers to circumvent the differences in typings as you may need to add more type assertions which is really annoying to do in pure JS. Annoying in TS, but eh.

For the example I wrote, your process function would look like

{
	// ...extraCommandInfo
	process(cmd) {
		// cmd is a ChatInputCommand
	}
}

Then when you receive an interaction with a type of type 2 (commands), you could just straight up pass the payload to

const commandExists = manager.handle(payload)

handle also has a 2nd param called replyFn which is optional. If you've already replied to the interaction, then great. Otherwise, this checks for if the command exists in the manager first and then continues to try to reply. If the command already expired, it fast fails without possibly running expensive command code.

if there's an error with handling the command, the manager can be initialized with an errorHandler function in the constructor which can either be a simple console.log or a function to send the error off to a dev reporting channel/etc.

The Exports

On top of the CommandManager, this package exports a few other helper classes. These are mainly for avoiding findFirst/Last or filter ops on the raw API data arrays for options/etc. For the options specifically, you can also type assert them as specific types supported by Discord's API. (string, number, boolean)

About

A generic Discord interaction based command manager and handler

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors