dcirc.py
· 1.2 KiB · Python
Raw
#!/usr/bin/env python3
import irc3
import websockets
import asyncio
import json
@irc3.plugin
class DCIRC:
requires = ["irc3.plugins.command"]
def __init__(self, bot):
self.bot = bot
self.bot.loop.create_task(self.websocket("wss://localhost:5601"))
async def websocket(self, uri):
headers = {"Cookie": ""}
websocket = websockets.client.connect(uri, extra_headers=headers)
await websocket.send(json.dumps({"path":"session/v0/socket","method":"POST","data":{"authorization":""},"callback_id":1}))
while True:
msg = json.loads(await websocket.recv())
if "event" in msg and msg["event"] == "hub_message":
n = msg["data"]["from"]["nick"]
msg["data"]["from"]["nick"] = n[0] + "\u200b" + n[1:]
self.bot.privmsg(self.bot.config["autojoins"][0],
"<{from[nick]}> {text}".format(**msg["data"]))
def main():
config = dict(nick="", autojoins=[""], host="irc..net", port=6667, includes=["irc3.plugins.core", "irc3.plugins.command", __name__])
bot = irc3.IrcBot.from_config(config)
bot.run(forever=True)
if __name__ == "__main__":
main()
| 1 | #!/usr/bin/env python3 |
| 2 | import irc3 |
| 3 | import websockets |
| 4 | import asyncio |
| 5 | import json |
| 6 | |
| 7 | @irc3.plugin |
| 8 | class DCIRC: |
| 9 | requires = ["irc3.plugins.command"] |
| 10 | |
| 11 | def __init__(self, bot): |
| 12 | self.bot = bot |
| 13 | self.bot.loop.create_task(self.websocket("wss://localhost:5601")) |
| 14 | |
| 15 | async def websocket(self, uri): |
| 16 | headers = {"Cookie": ""} |
| 17 | websocket = websockets.client.connect(uri, extra_headers=headers) |
| 18 | await websocket.send(json.dumps({"path":"session/v0/socket","method":"POST","data":{"authorization":""},"callback_id":1})) |
| 19 | while True: |
| 20 | msg = json.loads(await websocket.recv()) |
| 21 | if "event" in msg and msg["event"] == "hub_message": |
| 22 | n = msg["data"]["from"]["nick"] |
| 23 | msg["data"]["from"]["nick"] = n[0] + "\u200b" + n[1:] |
| 24 | self.bot.privmsg(self.bot.config["autojoins"][0], |
| 25 | "<{from[nick]}> {text}".format(**msg["data"])) |
| 26 | |
| 27 | def main(): |
| 28 | config = dict(nick="", autojoins=[""], host="irc..net", port=6667, includes=["irc3.plugins.core", "irc3.plugins.command", __name__]) |
| 29 | bot = irc3.IrcBot.from_config(config) |
| 30 | bot.run(forever=True) |
| 31 | |
| 32 | if __name__ == "__main__": |
| 33 | main() |
| 34 |