如何使用 discord bot 回复图片
How to reply with an image with discord bot
我是 rust 的新手,作为练习项目,我正在使用 Serenity 构建一个机器人来处理与 Discord 的交互。机器人应该能够回复带有图像的消息。我可以像这样使用 CreateMessage 让机器人 post 频道上的图像:
let f = [(&tokio::fs::File::open("image.png").await?, "image.png")];
return match msg.channel_id.send_message(&context.http, |m| {
m.content(replied_message.author);
m.files(f);
return m;
}).await {
Ok(_) => Ok(()),
Err(why) => Err(CommandError::from(why)),
};
不幸的是,此方法不适用于回复,回复需要实现 std::fmt::Display 的内容。我可以使用 MessageBuilder,但它构造了一个字符串,我不知道如何向其添加图像,除非我添加一个 URL。该图像是一个 image::DynamicImage 实例,至少可以说从另一个服务提供它是不切实际的。
如何使用message.reply_ping(&context.http, &reply)
发送图片?
您可以像在现有代码中一样使用 send_message()
方法,并添加一些小的内容。
CreateMessage
有一个 reference_message()
方法可以用来设置要回复的消息。它有 allowed_mentions()
配置 ping 的方法:
match msg
.channel_id
.send_message(&context.http, |m| {
// Reply to the given message
m.reference_message(&replied_message);
// Ping the replied user
m.allowed_mentions(|am| {
am.replied_user(true);
am
});
// Attach image
m.files(f);
m
})
.await
{
// ...
}
我是 rust 的新手,作为练习项目,我正在使用 Serenity 构建一个机器人来处理与 Discord 的交互。机器人应该能够回复带有图像的消息。我可以像这样使用 CreateMessage 让机器人 post 频道上的图像:
let f = [(&tokio::fs::File::open("image.png").await?, "image.png")];
return match msg.channel_id.send_message(&context.http, |m| {
m.content(replied_message.author);
m.files(f);
return m;
}).await {
Ok(_) => Ok(()),
Err(why) => Err(CommandError::from(why)),
};
不幸的是,此方法不适用于回复,回复需要实现 std::fmt::Display 的内容。我可以使用 MessageBuilder,但它构造了一个字符串,我不知道如何向其添加图像,除非我添加一个 URL。该图像是一个 image::DynamicImage 实例,至少可以说从另一个服务提供它是不切实际的。
如何使用message.reply_ping(&context.http, &reply)
发送图片?
您可以像在现有代码中一样使用 send_message()
方法,并添加一些小的内容。
CreateMessage
有一个 reference_message()
方法可以用来设置要回复的消息。它有 allowed_mentions()
配置 ping 的方法:
match msg
.channel_id
.send_message(&context.http, |m| {
// Reply to the given message
m.reference_message(&replied_message);
// Ping the replied user
m.allowed_mentions(|am| {
am.replied_user(true);
am
});
// Attach image
m.files(f);
m
})
.await
{
// ...
}