commandorobo 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +5 -5
  2. data/lib/commandorobo.rb +277 -277
  3. data/lib/constants.rb +36 -36
  4. data/lib/util.rb +66 -66
  5. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: 3ac99deb0f38ebce889bec9f8e12c87307edee105e8d4094e747f6816e66633d
4
- data.tar.gz: d93ac7ced6fb6a896317ee03572d6c3b4e75e6014b172734c170f5b8f8096ab1
2
+ SHA1:
3
+ metadata.gz: a3b8fb03dd90bc3baea50bf52710a6743d162b2c
4
+ data.tar.gz: ce0eb0b3b9cc9ae194769856d70f60684cf8ebc2
5
5
  SHA512:
6
- metadata.gz: 8f2e4168ebef2349d1b83bf3fe8865e96d9264483e3dc0306127ee004797b95bf63803d0262a1c8a584bfc3d454104bc065388ae9a4408511b800499f014b8a8
7
- data.tar.gz: a5b006bc0b089a01216846a6d4a9c28472695ba4c0476d35305c9df6f9c5652e1b2736b2c4a3ad2c227d9bd616125477d354354bef232bed5df5c02ab843dc66
6
+ metadata.gz: 4e87b38c4fc8436b98b2ac2f497993fcaf237c1aab2d74ab2084e342b4437c62c88d52ab24074ea53e0eabf4443f11370b8ed71caef90bc9aed4cdff77827f70
7
+ data.tar.gz: 4f9550df7da380cccababc97d848d9ad90715a296b787afbcd7b708455c0af54a012924db5d4e1de23b81b47b6745d991327180c02eb6401dc2ddf5adb83fc2f
data/lib/commandorobo.rb CHANGED
@@ -1,277 +1,277 @@
1
- # Commandorobo
2
- # written by ry00001 and Team ruborobo (ry00001, Erisa Arrowsmith (Seriel))
3
- # Originally used for ruborobo (https://github.com/ry00001/ruborobo)
4
-
5
- require 'discordrb'
6
- require_relative './constants.rb'
7
- require_relative './util.rb'
8
-
9
- # A discordrb command framework
10
- module Commandorobo
11
- include Constants
12
- include Utils
13
-
14
- # Class that gets returned by Command#perm_check whenever the user does not have permission.
15
- # @attr_reader [Array] perm The permissions returned as an array of symbols.
16
- class NoPermission
17
- attr_reader :perm
18
- def initialize(perm)
19
- @perm = perm
20
- end
21
-
22
- # Generates a "pretty" array of strings for permissions.
23
- # @return [Array] An array of strings representing the pretty name for permissions.
24
- def prettify
25
- @perm.map do |t| # <hackiness>
26
- t.to_s.split(/_/).map(&:capitalize)
27
- end # </hackiness>
28
- end
29
- end
30
-
31
- # Class to represent an invoker (prefix, suffix).
32
- # @attr_reader [String] value The value of the invoker.
33
- # @attr_reader [Symbol] type The type of the invoker.
34
- class Invoker
35
- attr_reader :value, :type
36
- def initialize(val, type, **kwargs)
37
- if type == :regex
38
- val = Regexp::new(val)
39
- end
40
- if !kwargs[:sep].nil? && type == :dual
41
- val = val.split(kwargs[:sep])
42
- end
43
- @value = val
44
- @type = type
45
- end
46
-
47
- # Internal - checks to see if regexes match.
48
- # @param [String] text The text to check.
49
- # @raise [RuntimeError] The invoker wasn't a regex.
50
- def match(text)
51
- if @type != :regex
52
- raise "Incorrect type for function."
53
- end
54
- return @value.match(text)
55
- end
56
-
57
- # Gets the actual command and arguments from a string.
58
- # @param [String] text The text to extrapolate.
59
- def extrapolate(text)
60
- case @type
61
- when :prefix
62
- return text[@value.length..text.length]
63
- when :suffix
64
- return text[0..-@value.length-1]
65
- when :dual
66
- return text[@value[0].length..-@value[1].length-1]
67
- when :regex
68
- return self.match(text)
69
- end
70
- end
71
-
72
- # Checks to see if the invoker is correct for the text.
73
- # @param [String] text The text to check.
74
- def check(text)
75
- case @type
76
- when :prefix
77
- return text.start_with? @value
78
- when :suffix
79
- return text.end_with? @value
80
- when :dual
81
- return text.start_with?(@value[0]) && text.end_with?(@value[1])
82
- when :regex
83
- return !self.match(text).nil?
84
- end
85
- end
86
- end
87
-
88
- # Class that gets passed to any block defined in {Bot#cmd}. Represents arguments.
89
- # @attr_reader [Array] raw The unprocessed array of strings that make up the arguments.
90
- class Arguments
91
- attr_reader :raw
92
- def initialize(raw)
93
- @raw = raw
94
- end
95
-
96
- # Leverages a utility function to grab switch information.
97
- # @return [Hash] The switches.
98
- def switches
99
- Commandorobo::Utils::consume_switch(@raw.join(' '))
100
- end
101
-
102
- # Leverages a utility function to remove switches.
103
- # @return [String] The 'switchless' version of the arguments.
104
- def noswitch
105
- Commandorobo::Utils::remove_switch(@raw.join(' '))
106
- end
107
- end
108
-
109
- # Class that represents a command.
110
- # @attr_reader [Symbol] name The name of the command.
111
- # @attr_reader [Block] code The code to execute when the command fires.
112
- # @attr_reader [Array] permissions The permissions required to execute the command.
113
- # @attr_reader [String] description The description for the command.
114
- # @attr [Array] invokers The invokers for the command.
115
- class Command
116
- attr_reader :name, :code, :permissions, :description
117
- attr_accessor :invokers
118
- def initialize(name, code, permissions, description, invokers, bot)
119
- @name = name
120
- @code = code
121
- @permissions = permissions
122
- @description = description
123
- @invokers = invokers.nil? ? [name] : invokers
124
- @bot = bot
125
- end
126
-
127
- # Invokes the command.
128
- # @param [Discordrb::Event] event The Event object to pass to the command.
129
- # @param [Array] args The arguments to pass to the command.
130
- # @return [nil]
131
- def invoke(event, args)
132
- @code.call(event, args)
133
- end
134
-
135
- # Checks permissions for a command.
136
- # @param [Discordrb::Event] event The event to use to check.
137
- # @return [true, Commandorobo::NoPermission]
138
- def perm_check(event)
139
- perms = {}
140
- @permissions.each do |p|
141
- if p == :bot_owner
142
- perms[p] = @bot.owners.include?(event.author.id)
143
- else
144
- perms[p] = event.author.permission?(p)
145
- end
146
- end
147
- if !perms.values.all?
148
- noperms = []
149
- perms.keys.each do |p|
150
- if !perms[p]
151
- noperms << p
152
- end
153
- end
154
- Commandorobo::NoPermission.new(noperms)
155
- else
156
- true
157
- end
158
- end
159
- end
160
-
161
- # The main bot class.
162
- # An extension of discordrb's.
163
- # @attr [Array] invokers The invokers for the bot. Can be modified, but I'd rather you use {Bot#invoke_by}.
164
- # @attr_reader [Hash] config The configuration for the bot.
165
- # @attr_reader [Array] commands The commands, in an array. This isn't a hash because I have a method for looking them up.
166
- # @attr_reader [Array] listeners Bound event listeners.
167
- # @attr_reader [Array] owners List of user IDs to consider as bot owners.
168
- class Bot < Discordrb::Bot
169
- attr_accessor :invokers
170
- attr_reader :config, :commands, :listeners, :owners
171
- def initialize(config, token, **kwargs)
172
- @config = config
173
- @commands = []
174
- @listeners = {}
175
- @invokers = config['invokers'].map {|i| Commandorobo::Invoker.new(i[1], i[0].to_sym)}
176
- @owners = @config['owner'] || kwargs[:owners]
177
- super(token: token, **kwargs)
178
- # begin command
179
- self.message do |ev|
180
- meme = self.get_invoker(ev.text)
181
- if meme.nil?
182
- next
183
- end
184
- awau = meme.extrapolate(ev.text)
185
- if awau.is_a?(MatchData)
186
- awau = awau[1].split(/ /)
187
- else
188
- awau = awau.split(/ /)
189
- end
190
- cmd = awau.first
191
- acmd = self.get_command cmd.to_sym
192
- awau.shift
193
- sm = awau
194
- if !acmd.nil?
195
- pc = acmd.perm_check(ev)
196
- if pc.is_a?(Commandorobo::NoPermission)
197
- self.idispatch(:command_noperms, ev, acmd, pc)
198
- next
199
- end
200
- begin
201
- a = acmd.invoke(ev, Commandorobo::Arguments.new(sm))
202
- ev.respond(a)
203
- rescue Exception => err
204
- self.idispatch(:command_error, ev, acmd, err)
205
- else
206
- self.idispatch(:command_notfound, ev, acmd)
207
- end
208
- end
209
- end
210
- end
211
-
212
- # Gets a command based on its name.
213
- # @param [Symbol] name The command to look for.
214
- # @return [Commandorobo::Command] The command.
215
- def get_command(name)
216
- @commands.select {|c| c.invokers.include? name}.compact[0]
217
- end
218
-
219
- # Registers a new event hook.
220
- # @param [Symbol] name The event hook.
221
- # @param [Block] block The code to run when the event fires.
222
- # @return [nil]
223
- def evt(name, &block)
224
- if name.is_a? String
225
- name = name.to_sym
226
- end
227
- if @listeners[name].nil?
228
- @listeners[name] = []
229
- end
230
- @listeners[name] << block
231
- end
232
-
233
- # Internal.
234
- # @raise [RuntimeError] No event hooks registered for the event.
235
- # @return [nil]
236
- def idispatch(name, *args)
237
- if name.is_a? String
238
- name = name.to_sym
239
- end
240
- thing = @listeners[name]
241
- if thing.nil?
242
- raise "No event hooks registered for #{name.to_s}"
243
- else
244
- thing.each do |func|
245
- func.call(*args)
246
- end
247
- end
248
- end
249
-
250
- # Adds a command.
251
- # @param [Symbol] sym The command name.
252
- # @param [Array] perms The permissions required by the command. Can be any valid discordrb permission, or :bot_owner, that will restrict to owners.
253
- # @param [String, nil] desc The description for the command.
254
- # @param [Block] block The block to run when the command is invoked.
255
- # @return [nil]
256
- def cmd(sym, perms:[], desc:nil, invokers:[], &block)
257
- invokers << sym
258
- @commands << Commandorobo::Command.new(sym, block, perms, desc, invokers, self)
259
- end
260
-
261
- # Adds an invoker.
262
- # @param [String] value The value of the invoker.
263
- # @param [Symbol] type The type of the invoker.
264
- # @return [nil]
265
- def invoke_by(value, type, **kwargs)
266
- @invokers << Commandorobo::Invoker.new(value, type, kwargs)
267
- end
268
-
269
- # Grabs an invoker from text.
270
- # @param [String] text The text to parse.
271
- # @return [nil]
272
- def get_invoker(text)
273
- @invokers.map {|a| a if a.check text}.reject(&:!).first # reject all false
274
- end
275
- end
276
- end
277
-
1
+ # Commandorobo
2
+ # written by ry00001 and Team ruborobo (ry00001, Erisa Arrowsmith (Seriel))
3
+ # Originally used for ruborobo (https://github.com/ry00001/ruborobo)
4
+
5
+ require 'discordrb'
6
+ require_relative './constants.rb'
7
+ require_relative './util.rb'
8
+
9
+ # A discordrb command framework
10
+ module Commandorobo
11
+ include Constants
12
+ include Utils
13
+
14
+ # Class that gets returned by Command#perm_check whenever the user does not have permission.
15
+ # @attr_reader [Array] perm The permissions returned as an array of symbols.
16
+ class NoPermission
17
+ attr_reader :perm
18
+ def initialize(perm)
19
+ @perm = perm
20
+ end
21
+
22
+ # Generates a "pretty" array of strings for permissions.
23
+ # @return [Array] An array of strings representing the pretty name for permissions.
24
+ def prettify
25
+ @perm.map do |t| # <hackiness>
26
+ t.to_s.split(/_/).map(&:capitalize).join(' ')
27
+ end # </hackiness>
28
+ end
29
+ end
30
+
31
+ # Class to represent an invoker (prefix, suffix).
32
+ # @attr_reader [String] value The value of the invoker.
33
+ # @attr_reader [Symbol] type The type of the invoker.
34
+ class Invoker
35
+ attr_reader :value, :type
36
+ def initialize(val, type, **kwargs)
37
+ if type == :regex
38
+ val = Regexp::new(val)
39
+ end
40
+ if !kwargs[:sep].nil? && type == :dual
41
+ val = val.split(kwargs[:sep])
42
+ end
43
+ @value = val
44
+ @type = type
45
+ end
46
+
47
+ # Internal - checks to see if regexes match.
48
+ # @param [String] text The text to check.
49
+ # @raise [RuntimeError] The invoker wasn't a regex.
50
+ def match(text)
51
+ if @type != :regex
52
+ raise "Incorrect type for function."
53
+ end
54
+ return @value.match(text)
55
+ end
56
+
57
+ # Gets the actual command and arguments from a string.
58
+ # @param [String] text The text to extrapolate.
59
+ def extrapolate(text)
60
+ case @type
61
+ when :prefix
62
+ return text[@value.length..text.length]
63
+ when :suffix
64
+ return text[0..-@value.length-1]
65
+ when :dual
66
+ return text[@value[0].length..-@value[1].length-1]
67
+ when :regex
68
+ return self.match(text)
69
+ end
70
+ end
71
+
72
+ # Checks to see if the invoker is correct for the text.
73
+ # @param [String] text The text to check.
74
+ def check(text)
75
+ case @type
76
+ when :prefix
77
+ return text.start_with? @value
78
+ when :suffix
79
+ return text.end_with? @value
80
+ when :dual
81
+ return text.start_with?(@value[0]) && text.end_with?(@value[1])
82
+ when :regex
83
+ return !self.match(text).nil?
84
+ end
85
+ end
86
+ end
87
+
88
+ # Class that gets passed to any block defined in {Bot#cmd}. Represents arguments.
89
+ # @attr_reader [Array] raw The unprocessed array of strings that make up the arguments.
90
+ class Arguments
91
+ attr_reader :raw
92
+ def initialize(raw)
93
+ @raw = raw
94
+ end
95
+
96
+ # Leverages a utility function to grab switch information.
97
+ # @return [Hash] The switches.
98
+ def switches
99
+ Commandorobo::Utils::consume_switch(@raw.join(' '))
100
+ end
101
+
102
+ # Leverages a utility function to remove switches.
103
+ # @return [String] The 'switchless' version of the arguments.
104
+ def noswitch
105
+ Commandorobo::Utils::remove_switch(@raw.join(' '))
106
+ end
107
+ end
108
+
109
+ # Class that represents a command.
110
+ # @attr_reader [Symbol] name The name of the command.
111
+ # @attr_reader [Block] code The code to execute when the command fires.
112
+ # @attr_reader [Array] permissions The permissions required to execute the command.
113
+ # @attr_reader [String] description The description for the command.
114
+ # @attr [Array] invokers The invokers for the command.
115
+ class Command
116
+ attr_reader :name, :code, :permissions, :description
117
+ attr_accessor :invokers
118
+ def initialize(name, code, permissions, description, invokers, bot)
119
+ @name = name
120
+ @code = code
121
+ @permissions = permissions
122
+ @description = description
123
+ @invokers = invokers.nil? ? [name] : invokers
124
+ @bot = bot
125
+ end
126
+
127
+ # Invokes the command.
128
+ # @param [Discordrb::Event] event The Event object to pass to the command.
129
+ # @param [Array] args The arguments to pass to the command.
130
+ # @return [nil]
131
+ def invoke(event, args)
132
+ @code.call(event, args)
133
+ end
134
+
135
+ # Checks permissions for a command.
136
+ # @param [Discordrb::Event] event The event to use to check.
137
+ # @return [true, Commandorobo::NoPermission]
138
+ def perm_check(event)
139
+ perms = {}
140
+ @permissions.each do |p|
141
+ if p == :bot_owner
142
+ perms[p] = @bot.owners.include?(event.author.id)
143
+ else
144
+ perms[p] = event.author.permission?(p)
145
+ end
146
+ end
147
+ if !perms.values.all?
148
+ noperms = []
149
+ perms.keys.each do |p|
150
+ if !perms[p]
151
+ noperms << p
152
+ end
153
+ end
154
+ Commandorobo::NoPermission.new(noperms)
155
+ else
156
+ true
157
+ end
158
+ end
159
+ end
160
+
161
+ # The main bot class.
162
+ # An extension of discordrb's.
163
+ # @attr [Array] invokers The invokers for the bot. Can be modified, but I'd rather you use {Bot#invoke_by}.
164
+ # @attr_reader [Hash] config The configuration for the bot.
165
+ # @attr_reader [Array] commands The commands, in an array. This isn't a hash because I have a method for looking them up.
166
+ # @attr_reader [Array] listeners Bound event listeners.
167
+ # @attr_reader [Array] owners List of user IDs to consider as bot owners.
168
+ class Bot < Discordrb::Bot
169
+ attr_accessor :invokers
170
+ attr_reader :config, :commands, :listeners, :owners
171
+ def initialize(config, token, **kwargs)
172
+ @config = config
173
+ @commands = []
174
+ @listeners = {}
175
+ @invokers = config['invokers'].map {|i| Commandorobo::Invoker.new(i[1], i[0].to_sym)}
176
+ @owners = @config['owner'] || kwargs[:owners]
177
+ super(token: token, **kwargs)
178
+ # begin command
179
+ self.message do |ev|
180
+ meme = self.get_invoker(ev.text)
181
+ if meme.nil?
182
+ next
183
+ end
184
+ awau = meme.extrapolate(ev.text)
185
+ if awau.is_a?(MatchData)
186
+ awau = awau[1].split(/ /)
187
+ else
188
+ awau = awau.split(/ /)
189
+ end
190
+ cmd = awau.first
191
+ acmd = self.get_command cmd.to_sym
192
+ awau.shift
193
+ sm = awau
194
+ if !acmd.nil?
195
+ pc = acmd.perm_check(ev)
196
+ if pc.is_a?(Commandorobo::NoPermission)
197
+ self.idispatch(:command_noperms, ev, acmd, pc)
198
+ next
199
+ end
200
+ begin
201
+ a = acmd.invoke(ev, Commandorobo::Arguments.new(sm))
202
+ ev.respond(a)
203
+ rescue Exception => err
204
+ self.idispatch(:command_error, ev, acmd, err)
205
+ else
206
+ self.idispatch(:command_notfound, ev, acmd)
207
+ end
208
+ end
209
+ end
210
+ end
211
+
212
+ # Gets a command based on its name.
213
+ # @param [Symbol] name The command to look for.
214
+ # @return [Commandorobo::Command] The command.
215
+ def get_command(name)
216
+ @commands.select {|c| c.invokers.include? name}.compact[0]
217
+ end
218
+
219
+ # Registers a new event hook.
220
+ # @param [Symbol] name The event hook.
221
+ # @param [Block] block The code to run when the event fires.
222
+ # @return [nil]
223
+ def evt(name, &block)
224
+ if name.is_a? String
225
+ name = name.to_sym
226
+ end
227
+ if @listeners[name].nil?
228
+ @listeners[name] = []
229
+ end
230
+ @listeners[name] << block
231
+ end
232
+
233
+ # Internal.
234
+ # @raise [RuntimeError] No event hooks registered for the event.
235
+ # @return [nil]
236
+ def idispatch(name, *args)
237
+ if name.is_a? String
238
+ name = name.to_sym
239
+ end
240
+ thing = @listeners[name]
241
+ if thing.nil?
242
+ raise "No event hooks registered for #{name.to_s}"
243
+ else
244
+ thing.each do |func|
245
+ func.call(*args)
246
+ end
247
+ end
248
+ end
249
+
250
+ # Adds a command.
251
+ # @param [Symbol] sym The command name.
252
+ # @param [Array] perms The permissions required by the command. Can be any valid discordrb permission, or :bot_owner, that will restrict to owners.
253
+ # @param [String, nil] desc The description for the command.
254
+ # @param [Block] block The block to run when the command is invoked.
255
+ # @return [nil]
256
+ def cmd(sym, perms:[], desc:nil, invokers:[], &block)
257
+ invokers << sym
258
+ @commands << Commandorobo::Command.new(sym, block, perms, desc, invokers, self)
259
+ end
260
+
261
+ # Adds an invoker.
262
+ # @param [String] value The value of the invoker.
263
+ # @param [Symbol] type The type of the invoker.
264
+ # @return [nil]
265
+ def invoke_by(value, type, **kwargs)
266
+ @invokers << Commandorobo::Invoker.new(value, type, kwargs)
267
+ end
268
+
269
+ # Grabs an invoker from text.
270
+ # @param [String] text The text to parse.
271
+ # @return [nil]
272
+ def get_invoker(text)
273
+ @invokers.map {|a| a if a.check text}.reject(&:!).first # reject all false
274
+ end
275
+ end
276
+ end
277
+
data/lib/constants.rb CHANGED
@@ -1,36 +1,36 @@
1
- module Commandorobo
2
- # Constants for commandorobo.
3
- module Constants
4
- # An array of every valid Discordrb permission as a symbol.
5
- ValidPerms = [
6
- :create_instant_invite,
7
- :kick_members,
8
- :ban_members,
9
- :administrator,
10
- :manage_channels,
11
- :manage_server,
12
- :add_reactions,
13
- :view_audit_log,
14
- :read_messages,
15
- :send_messages,
16
- :send_tts_messages,
17
- :manage_messages,
18
- :embed_links,
19
- :attach_files,
20
- :read_message_history,
21
- :mention_everyone,
22
- :use_external_emoji,
23
- :connect,
24
- :speak,
25
- :mute_members,
26
- :deafen_members,
27
- :move_members,
28
- :use_voice_activity,
29
- :change_nickname,
30
- :manage_nicknames,
31
- :manage_roles,
32
- :manage_webhooks,
33
- :manage_emojis
34
- ].freeze
35
- end
36
- end
1
+ module Commandorobo
2
+ # Constants for commandorobo.
3
+ module Constants
4
+ # An array of every valid Discordrb permission as a symbol.
5
+ ValidPerms = [
6
+ :create_instant_invite,
7
+ :kick_members,
8
+ :ban_members,
9
+ :administrator,
10
+ :manage_channels,
11
+ :manage_server,
12
+ :add_reactions,
13
+ :view_audit_log,
14
+ :read_messages,
15
+ :send_messages,
16
+ :send_tts_messages,
17
+ :manage_messages,
18
+ :embed_links,
19
+ :attach_files,
20
+ :read_message_history,
21
+ :mention_everyone,
22
+ :use_external_emoji,
23
+ :connect,
24
+ :speak,
25
+ :mute_members,
26
+ :deafen_members,
27
+ :move_members,
28
+ :use_voice_activity,
29
+ :change_nickname,
30
+ :manage_nicknames,
31
+ :manage_roles,
32
+ :manage_webhooks,
33
+ :manage_emojis
34
+ ].freeze
35
+ end
36
+ end
data/lib/util.rb CHANGED
@@ -1,67 +1,67 @@
1
- # Utility module for commandorobo
2
- # Written by ry00001
3
-
4
- module Commandorobo
5
- # Utilities for commandorobo.
6
- module Utils
7
- # Removes dashes. Used internally.
8
- # @param [String] i String to remove dashes from.
9
- # @return [String] String with no dashes.
10
- def self.nodash(i)
11
- i.split('').reject {|j| j == '-'}.join('')
12
- end
13
-
14
- # Gets switches from text.
15
- # @param [String] str String to parse.
16
- # @return [Hash] Switches as a hash.
17
- def self.consume_switch(str)
18
- nextarg = nil
19
- parsed = false
20
- switches = {}
21
- ws = str.split(' ')
22
- ws.each_with_index do |k, i|
23
- parsed = false
24
- if k.start_with?('-') && k.length > 1
25
- # oh heck a switch
26
- if k[1] == '-'
27
- # oh heck another switch
28
- k = self.nodash(k)
29
- switches[k.to_sym] = ws[i+1].nil? && !ws[i+1].start_with?('-') ? true : ws[i+1]
30
- else
31
- # no double-switch: interpret literally
32
- k = self.nodash(k)
33
- if k.length == 1
34
- switches[k.to_sym] = ws[i+1].nil? && !ws[i+1].start_with?('-') ? true : ws[i+1]
35
- else
36
- k.chars.each do |l|
37
- switches[l.to_sym] = true
38
- end
39
- switches[switches.keys.last] = ws[i+1].nil? && !ws[i+1].start_with?('-') ? true : ws[i+1]
40
- end
41
- end
42
- end
43
- end
44
- return switches
45
- end
46
-
47
- # Does the opposite of {Commandorobo::Utils::consume_switch}, it removes switches.
48
- # @param [String] str String to parse.
49
- # @return [Array] A raw array of removed switches.
50
- def self.remove_switch(str)
51
- parsed = []
52
- skip = false
53
- str.split(' ').each do |i|
54
- if skip
55
- skip = false
56
- next
57
- end
58
- if i.start_with?('-') && i.length > 1
59
- skip = true
60
- else
61
- parsed << i
62
- end
63
- end
64
- parsed
65
- end
66
- end
1
+ # Utility module for commandorobo
2
+ # Written by ry00001
3
+
4
+ module Commandorobo
5
+ # Utilities for commandorobo.
6
+ module Utils
7
+ # Removes dashes. Used internally.
8
+ # @param [String] i String to remove dashes from.
9
+ # @return [String] String with no dashes.
10
+ def self.nodash(i)
11
+ i.split('').reject {|j| j == '-'}.join('')
12
+ end
13
+
14
+ # Gets switches from text.
15
+ # @param [String] str String to parse.
16
+ # @return [Hash] Switches as a hash.
17
+ def self.consume_switch(str)
18
+ nextarg = nil
19
+ parsed = false
20
+ switches = {}
21
+ ws = str.split(' ')
22
+ ws.each_with_index do |k, i|
23
+ parsed = false
24
+ if k.start_with?('-') && k.length > 1
25
+ # oh heck a switch
26
+ if k[1] == '-'
27
+ # oh heck another switch
28
+ k = self.nodash(k)
29
+ switches[k.to_sym] = ws[i+1].nil? && !ws[i+1].start_with?('-') ? true : ws[i+1]
30
+ else
31
+ # no double-switch: interpret literally
32
+ k = self.nodash(k)
33
+ if k.length == 1
34
+ switches[k.to_sym] = ws[i+1].nil? && !ws[i+1].start_with?('-') ? true : ws[i+1]
35
+ else
36
+ k.chars.each do |l|
37
+ switches[l.to_sym] = true
38
+ end
39
+ switches[switches.keys.last] = ws[i+1].nil? && !ws[i+1].start_with?('-') ? true : ws[i+1]
40
+ end
41
+ end
42
+ end
43
+ end
44
+ return switches
45
+ end
46
+
47
+ # Does the opposite of {Commandorobo::Utils::consume_switch}, it removes switches.
48
+ # @param [String] str String to parse.
49
+ # @return [Array] A raw array of removed switches.
50
+ def self.remove_switch(str)
51
+ parsed = []
52
+ skip = false
53
+ str.split(' ').each do |i|
54
+ if skip
55
+ skip = false
56
+ next
57
+ end
58
+ if i.start_with?('-') && i.length > 1
59
+ skip = true
60
+ else
61
+ parsed << i
62
+ end
63
+ end
64
+ parsed
65
+ end
66
+ end
67
67
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: commandorobo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - ry00001
@@ -31,7 +31,7 @@ dependencies:
31
31
  - !ruby/object:Gem::Version
32
32
  version: 3.1.0
33
33
  description: A command framework for discordrb.
34
- email: othery00001@gmail.com
34
+ email: ry00001@protonmail.com
35
35
  executables: []
36
36
  extensions: []
37
37
  extra_rdoc_files: []
@@ -59,7 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
59
  version: '0'
60
60
  requirements: []
61
61
  rubyforge_project:
62
- rubygems_version: 2.7.6
62
+ rubygems_version: 2.6.14
63
63
  signing_key:
64
64
  specification_version: 4
65
65
  summary: Command framework for discordrb