-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.rb
311 lines (241 loc) · 7.26 KB
/
bot.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/env ruby
require 'discordrb'
require 'json'
OK = "\U1F44C"
BYE = "\U1F44B"
NO = "\u274E".freeze
YES = "\u2705".freeze
File.open(ARGV[0] || "./config.json", "r+") do |f|
f.write("{\n \n}") if f.read.empty?
f.seek 0
CONFIG = JSON.parse(f.read)
end
File.open("./help.json", "r+") do |f|
f.write("{\n \n}") if f.read.empty?
f.seek 0
HELP = JSON.parse(f.read).map do |k1, v1|
k1 = k1.to_sym
v1 = v1.map { |k2, v2| [k2.to_sym, v2] }.to_h
[k1, v1]
end.to_h
end
class CommandBot < Discordrb::Commands::CommandBot
def pick_quote
begin
quotes = File.read("./quotes.json")
quotes = JSON.parse(quotes)["frases"]
raise unless quotes.any?
rescue Exception => e
puts e.backtrace.join ""
puts e.message
"Manutenção sempre é foda nesse país..."
else
quotes.sample
end
end
def add_quote quote
begin
quotes = File.read("./quotes.json")
quotes = JSON.parse(quotes)["frases"] << quote
File.open("./quotes.json", "w") do |f|
f.puts(JSON.pretty_generate({"frases" => quotes}))
end
rescue Exception => e
puts e.backtrace.join ""
puts e.message
end
end
def log_event event
author = event.author
server = event.server
channel = event.channel
message = event.message
time = Time.now
name = "#{author.name}##{author.discriminator}"
out = [
"[#{time}]:",
" Message: #{message.id}",
" Content: #{message.content}",]
if channel.private?
if author.id == CONFIG["client_id"]
author = event.channel.recipient
name = "#{author.name}##{author.discriminator}"
out.insert(1, " Recipient: #{name} (#{author.id})")
else
out.insert(1, " Sender: #{name} (#{author.id})")
end
elsif channel.group?
if author.id == CONFIG["client_id"]
out.insert(1, " Group: #{channel.name} (#{channel.id})")
else
out.insert(1, " Author: #{name} (#{author.id})")
out.insert(2, " Group: #{channel.name} (#{channel.id})")
end
elsif server
out.insert(1, " Author: #{name} (#{author.id})")
out.insert(2, " Server: #{server.name} (#{server.id})")
out.insert(2, " Channel: #{channel.name} (#{channel.id})")
end
File.open("./requests.log", "a+") do |f|
f.puts out.join("\n")
end
end
def add_prefix id, prefix
begin
prefixes = File.read("./prefixes.json")
prefixes = JSON.parse(prefixes)
prefixes[id.to_s] = prefix
File.open("./prefixes.json", "w") do |f|
f.puts(JSON.pretty_generate(prefixes))
end
rescue Exception => e
puts e.backtrace.join ""
puts e.message
end
end
def get_prefixes
File.open("./prefixes.json", "r+") do |f|
f.write("{\n \n}") if f.read.empty?
f.seek 0
JSON.parse(f.read)
end
end
end
prefix_proc = proc do |message|
prefixes = File.open("./prefixes.json", "r+") do |f|
f.write("{\n \n}") if f.read.empty?
f.seek 0
JSON.parse(f.read)
end
id = message.channel.server.id rescue message.channel.id
prefix = prefixes[id.to_s] || CONFIG["prefix"]
message.content[prefix.size .. -1] if message.content.start_with? prefix
end
OPT = {
:token => CONFIG["bot_token"],
:client_id => CONFIG["client_id"],
:prefix => prefix_proc,
:parse_self => true
}
bot = CommandBot.new OPT
bot.command :add, HELP[:add] do |event, *text|
bot.log_event(event) unless event.channel.private?
if event.channel.private? and event.author.id != CONFIG["bot_owner"]
next "Muleque espertinho... (Sua frase **não** foi adicionada)"
end
if text.include? bot.profile.mention
next "Muleque espertinho... (Sua frase **não** foi adicionada)"
end
text = text.join(' ')
begin
bot.add_quote(text)
rescue Exception => e
puts e.backtrace.join ""
puts e.message
else
"Acha que eu sou gado pra ficar me mamando? (Sua frase foi adicionada)"
end
end
bot.command :say, HELP[:say] do |event, *text|
next CONFIG["no_permission"] unless event.user.id == CONFIG["bot_owner"]
bot.log_event(event) unless event.channel.private?
event.message.delete() rescue puts "Couldn't delete message"
text.join(' ')
end
bot.command :eval, HELP[:eval] do |event, *text|
next CONFIG["no_permission"] unless event.user.id == CONFIG["bot_owner"]
bot.log_event(event) unless event.channel.private?
begin
event << eval(text.join(" "))
rescue Exception => e
puts e.backtrace.join ""
puts e.message
end
end
bot.command :ping, HELP[:ping] do |event|
mention = event.author.mention
time = (Time.now - event.timestamp).round(6) * 1000
bot.log_event(event) unless event.channel.private?
"#{mention} Criança, não me enche o saco (#{time.round(3)}ms)"
end
bot.command :prefix, HELP[:prefix] do |event, *text|
text = text.join(' ')
bot.log_event(event) unless event.channel.private?
if event.server
permission = event.user.permission?(:administrator, event.channel)
unless permission || event.user.id == CONFIG["bot_owner"]
next "Apenas administradores podem mudar o prefixo do servidor."
end
end
id = event.server.id rescue event.channel.id
prefix = bot.get_prefixes()[id.to_s] || CONFIG["prefix"]
next "Prefixo atual: \"#{prefix}\". Use `#{prefix}prefix Novo prefixo` para alterá-lo." if text.empty?
bot.add_prefix(id, text)
"Prefixo alterado: de \"#{prefix}\" para \"#{text}\""
end
bot.command :source, HELP[:source] do |event|
bot.log_event(event) unless event.channel.private?
CONFIG["source_repo"]
end
bot.command :invite, HELP[:invite] do |event|
bot.log_event(event) unless event.channel.private?
event.bot.invite_url
end
bot.command :restart, HELP[:restart] do |event|
next CONFIG["no_permission"] unless event.user.id == CONFIG["bot_owner"]
bot.log_event(event) unless event.channel.private?
event.message.react(BYE) rescue nil
exit 0
end
bot.command :quit, HELP[:quit] do |event|
next CONFIG["no_permission"] unless event.user.id == CONFIG["bot_owner"]
bot.log_event(event) unless event.channel.private?
event.message.react(BYE) rescue nil
exit 1
end
begin
require 'readline'
rescue LoadError
nil
else
bot.run :async
end
bot.mention contains: /prefixo?/i, from: not!(bot.profile) do |event|
begin
id = event.server.id rescue event.channel.id
prefix = bot.get_prefixes()[id] || "-"
event << "Prefixo atual: \"#{prefix}\". Use `#{prefix}prefix Novo prefixo` para alterá-lo."
rescue Exception => e
puts e.backtrace.join ""
puts e.message
ensure
bot.log_event(event) unless event.channel.private?
end
end
bot.mention contains: not!(/prefixo?/i), from: not!(bot.profile) do |event|
begin
id = event.server.id rescue event.channel.id
prefix = bot.get_prefixes()[id.to_s] || CONFIG["prefix"]
unless event.message.text.start_with? "#{prefix}add"
event << bot.pick_quote
end
rescue Exception => e
puts e.backtrace.join ""
puts e.message
ensure
bot.log_event(event) unless event.channel.private?
end
end
bot.private_message do |event|
bot.log_event(event)
end
bot.message from: bot.profile, private: false do |event|
bot.log_event(event)
end
begin
require './shell'
shell = Shell.new(bot, CONFIG)
shell.loop()
rescue LoadError
bot.run
end