我可以使用 f 字符串为我的 discord.py 机器人的嵌入图像设计 url 吗?

Can I use an f string to design a url for my discord.py bot's embeds' images?

我正在为 discord 机器人创建一个命令,我希望它能够从 Pokemon Showdown 网站向我发送一个动画精灵。

@client.command()
async def dex(ctx, args):
  args_l = args.lower()
  if "," in args_l:
    args_split = args_l.split()
    pokemon = args_split[1]

    url = "https://media2.giphy.com/media/3oEjI6SIIHBdRxXI40/200.gif"
    tag = args_split[0]

    if tag == "shiny":
      url = f"play.pokemonshowdown.com/sprites/ani-shiny/{pokemon}.gif"
    elif tag == "mega":
      url = f"play.pokemonshowdown.com/sprites/xyani/{pokemon}-mega.gif"
  else:
    url = f"play.pokemonshowdown.com/sprites/xyani/{pokemon}.gif"
  
  embed = discord.Embed(title="POKEDEX", color=c, timestamp=datetime.datetime.now())
  embed.set_footer(text="Pokedex")
  embed.description = f"Pokemon: {pokemon}\n\nApplicable Tags: {tag}\n\nThis auction has increments of {increment}\n\nAutobuy: {ab}"
  embed.set_image(url=url)
  await ctx.send(embed=embed)

当我 运行 这个命令时,dex,我的机器人不发送嵌入(await ctx.send(embed=embed) 不发送嵌入)。没有错误信息。当我尝试使用未添加到机器人的命令时,它会回复一条消息,“我没有此命令”(我将其设置为使用 CommandNotFound 错误检查器)。 我认为问题出在网址或 embed.set_image(url=url).

如果没有错误信息,很难确定,但看起来这是你的问题:

如果您的命令中没有 ,,当您尝试在 url = f"play.pokemonshowdown.com/sprites/xyani/{pokemon}.gif"

中使用它时,pokemon 变量未设置

您似乎需要将 else 块修改为如下内容:

else:
    pokemon = args_l
    url = f"play.pokemonshowdown.com/sprites/xyani/{pokemon}.gif"

您还需要在函数的前面指定要拆分的字符,因为 str.split() 没有任何参数拆分为空格而不是逗号,这似乎是您想要的:

args_split = args_l.split(",")

您正在尝试发送 pokemon、tag、ab 和 increment 变量,即使在 args 中没有 ',' 时它们没有设置,而且 url 必须在前面有 'https://'它工作。这样的事情似乎可行,但你又需要确保涵盖 args.

中没有 ',' 的情况
@client.command()
async def dex(ctx, args):
    pokemon = args
    args_l = args.lower()
    if "," in args_l:
        args_split = args_l.split(',')
        pokemon = args_split[1]

        url = "media2.giphy.com/media/3oEjI6SIIHBdRxXI40/200.gif"
        tag = args_split[0]

        if tag == "shiny":
            url = f"play.pokemonshowdown.com/sprites/ani-shiny/{pokemon}.gif"
        elif tag == "mega":
            url = f"play.pokemonshowdown.com/sprites/xyani/{pokemon}-mega.gif"
    else:
        url = f"play.pokemonshowdown.com/sprites/xyani/{pokemon}.gif"
    
    embed = discord.Embed(
        title="POKEDEX", color=discord.Colour.random(), timestamp=datetime.datetime.now())
    embed.set_footer(text="Pokedex")
    embed.description = f"Pokemon: {pokemon}\n\nApplicable Tags: {tag}\n\nThis auction has increments of \n\nAutobuy: "
    embed.set_image(url="https://"+url)
    await ctx.send(embed=embed)