commandorobo 0.0.2

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 +7 -0
  2. data/lib/commandorobo.rb +211 -0
  3. data/lib/constants.rb +34 -0
  4. data/lib/util.rb +61 -0
  5. metadata +66 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3c1b24d072f07081dfdb1df6ab18501fa4530cca6162e95e0106a3eaac9bf26c
4
+ data.tar.gz: 20e5d0e5d5accd3f4287d88edb8d46169aea27beb98ec74cc79de43d441b33b6
5
+ SHA512:
6
+ metadata.gz: 54ceee9db695de74bff50d97b5f478b1032b64b4feda0e23434b446c8bd1254c1454698469e1204995a9de9a0bc31646d217eb24895aef36e52ff44965384653
7
+ data.tar.gz: cb7ac9cf4cd91924668dba2792bc90f0709c8a71fceb754256e2fd115dd5fc0051cd4bf1cf3558e3d1387cbb55fc766cc801aabf18c9b67ab8026f8915ff73c2
@@ -0,0 +1,211 @@
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
+ module Commandorobo
10
+ include Constants
11
+ include Utils
12
+
13
+ class NoPermission
14
+ attr_reader :perm
15
+ def initialize(perm)
16
+ @perm = perm
17
+ end
18
+
19
+ def prettify # OH GOD PLEASE HELP ME
20
+ @perm.map do |t| # <hackiness>
21
+ t.to_s.split(/_/).map(&:capitalize).join(' ')
22
+ end # </hackiness>
23
+ end
24
+ end
25
+
26
+ class Invoker
27
+ attr_reader :value, :type
28
+ def initialize(val, type, **kwargs)
29
+ if type == :regex
30
+ val = Regexp::new(val)
31
+ end
32
+ if !kwargs[:sep].nil? && type == :dual
33
+ val = val.split(kwargs[:sep])
34
+ end
35
+ @value = val
36
+ @type = type
37
+ end
38
+
39
+ def match(text)
40
+ if @type != :regex
41
+ raise "Incorrect type for function."
42
+ end
43
+ return @value.match(text)
44
+ end
45
+
46
+ def extrapolate(text) # I WANT AN EXCUSE TO SAY EXTRAPOLATE OK
47
+ case @type
48
+ when :prefix
49
+ return text[@value.length..text.length]
50
+ when :suffix
51
+ return text[0..-@value.length-1]
52
+ when :dual
53
+ return text[@value[0].length..-@value[1].length-1]
54
+ when :regex
55
+ return self.match(text)
56
+ end
57
+ end
58
+
59
+ def check(text)
60
+ case @type
61
+ when :prefix
62
+ return text.start_with? @value
63
+ when :suffix
64
+ return text.end_with? @value
65
+ when :dual
66
+ return text.start_with?(@value[0]) && text.end_with?(@value[1])
67
+ when :regex
68
+ return !self.match(text).nil?
69
+ end
70
+ end
71
+ end
72
+
73
+ class Arguments
74
+ attr_reader :raw
75
+ def initialize(raw)
76
+ @raw = raw
77
+ end
78
+
79
+ def switches
80
+ Commandorobo::Utils::consume_switch(@raw.join(' '))
81
+ end
82
+
83
+ def noswitch
84
+ Commandorobo::Utils::remove_switch(@raw.join(' '))
85
+ end
86
+ end
87
+
88
+ class Command
89
+ attr_reader :name, :code, :permissions, :description, :invokers
90
+ def initialize(name, code, permissions, description, invokers, bot)
91
+ @name = name
92
+ @code = code
93
+ @permissions = permissions
94
+ @description = description
95
+ @invokers = invokers.nil? ? [name] : invokers
96
+ @bot = bot
97
+ end
98
+
99
+ def invoke(event, args)
100
+ @code.call(event, args)
101
+ end
102
+
103
+ def perm_check(event)
104
+ perms = {}
105
+ @permissions.each do |p|
106
+ if p == :bot_owner
107
+ perms[p] = @bot.config['owner'].include?(event.author.id)
108
+ else
109
+ perms[p] = event.author.permission?(p)
110
+ end
111
+ end
112
+ if !perms.values.all?
113
+ noperms = []
114
+ perms.keys.each do |p|
115
+ if !perms[p]
116
+ noperms << p
117
+ end
118
+ end
119
+ Commandorobo::NoPermission.new(noperms)
120
+ else
121
+ true
122
+ end
123
+ end
124
+ end
125
+
126
+ class Bot < Discordrb::Bot
127
+ attr_accessor :invokers
128
+ attr_reader :config, :commands, :listeners # what the hell ruby
129
+ def initialize(config, token, **kwargs)
130
+ @config = config
131
+ @commands = []
132
+ @listeners = {}
133
+ @invokers = config['invokers'].map {|i| Commandorobo::Invoker.new(i[1], i[0].to_sym)}
134
+ super(token: token, **kwargs)
135
+ # begin command
136
+ self.message do |ev|
137
+ meme = self.get_invoker(ev.text)
138
+ if meme.nil?
139
+ next
140
+ end
141
+ awau = meme.extrapolate(ev.text)
142
+ if awau.is_a?(MatchData)
143
+ awau = awau[1].split(/ /)
144
+ else
145
+ awau = awau.split(/ /)
146
+ end
147
+ cmd = awau.first
148
+ acmd = self.get_command cmd.to_sym
149
+ awau.shift
150
+ sm = awau
151
+ if !acmd.nil?
152
+ pc = acmd.perm_check(ev)
153
+ if pc.is_a?(Commandorobo::NoPermission)
154
+ self.idispatch(:command_noperms, ev, acmd, pc)
155
+ next
156
+ end
157
+ begin
158
+ a = acmd.invoke(ev, Commandorobo::Arguments.new(sm))
159
+ ev.respond(a)
160
+ rescue Exception => err
161
+ self.idispatch(:command_error, ev, acmd, err)
162
+ else
163
+ self.idispatch(:command_notfound, ev, acmd)
164
+ end
165
+ end
166
+ end
167
+ end
168
+
169
+ def get_command(name)
170
+ @commands.select {|c| c.invokers.include? name}.compact[0]
171
+ end
172
+
173
+ def evt(name, &block)
174
+ if name.is_a? String
175
+ name = name.to_sym
176
+ end
177
+ if @listeners[name].nil?
178
+ @listeners[name] = []
179
+ end
180
+ @listeners[name] << block
181
+ end
182
+
183
+ def idispatch(name, *args)
184
+ if name.is_a? String
185
+ name = name.to_sym
186
+ end
187
+ thing = @listeners[name]
188
+ if thing.nil?
189
+ raise "No event hooks registered for #{name.to_s}"
190
+ else
191
+ thing.each do |func|
192
+ func.call(*args)
193
+ end
194
+ end
195
+ end
196
+
197
+ def cmd(sym, perms:[], desc:nil, invokers:[], &block)
198
+ invokers << sym
199
+ @commands << Commandorobo::Command.new(sym, block, perms, desc, invokers, self)
200
+ end
201
+
202
+ def invoke_by(thing, type, **kwargs)
203
+ @invokers << Commandorobo::Invoker.new(thing, type, kwargs)
204
+ end
205
+
206
+ def get_invoker(text)
207
+ @invokers.map {|a| a if a.check text}.reject(&:!).first # reject all false
208
+ end
209
+ end
210
+ end
211
+
data/lib/constants.rb ADDED
@@ -0,0 +1,34 @@
1
+ module Commandorobo
2
+ module Constants
3
+ ValidPerms = [
4
+ :create_instant_invite,
5
+ :kick_members,
6
+ :ban_members,
7
+ :administrator,
8
+ :manage_channels,
9
+ :manage_server,
10
+ :add_reactions,
11
+ :view_audit_log,
12
+ :read_messages,
13
+ :send_messages,
14
+ :send_tts_messages,
15
+ :manage_messages,
16
+ :embed_links,
17
+ :attach_files,
18
+ :read_message_history,
19
+ :mention_everyone,
20
+ :use_external_emoji,
21
+ :connect,
22
+ :speak,
23
+ :mute_members,
24
+ :deafen_members,
25
+ :move_members,
26
+ :use_voice_activity,
27
+ :change_nickname,
28
+ :manage_nicknames,
29
+ :manage_roles,
30
+ :manage_webhooks,
31
+ :manage_emojis
32
+ ].freeze
33
+ end
34
+ end
data/lib/util.rb ADDED
@@ -0,0 +1,61 @@
1
+ # Utility module for commandorobo
2
+ # Written by ry00001
3
+
4
+ module Commandorobo
5
+ module Utils
6
+ def self.nodash(i)
7
+ i.split('').reject {|j| j == '-'}.join('')
8
+ end
9
+
10
+ def self.consume_switch(str) # this function does not eat nintendo switches. do not try to feed it those.
11
+ nextarg = nil
12
+ parsed = false
13
+ switches = {}
14
+ ws = str.split(' ')
15
+ ws.each_with_index do |k, i|
16
+ parsed = false
17
+ if k.start_with?('-') && k.length > 1
18
+ # oh heck a switch
19
+ if k[1] == '-'
20
+ # oh heck another switch
21
+ k = self.nodash(k)
22
+ switches[k.to_sym] = ws[i+1].nil? && !ws[i+1].start_with?('-') ? true : ws[i+1]
23
+ else
24
+ # no double-switch: interpret literally
25
+ k = self.nodash(k)
26
+ if k.length == 1
27
+ switches[k.to_sym] = ws[i+1].nil? && !ws[i+1].start_with?('-') ? true : ws[i+1]
28
+ else
29
+ k.chars.each do |l|
30
+ switches[l.to_sym] = true
31
+ end
32
+ switches[switches.keys.last] = ws[i+1].nil? && !ws[i+1].start_with?('-') ? true : ws[i+1]
33
+ end
34
+ end
35
+ end
36
+ end
37
+ return switches
38
+ end
39
+
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
+
44
+ def self.remove_switch(str)
45
+ parsed = []
46
+ skip = false
47
+ str.split(' ').each do |i|
48
+ if skip
49
+ skip = false
50
+ next
51
+ end
52
+ if i.start_with?('-') && i.length > 1
53
+ skip = true
54
+ else
55
+ parsed << i
56
+ end
57
+ end
58
+ parsed
59
+ end
60
+ end
61
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: commandorobo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - ry00001
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-03-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: discordrb
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.1'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 3.1.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '3.1'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 3.1.0
33
+ description: A command framework for discordrb.
34
+ email: othery00001@gmail.com
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - lib/commandorobo.rb
40
+ - lib/constants.rb
41
+ - lib/util.rb
42
+ homepage: https://github.com/ry00001/commandorobo
43
+ licenses:
44
+ - MIT
45
+ metadata: {}
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 2.7.6
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: Command framework for discordrb
66
+ test_files: []