-
Notifications
You must be signed in to change notification settings - Fork 0
/
beep.py
executable file
·348 lines (282 loc) · 11.9 KB
/
beep.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env python
from operator import itemgetter
import os
import re
import threading
import discord
from dotenv import load_dotenv
import json
import logging
import subprocess
from subprocess import CalledProcessError
from discord_slash import SlashCommand
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('discord')
logger.setLevel(logging.INFO)
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
zemke_id = 507097491762839555
class NodeRunnerError(Exception):
pass
def prevent_embed(s):
return re.sub(r'\((https://cwtsite.com[\S]*)\)$', r'<\1>', s)
class Env:
def __init__(self, getenv):
self.token = getenv('DISCORD_TOKEN')
self.script = getenv('SCRIPT') or './'
self.listen = getenv('LISTEN') == "1"
self.channel = int(getenv('CHANNEL')) if getenv('CHANNEL') is not None else None
class ThreadFactory:
def __init__(self):
import threading
def inst(self, target, args):
return threading.Thread(target=target, args=args)
class BeepBoop:
say_beep_boop = \
"Beep Bop CWT Bot. I act upon commands (see !cwtcommands)" \
" but I also mirror the CWT chat."
def __init__(self, client, getenv, listener_factory,
thread_factory=ThreadFactory()):
self.client = client
self.env = Env(getenv)
if self.env.listen:
logger.info("I'm listening.")
listener = listener_factory([self.client, self.env.channel])
thread = thread_factory.inst(listener.listen, (-1, self.send_message))
thread.start()
else:
logger.info('not listening, listen by setting env LISTEN to 1')
if self.env.channel is not None:
logger.info('CHANNEL env is set, but you\'re not listening')
def send_message(self, channelId, message):
self.client.loop.create_task(self.client.get_channel(channelId).send(prevent_embed(message)))
class Listener:
def __init__(self, client, channel_to_mirror_to, node_runner, open_stream):
self.posted = {}
self.client = client
self.node_runner = node_runner
self.open_stream = open_stream
self.channel_to_mirror_to = \
int(channel_to_mirror_to) if channel_to_mirror_to is not None else None
if self.channel_to_mirror_to is not None:
logger.info('mirroring CWT chat to channel %s only.', self.channel_to_mirror_to)
else:
logger.info('mirroring CWT chat to all channels on server.')
def listen(self, listen_iterations, cb):
if listen_iterations == -1:
while 1:
self.loop(cb)
else:
for _ in range(listen_iterations):
self.loop(cb)
def loop(self, cb):
logger.info('looping')
messages = self.open_stream()
for msg in messages.events():
if msg.event != "EVENT":
continue
data = json.loads(msg.data)
logger.info('EVENT %s', data)
for channel in self.get_channels():
self.process_message(data, channel.id, cb)
logger.info('out of loopery')
def process_message(self, data, channelId, cb):
if channelId not in self.posted:
self.posted[channelId] = []
if "id" not in data:
logger.warning("Data has no ID: %s", data)
return;
if data["id"] in self.posted[channelId]:
logger.info('message already received %s', channelId)
else:
if "newsType" in data and data["newsType"] == "DISCORD_MESSAGE" and \
re.search(r"\b%s\b" % channelId, data["body"].split(',')[1]):
logger.info('Discarding message sent from this same channel %s', str(channelId));
elif self.channel_to_mirror_to is None or self.channel_to_mirror_to == channelId:
try:
formatted = self.node_runner.format(data)
logger.info('sending to channel %s: %s', str(channelId), str(formatted))
cb(channelId, formatted)
except CalledProcessError as err:
logging.exception("Couldn't format, not sending.")
except:
logging.exception("Error while sending")
self.posted[channelId].append(data["id"])
def get_channels(self):
for guild in self.client.guilds:
for channel in guild.text_channels:
yield channel
class NodeRunner:
def __init__(self, script, runner):
self.script = script if script.endswith("/") else script + "/"
self.runner = runner
logging.info('node -v %s', str(self.runner(["node", "-v"])))
def handle(self, cmd, display_name, guild_id, channel_id):
link = ('https://discord.com/channels/%s/%s' % (str(guild_id), str(channel_id)))
arguments = ["node", self.script + 'handle.js', 'DISCORD', link, display_name, cmd]
return self._run(arguments)
def format(self, data):
category, author, body = itemgetter('category', 'author', 'body')(data)
arguments = ["node", self.script + 'format.js', category, author["username"], body]
if "newsType" in data and data["newsType"]:
arguments.append(data["newsType"])
return self._run(arguments)
def _run(self, args):
node = self.runner(args)
node.check_returncode()
stdout = node.stdout.decode('utf8')
ll = list(filter(lambda x: x.startswith("RES xx "), stdout.split('\n')))
try:
return ll[0][7:]
except:
logger.info("stdout: %s", stdout)
raise NodeRunnerError()
class EventHandler:
other_user_dm_response = \
"I don't offer private services, " \
"but here are some commands you can give into the public channels I'm in:"
confirm_all_channels = "Your message has been sent to all channels"
not_specified = "Not announcing to env channel as it's not specified."
confirm_specific_channel = "Message has been sent to that specific channel."
descr = {
"chat": "Send a message to the chat on cwtsite.com.",
"dice": "Roll a dice.",
"hell": "How frequently has Hell terrain been used this year?",
"terrain": "Map terrain usage stats.",
"winners": "All CWT winners.",
"commands": "Just a list of all available bot commands.",
"schedule": "What are the currently scheduled games?",
"playoffs": "Current playoff tree.",
"whatisthisthing": "What is this thing?",
"rafkagrass": "How frequently has Rafka played grassy maps this year?",
"url": "What's the URL of the best website out there?",
"github": "CWT is open source!",
}
chat_opt = {
"name": "message",
"type": 3,
"required": True,
"description": "Content of your message."
}
def __init__(self, client, node_runner, channel_to_mirror_to, avail_cmds):
self.client = client
self.node_runner = node_runner
self.channel_to_mirror_to = channel_to_mirror_to
self.slash = SlashCommand(self.client, sync_commands=True)
for cmd in avail_cmds: self.register_slash_command(cmd)
def register_slash_command(self, cmd):
self.slash.add_slash_command(
guild_ids=self.client.guilds,
cmd=self.on_slash_command,
options=[] if cmd != 'chat' else [self.chat_opt],
name=cmd,
description=self.descr[cmd])
logger.info("registered slash command: " + cmd)
async def on_message(self, message):
if message.author == self.client.user:
return
cmd = message.content.strip()
logger.info("MSG %s: %s", message.author.display_name, cmd)
if isinstance(message.channel, discord.DMChannel):
logger.info("is direct message")
await self.on_direct_message(message)
elif cmd == '!cwt':
logger.info("Received !cwt command")
await message.channel.send(BeepBoop.say_beep_boop)
elif cmd.startswith("!cwt"):
logger.info("commands starts with !cwt")
try:
result = self.node_runner.handle(
cmd, message.author.display_name,
message.channel.guild.id, message.channel.id)
logger.info("sending node result: %s", prevent_embed(result))
await message.channel.send(result)
except:
logger.exception("error handling command")
async def on_slash_command(self, ctx, message=None):
logger.info("on_slash_command %s %s", ctx.name, message)
err = "I'm taking a nap. ZzzzZZz"
try:
chatcmd = ctx.name == 'chat'
result = self.node_runner.handle(
"!cwt" + ctx.name + (' ' + message if chatcmd else ""),
ctx.author.display_name, ctx.guild_id, ctx.channel.id)
await ctx.send(err if not result else message if chatcmd else result)
except:
logger.exception("error handling command")
await ctx.send(err + "Z")
def register(self):
@self.client.event
async def on_ready():
logger.info("ready")
@self.client.event
async def on_message(message):
await self.on_message(message)
async def on_direct_message(self, message):
if message.author.id != zemke_id:
logger.info("dm from another user %s", message.content)
await message.channel.send(EventHandler.other_user_dm_response)
# DMChannel doesn't have guild
try:
await message.channel.send(self.node_runner.handle(
"!cwtcommands", message.author.display_name, "", message.channel.id))
except:
logger.exception("Failed responding to DM from other user")
await message.channel.send("Sorry, something went wrong.")
elif message.content.startswith("!adminannounce"):
[command, channel, *content] = message.content.split(" ")
content = " ".join(content).strip()
if channel == "-": # to all channels
logger.info('announcing to all channels')
for c in self.client.get_all_channels():
if isinstance(c, discord.TextChannel):
await c.send(content)
await message.channel.send(EventHandler.confirm_all_channels)
elif channel == "x": # to channel from env
if self.channel_to_mirror_to is None:
logger.info(EventHandler.not_specified)
await message.channel.send(EventHandler.not_specified)
else:
logger.info('announcing to env channel of %s', self.channel_to_mirror_to)
await self.client.get_channel(self.channel_to_mirror_to).send(content)
await message.channel.send("Message has been sent.")
else: # to channel as given by command
channel_to_send_to = self.client.get_channel(int(channel))
if channel_to_send_to is not None:
logger.info('sending to channel %s as specified by the command', channel)
await channel_to_send_to.send(content)
await message.channel.send(EventHandler.confirm_specific_channel)
else:
logger.warning("Channel %s could not be sent to as it doesn't exist", channel)
else:
await message.channel.send(
'Use "!adminannounce - <message>" to send to all channels; '
'or "!adminannounce x <message>" to send to CHANNEL (from env); '
'or "!adminannounce <channelId> <message>" to send to specific channel.')
if __name__ == "__main__":
load_dotenv()
node_runner = NodeRunner(
os.getenv("SCRIPT"),
lambda args: subprocess.run(args, stdout=subprocess.PIPE))
def listener_factory(args):
import requests
from sseclient import SSEClient
logger.info("args given to listener_factory: %s", args)
endpoint = os.getenv("CWT_MESSAGE_SSE_ENDPOINT")
logger.info("Will be listening to %s", endpoint)
return Listener(*args, node_runner, lambda: SSEClient(requests.get(endpoint, stream=True)))
beepBoop = BeepBoop(
client = discord.Client(intents=discord.Intents.default()),
getenv = os.getenv,
listener_factory = listener_factory)
avail_cmds = []
try:
avail_cmds_raw = node_runner.handle("!cwtcommands", "x", "y", "z")
logger.info("avail_cmds_raw " + str(avail_cmds_raw))
avail_cmds = [cmd[3:] for cmd in re.findall(r"!([A-Za-z]+)[,.]", avail_cmds_raw)]
except Exception as e:
logger.warn("couldn't get available commands", e)
logger.info("avail_cmds " + str(avail_cmds))
EventHandler(beepBoop.client, node_runner, beepBoop.env.channel, avail_cmds).register()
beepBoop.client.run(beepBoop.env.token)