Create your first plugin - Paragoumba/Bost GitHub Wiki
Bost's plugin API is free and open-source and anyone can use it to create a plugin for the Bost bots. Therefore, the Bost team is not responsible for the damages made on your machine by your plugin, or a plugin made by someone else.
Start by creating a Maven project. Install the bot in the local repo by following Compile the bot then add Bost as a dependency to your project. In this post we will create a plugin named TestPlugin:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<packaging>jar</packaging>
<version>1.0</version>
<modelVersion>4.0.0</modelVersion>
<groupId>fr.paragoumba.testplugin</groupId>
<artifactId>TestPlugin</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>fr.paragoumba.bost</groupId>
<artifactId>Bot</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Your main class must extend Plugin. The entrypoint of each plugin is onEnable. For your plugin to be valid, there must be a file named plugin.yml containing at least the name of your plugin and the name of the main class with its package. Here's our plugin.yml:
name: TestPlugin
main: fr.paragoumba.testplugin.TestPlugin
version: "1.0"
author: Paragoumba
You can validate your file plugin.yml with the JSON schema bost-schema.json.
To add commands to Bost, you need to register them. Here we register the test
command:
import fr.paragoumba.bost.api.Plugin;
import static fr.paragoumba.bost.CommandManager.registerCommand;
class TestPlugin extends Plugin {
@Override
public void onEnable(){
registerCommand("test", new Command(){
@Override
public boolean execute(String command, String[] args, Member sender, MessageChannel channel){
channel.sendMessage("Test!").queue();
return true;
}
});
}
}
This command will be executed when someone sends a message starting with ;test
. Be careful, ;
is the default prefix it can be anything else depending on the config.
Here the plugin just replies to the command ;test
by sending Test!
in the same channel.
The boolean you return in execute
is useful to let Bost know if the command executed successfully. If you return false, Bost will send a usage in the channel.
If a user types the command ;test 1 2 3
, the values of the parameters will be as follows:
-
command
contains the executed command. Will be equal totest
in this example. -
args
contains the parameters following the command. Will contain the following array ["1", "2", "3"]. -
sender
contains an instance of Member representing the user who sent the command. -
channel
contains an instance of MessageChannel in which the command has been sent. Useful for replying.
When registering your command, add a parameter to registerCommand:
registerCommand("test", new Command(){
@Override
public boolean execute(String command, String[] args, Member sender, MessageChannel channel){
if (args.length < 1){
channel.sendMessage("Test!").queue();
return true;
} else {
return false;
}
}
}, "%p%c - This command prints \"Test!\" in the channel if no parameters are given!");
The following variables can be used:
-
%p
will be replaced by the prefix used by the bot. -
%c
will be replaced by the string used to trigger your command without the prefix.
You can supply as many usages as you want. For example, if our test command can be called with zero or one parameter:
registerCommand("test", new Command(){
@Override
public boolean execute(String command, String[] args, Member sender, MessageChannel channel){
if (args.length < 1){
channel.sendMessage("Test!").queue();
return true;
} else if (args.length < 2){
channel.sendMessage("Parameter one: " + args[0]).queue();
return true;
} else {
return false;
}
}
},
"%p%c - This command prints \"Test!\" in the channel if no parameters are given!",
"%p%c arg - This command prints \"Parameter one: arg\" in the channel.");