From e34a0e4d10af876233acfdf57e71c19d2f09303d Mon Sep 17 00:00:00 2001 From: supopur Date: Wed, 22 Mar 2023 17:02:20 +0100 Subject: [PATCH] Added a reputation system --- commands.py | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/commands.py b/commands.py index 587cc15..3e8db07 100644 --- a/commands.py +++ b/commands.py @@ -41,28 +41,32 @@ class EssentialCommands(commands.Cog): @commands.Cog.listener() async def on_message(self, message): - print(message.content) + #print(message.content) if message.content.startswith('+rep') or message.content.startswith('-rep'): # extract the necessary information from the message content reputation_type = message.content[0] # '+' or '-' mentioned_users = message.mentions author_id = message.author.id + + if message.author in mentioned_users: + await message.channel.send("You can't give reputation to yourself.") + return comment = message.content.split(' ', 2)[2] if len(message.content.split(' ')) > 2 else '' - print(f"Rep for: {mentioned_users}") + #print(f"Rep for: {mentioned_users}") # load the reputation data from the TOML file try: with open(f"{self.rep_file}/{message.guild.id}.toml", 'r') as f: reputation_data = toml.load(f) - print(reputation_data) + #print(reputation_data) except: with open(f"{self.rep_file}/{message.guild.id}.toml", "w+") as f: f.write("[reputations]") with open(f"{self.rep_file}/{message.guild.id}.toml", 'r') as f: reputation_data = toml.load(f) - print(reputation_data) + #print(reputation_data) @@ -74,8 +78,9 @@ class EssentialCommands(commands.Cog): # check if the author has already given reputation to the user if str(author_id) in reputation_data[str(user.id)]: - await message.channel.send(f'You have already given reputation to {user.mention} before.') - continue + if reputation_data[str(user.id)][str(author_id)]["type"] == reputation_type: + await message.channel.send(f'You have already given reputation to {user.mention} before.') + continue # update the reputation data with the new entry reputation_data[str(user.id)][str(author_id)] = {'type': reputation_type, 'points': 1 if reputation_type == '+' else -1, 'comment': comment} @@ -84,7 +89,8 @@ class EssentialCommands(commands.Cog): with open(f"{self.rep_file}/{message.guild.id}.toml", 'w') as f: toml.dump(reputation_data, f) - await message.channel.send(f'You have given {reputation_type} reputation to {user.mention} with the comment: {comment}') + embed = discord.Embed(description=f"You have given {reputation_type} reputation to {user.mention} with the comment: {comment}", title="You have given reputation.") + await message.channel.send(embed=embed) @commands.slash_command() async def reputation(self, ctx, user: discord.User): @@ -107,6 +113,12 @@ class EssentialCommands(commands.Cog): # create the reputation leaderboard embed embed = discord.Embed(title=f'{user.name}#{user.discriminator}\'s Reputation', color=discord.Color.blue()) + if total_points == 0: + embed.color = discord.Color.yellow() + elif total_points < 0: + embed.color = discord.Color.red() + else: + embed.color = discord.Color.green() embed.add_field(name='Total Reputation Points', value=total_points) # loop through the reputation entries for the user and add them to the embed