Increase pie size, add name mapping

This commit is contained in:
Nexus 2024-03-19 00:25:45 +00:00
parent 451d9ba77e
commit faf8996a7b
Signed by: nex
GPG key ID: 0FA334385D0B689F

View file

@ -16,6 +16,7 @@ class QuoteQuota(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.quotes_channel_id = CONFIG["quote_a"].get("channel_id")
self.names = CONFIG["quote_a"].get("names", {})
@property
def quotes_channel(self) -> discord.TextChannel | None:
@ -28,18 +29,21 @@ class QuoteQuota(commands.Cog):
def generate_pie_chart(
usernames: list[str],
counts: list[int],
no_other: bool = False
) -> discord.File:
"""
Converts the given username and count tuples into a nice pretty pie chart.
:param usernames: The usernames
:param counts: The number of times the username appears in the chat
:param no_other: Disables the "other" grouping
:returns: The pie chart image
"""
def pct(v: int):
return f"{v:.1f}% ({(v / 100) * sum(counts):0f})"
return f"{v:.1f}% ({round((v / 100) * sum(counts))})"
if no_other is False:
other = []
# Any authors with less than 5% of the total count will be grouped into "other"
for i, author in enumerate(usernames.copy()):
@ -58,6 +62,8 @@ class QuoteQuota(commands.Cog):
counts,
labels=usernames,
autopct=pct,
startangle=90,
radius=2
)
fio = io.BytesIO()
fig.savefig(fio, format='jpg')
@ -78,6 +84,15 @@ class QuoteQuota(commands.Cog):
min_value=1,
max_value=365
)
],
merge_other: Annotated[
bool,
discord.Option(
bool,
name="merge_other",
description="Whether to merge authors with less than 5% of the total count into 'Other'.",
default=True
)
]
):
"""Checks the quote quota for the quotes channel."""
@ -113,15 +128,23 @@ class QuoteQuota(commands.Cog):
name = m.group(1)
name = name.strip().title()
if name == "Me":
name = message.author.name.strip().casefold()
if name in self.names:
name = self.names[name]
else:
filtered_messages += 1
continue
elif name in self.names:
name = self.names[name]
authors.setdefault(name, 0)
authors[name] += 1
file = await asyncio.to_thread(
self.generate_pie_chart,
list(authors.keys()),
list(authors.values())
list(authors.values()),
merge_other
)
return await ctx.edit(
content="{:,} messages (out of {:,}) were filtered (didn't follow format?)".format(