Initial proof of concept for bi-directional chat

TODO: Link mc users to discord so that sent messages arent nested under
the bot user's username.
TODO: De-coupe the minecraft and discord code-bases and find a way to
get the interactive minecraft prompt working again
feature/embed-uploads
Tristan Gosselin-Hane 7 years ago
parent 18a22fa71e
commit 99e7c7d67a
  1. 1
      Pipfile
  2. 14
      Pipfile.lock
  3. 48
      webhook-bridge.py

@ -11,6 +11,7 @@ minecraft = {git = "https://github.com/ammaraskar/pyCraft.git"}
requests = "*"
cryptography = "*"
future = "*"
"discord.py" = {git = "https://github.com/Rapptz/discord.py.git"}
[requires]
python_version = "3.6"

14
Pipfile.lock generated

@ -1,7 +1,7 @@
{
"_meta": {
"hash": {
"sha256": "c36c9f342620f87cb95fb3b03c8315938b7170acf71d107c43e1840d5dd70aa7"
"sha256": "32ddb27c51c3258cd2abbf1de5432269e16fdced75a4b251ca3a1ed8d62c0cfb"
},
"pipfile-spec": 6,
"requires": {
@ -65,7 +65,6 @@
"sha256:edabd457cd23a02965166026fd9bfd196f4324fe6032e866d0f3bd0301cd486f",
"sha256:fdf1c1dc5bafc32bc5d08b054f94d659422b05aba244d6be4ddc1c72d9aa70fb"
],
"markers": "python_version != '3.1.*' and python_version != '3.3.*' and python_version != '3.2.*' and python_version != '3.0.*' and python_version >= '2.7'",
"version": "==1.11.5"
},
"chardet": {
@ -100,6 +99,9 @@
"index": "pypi",
"version": "==2.3.1"
},
"discord.py": {
"git": "https://github.com/Rapptz/discord.py.git"
},
"future": {
"hashes": [
"sha256:e39ced1ab767b5936646cedba8bcce582398233d6a627067d4c6a454c90cfedb"
@ -115,19 +117,16 @@
"version": "==2.7"
},
"minecraft": {
"git": "https://github.com/ammaraskar/pyCraft.git",
"ref": "eb302094aa7357141e5f6b9f0cc252204890fbd3"
"git": "https://github.com/ammaraskar/pyCraft.git"
},
"pycparser": {
"hashes": [
"sha256:a988718abfad80b6b157acce7bf130a30876d27603738ac39f140993246b25b3"
],
"markers": "python_version != '3.1.*' and python_version != '3.3.*' and python_version != '3.2.*' and python_version != '3.0.*' and python_version >= '2.7'",
"version": "==2.19"
},
"pycraft": {
"git": "https://github.com/ammaraskar/pyCraft.git",
"ref": "eb302094aa7357141e5f6b9f0cc252204890fbd3"
"git": "https://github.com/ammaraskar/pyCraft.git"
},
"requests": {
"hashes": [
@ -149,7 +148,6 @@
"sha256:a68ac5e15e76e7e5dd2b8f94007233e01effe3e50e8daddf69acfd81cb686baf",
"sha256:b5725a0bd4ba422ab0e66e89e030c806576753ea3ee08554382c14e685d117b5"
],
"markers": "python_version != '3.0.*' and python_version != '3.1.*' and python_version != '3.3.*' and python_version != '3.2.*' and python_version >= '2.6' and python_version < '4'",
"version": "==1.23"
}
},

@ -15,6 +15,9 @@ from minecraft.networking.connection import Connection
from minecraft.networking.packets import Packet, clientbound, serverbound
from minecraft.compat import input
import discord
import asyncio
UUID_CACHE = {}
def get_options():
@ -34,6 +37,9 @@ def get_options():
help="connect to a server in offline mode "
"(no password required)")
parser.add_option("-t", "--token", dest="discord_token", default=None,
help="discord token to log the bot in with")
(options, args) = parser.parse_args()
if not options.username:
@ -81,6 +87,9 @@ def main():
connection = Connection(
options.address, options.port, auth_token=auth_token)
#Initialize the discord part
discord_bot = discord.Client()
def handle_join_game(join_game_packet):
print('Connected.')
@ -138,6 +147,37 @@ def main():
connection.connect()
@discord_bot.event
async def on_ready():
print('Logged in as')
print(discord_bot.user.name)
print(discord_bot.user.id)
print('------')
@discord_bot.event
async def on_message(message):
if message.content.startswith('!test'):
counter = 0
tmp = await discord_bot.send_message(message.channel, 'Calculating messages...')
async for log in discord_bot.logs_from(message.channel, limit=100):
if log.author == message.author:
counter += 1
await discord_bot.edit_message(tmp, 'You have {} messages.'.format(counter))
elif message.content.startswith('!sleep'):
await asyncio.sleep(5)
await discord_bot.send_message(message.channel, 'Done sleeping')
else:
print(message.author.name)
if not message.author.bot:
await discord_bot.delete_message(message)
packet = serverbound.play.ChatPacket()
packet.message = "{}: {}".format(message.author.name, message.content)
connection.write_packet(packet)
discord_bot.run(options.discord_token)
while True:
try:
text = input()
@ -153,10 +193,8 @@ def main():
except KeyboardInterrupt:
print("Bye!")
sys.exit()
if __name__ == "__main__":
try:
main()
except:
sys.exit()
main()

Loading…
Cancel
Save