making a bot respond to an image using discord.py(使用 discord.py 让机器人响应图像)
问题描述
使用 discord.py 进行机器人编码的新手.正如标题所示,我希望有人能告诉我如何让机器人响应发送图像的人,无论是从互联网粘贴还是从他们的计算机上传.
New to bot coding using discord.py. As the title suggests, I'm hoping if someone can tell me how to make a bot respond to someone sending an image, whether it's pasted from the internet or uploaded from their computer.
推荐答案
当然,你可以使用 .attachments
@client.event
async def on_message(message):
print(message.attachments)
对于来自外部链接的图片,您可以执行类似的操作
For pictures from outside links you could do something like
pic_ext = ['.jpg','.png','.jpeg']
for ext in pic_ext:
if message.content.endswith(ext):
#do stuff
.attachments 还返回一个包含字典的列表
.attachments also returns a list with a dict inside
[{'width': 1200, 'url': 'https://cdn.discordapp.com/attachments/421005768494678016/486646740993179688/1200px-Greek_uc_Omega.svg.png', 'size': 27042, 'proxy_url': 'https://media.discordapp.net/attachments/421005768494678016/486646740993179688/1200px-Greek_uc_Omega.svg.png', 'id': '486646740993179688', 'height': 1200, 'filename': '1200px-Greek_uc_Omega.svg.png'}]
所以要从中访问任何值(在本例中是它的 url),您可以执行类似
so to access any value (in this case its url) from it you can do something like
message.attachments[0]['url']
字典代码示例
try:
print(message.attachments[0]['url'])
except IndexError:
pass
网址代码示例
pic_ext = ['.jpg','.png','.jpeg']
@bot.event
async def on_message(message):
for ext in pic_ext:
if message.content.endswith(ext):
print("test")
这篇关于使用 discord.py 让机器人响应图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 discord.py 让机器人响应图像
基础教程推荐
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
