def restart_avaiable_karma(self): with DB.atomic() as transaction: try: Karma.update(avaiable_karma = DEFAULT_AVAIABLE_KARMA) \ .execute() except Exception as e: transaction.rollback() raise e return True
def reset_server_karma(self, guildId): with DB.atomic() as transaction: try: Karma.update(karma = 0).where(Karma.guild == guildId) \ .execute() except Exception as e: transaction.rollback() raise e return True
def take_karma(self, giverId, givenId, guildId): with DB.atomic() as transaction: try: given = self._fetch_karma(givenId, guildId) giver = self._fetch_karma(giverId, guildId) given.karma -= 1 giver.avaiable_karma -= 1 given.save() giver.save() except Exception as e: transaction.rollback() raise e return True
async def behave(self, ctx): user = ctx.message.content[:-2:] # Remove the ++ action = ctx.message.content[-2::] # Get the ++ or -- try: user = await self.userConverter.convert(ctx, user) except ConversionError: await ctx.send("Wrong user given!") return giver_id = int(ctx.message.author.id) given_id = int(user.id) if giver_id == given_id: await ctx.send("Giving karma to yourself, what a f****t.") return guild = ctx.message.guild try: userObj = await guild.fetch_member(given_id) except (Forbidden, HTTPException): await ctx.send("Cannot fetch given user!") return if not userObj: await ctx.send("User is not in this server!") return giver_id = str(giver_id) given_id = str(given_id) guild_id = str(guild.id) with DB.transaction() as transaction: try: if not self.guildRepo.check_guild(guild_id): self.guildRepo.insert_guild(guild_id, guild.name) if not self.clientRepo.check_user(giver_id): self.clientRepo.insert_user(giver_id, ctx.message.author.name) if not self.clientRepo.check_user(given_id): self.clientRepo.insert_user(given_id, user.name) if not self.karmaRepo.check_user_has_karma(giver_id, guild_id): self.karmaRepo.create_karma(giver_id, guild_id) if not self.karmaRepo.check_user_has_karma(given_id, guild_id): self.karmaRepo.create_karma(given_id, guild_id) except Exception as e: transaction.rollback() raise e if self.karmaRepo.get_user_avaiable_karma(giver_id, guild_id) <= 0: await ctx.send("You wasted your avaiable karma for this 24 hours.") return if action == "++": self.karmaRepo.give_karma(giver_id, given_id, guild_id) await ctx.send("Karma given!") elif action == "--": self.karmaRepo.take_karma(giver_id, given_id, guild_id) await ctx.send("Karma taken!")