2023-05-06 17:52:46 +02:00
|
|
|
import logging
|
2024-12-27 22:23:41 +01:00
|
|
|
from logging import Logger
|
2024-12-16 20:34:37 +01:00
|
|
|
from typing import Dict, List, Any
|
2023-05-06 19:17:40 +02:00
|
|
|
|
|
|
|
from discord import Cog, Message
|
2023-05-06 17:52:46 +02:00
|
|
|
from discord.ext import commands
|
|
|
|
|
2024-12-17 22:14:06 +01:00
|
|
|
from classes.holo_bot import HoloBot
|
2023-05-06 19:17:40 +02:00
|
|
|
from modules.database import col_analytics
|
2023-05-06 17:52:46 +02:00
|
|
|
|
2024-12-27 22:23:41 +01:00
|
|
|
logger: Logger = logging.getLogger(__name__)
|
2023-05-06 17:52:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Analytics(commands.Cog):
|
2024-12-17 22:14:06 +01:00
|
|
|
def __init__(self, client: HoloBot):
|
|
|
|
self.client: HoloBot = client
|
2023-05-06 17:52:46 +02:00
|
|
|
|
|
|
|
@Cog.listener()
|
2024-12-16 20:34:37 +01:00
|
|
|
async def on_message(self, message: Message) -> None:
|
2024-12-27 20:30:32 +01:00
|
|
|
"""Listener that collects analytical data (stickers, attachments, messages)."""
|
2023-05-06 17:52:46 +02:00
|
|
|
if (
|
|
|
|
(message.author != self.client.user)
|
2024-12-15 23:21:41 +01:00
|
|
|
and (message.author.bot is False)
|
|
|
|
and (message.author.system is False)
|
2023-05-06 17:52:46 +02:00
|
|
|
):
|
2024-12-27 20:30:32 +01:00
|
|
|
# Handle stickers
|
2024-12-16 20:34:37 +01:00
|
|
|
stickers: List[Dict[str, Any]] = []
|
|
|
|
|
2023-05-07 09:57:35 +02:00
|
|
|
for sticker in message.stickers:
|
|
|
|
stickers.append(
|
|
|
|
{
|
|
|
|
"id": sticker.id,
|
|
|
|
"name": sticker.name,
|
|
|
|
"format": sticker.format,
|
|
|
|
"url": sticker.url,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-12-27 20:30:32 +01:00
|
|
|
# Handle attachments
|
2024-12-16 20:34:37 +01:00
|
|
|
attachments: List[Dict[str, Any]] = []
|
|
|
|
|
2023-05-07 09:57:35 +02:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-12-27 20:30:32 +01:00
|
|
|
# Insert entry into the database
|
2024-12-16 16:25:35 +01:00
|
|
|
await col_analytics.insert_one(
|
2023-05-06 17:52:46 +02:00
|
|
|
{
|
2025-02-09 23:00:18 +01:00
|
|
|
"user_id": message.author.id,
|
2023-05-06 17:52:46 +02:00
|
|
|
"channel": message.channel.id,
|
2023-05-07 09:57:35 +02:00
|
|
|
"content": message.content,
|
2023-05-08 19:27:22 +02:00
|
|
|
"stickers": stickers,
|
2023-05-07 09:57:35 +02:00
|
|
|
"attachments": attachments,
|
2023-05-06 17:52:46 +02:00
|
|
|
}
|
|
|
|
)
|
2024-06-23 12:05:03 +02:00
|
|
|
|
|
|
|
|
2024-12-17 22:14:06 +01:00
|
|
|
def setup(client: HoloBot) -> None:
|
2024-06-23 12:05:03 +02:00
|
|
|
client.add_cog(Analytics(client))
|