From fb1d8206c0b3b9a5999cae1a2ec28ff70d088915 Mon Sep 17 00:00:00 2001 From: yourfriendoss Date: Sun, 14 Sep 2025 18:35:39 +0300 Subject: [PATCH] feat: fix rank names, add banned rank, and make rank. permissions. --- src/commands/system/rank.rs | 119 ++++++++++++++++++++++++++++++------ src/main.rs | 17 +++++- 2 files changed, 114 insertions(+), 22 deletions(-) diff --git a/src/commands/system/rank.rs b/src/commands/system/rank.rs index 05a69af..e5bac95 100644 --- a/src/commands/system/rank.rs +++ b/src/commands/system/rank.rs @@ -4,6 +4,7 @@ use crate::client::Client; use crate::client::ClientEvent; use crate::client::Player; use crate::commands::Command; +use crate::has_permission; use crate::commands::argument::{ArgumentSpec, ArgumentType, ParsedArgument, ParsedArguments}; use std::collections::HashMap; @@ -47,7 +48,7 @@ impl Command for RankCommand { default: None, children: &[ArgumentSpec { name: "sub_action", - arg_type: ArgumentType::Enum(&["set", "delete"]), + arg_type: ArgumentType::Enum(&["set", "unset"]), required: true, default: None, children: &[ArgumentSpec { @@ -88,6 +89,7 @@ impl Command for RankCommand { Some(ParsedArgument::Enum(s)) => s.as_str(), _ => "", }; + let command_user_cloned = command_user.clone(); let user_db = match args.get("user_id") { Some(ParsedArgument::String(s)) => { @@ -106,7 +108,7 @@ impl Command for RankCommand { return; } - let user = user_db.unwrap(); + let mut user = user_db.unwrap(); if !sub_action.is_empty() { let action = match args.get("action") { @@ -118,26 +120,103 @@ impl Command for RankCommand { Some(ParsedArgument::String(s)) => s.as_str(), _ => "", }; - match action { - "rank" => match action { - "set" => {} - "delete" => {} + if has_permission( + &command_user_cloned, + self.ranks.clone(), + format!("rank.{action}.{sub_action}"), + ) { + match action { + "rank" => match sub_action { + "set" => { + user.rank = permission_or_rank.to_string(); + sqlx::query("UPDATE users SET rank = $1 WHERE _id = $2") + .bind(user.rank.clone()) + .bind(user._id.clone()) + .execute(self.pool.as_ref()) + .await + .unwrap(); + client + .message(format!("Set `{}`'s rank to `{}`.", user._id, user.rank)) + .await; + } + "unset" => { + client + .message("To unset a rank, do `rank rank set user `.") + .await; + } + _ => {} + }, + "permission" => match sub_action { + "set" => { + let permission = permission_or_rank.to_string(); + if !user.extra_permissions.contains(&permission) { + user.extra_permissions.push(permission.clone()); + sqlx::query( + "UPDATE users SET extra_permissions = $1 WHERE _id = $2", + ) + .bind(&user.extra_permissions) + .bind(user._id.clone()) + .execute(self.pool.as_ref()) + .await + .unwrap(); + client + .message(format!( + "Added permission `{}` to `{}`.", + permission, user._id + )) + .await; + } else { + client + .message(format!( + "`{}` already has permission `{}`.", + user._id, permission + )) + .await; + } + } + "unset" => { + let permission = permission_or_rank.to_string(); + if user.extra_permissions.contains(&permission) { + user.extra_permissions.retain(|p| p != &permission); + sqlx::query( + "UPDATE users SET extra_permissions = $1 WHERE _id = $2", + ) + .bind(&user.extra_permissions) + .bind(user._id.clone()) + .execute(self.pool.as_ref()) + .await + .unwrap(); + client + .message(format!( + "Removed permission `{}` from `{}`.", + permission, user._id + )) + .await; + } else { + client + .message(format!( + "`{}` does not have permission `{}`.", + user._id, permission + )) + .await; + } + } + _ => {} + }, _ => {} - }, - "permission" => match action { - "set" => {} - "delete" => {} - _ => {} - }, - _ => {} - } + } - client - .message(format!( - "action: {}, sub_action: {}, permission_or_rank: {}", - action, sub_action, permission_or_rank - )) - .await; + client + .message(format!( + "action: {}, sub_action: {}, permission_or_rank: {}", + action, sub_action, permission_or_rank + )) + .await; + } else { + client.message( + format!("You do not have permission `rank.{action}.{sub_action}` to run this command."), + ).await; + } } else { let computed = match args.get("computed") { Some(ParsedArgument::Boolean(s)) => s, diff --git a/src/main.rs b/src/main.rs index ea16785..a6354bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,10 +62,15 @@ pub async fn get_ranks(registry: CommandRegistry) -> HashMap { ranks.insert( "owner".to_string(), Rank { - name: "owner".to_string(), + name: "Owner".to_string(), permissions: { let mut perms = command_permissions.clone(); perms.push("play.high_note_counts".to_string()); + perms.push("rank.rank.set".to_string()); + perms.push("rank.rank.unset".to_string()); + perms.push("rank.permission.set".to_string()); + perms.push("rank.permission.unset".to_string()); + perms }, }, @@ -74,7 +79,7 @@ pub async fn get_ranks(registry: CommandRegistry) -> HashMap { ranks.insert( "user".to_string(), Rank { - name: "user".to_string(), + name: "User".to_string(), permissions: { let mut perms = command_permissions.clone(); perms.retain(|p| p != "commands.launch"); @@ -83,6 +88,14 @@ pub async fn get_ranks(registry: CommandRegistry) -> HashMap { }, ); + ranks.insert( + "banned".to_string(), + Rank { + name: "Banned".to_string(), + permissions: vec![], + }, + ); + ranks }