Creating Extensions for SudoBot v8
SudoBot has support for extensions, and it was introduced in version 6.17. Extensions can extend the bot's feature, by adding commands, event listeners, services and a lot more stuff. You can install/uninstall/disable extensions, or create your own to make the bot behave exactly how you want. Extensions can be written using JavaScript and TypeScript.
In this article, you'll learn how SudoBot's extension system works.
The extensions
directory
The extensions
is a special directory in the project root. Every installed extension stays inside this directory. If you want to install extensions, you have to create a directory named extensions/
in the project root.
Each installed extension has a directory associated with it inside the extensions/
directory. Inside of that inner directory, there is a special file extension.json
which contains meta information about the extension, and how to build it.
The extension.json
file looks something like this:
Creating your first SudoBot extension
To get started, first create a directory named extensions
inside the project root. In that directory, create another directory for your extension. The name of this directory usually should be your extension's name. In this example, we'll name the extension "hello".
Then inside your extension's directory, create the extension.json
file, and the src/
directory. Inside src
, create events
and commands
directories. The final directory tree should look something like this:
+ sudobot/ [project root]
+ extensions/
+ hello/
+ src/
+ commands/
+ events/
- extension.json
Now add the following to your extension.json
file:
We'll be using TypeScript to write the extension in this example. If you'd like to use JavaScript instead, you can set language
to javascript
and you don't need to specify a build command, and your main directory will be the directory where you put your JavaScript files (usually src/
). You should also adjust the paths to point to that directory (rather than build/
which is used in this example).
Setting up TypeScript and Dependencies
First, run npm init
to initialize your extension project. This will ask you a few questions and create a package.json
file. Then run:
Remember this is a really important step to make sure your extension can access SudoBot's core utilities to initialize itself. If you don't link SudoBot with your extension, it will fail to import the necessary files.
Then we can go ahead and install the dependencies and also set up TypeScript.
This will add typescript
as a dev dependency and also create the tsconfig.json
file which contains the configuration for the TypeScript compiler.
Now open up tsconfig.json
file, and add the following to the compilerOptions
object:
This sets up the @sudobot
import alias for TypeScript, specifies the source root and build directory, and a few other things that are needed.
Remember to build the bot beforehand! As you can see, this alias points to
the build
directory which is created when you build the bot.
Then open up package.json
file and add the following inside the root object:
You might be thinking, why do we need to add the module aliases twice? It's because TypeScript doesn't actually deal with these module aliases, it just checks the types and imports. In runtime, we need another way to resolve these imports. We use module-alias
for that.
The entry point
We need to create the entry point now! Make a file src/index.ts
and put the following code inside of that file:
That's actually all we need inside this file.
Adding commands to the extension
Alright, let's add a command to the extension! Create a file src/commands/HelloCommand.ts
and inside of that file, put the following code:
This command just responds to the user with "Hello world, from the hello extension!".
Adding event listeners to the extension
Now, let's add an event listener to the extension! Create a file src/events/MessageCreateEvent.ts
and inside of that file, put the following code:
This event listener listens to messageCreate
event, and whenever someone sends a message with content "ping", it will reply to them.
Building the extension
Go to the project root, and run the extensions.js
script to build all the installed extensions:
This will take a little bit time. After that, you're ready to go. You can now start the bot (assuming you've build it already):
And then if everything was configured correctly, the hello
command will be loaded and can be executed on any server.
Congratulations, you've just built an extension for SudoBot!
Help and Support
If you need help with anything, feel free to create a discussion topic at the GitHub repo. You can also contact via email at [email protected], or join our Discord Server.