70 %
Chris Biscardi

Separating pin and delete message permissions in Discord using Rust and Serenity

Discord doesn't separate the Pin Message and Delete Message permissions, so if like the Party Corgi Network Server you want to give people the ability to pin messages while also not allowing them to delete arbitrary messages from other users, you can do it with a bot command.

Discord manage messages permission pane

In our case, we used Rust and Serenity to implement a !pin <message-id> command as seen here:

Prince pinning a message with the command

Implementing the command requires getting the Message using the ChannelId and MessageId (both as their u64 representations), and then calling .pin() on the message itself. Note that we're parsing into a u64 from the message contents here, so strings and such would cause either the parse or the get_message to fail.

rust
#[command]
fn pin(
ctx: &mut Context,
msg: &Message,
mut args: Args,
) -> CommandResult {
let message_id = args.single::<u64>()?;
let result = ctx
.http
.get_message(*msg.channel_id.as_u64(), message_id)
.and_then(|rmsg| rmsg.pin(&ctx.http));
if let Err(e) = result {
println!("{}", e);
}
Ok(())
}

Note: to get the message id you must right click on a message and click "Copy ID"

Copy ID highlighted in the popover menu

If you don't see the option to Copy ID you have to enable the developer tools in discord. Go to your user settings, then to the Appearance tab, and scroll to the bottom to see this Developer Mode toggle.

Discord developer mode setting