help.rb 3.2 KB

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