-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathastrocade.py
More file actions
122 lines (102 loc) · 3.48 KB
/
astrocade.py
File metadata and controls
122 lines (102 loc) · 3.48 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"""Entrypoint for the Astrocade Discord bot."""
import asyncio
import logging
from os import name as OS_NAME
from pathlib import Path
from sys import stdout
from typing import Final
from arc import GatewayClient
from hikari import (
Activity,
ActivityType,
ApplicationContextType,
GatewayBot,
GatewayConnectionError,
Intents,
Permissions,
Status,
)
from loguru import logger
from loguru_discord import DiscordSink
from loguru_discord.intercept import Intercept
from core.consts import (
DATABASE_PATH,
DISCORD_BOT_TOKEN,
DISCORD_SERVER_IDS,
IS_DEBUG,
LOG_DISCORD_WEBHOOK_LEVEL,
LOG_DISCORD_WEBHOOK_URL,
LOG_LEVEL,
)
from core.database import AsyncEngine, Database
from core.hooks import Hooks
async def start() -> None:
"""Initialize the Astrocade Discord Bot."""
logger.info("Astrocade - Discord Activities Enhancements")
logger.info("https://github.com/EthanC/Astrocade")
if LOG_LEVEL:
logger.remove()
logger.add(stdout, level=LOG_LEVEL)
logger.success(f"Set console logging level to {LOG_LEVEL}")
Intercept.setup({"TRACE_HIKARI": "TRACE"})
if LOG_DISCORD_WEBHOOK_URL:
logger.add(
DiscordSink(LOG_DISCORD_WEBHOOK_URL, suppress=[GatewayConnectionError]),
level=LOG_DISCORD_WEBHOOK_LEVEL,
backtrace=False,
)
logger.success(
f"Enabled logging to Discord webhook at level {LOG_DISCORD_WEBHOOK_LEVEL}"
)
# Replace default asyncio event loop with libuv on UNIX
# https://github.com/hikari-py/hikari#uvloop
if OS_NAME != "nt":
try:
import uvloop # type: ignore
uvloop.install()
logger.success("Installed libuv event loop")
except Exception as e:
logger.opt(exception=e).debug("Defaulted to asyncio event loop")
database: AsyncEngine = await Database.setup(DATABASE_PATH)
bot: GatewayBot = GatewayBot(
DISCORD_BOT_TOKEN,
allow_color=False,
banner=None,
suppress_optimization_warning=IS_DEBUG,
intents=Intents.GUILD_MESSAGES
| Intents.MESSAGE_CONTENT
| Intents.GUILD_PRESENCES,
)
client: GatewayClient = GatewayClient(
bot,
default_enabled_guilds=DISCORD_SERVER_IDS,
default_permissions=Permissions.VIEW_CHANNEL
| Permissions.READ_MESSAGE_HISTORY
| Permissions.SEND_MESSAGES,
invocation_contexts=[ApplicationContextType.GUILD],
)
client.set_type_dependency(AsyncEngine, database)
client.set_type_dependency(GatewayBot, bot)
client.load_extensions_from("extensions")
client.add_startup_hook(Hooks.client_startup)
client.add_shutdown_hook(Hooks.client_shutdown)
try:
await bot.start(
activity=Activity(
# Later overriden by recurring presence task
name="Pardon our space dust...",
type=ActivityType.WATCHING,
),
check_for_updates=False,
status=Status.DO_NOT_DISTURB,
)
await bot.join()
finally:
await Database.close(database)
if __name__ == "__main__":
try:
asyncio.run(start())
except KeyboardInterrupt:
logger.debug("Exiting due to keyboard interrupt")
except Exception as e:
logger.opt(exception=e).critical("Fatal error occurred during runtime")