-
Notifications
You must be signed in to change notification settings - Fork 3
/
bot.py
93 lines (68 loc) · 2.3 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
import asyncio
import asyncpg
import discord, os
from datetime import datetime
from discord.ext import commands
from cachetools import TTLCache
import Config
from Utils.Bot import Logger, Functions
# Bot intents
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
# Logger stuff
Logger.on_ready()
class SanyaBot(commands.Bot):
def __init__(self):
super().__init__(command_prefix=Config.Bot.prefix, intents=intents)
async def on_ready(self):
# Check database
await Functions.check_database(self)
# Set up users cache
self.users_cache = TTLCache(maxsize=Config.Bot.max_cached_users, ttl=Config.Bot.cache_time)
# Start date for status command
setattr(self, "start_time", datetime.now())
Logger.log("SANYA", "INFO", f"Logged in as {bot.user.name} with ID {bot.user.id}")
while not bot.is_closed():
await bot.change_presence(
activity=discord.Activity(
type=Config.Bot.presence[0],
name=Config.Bot.presence[1]
)
)
await asyncio.sleep(9999999)
bot = SanyaBot()
bot.remove_command("help")
# Prevent Bot from starting if Database credentials are missing
db_credentials = [
os.getenv("PGDATABASE"),
os.getenv("PGUSER"),
os.getenv("PGHOST"),
os.getenv("PGPASSWORD"),
os.getenv("PGPORT")
]
if None in db_credentials:
class PgException(Exception):
pass
raise PgException("Postgres credentials are missing.")
del db_credentials
# Database Pool
async def create_db_pool():
bot.pg_con = await asyncpg.create_pool(
database=os.getenv("PGDATABASE"),
user=os.getenv("PGUSER"),
host=os.getenv("PGHOST"),
password=os.getenv("PGPASSWORD"),
port=os.getenv("PGPORT")
)
bot.loop.run_until_complete(create_db_pool())
# Load cogs
for filename in os.listdir("./cogs"):
if filename.endswith(".py"):
try:
bot.load_extension(f"cogs.{filename[:-3]}")
Logger.log("SANYA", "INFO", f"Module {filename} successfully loaded.")
except Exception as e:
Logger.log("SANYA", "ERROR", f"Failed to load module {filename}: {e}")
# Run bot
bot.run(os.environ.get("BOT_TOKEN"))