Skip to content

Commit

Permalink
Document builder execute
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbt365 committed May 25, 2024
1 parent 3c47f28 commit 4a37978
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 36 deletions.
42 changes: 25 additions & 17 deletions src/builder/edit_onboarding/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
#[cfg(feature = "http")]
use super::Builder;

use crate::model::guild::OnboardingMode;
use crate::model::id::ChannelId;

#[cfg(feature = "http")]
use crate::model::id::GuildId;
#[cfg(feature = "http")]
use crate::model::Permissions;
#[cfg(feature = "http")]
use crate::http::CacheHttp;
#[cfg(feature = "http")]
use crate::model::guild::Onboarding;
#[cfg(feature = "http")]
use crate::internal::prelude::*;
use crate::model::guild::{Onboarding, OnboardingMode};
#[cfg(feature = "http")]
use crate::model::prelude::*;
#[cfg(feature = "http")]
use crate::model::Permissions;

mod prompt_option_structure;
mod prompt_structure;
Expand Down Expand Up @@ -82,7 +77,7 @@ impl<'a> EditOnboarding<'a, NeedsPrompts> {
Self::default()
}


/// The onboarding prompts that users can select the options of.
pub fn prompts(
self,
prompts: Vec<CreateOnboardingPrompt<prompt_structure::Ready>>,
Expand All @@ -101,11 +96,10 @@ impl<'a> EditOnboarding<'a, NeedsPrompts> {

impl<'a> EditOnboarding<'a, NeedsChannels> {
/// The list of default channels the user will have regardless of the answers given.
///
///
/// There are restrictions that apply only when onboarding is enabled, but these vary depending
/// on the current [Self::mode].
///
///
///
/// If the default mode is set, you must provide at least 7 channels, 5 of which must allow
/// @everyone to read and send messages. if advanced is set, the restrictions apply across the
/// default channels and the [Self::prompts], provided that they supply the remaining required
Expand Down Expand Up @@ -143,7 +137,7 @@ impl<'a> EditOnboarding<'a, NeedsEnabled> {

impl<'a> EditOnboarding<'a, NeedsMode> {
/// The current onboarding mode that controls where the readable channels are set.
///
///
/// If the default mode is set, you must provide at least 7 channels, 5 of which must allow
/// @everyone to read and send messages. if advanced is set, the restrictions apply across the
/// default channels and the [Self::prompts], provided that they supply the remaining required
Expand All @@ -169,20 +163,34 @@ impl<'a, Stage: Sealed> EditOnboarding<'a, Stage> {
}
}


#[cfg(feature = "http")]
#[async_trait::async_trait]
impl<'a> Builder for EditOnboarding<'a, Ready> {
type Context<'ctx> = GuildId;
type Built = Onboarding;

/// Sets [`Onboarding`] in the guild.
///
/// **Note**: Requires the [Manage Roles] and [Manage Guild] permissions.
///
/// # Errors
///
/// If the `cache` is enabled, returns a [`ModelError::InvalidPermissions`] if the current user
/// lacks permission. Otherwise returns [`Error::Http`], as well as if invalid data is given.
///
/// [Manage Roles]: Permissions::MANAGE_ROLES
/// [Manage Guild]: Permissions::MANAGE_GUILD
async fn execute(
mut self,
cache_http: impl CacheHttp,
ctx: Self::Context<'_>,
) -> Result<Self::Built> {
#[cfg(feature = "cache")]
crate::utils::user_has_guild_perms(&cache_http, ctx, Permissions::MANAGE_GUILD | Permissions::MANAGE_ROLES)?;
crate::utils::user_has_guild_perms(
&cache_http,
ctx,
Permissions::MANAGE_GUILD | Permissions::MANAGE_ROLES,
)?;

cache_http.http().set_guild_onboarding(ctx, &self, self.audit_log_reason).await
}
Expand Down
11 changes: 5 additions & 6 deletions src/builder/edit_onboarding/prompt_option_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ pub struct CreatePromptOption<Stage: Sealed> {
_stage: Stage,
}


impl Default for CreatePromptOption<NeedsChannels> {
/// See the documentation of [`Self::new`].
fn default() -> Self {
Expand All @@ -57,7 +56,7 @@ impl CreatePromptOption<NeedsChannels> {
}

/// The channels that become visible when selecting this option.
///
///
/// At least one channel or role must be selected or the option will not be valid.
pub fn channels(self, channel_ids: Vec<ChannelId>) -> CreatePromptOption<NeedsRoles> {
CreatePromptOption {
Expand All @@ -74,7 +73,7 @@ impl CreatePromptOption<NeedsChannels> {

impl CreatePromptOption<NeedsRoles> {
/// The roles granted from selecting this option.
///
///
/// At least one channel or role must be selected or the option will not be valid.
pub fn roles(self, role_ids: Vec<RoleId>) -> CreatePromptOption<NeedsTitle> {
CreatePromptOption {
Expand Down Expand Up @@ -117,7 +116,7 @@ impl<Stage: Sealed> CreatePromptOption<Stage> {
}
}

use serde::ser::{Serialize, Serializer, SerializeStruct};
use serde::ser::{Serialize, SerializeStruct, Serializer};

// This implementation allows us to put the emoji fields on without storing duplicate values.
impl<Stage: Sealed> Serialize for CreatePromptOption<Stage> {
Expand All @@ -142,10 +141,10 @@ impl<Stage: Sealed> Serialize for CreatePromptOption<Stage> {
state.serialize_field("emoji_animated", animated)?;
state.serialize_field("emoji_id", id)?;
state.serialize_field("emoji_name", name)?;
}
},
ReactionType::Unicode(name) => {
state.serialize_field("emoji_name", name)?;
}
},
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/builder/edit_onboarding/prompt_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl CreateOnboardingPrompt<NeedsPromptType> {

impl CreateOnboardingPrompt<NeedsPromptOptions> {
/// The options users can select for the prompt.
///
///
/// Each option must provide at least one role or channel.
pub fn options(
self,
Expand Down
7 changes: 6 additions & 1 deletion src/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4687,7 +4687,12 @@ impl Http {
}

/// Sets the onboarding configuration for the guild.
pub async fn set_guild_onboarding(&self, guild_id: GuildId, map: &impl serde::Serialize, audit_log_reason: Option<&str>) -> Result<Onboarding> {
pub async fn set_guild_onboarding(
&self,
guild_id: GuildId,
map: &impl serde::Serialize,
audit_log_reason: Option<&str>,
) -> Result<Onboarding> {
let body = to_vec(map)?;

self.fire(Request {
Expand Down
22 changes: 11 additions & 11 deletions src/model/guild/onboarding.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::all::ReactionType;
use serde::{Deserialize, Deserializer};
use crate::model::id::{ChannelId, GenericId, GuildId, RoleId, EmojiId};

use crate::all::ReactionType;
use crate::model::id::{ChannelId, EmojiId, GenericId, GuildId, RoleId};

/// Onboarding information for a [`Guild`].
///
Expand All @@ -15,7 +15,7 @@ pub struct Onboarding {
pub guild_id: GuildId,
/// A list of prompts associated with the onboarding process.
pub prompts: Vec<OnboardingPrompt>,
/// If onboarding is enabled, these channels will be visible by the user regardless of what
/// If onboarding is enabled, these channels will be visible by the user regardless of what
/// they select in onboarding.
pub default_channel_ids: Vec<ChannelId>,
/// Controls if onboarding is enabled, if onboarding is disabled, onboarding requirements are
Expand All @@ -26,9 +26,9 @@ pub struct Onboarding {
}

/// An onboarding prompt, otherwise known as a question.
///
///
/// At least one option is required, and there could be up to 50 options.
///
///
/// [Discord docs](https://discord.com/developers/docs/resources/guild#guild-onboarding-object).
#[derive(Debug, Clone, Deserialize, Serialize)]
#[non_exhaustive]
Expand Down Expand Up @@ -63,9 +63,9 @@ enum_number! {
}

/// An option, otherwise known as an answer, for an onboarding prompt.
///
///
/// An answer must provide at least 1 channel or role to be visible.
///
///
/// [Discord docs](https://discord.com/developers/docs/resources/guild#guild-onboarding-object-prompt-option-structure).
#[derive(Debug, Clone, Deserialize, Serialize)]
#[non_exhaustive]
Expand All @@ -88,9 +88,9 @@ pub struct OnboardingPromptOption {

enum_number! {
/// Defines the criteria used to satisfy Onboarding constraints that are required for enabling.
///
/// Currently only controls what channels count towards the constraints for enabling.
///
///
/// Currently only controls what channels count towards the constraints for enabling.
///
/// [Discord docs](https://discord.com/developers/docs/resources/guild#guild-onboarding-object-onboarding-mode).
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
#[serde(from = "u8", into = "u8")]
Expand Down Expand Up @@ -130,4 +130,4 @@ where
(None, Some(name)) => Some(ReactionType::Unicode(name)),
(None, None) => return Ok(None),
})
}
}

0 comments on commit 4a37978

Please sign in to comment.