logging added
This commit is contained in:
parent
b21d37b124
commit
495cb082a0
1 changed files with 99 additions and 95 deletions
|
|
@ -6,105 +6,109 @@ import discord
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
|
|
||||||
|
|
||||||
@commands.command(
|
class Highscores(commands.Cog):
|
||||||
description="Highscores",
|
|
||||||
help="Shows a bar graph of users in this channel and how many messages they have sent.",
|
|
||||||
brief="Display chat highscores",
|
|
||||||
aliases=["highscore"]
|
|
||||||
)
|
|
||||||
async def highscores(ctx, limit=15):
|
|
||||||
filename = str(ctx.channel.id) + ".log"
|
|
||||||
with open("data/chatgpt/logs/" + filename, 'r', encoding="utf-8") as logfile:
|
|
||||||
data = logfile.readlines()
|
|
||||||
logfile.close()
|
|
||||||
|
|
||||||
def is_username(user):
|
def __init__(self, bot):
|
||||||
for character in user:
|
self.bot = bot
|
||||||
if character.isupper():
|
|
||||||
return False
|
|
||||||
if not (character.isalpha() or character.isdigit() or character == '.' or character == '_'):
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
user_message_counts = {}
|
@commands.command(
|
||||||
for line in data:
|
description="Highscores",
|
||||||
try:
|
help="Shows a bar graph of users in this channel and how many messages they have sent.",
|
||||||
user = line[0:line.find(':')]
|
brief="Display chat highscores",
|
||||||
if is_username(user):
|
aliases=["highscore"]
|
||||||
if user not in user_message_counts and user != "" and len(user) <= 32:
|
)
|
||||||
user_message_counts[user] = 1
|
async def highscores(self, ctx, limit=15):
|
||||||
else:
|
filename = str(ctx.channel.id) + ".log"
|
||||||
user_message_counts[user] += 1
|
|
||||||
except Exception as error:
|
|
||||||
print("Error occurred in highscores")
|
|
||||||
|
|
||||||
sorted_message_counts = sorted(user_message_counts.items(), key=lambda x:x[1])
|
|
||||||
sorted_dict = dict(sorted_message_counts[-limit::])
|
|
||||||
keys = list(sorted_dict.keys())
|
|
||||||
values = list(sorted_dict.values())
|
|
||||||
fig, ax = plt.subplots()
|
|
||||||
bar_container = ax.barh(keys, values)
|
|
||||||
ax.set_xlabel("Message Count")
|
|
||||||
ax.set_ylabel("Username")
|
|
||||||
ax.set_title("Messages Sent in " + ctx.channel.name)
|
|
||||||
ax.bar_label(bar_container, label_type='center')
|
|
||||||
filepath = 'tmp/' + str(time.time_ns()) + '.png'
|
|
||||||
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')
|
|
||||||
await ctx.send(file=f)
|
|
||||||
|
|
||||||
|
|
||||||
@commands.command(
|
|
||||||
description="Highscores Server",
|
|
||||||
help="Shows a bar graph of users across all servers I am in and how many messages they have sent.",
|
|
||||||
brief="Display chat highscores",
|
|
||||||
aliases=["highscore_server"]
|
|
||||||
)
|
|
||||||
async def highscores_server(ctx, limit=15):
|
|
||||||
|
|
||||||
def is_username(user):
|
|
||||||
for character in user:
|
|
||||||
if character.isupper():
|
|
||||||
return False
|
|
||||||
if not (character.isalpha() or character.isdigit() or character == '.' or character == '_'):
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
user_message_counts = {}
|
|
||||||
data = []
|
|
||||||
for filename in os.listdir("data/chatgpt/logs/"):
|
|
||||||
with open("data/chatgpt/logs/" + filename, 'r', encoding="utf-8") as logfile:
|
with open("data/chatgpt/logs/" + filename, 'r', encoding="utf-8") as logfile:
|
||||||
data += logfile.readlines()
|
data = logfile.readlines()
|
||||||
logfile.close()
|
logfile.close()
|
||||||
user_message_counts = {}
|
|
||||||
for line in data:
|
|
||||||
try:
|
|
||||||
user = line[0:line.find(':')]
|
|
||||||
if is_username(user):
|
|
||||||
if user not in user_message_counts and user != "" and len(user) <= 32:
|
|
||||||
user_message_counts[user] = 1
|
|
||||||
else:
|
|
||||||
user_message_counts[user] += 1
|
|
||||||
except Exception as error:
|
|
||||||
print("Error occurred in highscores_server")
|
|
||||||
|
|
||||||
sorted_message_counts = sorted(user_message_counts.items(), key=lambda x:x[1])
|
def is_username(user):
|
||||||
sorted_dict = dict(sorted_message_counts[-limit::])
|
for character in user:
|
||||||
keys = list(sorted_dict.keys())
|
if character.isupper():
|
||||||
values = list(sorted_dict.values())
|
return False
|
||||||
fig, ax = plt.subplots()
|
if not (character.isalpha() or character.isdigit() or character == '.' or character == '_'):
|
||||||
bar_container = ax.barh(keys, values)
|
return False
|
||||||
ax.set_xlabel("Message Count")
|
return True
|
||||||
ax.set_ylabel("Username")
|
|
||||||
ax.set_title("Messages Sent in all channels I can see")
|
user_message_counts = {}
|
||||||
ax.bar_label(bar_container, label_type='center')
|
for line in data:
|
||||||
filepath = 'tmp/' + str(time.time_ns()) + '.png'
|
try:
|
||||||
plt.savefig(filepath, dpi=1000, bbox_inches="tight")
|
user = line[0:line.find(':')]
|
||||||
with open(filepath, "rb") as fh:
|
if is_username(user):
|
||||||
f = discord.File(fh, filename=str(ctx.channel.id) + '_hiscores.png')
|
if user not in user_message_counts and user != "" and len(user) <= 32:
|
||||||
await ctx.send(file=f)
|
user_message_counts[user] = 1
|
||||||
|
else:
|
||||||
|
user_message_counts[user] += 1
|
||||||
|
except Exception as error:
|
||||||
|
self.bot.logger.exception("Error occurred in highscores")
|
||||||
|
|
||||||
|
sorted_message_counts = sorted(user_message_counts.items(), key=lambda x:x[1])
|
||||||
|
sorted_dict = dict(sorted_message_counts[-limit::])
|
||||||
|
keys = list(sorted_dict.keys())
|
||||||
|
values = list(sorted_dict.values())
|
||||||
|
fig, ax = plt.subplots()
|
||||||
|
bar_container = ax.barh(keys, values)
|
||||||
|
ax.set_xlabel("Message Count")
|
||||||
|
ax.set_ylabel("Username")
|
||||||
|
ax.set_title("Messages Sent in " + ctx.channel.name)
|
||||||
|
ax.bar_label(bar_container, label_type='center')
|
||||||
|
filepath = 'tmp/' + str(time.time_ns()) + '.png'
|
||||||
|
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')
|
||||||
|
await ctx.send(file=f)
|
||||||
|
|
||||||
|
|
||||||
|
@commands.command(
|
||||||
|
description="Highscores Server",
|
||||||
|
help="Shows a bar graph of users across all servers I am in and how many messages they have sent.",
|
||||||
|
brief="Display chat highscores",
|
||||||
|
aliases=["highscore_server"]
|
||||||
|
)
|
||||||
|
async def highscores_server(self, ctx, limit=15):
|
||||||
|
|
||||||
|
def is_username(user):
|
||||||
|
for character in user:
|
||||||
|
if character.isupper():
|
||||||
|
return False
|
||||||
|
if not (character.isalpha() or character.isdigit() or character == '.' or character == '_'):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
user_message_counts = {}
|
||||||
|
data = []
|
||||||
|
for filename in os.listdir("data/chatgpt/logs/"):
|
||||||
|
with open("data/chatgpt/logs/" + filename, 'r', encoding="utf-8") as logfile:
|
||||||
|
data += logfile.readlines()
|
||||||
|
logfile.close()
|
||||||
|
user_message_counts = {}
|
||||||
|
for line in data:
|
||||||
|
try:
|
||||||
|
user = line[0:line.find(':')]
|
||||||
|
if is_username(user):
|
||||||
|
if user not in user_message_counts and user != "" and len(user) <= 32:
|
||||||
|
user_message_counts[user] = 1
|
||||||
|
else:
|
||||||
|
user_message_counts[user] += 1
|
||||||
|
except Exception as error:
|
||||||
|
self.bot.logger.exception("Error occurred in highscores_server")
|
||||||
|
|
||||||
|
sorted_message_counts = sorted(user_message_counts.items(), key=lambda x:x[1])
|
||||||
|
sorted_dict = dict(sorted_message_counts[-limit::])
|
||||||
|
keys = list(sorted_dict.keys())
|
||||||
|
values = list(sorted_dict.values())
|
||||||
|
fig, ax = plt.subplots()
|
||||||
|
bar_container = ax.barh(keys, values)
|
||||||
|
ax.set_xlabel("Message Count")
|
||||||
|
ax.set_ylabel("Username")
|
||||||
|
ax.set_title("Messages Sent in all channels I can see")
|
||||||
|
ax.bar_label(bar_container, label_type='center')
|
||||||
|
filepath = 'tmp/' + str(time.time_ns()) + '.png'
|
||||||
|
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')
|
||||||
|
await ctx.send(file=f)
|
||||||
|
|
||||||
async def setup(bot):
|
async def setup(bot):
|
||||||
bot.add_command(highscores)
|
bot.add_cog(Highscores(bot))
|
||||||
bot.add_command(highscores_server)
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue