v2 to v3 Migration Guide
Migration from v2 to v3
The v3 of the CommandHandler introduced a lot of new features I wanted to add for a long time, it is a complete rewrite from scratch for the commands & events part and the v2 code is completely incompatible, so this is the guide to migrate your code.
How to install it
Library now requires at least Node.js
v16, which will become LTS in october. Just select the beta version from npm.
Breaking changes
AdvancedClient
AdvancedClient#hasPermission
This method has been removed because of the method being not coherent with the class itself.
BetterEmbed
BetterEmbed#checkSize
Note :
checkSize
method still exists, but now just returns an object containing the fields too long.
CommandHandler
Methods
Defaults functions have new names that are more coherent :
Events
Launch event emits now the LaunchCommandHandlerOptions
.
Command
Commands are created in a very different way, using now a subclass, methods for defining SubCommands, a CommandContext
argument instead of a custom one depending on your message
event etc.
Example :
So you will have to change all of your commands. This choice was taken by me, so you can define methods in your classes that will do special things, see on the Added section.
DeleteMessageOptions
DeleteMessageOptions#options
has been removed, as a message cannot be deleted with a reason in the new Discord API.
Note You can also do this to delete the message of the command :
Event
It's the same thing as commands, now using classes to have improved behaviors.
Example :
Note: Notice that in both case, syntax is shorter and more readable.
Functions
argError
, codeError
and permissionsError
now only accept a CommandContext
argument instead of message
and command
.
saveJSON
only accept a JSON instead of any
for the value type.
Logger
Logger#test
test
has been renamed to debug
because it's a more logic feature & name, and it was more used for debugging than testing
LogType
is now a TS enum rather than an object, it's for having better type checking and better code.
Added
AdvancedClient
Getters
These are just shortcuts of the normal code to get them.
BetterEmbed
BetterEmbed#setImageFromFile
setImageFromFile(path)
lets you set the image of the embed from a local file.
BetterEmbed#setThumbnailFromFile
setThumbnailFromFile(path)
lets you set the thumbnail of the embed from a local file.
Command
As you now have to extend the Command
class to define a command, there are some included new classes with pre-defined methods, see ImageCommand & SlowCommand, you should also do this for commands with a lot of same behavior.
Commands can now have an overwritten registerSubCommands
method for defining SubCommands. They also have a new subCommand
method for registering a new SubCommand.
Command#execute
execute(ctx)
is a new internal method that should not be overwritten, otherwise command will not be properly executed with SubCommands.
Note : This method should on the other hand be used for custom
message
events.
Command#nameAndAliases
nameAndAliases
is a new getter to get an array containing the name & the aliases of the command if any.
Command#registerSubCommands
registerSubCommands()
is a new empty method than have to be overwritten for defining SubCommands.
Command#run
run(ctx)
is the method to overwrite to define the behavior of the command when executed.
Command#subcommand
subCommand(name, options?, callback)
is a new method for defining a SubCommand, it has to be used in the registerSubcommands
method, if you register a SubCommand in the run
method, the SubCommand will not be executed the first time. Example :
Command#subCommands
subCommands
is a new property containing all the SubCommands of the command.
Command#validate
validate(ctx)
is a new method to test all the other validation methods, it returns an object with the details of the error and the error data (like message content). It is used by the execute
method.
CommandContext
CommandContext is a new class used across many methods in the Command class.
See on the documentation what properties & methods this class include.
CommandHandler
CommandHandler#create
The method has a new property useMentionPrefix
and is a boolean, if set to true the mention of the bot will be added to the prefixes. The property is set to true
by default to follow the behavior of v2.
CommandHandler#getCommandAliasesAndNames
CommandHandler.getCommandAliasesAndNames()
is a new function returning in one array the name & all the aliases of all commands.
CommandHandler#getPrefixFromMessage
CommandHandler.getPrefixFromMessage(message)
is a new function returning the prefix (if found) used in the message.
CommandHandler#launch
The methods now lets you set the presence of the bot or cycling presences, see here how to do it.
CommandHandler#loadEvent
CommandHandler#loadEvent(directory, filename)
is a new function to load a single event.
CommandHandler#unloadCommand
CommandHandler.unloadCommand(name)
is a new function, unloading the following command if found.
CommandHandler#unloadEvent
CommandHandler.unloadEvent(name)
is a new function unloading the following event and unbinding it to the client if found.
Defaults
There is now a new default help
command.
See their documentation here.
Event
Event#run
run(ctx)
is the method to overwrite to define the behavior of the event when fired.
ImageCommand
ImageCommand
is a new subclass of the class Command
that should be used for commands returning embeds with an image or local images.
It adds sendLocalImage(options)
& sendImageEmbed(options)
methods, see on the documentation how to use them.
Logger
LogLevel
LogLevel
is a new TS enum to define levels of logs, so you can ignore or test Levels.
Logger#LEVEL
Logger.LEVEL
is a new property to ignore some logs that are lower than the level.
Example :
Logger#ignore
Logger.ignore
is an array for defining ignored titles and even titles with a lower level than the one set.
Example :
Logger#savingFiles
Logger.savingFiles
is an array for defining in which files the logs should be emitted in, logs are appended, the file is not overwritten.
SlowCommand
SlowCommand
is a new subclass of the class Command
that should be used for commands where the user has to wait.
It adds a waitEmoji
property that is by default equals to ⌛
and the startWait(message)
& stopWait(message)
methods to add then remove the emoji.
SubCommand
SubCommand
is a new subclass of the class Command
, it's useful for handling commands with different behavior of one command, it's not similar as arguments that are not currently handled.
SubCommand has the same field as Command
class, except that its run function uses a SubCommandContext
instead of a CommandContext
.
See here how to create and use SubCommands.
SubCommandContext
SubCommandContext
is a new class extending CommandContext
, used in run
functions of SubCommands.
See on the documentation what properties & methods this class include.
Types
There are some new useful types exported.
They are primarily used internally, but I think that it would be useless to export them.
Last updated