-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
173 lines (150 loc) · 5.13 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import asyncio
import os
import sys
import traceback
from datetime import datetime as dtime
from datetime import timezone as tz
import interactions as ipy
from aiohttp import ClientConnectorError
from interactions.client import const as ipy_const
from modules.commons import convert_float_to_time
from modules.const import BOT_TOKEN, SENTRY_DSN, USER_AGENT
from modules.oobe.commons import UnsupportedVersion
py_ver = sys.version_info
if py_ver < (3, 10):
raise UnsupportedVersion(
f"Python version {py_ver.major}.{py_ver.minor}.{py_ver.micro} is not supported. Please use Python 3.10 or newer.",
f"{py_ver.major}.{py_ver.minor}.{py_ver.micro}",
)
print(f"[Pyt] Python version : {py_ver.major}.{py_ver.minor}.{py_ver.micro}")
print(f"[Pyt] Python binary : {sys.executable}")
now: dtime = dtime.now(tz=tz.utc)
"""The current time"""
ipy_const.CLIENT_FEATURE_FLAGS["FOLLOWUP_INTERACTIONS_FOR_IMAGES"] = True
bot = ipy.AutoShardedClient(
token=BOT_TOKEN,
status=ipy.Status.IDLE,
activity=ipy.Activity(
name="system booting up...",
type=ipy.ActivityType.LISTENING,
),
delete_unused_application_cmds=True,
sync_interactions=True,
send_command_tracebacks=False,
intents=ipy.Intents.DEFAULT | ipy.Intents.MESSAGE_CONTENT,
)
"""The bot client"""
@ipy.listen()
async def on_ready():
"""
When the bot is ready
This function will be called when the bot is ready.
"""
guilds = len(bot.guilds)
bg = "[Sys]"
lbg = len(bg)
sp = " " * lbg
print("[Sys] Bot is ready!")
print(f"{sp} Logged in as : {bot.user.display_name}")
print(f"{sp} User ID : {bot.user.id}")
print(f"{sp} Guilds : {guilds}")
print(f"{sp} Shards : {bot.total_shards}")
print(f"{sp} User Agent : {USER_AGENT}")
# set bot status
await asyncio.sleep(2.5)
await bot.change_presence(
activity=ipy.Activity(
name=f"{guilds} guilds, {bot.total_shards:,} shards",
type=ipy.ActivityType.WATCHING,
),
status=ipy.Status.ONLINE,
)
async def main():
"""
Main function
This function will be run before the bot starts.
"""
print("[Ext] Loading core/bot extensions...")
exts: list[str] = [
"interactions.ext.sentry",
]
for i, ext in enumerate(exts):
i += 1
pg = f"[Ext] [{i}/{len(exts)}]"
lpg = len(pg)
sp = " " * lpg
print(f"{pg} Loading core/bot extension: {ext}")
try:
if ext == "interactions.ext.sentry" and SENTRY_DSN not in ["", None]:
bot.load_extension(
ext, token=SENTRY_DSN, filter=None, traces_sample_rate=0.3
)
else:
bot.load_extension(ext)
except Exception as ea:
print(f"{pg} Error while loading system extension: " + ext)
print(f"{sp} {ea}")
print("[Ext] If this error shows up while restart the bot, ignore")
# Load extensions
print("[Cog] Loading cog/extensions...")
# for each .py files in extensions folder, load it, except for commons.py
exts = os.listdir("extensions")
for i, ext in enumerate(exts):
i += 1
pg = f"[Cog] [{i}/{len(exts)}]"
lpg = len(pg)
sp = " " * lpg
try:
if ext.endswith(".py"):
print(f"{pg} Loading cog/extension: {ext}")
ext = ext[:-3]
if ext not in ["commons", "stats"]:
bot.load_extension("extensions." + ext)
else:
bot.load_extension("extensions." + ext, now=now)
else:
print(f"{pg} Skipping: {ext}, not a .py file")
except Exception as eb:
print(f"{pg} Error while loading extension: {ext}")
print(f"{sp} {eb}")
print(f"{sp} Traceback:")
traceback.print_exc()
print("[Cog] If this error shows up while restart the bot, ignore")
await bot.astart()
def uptime() -> None:
"""
Prints uptime, how long the bot has been online
Returns:
None
"""
bot_stop: dtime = dtime.now(tz=tz.utc)
print("[Bcm] Date: " + bot_stop.strftime("%d/%m/%Y %H:%M:%S"))
differences = bot_stop - bot_run
total_seconds = differences.total_seconds()
total_time = convert_float_to_time(
total_seconds, use_seconds=True, show_milliseconds=True
)
print(f" Uptime: {total_time}")
return
if __name__ == "__main__":
print("[Sys] Starting bot...")
bot_run = now
print("[Bcm] Date: " + bot_run.strftime("%d/%m/%Y %H:%M:%S"))
while True:
try:
asy = asyncio.run(main())
except KeyboardInterrupt:
print("[Sys] Bot stopped by user.")
uptime()
sys.exit(0)
except ClientConnectorError:
print("[Sys] Bot stopped due to connection error.")
uptime()
sys.exit(1)
except Exception as ex:
print("[Sys] Bot stopped due to error.")
print(f" {ex}")
print("[Err] Traceback:")
traceback.print_exc()
uptime()
sys.exit(1)