Parcourir la source

Merge branch 'help_command' of PMD/rotom_bot into master

kjswis il y a 6 ans
Parent
commit
440f7f6a17
3 fichiers modifiés avec 71 ajouts et 6 suppressions
  1. 12 2
      app/models/commands.rb
  2. 35 0
      app/responses/help.rb
  3. 24 4
      bot.rb

+ 12 - 2
app/models/commands.rb

@@ -1,13 +1,23 @@
 require 'optparse'
 
 class Command
-  attr_reader :name, :options_parser, :operation
+  attr_reader :name, :operation
 
-  def initialize(name, options ={}, &block)
+  def initialize(name, description, options =[], &block)
     @name = name
+    @description = description
+    @options = options
     @operation = block
   end
 
+  def description
+    @description
+  end
+
+  def options
+    @options
+  end
+
   def call(message_str, event=nil)
     match = /pkmn-\w+\s?(.*)/.match(message_str)
     args = match[1]

+ 35 - 0
app/responses/help.rb

@@ -0,0 +1,35 @@
+def all_commands(commands)
+  fields = []
+
+  commands.each do |command|
+    fields.push({name: "pkmn-#{command.name}", value: command.description})
+  end
+
+  Embed.new(
+    title: "Commands",
+    description: "To learn more about any of the listed commands, use `pkmn-help [command]`",
+    color: "#73FE49",
+    fields: fields
+  )
+end
+
+def command_usage(command)
+  fields = []
+
+  unless command.options.empty?
+    usage = "```\n"
+    command.options.each do |option|
+      usage += "pkmn-#{command.name} #{option}\n"
+    end
+    usage += "```"
+  end
+
+  fields.push({name: "Usage", value: usage}) if command.options
+
+  Embed.new(
+    title: "pkmn-#{command.name}",
+    description: command.description,
+    color: "#73fe49",
+    fields: fields
+  )
+end

+ 24 - 4
bot.rb

@@ -59,8 +59,9 @@ bot = Discordrb::Bot.new(token: token)
 # ---
 
 # Commands: structure basic bot commands here
+commands = []
 
-hello = Command.new(:hello) do |event|
+hello = Command.new(:hello, "Says hello!\nGreat for testing if the bot is responsive") do |event|
   user = event.author.nickname || event.author.name
 
   greetings = [
@@ -80,7 +81,25 @@ hello = Command.new(:hello) do |event|
   )
 end
 
-matchup = Command.new(:matchup) do |event, type|
+help = Command.new(:help, "Displays help information for the commands", [nil, :command]) do |event, command|
+  if (cmd = /pkmn-(\w+)/.match(command))
+    command = cmd[1]
+  end
+
+  if command
+    if cmd = commands.detect { |c| c.name == command.to_sym }
+      embed = command_usage(cmd)
+      event.send_embed("", embed)
+    else
+      event.respond("I don't know this command!")
+    end
+  else
+    embed = all_commands(commands)
+    event.send_embed("", embed)
+  end
+end
+
+matchup = Command.new(:matchup, "Displays a chart of effectiveness for the given type", [:type]) do |event, type|
   channel = event.channel.id
   file = "images/Type #{type.capitalize}.png"
 
@@ -91,7 +110,7 @@ matchup = Command.new(:matchup) do |event, type|
   end
 end
 
-app = Command.new(:app) do |event, name|
+app = Command.new(:app, "Gives the user links for starting or editing character applications", [nil, :name]) do |event, name|
   user = event.author
   user_channel = event.author.dm
 
@@ -115,7 +134,8 @@ end
 commands = [
   hello,
   matchup,
-  app
+  app,
+  help
 ]
 
 # This will trigger on every message sent in discord