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 Jun 3, 2024
1 parent 8261d3f commit f44c442
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/framework/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,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 @@ -239,6 +245,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 @@ -253,7 +260,16 @@ pub async fn insert_owners_from_http(
continue;
}

if let TeamMemberRole::Admin | TeamMemberRole::Developer = member.role {
// Default configuration.
let Some(initialized_teams) = initialized_teams else {
if let TeamMemberRole::Admin | TeamMemberRole::Developer = member.role {
owners.insert(member.user.id);
}
continue;
};

// s has specified the teams they want to be considered "Owner".
if initialized_teams.iter().any(|r| *r == member.role) {
owners.insert(member.user.id);
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/structs/framework_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ pub struct FrameworkOptions<U, E> {
///
/// True by default.
pub initialize_owners: bool,
/// If set and [`Self::initialize_owners`] is `true`, the selected teams will be initialized
/// using the results of [`serenity::Http::get_current_application_info()`].
///
/// When set to `None`, only users with the Developer and Admin roles are initialized.
///
/// 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 +127,7 @@ where
prefix_options: Default::default(),
owners: Default::default(),
initialize_owners: true,
initialized_team_roles: None,
__non_exhaustive: (),
}
}
Expand Down

0 comments on commit f44c442

Please sign in to comment.