Creating a Cinch plugin part 3: a word game bot for IRC
This is the third and final part of a tutorial for creating a cinch IRC bot game. If you haven't read the first two parts then you can start it here.
This part will add configuration options and a help command to the plugin.
At this stage, you should have a working word game. It allows someone to start a game, make guesses, win and cheat to end the game. The code should look pretty much like this.
It could do with some tidying up: for instance, if you start the game twice, it won't check to see whether there's an existing game in progress. But this is relatively simple stuff, so we'll move on to some of the things that are more specific to working with Cinch.
Adding a help command
First of all, let's add a help command, which we'll provide with
!word help. Users will be able to call this command to work out how to play the game.
Add this block of code somewhere in your
Note that you will need to add a single space on empty lines, as cinch will only print lines if they aren't blank.
Adding configuration options
There are a couple of things that we could allow users to configure. The most obvious one is allowing them to specify the dictionary used, or to allow them to provide a custom word list.
First, let's allow them to specify the path to the dictionary file. You set plugin configuration options along with all the other configuration (e.g. channel name, bot name, etc). You will probably have been using a test file for this, so add it there. Mine looks like this:
The plugin options are in a hash, which is indexed with the plugin class. Each plugin can have a hash of options, and options are accessed in the plugin with the
config hash. For example, in our
It's that simple. Now users can specify the file which contains the list of words.
However, this probably isn't enough. What if the user doesn't have a dictionary file with their particular OS? To be as inclusive as possible, we should provide the option to pass an array of words, so the user can define exactly which words are used. This way, if they want to get their words from a different source, they can do that. Let's add a
The reality is that they will use more words that this, otherwise it will be a very short game! In the plugin, we can now check to see whether the user has provided a list of words, and fall back to the dictionary file otherwise:
config hash is accessible to any of the instance methods that you use within your plugin, not just the initializer.
That's it! Thanks for reading, and I hope you learnt something about cinch. I'll be releasing the word game as a gem fairly soon, so you can just drop it in to your cinch bot.