Bot上传图片到执行命令的频道
Bot upload picture to the channel that the command was executed
是的,我已阅读 this 多遍。我是 Python 和 Discord 机器人编程的新手,我搞不懂这个。命令是 n!cat(n! 是前缀),它只是应该在执行命令的频道上随机发送一张猫的图片。这看起来很简单,但我无法弄清楚。这是我的代码:
@client.command()
async def cat():
cat_pic = random.randint(1,3)
if cat_pic == 1:
await client.send_file(channel, "cat1.jpg")
elif cat_pic == 2:
await client.send_file(channel, "cat2.jpg")
elif cat_pic == 3:
await client.send_file(channel, "cat3.jpg")
如您所见,我没有定义 channel
,因为我不知道如何定位执行命令的通道。有任何想法吗?谢谢!
@client.command(pass_context=True)
async def cat(ctx):
cat_pic = random.randint(1,3)
if cat_pic == 1:
await client.send_file(ctx.message.channel, "cat1.jpg")
elif cat_pic == 2:
await client.send_file(ctx.message.channel, "cat2.jpg")
elif cat_pic == 3:
await client.send_file(ctx.message.channel, "cat3.jpg")
装饰器中的pass_context传递消息的"context",也就是参数中的ctx
。您可以使用它来将其发送到频道。
是的,我已阅读 this 多遍。我是 Python 和 Discord 机器人编程的新手,我搞不懂这个。命令是 n!cat(n! 是前缀),它只是应该在执行命令的频道上随机发送一张猫的图片。这看起来很简单,但我无法弄清楚。这是我的代码:
@client.command()
async def cat():
cat_pic = random.randint(1,3)
if cat_pic == 1:
await client.send_file(channel, "cat1.jpg")
elif cat_pic == 2:
await client.send_file(channel, "cat2.jpg")
elif cat_pic == 3:
await client.send_file(channel, "cat3.jpg")
如您所见,我没有定义 channel
,因为我不知道如何定位执行命令的通道。有任何想法吗?谢谢!
@client.command(pass_context=True)
async def cat(ctx):
cat_pic = random.randint(1,3)
if cat_pic == 1:
await client.send_file(ctx.message.channel, "cat1.jpg")
elif cat_pic == 2:
await client.send_file(ctx.message.channel, "cat2.jpg")
elif cat_pic == 3:
await client.send_file(ctx.message.channel, "cat3.jpg")
装饰器中的pass_context传递消息的"context",也就是参数中的ctx
。您可以使用它来将其发送到频道。