#[cfg(feature = "http")]
use super::Builder;
#[cfg(feature = "http")]
use crate::http::CacheHttp;
#[cfg(feature = "http")]
use crate::internal::prelude::*;
use crate::model::guild::automod::EventType;
use crate::model::prelude::*;
#[derive(Clone, Debug, Serialize)]
#[must_use]
pub struct EditAutoModRule<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
name: Option<String>,
event_type: EventType,
#[serde(flatten, skip_serializing_if = "Option::is_none")]
trigger: Option<Trigger>,
#[serde(skip_serializing_if = "Option::is_none")]
actions: Option<Vec<Action>>,
#[serde(skip_serializing_if = "Option::is_none")]
enabled: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
exempt_roles: Option<Vec<RoleId>>,
#[serde(skip_serializing_if = "Option::is_none")]
exempt_channels: Option<Vec<ChannelId>>,
#[serde(skip)]
audit_log_reason: Option<&'a str>,
}
impl<'a> EditAutoModRule<'a> {
pub fn new() -> Self {
Self::default()
}
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn event_type(mut self, event_type: EventType) -> Self {
self.event_type = event_type;
self
}
pub fn trigger(mut self, trigger: Trigger) -> Self {
self.trigger = Some(trigger);
self
}
pub fn actions(mut self, actions: Vec<Action>) -> Self {
self.actions = Some(actions);
self
}
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = Some(enabled);
self
}
pub fn exempt_roles(mut self, roles: impl IntoIterator<Item = impl Into<RoleId>>) -> Self {
self.exempt_roles = Some(roles.into_iter().map(Into::into).collect());
self
}
pub fn exempt_channels(
mut self,
channels: impl IntoIterator<Item = impl Into<ChannelId>>,
) -> Self {
self.exempt_channels = Some(channels.into_iter().map(Into::into).collect());
self
}
pub fn audit_log_reason(mut self, reason: &'a str) -> Self {
self.audit_log_reason = Some(reason);
self
}
}
impl Default for EditAutoModRule<'_> {
fn default() -> Self {
Self {
name: None,
trigger: None,
actions: None,
enabled: None,
exempt_roles: None,
exempt_channels: None,
event_type: EventType::MessageSend,
audit_log_reason: None,
}
}
}
#[cfg(feature = "http")]
#[async_trait::async_trait]
impl Builder for EditAutoModRule<'_> {
type Context<'ctx> = (GuildId, Option<RuleId>);
type Built = Rule;
async fn execute(
self,
cache_http: impl CacheHttp,
ctx: Self::Context<'_>,
) -> Result<Self::Built> {
let http = cache_http.http();
match ctx.1 {
Some(id) => http.edit_automod_rule(ctx.0, id, &self, self.audit_log_reason).await,
None => http.create_automod_rule(ctx.0, &self, self.audit_log_reason).await,
}
}
}