help.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. HELP_BLUE = "#4976ca"
  2. def command_list_embed(commands, restriction = nil)
  3. fields = []
  4. desc = "To learn more about any of the listed commands," +
  5. " use `pkmn-help [command]`"
  6. commands.each do |cmd|
  7. fields.push({name: "pkmn-#{cmd.name}", value: cmd.cmd.description})
  8. end
  9. case restriction
  10. when :pm
  11. title = "PM Commands"
  12. desc = "Can only be used in a PM with <@639191920786276378> \n" +
  13. "To learn more about any of the listed commands, use `pkmn-help [command]`"
  14. else
  15. end
  16. Embed.new(
  17. title: title || "Commands",
  18. description: desc,
  19. color: HELP_BLUE,
  20. fields: fields
  21. )
  22. end
  23. def command_help(command, event)
  24. embed = Embed.new(
  25. title: command.to_s.gsub('Command', ' Command'),
  26. description: command.cmd.description,
  27. color: HELP_BLUE,
  28. footer: { text: "For more help, ask a Guild Master!" }
  29. )
  30. # Apply options, if there are any
  31. fields = command_usage(command, event) unless command.cmd.options.empty?
  32. # If in the admin channel, show additional admin options
  33. fields = admin_options(command, fields) if event.channel.id == ENV['ADMIN_CH'].to_i
  34. # Apply fields and return
  35. embed.fields = fields
  36. embed
  37. end
  38. def command_usage(command, event)
  39. # save the options, to shorten the logic later
  40. opts = command.cmd.options
  41. example = command.example_command(event)
  42. fields = []
  43. # Add navigation information, if there is any
  44. fields.push({
  45. name: "Reaction Navigation",
  46. value: opts[:nav].map{ |k,v| "#{v[0]} - `#{k}` - #{v[1]}" }.join("\n")
  47. }) if opts[:nav]
  48. # Save the command with its options, and the option instructions
  49. structure = "```pkmn-#{command.name} #{opts[:usage].map{ |k,v| k }.join(' | ')}```"
  50. instructions = opts[:usage].map{ |k,v| "- `#{k}` -- #{v}" }.join("\n")
  51. # Add usage information
  52. fields.push({
  53. name: "Command Usage",
  54. value: "#{structure}\n#{instructions}"
  55. })
  56. # Add an example command
  57. fields.push({
  58. name: "Example",
  59. value: "```pkmn-#{command.name} #{example.join(' | ')}```"
  60. })
  61. end
  62. def admin_options(command, fields)
  63. # Find the admin options, and return if there are none
  64. r_opts = command.admin_opts
  65. return fields unless r_opts
  66. # Save the command with its options, and the option instructions
  67. structure = "```pkmn-#{command.name} #{r_opts[:usage].map{ |k,v| k }.join(' | ')}```"
  68. instructions = r_opts[:usage].map{ |k,v| "- `#{k}` -- #{v}" }.join("\n")
  69. # Add usage information
  70. fields.push({
  71. name: "Administrator Options",
  72. value: "#{structure}\n#{instructions}"
  73. })
  74. fields
  75. end
  76. def command_error(title, command, event=nil)
  77. embed = Embed.new(
  78. title: title,
  79. description: command.cmd.description,
  80. color: ERROR_RED,
  81. footer: { text: "For more help, ask a Guild Master!" }
  82. )
  83. # Apply options, if there are any
  84. fields = command_usage(command, event) unless command.cmd.options.empty?
  85. # If in the admin channel, show additional admin options
  86. fields = admin_options(command, fields) if event&.channel&.id == ENV['ADMIN_CH'].to_i
  87. # Apply fields and return
  88. embed.fields = fields
  89. embed
  90. end