-
Notifications
You must be signed in to change notification settings - Fork 5
/
__main__.py
113 lines (90 loc) · 2.98 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
import asyncio
import argparse
import logging
import discord
import os
from logging.handlers import TimedRotatingFileHandler
from os import environ
from pathlib import Path
import seaborn as sns
from discord.ext import commands
from matplotlib import pyplot as plt
from tle import constants
from tle.util import font_downloader
from tle.util import codeforces_common as cf_common
from tle.util import discord_common
def setup():
# Make required directories.
for path in constants.ALL_DIRS:
os.makedirs(path, exist_ok=True)
# logging to console and file on daily interval
logging.basicConfig(
format="{asctime}:{levelname}:{name}:{message}",
style="{",
datefmt="%d-%m-%Y %H:%M:%S",
level=logging.INFO,
handlers=[
logging.StreamHandler(),
TimedRotatingFileHandler(
constants.LOG_FILE_PATH, when="D", backupCount=3, utc=True
),
],
)
# matplotlib and seaborn
plt.rcParams["figure.figsize"] = 7.0, 3.5
sns.set()
options = {
"axes.edgecolor": "#A0A0C5",
"axes.spines.top": False,
"axes.spines.right": False,
}
sns.set_style("darkgrid", options)
# Download fonts if necessary
font_downloader.maybe_download()
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--nodb", action="store_true")
args = parser.parse_args()
token = environ.get("BOT_TOKEN")
if not token:
logging.error("Token required")
return
setup()
intents = discord.Intents.default()
intents.members = True
prefix = environ.get("BOT_PREFIX")
if not prefix:
logging.error("Bot prefix required")
return
bot = commands.Bot(
command_prefix=commands.when_mentioned_or(prefix), intents=intents
)
cogs = [file.stem for file in Path("tle", "cogs").glob("*.py")]
for extension in cogs:
if extension != "tournament":
bot.load_extension(f"tle.cogs.{extension}")
logging.info(f'Cogs loaded: {", ".join(bot.cogs)}')
@bot.command(brief="Starts a tournament")
@commands.has_any_role("Admin", "Moderator")
async def load_tour(ctx):
"""Starts a new tournament"""
bot.load_extension(f"tle.cogs.tournament")
@bot.command(brief="Close the ongoing tournament")
@commands.has_any_role("Admin", "Moderator")
async def unload_tour(ctx):
"""Stops the ongoing tournament"""
bot.unload_extension(f"tle.cogs.tournament")
def no_dm_check(ctx):
if ctx.guild is None:
raise commands.NoPrivateMessage("I refuse.")
return True
# Restrict bot usage to inside guild channels only.
bot.add_check(no_dm_check)
@bot.event
async def on_ready():
await cf_common.initialize(args.nodb)
asyncio.create_task(discord_common.presence(bot))
bot.add_listener(discord_common.bot_error_handler, name="on_command_error")
bot.run(token)
if __name__ == "__main__":
main()