From 16921c23ff7186381a16ca0b03c521d56ee4eb21 Mon Sep 17 00:00:00 2001 From: Cara Salter Date: Sun, 16 Jan 2022 00:02:26 -0500 Subject: rroles: Make menu more interactive No longer requires all roles+reactions to be sent in one message, making it easier to not hit the timeout --- src/commands/reactionroles.rs | 186 +++++++++++++++++++++++++++++------------- 1 file changed, 129 insertions(+), 57 deletions(-) (limited to 'src/commands/reactionroles.rs') diff --git a/src/commands/reactionroles.rs b/src/commands/reactionroles.rs index df98ef3..8d448ed 100644 --- a/src/commands/reactionroles.rs +++ b/src/commands/reactionroles.rs @@ -1,8 +1,8 @@ -use std::{time::Duration, str::FromStr}; +use std::{str::FromStr, time::Duration}; -use crate::{Context, Error}; -use poise::serenity_prelude::{self as serenity, ReactionType, Emoji, ArgumentConvert}; +use crate::{models::ReactionRole, Context, Error}; use ::serenity::framework::standard::{Args, Delimiter}; +use poise::serenity_prelude::{self as serenity, ArgumentConvert, Emoji, ReactionType}; #[cfg(debug_assertions)] async fn allowed_to_create_roles(ctx: Context<'_>) -> Result { @@ -23,10 +23,10 @@ async fn allowed_to_create_roles(ctx: Context<'_>) -> Result { let member_permissions = member.permissions(ctx.discord())?; Ok(member_permissions.manage_roles()) + } + } else { + Ok(false) } -} else { - Ok(false) -} } /// Manages reaction role menus @@ -35,10 +35,10 @@ async fn allowed_to_create_roles(ctx: Context<'_>) -> Result { /// - init /// - add /// - remove -#[poise::command(prefix_command, ephemeral, - check="allowed_to_create_roles")] +#[poise::command(prefix_command, ephemeral, check = "allowed_to_create_roles")] pub async fn rroles(ctx: Context<'_>) -> Result<(), Error> { - ctx.say("Maybe you meant to request help for this? Try `/help rroles`").await?; + ctx.say("Maybe you meant to request help for this? Try `/help rroles`") + .await?; Ok(()) } @@ -48,19 +48,21 @@ pub async fn rroles(ctx: Context<'_>) -> Result<(), Error> { /// rroles init <#channel> /// Example: /// rroles init #get-roles -#[poise::command(prefix_command, ephemeral, - check="allowed_to_create_roles")] -pub async fn init(ctx: Context<'_>, - #[description = "The channel to create a new role menu in"] - channel: serenity::ChannelId) -> Result<(), Error> { - let mut rolemenu_msg = channel.send_message(ctx.discord(), |m| { - m.embed(|e| { - e.title("Reaction Role Menu"); - e.description("I haven't been initialized yet! Hold on just a second"); - e - }); - m - }).await?; +#[poise::command(prefix_command, ephemeral, check = "allowed_to_create_roles")] +pub async fn init( + ctx: Context<'_>, + #[description = "The channel to create a new role menu in"] channel: serenity::ChannelId, +) -> Result<(), Error> { + let mut rolemenu_msg = channel + .send_message(ctx.discord(), |m| { + m.embed(|e| { + e.title("Reaction Role Menu"); + e.description("I haven't been initialized yet! Hold on just a second"); + e + }); + m + }) + .await?; let mut menu = ctx.send(|m| { m.embed(|e| { @@ -71,19 +73,27 @@ pub async fn init(ctx: Context<'_>, m }).await?.unwrap().message().await?; - if let Some(title) = ctx.author().clone().await_reply(ctx.discord()).timeout(Duration::from_secs(10)).await { - rolemenu_msg.edit(ctx.discord(), |m| { - m.embed(|e| { - e.title(title.content.clone()); - e - }); - m - }).await?; + if let Some(title) = ctx + .author() + .clone() + .await_reply(ctx.discord()) + .timeout(Duration::from_secs(10)) + .await + { + rolemenu_msg + .edit(ctx.discord(), |m| { + m.embed(|e| { + e.title(title.content.clone()); + e + }); + m + }) + .await?; menu.edit(ctx.discord(), |m| { m.embed(|e| { e.title("Reaction Role Setup"); - e.description(format!("Great! I've set the title of your menu to `{}`.\n\nNext, let's add some roles! Reply to this message with a list of role names and reactions, like this:\n\n:female_sign::She/Her,:male_sign::He/Him", title.content.clone())); + e.description(format!("Great! I've set the title of your menu to `{}`.\n\nNext, let's add some roles! Send the first emoji you want to add.", title.content.clone())); e }); m @@ -94,41 +104,103 @@ pub async fn init(ctx: Context<'_>, } { let pool = ctx.data().pg.lock().unwrap().clone(); - if let Some(roles) = ctx.author().clone().await_reply(ctx.discord()).timeout(Duration::from_secs(30)).await { - let mut args = Args::new(&roles.content, &[Delimiter::Single(',')]); + loop { + if let Some(emoji) = ctx + .author() + .clone() + .await_reply(ctx.discord()) + .timeout(Duration::from_secs(30)) + .await + { + let content = emoji.content.clone(); + if content == "done" { + menu.edit(ctx.discord(), |m| { + m.embed(|e| { + e.title("Reaction Role Setup"); + e.description("Nice work! You're all set up! Use `~rroles add` and `~rroles del` to manage the roles in this menu!"); + e + }); + m + }).await?; + break; + } + let reaction = ReactionType::from_str(&emoji.content)?; - let mut rolelist_formatted = String::from("Choose the appropriate reaction to gain the role!\n"); + menu.edit(ctx.discord(), |m| { + m.embed(|e| { + e.title("Reaction Role Setup"); + e.description(format!("Sounds good! Let's give {} a role, okay? Reply to this message with the name of the role you'd like to assign to this emoji", reaction.clone())); + e + }); + m + }).await?; - for a in args.iter::() { - if let Ok(tuple) = a { - let split_str: Vec<&str> = tuple.split(':').collect(); - let reaction = ReactionType::from_str(split_str[0])?; - let role_name = split_str[1]; - if let Some(role) = ctx.guild().unwrap().role_by_name(role_name) { + if let Some(role) = ctx + .author() + .clone() + .await_reply(ctx.discord()) + .timeout(Duration::from_secs(30)) + .await + { + if let Some(role) = ctx.guild().unwrap().role_by_name(&role.content) { sqlx::query!("INSERT INTO reaction_roles (channel_id, message_id, guild_id, reaction, role_id) VALUES ($1, $2, $3, $4, $5)", rolemenu_msg.channel_id.0.to_string(), rolemenu_msg.id.0.to_string(), ctx.guild_id().unwrap().0.to_string(), reaction.to_string(), role.id.0.to_string()).execute(&pool).await?; rolemenu_msg.react(ctx.discord(), reaction.clone()).await?; - rolelist_formatted.push_str(&format!("{} - <@&{}>\n", get_reactiontype_display(&reaction), role.id.0.clone())); + + menu.edit(ctx.discord(), |m| { + m.embed(|e| { + e.title("Reaction Role Setup"); + e.description(format!("Great! I've added that to the menu.\n\nLet's keep adding roles! Send the next emoji you want to add, or 'done' to finish setup.")); + e + }); + m + }).await?; } else { - ctx.say(format!("Invalid role provided: {}", role_name)).await?; - return Ok(()); + menu.edit(ctx.discord(), |m| { + m.embed(|e| { + e.title("Reaction Role Setup"); + e.description("Whoops! I couldn't find that role! Let's try again! Send the emoji you want to assign to a role"); + e + }); + m + }).await?; + continue; } } + + let reactions = sqlx::query_as!( + ReactionRole, + "SELECT * FROM reaction_roles WHERE message_id=$1", + rolemenu_msg.id.0.to_string() + ) + .fetch_all(&pool) + .await?; + let mut rolelist_formatted = + String::from("Choose the appropriate reaction to gain the role!\n\n"); + for r in reactions { + rolelist_formatted.push_str(&format!("{} - <@&{}>\n", r.reaction, r.role_id)); + } + + let title = rolemenu_msg.clone().embeds[0] + .title + .clone() + .unwrap_or("Reaction Role Menu".to_string()); + rolemenu_msg + .edit(ctx.discord(), |m| { + m.embed(|e| { + e.title(title); + e.description(rolelist_formatted); + e + }); + m + }) + .await?; + } else { + ctx.say("No response within 30 seconds").await?; + return Ok(()); } - let title = rolemenu_msg.clone().embeds[0].title.clone().unwrap_or("Reaction Role Menu".to_string()); - rolemenu_msg.edit(ctx.discord(), |m| { - m.embed(|e| { - e.title(title); - e.description(rolelist_formatted); - e - }); - m - }).await?; - } else { - ctx.say("No response within 30 seconds").await?; - return Ok(()); } } - + Ok(()) } @@ -141,7 +213,7 @@ fn get_reactiontype_display(rt: &ReactionType) -> String { } else { format!("<{}>", id) } - }, + } _ => String::new(), } } -- cgit v1.2.3