Merge pull request 'Prevent Bots from running BotCommands' (#36) from Cmds_Cannot_Be_Ran_by_Bots into main
All checks were successful
ci/woodpecker/push/cargo-checks Pipeline was successful
All checks were successful
ci/woodpecker/push/cargo-checks Pipeline was successful
Reviewed-on: #36
This commit is contained in:
commit
7efb679b06
4 changed files with 90 additions and 3 deletions
4
.cargo/config.toml
Normal file
4
.cargo/config.toml
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
|
||||||
|
[env]
|
||||||
|
# Based on https://doc.rust-lang.org/cargo/reference/config.html
|
||||||
|
OtherBots = "Supibot,buttsbot,PotatBotat,StreamElements"
|
|
@ -14,7 +14,8 @@ futures = "0.3"
|
||||||
async-trait = "0.1.77"
|
async-trait = "0.1.77"
|
||||||
casual_logger = "0.6.5"
|
casual_logger = "0.6.5"
|
||||||
|
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
name = "bot_lib"
|
name = "bot_lib"
|
||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -13,3 +13,10 @@ access_token = <oath token>
|
||||||
bot_channels = <chnl1>,<chnl2>
|
bot_channels = <chnl1>,<chnl2>
|
||||||
prefix = <prefix>
|
prefix = <prefix>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
2. If required, adjust the following additional `.cargo\config.toml` configurations :
|
||||||
|
```
|
||||||
|
[env]
|
||||||
|
OtherBots = # Other Bots for special handling (e.g., bot commands cant be ran by bots)
|
||||||
|
```
|
|
@ -12,11 +12,40 @@ use crate::core::botinstance::ChType;
|
||||||
use crate::core::botlog;
|
use crate::core::botlog;
|
||||||
use crate::core::botmodules::{BotActionTrait, BotCommand, BotModule, ModulesManager};
|
use crate::core::botmodules::{BotActionTrait, BotCommand, BotModule, ModulesManager};
|
||||||
|
|
||||||
|
use dotenv::dotenv;
|
||||||
|
use std::env;
|
||||||
|
|
||||||
fn adminvector() -> Vec<String> {
|
fn adminvector() -> Vec<String> {
|
||||||
vec![String::from("ModulatingForce")]
|
vec![String::from("ModulatingForce")]
|
||||||
//vec![]
|
//vec![]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn otherbots_vector() -> Vec<String> {
|
||||||
|
// vec![String::from("ModulatingForce")]
|
||||||
|
// //vec![]
|
||||||
|
|
||||||
|
dotenv().ok();
|
||||||
|
let mut other_bots = Vec::new();
|
||||||
|
|
||||||
|
if let Ok(value) = env::var("OtherBots") {
|
||||||
|
for bot in value.split(',') {
|
||||||
|
other_bots.push(String::from(bot).to_lowercase())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
botlog::debug(
|
||||||
|
&format!(
|
||||||
|
"OtherBots : {:?}",other_bots,
|
||||||
|
),
|
||||||
|
Some("identity.rs > otherbots_vector()".to_string()),
|
||||||
|
None,
|
||||||
|
);
|
||||||
|
|
||||||
|
other_bots
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
pub async fn init(mgr: Arc<ModulesManager>) {
|
pub async fn init(mgr: Arc<ModulesManager>) {
|
||||||
botlog::trace(
|
botlog::trace(
|
||||||
"Went into Identity Module init",
|
"Went into Identity Module init",
|
||||||
|
@ -549,7 +578,7 @@ pub enum UserRole {
|
||||||
Broadcaster,
|
Broadcaster,
|
||||||
BotAdmin,
|
BotAdmin,
|
||||||
}
|
}
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub enum Permissible {
|
pub enum Permissible {
|
||||||
Allow,
|
Allow,
|
||||||
Block,
|
Block,
|
||||||
|
@ -780,6 +809,24 @@ impl IdentityManager {
|
||||||
|
|
||||||
let usr = usr.to_lowercase();
|
let usr = usr.to_lowercase();
|
||||||
|
|
||||||
|
|
||||||
|
let bot_vector = otherbots_vector() ; // result of pulling from Cargo.toml
|
||||||
|
|
||||||
|
botlog::trace(
|
||||||
|
&format!(
|
||||||
|
"Checking user is part of known bots: bot_vector.contains(&usr) : {:?}",bot_vector.contains(&usr)
|
||||||
|
),
|
||||||
|
Some("identity.rs > can_user_run()".to_string()),
|
||||||
|
None,
|
||||||
|
);
|
||||||
|
|
||||||
|
if bot_vector.contains(&usr) {
|
||||||
|
return (
|
||||||
|
Permissible::Block,
|
||||||
|
ChangeResult::NoChange("Other Bots Cannot Run Commands".to_string()),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// if cmdreqroles.len() == 0 {
|
// if cmdreqroles.len() == 0 {
|
||||||
if cmdreqroles.is_empty() {
|
if cmdreqroles.is_empty() {
|
||||||
// return Ok(Permissible::Allow)
|
// return Ok(Permissible::Allow)
|
||||||
|
@ -1382,6 +1429,34 @@ mod core_identity {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn otherbots_checks() {
|
||||||
|
Log::set_file_ext(Extension::Log);
|
||||||
|
|
||||||
|
let mut test_id_mgr = IdentityManager::init();
|
||||||
|
|
||||||
|
for bot in otherbots_vector() {
|
||||||
|
|
||||||
|
let (usr, channelname, chat_badge, cmdreqroles) = (
|
||||||
|
bot,
|
||||||
|
ChType::Channel("twitchchanneltest".to_string()),
|
||||||
|
None,
|
||||||
|
vec![]
|
||||||
|
);
|
||||||
|
|
||||||
|
let rslt = test_id_mgr.can_user_run(usr, channelname, chat_badge, cmdreqroles).await;
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
(Permissible::Block,
|
||||||
|
ChangeResult::NoChange("Other Bots Cannot Run Commands".to_string())),
|
||||||
|
rslt
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn promote_workflow_01() {
|
async fn promote_workflow_01() {
|
||||||
Log::set_file_ext(Extension::Log);
|
Log::set_file_ext(Extension::Log);
|
||||||
|
|
Loading…
Reference in a new issue