#!/usr/bin/env python from operator import attrgetter import discord import db import util ATTRMAP = { "msg": "content", "msg_id": "id", "nick": "author.name", "host": "author.discriminator", "ident": "author.nick", "time": "created_at", "channel": "channel.name", "channel_nsfw": "channel.nsfw", "bot": "author.bot", "user_id": "author.id", "channel_id": "channel.id", "net": "author.guild.name", "net_id": "author.guild.id", "guild_member_count": "author.guild.member_count", "channel_category": "channel.category.name", "channel_category_id": "channel.category.id", "channel_category_nsfw": "channel.category.nsfw", } 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) a["ts"] = a["time"].isoformat() del a["time"] a["type"] = "msg" a["src"] = "dis" db.store_message(a)