added max amount of users to show on graph

sorted graph from highest to lowest
cleaned up working area
This commit is contained in:
phixxy 2024-01-21 02:01:51 -08:00
parent 3adc73beaf
commit 052df7d273

View file

@ -20,7 +20,7 @@ async def handle_error(error):
brief="Display chat highscores", brief="Display chat highscores",
aliases=["highscore"] aliases=["highscore"]
) )
async def highscores(ctx, limit=0): async def highscores(ctx, limit=15):
filename = str(ctx.channel.id) + ".log" filename = str(ctx.channel.id) + ".log"
with open("channels/logs/" + filename, 'r', encoding="utf-8") as logfile: with open("channels/logs/" + filename, 'r', encoding="utf-8") as logfile:
data = logfile.readlines() data = logfile.readlines()
@ -46,25 +46,19 @@ async def highscores(ctx, limit=0):
except Exception as error: except Exception as error:
await handle_error(error) await handle_error(error)
def remove_dict_keys_if_less_than_x(dictionary,x): sorted_message_counts = sorted(user_message_counts.items(), key=lambda x:x[1])
for key in dictionary: sorted_dict = dict(sorted_message_counts[-limit::])
if dictionary[key] <= x: keys = list(sorted_dict.keys())
dictionary.pop(key) values = list(sorted_dict.values())
return remove_dict_keys_if_less_than_x(dictionary,x)
return dictionary
print(user_message_counts)
remove_dict_keys_if_less_than_x(user_message_counts,limit)
keys = list(user_message_counts.keys())
values = list(user_message_counts.values())
fig, ax = plt.subplots() fig, ax = plt.subplots()
bar_container = ax.barh(keys, values) bar_container = ax.barh(keys, values)
ax.set_xlabel("Message Count") ax.set_xlabel("Message Count")
ax.set_ylabel("Username") ax.set_ylabel("Username")
ax.set_title("Messages Sent in " + ctx.channel.name) ax.set_title("Messages Sent in " + ctx.channel.name)
ax.bar_label(bar_container, label_type='center') ax.bar_label(bar_container, label_type='center')
plt.savefig(str(ctx.channel.id) + '_hiscores.png', dpi=1000, bbox_inches="tight") filepath = 'tmp/' + str(time.time_ns()) + '.png'
with open(str(ctx.channel.id) + '_hiscores.png', "rb") as fh: plt.savefig(filepath, dpi=1000, bbox_inches="tight")
with open(filepath, "rb") as fh:
f = discord.File(fh, filename=str(ctx.channel.id) + '_hiscores.png') f = discord.File(fh, filename=str(ctx.channel.id) + '_hiscores.png')
await ctx.send(file=f) await ctx.send(file=f)
@ -75,14 +69,7 @@ async def highscores(ctx, limit=0):
brief="Display chat highscores", brief="Display chat highscores",
aliases=["highscore_server"] aliases=["highscore_server"]
) )
async def highscores_server(ctx, limit=0): async def highscores_server(ctx, limit=15):
def remove_dict_keys_if_less_than_x(dictionary,x):
for key in dictionary:
if dictionary[key] <= x:
dictionary.pop(key)
return remove_dict_keys_if_less_than_x(dictionary,x)
return dictionary
def is_username(user): def is_username(user):
for character in user: for character in user:
@ -110,21 +97,19 @@ async def highscores_server(ctx, limit=0):
except Exception as error: except Exception as error:
await handle_error(error) await handle_error(error)
print(user_message_counts) sorted_message_counts = sorted(user_message_counts.items(), key=lambda x:x[1])
print("printed") sorted_dict = dict(sorted_message_counts[-limit::])
user_message_counts = remove_dict_keys_if_less_than_x(user_message_counts,limit) keys = list(sorted_dict.keys())
keys = user_message_counts.keys() values = list(sorted_dict.values())
print(keys)
values = user_message_counts.values()
print(values)
fig, ax = plt.subplots() fig, ax = plt.subplots()
bar_container = ax.barh(keys, values) bar_container = ax.barh(keys, values)
ax.set_xlabel("Message Count") ax.set_xlabel("Message Count")
ax.set_ylabel("Username") ax.set_ylabel("Username")
ax.set_title("Messages Sent in all channels I can see") ax.set_title("Messages Sent in all channels I can see")
ax.bar_label(bar_container, label_type='center') ax.bar_label(bar_container, label_type='center')
plt.savefig(str(ctx.channel.id) + '_hiscores.png', dpi=1000, bbox_inches="tight") filepath = 'tmp/' + str(time.time_ns()) + '.png'
with open(str(ctx.channel.id) + '_hiscores.png', "rb") as fh: plt.savefig(filepath, dpi=1000, bbox_inches="tight")
with open(filepath, "rb") as fh:
f = discord.File(fh, filename=str(ctx.channel.id) + '_hiscores.png') f = discord.File(fh, filename=str(ctx.channel.id) + '_hiscores.png')
await ctx.send(file=f) await ctx.send(file=f)