-
Notifications
You must be signed in to change notification settings - Fork 0
/
antiraider.rb
476 lines (460 loc) · 15 KB
/
antiraider.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# Copyright (c) 2021 Fossium-Team
# See LICENSE in the project root for license information.
require 'discordrb'
require 'time'
require 'fileutils'
require 'json'
def writeconfig
print "Enter your bot token: "
token = gets.chomp
print "Is this correct? \"#{token}\"\n(y)es\n(n)o\n"
confirmation = gets.chomp
if confirmation.downcase != "y" && confirmation.downcase != "yes"
puts "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nWrong token given"
puts "Re-running config creator\n\n"
writeconfig
return
end
puts "Writing config..."
confighash = {:token => ""}
if File.exists?('./config.json')
configfile = File.read('./config.json')
confighash = JSON.parse(configfile)
end
confighash['token'] = token
File.open('./config.json', 'w') { |file| file.write(JSON.dump(confighash)) }
end
unless File.exist?('./config.json')
puts "No config found\nStarting the config creator...\n"
writeconfig
puts "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nStarting the bot..."
end
if File.directory?('temp/')
FileUtils.remove_dir('temp/')
end
Dir.mkdir('temp/')
jsonfile = File.read('./config.json')
$jsonhash = JSON.parse(jsonfile)
def validatetoken
begin
Discordrb::API.validate_token("Bot #{$jsonhash['token']}")
rescue
puts "Oops...\nSomething went wrong"
puts "In most cases this means that the token is invalid or that you did not enable all intents"
puts "Do you want to\n(r)ewrite the config\n(q)uit\n"
answer = gets.chomp
if answer == "r"
puts "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nStarting the config creator..."
writeconfig
puts "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nStarting the bot...\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
validatetoken
return
elsif answer == "q"
exit
else
puts "That option does not exist\n"
validatetoken
return
end
end
end
class String
public
def is_number?
begin
Float(self)
return true
rescue
return false
end
end
end
def captchagen
question = ""
answer = ""
loop do
randomoperation = rand 1..4
if randomoperation == 1
operation = "+"
elsif randomoperation == 2
operation = "-"
elsif randomoperation == 3
operation = "x"
elsif randomoperation == 4
operation = ":"
end
number1 = rand 3..10
number2 = rand 3..10
question = "#{number1} #{operation} #{number2}"
if operation == "x"
answer = number1.send(:*, number2)
elsif operation == ":"
answer = number1.send(:/, number2)
elsif operation == "+"
answer = number1.send(:+, number2)
elsif operation == "-"
answer = number1.send(:-, number2)
end
break if !(answer % 1 != 0) && Integer(answer) >= 0
end
return question, answer
end
validatetoken
bot = Discordrb::Commands::CommandBot.new(token: $jsonhash['token'], prefix: "ar!", help_command: false, command_doesnt_exist_message: "Oops...\n`%command%` is not a valid command", spaces_allowed: true, compress_mode: :large, intents: [:servers, :server_messages, :server_bans, :server_emojis, :server_integrations, :server_webhooks, :server_invites, :server_voice_states, :server_presences, :server_message_reactions, :server_message_typing, :direct_messages, :direct_message_reactions, :direct_message_typing, :server_members])
bot.ready do |_|
puts "----------------------------------------"
puts "Connected!"
puts "Logged in as #{bot.profile.name}##{bot.profile.discriminator}"
puts "----------------------------------------"
bot.watching = "for raids | ar!help"
end
bot.mention do |event|
event.channel.send_message("My prefix is `ar!`", false, nil, nil, nil, event.message)
end
bot.command :help do |event|
event.channel.send_embed do |embed|
embed.colour = 0x0080FF
embed.title = 'Help'
embed.fields = [
Discordrb::Webhooks::EmbedField.new(
name: 'Config',
value: '`ar!config`'
)
]
end
end
bot.command :config, description: 'Configure the bot' do |event, category, setting, option|
unless event.user.permission?(:administrator)
event.channel.send_embed do |embed|
embed.colour = 0xFF0000
embed.title = 'Oops..'
embed.description = "You need to have the `Administrator` permission to use this command"
end
next
end
unless category
configfile = File.read('./config.json')
confighash = JSON.parse(configfile)
if confighash['timespan'] == nil
timespan = "60"
else
timespan = Integer(confighash['timespan']) * 60
end
if confighash['maxjoins'] == nil
maxjoins = "3"
else
maxjoins = Integer(confighash['maxjoins'])
end
if confighash['antiraidenabled'] == nil
antiraidenabled = "false"
else
antiraidenabled = confighash['antiraidenabled']
end
if confighash['captchaenabled'] == nil
captchaenabled = "false"
else
captchaenabled = confighash['captchaenabled']
end
if confighash['captcharole'] == nil
captcharole = "none"
else
captcharole = confighash['captcharole']
end
if confighash['joinrole'] == nil
joinrole = "none"
else
joinrole = confighash['joinrole']
end
event.channel.send_embed do |embed|
embed.colour = 0x0080FF
embed.title = 'Settings'
embed.fields = [
Discordrb::Webhooks::EmbedField.new(
name: 'AntiRaid:',
value: "**Enabled**\nCurrently: #{antiraidenabled}\n**Rate limit**\nCurrently: #{maxjoins} new member(s) every #{timespan} seconds"
),
Discordrb::Webhooks::EmbedField.new(
name: 'Captcha:',
value: "**Enabled**\nCurrently: #{captchaenabled}\n**Captcha role** (The role given after a user passes the captcha)\nCurrently: `#{captcharole}`"
),
Discordrb::Webhooks::EmbedField.new(
name: 'Misc:',
value: "**joinrole** (The role given when a user joins the server)\nCurrently: #{joinrole}"
),
Discordrb::Webhooks::EmbedField.new(
name: 'Categories and options',
value: "AntiRaid:\n- **enabled** - `true` or `false`\n- **ratelimit** - number/number\nCaptcha:\n- **enabled** - `true` or `false`\n- **role** - role ID\nMisc:\n- **joinrole** - role ID or none\n\nFor example: `ar!config antiraid ratelimit 3/60`\nor `ar!config captcha enabled true`"
)
]
end
next
end
unless setting
event.channel.send_embed do |embed|
embed.colour = 0xFF0000
embed.title = 'Oops...'
embed.description = 'No setting given'
end
next
end
unless option
event.channel.send_embed do |embed|
embed.colour = 0xFF0000
embed.title = 'Oops...'
embed.description = 'No option given'
end
next
end
if category.downcase == 'antiraid'
if setting.downcase == 'enabled'
if option.downcase != "true" && option.downcase != "false"
event.channel.send_embed do |embed|
embed.colour = 0xFF0000
embed.title = 'Oops...'
embed.description = "Incorrect option\nCorrect options: true and false"
end
next
end
configfile = File.read('./config.json')
confighash = JSON.parse(configfile)
confighash['antiraidenabled'] = option.downcase
File.open("./config.json", 'w') { |file| file.write(JSON.dump(confighash)) }
event.channel.send_embed do |embed|
embed.colour = 0x2ECC70
embed.title = "Set `antiraidenabled` to `#{option.downcase}`"
end
next
elsif setting.downcase == 'ratelimit'
option = option.downcase.split('/')
unless option[2] == nil
event.channel.send_embed do |embed|
embed.colour = 0xFF0000
embed.title = 'Oops...'
embed.description = 'Invalid syntax'
end
next
end
unless option[0].is_number? && option[1].is_number?
event.channel.send_embed do |embed|
embed.colour = 0xFF0000
embed.title = 'Oops...'
embed.description = 'That is not a number'
end
next
end
configfile = File.read('./config.json')
confighash = JSON.parse(configfile)
confighash['maxjoins'] = option[0]
confighash['timespan'] = option[1]
File.open("./config.json", 'w') { |file| file.write(JSON.dump(confighash)) }
event.channel.send_embed do |embed|
embed.colour = 0x2ECC70
embed.title = "Set `ratelimit` to `#{option.downcase}`"
end
next
end
elsif category.downcase == 'captcha'
if option.downcase == 'enabled'
if option.downcase != "true" && option.downcase != "false"
event.channel.send_embed do |embed|
embed.colour = 0xFF0000
embed.title = 'Oops...'
embed.description = "Incorrect option\nCorrect options: true and false"
end
next
end
configfile = File.read('./config.json')
confighash = JSON.parse(configfile)
if confighash['captcharole'] == nil && option.downcase != "false"
event.channel.send_embed do |embed|
embed.colour = 0xFF0000
embed.title = 'Oops...'
embed.description = "Please set `captcharole` first"
end
next
end
confighash['captchaenabled'] = option.downcase
File.open("./config.json", 'w') { |file| file.write(JSON.dump(confighash)) }
event.channel.send_embed do |embed|
embed.colour = 0x2ECC70
embed.title = "Set `captchaenabled` to `#{option.downcase}`"
end
next
elsif option.downcase == 'role'
unless option.is_number?
event.channel.send_embed do |embed|
embed.colour = 0xFF0000
embed.title = 'Oops...'
embed.description = 'That is not a number'
end
next
end
if event.server.role(option) == nil
event.channel.send_embed do |embed|
embed.colour = 0xFF0000
embed.title = 'Oops...'
embed.description = "That role doesn't exist"
end
next
end
configfile = File.read('./config.json')
confighash = JSON.parse(configfile)
confighash['captcharole'] = option
File.open("./config.json", 'w') { |file| file.write(JSON.dump(confighash)) }
event.channel.send_embed do |embed|
embed.colour = 0x2ECC70
embed.title = "Set `captcharole` to `#{option}`"
end
next
end
elsif category.downcase == 'misc'
if setting.downcase == 'joinrole'
unless option.is_number?
event.channel.send_embed do |embed|
embed.colour = 0xFF0000
embed.title = 'Oops...'
embed.description = 'That is not a number'
end
next
end
if event.server.role(option) == nil
event.channel.send_embed do |embed|
embed.colour = 0xFF0000
embed.title = 'Oops...'
embed.description = "That role doesn't exist"
end
next
end
configfile = File.read('./config.json')
confighash = JSON.parse(configfile)
confighash['joinrole'] = option
File.open("./config.json", 'w') { |file| file.write(JSON.dump(confighash)) }
event.channel.send_embed do |embed|
embed.colour = 0x2ECC70
embed.title = "Set `joinrole` to `#{option}`"
end
next
end
end
end
bot.member_join do |event|
unless event.server.bot.permission?(:kick_members)
event.server.members.each do |member|
if member.bot_account
next
end
if member.permission?(:administrator)
member.pm.send_embed do |embed|
embed.colour = 0xFF0000
embed.title = 'Oops...'
embed.description = "I do not have the kick members permission\nI need the kick members permission to kick users that are raiding your server"
end
end
end
end
configfile = File.read('./config.json')
confighash = JSON.parse(configfile)
if confighash['timespan'] == nil
timespan = 60
else
timespan = Integer(confighash['timespan'])
end
if confighash['maxjoins'] == nil
maxjoins = 3
else
maxjoins = Integer(confighash['maxjoins'])
end
if File.exist?("temp/#{event.server.id}.json")
logfile = File.read("temp/#{event.server.id}.json")
loghash = JSON.parse(logfile)
filetime = Time.parse(loghash['time'])
difference = Time.now - filetime
if difference >= timespan
loghash = {:time => Time.now}
loghash['joins'] = "1"
File.open("temp/#{event.server.id}.json", 'w') { |file| file.write(JSON.dump(loghash)) }
next
end
loghash['joins'] = "#{Integer(loghash['joins']) + 1}"
File.open("temp/#{event.server.id}.json", 'w') { |file| file.write(JSON.dump(loghash)) }
joincount = Integer(loghash['joins'])
if joincount >= maxjoins
event.user.pm.send_embed do |embed|
embed.colour = 0xFF0000
embed.title = 'Kicked by AntiRaider'
embed.description = "You have been kicked by AntiRaider because you might be tring to raid that server"
embed.footer = Discordrb::Webhooks::EmbedFooter.new(text: 'If you are not trying to raid please wait a few minutes and try to join again')
end
begin
event.server.kick(event.user, 'Might be a raider - AntiRaider')
rescue
event.server.members.each do |member|
if member.bot_account
next
end
if member.permission?(:administrator)
member.pm.send_embed do |embed|
embed.colour = 0xFF0000
embed.title = 'Oops...'
embed.description = "I could not kick #{member.username}##{member.discriminator}"
end
end
end
ensure
next
end
end
else
loghash = {:time => Time.now}
loghash['joins'] = "1"
File.open("temp/#{event.server.id}.json", 'w') { |file| file.write(JSON.dump(loghash)) }
end
if confighash['captchaenabled'] == nil || confighash['captchaenabled'] == 'false' && confighash['joinrole'] != nil
joinrole = event.server.role(confighash['joinrole'])
event.user.add_role(joinrole)
next
end
if confighash['captchaenabled'] == 'true'
captcharole = event.server.role(confighash['captcharole'])
question, answer = captchagen
event.user.pm.send_embed do |embed|
embed.colour = 0x0080FF
embed.title = 'Captcha'
embed.description = "What is `#{question}`"
end
triesleft = 5
event.user.await! do |captcha|
unless captcha.message.content.is_number?
false
next
end
if Integer(captcha.message.content) == answer
event.user.pm("Correct!")
event.user.add_role(captcharole, 'Passed captcha - AntiRaider')
true
else
triesleft -= 1
if triesleft == 0
event.user.pm('You are out of tries')
event.server.kick(event.user, 'Failed captcha - AntiRaider')
true
end
if triesleft == 1
begin
event.user.pm("Incorrect, you have #{triesleft} try left")
rescue
end
elsif triesleft <= -1
next
else
begin
event.user.pm("Incorrect, you have #{triesleft} tries left")
rescue
end
end
false
end
end
end
end
bot.run