Skip to content

Instantly share code, notes, and snippets.

@rvente
Last active May 27, 2023 05:12
Show Gist options
  • Save rvente/20ec4f8a42fb308d95170e81f8daa688 to your computer and use it in GitHub Desktop.
Save rvente/20ec4f8a42fb308d95170e81f8daa688 to your computer and use it in GitHub Desktop.
Context: this is a weekend-ware demonstration. This code isn't running in prod anywhere.
import discord
import os
from dotenv import load_dotenv
from zmq_client import ZMQClient
import zmq
load_dotenv()
DISCORD_AUTH_KEY = os.getenv('DISCORD_AUTH_KEY', "")
DISCORD_BOT_USER_ID: int = "<YOUR_BOT_USER_INTEGER>"
DATASTREAM_CHANNEL: int = "<YOUR_SELECTED_CHANNEL>"
DEALER_IP = "tcp://localhost:5557"
class VultureClient(discord.Client):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.zmq_client = ZMQClient("tcp://localhost:5557")
self.active = True
async def on_ready(self):
print('Logged on as', self.user)
async def on_message(self, message):
# don't respond to ourselves
if message.author == self.user:
return
if message.content == 'ping':
await message.channel.send('pong')
if message.channel.id == DATASTREAM_CHANNEL:
self.active = True
if "pause datastream" in message.content:
self.active = False
await message.delete(delay=2)
return # need to not send this to the server
await message.delete(delay=20)
print(message.content)
response = None
async for output in self.zmq_client.send_message(message.content):
"""editing the same message"""
NUM_LINES = 20
def stream_format(s, v_padding_newlines = NUM_LINES):
return f"```[{self.user.display_name}]\n" + "\n" * v_padding_newlines-1 + s + "```"
if response is None:
response = await message.channel.send(stream_format(output))
else:
tail_trim = "\n".join(output.split('\n')[-NUM_LINES:])[:1000]
v_padding_newlines = NUM_LINES - (tail_trim.count("\n") + 1)
tail_trim_padded = stream_format(tail_trim, v_padding_newlines)
response = await response.edit(content=tail_trim_padded)
if self.active == False:
break
if response is None:
return
await response.edit(content=response.content + "\nFinished processing")
await response.delete(delay=120)
if __name__ == "__main__":
intents = discord.Intents.default()
intents.message_content = True
client = VultureClient(intents=intents, dealer_ip=DEALER_IP)
client.run(token=DISCORD_AUTH_KEY)
import os
import zmq
import time
from datetime import datetime
if __name__ == "__main__":
DEALER_IP = "tcp://*:5557"
context = zmq.Context()
dealer = context.socket(zmq.PAIR)
dealer.bind(DEALER_IP)
while True:
inpt = dealer.recv().decode()
output = inpt
for i in range(40):
curr_time_human_readable = "UTC " + datetime.utcnow().strftime('%H:%M:%S.%f')[:-3]
snd_str = curr_time_human_readable +" " +str(output) + " " + str(i)
dealer.send_string(snd_str)
time.sleep(.4)
print(snd_str)
dealer.send_string("DONE")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment