Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discord.ext.pages Sending local images in embed within paginator #1011

Closed
Vaporik opened this issue Feb 13, 2022 · 1 comment
Closed

Discord.ext.pages Sending local images in embed within paginator #1011

Vaporik opened this issue Feb 13, 2022 · 1 comment
Labels
feature request New feature request

Comments

@Vaporik
Copy link

Vaporik commented Feb 13, 2022

Summary

discord.ext.page : Being able to send local image in an embed from a paginator

What is the feature request for?

discord.ext.pages

The Problem

I'm trying to send a local image from my computer in an embed stored in a page from the paginator. 
I've search through the entire discord.ext.pages docs and didn't even find the word "file" 
The problem is I need to send multiple pictures, in one page (in two embeds of course), and also multiple pages with again multiples embeds and pages.

IF THIS IS ALREADY POSSIBLE PLEASE TEL ME SINCE I DON'T WANT TO BOTHER ANYONE

The Ideal Solution

The ideal solution would be to be able to send a picture in a normal embed type of way :

embed =discord.Embed(title="Avatar", description="Avatar n°0", color=0x2bff00)
file = discord.File("data/cards/Avatar.jpg", filename="Avatar.jpg") 
embed.set_image(url=f"attachment://Avatar.jpg")    
await interaction.response.send_message(embed=embed, file=file)

Or if I have multiple images :

embed =discord.Embed(title="Avatar", description="Avatar n°0", color=0x2bff00)
files= [discord.File("data/cards/Avatar.jpg", filename="Avatar.jpg"), discord.File("data/cards/Avatar.jpg", filename="Avatar.jpg")]
embed.set_image(url=f"attachment://Avatar.jpg")    
await interaction.response.send_message(embed=embed, files=files)

The Current Solution

So I thought that by adding manually the file proprety into the respond function I would be able to solve the problem easily (Keep in mind I'm still in High school I'm not a 10X dev 😂)
I put the entire code here since you can copy it to see what it looks like ^^

from discord.file import File


    async def respond(
        self,
        interaction: discord.Interaction,
        ephemeral: bool = False,
        target: Optional[discord.abc.Messageable] = None,
        target_message: str = "Paginator sent!",
        file: Optional[File] = None
    ) -> Union[discord.Message, discord.WebhookMessage]:
        """Sends an interaction response or followup with the paginated items.
        """

        if not isinstance(interaction, discord.Interaction):
            raise TypeError(f"expected Interaction not {interaction.__class__!r}")

        if target is not None and not isinstance(target, discord.abc.Messageable):
            raise TypeError(f"expected abc.Messageable not {target.__class__!r}")

        self.update_buttons()

        page = self.pages[self.current_page]
        page = self.get_page_content(page)

        self.user = interaction.user
        if target:
            if file is None: # I added this to check for the file and then simply copied and pasted normal thing and added file since send_message supports it.
                await interaction.response.send_message(target_message, ephemeral=ephemeral)
            else : 
                await interaction.response.send_message(target_message, file=file, ephemeral=ephemeral)
            self.message = await target.send(
                content=page if isinstance(page, str) else None,
                embeds=[] if isinstance(page, str) else page,
                view=self,
            )
        else:
            if interaction.response.is_done():
                msg = await interaction.followup.send(
                    content=page if isinstance(page, str) else None,
                    embeds=[] if isinstance(page, str) else page,
                    view=self,
                    ephemeral=ephemeral,
                )

            else:
                if File is None :# same thing here as the change I made  
                    msg = await interaction.response.send_message(
                        content=page if isinstance(page, str) else None,
                        embeds=[] if isinstance(page, str) else page,
                        view=self,
                        ephemeral=ephemeral,
                    )
                else : 
                    msg = await interaction.response.send_message(
                        content=page if isinstance(page, str) else None,
                        embeds=[] if isinstance(page, str) else page,
                        view=self, file=file,
                        ephemeral=ephemeral
                    )
            if isinstance(msg, (discord.WebhookMessage, discord.Message)):
                self.message = msg
            elif isinstance(msg, discord.Interaction):
                self.message = await msg.original_message()

        return self.message

Heres my code for my paginator and my pages.
But heres the problem ... it puts the image in all the pages, whether I mentioned in or not .....
So I'm putting the result :
Capture d’écran 2022-02-13 221423
Capture d’écran 2022-02-13 221455

import asyncio

import discord
from discord.commands import SlashCommandGroup
from discord.ext import commands, pages


class PageTest(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.pages = [
            [
                discord.Embed(title=f"Vos stats :", description="En ici vous trouverais vos points de vie et autres statistiques ^^", color=0x2bff00),
                discord.Embed(title=f"Votre deck", description="Appuyer le bouton dessous la carte souhaitée, ensuite il te suffira dans la page suivante de cliquer sur la case désirée !", color=0x2bff00),
            ],
            discord.Embed(title=f"Le plateau de jeux : ", description=f"Désormais il te suffit de cliquer sur la case de ton choix !"),
            discord.Embed(title=f"Le plateau de jeux : ", description=f"Désormais il te suffit de cliquer sur la case de ton choix !")            
        ]
        self.pages[0][1].set_image(
            url=f"attachment://vierge_deck.jpg"
        )
        self.pages[2].set_image(
            url=f"attachment://avatar.png"
        )
    def get_pages(self):
        return self.pages
    
    pagetest = SlashCommandGroup("pagetest", "Commands for testing ext.pages")
   # These examples use a Slash Command Group in a cog for better organization - it's not required for using ext.pages.

    @pagetest.command(name="custom_buttons", guild_ids=[guild_id])
    async def pagetest_custom_buttons(self, ctx: discord.ApplicationContext):
        """Demonstrates adding buttons to the paginator when the default buttons are not used."""
        paginator = pages.Paginator(
            pages=self.get_pages(),
            use_default_buttons=False,
            loop_pages=False,
            show_disabled=False,	
            custom_view=deck()
        )
        paginator.add_button(
            pages.PaginatorButton(
                "prev", label="<", style=discord.ButtonStyle.green, loop_label="lst"
            )
        )
        paginator.add_button(
            pages.PaginatorButton(
                "page_indicator", style=discord.ButtonStyle.gray, disabled=True
            )
        )
        paginator.add_button(
            pages.PaginatorButton(
                "next", style=discord.ButtonStyle.green, loop_label="fst"
            )
        )
        paginator.add_button(
            pages.PaginatorButton(
                "J'ai fini pour ce tour", style=discord.ButtonStyle.red
            )
        )
        await paginator.respond(ctx.interaction, ephemeral=False, files=[discord.File(f"data/avatars/avatar.png", filename=f"avatar.png"),discord.File(f"data/cards/avatar_deck.jpg", filename=f"avatar_deck.jpg")])


def setup(client):
    client.add_cog(PageTest(client))  

Additional Context

To give you a little context, I'm trying to create a card game in Discord and the pages of discord.ext.pages would allow me to make the game's gameplay more interesting ^^
Thanks in advance if you help me.

@Vaporik Vaporik added the feature request New feature request label Feb 13, 2022
@krittick
Copy link
Contributor

This appears to be a duplicate of #692.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature request
Projects
None yet
Development

No branches or pull requests

2 participants