2022-09-02 21:30:45 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
from operator import attrgetter
|
|
|
|
|
|
|
|
import discord
|
|
|
|
|
|
|
|
import db
|
|
|
|
import util
|
2022-09-04 12:47:32 +00:00
|
|
|
from schemas.dis_s import ATTRMAP
|
2022-09-02 21:30:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DiscordClient(discord.Client):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.logger = None
|
|
|
|
self.did_something = False
|
|
|
|
name = self.__class__.__name__
|
|
|
|
self.log = util.get_logger(name)
|
|
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
async def on_ready(self):
|
|
|
|
self.log.info("Discord connection established.")
|
|
|
|
|
|
|
|
def recurse_dict(self, obj):
|
|
|
|
to_return = {}
|
|
|
|
for key, mapped in ATTRMAP.items():
|
|
|
|
try:
|
|
|
|
to_return[key] = attrgetter(mapped)(obj)
|
|
|
|
except AttributeError:
|
|
|
|
continue
|
|
|
|
|
|
|
|
return to_return
|
|
|
|
|
|
|
|
async def on_message(self, message):
|
|
|
|
if not message.content:
|
|
|
|
return
|
|
|
|
|
|
|
|
a = self.recurse_dict(message)
|
2022-09-05 06:20:30 +00:00
|
|
|
# a["ts"] = a["time"].isoformat()
|
2022-09-05 06:20:30 +00:00
|
|
|
a["ts"] = int(a["time"].timestamp())
|
2022-09-02 21:30:45 +00:00
|
|
|
del a["time"]
|
|
|
|
a["type"] = "msg"
|
|
|
|
a["src"] = "dis"
|
|
|
|
|
2022-09-13 21:17:46 +00:00
|
|
|
await db.queue_message(a)
|