Added a reputation system. How to use:

+rep @usermention You are a good guy!
-rep @usermention You are a bad guy and i dont like you!
/reputation {user}
main
supopur 2 years ago
commit e7f42313b6

@ -7,12 +7,28 @@ 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
self.rep_file = "cogs/essentials/reputation"
@discord.Cog.listener()
@ -38,6 +54,99 @@ 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
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}")
# 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)]:
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}
# 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)
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):
# 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())
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
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"]:

Loading…
Cancel
Save