Handle errors better

This commit is contained in:
Tanner Collin 2021-03-26 21:06:58 +00:00
parent 5df67e3241
commit 9d41cafb3e

View File

@ -35,7 +35,11 @@ async def new_message(event):
logging.info('[SEND] {}: {}'.format(name, text)) logging.info('[SEND] {}: {}'.format(name, text))
data = dict(name=name, message=text) data = dict(name=name, message=text)
await web.post(api('/up/sendmessage'), json=data) try:
await web.post(api('/up/sendmessage'), json=data)
except aiohttp.ClientError:
logging.exception('Problem sending message to dynmap:')
async def main(): async def main():
global web global web
@ -45,22 +49,25 @@ async def main():
logging.info('Bridge loaded') logging.info('Bridge loaded')
while True: while True:
async with web.get(api('/up/world/world/' + str(last_time))) as res: try:
j = await res.json(content_type='text/plain;charset=utf-8') async with web.get(api('/up/world/world/' + str(last_time))) as res:
last_time = j['timestamp'] j = await res.json(content_type='text/plain;charset=utf-8')
for update in j['updates']: last_time = j['timestamp']
if update['type'] != 'chat': continue for update in j['updates']:
if update['playerName'].startswith('[TG] '): continue if update['type'] != 'chat': continue
if update['playerName'].startswith('[TG] '): continue
name = update['playerName'] name = update['playerName']
icon = emojis[md5(name) % len(emojis)] icon = emojis[md5(name) % len(emojis)]
icon = settings.CUSTOM_EMOJIS.get(name, icon) icon = settings.CUSTOM_EMOJIS.get(name, icon)
message = '{} <{}> {}'.format(icon, name, update['message']) message = '{} <{}> {}'.format(icon, name, update['message'])
if message == last_msg: continue if message == last_msg: continue
logging.info('[RECV] ' + message) logging.info('[RECV] ' + message)
await bot.send_message(settings.CHAT_ID, message) await bot.send_message(settings.CHAT_ID, message)
last_msg = message last_msg = message
except aiohttp.ClientError:
logging.exception('Problem getting message from dynmap:')
await asyncio.sleep(1) await asyncio.sleep(1)