commandrb 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/commandrb.rb +112 -40
  3. metadata +11 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 837fd9c73dccf6017f686003621c0cb6cc6ee8ef
4
- data.tar.gz: 2aafaf2ec44c19c33dd70a0c2ab2757e268bb5de
3
+ metadata.gz: 1bdd7c0e995d1b0fd6a818164558252a72dd277a
4
+ data.tar.gz: 7a62a408b7e2f853bce21d3ee5a950afb8fd1713
5
5
  SHA512:
6
- metadata.gz: 89a2f798a7226f0bd6fc8f746144ba4593cf1a7f4719a89c69cfb2e8f39c6d2ee6f586020d2a6f48c466cb574cc3f746d33da2d83a9375c9500b4fc2e1c74451
7
- data.tar.gz: 0ba8d6f38dcc9b23029086f3d7d59140685dd0c06c5b7b3703666709c51530b3984177a9ffd20914763e1ebaf73faeecc4a4ed5c9b576f7d38d0914a1db6db3c
6
+ metadata.gz: d60c6b925fc8dfb87529e61c2ef215403b0f65a07aa8a3e809050a52727434a77109397bd12cd96b73f39e1f1ac1dd52b495ffba685bdbff2f5aede33fa618fc
7
+ data.tar.gz: 226676ac73f60169d62f45c53ddbf43e825a1ec75d12b63593a90d1f5c4a5e0b83d58909b81c9776501e5175490fd5a277fc343208d0e6a334e88e30741b6a0c
data/lib/commandrb.rb CHANGED
@@ -1,19 +1,44 @@
1
1
  class CommandrbBot
2
+
3
+ # Be able to adjust the config on the fly.
4
+ attr_accessor :config
5
+
6
+ # Needed to run the bot, or create custom events.
7
+ attr_accessor :bot
8
+
9
+ # Can manually manipulate commands using this.
2
10
  attr_accessor :commands
11
+
12
+ # Lets you change global prefixes while the bot is running (Not recommended!)
3
13
  attr_accessor :prefixes
4
- attr_accessor :bot
5
- attr_accessor :prefix_type
6
- attr_accessor :owners
7
- attr_accessor :typing_default
14
+
15
+
16
+ def add_command(name, attributes = {})
17
+ @commands[name.to_sym] = attributes
18
+ end
19
+
20
+ def remove_command(name)
21
+ begin
22
+ @commands.delete(name)
23
+ rescue
24
+ return false
25
+ end
26
+ true
27
+ end
8
28
 
9
29
  def initialize(init_hash)
10
30
 
31
+ # Setup the variables for first use.
11
32
  @commands = {}
12
33
  @prefixes = []
13
34
  @config = init_hash
14
35
 
15
- @config[:prefix_type] = 'rescue' if @config[:prefix_type].nil?
16
- @config[:typing_default] = false if @config[:typing_default].nil?
36
+ # Load sane defaults for options that aren't specified.
37
+
38
+ # @config[:prefix_type] = 'rescue' if @config[:prefix_type].nil?
39
+ @config[:typing_default] = false if @config[:typing_default].nil?
40
+ @config[:selfbot] = false if @config[:selfbot].nil?
41
+ @config[:delete_activators] = false if @config[:delete_activators].nil?
17
42
 
18
43
  if @config[:token].nil? or init_hash[:token] == ''
19
44
  puts 'No token supplied in init hash!'
@@ -21,7 +46,7 @@ class CommandrbBot
21
46
  end
22
47
 
23
48
  init_parse_self = init_hash[:parse_self] rescue nil
24
- init_type = init_hash[:type] rescue :bot
49
+ init_type = @config[:type]
25
50
 
26
51
  if init_type == :bot
27
52
  if init_hash[:client_id].nil?
@@ -30,100 +55,147 @@ class CommandrbBot
30
55
  end
31
56
  end
32
57
 
33
- @prefixes = []
34
-
35
58
  @config[:owners] = init_hash[:owners]
36
- puts 'Invalid owners supplied in init hash!'
37
59
 
38
60
  @prefixes = init_hash[:prefixes]
39
- puts 'Invalid prefixes supplied in init hash!'
40
61
 
41
62
  @bot = Discordrb::Bot.new(
42
63
  token: @config[:token],
43
64
  client_id: @config[:client_id],
44
65
  parse_self: init_parse_self,
45
- type: init_type
66
+ type: @config[:type]
46
67
  )
47
68
 
48
69
  unless init_hash[:ready].nil?
49
70
  @bot.ready do |event|
71
+ event.bot.game = @config[:game] unless config[:game].nil?
50
72
  init_hash[:ready].call(event)
51
73
  end
52
74
  end
53
75
 
54
- def add_command(name, attributes = {})
55
- @commands[name.to_sym] = attributes
56
- end
57
76
 
58
77
  # Command processing
59
78
  @bot.message do |event|
79
+ @command = nil
80
+ @event = nil
81
+ @chosen = nil
82
+ @args = nil
83
+ @rawargs = nil
60
84
  @continue = false
61
85
  @prefixes.each { |prefix|
62
86
  if event.message.content.start_with?(prefix)
63
87
 
64
88
  @commands.each { | key, command |
65
- if command[:triggers].nil?
66
- triggers = [key.to_s]
67
- else
68
- triggers = command[:triggers]
69
- end
89
+ triggers = command[:triggers].nil? ? [key.to_s] : command[:triggers]
70
90
 
71
91
  triggers.each { |trigger|
72
- @activator = prefix + trigger
73
- if event.message.content.start_with?(@activator)
74
- puts '@activator picked.'
75
- @continue = true
76
- break
77
- else
78
- next
92
+ @activator = prefix + trigger.to_s
93
+ @activator = @activator.downcase
94
+ if event.message.content.downcase.start_with?(@activator)
95
+
96
+ # Continue only if you've already chosen a choice.
97
+ unless @chosen.nil?
98
+ # If the new activator begins with the chosen one, then override it.
99
+ # Example: sh is chosen, shell is the new one.
100
+ # In this example, shell would override sh, preventing ugly bugs.
101
+ if @activator.start_with?(@chosen)
102
+ @chosen = @activator
103
+ # Otherwhise, just give up.
104
+ else
105
+ next
106
+ end
107
+ # If you haven't chosen yet, get choosing!
108
+ else
109
+ @continue = true
110
+ @chosen = @activator
111
+ end
79
112
  end
80
113
  }
81
114
 
82
115
  next unless @continue
83
116
 
117
+ break if @config[:selfbot] && event.user.id != @bot.profile.id
118
+
84
119
  # Command flag defaults
85
120
  command[:catch_errors] = @config[:catch_errors] if command[:catch_errors].nil?
86
121
  command[:owners_only] = false if command[:owners_only].nil?
87
122
  command[:max_args] = 2000 if command[:max_args].nil?
88
123
  command[:server_only] = false if command[:server_only].nil?
89
124
  command[:typing] = @config[:typing_default] if command[:typing_default].nil?
125
+ command[:delete_activator] = @config[:delete_activators] if command[:delete_activator].nil?
90
126
 
127
+ # If the command is set to owners only and the user is not the owner, show error and abort.
91
128
  if command[:owners_only]
92
- unless YuukiBot.config['owners'].include?(event.user.id)
129
+ unless @config[:owners].include?(event.user.id)
93
130
  event.respond('❌ You don\'t have permission for that!')
94
- break
131
+ next
95
132
  end
96
133
  end
97
134
 
135
+ # If the settings are to delete activating messages, then do that.
136
+ # I'm *hoping* this doesn't cause issues with argument extraction.
137
+ event.message.delete if command[:delete_activator]
138
+
139
+ # If the command is only for use in servers, display error and abort.
98
140
  if command[:server_only] && event.channel.private?
99
- event.respond('❌ This command will only work in servers!')
141
+ # For selfbots, a fancy embed will be used. WIP.
142
+ if @config[:selfbot]
143
+ event.channel.send_embed do |embed|
144
+ embed.colour = 0x221461
145
+ embed.title = '❌ An error has occured!'
146
+ embed.description = 'This command can only be used in servers!'
147
+ embed.footer = Discordrb::Webhooks::EmbedFooter.new(text: "Command: '#{event.message.content}'")
148
+ end
149
+ else
150
+ # If its not a selfbot, an ordinary message will be shown, may be changed to embed later.
151
+ event.respond('❌ This command will only work in servers!')
152
+ end
153
+ # Abort!
100
154
  next
101
155
  end
102
156
 
157
+ # If the user is a bot and the command is set to not pass bots OR the user is a bot and the global config is to not parse bots...
158
+ # ...then abort :3
103
159
  if (event.user.bot_account? && command[:parse_bots] == false) || (event.user.bot_account? && @config[:parse_bots] == false)
160
+ # Abort!
104
161
  next
105
162
  end
106
163
 
164
+ # If the config is setup to show typing messages, then do so.
107
165
  event.channel.start_typing if command[:typing]
108
166
 
167
+ # Grabs the arguments from the command message without the command part.
109
168
  args = event.message.content.slice!(@activator.length, event.message.content.size)
110
- args = args.split(' ')
169
+ # Split the arguments into an array for easy usage.
170
+ rawargs = args
171
+ args = args.split(/ /)
111
172
 
173
+ # Check the number of args for the command.
112
174
  if args.length > command[:max_args]
175
+ # May be replaced with an embed.
113
176
  event.respond("❌ Too many arguments! \nMax arguments: `#{command[:max_args]}`")
114
177
  next
115
178
  end
116
- if !command[:catch_errors] || @config['catch_errors']
117
- command[:code].call(event, args)
118
- else
119
- begin
120
- command[:code].call(event, args)
121
- rescue Exception => e
122
- event.respond("❌ An error has occured!! ```ruby\n#{e}```Please contact the bot owner with the above message for assistance.")
123
- end
124
- end
179
+
180
+ # All done here.
181
+ @command = command
182
+ @event = event
183
+ @args = args
184
+ @rawargs = rawargs
125
185
  break
126
186
  }
187
+ # If the command is configured to catch all errors, thy shall be done.
188
+ if !@command[:catch_errors] || @config['catch_errors']
189
+ # Run the command code!
190
+ @command[:code].call(@event, @args, @rawargs)
191
+ else
192
+ # Run the command code, but catch all errors and output accordingly.
193
+ begin
194
+ @command[:code].call(@event, @args, @rawargs)
195
+ rescue Exception => e
196
+ event.respond("❌ An error has occured!! ```ruby\n#{e}```Please contact the bot owner with the above message for assistance.")
197
+ end
198
+ end
127
199
  break
128
200
  end
129
201
  }
metadata CHANGED
@@ -1,29 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: commandrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seriel Shirogane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-18 00:00:00.000000000 Z
11
+ date: 2017-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: discordrb
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.1'
17
20
  - - ">="
18
21
  - !ruby/object:Gem::Version
19
- version: 3.0.0
22
+ version: 3.1.0
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '3.1'
24
30
  - - ">="
25
31
  - !ruby/object:Gem::Version
26
- version: 3.0.0
32
+ version: 3.1.0
27
33
  description: A customisable and easy to use Commands System for Discordrb.
28
34
  email: seriel@fl0.co
29
35
  executables: []
@@ -51,7 +57,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
57
  version: '0'
52
58
  requirements: []
53
59
  rubyforge_project:
54
- rubygems_version: 2.5.2
60
+ rubygems_version: 2.6.11
55
61
  signing_key:
56
62
  specification_version: 4
57
63
  summary: Commandrb