From dab473ce6eca4788dbec6c7d41649ff26a0d0799 Mon Sep 17 00:00:00 2001 From: Nathanael Sensfelder Date: Sat, 26 Jan 2019 23:37:17 +0100 Subject: Adds a quick'n'dirty script to connect to Discord. --- LICENSE | 27 ++++++++++++++++++++ README.md | 21 ++++++++++++++++ jh-discord.py | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 jh-discord.py diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8325baa --- /dev/null +++ b/LICENSE @@ -0,0 +1,27 @@ +Copyright (c) 2019, Nathanaƫl Sensfelder +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +* Neither the name of JabberHive nor the names of its contributors may be used + to endorse or promote products derived from this software without specific + prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..9d1c760 --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +## What is JabberHive? +JabberHive is a modular Reply Bot system. All "modules" are in fact separate +programs linked together using the JabberHive Protocol. Please refer to the +protocol for more information. + +## Component Description +* Discord Gateway for a JabberHive network. + +## JabberHive Protocol Compatibility +* **Protocol Version(s):** 1. +* **Inbound Connections:** None. +* **Outbound Connections:** Single. +* **Pipelining:** No. +* **Behavior:** Gateway. + +## Dependencies +- Python 3.5+ +- discord.py (https://github.com/Rapptz/discord.py) + +## Example of Use +``./jh-discord -d /tmp/lc0 -t YOUR_TOKEN'' diff --git a/jh-discord.py b/jh-discord.py new file mode 100644 index 0000000..d9e4503 --- /dev/null +++ b/jh-discord.py @@ -0,0 +1,80 @@ +import discord +import asyncio +import argparse +import socket + + +################################################################################ +## MAIN ######################################################################## +################################################################################ +parser = argparse.ArgumentParser( + description = ( + "Discord Client for JabberHive" + ) +) + +parser.add_argument( + '-d', + '--destination', + type = str, + help = 'UNIX socket this client connects to.', +) + +parser.add_argument( + '-t', + '--token', + type = str, + help = 'Discord token.', +) + +args = parser.parse_args() +server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + +client = discord.Client() + +def get_jh_reply (): + is_done = False + result = "" + jh_reply = b"" + + matched = 0 + + while not is_done: + c = b"\0" + jh_reply = b"" + + while (c != b"\n"): + c = server.recv(1) + jh_reply += c + + print(jh_reply) + + if ((jh_reply == b"!P \n") or (jh_reply == b"!N \n")): + is_done = True + else: + jh_reply = jh_reply.decode("UTF-8") + + if (jh_reply.startswith("!GR ")): + result = jh_reply[4:] + result = result[:-1] + + return result + +@client.event +async def on_ready(): + print('Logged in as') + print(client.user.name) + print(client.user.id) + print('------') + server.connect(args.destination) + +@client.event +async def on_message(message): + server.sendall(b"?RLR " + bytes(message.content, "utf8") + b"\n") + result = get_jh_reply() + print("Result: " + result) + + if (len(result) > 0): + await client.send_message(message.channel, result) + +client.run(args.token) -- cgit v1.2.3-70-g09d2