Skip to content

Commit

Permalink
Move karma graph generation to separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
jackra1n committed Mar 19, 2024
1 parent 8ffc58a commit 610f936
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 38 deletions.
1 change: 0 additions & 1 deletion core/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ async def on_command_error(self, ctx: commands.Context, error) -> None:
embed = discord.Embed(title=error_msg, description=f"```{error}```", color=discord.Color.red())
await self.get_channel(ERRORS_CHANNEL_ID).send(embed=embed)


async def close(self) -> None:
await self.db.pool.close()
await super().close()
77 changes: 40 additions & 37 deletions extensions/karma.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import re
import os
import re
from datetime import datetime

import discord
Expand Down Expand Up @@ -456,45 +456,48 @@ async def karma_graph(self, ctx: commands.Context):
timestamp = datetime.now().timestamp()
filename = f"karma_graph_{timestamp}.png"

x = [entry[1] for entry in karma_percentiles]
y = [entry[0] for entry in karma_percentiles]

plt.figure(figsize=(10, 7), facecolor='#141415')
plt.bar(x, y, color='#6971f8', width=4)

plt.title('Karma Graph', color='white')
plt.xlabel('Percentile of users', color='white')
plt.ylabel('Total karma', color='white')

plt.gca().spines['bottom'].set_color('white')
plt.gca().spines['left'].set_color('white')
plt.gca().tick_params(axis='x', colors='white')
plt.gca().tick_params(axis='y', colors='white')

plt.gca().set_facecolor('#141415')
plt.gca().spines['top'].set_visible(False)
plt.gca().spines['right'].set_visible(False)
plt.gca().spines['left'].set_visible(False)
plt.gca().spines['bottom'].set_visible(False)

plt.grid(axis='x', visible=False)
plt.grid(axis='y', color='#4a4d60')

def custom_formatter(x, pos):
if x >= 1e6: # Millions
return f'{x*1e-6:.0f}M'
elif x >= 1e3: # Thousands
return f'{x*1e-3:.0f}K'
else:
return f'{x:.0f}'

plt.gca().yaxis.set_major_formatter(plt.FuncFormatter(custom_formatter))
plt.savefig(filename, facecolor='#141415')

filename = self._generate_graph(filename, karma_percentiles)
await ctx.send(file=discord.File(filename, filename=f"{filename}.png"))
plt.close()
os.remove(filename)

def _generate_graph(self, filename: str, data: list[tuple[int, int]]) -> str:
x = [entry[1] for entry in data]
y = [entry[0] for entry in data]

plt.figure(figsize=(10, 6), facecolor="#141415")
plt.bar(x, y, color="#6971f8", width=4)

plt.title("Karma Graph", color="white", loc="left", fontsize=16)
plt.xlabel("Percentile of users", color="white")
plt.ylabel("Total karma", color="white")

plt.gca().spines["bottom"].set_color("white")
plt.gca().spines["left"].set_color("white")
plt.gca().tick_params(axis="x", colors="white")
plt.gca().tick_params(axis="y", colors="white")

plt.gca().set_facecolor("#141415")
plt.gca().spines["top"].set_visible(False)
plt.gca().spines["right"].set_visible(False)
plt.gca().spines["left"].set_visible(False)
plt.gca().spines["bottom"].set_visible(False)

plt.grid(axis="x", visible=False)
plt.grid(axis="y", color="#4a4d60")

def custom_formatter(x, pos):
if x >= 1e6: # Millions
return f"{x*1e-6:.0f}M"
elif x >= 1e3: # Thousands
return f"{x*1e-3:.0f}K"
else:
return f"{x:.0f}"

plt.gca().yaxis.set_major_formatter(plt.FuncFormatter(custom_formatter))
plt.savefig(filename, facecolor="#141415")
plt.close()
return filename

@commands.hybrid_group(name="post", aliases=["po"], invoke_without_command=True)
async def post(self, ctx: commands.Context):
await ctx.send_help(ctx.command)
Expand Down

0 comments on commit 610f936

Please sign in to comment.