简体   繁体   English

如何在discord.py中获取和编辑嵌入?

[英]How to fetch and edit embeds in discord.py?

I want the bot to fetch embeds and scroll between them .我希望机器人获取嵌入并在它们之间滚动。 Here is what I tried doing:这是我尝试做的:

    @commands.command()
    async def test(self,ctx):
    
        chan = self.bot.get_channel(746339276983238677)
        message1 = await chan.fetch_message(761267709438328904)
        message2 = await chan.fetch_message(761267744721076265)
        message3 = await chan.fetch_message(761267778639495168)

        page1= message1.embeds[0]
        
        page2= message2.embeds[0]
        
        page3= message3.embeds[0]



        contents = [page1,page2,page3]
        pages = 3
        cur_page = 1
        message = await ctx.send(f"Page {cur_page}/{pages}:\n{contents[cur_page-1]}")

        await message.add_reaction("◀️")
        await message.add_reaction("▶️")

        def check(reaction, user):
            return user == ctx.author and str(reaction.emoji) in ["◀️", "▶️"]
        while True:
            try:
                reaction, user = await self.bot.wait_for("reaction_add", timeout=60, check=check)

                if str(reaction.emoji) == "▶️" and cur_page != pages:
                    cur_page += 1
                    await message.edit(content=f"Page {cur_page}/{pages}:\n{contents[cur_page-1]}")
                    await message.remove_reaction(reaction, user)

                elif str(reaction.emoji) == "◀️" and cur_page > 1:
                    cur_page -= 1
                    await message.edit(content=f"Page {cur_page}/{pages}:\n{contents[cur_page-1]}")
                    await message.remove_reaction(reaction, user)

                else:
                    await message.remove_reaction(reaction, user)
            except asyncio.TimeoutError:
                await message.delete()
                break

It doesn't send the embeds.它不发送嵌入。 it sends this:它发送这个:

在此处输入图片说明

How do I fetch embeds and send them and then how to I edit them?我如何获取嵌入并发送它们,然后如何编辑它们?

In order to send an embed, you're supposed to use为了发送嵌入,你应该使用

await ctx.send(embed=embed_variable)

attaching it to the message.将其附加到消息中。 You're sending the actual embed instance where it expects a string.您正在发送需要字符串的实际嵌入实例。 So in your case, you should use所以在你的情况下,你应该使用

await ctx.send(embed=contents[cur_page-1])

Afterwards, you can edit them like you want to, using the usual embed methods & attributes ( add_field , set_footer , ...).之后,您可以使用通常的嵌入方法和属性( add_fieldset_footer ,...)随意编辑它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM