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