-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
186 lines (155 loc) · 5.35 KB
/
main.py
File metadata and controls
186 lines (155 loc) · 5.35 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
import asyncio
import logging
import signal
import sys
import traceback
from contextlib import suppress
import disnake
from disnake.ext import commands
import constants
from cogs import AlphaTrackerCog
from cogs import AuctionTrackerCog
from cogs import FireSaleTrackerCog
from cogs import GuildCog
from cogs import ItemDBCog
from cogs import LoggerCog
from cogs import MotdTrackerCog
from cogs import RankTrackerCog
from cogs import StatusUpdaterCog
from cogs import VersionTrackerCog
from cogs import WikiTrackerCog
from cogs import ZoneTrackerCog
from cogs import JobTrackerCog
from modules import asyncreqs
from modules import utils
# load Skypixel logger
root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG)
root_handler = logging.FileHandler(
filename="storage/skypixel.log", encoding="utf-8", mode="w"
)
root_handler.setFormatter(
logging.Formatter("[%(asctime)s:%(levelname)s:%(name)s:%(lineno)d] %(message)s")
)
root_logger.addHandler(root_handler)
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(logging.INFO)
console_handler.setFormatter(
logging.Formatter("[%(levelname)s:%(name)s:%(lineno)d] %(message)s")
)
root_logger.addHandler(console_handler)
logger = logging.getLogger(__name__)
# load disnake logger
disnake_logger = logging.getLogger("disnake")
disnake_logger.setLevel(logging.DEBUG)
disnake_handler = logging.FileHandler(
filename="storage/disnake.log", encoding="utf-8", mode="w"
)
disnake_handler.setFormatter(
logging.Formatter("%(asctime)s:%(levelname)s:%(name)s: %(message)s")
)
disnake_logger.addHandler(disnake_handler)
disnake_logger.propagate = False
intents = disnake.Intents.default()
intents.members = True # type: ignore
bot = commands.InteractionBot(
intents=intents,
owner_id=constants.OWNER_ID,
)
bot.add_cog(AlphaTrackerCog(bot))
bot.add_cog(AuctionTrackerCog(bot))
bot.add_cog(FireSaleTrackerCog(bot))
bot.add_cog(GuildCog(bot))
bot.add_cog(ItemDBCog(bot))
bot.add_cog(LoggerCog(bot))
bot.add_cog(MotdTrackerCog(bot))
bot.add_cog(RankTrackerCog(bot))
bot.add_cog(StatusUpdaterCog(bot))
bot.add_cog(VersionTrackerCog(bot))
bot.add_cog(WikiTrackerCog(bot))
bot.add_cog(ZoneTrackerCog(bot))
bot.add_cog(JobTrackerCog(bot))
constants.BOT = bot
logger.debug("Loaded all cogs + set constants.BOT")
# put here instead of a cog so it has main.py's scope
@bot.message_command(
name="Execute",
install_types=disnake.ApplicationInstallTypes.all(),
contexts=disnake.InteractionContextTypes.all(),
)
async def execute(inter: disnake.MessageCommandInteraction, message: disnake.Message):
if not await bot.is_owner(inter.author):
return await inter.send(
embed=utils.make_error(
"Not Owner", "You must be the bot owner to use this command!"
)
)
await inter.response.defer()
try:
tmp_dic = {}
clean_content = message.content.replace("\u2069", "").replace("\u2068", "")
executing_string = "async def temp_func():\n {}\n".format(
clean_content.partition("\n")[2]
.strip("`")
.replace("\n", " \n ")
.replace("”", '"')
.replace("’", "'")
.replace("‘", "'")
)
logger.info(executing_string)
exec(executing_string, {**globals(), **locals()}, tmp_dic)
await tmp_dic["temp_func"]()
await message.add_reaction("✅")
except: # noqa: E722
error = traceback.format_exc()
logger.error(error)
await asyncio.gather(
message.add_reaction("❌"),
inter.send(f"Error while running code:\n```py\n{error}```"),
)
class DevServerUnavailable(commands.CheckFailure):
pass
class UserNotInDevServer(commands.CheckFailure):
pass
class UserNotAPatron(commands.CheckFailure):
pass
@bot.application_command_check()
async def patron_only_check(inter: disnake.Interaction):
owners = inter.authorizing_integration_owners
if owners is None or owners.user_id is None:
return True
guild = bot.get_guild(constants.DEV_SERVER_ID)
if guild is None:
raise DevServerUnavailable("dev server unavailable")
member = guild.get_member(inter.author.id)
if member is None:
with suppress(disnake.NotFound):
member = await guild.fetch_member(inter.author.id)
if member is None:
raise UserNotInDevServer("user not in dev server")
if not member.get_role(constants.PATRON_ROLE):
raise UserNotAPatron("user is not a patron")
return True
async def on_close():
logger.info("Shutting down...")
await asyncreqs.close()
logger.info("Closed asyncreqs")
# PYRIGHT I PROMISE THIS CODE WORKS PLEASE SHUT UP
await asyncio.gather( # pyright: ignore[reportCallIssue]
*[ # pyright: ignore[reportArgumentType]
cog.close() # pyright: ignore[reportAttributeAccessIssue]
for cog in bot.cogs.values()
if hasattr(cog, "close") and callable(cog.close) # pyright: ignore[reportAttributeAccessIssue]
]
)
logger.info("Closed all cogs")
await bot.close()
logger.info("Closed bot")
sys.exit(0)
@bot.event
async def on_ready():
def signal_handler(*_):
asyncio.create_task(on_close())
signal.signal(signal.SIGINT, signal_handler)
logger.info(f"Logged in as {bot.user} (ID: {bot.user.id})")
bot.run(constants.BOT_TOKEN)