Ejemplo n.º 1
0
 async def _add_db_sighting_report(self, ctx, message):
     channel = ctx.channel
     guild = channel.guild
     author = ctx.author
     wild_dict = self.bot.guild_dict[guild.id]['wildreport_dict'][message.id]
     created = round(message.created_at.timestamp())
     __, __ = GuildTable.get_or_create(snowflake=guild.id)
     __, __ = TrainerTable.get_or_create(snowflake=author.id, guild=guild.id)
     report = TrainerReportRelation.create(created=created, trainer=author.id,
                                           location=wild_dict['location_id'], message=message.id)
     try:
         SightingTable.create(trainer_report=report, pokemon=wild_dict['pokemon_id'])
     except Exception as e:
         self.bot.logger.info(f"Failed to create sighting table entry with error: {e}")
Ejemplo n.º 2
0
 async def _add_db_research_report(self, ctx, message):
     channel = ctx.channel
     guild = channel.guild
     author = ctx.author
     quest_dict = self.bot.guild_dict[guild.id]['questreport_dict'][
         message.id]
     created = round(message.created_at.timestamp())
     __, __ = GuildTable.get_or_create(snowflake=guild.id)
     __, __ = TrainerTable.get_or_create(snowflake=author.id,
                                         guild=guild.id)
     report = TrainerReportRelation.create(
         created=created,
         trainer=author.id,
         location=quest_dict['location_id'],
         message=message.id)
     try:
         ResearchTable.create(trainer_report=report,
                              quest=quest_dict['quest_id'],
                              reward=quest_dict['reward'])
     except Exception as e:
         self.bot.logger.info(
             f"Failed to create research table entry with error: {e}")
Ejemplo n.º 3
0
    async def _sub_add(self, ctx, *, content=None):
        """Create a subscription

        **Usage**: `!sub add <type> <target>`
        Kyogre will send you a notification if an event is generated
        matching the details of your subscription.
        
        **Valid types**: `pokemon, raid, research, wild, gym, item, lure`
        **Note**: 'pokemon' includes raid, research, and wild reports"""
        message = ctx.message
        channel = message.channel
        guild = message.guild
        trainer = message.author.id
        error_list = []

        if content is None:
            return await self._guided_subscription(ctx, 'Add')
        content = content.strip().lower()
        if content == 'shiny':
            candidate_list = [('shiny', 'shiny', 'shiny')]
        else:
            error_message = self._get_subscription_command_error(
                content, self.valid_types)
            if error_message:
                response = await message.channel.send(error_message)
                return await utils.sleep_and_cleanup([message, response], 10)

            candidate_list, error_list = await self._parse_subscription_content(
                content, 'add', message)

        existing_list = []
        sub_list = []

        # don't remove. this makes sure the guild and trainer are in the db
        guild_obj, __ = GuildTable.get_or_create(snowflake=guild.id)
        trainer_obj, __ = TrainerTable.get_or_create(snowflake=trainer,
                                                     guild=guild.id)
        if guild_obj is None or trainer_obj is None:
            pass
        s_type = ''
        for sub in candidate_list:
            s_type = sub[0]
            s_target = sub[1]
            s_entry = sub[2]
            if len(sub) > 3:
                spec = sub[3]
                try:
                    result, __ = SubscriptionTable.get_or_create(
                        guild_id=ctx.guild.id,
                        trainer=trainer,
                        type=s_type,
                        target=s_target)
                    current_gym_ids = result.specific
                    split_ids = []
                    if current_gym_ids:
                        current_gym_ids = current_gym_ids.strip('[]')
                        split_id_string = current_gym_ids.split(', ')
                        for s in split_id_string:
                            try:
                                split_ids.append(int(s))
                            except ValueError:
                                pass
                    spec = [int(s) for s in spec]
                    new_ids = set(split_ids + spec)
                    result.specific = list(new_ids)
                    if len(result.specific) > 0:
                        result.save()
                        sub_list.append(s_entry)
                except:
                    error_list.append(s_entry)
            else:
                try:
                    SubscriptionTable.create(guild_id=ctx.guild.id,
                                             trainer=trainer,
                                             type=s_type,
                                             target=s_target)
                    sub_list.append(s_entry)
                except IntegrityError:
                    existing_list.append(s_entry)
                except:
                    error_list.append(s_entry)

        sub_count = len(sub_list)
        existing_count = len(existing_list)
        error_count = len(error_list)

        confirmation_msg = f'{ctx.author.mention}, successfully added {sub_count} new {s_type} subscriptions'
        if sub_count > 0:
            confirmation_msg += '\n**{sub_count} Added:** \n\t{sub_list}'.format(
                sub_count=sub_count, sub_list=',\n\t'.join(sub_list))
        if existing_count > 0:
            confirmation_msg += '\n**{existing_count} Already Existing:** \n\t{existing_list}'\
                .format(existing_count=existing_count, existing_list=', '.join(existing_list))
        if error_count > 0:
            confirmation_msg += '\n**{error_count} Errors:** \n\t{error_list}\n(Check the spelling and try again)'\
                .format(error_count=error_count, error_list=', '.join(error_list))

        await channel.send(content=confirmation_msg)
Ejemplo n.º 4
0
    async def _sub_remove(self, ctx, *, content=None):
        """Remove a subscription

        **Usage**: `!sub remove <type> <target>`
        You will no longer be notified of the specified target for the given event type.
        You must remove subscriptions using the same type with which they were added.
        It may be helpful to do `!sub list` first to see your existing subscriptions.

        You can remove all subscriptions of a type:
        `!sub remove <type> all`

        Or remove all subscriptions:
        `!sub remove all all`

        Or started a guided session with:
        `!sub remove`

        **Valid types**: `pokemon, raid, research, wild, gym, item, lure`
        **Note**: 'pokemon' includes raid, research, and wild reports"""
        message = ctx.message
        channel = message.channel
        guild = message.guild
        trainer = message.author.id

        if content is None:
            return await self._guided_subscription(ctx, 'Remove')
        content = content.strip().lower()
        if content == 'shiny':
            sub_type, target = ['shiny', 'shiny']
        else:
            error_message = self._get_subscription_command_error(
                content, self.valid_types)
            if error_message:
                response = await message.channel.send(error_message)
                return await utils.sleep_and_cleanup([message, response], 10)
            sub_type, target = content.split(' ', 1)

        candidate_list = []
        error_list = []
        not_found_list = []
        remove_list = []

        trainer_query = (TrainerTable.select(
            TrainerTable.snowflake).where((TrainerTable.snowflake == trainer)
                                          & (TrainerTable.guild == guild.id)))

        # check for special cases
        skip_parse = False

        if sub_type == 'all':
            if target == 'all':
                try:
                    remove_count = SubscriptionTable.delete()\
                        .where((SubscriptionTable.trainer << trainer_query) &
                               (SubscriptionTable.guild_id == ctx.guild.id)).execute()
                    message = f'I removed your {remove_count} subscriptions!'
                except:
                    message = 'I was unable to remove your subscriptions!'
                confirmation_msg = f'{message}'
                await channel.send(content=confirmation_msg)
                return
            else:
                target = target.split(',')
                if sub_type == 'pokemon':
                    for name in target:
                        pkmn = Pokemon.get_pokemon(self.bot, name)
                        if pkmn:
                            candidate_list.append(
                                (sub_type, pkmn.name, pkmn.name))
                        else:
                            error_list.append(name)
                if sub_type != "gym":
                    skip_parse = True
        elif target == 'all':
            candidate_list.append((sub_type, target, target))
            skip_parse = True
        elif target == 'shiny':
            candidate_list = [('shiny', 'shiny', 'shiny')]
            sub_type, target = ['shiny', 'shiny']
            skip_parse = True
        if not skip_parse:
            candidate_list, error_list = await self._parse_subscription_content(
                content, 'remove', message)
        remove_count = 0
        s_type = ''
        for sub in candidate_list:
            s_type = sub[0]
            s_target = sub[1]
            s_entry = sub[2]
            if len(sub) > 3:
                spec = sub[3]
                try:
                    result, __ = SubscriptionTable.get_or_create(
                        guild_id=ctx.guild.id,
                        trainer=trainer,
                        type='gym',
                        target=s_target)
                    current_gym_ids = result.specific
                    split_ids = []
                    if current_gym_ids:
                        current_gym_ids = current_gym_ids.strip('[]')
                        split_id_string = current_gym_ids.split(', ')
                        for s in split_id_string:
                            try:
                                split_ids.append(int(s))
                            except ValueError:
                                pass
                    for s in spec:
                        if s in split_ids:
                            remove_count += 1
                            split_ids.remove(s)
                    result.specific = split_ids
                    result.save()
                    remove_list.append(s_entry)
                except:
                    error_list.append(s_entry)
            else:
                try:
                    if s_type == 'all':
                        remove_count += SubscriptionTable.delete().where(
                            (SubscriptionTable.trainer << trainer_query)
                            & (SubscriptionTable.target == s_target)
                            & (SubscriptionTable.guild_id == ctx.guild.id)
                        ).execute()
                    elif s_target == 'all':
                        remove_count += SubscriptionTable.delete().where(
                            (SubscriptionTable.trainer << trainer_query)
                            & (SubscriptionTable.type == s_type)
                            & (SubscriptionTable.guild_id == ctx.guild.id)
                        ).execute()
                    else:
                        remove_count += SubscriptionTable.delete().where(
                            (SubscriptionTable.trainer << trainer_query)
                            & (SubscriptionTable.type == s_type)
                            & (SubscriptionTable.target == s_target)
                            & (SubscriptionTable.guild_id == ctx.guild.id)
                        ).execute()
                    if remove_count > 0:
                        remove_list.append(s_entry)
                    else:
                        not_found_list.append(s_entry)
                except:
                    error_list.append(s_entry)

        not_found_count = len(not_found_list)
        error_count = len(error_list)

        confirmation_msg = f'{ctx.author.mention}, successfully removed {remove_count} {s_type} subscriptions'
        if remove_count > 0:
            confirmation_msg += '\n**{remove_count} Removed:** \n\t{remove_list}'\
                .format(remove_count=remove_count, remove_list=',\n\t'.join(remove_list))
        if not_found_count > 0:
            confirmation_msg += '\n**{not_found_count} Not Found:** \n\t{not_found_list}'\
                .format(not_found_count=not_found_count, not_found_list=', '.join(not_found_list))
        if error_count > 0:
            confirmation_msg += '\n**{error_count} Errors:** \n\t{error_list}\n(Check the spelling and try again)'\
                .format(error_count=error_count, error_list=', '.join(error_list))
        await channel.send(content=confirmation_msg)
Ejemplo n.º 5
0
 def _count_usage(ctx):
     __, __ = GuildTable.get_or_create(snowflake=ctx.guild.id)
     trainer, __ = TrainerTable.get_or_create(snowflake=ctx.author.id,
                                              guild=ctx.guild.id)
     now = round(time.time())
     APIUsageTable.create(trainer=trainer, date=now)