Skip to content

Creating a chuck norris joke plugin

Dominic edited this page May 21, 2017 · 3 revisions

This page describes how to create a plugin. The sample project is included in the DylanBot repository.

This "tutorial" assumes you've already created a project and added the necessary references.

Plugin class

The first thing your plugin needs is one class deriving from the Plugin class.

public class ChuckNorrisDatabasePlugin : Plugin
{

}

For the plugin class it is necessary to create 2 methods, one named "Load" gets executed when the plugin is being loaded. The other one named "Unload" gets executed when the plugin should do Unload routines, either because it was deactivated, or the Bot is shutting down.

public class ChuckNorrisDatabasePlugin : Plugin
{
    public override void Load()
    {
    }

    public override void Unload()
    {
    }
}

This plugin will use the "The Internet Chuck Norris Database" to get jokes and sends it to Twitch through Chat messages.

A command

We first need to register our command. So in the Load routine we'll add a call to PluginSystem.Commands.RegisterGlobalCommand. We want that our method gets run when we type in chat !chuck.

When we look at the RegisterGlobalCommand method we see that the first parameter is the command, and the second the method that should get executed. Since the RegisterGlobalCommand is handling the prefix we do not need to add the ! part.

Note: Adding multiple commands with the same function is called aliasing, and can be added as the third parameter!

Note: It is not possible to create multiple methods with the same command handler.

PluginSystem.Commands.RegisterGlobalCommand("chuck", OnJokeCommand);

In our Unload method we then need to unregister the command:

PluginSystem.Commands.UnregisterGlobalCommand("chuck");

And then create our method:

private bool OnJokeCommand(OnChatCommandReceivedArgs args)
{
    return false;
}

The command logic

For us to be able to get a random joke we'll need to create a class so we can deserialize the result to. I don't go in much detail on this one since it should be self-explaining.

public class JokeResult
{
    public class ValueClass
    {
        [JsonProperty("id")]
        public int Id;

        [JsonProperty("joke")]
        public string Joke;

        [JsonProperty("categories")]
        public List<object> Categories { get; set; }
    }

    [JsonProperty("type")]
    public string Type;
    [JsonProperty("value")]
    public ValueClass Value;
}

We get a random joke from the url, deserialize the result, check if it correct and if it is send out to chat.

using (WebClient web = new WebClient())
{
    var result = JsonConvert.DeserializeObject<JokeResult>(web.DownloadString("http://api.icndb.com/jokes/random"));
    if (result != null && result.Type == "success")
    {
        PluginSystem.SendChatMessage(result.Value.Joke);
    }
    else
    {
        PluginSystem.SendChatMessage("I was unable to find any jokes :/");
    }
}

When we have done everything correctly the bot should load up and it should say that our plugin was loaded.

When we now type in chat !joke it should sent a random joke.

--- Here comes the next part talking about arguments ---

Clone this wiki locally