commandorobo 0.0.3 → 0.0.4

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
  SHA256:
3
- metadata.gz: fc21ecf86acf9004a016400ba93306630de2e3633123fdfd5164804babe03f2a
4
- data.tar.gz: 7fb4cd8a5002b3389db1749d143e70401e1a4e39eb8f8e432d7da69e421d20ee
3
+ metadata.gz: c27daac909e3282e3e0afaddba356195f63a5caee2278bb0e5fcb2d45078b5fa
4
+ data.tar.gz: 969c5f78d351e2eac02c4adf32918bccd2f04b8ce48d1e5e5162212ee47532f2
5
5
  SHA512:
6
- metadata.gz: dba076b5a961c2fa96af5fe2b020246937455d54b76eb66616ee60de5b0b0792129dd69e2fef13081f17dd8e8435c95c2ff508c5134d898275b4f03751042358
7
- data.tar.gz: 933e239dc5e06c18c965fec1ee21bb8527034c4eb926f72e4e59633d9ffda9031924cc42c252d36cb5a95e5f57b717a7c903b398b0d3fc43dc41f746c54fb8eb
6
+ metadata.gz: 18ef66d68cdf00c9c20c559ca7b0d3ab85b2da5bc52cadb414752e76982c73157f00b7d288dba6bd25888d0c5a78eaa36da0ac0e14cb1b069849e46bf10facb8
7
+ data.tar.gz: f619413c4e914164ebab4725345e4724ffe8ab46e7ba905232cbf6590196c4010f56f4a7e02afa548641363bbc6ba2e5a0690d7b7b0d7a19d9223879b54ace1f
data/lib/commandorobo.rb CHANGED
@@ -6,23 +6,31 @@ require 'discordrb'
6
6
  require_relative './constants.rb'
7
7
  require_relative './util.rb'
8
8
 
9
+ # A discordrb command framework
9
10
  module Commandorobo
10
11
  include Constants
11
12
  include Utils
12
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.
13
16
  class NoPermission
14
17
  attr_reader :perm
15
18
  def initialize(perm)
16
19
  @perm = perm
17
20
  end
18
21
 
19
- def prettify # OH GOD PLEASE HELP ME
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
20
25
  @perm.map do |t| # <hackiness>
21
- t.to_s.split(/_/).map(&:capitalize).join(' ')
26
+ t.to_s.split(/_/).map(&:capitalize)
22
27
  end # </hackiness>
23
28
  end
24
29
  end
25
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.
26
34
  class Invoker
27
35
  attr_reader :value, :type
28
36
  def initialize(val, type, **kwargs)
@@ -36,6 +44,9 @@ module Commandorobo
36
44
  @type = type
37
45
  end
38
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.
39
50
  def match(text)
40
51
  if @type != :regex
41
52
  raise "Incorrect type for function."
@@ -43,7 +54,9 @@ module Commandorobo
43
54
  return @value.match(text)
44
55
  end
45
56
 
46
- def extrapolate(text) # I WANT AN EXCUSE TO SAY EXTRAPOLATE OK
57
+ # Gets the actual command and arguments from a string.
58
+ # @param [String] text The text to extrapolate.
59
+ def extrapolate(text)
47
60
  case @type
48
61
  when :prefix
49
62
  return text[@value.length..text.length]
@@ -56,6 +69,8 @@ module Commandorobo
56
69
  end
57
70
  end
58
71
 
72
+ # Checks to see if the invoker is correct for the text.
73
+ # @param [String] text The text to check.
59
74
  def check(text)
60
75
  case @type
61
76
  when :prefix
@@ -70,23 +85,36 @@ module Commandorobo
70
85
  end
71
86
  end
72
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.
73
90
  class Arguments
74
91
  attr_reader :raw
75
92
  def initialize(raw)
76
93
  @raw = raw
77
94
  end
78
95
 
96
+ # Leverages a utility function to grab switch information.
97
+ # @return [Hash] The switches.
79
98
  def switches
80
99
  Commandorobo::Utils::consume_switch(@raw.join(' '))
81
100
  end
82
101
 
102
+ # Leverages a utility function to remove switches.
103
+ # @return [String] The 'switchless' version of the arguments.
83
104
  def noswitch
84
105
  Commandorobo::Utils::remove_switch(@raw.join(' '))
85
106
  end
86
107
  end
87
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.
88
115
  class Command
89
- attr_reader :name, :code, :permissions, :description, :invokers
116
+ attr_reader :name, :code, :permissions, :description
117
+ attr_accessor :invokers
90
118
  def initialize(name, code, permissions, description, invokers, bot)
91
119
  @name = name
92
120
  @code = code
@@ -96,15 +124,22 @@ module Commandorobo
96
124
  @bot = bot
97
125
  end
98
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]
99
131
  def invoke(event, args)
100
132
  @code.call(event, args)
101
133
  end
102
134
 
135
+ # Checks permissions for a command.
136
+ # @param [Discordrb::Event] event The event to use to check.
137
+ # @return [true, Commandorobo::NoPermission]
103
138
  def perm_check(event)
104
139
  perms = {}
105
140
  @permissions.each do |p|
106
141
  if p == :bot_owner
107
- perms[p] = @bot.config['owner'].include?(event.author.id)
142
+ perms[p] = @bot.owners.include?(event.author.id)
108
143
  else
109
144
  perms[p] = event.author.permission?(p)
110
145
  end
@@ -123,14 +158,21 @@ module Commandorobo
123
158
  end
124
159
  end
125
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.
126
167
  class Bot < Discordrb::Bot
127
168
  attr_accessor :invokers
128
- attr_reader :config, :commands, :listeners # what the hell ruby
169
+ attr_reader :config, :commands, :listeners
129
170
  def initialize(config, token, **kwargs)
130
171
  @config = config
131
172
  @commands = []
132
173
  @listeners = {}
133
174
  @invokers = config['invokers'].map {|i| Commandorobo::Invoker.new(i[1], i[0].to_sym)}
175
+ @owners = @config['owner'] || kwargs[:owners]
134
176
  super(token: token, **kwargs)
135
177
  # begin command
136
178
  self.message do |ev|
@@ -166,10 +208,17 @@ module Commandorobo
166
208
  end
167
209
  end
168
210
 
211
+ # Gets a command based on its name.
212
+ # @param [Symbol] name The command to look for.
213
+ # @return [Commandorobo::Command] The command.
169
214
  def get_command(name)
170
215
  @commands.select {|c| c.invokers.include? name}.compact[0]
171
216
  end
172
217
 
218
+ # Registers a new event hook.
219
+ # @param [Symbol] name The event hook.
220
+ # @param [Block] block The code to run when the event fires.
221
+ # @return [nil]
173
222
  def evt(name, &block)
174
223
  if name.is_a? String
175
224
  name = name.to_sym
@@ -180,6 +229,9 @@ module Commandorobo
180
229
  @listeners[name] << block
181
230
  end
182
231
 
232
+ # Internal.
233
+ # @raise [RuntimeError] No event hooks registered for the event.
234
+ # @return [nil]
183
235
  def idispatch(name, *args)
184
236
  if name.is_a? String
185
237
  name = name.to_sym
@@ -194,15 +246,28 @@ module Commandorobo
194
246
  end
195
247
  end
196
248
 
249
+ # Adds a command.
250
+ # @param [Symbol] sym The command name.
251
+ # @param [Array] perms The permissions required by the command. Can be any valid discordrb permission, or :bot_owner, that will restrict to owners.
252
+ # @param [String, nil] desc The description for the command.
253
+ # @param [Block] block The block to run when the command is invoked.
254
+ # @return [nil]
197
255
  def cmd(sym, perms:[], desc:nil, invokers:[], &block)
198
256
  invokers << sym
199
257
  @commands << Commandorobo::Command.new(sym, block, perms, desc, invokers, self)
200
258
  end
201
259
 
202
- def invoke_by(thing, type, **kwargs)
203
- @invokers << Commandorobo::Invoker.new(thing, type, kwargs)
260
+ # Adds an invoker.
261
+ # @param [String] value The value of the invoker.
262
+ # @param [Symbol] type The type of the invoker.
263
+ # @return [nil]
264
+ def invoke_by(value, type, **kwargs)
265
+ @invokers << Commandorobo::Invoker.new(value, type, kwargs)
204
266
  end
205
267
 
268
+ # Grabs an invoker from text.
269
+ # @param [String] text The text to parse.
270
+ # @return [nil]
206
271
  def get_invoker(text)
207
272
  @invokers.map {|a| a if a.check text}.reject(&:!).first # reject all false
208
273
  end
data/lib/constants.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  module Commandorobo
2
+ # Constants for commandorobo.
2
3
  module Constants
4
+ # An array of every valid Discordrb permission as a symbol.
3
5
  ValidPerms = [
4
6
  :create_instant_invite,
5
7
  :kick_members,
data/lib/util.rb CHANGED
@@ -2,12 +2,19 @@
2
2
  # Written by ry00001
3
3
 
4
4
  module Commandorobo
5
+ # Utilities for commandorobo.
5
6
  module Utils
7
+ # Removes dashes. Used internally.
8
+ # @param [String] i String to remove dashes from.
9
+ # @return [String] String with no dashes.
6
10
  def self.nodash(i)
7
11
  i.split('').reject {|j| j == '-'}.join('')
8
12
  end
9
13
 
10
- def self.consume_switch(str) # this function does not eat nintendo switches. do not try to feed it those.
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)
11
18
  nextarg = nil
12
19
  parsed = false
13
20
  switches = {}
@@ -37,10 +44,9 @@ module Commandorobo
37
44
  return switches
38
45
  end
39
46
 
40
- def self.fish_xdm_login_hack_hack_hack_hack # I don't know why this is here but it is, enjoy
41
- 'perry was here'
42
- end
43
-
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.
44
50
  def self.remove_switch(str)
45
51
  parsed = []
46
52
  skip = false
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.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - ry00001