From 2fb9fc77a22e516db89b442d23ad7c202114d11a Mon Sep 17 00:00:00 2001 From: nexy7574 Date: Mon, 15 Apr 2024 19:47:58 +0100 Subject: [PATCH] Change processing engine --- src/cogs/quote_quota.py | 77 +++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 42 deletions(-) diff --git a/src/cogs/quote_quota.py b/src/cogs/quote_quota.py index 790b7b4..ae8bab7 100644 --- a/src/cogs/quote_quota.py +++ b/src/cogs/quote_quota.py @@ -6,7 +6,7 @@ import io import matplotlib.pyplot as plt from datetime import timedelta, datetime from discord.ext import commands -from typing import Iterable, Annotated +from typing import Callable, Iterable, Annotated from conf import CONFIG @@ -183,6 +183,38 @@ class QuoteQuota(commands.Cog): file=file ) + def _metacounter( + self, + messages: list[discord.Message], + filter_func: Callable[[discord.Message], bool], + *, + now: datetime = None + ) -> dict[str, float | int]: + now = now or discord.utils.utcnow().replace(minute=0, second=0, microsecond=0) + counts = { + "hour": 0, + "day": 0, + "week": 0, + "all_time": 0, + "per_minute": 0.0, + "per_hour": 0.0, + "per_day": 0.0, + } + for message in messages: + if filter_func(message): + counts["all_time"] += 1 + if message.created_at > now - timedelta(hours=1): + counts["hour"] += 1 + if message.created_at > now - timedelta(days=1): + counts["day"] += 1 + if message.created_at > now - timedelta(days=7): + counts["week"] += 1 + + counts["per_minute"] = counts["hour"] / 60 + counts["per_hour"] = counts["day"] / 24 + counts["per_day"] = counts["week"] / 7 + return counts + async def _process_trump_truths(self, messages: list[discord.Message]) -> dict[str, int]: """ Processes the given messages to count the number of posts by Donald Trump. @@ -201,23 +233,7 @@ class QuoteQuota(commands.Cog): return True return False - now = discord.utils.utcnow().replace(minute=0, second=0, microsecond=0) - counts = { - "hour": 0, - "day": 0, - "week": 0, - "all_time": 0 - } - for message in messages: - if is_truth(message): - counts["all_time"] += 1 - if message.created_at > now - timedelta(hours=1): - counts["hour"] += 1 - if message.created_at > now - timedelta(days=1): - counts["day"] += 1 - if message.created_at > now - timedelta(days=7): - counts["week"] += 1 - return counts + return self._metacounter(messages, is_truth) async def _process_tate_truths(self, messages: list[discord.Message]) -> dict[str, int]: """ @@ -234,30 +250,7 @@ class QuoteQuota(commands.Cog): return True return False - now = discord.utils.utcnow().replace(minute=0, second=0, microsecond=0) - counts = { - "hour": 0, - "day": 0, - "week": 0, - "all_time": 0, - "per_minute": 0.0, - "per_hour": 0.0, - "per_day": 0.0, - } - for message in messages: - if is_truth(message): - counts["all_time"] += 1 - if message.created_at > now - timedelta(hours=1): - counts["hour"] += 1 - if message.created_at > now - timedelta(days=1): - counts["day"] += 1 - if message.created_at > now - timedelta(days=7): - counts["week"] += 1 - - counts["per_minute"] = counts["hour"] / 60 - counts["per_hour"] = counts["day"] / 24 - counts["per_day"] = counts["week"] / 7 - return counts + return self._metacounter(messages, is_truth) async def _process_all_messages(self, channel: discord.TextChannel) -> discord.Embed: """