Class: Waylon::Skills::Help

Inherits:
Waylon::Skill show all
Defined in:
lib/waylon/skills/help.rb

Overview

A Skill for providing help

Instance Attribute Summary

Attributes inherited from Waylon::Skill

#request, #route, #sense, #tokens

Instance Method Summary collapse

Methods inherited from Waylon::Skill

#acknowledgement, #codify, config_namespace, #details, #initialize, #mention, #message, #named_tokens, perform, queue, #react, #reply, #reply_with_blocks, route, #threaded_reply

Methods included from BaseComponent

included

Constructor Details

This class inherits a constructor from Waylon::Skill

Instance Method Details

#build_header_block(skill, action) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/waylon/skills/help.rb', line 123

def build_header_block(skill, action)
  text = if action
           "Help for #{skill}##{action}:"
         else
           "Help for #{skill}:"
         end
  { type: "header", text: { type: "plain_text", text:, emoji: true } }
end

#build_help_block(this_route) ⇒ Object



132
133
134
135
136
137
138
139
140
# File 'lib/waylon/skills/help.rb', line 132

def build_help_block(this_route)
  {
    type: "section",
    text: {
      type: "mrkdwn",
      text: "  *#{this_route[:name]}*\n#{build_help_text(this_route)}"
    }
  }
end

#build_help_text(this_route) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/waylon/skills/help.rb', line 142

def build_help_text(this_route)
  route_help_text = []
  case this_route[:help]
  when String
    route_help_text << "    *Usage:* #{this_route[:help]}"
  when Hash
    route_help_text << "    *Usage:* #{this_route.dig(:help, :usage)}"
    if this_route.dig(:help, :description)
      route_help_text << "\n    *Description:* #{this_route.dig(:help, :description)}"
    end
  end
  route_help_text.join
end

#helpObject

Responds to “help” requests



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/waylon/skills/help.rb', line 19

def help
  skill = named_tokens[:skill]
  action = named_tokens[:action]

  react :book

  immediate_responses = [
    "I'll send you a DM to go over that with you.",
    "I'll DM you the details.",
    "Look for a private message with those details.",
    "You should have a private message with that information shortly."
  ]

  # Only send this if you aren't already in a DM
  threaded_reply "#{acknowledgement} #{immediate_responses.sample}" unless message.private?

  if sense.supports?(:blocks)
    reply_with_blocks(help_blocks(skill, action), private: true)
  else
    reply(help_text(skill, action), private: true)
  end
end

#help_blocks(skill = nil, action = nil) ⇒ Object

rubocop:disable Metrics/AbcSize



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/waylon/skills/help.rb', line 64

def help_blocks(skill = nil, action = nil) # rubocop:disable Metrics/AbcSize
  allowed_routes = SkillRegistry.instance.help(message.author)
  if skill
    if action
      this_route = allowed_routes[skill].find do |r|
        r[:name].to_s == "#{skill}##{action}"
      end

      return not_found_block(skill, action) unless this_route

      [build_header_block(skill, action), build_help_block(this_route)]
    else
      routes = allowed_routes[skill]
      return not_found_block(skill, nil) unless routes

      resp = [build_header_block(skill, nil)]
      routes.each { |r| resp << build_help_block(r) }
      resp
    end
  else
    allowed_routes = SkillRegistry.instance.help(message.author)
    help_blocks_for_all(allowed_routes)
  end
end

#help_blocks_for_all(routes) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/waylon/skills/help.rb', line 89

def help_blocks_for_all(routes)
  resp = [
    { type: "header", text: { type: "plain_text", text: "All known actions:", emoji: true } }
  ]

  routes.each do |k, v|
    resp += [
      { type: "divider" },
      {
        type: "section",
        text: {
          type: "mrkdwn",
          text: "*Actions for '#{k}':*"
        }
      }
    ]
    v.each { |r| resp << build_help_block(r) }
  end
  resp
end

#help_text(skill = nil, action = nil) ⇒ Object

rubocop:disable Metrics/AbcSize



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/waylon/skills/help.rb', line 42

def help_text(skill = nil, action = nil) # rubocop:disable Metrics/AbcSize
  allowed_routes = SkillRegistry.instance.help(message.author)
  resp = []
  if skill
    if action
      resp << "## Help for #{skill}##{action}:"
      this_route = allowed_routes[skill].find do |r|
        r[:name].to_s == "#{skill}##{action}"
      end
      return "I couldn't find #{action} on #{skill}..." unless this_route

      resp << build_help_text(this_route)
    else
      resp << "## Help for #{skill}:\n"
      routes = allowed_routes[skill]
      (routes || []).each { |r| resp << build_help_text(r) }
    end
  else
    help_text_for_all(allowed_routes)
  end
end

#help_text_for_all(routes) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/waylon/skills/help.rb', line 110

def help_text_for_all(routes)
  resp = []
  resp << "*All known actions:*\n"
  routes.each do |k, v|
    resp << "* *#{k}*:"
    v.each do |r|
      resp << build_help_text(r)
    end
    resp << " --- "
  end
  resp.join("\n")
end

#not_found_block(skill, action) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/waylon/skills/help.rb', line 156

def not_found_block(skill, action)
  sentence = if action
               "I couldn't find any '#{action}' action on the '#{skill}' skill..."
             else
               "I couldn't find any routes related to a '#{skill}' skill..."
             end
  [
    {
      type: "section",
      text: {
        type: "mrkdwn",
        text: sentence
      }
    }
  ]
end