Exemplo n.º 1
0
    async def add_pokemon(self, discord_id, rarity, is_shiny):
        today = datetime.now().date()
        try:
            # Ensure User is in database
            user_id = UserModel.select(UserModel.user_id).where(
                UserModel.discord_id == discord_id).scalar()
            if user_id is None:
                discord_user = await self.bot.fetch_user(discord_id)
                username = f'{discord_user.name}#{discord_user.discriminator}'
                user, created = UserModel.get_or_create(
                    discord_id=discord_user.id, username=username)
                user_id = user.user_id
            # Ensure UserStatModel object exist for user today
            userstat, created = UserStatModel.get_or_create(date=today,
                                                            user_id=user_id)

            # Check if we need to add rarity or shiny
            legendary_count, mythical_count, ultrabeast_count = 0, 0, 0
            if rarity == 'legendary':
                legendary_count = 1
            elif rarity == 'mythical':
                mythical_count = 1
            elif rarity == 'ultra beast':
                ultrabeast_count = 1
            shiny_count = 1 if is_shiny else 0

            (UserStatModel.update(
                catches=UserStatModel.catches + 1,
                legendary=UserStatModel.legendary + legendary_count,
                mythical=UserStatModel.mythical + mythical_count,
                ultrabeast=UserStatModel.ultrabeast + ultrabeast_count,
                shiny=UserStatModel.shiny +
                shiny_count).where((UserStatModel.user_id == user_id)
                                   & (UserStatModel.date == today)).execute())
        except Exception as e:
            logging.critical(
                f'add_pokemon: {e} | discord_id: {discord_id}, rarity: {rarity}, is_shiny: {is_shiny}'
            )