help.rb 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. require './app/commands/base_command.rb'
  2. class HelpCommand < BaseCommand
  3. def self.opts
  4. {
  5. usage: {
  6. command: "Searches for a command based on its name, the part that " +
  7. "comes after `pkmn-`. If no command is given, R0ry will list available commands"
  8. }
  9. }
  10. end
  11. def self.cmd
  12. desc = "Displays helpful information for R0ry's commands"
  13. @cmd ||= Command.new(:help, desc, opts) do |event, command|
  14. # Short name for the command requested, if exists
  15. cmd_name = command.match(/(pkmn-)?(\w+)/)[2] if command
  16. all_cmds = BaseCommand.descendants
  17. # --Execution--
  18. # When a command is specified
  19. case command
  20. when /admin/i && event.channel.id == ENV['ADMIN_CH']
  21. when nil
  22. # List of commands, by restrictions: server, and pm
  23. server_commands = all_cmds.filter{ |bc| bc.restricted_to == nil }
  24. pm_commands = all_cmds.filter{ |bc| bc.restricted_to == :pm }
  25. reply = []
  26. reply.push(BotResponse.new(embed: command_list_embed(pm_commands, :pm)))
  27. reply.push(BotResponse.new(embed: command_list_embed(server_commands)))
  28. reply
  29. else
  30. # Find the command, and display
  31. cmd = all_cmds.find { |bc| bc.name == cmd_name.to_sym }
  32. command_help(cmd, event)
  33. end
  34. rescue
  35. command_error("Command not found!", HelpCommand)
  36. end
  37. end
  38. def self.example_command(event=nil)
  39. case ['', 'command'].sample
  40. when ''
  41. []
  42. when 'command'
  43. [BaseCommand.descendants.sample.name]
  44. end
  45. end
  46. end