Make macro for reaction handling

This commit is contained in:
kageru 2021-11-25 22:05:46 +01:00
parent d50733c8e9
commit b329bc7ea7
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2

View File

@ -18,42 +18,30 @@ lazy_static! {
}
macro_rules! role_action {
($reaction: expr, $ctx: expr, $action: ident, $role_id: expr) => {
$reaction
.guild_id
.expect("Configured message id is not in a guild")
.member(&$ctx, $reaction.user_id.unwrap())
.await?
.$action(&$ctx, $role_id)
.await?
($reaction: expr, $ctx: expr, $action: ident) => {
if let ReactionType::Unicode(emoji) = &$reaction.emoji {
if let Some(&role_id) = ROLE_CONFIG
.iter()
.find(|c| $reaction.message_id == c.message_id)
.and_then(|rm| rm.roles.get(emoji))
{
$reaction
.guild_id
.expect("Configured message id is not in a guild")
.member(&$ctx, $reaction.user_id.unwrap())
.await?
.$action(&$ctx, role_id)
.await?
}
}
};
}
pub(crate) async fn reaction_added(ctx: Context, reaction: Reaction) -> serenity::Result<()> {
if let Some(rm) = ROLE_CONFIG
.iter()
.find(|c| reaction.message_id == c.message_id)
{
if let ReactionType::Unicode(emoji) = &reaction.emoji {
if let Some(role_id) = rm.roles.get(emoji) {
role_action!(reaction, ctx, add_role, *role_id);
}
}
}
Ok(())
Ok(role_action!(reaction, ctx, add_role))
}
pub(crate) async fn reaction_removed(ctx: Context, reaction: Reaction) -> serenity::Result<()> {
if let Some(rm) = ROLE_CONFIG
.iter()
.find(|c| reaction.message_id == c.message_id)
{
if let ReactionType::Unicode(emoji) = &reaction.emoji {
if let Some(role_id) = rm.roles.get(emoji) {
role_action!(reaction, ctx, remove_role, *role_id);
}
}
}
Ok(())
Ok(role_action!(reaction, ctx, remove_role))
}
#[derive(Debug, PartialEq, Deserialize)]