用 Python 编写 Discord 机器人 - 如何让机器人重复图像?

时间:2021-01-31 19:00:19

标签: python discord.py

我想让我的机器人重复某人发送的图像(当提到特定短语时)。这是我的代码:

submission_triggers = ['#submission', '#Submission']

@client.event
async def on_message(message):
  if message.author == client.user:
   return
  
  if any(word in msg for word in submission_triggers):
    channel = client.get_channel(800476409587171369)
    image = message.content
    channel.send(file=discord.File(image))

此代码不起作用,我收到此错误: No such file or directory: '#submission' 不太确定如何解决这个问题。我是编程新手,因此不胜感激。

2 个答案:

答案 0 :(得分:2)

您可以使用消息对象的 .attachment 属性来访问所提供的附件。然后,您可以访问 .url 属性并将其发送。

您还错过了 await 上的 channel.send()

submission_triggers = ['#submission', '#Submission']

@client.event
async def on_message(message):
  if message.author == client.user:
   return
  
  if any(word in msg for word in submission_triggers):
    channel = client.get_channel(800476409587171369)
    image_url = message.attachments[0].url # Attachments are stored in a list, so get the first one.
    await channel.send("{url}")

顺便说一句,您可以通过更改 if 语句来摆脱提交触发器列表:

if "#submission" in msg.lower().split():

答案 1 :(得分:1)

message.content 还包含消息的文本部分,#submission。预处理 message.content 以排除文本部分以消除错误。