Exemplo n.º 1
0
 async def send_deals_to_channel(self, deals_list: List[Deal],
                                 channel: discord.TextChannel):
     if len(deals_list) == 0:
         return
     try:
         await channel.purge()
         await asyncio.sleep(
             1
         )  # This is due to the Discord sometimes not clearing the channel.
         await channel.send(
             content=
             f"```Last updated: {datetime.now().strftime('%d-%m-%Y %H:%M:%S')}```"
         )
         await channel.send(
             content=f"```Here's a list of {len(deals_list)} new deals!```")
         for deal in deals_list:
             await channel.send(embed=get_embed_from_deal(deal))
         await channel.send(content=f"```That's it for today :(```")
     except discord.errors.NotFound:
         logging.error(
             f'Channel {channel.name} has been deleted while the bot was working on {channel.guild}'
         )
         category, channels = await initialize_channels(channel.guild)
         config = update_guild_config(filename='config.yaml',
                                      guild=channel.guild,
                                      category=category,
                                      channels=channels)
         new_channel_id = config[channel.guild.id]['channels'][channel.name]
         await self.send_deals_to_channel(
             deals_list, self.bot.get_channel(new_channel_id))
 async def random(self, ctx: commands.Context, min_price: int = 0):
     try:
         deal = await get_random_deal(min_price)
     except NoDealsFound:
         await ctx.send(
             content=
             '```Unable to find random deal with provided minimum discount price```'
         )
         return
     await ctx.send(
         content=f"Here's a random deal for you, **{ctx.author.name}**!",
         embed=get_embed_from_deal(deal))
Exemplo n.º 3
0
    async def send_deals_to_channel(self, deals_list: List[Deal],
                                    channel: discord.TextChannel):
        """Method that sends deals to the channel specified.

        :param deals_list: List of Deal dataclass objects.
        :param channel: discord.py Channel class object.
        :return: None
        """
        if len(deals_list) == 0:
            return
        try:
            await channel.purge()
            await asyncio.sleep(
                1
            )  # This is due to the Discord sometimes not clearing the channel.
            await channel.send(
                content=
                f"**Here's a list of {len(deals_list)} new :video_game: deals!**"
            )
            await channel.send(
                content=
                f"```Last updated: {datetime.now().strftime('%d-%m-%Y %H:%M:%S')} UTC```"
            )
            for deal in deals_list:
                await channel.send(embed=get_embed_from_deal(deal))
            await channel.send(content="```That's it for today :(```")
        except discord.errors.NotFound:
            logging.error(
                f'Channel {channel.name} has been deleted while the bot was working on {channel.guild}'
            )
            new_channel = await channel.guild.create_text_channel(
                name=channel.name, category=channel.category)
            await crud.channel.update_by_discord_id(channel.id, {
                'discord_id': new_channel.id,
                'name': channel.name
            })
            await self.send_deals_to_channel(deals_list, new_channel)
    async def flip(self,
                   ctx: commands.Context,
                   min_price: int = 0,
                   max_price: int = 60):
        try:
            deals_list = await get_deals(min_price=min_price,
                                         max_price=max_price,
                                         amount=60)
        except NoDealsFound:
            await ctx.send(
                content=f'```No deals found within specified price range```')
            return
        start_message = await ctx.send(
            content=
            f"```Here's a flipbook of deals for you, {ctx.author.name}!```")
        pages = len(deals_list)
        current_page = 1
        deal = deals_list[0]
        message = await ctx.send(content=f'**Page {current_page}/{pages}**',
                                 embed=get_embed_from_deal(deal))
        await message.add_reaction('◀️')
        await message.add_reaction('▶️')
        active = True

        def check(_reaction, _user):
            return (_user != self.bot.user
                    and str(_reaction.emoji) in ['◀️', '▶️']
                    and _reaction.message == message)

        while active:
            try:
                reaction, user = await self.bot.wait_for('reaction_add',
                                                         timeout=120,
                                                         check=check)

                if user != ctx.author:
                    await message.remove_reaction(reaction, user)
                    continue

                if str(reaction.emoji) == '▶️':
                    if current_page == pages:
                        current_page = 1
                    else:
                        current_page += 1
                    deal = deals_list[current_page - 1]
                    await message.edit(
                        content=f'**Page {current_page}/{pages}**',
                        embed=get_embed_from_deal(deal))
                    await message.remove_reaction(reaction, user)

                if str(reaction.emoji) == '◀️':
                    if current_page == 1:
                        current_page = pages
                    else:
                        current_page -= 1
                    deal = deals_list[current_page - 1]
                    await message.edit(
                        content=f'**Page {current_page}/{pages}**',
                        embed=get_embed_from_deal(deal))
                    await message.remove_reaction(reaction, user)
            except asyncio.TimeoutError:
                await start_message.delete()
                await message.delete()
                active = False