-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscordbridge.py
39 lines (34 loc) · 1.13 KB
/
discordbridge.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
#! /usr/bin/env python
# bridge <-> discord bridge
import json
import socket
import threading
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect('/tmp/chatbridge')
s.sendall(b'IRC' + bytes([0xFF]))
def send(sender, message):
s.sendall(bytes([0xFE]) + bytes(json.dumps({"message": message, "sender": sender}), 'utf-8') + bytes([0xFF]))
def discordsend(text):
#Put code here to send text to Discord
def listen():
buf = b''
receiving = False
while True:
byte = s.recv(1)
if receiving:
if byte == bytes([0xFF]):
data = json.loads(buf.decode('utf-8'))
message = '[{}] <{}> {}'.format(data['platform'], data['sender'], data['message'])
dicordsend(message)
receiving = False
buf = bytes()
else:
buf += byte
else:
if byte == bytes([0xFE]):
receiving = True
buf = bytes()
thread = threading.Thread(target=listen)
thread.start()
#put code here to listen on discord
#when you get a message from discord, call send(sendername, messagetext)