Python 在循环中通过命令发送图像?
因此,我想做的是发送游戏角色的图片(物品、符文等),如果用户在代码中键入角色的名称(?character1),机器人会发送图片。这很容易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
@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+
file=discord.file(f'path/to/character{num}.png')
它们位于同一文件夹中,这就是为什么我感到困惑。我只是好奇-如果你在同一个文件夹中创建一个新文件夹,并将图片放在其中,然后通过该文件夹访问图片,是否会抛出相同的错误?对不起,我是初学者,而且我很差,所以我误读了代码,它正在工作,我只是不明白我应该删除“字符”从f'字符。现在开始工作了,非常感谢!