-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
142 lines (123 loc) · 4.24 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import discord
import os
import datetime
from discord.ext.commands import Bot, when_mentioned_or
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
from discord.ext.commands import (
CommandNotFound,
CommandOnCooldown,
MissingPermissions,
MissingRequiredArgument,
BadArgument,
MemberNotFound,
)
from utils import tasks
from constants import AUTO_UPDATE_TIME
intents = discord.Intents.default()
intents.members = False
client = Bot(
case_insensitive=True,
description="Lockout Bot",
command_prefix=when_mentioned_or("."),
intents=intents,
)
logging_channel = None
@client.event
async def on_ready():
global logging_channel
logging_channel = await client.fetch_channel(
os.environ.get("LOGGING_CHANNEL")
)
await client.change_presence(
activity=discord.Game(name="Updating problemsetting")
)
await logging_channel.send(f"Updated problemset")
await tasks.update_problemset(client)
await client.change_presence(activity=discord.Game(name="in matches ⚔️"))
await logging_channel.send(f"Bot ready")
scheduler = AsyncIOScheduler()
scheduler.add_job(update, "interval", seconds=AUTO_UPDATE_TIME)
scheduler.add_job(
tasks.create_backup,
CronTrigger(hour="0, 6, 12, 18", timezone="Asia/Kolkata"),
[client],
)
scheduler.add_job(
tasks.update_ratings,
CronTrigger(minute="30", timezone="Asia/Kolkata"),
[client],
)
scheduler.add_job(
tasks.update_problemset,
CronTrigger(hour="8", timezone="Asia/Kolkata"),
[client],
)
scheduler.add_job(
tasks.scrape_authors,
CronTrigger(day_of_week="0", timezone="Asia/Kolkata"),
[client],
)
scheduler.start()
async def update():
await tasks.update_matches(client)
await tasks.update_rounds(client)
@client.event
async def on_command_error(ctx: discord.ext.commands.Context, error: Exception):
if isinstance(error, CommandNotFound):
pass
elif isinstance(error, CommandOnCooldown):
tot = error.cooldown.per
rem = error.retry_after
msg = f"{ctx.author.mention} That command has a default cooldown of {str(datetime.timedelta(seconds=tot)).split('.')[0]}.\n"
msg += f"Please retry after {str(datetime.timedelta(seconds=rem)).split('.')[0]}."
embed = discord.Embed(description=msg, color=discord.Color.red())
embed.set_author(name=f"Slow down!")
await ctx.send(embed=embed)
elif isinstance(error, MemberNotFound):
command = ctx.command
command.reset_cooldown(ctx)
await ctx.send(
embed=discord.Embed(
description=f"`{str(error)}`\nTry mentioning the user instead of typing name/id",
color=discord.Color.gold(),
)
)
elif isinstance(error, BadArgument) or isinstance(
error, MissingRequiredArgument
):
command = ctx.command
command.reset_cooldown(ctx)
usage = f"`.{str(command)} "
params = []
for key, value in command.params.items():
if key not in ["self", "ctx"]:
params.append(
f"[{key}]" if "NoneType" in str(value) else f"<{key}>"
)
usage += " ".join(params)
usage += "`"
if command.help:
usage += f"\n\n{command.help}"
await ctx.send(
embed=discord.Embed(
description=f"The correct usage is: {usage}",
color=discord.Color.gold(),
)
)
elif isinstance(error, MissingPermissions):
await ctx.send(f"{str(error)}")
else:
desc = f"{ctx.author.name}({ctx.author.id}) {ctx.guild.name}({ctx.guild.id}) {ctx.message.content}\n"
desc += f"**{str(error)}**"
await logging_channel.send(desc)
if __name__ == "__main__":
for filename in os.listdir("./cogs"):
if filename.endswith(".py"):
try:
client.load_extension(f"cogs.{filename[:-3]}")
except Exception as e:
print(f"Failed to load file {filename}: {str(e)}")
print(str(e))
token = os.environ.get("LOCKOUT_BOT_TOKEN")
client.run(token)