-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.py
99 lines (84 loc) · 2.89 KB
/
bot.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
import argparse
import datetime
import os
import traceback
from typing import Union
import aiohttp
import asyncpg
import discord
from discord.ext import commands
import config
from cogs.help import CustomHelpCommand
from utils.constants import MAIN_GUILD_ID
parser = argparse.ArgumentParser()
parser.add_argument(
"-d",
"--development",
help="Run the bot in the development state",
action="store_true",
)
args = parser.parse_args()
class Blist(commands.Bot):
pool: asyncpg.Pool
user: discord.ClientUser
def __init__(self):
super().__init__(
command_prefix=commands.when_mentioned_or(
*["bs?", "Bs?", "bS?", "BS?"]
if args.development
else ["bs!", "Bs!", "bS!", "BS!"]
),
case_insensitive=True,
max_messages=500,
reconnect=True,
help_command=CustomHelpCommand(command_attrs={"hidden": True}),
intents=discord.Intents(
members=True,
emojis=True,
messages=True,
guilds=True,
message_content=True,
),
)
@property
def main_guild(self) -> Union[discord.Guild, discord.Object]:
return self.get_guild(MAIN_GUILD_ID) or discord.Object(MAIN_GUILD_ID)
async def setup_hook(self):
self.session = aiohttp.ClientSession()
print("---------------------")
print(f"{self.user} is ready")
print("---------------------")
self.uptime = datetime.datetime.utcnow().strftime("%c")
try:
self.pool = await asyncpg.create_pool(config.db_url) # type: ignore
except Exception as error:
print("There was a problem connecting to the database")
print(f"\n{error}")
for file in os.listdir("cogs"):
if file.endswith(".py") and not file.startswith("_"):
try:
await self.load_extension(f"cogs.{file[:-3]}")
except commands.ExtensionFailed as e:
print(f"Failed to load {file}")
traceback.print_exception(e.__class__, e, e.__traceback__)
else:
print(f"Successfully loaded {file}!")
try:
await self.load_extension("jishaku")
except commands.ExtensionFailed as e:
print("Failed to load jishaku")
traceback.print_exception(e.__class__, e, e.__traceback__)
else:
print(f"Successfully loaded jishaku!")
async def close(self):
if hasattr(self, "pool"):
await self.pool.close()
if hasattr(self, "session"):
await self.session.close()
await super().close()
def run(self):
return super().run(
config.bot_token_dev if args.development else config.bot_token
)
if __name__ == "__main__":
Blist().run()