Skip to content

Commit

Permalink
Fix clippy warnings and apply next patches to new code (serenity-rs…
Browse files Browse the repository at this point in the history
…#2061)

Co-authored-by: Michael Krasnitski <42564254+mkrasnitski@users.noreply.github.com>
  • Loading branch information
2 people authored and arqunis committed Oct 24, 2023
1 parent 7066d77 commit fe31763
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 99 deletions.
128 changes: 42 additions & 86 deletions src/builder/edit_automod_rule.rs
Original file line number Diff line number Diff line change
@@ -1,133 +1,89 @@
use std::collections::HashMap;

use crate::json::{json, Value};
use crate::model::guild::automod::{Action, EventType, Trigger};
use crate::model::id::{ChannelId, RoleId};

#[derive(Clone, Debug)]
pub struct EditAutoModRule(pub HashMap<&'static str, Value>);
#[derive(Clone, Debug, Serialize)]
pub struct EditAutoModRule {
event_type: EventType,
#[serde(skip_serializing_if = "Option::is_none")]
name: Option<String>,
#[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>>,
}

impl EditAutoModRule {
/// The display name of the rule.
pub fn name<D: ToString>(&mut self, name: D) -> &mut Self {
self.0.insert("name", Value::from(name.to_string()));

pub fn name(&mut self, name: impl Into<String>) -> &mut Self {
self.name = Some(name.into());
self
}

/// Set the event context the rule should be checked.
pub fn event_type(&mut self, event_type: EventType) -> &mut Self {
self.0.insert("event_type", u8::from(event_type).into());

self.event_type = event_type;
self
}

/// Set the type of content which can trigger the rule.
///
/// **None**: The trigger type can't be edited after creation. Only its values.
pub fn trigger(&mut self, trigger: Trigger) -> &mut Self {
self.0.insert("trigger_type", u8::from(trigger.kind()).into());

match trigger {
Trigger::Keyword(keyword_filter) => {
let value = json!({
"keyword_filter": keyword_filter,
});
self.0.insert("trigger_metadata", value);
},
Trigger::KeywordPreset(presets) => {
let value = json!({
"presets": presets,
});
self.0.insert("trigger_metadata", value);
},
_ => {},
}

self.trigger = Some(trigger);
self
}

/// Set the actions which will execute when the rule is triggered.
pub fn actions<I>(&mut self, actions: I) -> &mut Self
where
I: IntoIterator<Item = Action>,
{
let actions = actions
.into_iter()
.map(|action| {
let kind = action.kind();
match action {
Action::Alert(channel_id) => {
json!({
"type": kind,
"metadata": {
"channel_id": channel_id.0.to_string(),
},
})
},
Action::Timeout(duration) => {
json!({
"type": kind,
"metadata": {
"duration_seconds": duration,
},
})
},
Action::BlockMessage | Action::Unknown(_) => {
json!({
"type": kind,
})
},
}
})
.collect();

self.0.insert("actions", actions);

pub fn actions(&mut self, actions: Vec<Action>) -> &mut Self {
self.actions = Some(actions);
self
}

/// Set whether the rule is enabled.
pub fn enabled(&mut self, enabled: bool) -> &mut Self {
self.0.insert("enabled", Value::from(enabled));

self.enabled = Some(enabled);
self
}

/// Set roles that should not be affected by the rule.
///
/// Maximum of 20.
pub fn exempt_roles<I>(&mut self, roles: I) -> &mut Self
where
I: IntoIterator<Item = RoleId>,
{
let ids = roles.into_iter().map(|id| id.0.to_string()).collect();

self.0.insert("exempt_roles", ids);

pub fn exempt_roles(
&mut self,
roles: impl IntoIterator<Item = impl Into<RoleId>>,
) -> &mut Self {
self.exempt_roles = Some(roles.into_iter().map(Into::into).collect());
self
}

/// Set channels that should not be affected by the rule.
///
/// Maximum of 50.
pub fn exempt_channels<I>(&mut self, channels: I) -> &mut Self
where
I: IntoIterator<Item = ChannelId>,
{
let ids = channels.into_iter().map(|id| id.0.to_string()).collect();

self.0.insert("exempt_channels", ids);

pub fn exempt_channels(
&mut self,
channels: impl IntoIterator<Item = impl Into<ChannelId>>,
) -> &mut Self {
self.exempt_channels = Some(channels.into_iter().map(Into::into).collect());
self
}
}

impl Default for EditAutoModRule {
fn default() -> Self {
let mut builder = Self(HashMap::new());
builder.event_type(EventType::MessageSend);

builder
Self {
name: None,
trigger: None,
actions: None,
enabled: None,
exempt_roles: None,
exempt_channels: None,
event_type: EventType::MessageSend,
}
}
}
12 changes: 8 additions & 4 deletions src/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2444,8 +2444,12 @@ impl Http {
/// Creates an auto moderation rule in a guild.
///
/// This method requires `MANAGE_GUILD` permissions.
pub async fn create_automod_rule(&self, guild_id: u64, map: &JsonMap) -> Result<Rule> {
let body = to_vec(&map)?;
pub async fn create_automod_rule(
&self,
guild_id: u64,
map: &impl serde::Serialize,
) -> Result<Rule> {
let body = to_vec(map)?;

self.fire(Request {
body: Some(body),
Expand All @@ -2465,9 +2469,9 @@ impl Http {
&self,
guild_id: u64,
rule_id: u64,
map: &JsonMap,
map: &impl serde::Serialize,
) -> Result<Rule> {
let body = to_vec(&map)?;
let body = to_vec(map)?;

self.fire(Request {
body: Some(body),
Expand Down
3 changes: 1 addition & 2 deletions src/model/application/interaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ impl Interaction {
pub fn app_permissions(&self) -> Option<Permissions> {
match self {
Self::Ping(_) => None,
Self::ApplicationCommand(i) => i.app_permissions,
Self::ApplicationCommand(i) | Self::Autocomplete(i) => i.app_permissions,
Self::MessageComponent(i) => i.app_permissions,
Self::Autocomplete(i) => i.app_permissions,
Self::ModalSubmit(i) => i.app_permissions,
}
}
Expand Down
9 changes: 2 additions & 7 deletions src/model/guild/guild_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ use crate::collector::{MessageCollectorBuilder, ReactionCollectorBuilder};
use crate::http::{CacheHttp, Http, UserPagination};
#[cfg(feature = "model")]
use crate::internal::prelude::*;
use crate::json;
#[cfg(feature = "model")]
use crate::json::json;
#[cfg(feature = "model")]
Expand Down Expand Up @@ -116,9 +115,7 @@ impl GuildId {
let mut builder = EditAutoModRule::default();
f(&mut builder);

let map = json::hashmap_to_json_map(builder.0);

http.as_ref().create_automod_rule(self.get(), &map).await
http.as_ref().create_automod_rule(self.get(), &builder).await
}

/// Edit an auto moderation [`Rule`] by its ID.
Expand All @@ -141,9 +138,7 @@ impl GuildId {
let mut builder = EditAutoModRule::default();
f(&mut builder);

let map = json::hashmap_to_json_map(builder.0);

http.as_ref().edit_automod_rule(self.get(), rule_id.into().get(), &map).await
http.as_ref().edit_automod_rule(self.get(), rule_id.into().get(), &builder).await
}

/// Deletes an auto moderation [`Rule`] from the guild.
Expand Down

0 comments on commit fe31763

Please sign in to comment.