Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在循环中通过命令发送图像?_Python_Discord_Discord.py - Fatal编程技术网

Python 在循环中通过命令发送图像?

Python 在循环中通过命令发送图像?,python,discord,discord.py,Python,Discord,Discord.py,因此,我想做的是发送游戏角色的图片(物品、符文等),如果用户在代码中键入角色的名称(?character1),机器人会发送图片。这很容易 @client.command() async def character1(ctx): await ctx.send(file=discord.File('character1.png')) @client.command() async def character2(ctx): await ctx.send(file=discord.Fi

因此,我想做的是发送游戏角色的图片(物品、符文等),如果用户在代码中键入角色的名称(?character1),机器人会发送图片。这很容易

@client.command()
async def character1(ctx):
    await ctx.send(file=discord.File('character1.png'))

@client.command()
async def character2(ctx):
    await ctx.send(file=discord.File('character2.png'))

@client.command
async def character3(ctx):
    await ctx.send(file=discord.File('character3.png'))

但是有很多字符,我想知道是否可以使用一种方法来节省一些时间,该方法查看用户发送的命令,检查是否有带有命令名称的图片,然后发送。因为与我的示例一样,除了.png扩展名外,命令和图片的名称都是相同的。

您可以这样传入一个参数:

@client.command()
异步定义字符(ctx,num):
等待ctx.send(file=discord.file(f'character{num}.png'))
这意味着命令格式将是,例如:
?字符1


参考文献:

  • -Python 3.6+

感谢您的快速回答,使用此代码,我遇到了以下错误:discord.ext.commands.errors.CommandInvokeError:Command引发了异常:FileNotFoundError:[Errno 2]没有这样的文件或目录:“character1.png”@y\os图片在哪里?如果它们与bot的文件位于同一文件夹中,那么应该可以工作,否则您可以获取它们的相对路径,例如
file=discord.file(f'path/to/character{num}.png')
它们位于同一文件夹中,这就是为什么我感到困惑。我只是好奇-如果你在同一个文件夹中创建一个新文件夹,并将图片放在其中,然后通过该文件夹访问图片,是否会抛出相同的错误?对不起,我是初学者,而且我很差,所以我误读了代码,它正在工作,我只是不明白我应该删除“字符”从f'字符。现在开始工作了,非常感谢!