set_welcome: wire up role interaction
This commit is contained in:
parent
a2106caf22
commit
9dfc3b21ff
2 changed files with 52 additions and 0 deletions
45
src/handlers/event/give_role.rs
Normal file
45
src/handlers/event/give_role.rs
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
use eyre::Result;
|
||||||
|
use log::debug;
|
||||||
|
use poise::serenity_prelude::{
|
||||||
|
ComponentInteraction, Context, CreateEmbed, CreateInteractionResponseFollowup, RoleId,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub async fn handle(ctx: &Context, component_interaction: &ComponentInteraction) -> Result<()> {
|
||||||
|
let Some(guild_id) = component_interaction.guild_id else {
|
||||||
|
debug!("Ignoring component interaction not from guild!");
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
|
||||||
|
let Ok(role_id) = RoleId::from_str(&component_interaction.data.custom_id) else {
|
||||||
|
debug!("Ignoring component interaction that doesn't contain a role as it's ID");
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
|
||||||
|
component_interaction.defer_ephemeral(ctx).await?;
|
||||||
|
|
||||||
|
let mut followup = CreateInteractionResponseFollowup::new().ephemeral(true);
|
||||||
|
if let Some(role) = guild_id.roles(ctx).await?.get(&role_id) {
|
||||||
|
let guild_member = guild_id.member(ctx, component_interaction.user.id).await?;
|
||||||
|
|
||||||
|
let mut embed = CreateEmbed::new();
|
||||||
|
if guild_member.roles.contains(&role_id) {
|
||||||
|
guild_member.remove_role(ctx, role_id).await?;
|
||||||
|
embed = embed.description(format!("❌ Removed `{}`", role.name));
|
||||||
|
} else {
|
||||||
|
guild_member.add_role(ctx, role_id).await?;
|
||||||
|
embed = embed.description(format!("✅ Added `{}`", role.name));
|
||||||
|
}
|
||||||
|
|
||||||
|
followup = followup.add_embed(embed);
|
||||||
|
} else {
|
||||||
|
followup = followup.content(format!(
|
||||||
|
"Role ID {role_id} doesn't seem to exist. Please let the moderators know!"
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
component_interaction.create_followup(ctx, followup).await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
|
@ -9,6 +9,7 @@ mod analyze_logs;
|
||||||
mod delete_on_reaction;
|
mod delete_on_reaction;
|
||||||
mod eta;
|
mod eta;
|
||||||
mod expand_link;
|
mod expand_link;
|
||||||
|
mod give_role;
|
||||||
pub mod pluralkit;
|
pub mod pluralkit;
|
||||||
mod support_onboard;
|
mod support_onboard;
|
||||||
|
|
||||||
|
@ -29,6 +30,12 @@ pub async fn handle(
|
||||||
ctx.set_presence(Some(activity), OnlineStatus::Online);
|
ctx.set_presence(Some(activity), OnlineStatus::Online);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FullEvent::InteractionCreate { interaction } => {
|
||||||
|
if let Some(component_interaction) = interaction.as_message_component() {
|
||||||
|
give_role::handle(ctx, component_interaction).await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
FullEvent::Message { new_message } => {
|
FullEvent::Message { new_message } => {
|
||||||
trace!("Recieved message {}", new_message.content);
|
trace!("Recieved message {}", new_message.content);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue