Implemented hacky auto-reconnect from kick because of a pyCraft bug.

feature/embed-uploads
Tristan Gosselin-Hane 7 years ago
parent 54121ad015
commit ee8eeb1640
  1. 45
      webhook-bridge.py

@ -7,6 +7,7 @@ import sys
import re import re
import requests import requests
import json import json
import time
from optparse import OptionParser from optparse import OptionParser
from config import Configuration from config import Configuration
from database import DiscordChannel from database import DiscordChannel
@ -94,12 +95,46 @@ def main():
#Initialize the discord part #Initialize the discord part
discord_bot = discord.Client() discord_bot = discord.Client()
def handle_join_game(join_game_packet): def handle_disconnect(join_game_packet):
print('Connected.') print('Disconnected.')
nonlocal connection
connection.disconnect(immediate=True)
time.sleep(5)
print('Reconnecting.')
if options.offline:
print("Connecting in offline mode...")
connection = Connection(
options.address, options.port, username=options.username)
else:
auth_token = authentication.AuthenticationToken()
try:
auth_token.authenticate(options.username, options.password)
except YggdrasilError as e:
print(e)
sys.exit()
print("Logged in as %s..." % auth_token.username)
connection = Connection(
options.address, options.port, auth_token=auth_token)
register_handlers(connection)
connection.connect()
def register_handlers(connection):
connection.register_packet_listener( connection.register_packet_listener(
handle_join_game, clientbound.play.JoinGamePacket) handle_join_game, clientbound.play.JoinGamePacket)
connection.register_packet_listener(
print_chat, clientbound.play.ChatMessagePacket)
connection.register_packet_listener(
handle_health_update, clientbound.play.UpdateHealthPacket)
connection.register_packet_listener(
handle_disconnect, clientbound.play.DisconnectPacket)
def handle_join_game(join_game_packet):
print('Connected.')
def print_chat(chat_packet): def print_chat(chat_packet):
json_data = json.loads(chat_packet.json_data) json_data = json.loads(chat_packet.json_data)
@ -146,9 +181,6 @@ def main():
'embeds': [{'title': '{}'.format(message)}]} 'embeds': [{'title': '{}'.format(message)}]}
post = requests.post(WEBHOOK_URL,json=webhook_payload) post = requests.post(WEBHOOK_URL,json=webhook_payload)
connection.register_packet_listener(
print_chat, clientbound.play.ChatMessagePacket)
def handle_health_update(health_update_packet): def handle_health_update(health_update_packet):
if health_update_packet.health <= 0: if health_update_packet.health <= 0:
#We need to respawn!!!! #We need to respawn!!!!
@ -157,8 +189,7 @@ def main():
packet.action_id = serverbound.play.ClientStatusPacket.RESPAWN packet.action_id = serverbound.play.ClientStatusPacket.RESPAWN
connection.write_packet(packet) connection.write_packet(packet)
connection.register_packet_listener( register_handlers(connection)
handle_health_update, clientbound.play.UpdateHealthPacket)
connection.connect() connection.connect()

Loading…
Cancel
Save