Exemplo n.º 1
0
class MayMayMenu(menus.MenuPages, inherit_buttons=False):
    """
    Code used from Rapptz' discord-ext-menus GitHub repository
    Provided by the MIT License
    https://github.com/Rapptz/discord-ext-menus
    Copyright (c) 2015-2020 Danny Y. (Rapptz)
    """
    @menus.button('\U000023ee', position=menus.First(0))
    async def go_to_first_page(self, payload):
        """go to the first page"""
        await self.show_page(0)

    @menus.button('\U000025c0', position=menus.Position(0))
    async def go_to_previous_page(self, payload):
        """go to the previous page"""
        await self.show_checked_page(self.current_page - 1)

    @menus.button('\U000023f9', position=menus.Position(3))
    async def stop_pages(self, payload):
        """stops the pagination session."""
        self.stop()
        await self.message.delete()

    @menus.button('\U000025b6', position=menus.Position(5))
    async def go_to_next_page(self, payload):
        """go to the next page"""
        await self.show_checked_page(self.current_page + 1)

    @menus.button('\U000023ed', position=menus.Position(6))
    async def go_to_last_page(self, payload):
        await self.show_page(self._source.get_max_pages() - 1)

    @menus.button('\U0001f522', position=menus.Position(4))
    async def _1234(self, payload):
        i = await self.ctx.send("What page would you like to go to?")
        msg = await self.ctx.bot.wait_for('message', check=lambda m: m.author == self.ctx.author)
        page = 0
        try:
            page += int(msg.content)
        except ValueError:
            return await self.ctx.send(
                f"**{self.ctx.author.name}**, **{msg.content}** could not be turned into an integer! Please try again!",
                delete_after=3)

        if page > (self._source.get_max_pages()):
            await self.ctx.send(f"There are only **{self._source.get_max_pages()}** pages!", delete_after=3)
        elif page < 1:
            await self.ctx.send(f"There is no **{page}th** page!", delete_after=3)
        else:
            index = page - 1
            await self.show_checked_page(index)
            await i.edit(content=f"Transported to page **{page}**!", delete_after=3)
Exemplo n.º 2
0
    def create_buttons(self, reactions):
        # override if you want button to do something different
        try:
            for index, emoji in enumerate(reactions):
                # each button calls `self.button_response(payload)` so you can do based on that
                def callback(items):
                    async def inside(self, payload):
                        await self.button_response(payload, **items)
                        self.stop()

                    return inside

                self.add_button(
                    menus.Button(emoji,
                                 callback(reactions[emoji]),
                                 position=menus.Position(index)))
        except IndexError:
            pass
class EditMenu(menus.Menu):
    def __init__(self, channel):
        super().__init__(timeout=60,
                         delete_message_after=True,
                         check_embeds=True)
        self.channel = channel
        self.help = False

    def get_settings(self):
        return self.bot.configs.get(str(self.channel.id), {})

    async def set_settings(self, name, value):
        settings = self.get_settings()
        settings[name] = value
        self.bot.configs[str(self.channel.id)] = settings
        await self.bot.configs.save()

    @cached_property
    def main_menu(self):
        embed = discord.Embed(description=f'Editing **{self.channel.name}**',
                              color=self.ctx.guild.me.color)
        embed.set_author(name=self.ctx.author,
                         icon_url=self.ctx.author.avatar_url)
        embed.set_footer(text='Please react with an emoji to continue.')
        entries = []
        for emoji, button in self.buttons.items():
            entries.append(f'{emoji} {inspect.getdoc(button.action)}')
        embed.add_field(name='Options:', value='\n'.join(entries))
        return embed

    async def send_initial_message(self, ctx, channel):
        return await ctx.send(embed=self.main_menu)

    async def wait_for_message(self):
        message = await self.bot.wait_for(
            'message',
            check=lambda m: m.author == self.ctx.author and m.channel == self.
            ctx.channel,
            timeout=60)
        if not self._running:
            raise asyncio.TimeoutError()
        return message

    async def clean_up(self, to_delete):
        if self.ctx.channel.permissions_for(self.ctx.guild.me).manage_messages:
            await self.ctx.channel.delete_messages(to_delete)

    @menus.button(NAME, position=menus.Position(1))
    async def on_name(self, _):
        """Changes the default name"""
        msg = 'Please type the name you want to set.'
        to_delete = []
        while True:
            to_delete.append(await self.ctx.send(msg))
            try:
                message = await self.wait_for_message()
            except asyncio.TimeoutError:
                return
            to_delete.append(message)
            if len(message.content) < 2:
                msg = 'The name cannot be less than 2 characters. Try again.'
            else:
                await self.set_settings('name', message.content)
                to_delete.append(await self.ctx.send('Name has been updated.'))
                await asyncio.sleep(3)
                break
        self.bot.loop.create_task(self.clean_up(to_delete))

    @menus.button(LIMIT, position=menus.Position(2))
    async def on_limit(self, _):
        """Sets the default limit"""
        msg = 'Please type the limit you want to set.'
        to_delete = []
        while True:
            to_delete.append(await self.ctx.send(msg))
            try:
                message = await self.wait_for_message()
            except asyncio.TimeoutError:
                return
            to_delete.append(message)
            try:
                number = int(message.content)
            except ValueError:
                msg = 'This is not a number. Try again.'
            else:
                if not 0 <= number <= 99:
                    msg = 'The limit must be between 0 and 99. Try again'
                else:
                    await self.set_settings('limit', number)
                    to_delete.append(await
                                     self.ctx.send('Limit has been updated.'))
                    await asyncio.sleep(3)
                    break
        self.bot.loop.create_task(self.clean_up(to_delete))

    @menus.button(POSITION, position=menus.Position(3))
    async def on_position(self, _):
        """Changes the position."""
        top = not self.get_settings().get('top', False)
        await self.set_settings('top', top)
        pos = 'top' if top else 'bottom'
        await self.ctx.send(f'New channels are now created at the {pos}.',
                            delete_after=3)

    @menus.button(CATEGORY, position=menus.Position(4))
    async def on_category(self, _):
        """Sets the category"""
        msg = 'Please type the name or id of the category you want to set.'
        to_delete = []
        while True:
            to_delete.append(await self.ctx.send(msg))
            try:
                message = await self.wait_for_message()
            except asyncio.TimeoutError:
                return
            to_delete.append(message)
            try:
                category_id = int(message.content)
            except ValueError:
                for category in self.ctx.guild.categories:
                    if category.name.casefold() == message.content.casefold():
                        category_id = category.id
                        break
                else:
                    msg = f'Category "{message.content}" not found. Try again'
                    continue
            await self.set_settings('category', category_id)
            to_delete.append(await self.ctx.send('Category has been updated.'))
            await asyncio.sleep(3)
            break
        self.bot.loop.create_task(self.clean_up(to_delete))

    @menus.button(HELP, position=menus.Position(7))
    async def on_help(self, _):
        """Shows you information"""
        if self.help:
            await self.message.edit(embed=self.main_menu)
            self.help = False
        else:
            embed = discord.Embed(
                title=f'Information',
                description=
                'In this menu you can change the auto-channel settings.\n'
                'You can use the buttons below to do the following actions:',
                color=self.ctx.guild.me.color)
            embed.set_author(name=self.ctx.author,
                             url=self.ctx.author.avatar_url)
            settings = self.get_settings()
            name = settings.get('name', '@user\'s channel')
            embed.add_field(
                name=f'{NAME} Change the default name ({name})',
                value='Lets you change the default name for created channels.\n'
                'Use @user to reference the user who created the channel.\n'
                'Use @game to reference the game the user is playing.\n'
                'Use @position to reference the amount of created channels in this category.',
                inline=False)
            limit = settings.get('limit', 10)
            embed.add_field(
                name=f'{LIMIT} Set the default limit ({limit})',
                value='Sets the default limit for created channels.\n'
                'Must be between `0` and `99`. Use `0` to remove the user limit.',
                inline=False)
            pos = 'top' if settings.get('top', False) else 'bottom'
            embed.add_field(
                name=f'{POSITION} Change the position ({pos})',
                value='Changes the position channels are created at.\n'
                'This can either be above or below the auto-channel.',
                inline=False)
            try:
                category = self.ctx.guild.get_channel(settings['category'])
            except KeyError:
                category = self.channel.category
            if category is None:
                category = 'No category'
            else:
                category = category.name
            embed.add_field(
                name=f'{CATEGORY} Set the category ({category})',
                value=
                'Lets you change the category new channels are created in.\n',
                inline=False)
            await self.message.edit(embed=embed)
            self.help = True

    @menus.button(EXIT, position=menus.Position(8))
    async def on_exit(self, _):
        """Exits the menu"""
        self.stop()
Exemplo n.º 4
0
class CatchAllMenu(menus.MenuPages, inherit_buttons=False):
    def __init__(self, source, **kwargs):
        super().__init__(source, **kwargs)
        self._info_page = f"Info:\n<:arrow_left:731310897989156884> • Go back one page\n<:arrow_right:731311292346007633> • Go forward one page\n<:last_page_left:731315722554310740> • Go the the first page\n<:last_page_right:731315722986324018> • Go to the last page\n<:stop_button:731316755485425744> • Stop the paginator\n<:1234:731401199797927986> • Go to a page of your choosing\n<:info:731324830724390974> • Brings you here"

    @menus.button('<:last_page_left:731315722554310740>',
                  position=menus.First(0))
    async def go_to_first_page(self, payload):
        """go to the first page"""
        await self.show_page(0)

    @menus.button('<:arrow_left:731310897989156884>',
                  position=menus.Position(0))
    async def go_to_previous_page(self, payload):
        """go to the previous page"""
        await self.show_checked_page(self.current_page - 1)

    @menus.button('<:stop_button:731316755485425744>',
                  position=menus.Position(3))
    async def stop_pages(self, payload):
        """stops the pagination session."""
        self.stop()
        await self.message.delete()

    @menus.button('<:arrow_right:731311292346007633>',
                  position=menus.Position(5))
    async def go_to_next_page(self, payload):
        """go to the next page"""
        await self.show_checked_page(self.current_page + 1)

    @menus.button('<:last_page_right:731315722986324018>',
                  position=menus.Position(6))
    async def go_to_last_page(self, payload):
        await self.show_page(self._source.get_max_pages() - 1)

    @menus.button('<:1234:731401199797927986>', position=menus.Position(4))
    async def _1234(self, payload):
        i = await self.ctx.send("What page would you like to go to?")
        msg = await self.ctx.bot.wait_for(
            'message', check=lambda m: m.author == self.ctx.author)
        page = 0
        try:
            page += int(msg.content)
        except ValueError:
            return await self.ctx.send(
                f"**{self.ctx.author.name}**, **{msg.content}** could not be turned into an integer! Please try again!",
                delete_after=3)

        if page > (self._source.get_max_pages()):
            await self.ctx.send(
                f"There are only **{self._source.get_max_pages()}** pages!",
                delete_after=3)
        elif page < 1:
            await self.ctx.send(f"There is no **{page}th** page!",
                                delete_after=3)
        else:
            index = page - 1
            await self.show_checked_page(index)
            await i.edit(content=f"Transported to page **{page}**!",
                         delete_after=3)

    @menus.button('<:info:731324830724390974>', position=menus.Position(1))
    async def on_info(self, payload):
        await self.message.edit(embed=discord.Embed(
            description=self.info_page, colour=self.ctx.bot.colour))

    @property
    def info_page(self):
        return self._info_page

    def add_info_fields(self, fields: dict):
        for key, value in fields.items():
            self._info_page += f"\n{key} • {value}"