繁体   English   中英

在 Python 中编程 Discord 机器人 - 如何让机器人重复图像?

[英]Programming a Discord bot in Python- How do I make the bot repeat an image?

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

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'不太确定如何解决此问题。 我是编程新手,所以任何见解都将不胜感激。

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

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

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():

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM