Add pinned command

This commit is contained in:
Agatha Rose 2020-05-23 19:22:27 +03:00
parent 21b9a1ded7
commit e975b9689a
1 changed files with 45 additions and 1 deletions

View File

@ -50,7 +50,7 @@ impl EventHandler for Handler {
#[group]
#[commands(
init, ping, halt, list_srv, host, ship, bottom_rng, headpat, uwu, gayculator, sausage, help,
embed, define, owo, info, echo, desc
embed, define, owo, info, echo, desc, pinned
)]
struct General;
@ -526,6 +526,11 @@ fn help(ctx: &mut Context, message: &Message) -> CommandResult {
true,
),
("owo!desc", "Display channel's topic", true),
(
"owo!pinned ``[num]``",
"Display channel's Nth pinned message",
true,
),
("owo!ship ``[names]``", "*Shipping intensifies*", true),
("owo!headpat ``name``", "Headpat someone", true),
("owo!owo ``text``", "owoify input text", true),
@ -639,3 +644,42 @@ fn desc(ctx: &mut Context, message: &Message) -> CommandResult {
Ok(())
}
#[command]
fn pinned(ctx: &mut Context, message: &Message, mut args: Args) -> CommandResult {
let pinned = match message.channel_id.pins(&ctx.http) {
Ok(v) => v,
Err(e) => {
return Err(CommandError(
format!("Could not get pinned messages! Error: {}", e).to_string(),
));
}
};
let idx = args.single::<usize>().unwrap_or(0);
if pinned.is_empty() {
return Err(CommandError("No pinned messages found!".to_string()));
}
if idx > pinned.len() - 1 {
return Err(CommandError("Index out of bounds!".to_string()));
}
let _ = message.channel_id.send_message(&ctx.http, |m| {
m.embed(|e| {
e.title(format!("Pinned message #{}", idx))
.description(&pinned[idx].content)
.timestamp(&pinned[idx].timestamp);
e.author(|a| {
//assuming first array element is name and second is icon url
a.name(&pinned[idx].author.name)
.icon_url(&pinned[idx].author.avatar_url().unwrap())
});
if !&pinned[idx].attachments.is_empty() {
e.image(&pinned[idx].attachments[0].url);
}
e
})
});
Ok(())
}