-
Notifications
You must be signed in to change notification settings - Fork 5
/
bot.py
152 lines (128 loc) Β· 4.55 KB
/
bot.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
143
144
145
146
147
148
149
150
151
152
from dotenv import load_dotenv
load_dotenv()
import asyncio
import logging
import os
import aiohttp
import interactions as ipy
from beanie import init_beanie
from interactions.ext import prefixed_commands
from motor.motor_asyncio import AsyncIOMotorClient
from pymongo.server_api import ServerApi
import common.utils as utils
from common.const import *
from common.models import Tag
logger = logging.getLogger("astro_bot")
logger.setLevel(logging.DEBUG)
stderr_handler = logging.StreamHandler()
stderr_handler.setLevel(logging.WARNING)
logger.addHandler(stderr_handler)
file_handler = logging.FileHandler(filename="discord.log", encoding="utf-8", mode="a")
file_handler.setFormatter(logging.Formatter("%(asctime)s:%(levelname)s:%(name)s: %(message)s"))
file_handler.setLevel(logging.INFO)
logger.addHandler(file_handler)
activity = ipy.Activity.create(name="you. π", type=ipy.ActivityType.WATCHING)
intents = ipy.Intents.new(
guilds=True,
guild_members=True,
guild_moderation=True,
guild_messages=True,
direct_messages=True,
message_content=True,
)
bot = ipy.Client(
intents=intents,
sync_interactions=False,
debug_scope=METADATA["guild"],
disable_dm_commands=True,
status=ipy.Status.DO_NOT_DISTURB,
activity=activity,
fetch_members=True,
send_command_tracebacks=False,
logger=logger,
)
prefixed_commands.setup(bot)
async def start():
client = AsyncIOMotorClient(os.environ["MONGO_DB_URL"], server_api=ServerApi("1"))
await init_beanie(client.Astro, document_models=[Tag]) # type: ignore
bot.session = aiohttp.ClientSession()
ext_list = utils.get_all_extensions(SRC_PATH)
for ext in ext_list:
bot.load_extension(ext)
try:
await bot.astart(os.environ["TOKEN"])
finally:
await bot.session.close()
@ipy.listen("command_error", disable_default_listeners=True)
async def on_command_error(event: ipy.events.CommandError):
# basically, this, compared to the built-in version:
# - makes sure if the error can be sent ephemerally, it will
# - only log the error if it isn't an "expected" error (check failure, cooldown, etc)
# - send a message to the user if there's an unexpected error
try:
if isinstance(event.error, ipy.errors.CommandOnCooldown):
await utils.error_send(
event.ctx,
msg=(
":x: This command is on cooldown.\n"
f"Please try again in {int(event.error.cooldown.get_cooldown_time())} seconds."
),
color=ASTRO_COLOR,
)
elif isinstance(event.error, ipy.errors.MaxConcurrencyReached):
await utils.error_send(
event.ctx,
msg=(
":x: This command has reached its maximum concurrent usage.\nPlease try again"
" shortly."
),
color=ASTRO_COLOR,
)
elif isinstance(event.error, ipy.errors.CommandCheckFailure):
await utils.error_send(
event.ctx,
msg=":x: You do not have permission to run this command.",
color=ipy.BrandColors.YELLOW,
)
elif isinstance(event.error, ipy.errors.BadArgument):
await utils.error_send(
event.ctx,
msg=f":x: {event.error}",
color=ipy.MaterialColors.RED,
)
else:
await utils.error_send(
event.ctx,
msg=(
":x: An unexpected error has occured. The error will be logged and should be"
" fixed shortly."
),
color=ipy.RoleColors.DARK_RED,
)
bot.dispatch(
ipy.events.Error(
source=f"cmd `/{event.ctx.invoke_target}`", # type: ignore
error=event.error,
args=event.args,
kwargs=event.kwargs,
ctx=event.ctx,
)
)
except ipy.errors.LibraryException:
bot.dispatch(
ipy.events.Error(
source=f"cmd `/{event.ctx.invoke_target}`", # type: ignore
error=event.error,
args=event.args,
kwargs=event.kwargs,
ctx=event.ctx,
)
)
@ipy.listen("startup")
async def on_startup():
print(f"Logged in as {bot.user.tag}.")
if __name__ == "__main__":
try:
asyncio.run(start())
except KeyboardInterrupt:
logger.info("Shutting down.")