-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.py
executable file
·494 lines (432 loc) · 18.8 KB
/
main.py
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
import datetime
import importlib
import json
import logging as logger
import os
from sys import modules
import aiohttp
import aiomysql
import discord
import urllib3
from discord.ext import commands, tasks
from dotenv import load_dotenv
import sql
from cogs import punishments
from cogs.logging import update_leaderboards
log = logger.getLogger('discord')
log.setLevel(logger.INFO)
handler = logger.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
handler.setFormatter(logger.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
log.addHandler(handler)
urllib3.disable_warnings()
global PRELOADED_MODULES
# sys and importlib are ignored here too
PRELOADED_MODULES = set(modules.values())
load_dotenv()
token = os.getenv('DISCORD_TOKEN')
gh_token = os.getenv('GITHUB_TOKEN')
nebula_token = os.getenv('NEBULA_TOKEN')
is_testing = os.getenv('TESTING')
is_testing = True if is_testing == '1' else False
def get_prefix(client, message):
"""Returns the prefix for the specified server"""
if message.guild is None:
return "!"
with open('data/prefixes.json', 'r') as f:
prefixes = json.load(f)
return prefixes[str(message.guild.id)]
intents = discord.Intents.default()
intents.members = True
intents.typing = False
intents.presences = False
bot = commands.Bot(command_prefix='!', intents=intents)
bot.remove_command('help')
bot.owner_id = 196282885601361920
bot.gh_token = gh_token
bot.nebula_token = nebula_token
with open('data/variables.json', 'r') as file:
bot.maintenance_mode = json.load(file).get("maintenance_mode")
@bot.event
async def on_ready():
"""Wait until bot has connected to discord"""
print("Connected to discord")
bot.pool = await aiomysql.create_pool(host=os.getenv("MYSQL_HOST"), port=3306, user='jacobvs', password=os.getenv("MYSQL_PASSWORD"),
db='mysql', loop=bot.loop, connect_timeout=60)
print("Connected to DB")
bot.start_time = datetime.datetime.now()
bot.raid_db = {}
bot.mapmarkers = {}
bot.players_in_game = []
bot.active_raiders = {}
bot.serverwleaderboard = []
await build_guild_db()
for g in bot.guild_db:
if bot.guild_db[g][sql.gld_cols.leaderboardchannel] and bot.guild_db[g][sql.gld_cols.zerorunchannel]:
bot.serverwleaderboard.append(g)
bot.raid_db[g] = {"afk": {}, "cp": {}, "leaders": []}
if bot.maintenance_mode:
await bot.change_presence(status=discord.Status.idle, activity=discord.Game("IN MAINTENANCE MODE!"))
else:
await bot.change_presence(status=discord.Status.online, activity=discord.Game("!patreon | !modmail"))
bot.loop.create_task(update_leaderboards(bot))
bot.active_punishments = {}
if not is_testing:
active = await sql.get_all_active_punishments(bot.pool)
for p in active:
guild = bot.get_guild(p[sql.punish_cols.gid])
if guild:
member = guild.get_member(p[sql.punish_cols.uid])
if member:
ptype = p[sql.punish_cols.type]
until = p[sql.punish_cols.endtime]
tsecs = (until - datetime.datetime.utcnow()).total_seconds()
if ptype == 'suspend':
roles = await sql.get_suspended_roles(bot.pool, member.id, guild)
t = bot.loop.create_task(punishments.punishment_handler(bot, guild, member, ptype, tsecs, roles))
else:
t = bot.loop.create_task(punishments.punishment_handler(bot, guild, member, ptype, tsecs))
bot.active_punishments[str(guild.id) + str(member.id) + ptype] = t
init_queues()
bot.in_queue = {0: [], 1: []}
guild = bot.get_guild(660344559074541579)
bot.patreon_role = guild.get_role(736954974545641493)
pdata = await sql.get_all_patreons(bot.pool)
lst = [r[0] for r in pdata]
bot.patreon_ids = set(lst)
bot.events = {}
event_update.start()
patreon_role.start()
bot.beaned_ids = set([])
for g in bot.guilds:
for vc in g.voice_channels:
if 'Raiding -' in vc.name:
try:
print(f"Removing temporary VC in {g.name} | name: {vc.name}")
await vc.delete(reason='Removing temporary channel on restart.')
except Exception:
print("Removing temp channel failed!")
print(f'{bot.user.name} is now online!')
async def build_guild_db():
bot.guild_db = await sql.construct_guild_database(bot.pool, bot)
# Link queue to raiding category (queue_channel, category)
# 1. Dungeoneer (oryx raiding `Queue`)
queue_links = [(660347818061332481, 660347478620766227), (736226011733033041, 736220007356432426)]
def init_queues():
bot.queue_links = {}
bot.queues = {}
bot.morder = {}
for i in queue_links:
queue = bot.get_channel(i[0])
bot.queues[i[0]] = []
for m in queue.members:
bot.queues[i[0]].append(m.id)
category = bot.get_channel(i[1])
bot.queue_links[queue.id] = (queue, category)
bot.queue_links[category.id] = (queue, category)
for g in bot.guilds:
bot.morder[g.id] = {'npriority': 0, 'nnormal': 0, 'nvc': 0}
@bot.command(usage="resetqueues")
@commands.is_owner()
async def resetqueues(ctx):
init_queues()
@bot.command(usage="load <cog>")
@commands.is_owner()
async def load(ctx, extension):
"""Load specified cog"""
extension = extension.lower()
bot.load_extension(f'cogs.{extension}')
await ctx.send('{} has been loaded.'.format(extension.capitalize()))
@bot.command(usage="unload <cog>")
@commands.is_owner()
async def unload(ctx, extension):
"""Unload specified cog"""
extension = extension.lower()
bot.unload_extension(f'cogs.{extension}')
await ctx.send('{} has been unloaded.'.format(extension.capitalize()))
@bot.command(usage="reload <cog/guilds/utils/all>")
@commands.is_owner()
async def reload(ctx, extension):
"""Reload specified cog"""
extension = extension.lower()
if extension == 'guilds':
await build_guild_db()
extension = 'Guild Database'
elif extension == 'all':
import sql
importlib.reload(sql)
import utils
importlib.reload(utils)
import checks
importlib.reload(checks)
import embeds
importlib.reload(embeds)
import cogs.Minigames.blackjack
importlib.reload(cogs.Minigames.blackjack)
import cogs.Minigames.coinflip
importlib.reload(cogs.Minigames.coinflip)
import cogs.Minigames.connect4
importlib.reload(cogs.Minigames.connect4)
import cogs.Minigames.hangman
importlib.reload(cogs.Minigames.hangman)
import cogs.Minigames.highlow
importlib.reload(cogs.Minigames.highlow)
import cogs.Minigames.roulette
importlib.reload(cogs.Minigames.roulette)
import cogs.Minigames.russianroulette
importlib.reload(cogs.Minigames.russianroulette)
import cogs.Minigames.slots
importlib.reload(cogs.Minigames.slots)
import cogs.Minigames.tictactoe
importlib.reload(cogs.Minigames.tictactoe)
import cogs.Raiding.afk_check
importlib.reload(cogs.Raiding.afk_check)
import cogs.Raiding.fametrain
importlib.reload(cogs.Raiding.fametrain)
import cogs.Raiding.headcount
importlib.reload(cogs.Raiding.headcount)
import cogs.Raiding.realmclear
importlib.reload(cogs.Raiding.realmclear)
import cogs.Raiding.vc_select
importlib.reload(cogs.Raiding.vc_select)
bot.reload_extension('cogs.casino')
bot.reload_extension('cogs.core')
bot.reload_extension('cogs.error')
bot.reload_extension('cogs.logging')
bot.reload_extension('cogs.minigames')
bot.reload_extension('cogs.misc')
bot.reload_extension('cogs.moderation')
bot.reload_extension('cogs.punishments')
bot.reload_extension('cogs.raiding')
bot.reload_extension('cogs.verification')
elif extension == 'utils':
import sql
importlib.reload(sql)
import utils
importlib.reload(utils)
import checks
importlib.reload(checks)
import embeds
importlib.reload(embeds)
elif extension == 'raiding':
bot.reload_extension('cogs.raiding')
import cogs.Raiding.afk_check
importlib.reload(cogs.Raiding.afk_check)
import cogs.Raiding.fametrain
importlib.reload(cogs.Raiding.fametrain)
import cogs.Raiding.headcount
importlib.reload(cogs.Raiding.headcount)
import cogs.Raiding.realmclear
importlib.reload(cogs.Raiding.realmclear)
import cogs.Raiding.vc_select
importlib.reload(cogs.Raiding.vc_select)
elif extension == 'logging':
bot.reload_extension('cogs.logging')
import cogs.Raiding.logrun
importlib.reload(cogs.Raiding.logrun)
elif extension == 'casino':
bot.reload_extension('cogs.casino')
import cogs.Minigames.blackjack
importlib.reload(cogs.Minigames.blackjack)
import cogs.Minigames.coinflip
importlib.reload(cogs.Minigames.coinflip)
import cogs.Minigames.connect4
importlib.reload(cogs.Minigames.connect4)
import cogs.Minigames.hangman
importlib.reload(cogs.Minigames.hangman)
import cogs.Minigames.highlow
importlib.reload(cogs.Minigames.highlow)
import cogs.Minigames.roulette
importlib.reload(cogs.Minigames.roulette)
import cogs.Minigames.russianroulette
importlib.reload(cogs.Minigames.russianroulette)
import cogs.Minigames.slots
importlib.reload(cogs.Minigames.slots)
import cogs.Minigames.tictactoe
importlib.reload(cogs.Minigames.tictactoe)
else:
bot.reload_extension(f'cogs.{extension}')
await ctx.send('{} has been reloaded.'.format(extension.capitalize()))
# def rreload(module, paths=None, mdict=None):
# """Recursively reload modules."""
# if paths is None:
# paths = ['']
# if mdict is None:
# mdict = {}
# if module not in mdict:
# # modules reloaded from this module
# mdict[module] = []
# reload(module)
# for attribute_name in dir(module):
# attribute = getattr(module, attribute_name)
# if type(attribute) is ModuleType:
# if attribute not in mdict[module]:
# if attribute.__name__ not in sys.builtin_module_names:
# if os.path.dirname(attribute.__file__) in paths:
# mdict[module].append(attribute)
# rreload(attribute, paths, mdict)
# reload(module)
@bot.command(usage="fleave <id>")
@commands.is_owner()
async def fleave(ctx, id: int):
server = bot.get_guild(id)
await server.leave()
await ctx.send(f"Ooga-Booga has left {server.name}")
@bot.command(usage="maintenance")
@commands.is_owner()
async def maintenance(ctx):
if bot.maintenance_mode:
bot.maintenance_mode = False
await bot.change_presence(status=discord.Status.online, activity=discord.Game("!patreon | !modmail"))
await ctx.send("Maintenance mode has been turned off!")
else:
bot.maintenance_mode = True
await bot.change_presence(status=discord.Status.idle, activity=discord.Game("IN MAINTENANCE MODE!"))
# with open('data/queues.pkl', 'wb') as file:
# pickle.dump(bot.queues, file, pickle.HIGHEST_PROTOCOL)
#
# print('Saved queue to file')
await ctx.send("Maintenance mode has been turned on!")
with open("data/variables.json", 'r+') as f:
data = json.load(f)
data["maintenance_mode"] = bot.maintenance_mode
f.seek(0)
json.dump(data, f)
f.truncate()
for filename in os.listdir('./cogs/'):
if filename.endswith('.py'):
bot.load_extension(f'cogs.{filename[:-3]}')
@bot.command(usage='syncm')
@commands.is_owner()
@commands.guild_only()
async def syncm(ctx):
verified_role = bot.guild_db.get(ctx.guild.id)[sql.gld_cols.verifiedroleid]
m_names = []
# (id, ign, 'verified', ctx.guild.name, alt1, alt2)
async for m in ctx.guild.fetch_members():
if verified_role in m.roles:
if m.nick:
name = None
alt1 = None
alt2 = None
if " | " in m.nick:
ns = m.nick.split(" | ")
for n in ns:
if not name:
name = "".join([c for c in n if c.isalpha()])
elif not alt1:
alt1 = "".join([c for c in n if c.isalpha()])
elif not alt2:
alt2 = "".join([c for c in n if c.isalpha()])
else:
name = "".join([c for c in m.nick if c.isalpha()])
m_names.append((m.id, name, 'verified', ctx.guild.name, alt1, alt2))
await ctx.send(f'Created {len(m_names)} member profiles... Awaiting insertion into DB...')
async with bot.pool.acquire() as conn:
async with conn.cursor() as cursor:
s = f"Replace INTO rotmg.users (id, ign, status, verifiedguilds, alt1, alt2) VALUES (%s, %s, %s, %s, %s, %s)"
await cursor.executemany(s, m_names)
await conn.commit()
await ctx.send(f'Inserted {len(m_names)} members into DB. Success.')
# Error Handlers
@bot.event
async def on_error(event, *args, **kwargs):
"""Log errors for debugging"""
with open('err.log', 'a') as f:
if event == 'on_message':
f.write(f'Unhandled message: {args[0]}\n')
else:
f.write(f'Unhandled error: {args[0]}\n')
# Checks
@bot.check
async def maintenance_mode(ctx):
if bot.maintenance_mode:
if await bot.is_owner(ctx.author):
return True
embed = discord.Embed(title="Error!", description="Ooga-booga has been put into maintenance mode by the developer! "
"This means bugs are being fixed or new features are being added.\n"
"Please be patient and if this persists for too long, contact <@196282885601361920>.",
color=discord.Color.orange())
await ctx.send(embed=embed)
return False
return True
# @bot.check
# async def global_perms_check(ctx):
# # if await bot.is_owner(ctx.author):
# # print(ctx.__dict__)
# # await ctx.invoke(ctx.command)
# # return False
# return True
# looping events
eventTable = {
-0x2: ("Waiting For Events", "https://i.imgur.com/zQ0Ts1X.gif"),
-0x1: ("Realm Closed", "https://i.imgur.com/oR1wqQ3.png"),
0x10d8: ("Killer Bee Nest", "https://i.imgur.com/QA1xlFQ.png"),
0x4b2d: ("Keyper", "https://i.imgur.com/ldJqTmK.png"),
0xb327: ("Aliens", "https://i.imgur.com/fG1jXLw.png"),
0x0e55: ("Beach Bum", "https://static.drips.pw/rotmg/wiki/Enemies/Beach%20Bum.png"),
0x0d59: ("Cube God", "https://static.drips.pw/rotmg/wiki/Enemies/Cube%20God.png"),
0x0e29: ("Cube God", "https://static.drips.pw/rotmg/wiki/Enemies/Cube%20God.png"),
0x67d: ("Cyclops God", "https://static.drips.pw/rotmg/wiki/Enemies/Cyclops%20God.png"),
0xb1d1: ("Rock Dragon", "https://i.imgur.com/ziFj319.png"),
0x091f: ("Ent Ancient", "https://static.drips.pw/rotmg/wiki/Enemies/Ent%20Ancient.png"),
0x6500: ("Dwarf Miner", "https://i.imgur.com/8ra9MdF.png"),
0xb030: ("Lost Sentry", "https://i.imgur.com/WeGhVJI.png"),
0x0e37: ("Ghost Ship", "https://i.imgur.com/5NmRR9I.png"),
0x0d54: ("Grand Sphinx", "https://i.imgur.com/YurYXrE.png"),
0x0d61: ("Hermit God", "https://i.imgur.com/yN5xWM3.png"),
0x091b: ("Lich King", "https://static.drips.pw/rotmg/wiki/Enemies/Lich.png"),
0xb1d1: ("Rock Dragon", "https://i.imgur.com/ziFj319.png"),
0x0d50: ("Lord of the Lost Lands", "https://static.drips.pw/rotmg/wiki/Enemies/Lord%20of%20the%20Lost%20Lands.png"),
0x0d5f: ("Pentaract", "https://static.drips.pw/rotmg/wiki/Enemies/Pentaract%20Tower.png"),
0x0d56: ("Skull Shrine", "https://static.drips.pw/rotmg/wiki/Enemies/Skull%20Shrine.png"),
0x0e51: ("Skull Shrine", "https://static.drips.pw/rotmg/wiki/Enemies/Skull%20Shrine.png"),
0x668: ("Red Demon", "https://static.drips.pw/rotmg/wiki/Enemies/Red%20Demon.png"),
0x734d: ("Avatar", "https://static.drips.pw/rotmg/wiki/Enemies/shtrs%20Defense%20System.png"),
0x6fca: ("Temple Statues", "https://i.imgur.com/0kDx9CD.png"),
0x6fcb: ("Temple Statues", "https://i.imgur.com/URY2vc0.png"),
}
@tasks.loop(seconds=10)
async def event_update():
print('Updating Events')
try:
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(10)) as cs:
async with cs.request("GET", 'https://realmstock.network/Public/EventHistory') as r:
if r.status == 200:
d = await r.read()
lines = d.split()
for t in lines:
l = t.decode('ASCII')
info = l.split("|")
d = eventTable[int(info[0])] if int(info[0]) in eventTable else ("N/A", "https://i.imgur.com/b59f6ff.png")
name = d[0]
img = d[1]
realm = info[1]
server = info[2]
population = int(info[3]) if info[3] != '?' else 404
events_left = int(info[4]) if info[4] != '?' else 404
t = info[5].split(':')
now = datetime.datetime.utcnow()
time = datetime.datetime(year=now.year, month=now.month, day=now.day, hour=int(t[0]), minute=int(t[1])).timestamp()
if server in bot.events:
bot.events[server][str(realm)] = {'Events': events_left, 'Population': population, 'Event': name, 'Image': img, 'Timestamp': time}
else:
bot.events[server] = {str(realm): {'Events': events_left, 'Population': population, 'Event': name, 'Image': img, 'Timestamp': time}}
else:
print("ERROR: Event GET status != 200")
except aiohttp.ClientTimeout:
pass
@tasks.loop(minutes=40)
async def patreon_role():
fungal: discord.Guild = bot.get_guild(635413437647683596)
members = await sql.get_all_patreons(bot.pool)
for m in members:
mem: discord.Member = fungal.get_member(m[sql.usr_cols.id])
if mem:
role = fungal.get_role(757310963635716208)
if role not in mem.roles:
try:
await mem.add_roles(role)
except discord.Forbidden or discord.HTTPException:
pass
print("Attempting to connect to Discord")
bot.run(token)