Jarquafelmu

Untitled

Sep 23rd, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.08 KB | None | 0 0
  1. from redbot.core import commands, Config, checks
  2. import discord
  3.  
  4. class Karma:
  5.     """My custom cog"""
  6.  
  7.     def __init__(self, bot):
  8.         self.bot = bot
  9.         self.karma_type = self.KarmaType()
  10.  
  11.         self.conf = Config.get_conf(self, 7511382811)
  12.         self.conf.register_member(
  13.             # thanks received
  14.             beenThanked=0,
  15.             allTimeBeenThanked=0,
  16.  
  17.             # thanks given
  18.             hasThanked=0,
  19.             allTimeHasThanked=0
  20.         )
  21.  
  22.     class KarmaType:
  23.         """Enum for keeping track of whether we're adding or removing karma"""
  24.         @property
  25.         def LIKES(self):
  26.             return 0
  27.  
  28.         @property
  29.         def LIKED(self):
  30.             return 1
  31.        
  32.     async def thank_member(self, user: discord.Member = None, modifier: int = 0, karma_type: int = -1):
  33.         """
  34.        Modifies a user's karma score
  35.        """
  36.        
  37.         channel = self.bot.get_channel(493101259012702210)
  38.         await channel.send("In `thank_member`")
  39.        
  40.         # abort if we don't have a user to look up
  41.         if user is None:
  42.             return
  43.  
  44.         try:
  45.             async with self.conf.member(user) as member:
  46.                 if karma_type == self.karma_type.LIKES:
  47.                     member.beenThanked.set(member.beenThanked + modifier)
  48.                     member.allTimeBeenThanked.set(member.allTimeBeenThanked + modifier)
  49.                 elif karma_type == self.karma_type.LIKED:
  50.                     member.hasThanked.set(member.hasThanked + modifier)
  51.                     member.allTimeHasThanked.set(member.allTimeHasThanked + modifier)
  52.         except:
  53.             await channel.send("User was not found in the member list.")
  54.            
  55.        
  56.     async def thank_handler(self, reaction, user, modifier):        
  57.         message = reaction.message
  58.         channel = message.channel
  59.         thankee = message.user
  60.         thanker = user
  61.        
  62.         await channel.send("In `thank_handler`")
  63.                
  64.         self.thank_member(thankee, modifier, self.karma_type.LIKED)
  65.         self.thank_member(thanker, modifier, self.karma_type.LIKES)
  66.  
  67.         thankee_score = self.conf.member(thankee).allTimeThanks()
  68.         thanker_score = self.conf.member(thanker).allTimeThanked()
  69.  
  70.         await channel.send(f"{thankee.display_name}'s thanked score changed!")
  71.  
  72.         await channel.send(f"{thankee.display_name} has been thanked {thankee_score} times.")
  73.  
  74.         await channel.send(f"{thanker.display_name} has thanked {thanker_score} times")
  75.  
  76.     # Reaction events
  77.     # User adds a reaction to a message
  78.     async def check_karma_added(self, reaction, user):
  79.         """
  80.        Check the reaction to see if it is a valid karma action
  81.        If so, it increments the message author's thanked score and
  82.        increases the user's thanks score.
  83.        """
  84.        
  85.         channel = reaction.message.channel
  86.        
  87.         await channel.send("In `check_karma_added`")
  88.        
  89.         # the emoji used must be the correct emoji
  90.         if str(reaction.emoji) != '👍':
  91.             return await channel.send(f"{reaction.emoji} found but was not expected")
  92.         # user should not be able to like their own posts
  93.         # if message.author == user:
  94.         #   return
  95.        
  96.         self.thank_handler(reaction, user, 1)
  97.  
  98.     # User removes a reaction to a message
  99.     async def check_karma_removed(self, reaction, user):
  100.         """
  101.        Check the reaction to see if it is a valid karma action
  102.        If so, it decrements the message author's thanked score and
  103.        increases the user's thanks score.
  104.        """
  105.         channel = reaction.message.channel
  106.         await channel.send("In `check_karma_removed`")
  107.         # the emoji used must be the correct emoji
  108.         if str(reaction.emoji) != '👍':
  109.             return await channel.send(f"{reaction.emoji} found but was not expected")
  110.        
  111.         # user should not be able to like their own posts
  112.         # if message.author == user:
  113.         #   return
  114.        
  115.         self.thank_handler(reaction, user, -1)
Advertisement
Add Comment
Please, Sign In to add comment