this repo has no description
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: color-me command (#19)

+122
+120
src/commands/user/color_me.rs
··· 1 + use crate::types::Context; 2 + use color_eyre::eyre::Result; 3 + use poise::CreateReply; 4 + use poise::serenity_prelude::{Colour, EditRole}; 5 + 6 + /// Change your display color or remove your color role. 7 + #[poise::command(slash_command)] 8 + pub async fn color_me( 9 + ctx: Context<'_>, 10 + #[description = "Hex color code (e.g., #FF5733 or FF5733)"] color: Option<String>, 11 + ) -> Result<()> { 12 + let guild_id = match ctx.guild_id() { 13 + Some(id) => id, 14 + None => { 15 + ctx.send( 16 + CreateReply::default() 17 + .content("This command can only be used in a server.") 18 + .ephemeral(true), 19 + ) 20 + .await?; 21 + return Ok(()); 22 + } 23 + }; 24 + 25 + let member = guild_id.member(ctx.http(), ctx.author().id).await?; 26 + let username = &ctx.author().name; 27 + 28 + // lookup if user already has a role with their username 29 + let existing_role = guild_id 30 + .roles(ctx.http()) 31 + .await? 32 + .into_iter() 33 + .find(|(_, role)| role.name == *username); 34 + 35 + match color { 36 + Some(color_str) => { 37 + // parse the hex color 38 + let color_str = color_str.trim_start_matches('#'); 39 + let color_value = match u32::from_str_radix(color_str, 16) { 40 + Ok(val) => val, 41 + Err(_) => { 42 + ctx.send( 43 + CreateReply::default() 44 + .content("Invalid hex color! Please provide a valid hex color code (e.g., #FF5733 or FF5733).") 45 + .ephemeral(true), 46 + ) 47 + .await?; 48 + return Ok(()); 49 + } 50 + }; 51 + 52 + let colour = Colour::new(color_value); 53 + 54 + match existing_role { 55 + Some((role_id, _)) => { 56 + // update existing role 57 + guild_id 58 + .edit_role(ctx.http(), role_id, EditRole::new().colour(colour.0)) 59 + .await?; 60 + ctx.send( 61 + CreateReply::default() 62 + .content(format!("Updated your color to `#{color_str}`! <3")) 63 + .ephemeral(true), 64 + ) 65 + .await?; 66 + } 67 + None => { 68 + // or else create new role 69 + let new_role = guild_id 70 + .create_role( 71 + ctx.http(), 72 + EditRole::new() 73 + .name(username) 74 + .colour(colour.0) 75 + .hoist(false) 76 + .mentionable(false), 77 + ) 78 + .await?; 79 + 80 + member.add_role(ctx.http(), new_role.id).await?; 81 + 82 + ctx.send( 83 + CreateReply::default() 84 + .content(format!( 85 + "Created a new role and set your color to `#{color_str}`! <3" 86 + )) 87 + .ephemeral(true), 88 + ) 89 + .await?; 90 + } 91 + } 92 + } 93 + None => { 94 + // remove the role if it exists 95 + match existing_role { 96 + Some((role_id, _)) => { 97 + member.remove_role(ctx.http(), role_id).await?; 98 + guild_id.delete_role(ctx.http(), role_id).await?; 99 + 100 + ctx.send( 101 + CreateReply::default() 102 + .content("Removed your color role.") 103 + .ephemeral(true), 104 + ) 105 + .await?; 106 + } 107 + None => { 108 + ctx.send( 109 + CreateReply::default() 110 + .content("You don't have a color role to remove.") 111 + .ephemeral(true), 112 + ) 113 + .await?; 114 + } 115 + } 116 + } 117 + } 118 + 119 + Ok(()) 120 + }
+1
src/commands/user/mod.rs
··· 1 1 pub mod avatar; 2 + pub mod color_me; 2 3 pub mod whois;
+1
src/main.rs
··· 30 30 // user commands 31 31 commands::user::whois::whois(), 32 32 commands::user::avatar::avatar(), 33 + commands::user::color_me::color_me(), 33 34 // bot commands 34 35 commands::bot::ping::ping(), 35 36 commands::bot::bot::botinfo(),