rustcord/src/commands/pinned.rs

74 lines
2.2 KiB
Rust

use serenity::{
framework::standard::{macros::command, Args, CommandError, CommandResult},
model::{channel::Message, id::ChannelId},
prelude::*,
};
// Prints Nth pinned message
#[command]
fn pinned(ctx: &mut Context, message: &Message, mut args: Args) -> CommandResult {
// defaults to latest pinned message if no args are provided
let mut idx = args.single::<usize>().unwrap_or(1);
// Makes pinned messages 1-indexed
if idx != 0 {
idx -= 1;
}
let target_channel = match args.single::<ChannelId>() {
Ok(v) => v,
Err(_) => message.channel_id,
};
let pinned = match target_channel.pins(&ctx.http) {
Ok(v) => v,
Err(e) => {
return Err(CommandError(s!(format!(
"Could not get pinned messages! Error: {}",
e
))));
}
};
if pinned.is_empty() {
return Err(CommandError(s!("No pinned messages found!")));
}
if idx > pinned.len() - 1 {
return Err(CommandError(s!("Index out of bounds!")));
}
let channel = match pinned[idx].channel(&ctx) {
Some(g) => g,
None => return Err(CommandError(s!("Could not get Channel"))),
};
let guild_arc = match channel.guild() {
Some(a) => a,
None => return Err(CommandError(s!("Could not find Guild Arc"))),
};
let guild = guild_arc.read();
let guild_id = guild.guild_id;
let msg_link = format!(
"https://discordapp.com/channels/{}/{}/{}",
guild_id, &pinned[idx].channel_id, &pinned[idx].id
);
let _ = message.channel_id.send_message(&ctx.http, |m| {
m.embed(|e| {
e.title(format!("Pinned message #{}", idx + 1))
.description(&pinned[idx].content)
.field("🔗 link", format!("[original message]({})", msg_link), true)
.timestamp(&pinned[idx].timestamp);
e.author(|a| {
a.name(&pinned[idx].author.name)
.icon_url(&pinned[idx].author.avatar_url().unwrap())
});
e.colour(0xffd1dc);
if !&pinned[idx].attachments.is_empty() {
e.image(&pinned[idx].attachments[0].url);
}
e
})
});
Ok(())
}