【问题标题】:How do you send more than one embed per Interaction in Discord.py?在 Discord.py 中如何为每次交互发送多个嵌入?
【发布时间】:2023-01-07 10:48:26
【问题描述】:

我想做一个 Discord 交互,发送图片的频率与你在“多少”中所说的一样多,但使用我当前的代码,它发送 1 个嵌入图片,其余的不发送图片。如何解决这个问题?

@tree.command(name='embed', description='embed')
async def embed(interaction: discord.Interaction, seeable: bool, howmany: typing.Optional[int]):
                    embed = discord.Embed(title="Here is a title", color=0xff8c00)
                    file = discord.File(f"[file path]", filename="image.png")
                    embed.set_image(url="attachment://image.png")
                    if seeable == True:
                        await interaction.response.send_message(file=file, embed=embed)
                        if howmany >= 2:
                            for i in range(howmany-1):
                                await interaction.followup.send(file=file, embed=embed)
                    if seeable == False:
                        await interaction.response.send_message(file=file, embed=embed, ephemeral=True)
                        if howmany >= 2:
                            for i in range(howmany-1):
                                await interaction.followup.send(file=file, embed=embed, ephemeral=True)

它在没有交互的情况下已经工作得很好,就像旧的前缀系统一样。 如果您删除所有 # 它将从文件路径上传文件。否则它将显示来自网站的图片:

if message.content.startswith('+image'):
    count = 0
    args = message.content.split(' ')
    if len(args) < 2:
        count = 1
    else:
        if args[1].isdigit():
            count = int(args[1])
        else:
            await message.channel.send("How many should it be?")
    for i in range(count):
        random = random.randint(1,68)
        embed = discord.Embed(title="Title", color=0xff8c00)
        embed.set_image(url=f"https://www.awebsite/pic{random}.png")
        #file = discord.File(f"C:a/file/path/pic{random}.png", filename="image.png")
        #embed.set_image(url="attachment://image.png")
        #await message.channel.send(file=file, embed=embed)
        await message.channel.send(embed=embed)

【问题讨论】:

    标签: python discord discord.py embed discord-interactions


    【解决方案1】:

    仔细查看send_message的文档:https://discordpy.readthedocs.io/en/stable/interactions/api.html?highlight=send_message#discord.InteractionResponse.send_message

    参数:

    • embeds (List[Embed]) – 与内容一起发送的嵌入列表。最多 10 个。这不能与嵌入参数混合使用。

    • embed (Embed) – 要发送的内容的丰富嵌入。这不能与 embeds 参数混合使用。

    换句话说:如果您想发送多个嵌入,请使用 embeds-kwarg 而不是 embed,并传入您要发送的嵌入列表。

    ...send_message(..., embeds=[embed1, embed2, embed3])
    

    您可以在发送常规消息(而不是回复交互)时做同样的事情。

    【讨论】:

      【解决方案2】:

      遗憾的是社区还没有回应,所以我自己修复了这个错误。我知道这不是最漂亮的代码,所以如果您有更漂亮的方法,请告诉我。

      @tree.command(name='embed', description='embed')
      async def embed(interaction: discord.Interaction, seeable: bool, menge: typing.Optional[int]):
              if seeable == True:
                  zufall = random.randint(1, 68)
                  embed = discord.Embed(title="embed", color=0xff8c00)
                  file = discord.File(f"C:/file/path/pic{zufall}.png", filename="image.png")
                  embed.set_image(url="attachment://image.png")
                  await interaction.response.send_message(file=file, embed=embed)
                  
                  if menge >= 2:
                          for i in range(menge - 1):
                          zufall = random.randint(1, 68)
                          embed = discord.Embed(title="embed", color=0xff8c00)
                          file = discord.File(f"C:/file/path/pic{zufall}.png", filename="image.png")
                          embed.set_image(url="attachment://image.png")
                          await interaction.response.send_message(file=file, embed=embed)
      
              if seeable == False:
                  zufall = random.randint(1, 68)
                  embed = discord.Embed(title="embed", color=0xff8c00)
                  file = discord.File(f"C:/file/path/pic{zufall}.png", filename="image.png")
                  embed.set_image(url="attachment://image.png")
                  await interaction.response.send_message(file=file, embed=embed, ephemeral=True)
                  #await interaction.response.send_message(embed=embed)
      
                  if menge >= 2:
                          for i in range(menge - 1):
                          zufall = random.randint(1, 68)
                          embed = discord.Embed(title="embed", color=0xff8c00)
                          file = discord.File(f"C:/file/path/pic{zufall}.png", filename="image.png")
                          embed.set_image(url="attachment://image.png")
                          await interaction.response.send_message(file=file, embed=embed, ephemeral=True)
      

      我希望我能帮助别人。 Ihr seids soiche Heisln。

      【讨论】:

        【解决方案3】:

        你可以删除“if seeable”条件并将 seeable 设置为 ephemeral 像这样 :

            @tree.command(name='embed', description='embed')
            async def embed(interaction: discord.Interaction, seeable: bool, menge: typing.Optional[int]):
                    embed = discord.Embed(title="Here is a title", color=0xff8c00)
                    file = discord.File(f"[file path]", filename="image.png")
                    embed.set_image(url="attachment://image.png")
            
                    await interaction.response.send_message(file=file, embed=embed, ephemeral=seeable)
                    if menge >= 2:
                        for i in range(menge - 1):
                            await interaction.followup.send(file=file, embed=embed, ephemeral=seeable)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-07-14
          • 2020-04-21
          • 2021-02-05
          • 1970-01-01
          • 1970-01-01
          • 2023-03-09
          • 2019-04-14
          • 1970-01-01
          相关资源
          最近更新 更多