70 %
Chris Biscardi

Automatically Pinning Spotify listening party messages in Discord with Rust and Serenity

Discord has a Spotify integration that allows you to send rich embed messages which allow other people who have connected their Spotify account to listen along with you. The discoverability of these isn't great so in the Party Corgi Network Discord server we've implemented bot functionality to automatically pin these messages to the #listening-party channel as seen below.

a pinned message in discord

We do this using Rust and Serenity. Serenity's EventHandler trait can be used to implement a function that gets called whenever a new message is sent. The key point here is that Messages have kinds, which represent what kind of message it is and that Spotify has it's own MessageActivityKind::LISTEN value to identify relevant messages.

Using this and the useful helpers on Message, we can .pin() the message in question automatically whenever someone posts a listening party.

rust
impl EventHandler for Handler {
fn message(
&self,
_ctx: Context,
_new_message: Message,
) {
if _new_message.channel_id
== CHANNEL__LISTENING_PARTY
{
match &_new_message.activity {
Some(activity) => match activity.kind {
// pin the message if it's a Spotify LISTEN
MessageActivityKind::LISTEN => {
match _new_message.pin(_ctx.http) {
Ok(_) => {}
Err(_) => {}
}
}
_ => (),
},
None => (),
}
}
}
}

We don't return any useful values although we'll add error handling messages as well as adding the event information to our Honeycomb instrumentation in the future.