This repository was archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbot.py
145 lines (125 loc) · 4.98 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
import asyncio
import io
import json
import logging
import textwrap
import traceback
from contextlib import redirect_stdout
import websockets
from discord.ext import commands
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)
super().__init__(**kwargs, loop=loop)
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.load_extension("eval")
self.loop.create_task(self.ensure_ipc())
self.run(kwargs['token'])
async def on_ready(self):
self.log.info(f'[Cluster#{self.cluster_name}] Ready called.')
self.pipe.send(1)
self.pipe.close()
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