Update help_command logic

The `help_command` method for each addon/the bot were not returning the
help message, but were instead sending the help messages themselves.
Update the logic to simply return the help message string.
This commit is contained in:
Bill Niblock 2021-02-16 23:13:46 -05:00
parent 69a22c8a22
commit 22c9590816
4 changed files with 41 additions and 55 deletions

View file

@ -32,22 +32,16 @@ module Chronicle
pfx = @bot.cmd_prefix pfx = @bot.cmd_prefix
cmd = message.content[:body].split(/\s+/)[1].gsub(/#{pfx}/, '') cmd = message.content[:body].split(/\s+/)[1].gsub(/#{pfx}/, '')
res = 'Invalid command'
case cmd case cmd
when "addcommand" when "addcommand"
res = cmd_add_usage cmd_add_usage
when "modcommand" when "modcommand"
res = cmd_mod_usage cmd_mod_usage
when "remcommand" when "remcommand"
res = cmd_rem_usage cmd_rem_usage
else else
res = cmd_custom_usage(cmd) cmd_custom_usage(cmd)
end end
room = @bot.client.ensure_room(message.room_id)
room.send_notice(res)
end end
# Handle a command from the Matrix protocol # Handle a command from the Matrix protocol

View file

@ -17,15 +17,10 @@ module Chronicle
# Provide help information for this command # Provide help information for this command
# #
# @param message [Message object] The relevant message object # @param message [Message object] The relevant message object (Unused)
def help_command(message) def help_command(_)
room = @bot.client.ensure_room(message.room_id) 'Rolls dice, and provides a result' \
"\nUsage: !roll DICE\nExample: !roll 3d6"
res = 'Rolls dice, and provides a result'
res += "\nUsage: !roll DICE"
res += "\nExample: !roll 3d6"
room.send_notice(res)
end end
# Handle a command from the Matrix protocol # Handle a command from the Matrix protocol

View file

@ -17,14 +17,10 @@ module Chronicle
# Provide help information for this command # Provide help information for this command
# #
# @param message [Message object] The relevant message object # @param message [Message object] The relevant message object (Unused)
def help_command(message) def help_command(_)
room = @bot.client.ensure_room(message.room_id) 'Returns "Pong!"' \
"\nUsage: !ping"
res = 'Returns "Pong!"'
res += "\nUsage: !ping"
room.send_notice(res)
end end
# Handle a command from the Matrix protocol # Handle a command from the Matrix protocol
@ -52,14 +48,10 @@ module Chronicle
# Provide help information for this command # Provide help information for this command
# #
# @param message [Message object] The relevant message object # @param message [Message object] The relevant message object (Unused)
def help_command(message) def help_command(_)
room = @bot.client.ensure_room(message.room_id) 'Questions the fates to determine an answer for your question.' \
"\nUsage: !8ball QUESTION"
res = 'Questions the fates to determine an answer for your question.'
res += "\nUsage: !8ball QUESTION"
room.send_notice(res)
end end
# Handle a command from the Matrix protocol # Handle a command from the Matrix protocol

View file

@ -31,18 +31,25 @@ module Chronicle
# Chronicle Bot for Matrix # Chronicle Bot for Matrix
module Matrix module Matrix
# Begin the beast # Begin the beast
def self.start(args) def self.start
unless args.length >= 2 unless ENV["CHRONICLE_HOMESERVER"]
raise "Usage: #{$PROGRAM_NAME} [-d] homeserver_url access_token" raise "Export your homeserver URL to CHRONICLE_HOMESERVER"
end end
if args.first == '-d' unless ENV["CHRONICLE_ACCESS_TOKEN"]
raise "Export your access token to CHRONICLE_ACCESS_TOKEN"
end
if ENV["CHRONICLE_DEBUG"]
Thread.abort_on_exception = true Thread.abort_on_exception = true
MatrixSdk.debug! MatrixSdk.debug!
args.shift
end end
bot = ChronicleBot.new args[0], args[1] bot = ChronicleBot.new(
ENV["CHRONICLE_HOMESERVER"],
ENV["CHRONICLE_ACCESS_TOKEN"]
)
bot.run bot.run
end end
@ -59,7 +66,7 @@ module Chronicle
@allowed_commands = {} @allowed_commands = {}
register_commands register_commands
available_commands(self, ['listcommands', 'help']) available_commands(self, %w[listcommands help])
end end
# All available commands # All available commands
@ -93,12 +100,10 @@ module Chronicle
case cmd case cmd
when 'listcommands' when 'listcommands'
res = '!listcommands: List available commands managed by this bot' '!listcommands: List available commands managed by this bot'
else else
res = 'Try !listcommands or !help' 'Try !listcommands or !help'
end end
res
end end
# Handle a command from the Matrix protocol # Handle a command from the Matrix protocol
@ -110,19 +115,19 @@ module Chronicle
res = 'Invalid command' res = 'Invalid command'
case cmd res = case cmd
when 'listcommands' when 'listcommands'
res = 'Currently available commands: ' "Currently available commands: #{@all_commands.keys.sort.join(', ')}"
res += @all_commands.keys.join(', ') when 'help'
when 'help' if message.content[:body].split(/\s+/).count <= 1
res = if message.content[:body].split(/\s+/).count <= 1
'!help: Get help for a specific command' \ '!help: Get help for a specific command' \
"\nUsage: !help COMMAND" "\nUsage: !help COMMAND"
else else
second_cmd = message.content[:body].split(/\s+/)[1].gsub(/#{pfx}/, '') second_cmd = message.content[:body].split(/\s+/)[1]
res = @all_commands[second_cmd.strip].help_command(message) .gsub(/#{pfx}/, '')
@all_commands[second_cmd.strip].help_command(message)
end end
end end
room = @client.ensure_room(message.room_id) room = @client.ensure_room(message.room_id)
room.send_notice(res) room.send_notice(res)