Search code examples
pythondiscorddiscord.pybots

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


I'm creating a command for a discord bot and I want it to be able to send me an animated sprite from the Pokemon Showdown website.

@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)

When I run this command, dex, my bot does not send an embed (await ctx.send(embed=embed) isn't sending an embed). There is no error message. When I try using a command that isn't added to the bot, it responds with a message, "I do not have this command" (which I set it to do with a CommandNotFound error checker). I think the problem is either in the urls or the embed.set_image(url=url).


Solution

  • You are trying to send pokemon, tag, ab, and increment variables even though they are not set when there is no ',' in args, also the urls have to have 'https://' at the front for it to work. Something like this seems to work but then again you need to make sure you cover the case where there is no ',' in 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)
    

    like this