lita-discord_oauth 1.0.0 → 1.1.0.alpha.100

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,45 +1,45 @@
1
- # lita-discord
2
-
3
- [![Build Status][travis-img]][travis-url]
4
- [![Gem Version][gem-img]][gem-url]
5
- [![Gem Downloads][gem-dl-img]][gem-url]
6
- [![Dependency status][gem-dep-img]][gem-dep-url]
7
- [![Codacy Badge][codacy-img]][codacy-url]
8
-
9
- Discord adapter for Lita, using OAuth tokens.
10
-
11
- ## Installation
12
-
13
- Add lita-discord to your Lita instance's Gemfile:
14
-
15
- ``` ruby
16
- gem "lita-discord_oauth"
17
- ```
18
-
19
- ## Configuration
20
-
21
- The adapter exposes two configuration settings:
22
-
23
- * `config.adapters.discord_oauth.token = ''`
24
- Bot account token
25
- * `config.adapters.discord_oauth.client = ''`
26
- Bot client ID
27
-
28
- You can get both these values from [this page](https://discordapp.com/developers/applications/me) - Make sure that the application is a bot user!
29
-
30
- ![bot user](https://ducohosting.com/screenshots/isaac28fe06147f6a73a1a654433cf2ef3d37.png)
31
-
32
- ## Usage
33
-
34
- This bot includes a custom help command. You must disable the built-in help command to prevent double output.
35
-
36
-
37
- [travis-img]: https://img.shields.io/travis/cascer1/lita-discord_oauth.svg
38
- [travis-url]: https://travis-ci.org/cascer1/lita-discord_oauth
39
- [gem-img]: https://img.shields.io/gem/v/lita-discord_oauth.svg
40
- [gem-url]: https://badge.fury.io/rb/lita-discord_oauth
41
- [gem-dl-img]: https://img.shields.io/gem/dv/lita-discord_oauth/stable.svg
42
- [gem-dep-img]: https://img.shields.io/gemnasium/cascer1/lita-discord_oauth.svg
43
- [gem-dep-url]: https://gemnasium.com/github.com/cascer1/lita-discord_oauth
44
- [codacy-img]: https://img.shields.io/codacy/grade/a66b558f1d7a4fd89673e5514e437493.svg
1
+ # lita-discord
2
+
3
+ [![Build Status][travis-img]][travis-url]
4
+ [![Gem Version][gem-img]][gem-url]
5
+ [![Gem Downloads][gem-dl-img]][gem-url]
6
+ [![Dependency status][gem-dep-img]][gem-dep-url]
7
+ [![Codacy Badge][codacy-img]][codacy-url]
8
+
9
+ Discord adapter for Lita, using OAuth tokens.
10
+
11
+ ## Installation
12
+
13
+ Add lita-discord to your Lita instance's Gemfile:
14
+
15
+ ``` ruby
16
+ gem "lita-discord_oauth"
17
+ ```
18
+
19
+ ## Configuration
20
+
21
+ The adapter exposes two configuration settings:
22
+
23
+ * `config.adapters.discord_oauth.token = ''`
24
+ Bot account token
25
+ * `config.adapters.discord_oauth.client = ''`
26
+ Bot client ID
27
+
28
+ You can get both these values from [this page](https://discordapp.com/developers/applications/me) - Make sure that the application is a bot user!
29
+
30
+ ![bot user](https://ducohosting.com/screenshots/isaac28fe06147f6a73a1a654433cf2ef3d37.png)
31
+
32
+ ## Usage
33
+
34
+ This bot overrides the default help provider
35
+
36
+
37
+ [travis-img]: https://img.shields.io/travis/cascer1/lita-discord_oauth.svg
38
+ [travis-url]: https://travis-ci.org/cascer1/lita-discord_oauth
39
+ [gem-img]: https://img.shields.io/gem/v/lita-discord_oauth.svg
40
+ [gem-url]: https://badge.fury.io/rb/lita-discord_oauth
41
+ [gem-dl-img]: https://img.shields.io/gem/dv/lita-discord_oauth/stable.svg
42
+ [gem-dep-img]: https://img.shields.io/gemnasium/cascer1/lita-discord_oauth.svg
43
+ [gem-dep-url]: https://gemnasium.com/github.com/cascer1/lita-discord_oauth
44
+ [codacy-img]: https://img.shields.io/codacy/grade/a66b558f1d7a4fd89673e5514e437493.svg
45
45
  [codacy-url]: https://www.codacy.com/app/cascer1/lita-discord_oauth?utm_source=github.com&utm_medium=referral&utm_content=cascer1/lita-discord_oauth&utm_campaign=Badge_Grade
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
- require 'bundler/gem_tasks'
2
- require 'rspec/core/rake_task'
3
-
4
- RSpec::Core::RakeTask.new(:spec)
5
-
6
- task default: :spec
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -1,99 +1,99 @@
1
- require 'discordrb'
2
-
3
- module Lita
4
- module Adapters
5
- class Discord_oauth < Adapter
6
- config :token, type: String, required: true
7
- config :client, type: String, required: true
8
-
9
- def initialize(robot)
10
- super
11
- @client = ::Discordrb::Bot.new token: config.token, client_id: config.client
12
- end
13
-
14
- def run
15
- Lita.logger.debug('Starting discord_oauth adapter')
16
- @client.ready do |e|
17
- robot.trigger(:connected)
18
-
19
- version = Gem.loaded_specs['lita-discord_oauth'].version
20
-
21
- @client.game = "Version #{version}"
22
-
23
- @client.message do |event|
24
- message = event.message
25
- channel = event.channel.id.to_s
26
- author_id = message.author.id.to_s
27
-
28
- if event.channel.pm?
29
- # We're dealing with a PM
30
- author_name = message.author.username.to_s
31
- else
32
- # We're dealing with a regular (server) message
33
- author_name = message.author.display_name.to_s
34
- end
35
-
36
-
37
-
38
- Lita.logger.debug("Received message from #{author_name}(#{author_id}): #{message.content}")
39
- Lita.logger.debug("Finding user #{author_name}")
40
-
41
- user = Lita::User.find_by_name(author_name)
42
-
43
- if user == nil
44
- Lita.logger.debug("User #{author_name} not found, trying ID #{author_id}")
45
- user = Lita::User.find_by_id(author_id)
46
-
47
- if user != nil
48
- Lita.logger.debug("User #{author_id} found, updating name to #{author_name}")
49
- user = Lita::User.create(author_id, {name: author_name})
50
- end
51
-
52
- Lita.logger.debug("User #{author_id} not found, creating now")
53
- user = Lita::User.create(author_id, {name: author_name}) unless user
54
- end
55
-
56
- Lita.logger.debug('User ID: ' + user.id)
57
- Lita.logger.debug('Channel ID: ' + channel)
58
-
59
- source = Lita::Source.new(user: user, room: channel)
60
- msg = Lita::Message.new(robot, message.content, source)
61
-
62
- robot.receive(msg) unless message.from_bot?
63
-
64
- end
65
- end
66
-
67
-
68
- @client.run
69
- end
70
-
71
- def shut_down
72
- @client.stop
73
- end
74
-
75
- def send_messages(target, messages)
76
- Lita.logger.debug("Sending message to user #{target.user.id} in channel #{target.room}")
77
-
78
- mention = @client.user(target.user.id).mention
79
-
80
- messages.each do |message|
81
- if mention
82
- no_mention = message.slice!('|||NOMENTION|||')
83
-
84
- message = mention + ",\n" + message if no_mention != '|||NOMENTION|||'
85
-
86
-
87
-
88
- Lita.logger.info('Message is too long') if message.length > 2000
89
-
90
- @client.send_message(target.room, message)
91
- end
92
- end
93
- end
94
-
95
- end
96
-
97
- Lita.register_adapter(:discord_oauth, Discord_oauth)
98
- end
99
- end
1
+ require 'discordrb'
2
+
3
+ module Lita
4
+ module Adapters
5
+ class Discord_oauth < Adapter
6
+ config :token, type: String, required: true
7
+ config :client, type: String, required: true
8
+
9
+ def initialize(robot)
10
+ super
11
+ @client = ::Discordrb::Bot.new token: config.token, client_id: config.client
12
+ end
13
+
14
+ def run
15
+ Lita.logger.debug('Starting discord_oauth adapter')
16
+ @client.ready do |e|
17
+ robot.trigger(:connected)
18
+
19
+ version = Gem.loaded_specs['lita-discord_oauth'].version
20
+
21
+ @client.game = "Version #{version}"
22
+
23
+ @client.message do |event|
24
+ message = event.message
25
+ channel = event.channel.id.to_s
26
+ author_id = message.author.id.to_s
27
+
28
+ if event.channel.pm?
29
+ # We're dealing with a PM
30
+ author_name = message.author.username.to_s
31
+ else
32
+ # We're dealing with a regular (server) message
33
+ author_name = message.author.display_name.to_s
34
+ end
35
+
36
+
37
+
38
+ Lita.logger.debug("Received message from #{author_name}(#{author_id}): #{message.content}")
39
+ Lita.logger.debug("Finding user #{author_name}")
40
+
41
+ user = Lita::User.find_by_name(author_name)
42
+
43
+ if user == nil
44
+ Lita.logger.debug("User #{author_name} not found, trying ID #{author_id}")
45
+ user = Lita::User.find_by_id(author_id)
46
+
47
+ if user != nil
48
+ Lita.logger.debug("User #{author_id} found, updating name to #{author_name}")
49
+ user = Lita::User.create(author_id, {name: author_name})
50
+ end
51
+
52
+ Lita.logger.debug("User #{author_id} not found, creating now")
53
+ user = Lita::User.create(author_id, {name: author_name}) unless user
54
+ end
55
+
56
+ Lita.logger.debug('User ID: ' + user.id)
57
+ Lita.logger.debug('Channel ID: ' + channel)
58
+
59
+ source = Lita::Source.new(user: user, room: channel)
60
+ msg = Lita::Message.new(robot, message.content, source)
61
+
62
+ robot.receive(msg) unless message.from_bot?
63
+
64
+ end
65
+ end
66
+
67
+
68
+ @client.run
69
+ end
70
+
71
+ def shut_down
72
+ @client.stop
73
+ end
74
+
75
+ def send_messages(target, messages)
76
+ Lita.logger.debug("Sending message to user #{target.user.id} in channel #{target.room}")
77
+
78
+ mention = @client.user(target.user.id).mention
79
+
80
+ messages.each do |message|
81
+ if mention
82
+ no_mention = message.slice!('|||NOMENTION|||')
83
+
84
+ message = mention + ",\n" + message if no_mention != '|||NOMENTION|||'
85
+
86
+
87
+
88
+ Lita.logger.info('Message is too long') if message.length > 2000
89
+
90
+ @client.send_message(target.room, message)
91
+ end
92
+ end
93
+ end
94
+
95
+ end
96
+
97
+ Lita.register_adapter(:discord_oauth, Discord_oauth)
98
+ end
99
+ end
@@ -1,169 +1,169 @@
1
- require 'lita/handler/chat_router'
2
-
3
- module Lita
4
- # A namespace to hold all subclasses of {Handler}.
5
- module Handlers
6
- # Provides online help about Lita commands for users.
7
- class DiscordHelp
8
- extend Lita::Handler::ChatRouter
9
-
10
- route(/^help\s*(.+)?/, :help, command: true, help: {
11
- 'help' => t('help.help_value'),
12
- t('help.help_command_key') => t('help.help_command_value')
13
- })
14
-
15
- # Outputs help information about Lita commands.
16
- # @param response [Lita::Response] The response object.
17
- # @return [void]
18
- def help(response)
19
- output = build_help(response)
20
- output = filter_help(output, response)
21
- first_message = true
22
-
23
- output = output.join("\n")
24
-
25
- messages = split_message(output)
26
-
27
- Lita.logger.debug(messages)
28
-
29
- messages.each { |message|
30
- if first_message
31
- response.reply_privately("```\n#{message}\n```")
32
- first_message = false
33
- else
34
- response.reply_privately("|||NOMENTION|||```\n#{message}\n```")
35
- end
36
- }
37
-
38
- if messages.length > 1
39
- response.reply_privately('Sorry for splitting that up in multiple messages, Discord doesn\'t allow me to send responses longer than 2000 characters :(')
40
- end
41
-
42
- end
43
-
44
- private
45
-
46
- def split_message(message)
47
- max_length = 2000 - 30 # Substract 25 for mention safety
48
- messages = Array.new
49
-
50
- if message.length < max_length
51
- messages.push(message)
52
- else
53
- message_copy = message
54
-
55
- loop do
56
- Lita.logger.debug("Message copy: #{message_copy}")
57
- # part = 'Lorem ipsum, dolor sit amet' # I don't know why we need this, but we do
58
-
59
- part = get_message_part(message_copy, max_length)
60
-
61
- Lita.logger.debug("Part: #{part}")
62
- part = message_copy.slice!(part)
63
- messages.push(part)
64
-
65
- break if message_copy.length <= 0
66
- end
67
-
68
- end
69
-
70
- messages
71
- end
72
-
73
- def get_message_part(message, limit)
74
- Lita.logger.debug("Getting message part from #{message}")
75
-
76
- Lita.logger.debug('Message was short enough, returning without modifications') if message.length <= limit
77
- return message if message.length <= limit # No need to check anything if the message is short enough
78
-
79
- part = message.to_s[0...limit - 1]
80
- break_index = part.rindex("\n")
81
-
82
- Lita.logger.debug("Break index: #{break_index}")
83
-
84
- if break_index != nil && break_index != 0
85
- message[0, break_index]
86
- else
87
- message
88
- end
89
- end
90
-
91
- def table_row(key, value)
92
- key_width = 30
93
- value_width = 107
94
-
95
- key_text = key.ljust(key_width, ' ')
96
-
97
- value_words = value.split(' ')
98
-
99
- value_text = ''
100
- value_line = ''
101
-
102
- value_words.each do |word|
103
- new_value_line = "#{value_line} #{word}"
104
-
105
- if new_value_line.length > value_width
106
- value_text += "#{value_line}\n" + ''.ljust(key_width + 1, ' ')
107
- value_line = word
108
- else
109
- value_line = "#{value_line} #{word}"
110
- end
111
-
112
- end
113
-
114
- if value_line.length > 0
115
- value_text = "#{value_text}\n" + ''.ljust(key_width + 1, ' ') + "#{value_line}"
116
- end
117
-
118
- key_text + value_text + "\n"
119
- end
120
-
121
- # Checks if the user is authorized to at least one of the given groups.
122
- def authorized?(user, required_groups)
123
- required_groups.nil? || required_groups.any? do |group|
124
- robot.auth.user_in_group?(user, group)
125
- end
126
- end
127
-
128
- # Creates an array of help info for all registered routes.
129
- def build_help(response)
130
- robot.handlers.map do |handler|
131
- next unless handler.respond_to?(:routes)
132
-
133
- handler.routes.map do |route|
134
- route.help.map do |command, description|
135
- if authorized?(response.user, route.required_groups)
136
- help_command(route, command, description)
137
- end
138
- end
139
- end
140
- end.flatten.compact
141
- end
142
-
143
- # Filters the help output by an optional command.
144
- def filter_help(output, response)
145
- filter = response.matches[0][0]
146
-
147
- if filter
148
- output.select { |line| /(?:@?#{name}[:,]?)?#{filter}/i === line }
149
- else
150
- output
151
- end
152
- end
153
-
154
- # Formats an individual command's help message.
155
- def help_command(route, command, description)
156
- command = "#{name}: #{command}" if route.command?
157
-
158
- table_row(command, description)
159
- end
160
-
161
- # The way the bot should be addressed in order to trigger a command.
162
- def name
163
- robot.config.robot.mention_name || robot.config.robot.name
164
- end
165
- end
166
-
167
- Lita.register_handler(DiscordHelp)
168
- end
169
- end
1
+ require 'lita/handler/chat_router'
2
+
3
+ module Lita
4
+ # A namespace to hold all subclasses of {Handler}.
5
+ module Handlers
6
+ # Provides online help about Lita commands for users.
7
+ class DiscordHelp
8
+ extend Lita::Handler::ChatRouter
9
+
10
+ route(/^help\s*(.+)?/, :help, command: true, help: {
11
+ 'help' => t('help.help_value'),
12
+ t('help.help_command_key') => t('help.help_command_value')
13
+ })
14
+
15
+ # Outputs help information about Lita commands.
16
+ # @param response [Lita::Response] The response object.
17
+ # @return [void]
18
+ def help(response)
19
+ output = build_help(response)
20
+ output = filter_help(output, response)
21
+ first_message = true
22
+
23
+ output = output.join("\n")
24
+
25
+ messages = split_message(output)
26
+
27
+ Lita.logger.debug(messages)
28
+
29
+ messages.each { |message|
30
+ if first_message
31
+ response.reply_privately("```\n#{message}\n```")
32
+ first_message = false
33
+ else
34
+ response.reply_privately("|||NOMENTION|||```\n#{message}\n```")
35
+ end
36
+ }
37
+
38
+ if messages.length > 1
39
+ response.reply_privately('Sorry for splitting that up in multiple messages, Discord doesn\'t allow me to send responses longer than 2000 characters :(')
40
+ end
41
+
42
+ end
43
+
44
+ private
45
+
46
+ def split_message(message)
47
+ max_length = 2000 - 30 # Substract 25 for mention safety
48
+ messages = Array.new
49
+
50
+ if message.length < max_length
51
+ messages.push(message)
52
+ else
53
+ message_copy = message
54
+
55
+ loop do
56
+ Lita.logger.debug("Message copy: #{message_copy}")
57
+ # part = 'Lorem ipsum, dolor sit amet' # I don't know why we need this, but we do
58
+
59
+ part = get_message_part(message_copy, max_length)
60
+
61
+ Lita.logger.debug("Part: #{part}")
62
+ part = message_copy.slice!(part)
63
+ messages.push(part)
64
+
65
+ break if message_copy.length <= 0
66
+ end
67
+
68
+ end
69
+
70
+ messages
71
+ end
72
+
73
+ def get_message_part(message, limit)
74
+ Lita.logger.debug("Getting message part from #{message}")
75
+
76
+ Lita.logger.debug('Message was short enough, returning without modifications') if message.length <= limit
77
+ return message if message.length <= limit # No need to check anything if the message is short enough
78
+
79
+ part = message.to_s[0...limit - 1]
80
+ break_index = part.rindex("\n")
81
+
82
+ Lita.logger.debug("Break index: #{break_index}")
83
+
84
+ if break_index != nil && break_index != 0
85
+ message[0, break_index]
86
+ else
87
+ message
88
+ end
89
+ end
90
+
91
+ def table_row(key, value)
92
+ key_width = 30
93
+ value_width = 107
94
+
95
+ key_text = key.ljust(key_width, ' ')
96
+
97
+ value_words = value.split(' ')
98
+
99
+ value_text = ''
100
+ value_line = ''
101
+
102
+ value_words.each do |word|
103
+ new_value_line = "#{value_line} #{word}"
104
+
105
+ if new_value_line.length > value_width
106
+ value_text += "#{value_line}\n" + ''.ljust(key_width + 1, ' ')
107
+ value_line = word
108
+ else
109
+ value_line = "#{value_line} #{word}"
110
+ end
111
+
112
+ end
113
+
114
+ if value_line.length > 0
115
+ value_text = "#{value_text}\n" + ''.ljust(key_width + 1, ' ') + "#{value_line}"
116
+ end
117
+
118
+ key_text + value_text + "\n"
119
+ end
120
+
121
+ # Checks if the user is authorized to at least one of the given groups.
122
+ def authorized?(user, required_groups)
123
+ required_groups.nil? || required_groups.any? do |group|
124
+ robot.auth.user_in_group?(user, group)
125
+ end
126
+ end
127
+
128
+ # Creates an array of help info for all registered routes.
129
+ def build_help(response)
130
+ robot.handlers.map do |handler|
131
+ next unless handler.respond_to?(:routes)
132
+
133
+ handler.routes.map do |route|
134
+ route.help.map do |command, description|
135
+ if authorized?(response.user, route.required_groups)
136
+ help_command(route, command, description)
137
+ end
138
+ end
139
+ end
140
+ end.flatten.compact
141
+ end
142
+
143
+ # Filters the help output by an optional command.
144
+ def filter_help(output, response)
145
+ filter = response.matches[0][0]
146
+
147
+ if filter
148
+ output.select { |line| /(?:@?#{name}[:,]?)?#{filter}/i === line }
149
+ else
150
+ output
151
+ end
152
+ end
153
+
154
+ # Formats an individual command's help message.
155
+ def help_command(route, command, description)
156
+ command = "#{name}: #{command}" if route.command?
157
+
158
+ table_row(command, description)
159
+ end
160
+
161
+ # The way the bot should be addressed in order to trigger a command.
162
+ def name
163
+ robot.config.robot.mention_name || robot.config.robot.name
164
+ end
165
+ end
166
+
167
+ Lita.register_handler(DiscordHelp)
168
+ end
169
+ end
@@ -1,8 +1,8 @@
1
- require 'lita'
2
-
3
- Lita.load_locales Dir[File.expand_path(
4
- File.join('..', '..', 'locales', '*.yml'), __FILE__
5
- )]
6
-
7
- require 'lita/adapters/discord_oauth'
1
+ require 'lita'
2
+
3
+ Lita.load_locales Dir[File.expand_path(
4
+ File.join('..', '..', 'locales', '*.yml'), __FILE__
5
+ )]
6
+
7
+ require 'lita/adapters/discord_oauth'
8
8
  require 'lita/handlers/discord_help'