-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
98 lines (82 loc) · 3.15 KB
/
Copy pathmain.py
File metadata and controls
98 lines (82 loc) · 3.15 KB
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
from __future__ import annotations
import asyncio
import logging
from datetime import datetime
from logging import handlers
from typing import TYPE_CHECKING
from discord import Game, Guild, Intents, Interaction
from discord.ext import commands
from discord.utils import setup_logging
from src.client import SithClient
from src.commands.admin import AdminCog
from src.commands.club import ClubCog
from src.commands.help import HelpCog
from src.commands.misc import MiscCog
from src.commands.news import NewsCog
from src.commands.role import RoleCog
from src.settings import BASE_DIR, Settings
if TYPE_CHECKING:
from discord.app_commands import Command
from discord.ext.commands import Context
class AeBot(commands.Bot):
watched_guild: Guild
def __init__(self, client: SithClient):
self.settings = Settings()
self.logger = logging.getLogger("discord")
self.client = client
super().__init__(
command_prefix=self.settings.bot.command_prefix, intents=Intents.all()
)
async def setup_hook(self):
await self.add_cog(ClubCog(self))
await self.add_cog(NewsCog(self))
await self.add_cog(AdminCog(self))
await self.add_cog(MiscCog())
await self.add_cog(RoleCog(self))
await self.add_cog(HelpCog(self))
async def on_ready(self):
await self.wait_until_ready()
self.watched_guild = self.get_guild(self.settings.guild.id)
await self.change_presence(activity=Game(name="/help help"))
self.logger.info(f"Bot ready to act on {self.watched_guild.name}")
async def on_command(self, ctx: Context):
start = ctx.message.created_at
duration = datetime.now(tz=start.tzinfo) - start
self.logger.info(
f"Command `{ctx.command.name}` called by {ctx.author.name} "
f"[{duration.total_seconds():.6f}sec]"
)
async def on_app_command_completion(
self, interaction: Interaction, command: Command
):
start = interaction.created_at
duration = datetime.now(tz=start.tzinfo) - start
cmd_name = (
f"{command.parent.name} {command.name}" if command.parent else command.name
)
self.logger.info(
f"Slash command `{cmd_name}` "
f"called by {interaction.user.name} in {interaction.channel.name} "
f"[{duration.total_seconds():.6f}sec]"
)
async def main():
async with SithClient() as client:
bot = AeBot(client)
# Setup the logging (stream handler and file handler)
setup_logging()
(BASE_DIR / "data").mkdir(exist_ok=True)
(BASE_DIR / "log").mkdir(exist_ok=True)
handler = handlers.RotatingFileHandler(
filename=BASE_DIR / "log/bot.log",
maxBytes=10485760, # 10Mo
backupCount=5,
)
formatter = logging.Formatter(
"[{asctime}] [{levelname:<8}] {name}: {message}",
"%Y-%m-%d %H:%M:%S",
style="{",
)
setup_logging(handler=handler, formatter=formatter)
await bot.start(bot.settings.bot.token.get_secret_value())
if __name__ == "__main__":
asyncio.run(main())