Discord/cogs/analytics.py

65 lines
2.0 KiB
Python

import logging
from typing import Dict, List, Any
from discord import Cog, Message
from discord.ext import commands
from libbot.pycord.classes import PycordBot
from modules.database import col_analytics
logger = logging.getLogger(__name__)
class Analytics(commands.Cog):
def __init__(self, client: PycordBot):
self.client: PycordBot = client
@Cog.listener()
async def on_message(self, message: Message) -> None:
if (
(message.author != self.client.user)
and (message.author.bot is False)
and (message.author.system is False)
):
stickers: List[Dict[str, Any]] = []
for sticker in message.stickers:
stickers.append(
{
"id": sticker.id,
"name": sticker.name,
"format": sticker.format,
"url": sticker.url,
}
)
attachments: List[Dict[str, Any]] = []
for attachment in message.attachments:
attachments.append(
{
"content_type": attachment.content_type,
"description": attachment.description,
"filename": attachment.filename,
"is_spoiler": attachment.is_spoiler(),
"size": attachment.size,
"url": attachment.url,
"width": attachment.width,
"height": attachment.height,
}
)
await col_analytics.insert_one(
{
"user": message.author.id,
"channel": message.channel.id,
"content": message.content,
"stickers": stickers,
"attachments": attachments,
}
)
def setup(client: PycordBot) -> None:
client.add_cog(Analytics(client))