Skip to content

Commit

Permalink
Allow only specific team roles to be initailized to owners
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbt365 committed May 26, 2024
1 parent 9f19a7d commit 9507d80
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/framework/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,13 @@ impl<U: Send + Sync, E: Send + Sync> serenity::Framework for Framework<U, E> {
self.shard_manager = Some(client.shard_manager.clone());

if self.options.initialize_owners {
if let Err(e) = insert_owners_from_http(&client.http, &mut self.options.owners).await {
if let Err(e) = insert_owners_from_http(
&client.http,
&mut self.options.owners,
&self.options.initialized_team_roles,
)
.await
{
tracing::warn!("Failed to insert owners from HTTP: {e}");
}
}
Expand Down Expand Up @@ -236,6 +242,7 @@ fn message_content_intent_sanity_check<U, E>(
pub async fn insert_owners_from_http(
http: &serenity::Http,
owners: &mut std::collections::HashSet<serenity::UserId>,
initialized_teams: &Option<Vec<serenity::TeamMemberRole>>,
) -> Result<(), serenity::Error> {
let application_info = http.get_current_application_info().await?;

Expand All @@ -245,9 +252,12 @@ pub async fn insert_owners_from_http(

if let Some(team) = application_info.team {
for member in team.members {
// This `if` currently always evaluates to true but it becomes important once
// Discord implements more team roles than Admin
if member.permissions.iter().any(|p| p == "*") {
let Some(initialized_teams) = initialized_teams else {
owners.insert(member.user.id);
continue;
};

if initialized_teams.iter().any(|r| *r == member.role) {
owners.insert(member.user.id);
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/structs/framework_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ pub struct FrameworkOptions<U, E> {
///
/// True by default.
pub initialize_owners: bool,
/// If set and [`Self::initialize_owners`] is true, only select teams will be initialized by
/// the results of [`serenity::Http::get_current_application_info()`].
///
/// None by default.
pub initialized_team_roles: Option<Vec<serenity::TeamMemberRole>>,
// #[non_exhaustive] forbids struct update syntax for ?? reason
#[doc(hidden)]
pub __non_exhaustive: (),
Expand Down Expand Up @@ -120,6 +125,7 @@ where
prefix_options: Default::default(),
owners: Default::default(),
initialize_owners: true,
initialized_team_roles: None,
__non_exhaustive: (),
}
}
Expand Down

0 comments on commit 9507d80

Please sign in to comment.