Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from redbot.core import commands, Config, checks
- import discord
- class Karma:
- """My custom cog"""
- def __init__(self, bot):
- self.bot = bot
- self.karma_type = self.KarmaType()
- self.conf = Config.get_conf(self, 7511382811)
- self.conf.register_member(
- # thanks received
- beenThanked=0,
- allTimeBeenThanked=0,
- # thanks given
- hasThanked=0,
- allTimeHasThanked=0
- )
- class KarmaType:
- """Enum for keeping track of whether we're adding or removing karma"""
- @property
- def LIKES(self):
- return 0
- @property
- def LIKED(self):
- return 1
- async def thank_member(self, user: discord.Member = None, modifier: int = 0, karma_type: int = -1):
- """
- Modifies a user's karma score
- """
- channel = self.bot.get_channel(493101259012702210)
- await channel.send("In `thank_member`")
- # abort if we don't have a user to look up
- if user is None:
- return
- try:
- async with self.conf.member(user) as member:
- if karma_type == self.karma_type.LIKES:
- member.beenThanked.set(member.beenThanked + modifier)
- member.allTimeBeenThanked.set(member.allTimeBeenThanked + modifier)
- elif karma_type == self.karma_type.LIKED:
- member.hasThanked.set(member.hasThanked + modifier)
- member.allTimeHasThanked.set(member.allTimeHasThanked + modifier)
- except:
- await channel.send("User was not found in the member list.")
- async def thank_handler(self, reaction, user, modifier):
- message = reaction.message
- channel = message.channel
- thankee = message.user
- thanker = user
- await channel.send("In `thank_handler`")
- self.thank_member(thankee, modifier, self.karma_type.LIKED)
- self.thank_member(thanker, modifier, self.karma_type.LIKES)
- thankee_score = self.conf.member(thankee).allTimeThanks()
- thanker_score = self.conf.member(thanker).allTimeThanked()
- await channel.send(f"{thankee.display_name}'s thanked score changed!")
- await channel.send(f"{thankee.display_name} has been thanked {thankee_score} times.")
- await channel.send(f"{thanker.display_name} has thanked {thanker_score} times")
- # Reaction events
- # User adds a reaction to a message
- async def check_karma_added(self, reaction, user):
- """
- Check the reaction to see if it is a valid karma action
- If so, it increments the message author's thanked score and
- increases the user's thanks score.
- """
- channel = reaction.message.channel
- await channel.send("In `check_karma_added`")
- # the emoji used must be the correct emoji
- if str(reaction.emoji) != '👍':
- return await channel.send(f"{reaction.emoji} found but was not expected")
- # user should not be able to like their own posts
- # if message.author == user:
- # return
- self.thank_handler(reaction, user, 1)
- # User removes a reaction to a message
- async def check_karma_removed(self, reaction, user):
- """
- Check the reaction to see if it is a valid karma action
- If so, it decrements the message author's thanked score and
- increases the user's thanks score.
- """
- channel = reaction.message.channel
- await channel.send("In `check_karma_removed`")
- # the emoji used must be the correct emoji
- if str(reaction.emoji) != '👍':
- return await channel.send(f"{reaction.emoji} found but was not expected")
- # user should not be able to like their own posts
- # if message.author == user:
- # return
- self.thank_handler(reaction, user, -1)
Advertisement
Add Comment
Please, Sign In to add comment