lita-discord_oauth 0.1.2.alpha.51 → 0.1.2.alpha.52

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 93042e81bb196d3eef785859717aeefe1b33824c
4
- data.tar.gz: aeb1ce23ee900334b0084fdb11ec21bba1d06905
3
+ metadata.gz: 7f951a4406d047cccaf7fb9ae7591e0ed972fc6d
4
+ data.tar.gz: 681388281dca1c5a79ec6eb617aed9818d41a130
5
5
  SHA512:
6
- metadata.gz: 571d240338b8d761d9bea62879f60c05e6b1a4b37231a7da291f9d4fa981faccdce18748ecf4746d5cdd60c3f915f1fc4f9c8aefd0f7386ca3e372fdf342e6c2
7
- data.tar.gz: cf2597d8b0aeaf19c1effc18f9e0956a1b2b746da4f514d428ee226f4e9a6329b35e0086943188309903599b74746fc560d81c0e65f2be441be6c3950bccb34b
6
+ metadata.gz: 582348fc7c29b70bd096cab81fc45536405c42d71bf4c64f414e7ea580458dde1ab92160f440d68a4ca9d15ed8adb4ee98313bc18031f34d1a5c3d8d5cd8a67c
7
+ data.tar.gz: 7a8d990c6a9b053e53d28a5c04ca97f51f9c9ff11324e198a3072f72343f5e55b721185f60c25ae8d85ced9cc1be3d47d7d01cba1dc62ddcfa889be5017ac235
@@ -0,0 +1,99 @@
1
+ module Lita
2
+ # A namespace to hold all subclasses of {Handler}.
3
+ module Handlers
4
+ # Provides online help about Lita commands for users.
5
+ class DiscordHelp < Handler
6
+ route(/^help\s*(.+)?/, :help, command: true, help: {
7
+ "help" => t("help.help_value"),
8
+ t("help.help_command_key") => t("help.help_command_value")
9
+ })
10
+
11
+ # Outputs help information about Lita commands.
12
+ # @param response [Lita::Response] The response object.
13
+ # @return [void]
14
+ def help(response)
15
+ output = build_help(response)
16
+ output = filter_help(output, response)
17
+ response.reply_privately output.join("\n")
18
+ end
19
+
20
+ private
21
+
22
+ def table_row(key, value)
23
+ key_width = 25
24
+ value_width = 112
25
+
26
+ key_text = key.ljust(key_width, ' ')
27
+
28
+ value_words = value.split(' ')
29
+
30
+ value_text = ''
31
+ value_line = ''.ljust(key_width + 1, ' ')
32
+ value_words.each do |word|
33
+ new_value_line = "#{value_line} #{word}"
34
+
35
+ if new_value_line.length > value_width
36
+ value_text += "#{value_line}\n" + ''.ljust(key_width + 1, ' ')
37
+ value_line = word
38
+ else
39
+ value_line = "#{value_line} #{word}"
40
+ end
41
+
42
+ end
43
+
44
+ if value_line.length > 0
45
+ value_text = "#{value_text}\n" + ''.ljust(key_width + 1, ' ') + "#{value_line}"
46
+ end
47
+
48
+ key_text + value_text
49
+ end
50
+
51
+ # Checks if the user is authorized to at least one of the given groups.
52
+ def authorized?(user, required_groups)
53
+ required_groups.nil? || required_groups.any? do |group|
54
+ robot.auth.user_in_group?(user, group)
55
+ end
56
+ end
57
+
58
+ # Creates an array of help info for all registered routes.
59
+ def build_help(response)
60
+ robot.handlers.map do |handler|
61
+ next unless handler.respond_to?(:routes)
62
+
63
+ handler.routes.map do |route|
64
+ route.help.map do |command, description|
65
+ if authorized?(response.user, route.required_groups)
66
+ help_command(route, command, description)
67
+ end
68
+ end
69
+ end
70
+ end.flatten.compact
71
+ end
72
+
73
+ # Filters the help output by an optional command.
74
+ def filter_help(output, response)
75
+ filter = response.matches[0][0]
76
+
77
+ if filter
78
+ output.select { |line| /(?:@?#{name}[:,]?)?#{filter}/i === line }
79
+ else
80
+ output
81
+ end
82
+ end
83
+
84
+ # Formats an individual command's help message.
85
+ def help_command(route, command, description)
86
+ command = "#{name}: #{command}" if route.command?
87
+
88
+ table_row(command, description)
89
+ end
90
+
91
+ # The way the bot should be addressed in order to trigger a command.
92
+ def name
93
+ robot.config.robot.mention_name || robot.config.robot.name
94
+ end
95
+ end
96
+
97
+ Lita.register_handler(DiscordHelp)
98
+ end
99
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-discord_oauth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2.alpha.51
4
+ version: 0.1.2.alpha.52
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cas Eliëns
@@ -123,6 +123,7 @@ files:
123
123
  - Rakefile
124
124
  - lib/lita-discord_oauth.rb
125
125
  - lib/lita/adapters/discord_oauth.rb
126
+ - lib/lita/handlers/discord_help.rb
126
127
  - lita-discord_oauth.gemspec
127
128
  - locales/en.yml
128
129
  - spec/lita/adapters/discord_oauth_spec.rb