我正在研究一个不和谐机器人,它处理直接消息并将消息发布到我所在的不和谐服务器中的频道。这是按预期工作,但我想知道是否有任何方式发送图像,它得到通过直接消息,以及?
我正在使用Python来编写我的bot
# Post the message into the channel
await server_channel.send(f'**Complaint about the following**n{message.content}')
目前它只能输出文本,当图像发送{消息。Content}返回空白
我甚至不知道从哪里开始。机器人目前能够在特定频道发布直接消息(DM)内容,并回复DM确认其已发布。
编辑:我已经能够用下面的代码
来解决这个问题# Get the server channel you want to send the message to
server_channel = client.get_channel(int(CHANNEL_ID))
if message.attachments:
# Get the image file
image_file = message.attachments[0]
# Read the image file content as bytes
image_bytes = await image_file.read()
# Send the image to the server channel
await server_channel.send(file=discord.File(fp=BytesIO(image_bytes), filename=image_file.filename))
您可以使用discord.Message.attachments
,它返回附件列表,您可以循环并获得discord.Attachment
属性(如URL)。之后,你可以简单地发送URL作为内容,或者你可以发送discord.File
使用请求和:下面是一个例子。