-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
91 lines (72 loc) · 2.58 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
"""
This bot made with ❤️
"""
__author__ = "Valien"
__version__ = "0.0.16"
__maintainer__ = "Valien"
__link__ = "https://github.com/rvalien/orbbot"
import asyncpg
import datetime
import discord
import logging.handlers
import os
import redis
from discord.ext import commands
from models.db_gino import on_startup as gino_on_startup
from tasks.tasks import bdays_check, deadline_check
INITIAL_EXTENSIONS = [
"cogs.qc",
"cogs.commands",
"cogs.books",
]
token = os.environ["TOKEN"]
# token = os.environ["TEST_TOKEN"]
admin = os.environ["ADMIN"]
prefix = os.getenv("PREFIX", "!")
database_url = os.environ["DATABASE_URL"]
CLIENT = redis.from_url(os.environ["REDIS_URL"])
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
logger = logging.getLogger('discord')
logger.setLevel(logging.DEBUG)
logging.getLogger('discord.http').setLevel(logging.INFO)
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
bot = commands.Bot(
command_prefix=commands.when_mentioned_or(prefix),
intents=intents,
description="Small bot for lil QC community",
case_insensitive=True,
)
@bot.event
async def on_ready():
"""https://discordpy.readthedocs.io/en/latest/api.html#discord.on_ready"""
bot.github = "https://github.com/rvalien/orbbot"
bot.launch_time = datetime.datetime.utcnow()
bot.pg_con = await asyncpg.create_pool(database_url)
sql_path = "sql_queries"
for file in os.listdir(sql_path):
logger.info(f"prepare db with: {file}\n")
with open(os.path.join(sql_path, file), encoding="utf-8", mode="r") as raw_file:
query = raw_file.read()
await bot.pg_con.execute(query)
await gino_on_startup(database_url)
logger.info(f"Init {bot.user.name}-{bot.user.id}.API version: {discord.__version__}. bot version: {__version__}")
logger.info(f"beep-boop i'm online...{bot.launch_time}!")
bdays_check.start(bot)
deadline_check.start(bot, CLIENT)
for extension in INITIAL_EXTENSIONS:
try:
await bot.load_extension(extension)
except Exception as e:
logger.warning(f"Failed to load extension {extension}{type(e).__name__}: {e}")
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandNotFound):
await ctx.message.add_reaction("⚠️")
else:
logger.error(f"{ctx.message.author}, {error}")
if __name__ == "__main__":
bot.run(token, reconnect=True)