async def notify_member_gain(self, message: discord.Message, member: discord.Member) -> None: """ notify the member that he gained karma, configureable through configuration. :param message: the discord message, used to link to the message. :param member: the member to notify, if applicable. :return: None """ if str(karma()['log']).lower() == 'true': log_message = '{}{} earned karma in {}. {}'.format( member.name + '#' + member.discriminator, f' ({member.nick})' if member.Nick is not None else '', message.channel.mention, message.jump_url) log_channel = self.bot.get_channel(int(config['channel']['log'])) log_channel.send(log_message) if str(karma()['message']).lower() == 'true': result = f'Congratulations {member.mention}, you have earned karma from {member.author.mention}. ' if str(karma()['self_delete']).lower() == 'true': result += revoke_message.format(message.author.mention) message_channel = self.bot.get_channel(message.channel.id) await message_channel.send(result) if str(karma()['emote']).lower() == 'true': await message.add_reaction(reaction_emoji()['karma_gain']) if str(karma()['self_delete']).lower() == 'true': await message.add_reaction(reaction_emoji()['karma_delete'])
async def on_reaction_add(self, reaction: discord.Reaction, user: discord.User) -> None: """ If the karma deletion is set, will remove all karma gained through the message, if the message author clicks it and then remove all reactions on the message :param reaction: reaction which was clicked :param user: user who added the reaction :return: None """ if (self.karma_service.find_message(str( reaction.message.id)) is not None and reaction.message.author.id == user.id and reaction.me): if reaction.emoji == reaction_emoji()["karma_delete"]: if str(karma()["self_delete"]).lower() == "true": log.info( "Removing karma because the karma_delete emoji was clicked by author" ) for other_reaction in reaction.message.reactions: await clear_reaction(other_reaction) await self.remove_karma(reaction.message, reaction.message.guild, "self emoji clear") elif reaction.emoji == reaction_emoji()["karma_gain"]: log.info( "Removing other aura emoji because gain was clicked by author" ) for other_reaction in reaction.message.reactions: if reaction is not other_reaction: await clear_reaction(other_reaction)
async def clear_reaction(reaction: discord.Reaction) -> None: """ clears a reaction from a message if that reaction has an emoji from aura. :param reaction: reaction to clear :return: none """ for aura_emoji in reaction_emoji().keys(): if reaction.emoji == reaction_emoji()[aura_emoji]: await reaction.clear() return
async def on_message(self, message: discord.Message) -> None: """ message listener, calls methods to validate valid karma gain and filter out blacklisted members. :param message: discord.Message :return: None """ guild_id: int = message.guild.id if not message.author.bot: # validate the message, is it a karma message? if await validate_message(message): # check if member is blacklisted if (self.blocker_service.find_member( Member(str(guild_id), message.author.id)) is None): # not blacklisted try to give karma await self.give_karma(message, message.guild) else: # is blacklisted, check configuration on how to tell the user he is blacklisted if str(config["blacklist"]["dm"]).lower() == "true": log.info( "Sending Blacklist dm to {} in guild {}".format( message.author.id, guild_id)) await message.author.send( "You have been blacklisted from giving out Karma, " "if you believe this to be an error contact {}.". format(config["blacklist"]["contact"])) if str(config["blacklist"]["emote"]).lower() == "true": await message.add_reaction( reaction_emoji()["karma_blacklist"])
async def on_message(self, message: discord.Message) -> None: """ message listener, calls methods to validate valid karma gain and filter out blacklisted members. :param message: discord.Message :return: None """ guild_id: int = message.guild.id if message.author.bot: return if not await validate_message(message): return # check if member is blacklisted if self.blocker_service.find_member( Member(str(guild_id), message.author.id)) is not None: if str(config['blacklist']['dm']).lower() == 'true': log.info( f'Sending Blacklist dm to {message.author.id} in guild {guild_id}' ) await message.author.send( f'You have been blacklisted from giving out karma, if you believe this ' + f'to be an error, contact {config["blacklist"]["contact"]}' ) if str(config['blacklist']['emote']).lower() == 'true': await message.add_reaction(reaction_emoji()['karma_blacklist']) return await self.give_karma(message, message.guild)
async def on_reaction_add(self, reaction: discord.Reaction, user: discord.User) -> None: """ If the karma deletion is set, will remove all karma gained through the message, if the message author clicks it and then remove all reactions on the message :param reaction: reaction which was clicked :param user: user who added the reaction :return: None """ if self.karma_service.find_message(str(reaction.message.id)) is None or \ reaction.message.author.id != user.id or \ not reaction.me: return if reaction.emoji == reaction_emoji()['karma_delete']: if str(karma()['self_delete']).lower() != 'true': return log.info( 'Removing karma because the karma_delete emoji was clicked by author' ) for other_reaction in reaction.message.reactions: await clear_reaction(other_reaction) await self.remove_karma(reaction.message, reaction.message.guild, 'self emoji clear') return log.info('Removing aura emojis because gain was clicked by author') for other_reaction in reaction.message.reactions: if reaction is not other_reaction: await clear_reaction(other_reaction)
def create_feedback_fields(self, embed): feedback = "" emoji = reaction_emoji() if str(karma()["emote"]).lower() == "true": feedback += "Aura will react with a {} to verify that you have given out karma. \n".format( emoji["karma_gain"] ) if str(karma()["time-emote"]).lower() == "true": feedback += "Aura will react with a {} to show that at least one user is on a cooldown with you. \n".format( emoji["karma_cooldown"] ) if str(karma()["self_delete"]).lower() == "true": feedback += "Aura will react with a {} for you to revert giving out the karma, by reacting to it.\n".format( emoji["karma_delete"] ) if str(blacklist()["emote"]).lower() == "true": feedback += "Aura will react with a {} if you are blacklisted from giving out karma. \n".format( emoji["karma_blacklist"] ) if str(karma()["message"]).lower() == "true": feedback += "Aura will congratulate the user/s in chat.\n" if str(karma()["time-message"]).lower() == "true": feedback += "Aura will remind you of the cooldown in the chat.\n" if str(blacklist()["dm"]).lower() == "true": feedback += "Aura will contact you privately, if you are blacklisted.\n" if str(karma()["edit"]).lower() == "true": feedback += "Aura will add karma, if message was not a valid karma message before editing it.\n" feedback += "Aura will remove karma, if message is not a valid karma message after editing it." embed.add_field(name="**Aura Feedback**", value=feedback) return embed
async def notify_member_gain(self, message: discord.Message, member: discord.Member) -> None: """ notify the member that he gained karma, configureable through configuration. :param message: the discord message, used to link to the message. :param member: the member to notify, if applicable. :return: None """ if str(karma()["log"]).lower() == "true": if member.nick is None: await self.bot.get_channel(int(config["channel"]["log"])).send( "{} earned karma in {}. {}".format( member.name + "#" + member.discriminator, message.channel.mention, message.jump_url, )) else: await self.bot.get_channel(int(config["channel"]["log"])).send( "{} ({}) earned karma in {}. {}".format( member.name + "#" + member.discriminator, member.nick, message.channel.mention, message.jump_url, )) if str(karma()["message"]).lower() == "true": if str(karma()["self_delete"]).lower() == "true": await self.bot.get_channel( message.channel.id ).send("Congratulations {}, you have earned karma from {}. ". format(member.mention, message.author.mention) + revoke_message.format(message.author.mention)) else: await self.bot.get_channel(message.channel.id).send( "Congratulations {}, you have earned karma from {}.". format(member.mention, message.author.mention)) if str(karma()["emote"]).lower() == "true": await message.add_reaction(reaction_emoji()["karma_gain"]) if str(karma()["self_delete"]).lower() == "true": await message.add_reaction(reaction_emoji()["karma_delete"])
def create_feedback_fields(self, embed): emoji = reaction_emoji() def update_fmt(config_key, msg, emoji_key, fb=''): if str(config_key).lower() == 'true': fb += msg.format(emoji[emoji_key]) return fb def update(config_key, msg, fb=''): if str(config_key).lower() == 'true': fb += msg return fb feedback = update_fmt(karma()['emote'], 'Aura will react with a {} to verify that you have given out karma. \n', 'karma_gain', '') feedback = update_fmt(karma()['time_emote'], 'Aura will react with a {} to show that at least one user is on a cooldown with you. \n', 'karma_cooldown', feedback) feedback = update_fmt(karma()['self_delete'], 'Aura will react with a {} for you to revert giving out the karma, by reacting to it.\n', 'karma_delete', feedback) feedback = update_fmt(blacklist()['emote'], 'Aura will react with a {} if you are blacklisted from giving out karma. \n', 'karma_blacklist', feedback) feedback = update(karma()['message'], 'Aura will congratulate the user(s) in chat.\n', feedback) feedback = update(karma()['time_message'], 'Aura will remind you of the cooldown in the chat.\n', feedback) feedback = update(blacklist()['dm'], 'Aura will contact you privately, if you are blacklisted.\n', feedback) feedback = update(blacklist()['edit'], 'Aura will add karma, if message was not a valid karma message before editing it.\n' + 'Aura will remove karma, if message is not a valid karma message after editing it.', feedback) embed.add_field(name='**Aura Feedback**', value=feedback) return embed
async def on_reaction_remove(self, reaction: discord.Reaction, user: discord.User) -> None: """ If the thumps up reaction is removed then, remove the karma gained through the karma message. :param reaction: the reaction that was removed :param user: user who added the reaction first :return: None """ # if aura made this reaction then it was very clearly a karma message if user.id != self.bot.user.id and reaction.emoji != reaction_emoji( )['karma_gain']: return message = reaction.message if self.karma_service.find_message(str(message.id)) is not None: await self.remove_karma(message, message.guild, 'reaction remove')
async def on_reaction_clear(self, message, reactions) -> None: """ If all reactions are cleared and one of those reactions is a karma gain emoji and was made by Aura then remove all karma associated. :param message: :param reactions: :return: """ for reaction in reactions: if reaction.emoji == reaction_emoji()["karma_gain"]: # reaction me is very much the same as checking the user id # was the reaction made by aura if reaction.me: # find message id in db if self.karma_service.find_message(str( message.id)) is not None: await self.remove_karma(message, message.guild, "reaction clear")
async def give_karma(self, message: discord.Message, guild: discord.Guild) -> None: """ give karma to all the users in the message except the author, other bots or aura itself :param message: message containing the mentions :param guild: guild of the karma message :return: None """ # walk through the mention list which contains discord: Members for mention in set(message.mentions): member = mention # filter out message author, aura and other bots if (member.id != message.author.id and member.id != self.bot.user.id and not self.bot.get_user(member.id).bot): # check if giver-receiver combo on cooldown if (member.id not in self._members_on_cooldown[guild.id][ message.author.id]): karma_member = KarmaMember(guild.id, member.id, message.channel.id, message.id) self.karma_service.upsert_karma_member(karma_member) await self.cooldown_user(guild.id, message.author.id, member.id) await self.notify_member_gain(message, member) log.info("{} gave karma to {} in guild {}".format( message.author.id, member.id, guild.id)) else: log.info( "Sending configured cooldown response to {} in guild {}" .format(message.author.id, guild.id)) if str(config["karma"]["time-emote"]).lower() == "true": await message.add_reaction( reaction_emoji()["karma_cooldown"]) if str(config["karma"]["time-message"]).lower() == "true": await self.bot.get_channel(message.channel.id).send( "Sorry {}, your karma for {} needs time to recharge" .format(message.author.mention, member.name))
async def give_karma(self, message: discord.Message, guild: discord.Guild) -> None: """ give karma to all the users in the message except the author, other bots or aura itself :param message: message containing the mentions :param guild: guild of the karma message :return: None """ # walk through the mention list which contains discord: Members for member in set(message.mentions): # filter out message author, aura and other bots a_id = message.author.id m_id = member.id if m_id == a_id or m_id == self.bot.user.id or self.bot.get_user( member).bot: continue # check if giver-receiver combo on cooldown if m_id in self._members_on_cooldown[guild.id][a_id]: log.info( f'Sending configured cooldown response to {a_id} in guild {guild.id}' ) if str(config['karma']['time-emote']).lower() == "true": await message.add_reaction( reaction_emoji()['karma_cooldown']) if str(config['karma']['time-message']).lower() == "true": await self.bot.get_channel(message.channel.id) \ .send(f'Sorry {message.author.mention}, your karma for {member.name} needs time to recharge') continue karma_member = KarmaMember(guild.id, member.id, message.channel.id, message.id) self.karma_service.upsert_karma_member(karma_member) await self.cooldown_user(guild.id, message.author.id, member.id) await self.notify_member_gain(message, member) log.info(f'{a_id} gave karma to {m_id} in guild {guild.id}')