Skip to content
This repository was archived by the owner on Sep 1, 2023. It is now read-only.

Commit 241acf7

Browse files
committed
feat: add mods deny <id> <...perm> command
1 parent fb759da commit 241acf7

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

src/commands/mods/deny.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use crate::entities::moderator::Feature;
2+
use spinners::*;
3+
fn subtract_vecs<T>(v1: &Vec<T>, v2: &Vec<T>) -> Vec<T>
4+
where T: Eq + Clone
5+
{
6+
v1.iter().filter(|&x| !v2.contains(x)).cloned().collect()
7+
}
8+
pub fn deny(id: u128, features: Vec<Feature>) {
9+
let token = super::expect_token();
10+
let app_id = crate::handle_result!(super::ask_for_app(token.clone(), "modify the mod's permissions"));
11+
let mut spinner = Spinner::new(Spinners::Toggle2, "Removing the permissions...".into());
12+
let moderator = crate::handle_result!(crate::entities::moderator::Mod::fetch_mod(token.clone(), id, app_id));
13+
match moderator {
14+
Some(mut moderator) => {
15+
crate::handle_result!(moderator.set_features(subtract_vecs(&moderator.get_features(), &features), token.clone()), spinner);
16+
spinner.stop_with_message(super::format_log(&format!("{:?} were removed successfully!", features)));
17+
},
18+
None => {
19+
spinner.stop_with_message(super::format_err("That moderator doesn't exist!"));
20+
}
21+
}
22+
}

src/commands/mods/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod add;
22
pub mod remove;
33
pub mod allow;
4+
pub mod deny;
45
use super::*;

src/entities/moderator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
33
use serde_enum_str::*;
44
use std::fmt::Debug;
55

6-
#[derive(Deserialize_enum_str, Serialize_enum_str, Clone)]
6+
#[derive(Deserialize_enum_str, Serialize_enum_str, Clone, Eq, PartialEq)]
77
pub enum Feature {
88
#[serde(rename = "start_app")]
99
Start,

src/main.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,17 @@ fn main() -> std::io::Result<()> {
141141
.multiple_occurrences(true)
142142
)
143143
)
144+
.subcommand(
145+
Command::new("deny")
146+
.about("Removes permissions from a moderator")
147+
.arg(Arg::new("id").value_parser(value_parser!(u128)).action(clap::ArgAction::Set))
148+
.arg(
149+
Arg::new("perm")
150+
.value_parser(value_parser!(Feature))
151+
.action(clap::ArgAction::Append)
152+
.multiple_occurrences(true)
153+
)
154+
)
144155
.after_help("Be careful with what people you add and what permissions you give: With Great Power comes Great Responsability.")
145156
);
146157
let matches = cmd.get_matches();
@@ -196,6 +207,14 @@ fn main() -> std::io::Result<()> {
196207
commands::mods::remove::remove(id);
197208
Ok(())
198209
}
210+
Some(("deny", matches)) => {
211+
let id: u128 = *matches.get_one("id").unwrap();
212+
let features: Vec<Feature> = matches.get_many("perm").unwrap()
213+
.map(|perm: &Feature| perm.clone())
214+
.collect();
215+
commands::mods::deny::deny(id, features);
216+
Ok(())
217+
}
199218
Some(("allow", matches)) => {
200219
let id: u128 = *matches.get_one("id").unwrap();
201220
let features: Vec<Feature> = matches.get_many("perm").unwrap()

0 commit comments

Comments
 (0)