Defining the argument number in discord.py, sending a picture

Question:

The problem is on the 10th line, (don’t mind the 9th)
I want the discord bot to send back a message with arguments like this …/arg1/arg1.arg2.png after they send in the command !maps arg1 arg2

@bot.command(brief="test")
async def maps(ctx, *args):
    if not args:
        await ctx.channel.send("Nenapsal jsi Rod/ Rod a druh! nVysvětlivka:")
        await ctx.channel.send(
            "https://cdn.discordapp.com/attachments/661985293834125342/808308254081417227/acz_map_command.png"
        )

    else:
        await ctx.channel.send('Mapa výskytu: *{}*'.format(
            ' '.join(args).capitalize()))
        #await ctx.channel.send('https://antmap.coc.tools/images/{0}/{0}.{0}.png'.format(''.join(args).rpartition("")))
        await ctx.channel.send(f'https://ants-api.qwq.xyz/static/antmaps/{0}/{0}.{1}.png')
        await ctx.channel.send('AntWiki: *{}*'.format(
            ' '.join(args).capitalize()))
        await ctx.channel.send(
            'https://antwiki.org/wiki/{}'.format(
                '_'.join(args).capitalize()))

await ctx.channel.send(f'https://ants-api.qwq.xyz/static/antmaps/{0}/{0}.{1}.png')
Won’t send to the channel https://ants-api.qwq.xyz/static/antmaps/Common/Common.ant.png after receiving the command !maps Common ant
It only does send in https://ants-api.qwq.xyz/static/antmaps/0/0.1.png

Asked By: Jakub Šidlík

||

Answers:

I see your issue now. You’re not using f-strings correctly. Replace your send with the below and it should work as expected.

await ctx.channel.send(f'https://ants-api.qwq.xyz/static/antmaps/{args[0]}/{args[0]}.{args[1]}.png')

Inside the { } you just put 0 or 1. Python understands that as literally 0 or 1 – hence the link you’re getting. You need to change it so it’s actually getting the value you want from args.

Answered By: ESloman

A bit different approach I found thanks to ESloman and ChatGPT:

@bot.command(brief="Za map napiš Rod a druh a vyskočí ti mapa výskytu!")
async def map(ctx, Rod: str, druh: str = None):
    if not Rod:
        await ctx.channel.send("Nenapsal jsi __Rod__ nebo __Rod a druh__! nVysvětlivka:")
        await ctx.channel.send(
            "https://cdn.discordapp.com/attachments/661985293834125342/808308254081417227/acz_map_command.png"
        )
    else:
        if druh:
            url = f'https://ants-api.qwq.xyz/static/antmaps/{Rod}/{Rod}.{druh}.png'
            await ctx.channel.send('Mapa výskytu: *{}*'.format(
                ' '.join([Rod, druh]).capitalize()))
            await ctx.send(url)
        else:
            url = f'https://ants-api.qwq.xyz/static/antmaps/{Rod}/{Rod}.png'
            await ctx.channel.send('Mapa výskytu: *{}*'.format(
                Rod.capitalize()))
            await ctx.send(url)
        await ctx.channel.send('AntWiki: *{}*'.format(
            ' '.join([Rod, druh]).capitalize()))
        await ctx.channel.send('https://antwiki.org/wiki/{}'.format(
            '_'.join([Rod, druh]).capitalize()))
    ```
Answered By: Jakub Šidlík
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.