From 1fc0983f0bf53fc8b71b00711fe12bff2d2326bd Mon Sep 17 00:00:00 2001 From: Soulsender Date: Thu, 6 Jun 2024 02:26:02 -0700 Subject: [PATCH] added leaderboard command and tracking --- cogs/leaderboard.py | 49 +++++++++++++++++++++++++++++++++++++++++++++ data.csv | 3 +-- main.py | 36 ++++++++++++++++++++++++++++----- 3 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 cogs/leaderboard.py diff --git a/cogs/leaderboard.py b/cogs/leaderboard.py new file mode 100644 index 0000000..6b612b9 --- /dev/null +++ b/cogs/leaderboard.py @@ -0,0 +1,49 @@ +import nextcord +from nextcord.ext import commands +import os +import csv + +# get path name for csv file +file_path = os.path.join(os.getcwd(), "") + +class Leaderboard(commands.Cog): + def __init__(self,bot): + self.bot=bot + + @commands.Cog.listener() + async def on_ready(self): + print(f"leaderboard - Loaded") + + @nextcord.slash_command(name="leaderboard", description="Frong leaderboard") + async def leaderboard(self, interaction: nextcord.Interaction): + csv_dict = {} + data = "" + total = 0 + with open(file_path + "data.csv", 'r') as file: + reader = csv.DictReader(file) + + # turn csv into dictionary + for row in reader: + key = row.pop('Name') # Replace 'Key_Column_Name' with the actual column name for the key + csv_dict[key] = row + + # sort the dictionary + sorted_dict = dict(sorted(csv_dict.items(), key=lambda x: int(x[1]['Value']), reverse=True)) + + # get values from dictionary + for key, value in sorted_dict.items(): + content = "**" + key + "**: " + data += content + for key2, value2 in dict(value).items(): + content2 = value2 + "\n" + data += content2 + total += int(value2) + + # send embed + embed = nextcord.Embed(title=":crown: **LEADERBOARD**", color=0xd6b509) + embed.add_field(name=":speech_balloon: __FRONGS BY USER__", value=data,inline=False) + embed.add_field(name=":loudspeaker: __TOTAL FRONGS__", value=total,inline=False) + await interaction.response.send_message(embed=embed, ephemeral=True) + +def setup(bot): + bot.add_cog(Leaderboard(bot)) \ No newline at end of file diff --git a/data.csv b/data.csv index 607decd..ca6bb2b 100644 --- a/data.csv +++ b/data.csv @@ -1,2 +1 @@ -Name,Value - +Name,Value diff --git a/main.py b/main.py index fa05e86..467469e 100644 --- a/main.py +++ b/main.py @@ -4,9 +4,30 @@ import os import aiohttp from random import randint +import csv load_dotenv() +def update_csv(name, filename): + updated = False + with open(filename, 'r') as file: + reader = csv.DictReader(file) + rows = list(reader) + for row in rows: + if row['Name'] == name: + row['Value'] = str(int(row['Value']) + 1) + updated = True + break + else: + rows.append({'Name': name, 'Value': '1'}) + updated = True + + if updated: + with open(filename, 'w', newline='') as file: + writer = csv.DictWriter(file, fieldnames=['Name', 'Value']) + writer.writeheader() + writer.writerows(rows) + def main(): # allows privledged intents for monitoring members joining, roles editing, and role assignments intents = nextcord.Intents.default() @@ -24,17 +45,18 @@ def main(): owner_id="null", ) + csv_file = "data.csv" # responses for the arch user replies responses = ["", "Oh you use arch? Why don’t you `sudo pacman -S some-bitches`.", """ -``` -sudo pacman -Syu -reboot + ``` + sudo pacman -Syu + reboot -grub rescue> -``` + grub rescue> + ``` """, "https://tenor.com/view/arch-linux-linux-open-source-arch-i-use-arch-btw-gif-25315741", "https://tenor.com/view/arch-linux-i-use-arch-lonely-gif-26341678", @@ -78,6 +100,10 @@ async def on_message(message): frong_words = ["frong"] for word in frong_words: if word.lower() in content_lower: + # increment message author frong count + update_csv(str(message.author), 'data.csv') + + # send frong response await message.channel.send('frong', files=[nextcord.File('frong.png')]) return