-
Notifications
You must be signed in to change notification settings - Fork 1
Plugins
Creating plugins is a very simple task. To create your first plugin, create a folder on your local machine with the name of your plugin (it can be any name, but for the purposes of this example we will name it after our plugin, MyPlugin.)
Once you've made your folder, create a TypeScript file titled plugin.ts
.
Inside of your plugin.ts
file, paste the following sample code:
import {Cast, Logger, Plugin} from "cast";
import {EventEmitter} from "events";
export default class MyPlugin extends EventEmitter implements Plugin {
public cast: Cast;
public logger: Logger;
public name: string = "My Plugin";
public version: string = "1.2.3";
public debugMode: boolean = false;
public pluginConfig: object = {};
public listeningEvents: string[] = ['message'];
public id: string = "myplugin";
public onLoad(cast: Cast, logger: Logger): Promise<void> {
return new Promise((resolve, reject) => {
this.cast = cast;
this.logger = logger;
this.logger.log("Helo i am loaded");
resolve();
});
}
public onEnable(): Promise<void> {
return new Promise((resolve, reject) => {
this.attachListeners();
this.logger.log("Helo i am here");
resolve();
});
}
public onDisable(): Promise<void> {
return new Promise((resolve, reject) => {
this.logger.log("Helo i am leave now");
resolve();
});
}
private attachListeners(): void {
this.on('message', (message) => {
this.logger.log(`${message.author.username}: ${message.cleanContent}`);
});
}
}
You now have a basic Cast plugin! Go ahead and compile your plugin, then copy your folder into the plugins directory of your Cast bot. When you start your bot, you should see Helo i am loaded
, Helo i am here
and Helo i am leave now
.
Did you notice the this.on('message', ...);
method towards the bottom of the code? This is an example of an event, which are reflections of Discord.JS events. There are a few other events mixed in that are visible here.
The reason that events are reflected onto the plugin object instead of accessing them directly via the client object is to allow per-guild plugin enabling/disabling and blacklisting of users. Plugins which listen directly on the client object will be rejected from official repositories.
Plugins can also have commands, which are handled by the CommandManager. To add commands to your plugin, add the following code snippets where directed:
Replace your import statements with the snippet below.
import {Cast, CommandContainer, Logger, Plugin} from "cast";
import {EventEmitter} from "events";
import * as path from "path";
Add the following property to the definitions of properties in the top of your class.
public commands: CommandContainer;
Inside of your onLoad
method, add the following code:
this.commands = this.cast.createCommandContainer(path.join(__dirname, "commands"), this);
Inside of your onEnable
method, add the following code:
this.commands.loadAll();
After inserting the code above, it's time to set up your command workspace.
- Create a folder titled
commands
in your plugin's source folder. - Inside of your newly created folder, create a file titled
helloworld.ts
- Now, paste the code provided into your newly created file.
import {Cast, Command, Plugin, Response} from "cast";
import {Message} from "discord.js";
export default class HelloWorld implements Command {
public parent: Plugin;
public cast: Cast;
public permission: "myplugin.helloworld";
public handle(response: Response, message: Message, args: string[]): Promise<boolean> {
return new Promise((resolve, reject) => {
response.reply(`Hello world! ${args.join(" ")}`).then(() => resolve(false));
});
}
}
Now, it's time to test your plugin
- Compile your plugin (
tsc
should work) - Copy your myplugin folder into your Cast bot's plugin folder, replacing the old myplugin folder if necessary.
- Add the
myplugin.helloworld
permission to the list of default permissions - Start your bot
- Try out your command! Run
${bot prefix}helloworld arg1 arg2 arg3
from the Discord chat, and if all goes well, you should seehelloworld arg1 arg2 arg3
sent by your bot.