diff --git a/src/discord/color.rs b/src/discord/color.rs index f24ecf8..5d1de3a 100644 --- a/src/discord/color.rs +++ b/src/discord/color.rs @@ -1,96 +1,96 @@ -use crate::{command, group}; -use serenity::builder::EditRole; -use serenity::client::Context; -use serenity::framework::standard::{Args, CommandResult}; -use serenity::model::channel::Message; -use serenity::model::Colour; +use crate::discord::Context; +use crate::error::Error; +use poise::serenity_prelude::builder::EditRole; +use poise::serenity_prelude::model::Colour; -#[group] -#[commands(set_color, remove_color)] -pub struct Color; +#[poise::command(prefix_command, guild_only)] +pub async fn set_color( + ctx: Context<'_>, + #[description = "Color you want your role to be"] + #[rest] + color: String, +) -> Result<(), Error> { + let mut color_parts = color.split(" "); -#[command] -#[example("#35BB1D")] -#[example("0x35BB1D")] -#[example("53 187 29")] -#[description("Set your name color.")] -#[min_args(1)] -#[max_args(3)] -#[only_in(guilds)] -async fn set_color(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { - let color = if args.len() == 3 { - let r = args.parse::()?; - args.advance(); - let g = args.parse::()?; - args.advance(); - let b = args.parse::()?; - args.advance(); + let color = if color_parts.clone().count() == 3 { + let r = color_parts + .next() + .unwrap() + .parse::() + .map_err(|_| Error::ParseFailure)?; + let g = color_parts + .next() + .unwrap() + .parse::() + .map_err(|_| Error::ParseFailure)?; + let b = color_parts + .next() + .unwrap() + .parse::() + .map_err(|_| Error::ParseFailure)?; Colour::from_rgb(r, g, b) } else { - let color_str = args.rest(); + let color_str = color_parts.next().unwrap(); if color_str.starts_with("0x") || color_str.starts_with('#') { let color_str = color_str.replace("0x", "").replace('#', ""); - let val = u32::from_str_radix(&color_str, 16)?; + let val = u32::from_str_radix(&color_str, 16).map_err(|_| Error::ParseFailure)?; Colour::new(val) } else { - Colour::new(color_str.parse()?) + Colour::new(color_str.parse().map_err(|_| Error::ParseFailure)?) } }; - let role = if let Some(role) = msg.member.as_ref().unwrap().roles.iter().find(|r| { - ctx.cache - .role(msg.guild_id.unwrap(), **r) - .unwrap() - .name - .contains("COwOlor") - }) { - *role + let guild_id = ctx.guild_id().unwrap(); + let member = ctx.author_member().await.unwrap(); + + let roles = member.roles(&ctx.cache()).unwrap(); + + let color_role = roles.iter().find(|role| role.name.contains("COwOlor")); + + let color_role = if let Some(color_role) = color_role { + color_role.id } else { - msg.guild_id - .unwrap() + guild_id .create_role( - &ctx.http, - EditRole::new().name(&format!("{} COwOlor", msg.author.name)), + ctx.http(), + EditRole::new().name(format!("{} COwOlor", ctx.author().name)), ) .await? .id }; - msg.guild_id - .unwrap() - .edit_role(&ctx.http, role, EditRole::new().colour(color)) + guild_id + .edit_role(&ctx.http(), color_role, EditRole::new().colour(color)) .await?; - msg.guild_id - .unwrap() - .member(&ctx.http, msg.author.id) + ctx.author_member() .await .unwrap() - .add_role(&ctx.http, role) + .add_role(&ctx.http(), color_role) .await?; - msg.reply(&ctx.http, "Color set!").await?; + ctx.reply("Be not afraid child, I have bestowed onto you your color. May your soul rest now.") + .await?; Ok(()) } -#[command] -#[max_args(0)] -#[only_in(guilds)] -#[description("Remove your name color.")] -async fn remove_color(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { - if let Some(role) = msg.member.as_ref().unwrap().roles.iter().find(|r| { - ctx.cache - .role(msg.guild_id.unwrap(), **r) - .unwrap() - .name - .contains("COwOlor") - }) { - msg.guild_id.unwrap().delete_role(&ctx.http, role).await?; +#[poise::command(prefix_command, guild_only)] +pub async fn remove_color(ctx: Context<'_>) -> Result<(), Error> { + let guild_id = ctx.guild_id().unwrap(); + let member = ctx.author_member().await.unwrap(); + let roles = member.roles(&ctx.cache()).unwrap(); + let color_role = roles.iter().find(|role| role.name.contains("COwOlor")); + + if let Some(role) = color_role { + guild_id.delete_role(ctx.http(), role.id).await?; + ctx.reply("Color removed!").await?; + } else { + ctx.reply("My child, I can't remove what has never existed.") + .await?; } - msg.reply(&ctx.http, "Color removed!").await?; Ok(()) } diff --git a/src/discord/mod.rs b/src/discord/mod.rs index 5930505..7e4e4f5 100644 --- a/src/discord/mod.rs +++ b/src/discord/mod.rs @@ -2,11 +2,11 @@ mod admin; mod album; mod birthday; mod celeryman; +mod color; use crate::config::GlobalData; use crate::error::Error; use log::info; -use magick_rust::bindings::wchar_t; use std::sync::Arc; use std::time::Duration; @@ -119,6 +119,8 @@ pub async fn run_bot(global_data: GlobalData) { celeryman::nudetayne(), celeryman::celeryman(), celeryman::tayne(), + color::set_color(), + color::remove_color(), ], event_handler: |ctx, event, framework, data| { Box::pin(event_handler(ctx, event, framework, data)) diff --git a/src/error.rs b/src/error.rs index e51161e..c4a8967 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,6 +1,6 @@ use crate::user; use serde::ser::StdError; -use std::fmt::{Display, Formatter}; +use std::fmt::{write, Display, Formatter}; #[derive(Debug)] #[allow(clippy::enum_variant_names)] @@ -13,6 +13,7 @@ pub enum Error { UserError(user::UserError), DbError(j_db::error::JDbError), ReqwestError(reqwest::Error), + ParseFailure, } impl StdError for Error {} @@ -63,6 +64,7 @@ impl Display for Error { Error::UserError(e) => write!(f, "User error: {}", e), Error::DbError(e) => write!(f, "DB error: {}", e), Error::ReqwestError(e) => write!(f, "Reqwest Error: {}", e), + Error::ParseFailure => write!(f, "Failed to parse something or other"), } } }