-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
63 lines (51 loc) · 2.13 KB
/
Copy pathmain.py
File metadata and controls
63 lines (51 loc) · 2.13 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
import logging
from os import environ
from pathlib import Path
from aiohttp import ClientSession
from discord import Activity, ActivityType, Intents
from discord.ext import commands
from discord.utils import setup_logging
setup_logging()
logger = logging.getLogger("Sakamoto")
if not (TOKEN := environ.get("TOKEN")):
raise OSError("TOKEN environment variable not set")
class Sakamoto(commands.AutoShardedBot):
"""Discord bot with shared application resources."""
def __init__(self):
intents = Intents.default()
intents.message_content = True
super().__init__(
description="You thought all I say is meow?",
command_prefix=commands.when_mentioned,
case_insensitive=True,
intents=intents,
)
self.session: ClientSession | None = None
self.color = 0xFF3351
self.db_path = "data/sakamoto.sqlite"
async def setup_hook(self):
self.session = ClientSession()
for ext in Path("extensions").rglob("*.py"):
if ext.name.startswith("_"):
continue
module = ".".join(ext.with_suffix("").parts)
try:
await self.load_extension(module)
logger.info("Loaded %s", module)
except Exception as e: # pylint: disable=broad-exception-caught
# Catches ExtensionFailed (like in steam.py), ImportErrors, etc.
logger.error("Failed to load %s: %s", module, e)
async def on_ready(self):
"""Set the bot presence after connecting to Discord."""
assert self.user is not None, "self.user is None in on_ready!"
display = Activity(name="Use /help to view all commands!", type=ActivityType.listening)
await self.change_presence(activity=display)
logger.info("I am online! - %s %s", self.user.name, self.user.id)
async def close(self):
if self.session:
await self.session.close()
await super().close()
logger.info("Session closed!")
if __name__ == "__main__":
logger.info("Starting Sakamoto...")
Sakamoto().run(TOKEN, reconnect=True, log_handler=None)