From f2ef82dd478e33bcf56ac2e94eb0ea296c698c97 Mon Sep 17 00:00:00 2001 From: supopur Date: Sun, 19 Mar 2023 12:39:07 +0100 Subject: [PATCH 1/3] Removed: reaction polls, Added: reputation system wich will do +rep @user Coment -rep @user comment. It will be stored per server --- commands.py | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/commands.py b/commands.py index 6340ea8..587cc15 100644 --- a/commands.py +++ b/commands.py @@ -13,6 +13,7 @@ with open("cogs/essentials/config.toml", "r") as f: class EssentialCommands(commands.Cog): def __init__(self, bot): self.bot = bot + self.rep_file = "cogs/essentials/reputation" @discord.Cog.listener() @@ -38,6 +39,87 @@ class EssentialCommands(commands.Cog): await gb_channel.send(embed=embed) + @commands.Cog.listener() + async def on_message(self, message): + 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 + comment = message.content.split(' ', 2)[2] if len(message.content.split(' ')) > 2 else '' + + 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) + + 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) + + + + # loop through the mentioned users + for user in mentioned_users: + # create the reputation entry if it doesn't exist + if str(user.id) not in reputation_data: + reputation_data[str(user.id)] = {} + + # 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 + + # 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} + + # save the reputation data back to the TOML file + 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}') + + @commands.slash_command() + async def reputation(self, ctx, user: discord.User): + # load the reputation data from the TOML file + try: + with open(f"{self.rep_file}/{ctx.guild.id}.toml", 'r') as f: + reputation_data = toml.load(f) + except: + reputation_data = "" + + + # check if the user has any reputation entries + + if str(user.id) not in reputation_data or reputation_data == "": + await ctx.respond(f'{user.mention} has not received any reputation yet.') + return + + # get the total reputation points for the user + total_points = sum([data['points'] for author_id, data in reputation_data[str(user.id)].items()]) + + # create the reputation leaderboard embed + embed = discord.Embed(title=f'{user.name}#{user.discriminator}\'s Reputation', color=discord.Color.blue()) + embed.add_field(name='Total Reputation Points', value=total_points) + + # loop through the reputation entries for the user and add them to the embed + for author_id, data in reputation_data[str(user.id)].items(): + author = self.bot.get_user(int(author_id)) + reputation_type = '+' if data['type'] == '+' else '-' + comment = data['comment'] if data['comment'] else 'No comment.' + + embed.add_field(name=f'{author.name}#{author.discriminator} {reputation_type}', value=comment, inline=False) + + await ctx.respond(embed=embed) + + @commands.slash_command(guild_ids=guild_ids) async def ping(self, ctx): if not esconf["commands"]["ping"]: From e34a0e4d10af876233acfdf57e71c19d2f09303d Mon Sep 17 00:00:00 2001 From: supopur Date: Wed, 22 Mar 2023 17:02:20 +0100 Subject: [PATCH 2/3] 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 From 88784b5baca1471ad185ec46773b5e1dc3979ffc Mon Sep 17 00:00:00 2001 From: supopur Date: Wed, 22 Mar 2023 17:04:35 +0100 Subject: [PATCH 3/3] Fixed stuff with a dir --- commands.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/commands.py b/commands.py index 3e8db07..1dda192 100644 --- a/commands.py +++ b/commands.py @@ -7,9 +7,24 @@ with open("config.toml", "r") as f: config = toml.load(f) guild_ids=config["bot"]["guild_ids"] + +MYDIR = ("cogs/essentials/reputation") +CHECK_FOLDER = os.path.isdir(MYDIR) + +# If folder doesn't exist, then create it. +if not CHECK_FOLDER: + os.makedirs(MYDIR) + print("created folder : ", MYDIR) + +else: + print(MYDIR, "folder already exists.") + + with open("cogs/essentials/config.toml", "r") as f: esconf = toml.load(f) + + class EssentialCommands(commands.Cog): def __init__(self, bot): self.bot = bot