forked from RocketRace/robot-is-you
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ROBOT.py
224 lines (199 loc) · 7.52 KB
/
ROBOT.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
from __future__ import annotations
import logging
from src.cogs.operations import OperationMacros
import aiohttp
from src import synchronization
import traceback
import asyncio
from datetime import datetime
from typing import Any, Coroutine
import discord
import jishaku
from discord.ext import commands
import auth
import config
from src.constants import MAXIMUM_GUILD_THRESHOLD, GAMEOVER_GUILD_THRESHOLD
from src.db import Database
from src.cogs.render import Renderer
from src.cogs.variants import VariantHandlers
class Context(commands.Context):
async def error(self, msg: str) -> discord.Message:
await self.message.add_reaction("\u26a0\ufe0f")
return await self.reply(msg)
async def send(self, content: str = "", embed: discord.Embed | None = None, **kwargs) -> discord.Message:
if len(content) > 2000:
msg = " [...] \n\n (Character limit reached!)"
content = content[:2000-len(msg)] + msg
if embed is not None:
if content:
return await super().send(content, embed=embed, **kwargs)
return await super().send(embed=embed, **kwargs)
elif content:
return await super().send(content, embed=embed, **kwargs)
return await super().send(**kwargs)
class Bot(commands.Bot):
'''Custom bot class :)'''
db: Database
variant_handlers: VariantHandlers
operation_macros: OperationMacros
renderer: Renderer
def __init__(
self,
command_prefix,
*,
cogs: list[str],
embed_color: discord.Color,
webhook_url: str,
prefixes: list[str],
db_path: str,
# instance_id: int,
# event_queue: asyncio.Queue[synchronization.CallbackEvent],
original_id: int,
**kwargs
):
self.started = datetime.utcnow()
self.loading = True
self.exit_code = 0
self.embed_color = embed_color
self.webhook_url = webhook_url
self.prefixes = prefixes
self.db = Database()
self.db_path = db_path
# TODO(netux): remove
# self.instance_id = instance_id
# self.event_queue = event_queue
self.original_id = original_id
self.cog_names = cogs
super().__init__(command_prefix, **kwargs)
async def setup_hook(self) -> None:
for cog in self.cog_names:
await self.load_extension(cog, package="ROBOT")
# TODO(netux): remove
# async def request(self, event: synchronization.CallbackEvent):
# '''Send a request to the event manager of the bot instances.'''
# await self.event_queue.put(event)
async def get_context(self, message: discord.Message) -> Context:
return await super().get_context(message, cls=Context)
async def close(self) -> None:
await self.db.close()
await self.session.close()
await super().close()
async def on_ready(self) -> None:
print(f"{self.user}: Logged in!")
print(f"{self.user}: Invite: {discord.utils.oauth_url(str(self.user.id))}")
await self.db.connect(self.db_path)
if MAXIMUM_GUILD_THRESHOLD < len(self.guilds) < GAMEOVER_GUILD_THRESHOLD:
print(f"{self.user}: WARNING: Dangerously close to the guild limit, purging...")
guild_ids: set[int] = {row[0] for row in await self.db.conn.fetchall(
'''SELECT guild_id FROM guilds WHERE bot_id == ?;'''
)}
count = len(self.guilds) - MAXIMUM_GUILD_THRESHOLD
for guild in self.guilds:
if guild.id not in guild_ids:
await guild.leave()
count -= 1
if count == 0:
break
print(f"{self.user}: Purged {count} guilds.")
await self.db.conn.executemany(
'''
INSERT INTO guilds VALUES (?, ?)
ON CONFLICT (guild_id) DO NOTHING;
''',
((guild.id, self.user.id) for guild in self.guilds)
)
print(f"{self.user}: Established database connection!")
self.session = aiohttp.ClientSession()
print(f"{self.user}: Client session ready!")
async def start_with_exit_code(self, token: str) -> int:
await self.start(token)
return self.exit_code
logging.basicConfig(filename=config.log_file, level=logging.WARNING)
bots: list[Bot] = []
# TODO(netux): remove
# starters: list[Coroutine[Any, Any, int]] = []
# mpsc_queue = asyncio.Queue()
# async def shared_event_handler(event_queue: asyncio.Queue[synchronization.CallbackEvent]) -> int:
# while True:
# item = await event_queue.get()
# event = item.event
# callback = item.callback
# try:
# if isinstance(event, synchronization.CogRefreshEvent):
# cog = event.cog
# if cog is None:
# for bot in bots:
# # construct a list to avoid iterating over a dict as it's mutated
# for ext in list(bot.extensions):
# await bot.reload_extension(ext)
# else:
# for bot in bots:
# await bot.reload_extension(cog)
# await callback()
# except:
# traceback.print_exc()
if __name__ == "__main__":
async def main():
bot = Bot(
# Prefixes
commands.when_mentioned_or(*config.prefixes) if config.trigger_on_mention else config.prefixes,
# Other behavior parameters
case_insensitive=True,
activity=discord.Game(name=config.activity),
description=config.description,
# Never mention roles, @everyone or @here
allowed_mentions=discord.AllowedMentions(everyone=False, roles=False),
# Only receive message and reaction events
intents=discord.Intents(messages=True, reactions=True, guilds=True, message_content=True),
# Disable the member cache
member_cache_flags=discord.MemberCacheFlags.none(),
# Disable the message cache
max_messages=None,
# Don't chunk guilds
chunk_guilds_at_startup=False,
# for hot reloading
cogs=config.cogs,
embed_color=config.embed_color,
# display only
prefixes=config.prefixes,
db_path=config.db_path,
# synchronization
# TODO(netux): remove
# instance_id=0,
# event_queue=mpsc_queue,
# logging
webhook_url=auth.webhook_url,
original_id=config.original_id
)
# start the client
async with bot:
return await bot.start_with_exit_code(auth.token)
exit_code = 0
try:
asyncio.run(main())
except KeyboardInterrupt:
exit_code = 1
finally:
exit(exit_code)
# TODO(netux): remove
# async def main() -> int:
# events = asyncio.create_task(shared_event_handler(mpsc_queue))
# tasks = [asyncio.create_task(starter) for starter in starters]
# try:
# for returner in asyncio.as_completed(tasks):
# return await returner
# else:
# raise RuntimeError("There are no bots to run")
# finally:
# print("Shutting down bots...")
# events.cancel()
# await asyncio.gather(
# *(bot.close() for bot in bots if not bot.is_closed())
# )
# exit_code = 0
# try:
# x = asyncio.run(main())
# except KeyboardInterrupt:
# exit_code = 1
# finally:
# exit(exit_code)