monolith/sources/dis.py
2022-09-23 07:20:30 +01:00

46 lines
1.1 KiB
Python

#!/usr/bin/env python
from operator import attrgetter
import discord
import db
import util
from schemas.dis_s import ATTRMAP
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)
self.log.info("Starting Discord client")
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)
# a["ts"] = a["time"].isoformat()
a["ts"] = int(a["time"].timestamp())
del a["time"]
a["type"] = "msg"
a["src"] = "dis"
await db.queue_message(a)