From 2130c993b041e4f0a331309cb8b8f6968304bfa9 Mon Sep 17 00:00:00 2001 From: nexy7574 Date: Mon, 15 Apr 2024 18:24:55 +0100 Subject: [PATCH] Add truths_counter command --- src/cogs/quote_quota.py | 68 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/src/cogs/quote_quota.py b/src/cogs/quote_quota.py index b48cabc..ed3b048 100644 --- a/src/cogs/quote_quota.py +++ b/src/cogs/quote_quota.py @@ -4,7 +4,7 @@ import re import discord import io import matplotlib.pyplot as plt -from datetime import timedelta +from datetime import timedelta, datetime from discord.ext import commands from typing import Iterable, Annotated @@ -183,6 +183,72 @@ class QuoteQuota(commands.Cog): file=file ) + @commands.slash_command(name="truths") + async def truths_counter(self, ctx: discord.ApplicationContext): + """Counts the number of times the word 'truth' has been said in the quotes channel.""" + channel = discord.utils.get(ctx.guild.text_channels, name="spam") + BOT_ID = 1101439218334576742 + if not channel: + return await ctx.respond(":x: Cannot find spam channel.") + + await ctx.defer() + now = discord.utils.utcnow().replace(minute=0, second=0, microsecond=0) + + embed = discord.Embed( + title="Counting truths, please wait.", + description="Counting truths for this hour", + color=discord.Color.blurple(), + timestamp=now + ) + await ctx.respond(embed=embed) + + count_hour = 0 + count_day = 0 + count_week = 0 + count_all_time = 0 + + async for message in channel.history(limit=None, after=now - timedelta(hours=1), before=now): + if message.author.id == BOT_ID: + if not any([x.type == "rich" for x in message.embeds]): + continue + count_hour += 1 + + per_minute = count_hour / 60 + embed.add_field(name="This hour", value=f"{count_hour:,} ({per_minute:,.1f}/min)", inline=False) + embed.description = "counting truths for the last 24 hours" + await ctx.edit(embed=embed) + + async for message in channel.history(limit=None, after=now - timedelta(hours=1), before=now): + if message.author.id == BOT_ID: + if not any([x.type == "rich" for x in message.embeds]): + continue + count_hour += 1 + per_hour = count_day / 24 + embed.add_field(name="Last 24 hours", value=f"{count_day:,} ({per_hour:,.1f}/hour)", inline=False) + embed.description = "counting truths for the last 7 days" + await ctx.edit(embed=embed) + + async for message in channel.history(limit=None, after=now - timedelta(days=7), before=now): + if message.author.id == BOT_ID: + if not any([x.type == "rich" for x in message.embeds]): + continue + count_week += 1 + per_day = count_week / 7 + embed.add_field(name="Last 7 days", value=f"{count_week:,} ({per_day:,.1f}/day)", inline=False) + embed.description = "counting truths for all time" + await ctx.edit(embed=embed) + + async for message in channel.history(limit=None, after=discord.Object(1228516325681532928)): # bot's first msg + if message.author.id == BOT_ID: + if not any([x.type == "rich" for x in message.embeds]): + continue + count_all_time += 1 + + embed.add_field(name="All time", value=f"{count_all_time:,}", inline=False) + embed.description = None + embed.title = "Truth count" + await ctx.edit(embed=embed) + def setup(bot): bot.add_cog(QuoteQuota(bot))