使用discord.py让机器人响应图像

问题描述 投票:0回答:1

使用discord.py进行bot编码的新手。正如标题所示,我希望有人能告诉我如何让机器人回复发送图像的人,无论是从互联网上粘贴还是从他们的计算机上传。

python bots discord.py
1个回答
2
投票

当然,你可以使用.attachments

@client.event
async def on_message(message):
  print(message.attachments)

对于外部链接的图片,您可以执行类似的操作

  pic_ext = ['.jpg','.png','.jpeg']
  for ext in pic_ext:
    if message.content.endswith(ext):
      #do stuff

.attachments还会返回一个带有dict的列表

[{'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)你可以做类似的事情

message.attachments[0]['url']

dict代码示例

  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")
© www.soinside.com 2019 - 2024. All rights reserved.