-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
91 lines (65 loc) · 2.14 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
import logging
import os
import re
import sys
import traceback
import aiohttp
import asyncpg
import discord
import dotenv
from dotenv import load_dotenv
from discord.ext import commands
from cogs import EXTENSIONS
async def get_prefix(bot, message):
extras = ["jd/", "j/"]
comp = re.compile("^(" + "|".join(map(re.escape, extras)) + ").*", flags=re.I)
match = comp.match(message.content)
if match is not None:
extras.append(match.group(1))
return commands.when_mentioned_or(*extras)(bot, message)
class JDJGBot(commands.Bot):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.db = None
self.session = None
async def start(self, *args, **kwargs):
self.session = aiohttp.ClientSession()
try:
self.db = await asyncpg.create_pool(self.db_key)
self.linked_data = await self.db.fetch("SELECT * FROM global_link")
self.linked_channels = [c.get("channel_id") for c in self.linked_data]
except Exception as e:
print(f"Error connecting to database: {e}")
self.db = None
await super().start(*args, **kwargs)
async def close(self):
if self.session:
await self.session.close()
if self.db:
await self.db.close()
await super().close()
async def setup_hook(self):
for cog in EXTENSIONS:
try:
await self.load_extension(f"{cog}")
except commands.errors.ExtensionError:
traceback.print_exc()
bot = JDJGBot(command_prefix=(get_prefix), intents=discord.Intents.all())
@bot.event
async def on_error(event, *args, **kwargs):
more_information = sys.exc_info()
error_wanted = traceback.format_exc()
traceback.print_exc()
# print(more_information[0])
logging.basicConfig(level=logging.INFO)
def main():
db_key = os.getenv("DB_key")
token = os.getenv("TOKEN")
if db_key is None or token is None:
load_dotenv()
db_key = os.getenv("DB_key")
token = os.getenv("TOKEN")
bot.db_key = db_key
bot.run(token)
if __name__ == "__main__":
main()