/
eddn.py
87 lines (67 loc) · 2.52 KB
/
eddn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import asyncio
import datetime
import traceback
import discord
import zlib
import zmq
from zmq.asyncio import Context
import sys
import time
import json
from types import SimpleNamespace
# import pyperclip
import config
"""
" Configuration
"""
__relayEDDN = 'tcp://eddn.edcd.io:9500'
__timeoutEDDN = 600000
"""
" Start
"""
async def eddn(bot):
context = Context.instance()
subscriber = context.socket(zmq.SUB)
subscriber.subscribe(b"")
subscriber.set(zmq.RCVTIMEO, __timeoutEDDN)
allowed_events = ['Location', 'FSDJump']
bgs = bot.get_cog('BGS')
if bgs is None:
bot.load_extension('cogs.bgs')
bgs = bot.get_cog('BGS')
while bot.bgs_run:
try:
subscriber.connect(__relayEDDN)
while bot.bgs_run:
__message = await subscriber.recv()
if not __message:
subscriber.disconnect(__relayEDDN)
break
__message = zlib.decompress(__message)
if ("prismatic imperium" in str(__message).lower()
or "adamantine union" in str(__message).lower()
or "colonists of aurora" in str(__message).lower()):
__json = json.loads(__message, object_hook=lambda d: SimpleNamespace(**d))
message = __json.message
if message.event in allowed_events:
await bgs.submit(message)
except zmq.ZMQError as e:
print('ZMQSocketException: ' + str(e))
sys.stdout.flush()
subscriber.disconnect(__relayEDDN)
time.sleep(5)
except Exception as error:
embed = discord.Embed(title='Command Exception', color=discord.Color.red())
embed.set_footer(text='Occured on')
embed.timestamp = datetime.datetime.utcnow()
exc = ''.join(traceback.format_exception(type(error), error, error.__traceback__, chain=False))
exc = exc.replace('`', '\u200b`')
embed.description = '```py\n{}\n```'.format(exc)
embed.add_field(name='EDDN error', value="EDDN encountered an error")
try:
for channel in config.ERROR_CHANNELS:
await bot.get_channel(channel).send(type(error), embed=embed)
except Exception as error:
print(error)
subscriber.disconnect(__relayEDDN)
time.sleep(5)