diff --git a/README.md b/README.md index a9200b3..1d6690a 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,10 @@ productive fellow. ## Features -- Ping (returns Pong!; good for testing connectivity) -- Dice Roller (`!roll 2d4` -> `Roll: 2d4 ([2, 1]) ==> 3`) +- List available commands (`!listcommands`) and get help with them (`!help + [COMMAND]`) +- Ping (returns `Pong!`; good for testing connectivity) +- Dice Roller (`!roll 2d4` --> `Roll: 2d4 ([2, 1]) ==> 3`) - 8-ball (`!8ball Will I win the lottery?` --> `Try again later`) - Ad-hoc simple custom commands (`!addcommand hello Hey there!` --> `!hello` --> `Hey there!`) diff --git a/lib/addons/custom_commands.rb b/lib/addons/custom_commands.rb index ee576e7..3974767 100644 --- a/lib/addons/custom_commands.rb +++ b/lib/addons/custom_commands.rb @@ -25,6 +25,31 @@ module Chronicle @bot.available_commands(self, @custom_commands.keys) end + # Provide help for the commands of this addon + # + # @param message [Message object] The relevant message object + def help_command(message) + pfx = @bot.cmd_prefix + cmd = message.content[:body].split(/\s+/)[1].gsub(/#{pfx}/, '') + + res = 'Invalid command' + + case cmd + when "addcommand" + res = cmd_add_usage + when "modcommand" + res = cmd_mod_usage + when "remcommand" + res = cmd_rem_usage + else + res = cmd_custom_usage(cmd) + end + + room = @bot.client.ensure_room(message.room_id) + + room.send_notice(res) + end + # Handle a command from the Matrix protocol # # @param message [Message object] The relevant message object @@ -152,6 +177,18 @@ module Chronicle "You can modify it by typing `!modcommand #{command}`" end + # Help message for addcommand + def cmd_add_usage + 'Add a custom command. '\ + "\nUsage: !addcommand COMMAND TEXT" + end + + # Help message for modcommand + def cmd_custom_usage(cmd) + 'Prints text associated with the custom command'\ + "\nUsage: !#{cmd}" + end + # Error message when trying to add an existing command def cmd_addon_error 'This command is already used by another addon.' @@ -163,12 +200,24 @@ module Chronicle "You can add it by typing `!addcommand #{command}`" end + # Help message for modcommand + def cmd_mod_usage + 'Modify a custom command. '\ + "\nUsage: !modcommand EXISTING-COMMAND TEXT" + end + # Error message when trying to delete a non-existing command def cmd_rem_error 'This custom command does not exist. '\ 'Nothing to remove.' end + # Help message for modcommand + def cmd_rem_usage + 'Remove a custom command. '\ + "\nUsage: !remcommand EXISTING-COMMAND" + end + # Read the existing saved commands into memory def read_commands(msgid) cmds = {} diff --git a/lib/addons/roller.rb b/lib/addons/roller.rb index 1c522e9..7fb98cb 100644 --- a/lib/addons/roller.rb +++ b/lib/addons/roller.rb @@ -15,6 +15,19 @@ module Chronicle @bot = bot end + # Provide help information for this command + # + # @param message [Message object] The relevant message object + def help_command(message) + room = @bot.client.ensure_room(message.room_id) + + res = 'Rolls dice, and provides a result' + res += "\nUsage: !roll DICE" + res += "\nExample: !roll 3d6" + + room.send_notice(res) + end + # Handle a command from the Matrix protocol # # @param message [hash] The message data from Matrix diff --git a/lib/addons/utils.rb b/lib/addons/utils.rb index 38bd545..530e0e6 100644 --- a/lib/addons/utils.rb +++ b/lib/addons/utils.rb @@ -15,6 +15,18 @@ module Chronicle @bot = bot end + # Provide help information for this command + # + # @param message [Message object] The relevant message object + def help_command(message) + room = @bot.client.ensure_room(message.room_id) + + res = 'Returns "Pong!"' + res += "\nUsage: !ping" + + room.send_notice(res) + end + # Handle a command from the Matrix protocol # # @param message [Message object] The relevant message object @@ -38,6 +50,18 @@ module Chronicle @bot = bot end + # Provide help information for this command + # + # @param message [Message object] The relevant message object + def help_command(message) + room = @bot.client.ensure_room(message.room_id) + + res = 'Questions the fates to determine an answer for your question.' + res += "\nUsage: !8ball QUESTION" + + room.send_notice(res) + end + # Handle a command from the Matrix protocol # # @param message [Message object] The relevant message object diff --git a/lib/chronicle_bot.rb b/lib/chronicle_bot.rb index db76a22..ef85ab8 100755 --- a/lib/chronicle_bot.rb +++ b/lib/chronicle_bot.rb @@ -59,6 +59,7 @@ module Chronicle @allowed_commands = {} register_commands + available_commands(self, ['listcommands', 'help']) end # All available commands @@ -86,6 +87,47 @@ module Chronicle end end + def help_command(message) + pfx = @cmd_prefix + cmd = message.content[:body].split(/\s+/)[1].gsub(/#{pfx}/, '') + + case cmd + when 'listcommands' + res = '!listcommands: List available commands managed by this bot' + else + res = 'Try !listcommands or !help' + end + + res + end + + # Handle a command from the Matrix protocol + # + # @param message [Message object] The relevant message object + def matrix_command(message) + pfx = @cmd_prefix + cmd = message.content[:body].split(/\s+/)[0].gsub(/#{pfx}/, '') + + res = 'Invalid command' + + case cmd + when 'listcommands' + res = 'Currently available commands: ' + res += @all_commands.keys.join(', ') + when 'help' + res = if message.content[:body].split(/\s+/).count <= 1 + '!help: Get help for a specific command' \ + "\nUsage: !help COMMAND" + else + second_cmd = message.content[:body].split(/\s+/)[1].gsub(/#{pfx}/, '') + res = @all_commands[second_cmd.strip].help_command(message) + end + end + + room = @client.ensure_room(message.room_id) + room.send_notice(res) + end + def on_message(message) return unless message.content[:msgtype] == 'm.text'