feat: fix rank names, add banned rank, and make rank. permissions.
This commit is contained in:
parent
1c2cc8f1fe
commit
fb1d8206c0
2 changed files with 114 additions and 22 deletions
|
|
@ -4,6 +4,7 @@ use crate::client::Client;
|
||||||
use crate::client::ClientEvent;
|
use crate::client::ClientEvent;
|
||||||
use crate::client::Player;
|
use crate::client::Player;
|
||||||
use crate::commands::Command;
|
use crate::commands::Command;
|
||||||
|
use crate::has_permission;
|
||||||
|
|
||||||
use crate::commands::argument::{ArgumentSpec, ArgumentType, ParsedArgument, ParsedArguments};
|
use crate::commands::argument::{ArgumentSpec, ArgumentType, ParsedArgument, ParsedArguments};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
@ -47,7 +48,7 @@ impl Command for RankCommand {
|
||||||
default: None,
|
default: None,
|
||||||
children: &[ArgumentSpec {
|
children: &[ArgumentSpec {
|
||||||
name: "sub_action",
|
name: "sub_action",
|
||||||
arg_type: ArgumentType::Enum(&["set", "delete"]),
|
arg_type: ArgumentType::Enum(&["set", "unset"]),
|
||||||
required: true,
|
required: true,
|
||||||
default: None,
|
default: None,
|
||||||
children: &[ArgumentSpec {
|
children: &[ArgumentSpec {
|
||||||
|
|
@ -88,6 +89,7 @@ impl Command for RankCommand {
|
||||||
Some(ParsedArgument::Enum(s)) => s.as_str(),
|
Some(ParsedArgument::Enum(s)) => s.as_str(),
|
||||||
_ => "",
|
_ => "",
|
||||||
};
|
};
|
||||||
|
let command_user_cloned = command_user.clone();
|
||||||
|
|
||||||
let user_db = match args.get("user_id") {
|
let user_db = match args.get("user_id") {
|
||||||
Some(ParsedArgument::String(s)) => {
|
Some(ParsedArgument::String(s)) => {
|
||||||
|
|
@ -106,7 +108,7 @@ impl Command for RankCommand {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let user = user_db.unwrap();
|
let mut user = user_db.unwrap();
|
||||||
|
|
||||||
if !sub_action.is_empty() {
|
if !sub_action.is_empty() {
|
||||||
let action = match args.get("action") {
|
let action = match args.get("action") {
|
||||||
|
|
@ -118,15 +120,87 @@ impl Command for RankCommand {
|
||||||
Some(ParsedArgument::String(s)) => s.as_str(),
|
Some(ParsedArgument::String(s)) => s.as_str(),
|
||||||
_ => "",
|
_ => "",
|
||||||
};
|
};
|
||||||
|
if has_permission(
|
||||||
|
&command_user_cloned,
|
||||||
|
self.ranks.clone(),
|
||||||
|
format!("rank.{action}.{sub_action}"),
|
||||||
|
) {
|
||||||
match action {
|
match action {
|
||||||
"rank" => match action {
|
"rank" => match sub_action {
|
||||||
"set" => {}
|
"set" => {
|
||||||
"delete" => {}
|
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 <ID>`.")
|
||||||
|
.await;
|
||||||
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
},
|
},
|
||||||
"permission" => match action {
|
"permission" => match sub_action {
|
||||||
"set" => {}
|
"set" => {
|
||||||
"delete" => {}
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
},
|
},
|
||||||
_ => {}
|
_ => {}
|
||||||
|
|
@ -138,6 +212,11 @@ impl Command for RankCommand {
|
||||||
action, sub_action, permission_or_rank
|
action, sub_action, permission_or_rank
|
||||||
))
|
))
|
||||||
.await;
|
.await;
|
||||||
|
} else {
|
||||||
|
client.message(
|
||||||
|
format!("You do not have permission `rank.{action}.{sub_action}` to run this command."),
|
||||||
|
).await;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
let computed = match args.get("computed") {
|
let computed = match args.get("computed") {
|
||||||
Some(ParsedArgument::Boolean(s)) => s,
|
Some(ParsedArgument::Boolean(s)) => s,
|
||||||
|
|
|
||||||
17
src/main.rs
17
src/main.rs
|
|
@ -62,10 +62,15 @@ pub async fn get_ranks(registry: CommandRegistry) -> HashMap<String, Rank> {
|
||||||
ranks.insert(
|
ranks.insert(
|
||||||
"owner".to_string(),
|
"owner".to_string(),
|
||||||
Rank {
|
Rank {
|
||||||
name: "owner".to_string(),
|
name: "Owner".to_string(),
|
||||||
permissions: {
|
permissions: {
|
||||||
let mut perms = command_permissions.clone();
|
let mut perms = command_permissions.clone();
|
||||||
perms.push("play.high_note_counts".to_string());
|
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
|
perms
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
@ -74,7 +79,7 @@ pub async fn get_ranks(registry: CommandRegistry) -> HashMap<String, Rank> {
|
||||||
ranks.insert(
|
ranks.insert(
|
||||||
"user".to_string(),
|
"user".to_string(),
|
||||||
Rank {
|
Rank {
|
||||||
name: "user".to_string(),
|
name: "User".to_string(),
|
||||||
permissions: {
|
permissions: {
|
||||||
let mut perms = command_permissions.clone();
|
let mut perms = command_permissions.clone();
|
||||||
perms.retain(|p| p != "commands.launch");
|
perms.retain(|p| p != "commands.launch");
|
||||||
|
|
@ -83,6 +88,14 @@ pub async fn get_ranks(registry: CommandRegistry) -> HashMap<String, Rank> {
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
ranks.insert(
|
||||||
|
"banned".to_string(),
|
||||||
|
Rank {
|
||||||
|
name: "Banned".to_string(),
|
||||||
|
permissions: vec![],
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
ranks
|
ranks
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue