twilight_http/request/scheduled_event/create_guild_scheduled_event/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
mod external;
mod stage_instance;
mod voice;
pub use self::{
external::CreateGuildExternalScheduledEvent,
stage_instance::CreateGuildStageInstanceScheduledEvent, voice::CreateGuildVoiceScheduledEvent,
};
use super::EntityMetadataFields;
use crate::{
client::Client,
error::Error,
request::{AuditLogReason, Request},
response::ResponseFuture,
routing::Route,
};
use serde::Serialize;
use twilight_model::{
guild::scheduled_event::{EntityType, GuildScheduledEvent, PrivacyLevel},
id::{
marker::{ChannelMarker, GuildMarker},
Id,
},
util::Timestamp,
};
use twilight_validate::request::{
audit_reason as validate_audit_reason, scheduled_event_name as validate_scheduled_event_name,
ValidationError,
};
#[derive(Serialize)]
struct CreateGuildScheduledEventFields<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
channel_id: Option<Id<ChannelMarker>>,
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
entity_metadata: Option<EntityMetadataFields<'a>>,
#[serde(skip_serializing_if = "Option::is_none")]
entity_type: Option<EntityType>,
#[serde(skip_serializing_if = "Option::is_none")]
image: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
name: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
privacy_level: Option<PrivacyLevel>,
#[serde(skip_serializing_if = "Option::is_none")]
scheduled_end_time: Option<&'a Timestamp>,
#[serde(skip_serializing_if = "Option::is_none")]
scheduled_start_time: Option<&'a Timestamp>,
}
/// Create a scheduled event in a guild.
///
/// Once a guild is selected, you must choose one of three event types to
/// create. The request builders will ensure you provide the correct data to
/// Discord. See [Discord Docs/Create Guild Schedule Event].
///
/// The name must be between 1 and 100 characters in length. For external
/// events, the location must be between 1 and 100 characters in length.
///
/// # Examples
///
/// Create an event in a stage instance:
///
/// ```no_run
/// # use twilight_http::Client;
/// use twilight_model::{guild::scheduled_event::PrivacyLevel, id::Id, util::Timestamp};
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let client = Client::new("token".to_owned());
/// let guild_id = Id::new(1);
/// let channel_id = Id::new(2);
/// let garfield_start_time = Timestamp::parse("2022-01-01T14:00:00+00:00")?;
///
/// client
/// .create_guild_scheduled_event(guild_id, PrivacyLevel::GuildOnly)
/// .stage_instance(
/// channel_id,
/// "Garfield Appreciation Hour",
/// &garfield_start_time,
/// )
/// .description("Discuss: How important is Garfield to You?")
/// .await?;
/// # Ok(()) }
/// ```
///
/// Create an external event:
///
/// ```no_run
/// # use twilight_http::Client;
/// use twilight_model::{guild::scheduled_event::PrivacyLevel, id::Id, util::Timestamp};
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let client = Client::new("token".to_owned());
/// let guild_id = Id::new(1);
/// let garfield_con_start_time = Timestamp::parse("2022-01-04T08:00:00+00:00")?;
/// let garfield_con_end_time = Timestamp::parse("2022-01-06T17:00:00+00:00")?;
///
/// client
/// .create_guild_scheduled_event(guild_id, PrivacyLevel::GuildOnly)
/// .external(
/// "Garfield Con 2022",
/// "Baltimore Convention Center",
/// &garfield_con_start_time,
/// &garfield_con_end_time,
/// )
/// .description(
/// "In a spiritual successor to BronyCon, Garfield fans from \
/// around the globe celebrate all things related to the loveable cat.",
/// )
/// .await?;
/// # Ok(()) }
/// ```
///
/// [Discord Docs/Create Guild Scheduled Event]: https://discord.com/developers/docs/resources/guild-scheduled-event#create-guild-scheduled-event
pub struct CreateGuildScheduledEvent<'a> {
guild_id: Id<GuildMarker>,
http: &'a Client,
fields: Result<CreateGuildScheduledEventFields<'a>, ValidationError>,
reason: Result<Option<&'a str>, ValidationError>,
}
impl<'a> CreateGuildScheduledEvent<'a> {
pub(crate) const fn new(
http: &'a Client,
guild_id: Id<GuildMarker>,
privacy_level: PrivacyLevel,
) -> Self {
Self {
guild_id,
http,
fields: Ok(CreateGuildScheduledEventFields {
channel_id: None,
description: None,
entity_metadata: None,
entity_type: None,
image: None,
name: None,
privacy_level: Some(privacy_level),
scheduled_end_time: None,
scheduled_start_time: None,
}),
reason: Ok(None),
}
}
/// Create an external scheduled event in a guild.
///
/// The name must be between 1 and 100 characters in length.
///
/// # Errors
///
/// Returns an error of type [`ScheduledEventName`] if the name is invalid.
///
/// [`ScheduledEventName`]: twilight_validate::request::ValidationErrorType::ScheduledEventName
pub fn external(
mut self,
name: &'a str,
location: &'a str,
scheduled_start_time: &'a Timestamp,
scheduled_end_time: &'a Timestamp,
) -> CreateGuildExternalScheduledEvent<'a> {
self.fields = self.fields.and_then(|mut fields| {
validate_scheduled_event_name(name)?;
fields.name.replace(name);
Ok(fields)
});
CreateGuildExternalScheduledEvent::new(
self,
name,
location,
scheduled_start_time,
scheduled_end_time,
)
}
/// Create a stage instance scheduled event in a guild.
///
/// The name must be between 1 and 100 characters in length.
///
/// # Errors
///
/// Returns an error of type [`ScheduledEventName`] if the name is invalid.
///
/// [`ScheduledEventName`]: twilight_validate::request::ValidationErrorType::ScheduledEventName
pub fn stage_instance(
mut self,
channel_id: Id<ChannelMarker>,
name: &'a str,
scheduled_start_time: &'a Timestamp,
) -> CreateGuildStageInstanceScheduledEvent<'a> {
self.fields = self.fields.and_then(|mut fields| {
validate_scheduled_event_name(name)?;
fields.name.replace(name);
Ok(fields)
});
CreateGuildStageInstanceScheduledEvent::new(self, channel_id, name, scheduled_start_time)
}
/// Create a voice channel scheduled event in a guild.
///
/// The name must be between 1 and 100 characters in length.
///
/// # Errors
///
/// Returns an error of type [`ScheduledEventName`] if the name is invalid.
///
/// [`ScheduledEventName`]: twilight_validate::request::ValidationErrorType::ScheduledEventName
pub fn voice(
mut self,
channel_id: Id<ChannelMarker>,
name: &'a str,
scheduled_start_time: &'a Timestamp,
) -> CreateGuildVoiceScheduledEvent<'a> {
self.fields = self.fields.and_then(|mut fields| {
validate_scheduled_event_name(name)?;
fields.name.replace(name);
Ok(fields)
});
CreateGuildVoiceScheduledEvent::new(self, channel_id, name, scheduled_start_time)
}
fn exec(self) -> ResponseFuture<GuildScheduledEvent> {
let http = self.http;
match self.try_into_request() {
Ok(request) => http.request(request),
Err(source) => ResponseFuture::error(source),
}
}
fn try_into_request(self) -> Result<Request, Error> {
let fields = self.fields.map_err(Error::validation)?;
Request::builder(&Route::CreateGuildScheduledEvent {
guild_id: self.guild_id.get(),
})
.json(&fields)
.build()
}
}
impl<'a> AuditLogReason<'a> for CreateGuildScheduledEvent<'a> {
fn reason(mut self, reason: &'a str) -> Self {
self.reason = validate_audit_reason(reason).and(Ok(Some(reason)));
self
}
}