https://linen.dev logo
How to send an embed
# help
n
I've found documentation on how to create a fake one but I don't know how to make a real one. I tried
Copy code
rust
let embed = Embed::fake(|e| {
            e.title("Embed title").description("Rust Bot commands").field(
                "hello",
                "help",
                false,
            )
        });
if msg.content == prefix.to_owned() + "help" {
            if let Err(e) = msg.channel_id.say(&ctx.http, embed).await {
                error!("Error sending message: {:?}", e);
            }
        }
Which as you know creates a fake one and sends it. How do you create a real one?
e
I'm not sure what the difference between a fake and a real one is here tbh? I know of another way to create an embed, but I think functionally it'll be similar to this, though perhaps with more options available.
n
a fake one just sends the json
instead of an embed
e
ah gotcha
n
thank you
Copy code
rust
pub async fn send_embed(&self title: impl ToString, body: impl ToString, colour: Option<Colour>) -> Result<Message, SerenityError> {
    info!("Sending embed `{}` with content `{}`", title.to_string(), body.to_string());
    self.channel_id.send_message(&self.http, |msg| {
        msg.embed(|embed| embed.title(title).description(body).colour(colour.unwrap_or(Colour::PURPLE)))
    }).await
}
gives me this error
Copy code
[{
    "resource": "/d:/Users/Arad/Desktop/rustbot/src/main.rs",
    "owner": "rustc",
    "code": {
        "value": "Click for full compiler diagnostic",
        "target": {
            "$mid": 1,
            "path": "/diagnostic message [4]",
            "scheme": "rust-analyzer-diagnostics-view",
            "query": "4",
            "fragment": "file:///d%3A/Users/Arad/Desktop/rustbot/src/main.rs"
        }
    },
    "severity": 8,
    "message": "`self` parameter is only allowed in associated functions\nassociated functions are those in `impl` or `trait` definitions",
    "source": "rustc",
    "startLineNumber": 13,
    "startColumn": 25,
    "endLineNumber": 13,
    "endColumn": 30
}]
e
yeah I abstracted it into the ReplyContext struct into that file and reorganised some of the methods into associated functions to keep things tidy. You can grab parts of that if you want to, or you can split it back out into its components if that's easier for what you're doing. If I go some commits back I should have just that... one sec.
n
im doing
Copy code
rust
async fn send_embed(
    http: impl AsRef<serenity::http::Http>,
    channel: ChannelId,
    title: impl ToString,
    body: impl ToString,
) -> Result<Message, SerenityError> {
    info!("Sending embed `{}` with content `{}`", title.to_string(), body.to_string());

    channel.send_message(http, |msg| {
        msg.embed(|embed| {
            embed.title(title).description(body).colour(Colour::PURPLE)
        })
    }).await
}

 if msg.content == prefix.to_owned() + "help" {
            send_embed(&ctx.http, msg.channel_id, "title", "body");
        }
and it doesnt send anything
e
It's an async function, you need to .await it
g
You can also ask for Serenity help in the Serenity Discord.
n
I use the Poise framework but it should be somewhat similar. This is how I do it:
Copy code
rust
#[poise::command(slash_command)]
pub async fn suggest_qotd(
    ctx: Context<'_>,
    #[description = "Question"] question: String
) -> Result<(), Error> {
    let channel = ctx.http().get_channel( /* Channel id */ ).await.unwrap();

    if let Err(e) = channel.id().send_message(ctx.http(), |msg|{
        msg.embed(|embed|{
            embed.title("QOTD Suggestion")
            .field("Question:", question, false)
            .thumbnail("https://i.imgur.com/7tyrmrL.png")
            .footer(|f|{
                f.text(format!("Suggested by {x}#{y}", x = ctx.author().name, y = ctx.author().discriminator))
                .icon_url(ctx.author().avatar_url().unwrap().to_string())
            })
        })
    }).await{
        // Handle error
    } else {
        // Handle error
    }
    Ok(())
}