Add help and listcommand logic

Both `!help` and `!listcommand` are handled by the bot itself, so take
advantage of how the addons method calls work to implement both
`matrix_command` and `help_command` on `self`.

Add `help_command` to each currently enabled and functional addon
This commit is contained in:
Bill Niblock 2021-02-14 22:37:56 -05:00
parent 0e942a58c1
commit 69a22c8a22
5 changed files with 132 additions and 2 deletions

View file

@ -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!`)

View file

@ -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 = {}

View file

@ -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

View file

@ -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

View file

@ -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'