reworked permission system :flooshed:

This commit is contained in:
poslop
2022-12-07 19:28:03 -06:00
parent 095d66d49f
commit 1911a0f976
5 changed files with 203 additions and 140 deletions

View File

@@ -1,48 +1,14 @@
use crate::structs::*;
use std::str::FromStr;
use std::fs::{File, OpenOptions};
use std::io::{Read, Write, Seek, BufRead};
use std::time::Duration;
use std::net::TcpStream;
use serenity::prelude::TypeMapKey;
use std::str;
use serenity::model::prelude::{Message, RoleId};
use serenity::prelude::{Context};
use std::{str};
use serenity::prelude::SerenityError;
use indoc::indoc;
pub struct TcpSock {
pub stream: TcpStream,
}
impl TcpSock {
pub fn new(ip: String, port: String) -> std::io::Result<Self> {
let stream = TcpStream::connect(format!("{}:{}", ip, port)).expect("Tcp connection fail");
stream.set_read_timeout(Some(Duration::from_millis(25)))?;
println!("Socket Connected!!");
Ok(TcpSock { stream })
}
}
impl TypeMapKey for TcpSock {
type Value = TcpSock;
}
#[derive(serde::Deserialize, serde::Serialize)]
pub struct Config {
pub discord_token: String,
pub ip: String,
pub port: String,
pub prefix: String,
pub roles: Roles
}
#[derive(serde::Deserialize, serde::Serialize)]
pub struct Roles {
pub auth: Vec<String>,
pub cons: Vec<String>
}
impl TypeMapKey for Config {
type Value = Config;
}
pub fn cons_rw(sock: &TcpSock, input: &str) -> String {
@@ -60,6 +26,7 @@ pub fn cons_rw(sock: &TcpSock, input: &str) -> String {
Err(_) => break(),
};
}
output = String::from_utf8(strip_ansi_escapes::strip(&output).unwrap()).unwrap();
output.truncate(4000);
output
@@ -93,45 +60,88 @@ let mut toml_file = OpenOptions::new()
.open("config.toml")
.unwrap();
let fill_conf =
indoc! {r#"
# Discord bot token
discord_token = ""
# Ip of the mindustry server
ip = "localhost"
# Port of the mindustry server socket
# Run 'config socketInputPort' in the mindustry console to find this port
port = "6859"
# Prefix used to call commands
# Can be any word letter or symbol
prefix = ";"
# These are the roles needed in order to use the associated command
# If an invalid role is used it will be ignored. If all the roles are invalid or the list is empty then anyone can use the command
[roles]
# Auth command
auth = [""]
# console command
cons = ["738543444322156574", "822523680391037009"]
"#};
let fill_conf = indoc! {r#"
[discord_settings]
// let fill_conf = Config {
// discord_token: String::from(""),
// ip: String::from("localhost"),
// port: String::from("6859"),
// prefix: String::from(";"),
// roles: Roles {
// auth: vec![String::from(""), String::from("")],
// cons: vec![String::from("")]
// }
// };
# Discord bot token
discord_token = ""
toml_file.write(fill_conf.as_bytes()).expect("Unable to write to new file");
# Ip of the mindustry server
ip = "localhost"
# Port of the mindustry server socket
# Run 'config socketInputPort' in the mindustry console to find this port
port = "6859"
# Prefix used to call commands
# Can be any word letter or symbol
prefix = ";"
# These are the role ids needed in order to use the console command
# If an invalid role is used it will be ignored
# If all the roles are invalid or the list is empty the setting will be ignored
[admin_roles]
# people with roles ids in the owner setting can use all console commands
# if left empty anyone can use any of the commands
owners = ["738543444322156574", "822523680391037009"]
# list of admin roles
admins = []
# this controls which commands admins have access too
[console]
# whether the command list is a whitelist or a blacklist
# true = whitelist
# false = blacklist
commands_whitelist = true
# which commands are whitelisted/blacklisted to admins
commands = ["config"]
"#};
toml_file.write(&fill_conf.as_bytes()).expect("Unable to write to new file");
toml_file.flush().unwrap();
toml_file.rewind().unwrap();
toml_file
}
}
pub async fn check_role(ctx: &Context, msg: &Message, roles: &Vec<String>) -> Result<bool, SerenityError> {
let mut invalid_roles = 0;
for v_id in roles {
let u_id = match u64::from_str(&v_id) {
Ok(n) => n,
Err(_e) =>
{
invalid_roles += 1;
continue
},
};
let check = msg.author.has_role(ctx, msg.guild_id.unwrap(), RoleId::from(u_id)).await?;
if check {
return Ok(check);
}
}
if invalid_roles == roles.len() {
return Ok(true)
}
Ok(false)
}
pub fn is_command(command: String, command_vec: &Vec<String>) -> bool {
for r in command_vec {
if r == &command {
return true;
}
}
false
}