-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
212 lines (185 loc) · 7.77 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
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
import asyncio
import io
import json
import logging
import textwrap
import traceback
from contextlib import redirect_stdout
import aiohttp
import discord
import websockets
from discord.ext import commands
from motor.motor_asyncio import AsyncIOMotorClient
from essentials.membercache import MemberCache
from essentials.messagecache import MessageCache
from essentials.multi_server import get_pre
from essentials.settings import SETTINGS
class ClusterBot(commands.AutoShardedBot):
def __init__(self, **kwargs):
self.pipe = kwargs.pop('pipe')
self.cluster_name = kwargs.pop('cluster_name')
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
intents = discord.Intents.all()
intents.messages = True
intents.members = True
intents.reactions = True
intents.message_content = True
intents.guilds = True
intents.presences = True
super().__init__(**kwargs, loop=loop, intents=intents)
self.websocket = None
self._last_result = None
self.ws_task = None
self.responses = asyncio.Queue()
self.eval_wait = False
log = logging.getLogger(f"Cluster#{self.cluster_name}")
log.setLevel(logging.DEBUG)
log.handlers = [logging.FileHandler(f'cluster-{self.cluster_name}.log', encoding='utf-8', mode='a')]
log.info(f'[Cluster#{self.cluster_name}] {kwargs["shard_ids"]}, {kwargs["shard_count"]}')
self.log = log
self.owner = None
self.db = None
self.session = None
self.emoji_dict = None
self.pre = None
self.remove_command('help')
self.load_extension("cogs.eval")
extensions = ['cogs.config', 'cogs.poll_controls', 'cogs.help', 'cogs.db_api', 'cogs.admin']
self.loop.create_task(self.ensure_ipc())
for ext in extensions:
self.load_extension(ext)
self.message_cache = MessageCache(self)
self.member_cache = MemberCache()
self.refresh_blocked = {}
self.refresh_queue = {}
self.run(kwargs['token'])
async def on_message(self, message):
# allow case insensitive prefix
prefix = await get_pre(self, message)
if type(prefix) == tuple:
prefixes = (t.lower() for t in prefix)
for pfx in prefixes:
if len(pfx) >= 1 and message.content.lower().startswith(pfx.lower()):
# print("Matching", message.content, "with", pfx)
message.content = pfx + message.content[len(pfx):]
await self.process_commands(message)
break
else:
if message.content.lower().startswith(prefix.lower()):
message.content = prefix + message.content[len(prefix):]
await self.process_commands(message)
async def on_ready(self):
self.owner = await self.fetch_user(SETTINGS.owner_id)
mongo = AsyncIOMotorClient(SETTINGS.mongo_db)
self.db = mongo.pollmaster
self.session = aiohttp.ClientSession()
with open('utils/emoji-compact.json', encoding='utf-8') as emojson:
self.emoji_dict = json.load(emojson)
self.pre = {entry['_id']: entry.get('prefix', 'pm!') async for entry in
self.db.config.find({}, {'_id', 'prefix'})}
await self.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name="pm!help and /help"))
self.log.info(f'[Cluster#{self.cluster_name}] Ready called.')
self.pipe.send(1)
self.pipe.close()
async def on_guild_join(self, server):
result = await self.db.config.find_one({'_id': str(server.id)})
if result is None:
await self.db.config.update_one(
{'_id': str(server.id)},
{'$set': {'prefix': 'pm!', 'admin_role': 'polladmin', 'user_role': 'polluser'}},
upsert=True
)
self.pre[str(server.id)] = 'pm!'
async def on_shard_ready(self, shard_id):
self.log.info(f'[Cluster#{self.cluster_name}] Shard {shard_id} ready')
async def on_command_error(self, ctx, exc):
if not isinstance(exc, (commands.CommandNotFound, commands.NotOwner)):
self.log.critical(''.join(traceback.format_exception(type(exc), exc, exc.__traceback__)))
# await ctx.send("check logs")
async def on_error(self, *args, **kwargs):
self.log.critical(traceback.format_exc())
def cleanup_code(self, content):
"""Automatically removes code blocks from the code."""
# remove ```py\n```
if content.startswith('```') and content.endswith('```'):
return '\n'.join(content.split('\n')[1:-1])
# remove `foo`
return content.strip('` \n')
async def close(self, *args, **kwargs):
self.log.info("shutting down")
await self.websocket.close()
await super().close()
async def exec(self, code):
env = {
'bot': self,
'_': self._last_result
}
env.update(globals())
body = self.cleanup_code(code)
stdout = io.StringIO()
to_compile = f'async def func():\n{textwrap.indent(body, " ")}'
try:
exec(to_compile, env)
except Exception as e:
return f'{e.__class__.__name__}: {e}'
func = env['func']
try:
with redirect_stdout(stdout):
ret = await func()
except Exception as e:
value = stdout.getvalue()
f'{value}{traceback.format_exc()}'
else:
value = stdout.getvalue()
if ret is None:
if value:
return str(value)
else:
return 'None'
else:
self._last_result = ret
return f'{value}{ret}'
async def websocket_loop(self):
while True:
try:
msg = await self.websocket.recv()
except websockets.ConnectionClosed as exc:
if exc.code == 1000:
return
raise
data = json.loads(msg, encoding='utf-8')
if self.eval_wait and data.get('response'):
await self.responses.put(data)
cmd = data.get('command')
if not cmd:
continue
if cmd == 'ping':
ret = {'response': 'pong'}
self.log.info("received command [ping]")
elif cmd == 'eval':
self.log.info(f"received command [eval] ({data['content']})")
content = data['content']
data = await self.exec(content)
ret = {'response': str(data)}
else:
ret = {'response': 'unknown command'}
ret['author'] = self.cluster_name
self.log.info(f"responding: {ret}")
try:
await self.websocket.send(json.dumps(ret).encode('utf-8'))
except websockets.ConnectionClosed as exc:
if exc.code == 1000:
return
raise
async def ensure_ipc(self):
self.websocket = w = await websockets.connect('ws://localhost:42069')
await w.send(self.cluster_name.encode('utf-8'))
try:
await w.recv()
self.ws_task = self.loop.create_task(self.websocket_loop())
self.log.info("ws connection succeeded")
except websockets.ConnectionClosed as exc:
self.log.warning(f"! couldnt connect to ws: {exc.code} {exc.reason}")
self.websocket = None
raise