Skip to content

Commit

Permalink
fix(intelligence): add configurable allowlist
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanccn committed Sep 29, 2024
1 parent fdef36f commit 8a5cb04
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/handlers/intelligence.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
use std::time::Duration;
use poise::serenity_prelude as serenity;

use color_eyre::eyre::Result;
use poise::serenity_prelude as serenity;
use once_cell::sync::Lazy;
use std::time::Duration;
use tokio::{task, time};

use crate::intelligence;

static ALLOWED_ROLES: Lazy<Vec<serenity::RoleId>> = Lazy::new(|| {
std::env::var("INTELLIGENCE_ALLOWED_ROLES")
.ok()
.map(|s| {
s.split(',')
.filter_map(|f| f.trim().parse::<serenity::RoleId>().ok())
.collect::<Vec<_>>()
})
.unwrap_or_default()
});

#[tracing::instrument(skip_all, fields(message_id = message.id.get()))]
pub async fn handle(message: &serenity::Message, ctx: &serenity::Context) -> Result<()> {
if message
Expand All @@ -15,6 +27,14 @@ pub async fn handle(message: &serenity::Message, ctx: &serenity::Context) -> Res
return Ok(());
}

let member = message.member(&ctx).await?;

if !member.permissions(&ctx.cache)?.administrator()
&& !member.roles.iter().any(|r| ALLOWED_ROLES.contains(r))
{
return Ok(());
}

if let Some(query) = message
.content
.strip_prefix(&format!("<@{}>", ctx.cache.current_user().id))
Expand All @@ -41,10 +61,7 @@ pub async fn handle(message: &serenity::Message, ctx: &serenity::Context) -> Res
let username = message.author.tag();
let display_name = message.author.global_name.clone().map(|s| s.into_string());

let nick = message
.member
.as_ref()
.and_then(|m| m.nick.as_ref().map(|s| s.to_owned().into_string()));
let nick = member.nick.as_ref().map(|s| s.to_owned().into_string());

let resp = intelligence::query(intelligence::Request {
query: query.to_owned(),
Expand Down

0 comments on commit 8a5cb04

Please sign in to comment.