使机器人使用discord.py响应图像 [英] making a bot respond to an image using discord.py

查看:68
本文介绍了使机器人使用discord.py响应图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用discord.py进行机器人编码的新手.就像标题所暗示的那样,我希望有人能告诉我如何使漫游器响应某人发送的图像,无论该图像是从Internet粘贴还是从他们的计算机上传的.

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还会返回一个列表,其中带有dict

.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响应图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆