diff --git a/DisCatSharp/Entities/Guild/Automod/AutomodRule.cs b/DisCatSharp/Entities/Guild/Automod/AutomodRule.cs index b99fc31a5..16e470be8 100644 --- a/DisCatSharp/Entities/Guild/Automod/AutomodRule.cs +++ b/DisCatSharp/Entities/Guild/Automod/AutomodRule.cs @@ -1,147 +1,150 @@ // This file is part of the DisCatSharp project, based off DSharpPlus. // // Copyright (c) 2021-2022 AITSYS // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. using System; using System.Collections.Generic; using System.Threading.Tasks; using DisCatSharp.Enums; using DisCatSharp.Exceptions; using DisCatSharp.Net.Models; using Newtonsoft.Json; namespace DisCatSharp.Entities { /// /// Represents an auto mod rule. /// public class AutomodRule : SnowflakeObject { /// /// Gets the id of the guild this rule belongs to. /// [JsonProperty("guild_id", NullValueHandling = NullValueHandling.Ignore)] public ulong GuildId { get; internal set; } /// /// Gets the name of this rule. /// [JsonProperty("name")] public string Name { get; internal set; } /// /// The id of the user who first created this rule. /// [JsonProperty("creator_id", NullValueHandling = NullValueHandling.Ignore)] public ulong CreatorId { get; internal set; } /// /// Gets the type of this rule. /// [JsonProperty("event_type")] public AutomodEventType EventType { get; internal set; } /// /// Gets the trigger type of this rule. /// [JsonProperty("trigger_type")] public AutomodTriggerType TriggerType { get; internal set; } /// /// Gets the rule trigger meta data. /// [JsonProperty("trigger_metadata", NullValueHandling = NullValueHandling.Ignore)] public AutomodTriggerMetadata TriggerMetadata { get; internal set; } /// /// The actions which will execute when the rule is triggered. /// [JsonProperty("actions")] public IReadOnlyList Actions { get; internal set; } /// /// Whether the rule is enabled. /// [JsonProperty("enabled")] public bool Enabled { get; internal set; } /// /// The role ids that should not be affected by the rule. /// Maximum of 20. /// [JsonProperty("exempt_roles", NullValueHandling = NullValueHandling.Ignore)] #pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. public IReadOnlyList? ExemptRoles { get; internal set; } #pragma warning restore CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. /// /// The channel ids that should not be affected by the rule. /// Maximum of 50. /// [JsonProperty("exempt_channels", NullValueHandling = NullValueHandling.Ignore)] #pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. public IReadOnlyList? ExemptChannels { get; internal set; } #pragma warning restore CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. [JsonIgnore] public DiscordGuild Guild => this.Discord.Guilds.TryGetValue(this.GuildId, out var guild) ? guild : null; /// /// Modifies this auto mod rule. /// /// Action to perform on this rule. /// The modified rule object. /// Thrown when the client does not have the permission. /// Thrown when the rule does not exist. /// Thrown when an invalid parameter was provided. /// Thrown when Discord is unable to process the request. public async Task ModifyAsync(Action action) { var mdl = new AutomodRuleEditModel(); action(mdl); if (mdl.TriggerMetadata.HasValue) { if ((mdl.TriggerMetadata.Value.KeywordFilter != null || mdl.TriggerMetadata.Value.RegexPatterns != null) && this.TriggerType != AutomodTriggerType.Keyword) throw new ArgumentException($"Cannot use KeywordFilter and RegexPattern for a {this.TriggerType} rule. Only {AutomodTriggerType.Keyword} is valid in this context."); else if (mdl.TriggerMetadata.Value.AllowList != null && this.TriggerType != AutomodTriggerType.KeywordPreset) throw new ArgumentException($"Cannot use AllowList for a {this.TriggerType} rule. Only {AutomodTriggerType.KeywordPreset} is valid in this context."); else if (mdl.TriggerMetadata.Value.MentionTotalLimit != null && this.TriggerType != AutomodTriggerType.MentionSpam) throw new ArgumentException($"Cannot use MentionTotalLimit for a {this.TriggerType} rule. Only {AutomodTriggerType.MentionSpam} is valid in this context."); + + if (mdl.TriggerMetadata.Value.MentionRaidProtectionEnabled != null && this.TriggerType != AutomodTriggerType.MentionSpam) + throw new ArgumentException($"Cannot use MentionRaidProtectionEnabled for a {this.TriggerType} rule. Only {AutomodTriggerType.MentionSpam} is valid in this context."); } return await this.Discord.ApiClient.ModifyAutomodRuleAsync(this.GuildId, this.Id, mdl.Name, mdl.EventType, mdl.TriggerMetadata, mdl.Actions, mdl.Enabled, mdl.ExemptRoles, mdl.ExemptChannels, mdl.AuditLogReason); } /// /// Deletes this auto mod rule. /// /// The reason for this deletion. /// Thrown when the client does not have the permission. /// Thrown when the rule does not exist. /// Thrown when an invalid parameter was provided. /// Thrown when Discord is unable to process the request. public async Task DeleteAsync(string reason = null) => await this.Discord.ApiClient.DeleteAutomodRuleAsync(this.GuildId, this.Id, reason); } } diff --git a/DisCatSharp/Entities/Guild/Automod/AutomodTriggerMetadata.cs b/DisCatSharp/Entities/Guild/Automod/AutomodTriggerMetadata.cs index 5e9bacb1d..df8714f77 100644 --- a/DisCatSharp/Entities/Guild/Automod/AutomodTriggerMetadata.cs +++ b/DisCatSharp/Entities/Guild/Automod/AutomodTriggerMetadata.cs @@ -1,79 +1,86 @@ // This file is part of the DisCatSharp project, based off DSharpPlus. // // Copyright (c) 2021-2022 AITSYS // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. using System.Collections.Generic; using System.Collections.ObjectModel; +using DisCatSharp.Attributes; using DisCatSharp.Enums; using Newtonsoft.Json; namespace DisCatSharp.Entities { /// /// Represents the rule's meta data. /// public class AutomodTriggerMetadata { /// /// The substrings which will be searched for in content. Maximum of 1000. /// A keyword can be a phrase which contains multiple words. Wildcard symbols (not available to allow lists) can be used to customize how each keyword will be matched. /// See keyword matching strategies. Each keyword must be 30 characters or less. /// [JsonProperty("keyword_filter", NullValueHandling = NullValueHandling.Ignore)] #pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. public ReadOnlyCollection? KeywordFilter { get; set; } #pragma warning restore CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. /// /// The internally predefined word-sets which will be searched for in content. /// [JsonProperty("presets", NullValueHandling = NullValueHandling.Ignore)] #pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. public IReadOnlyList? Presets { get; internal set; } #pragma warning restore CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. /// /// The regex patterns the content will be checked against. Maximum of 10. /// Only Rust flavored regex is currently supported, which can be tested in online editors such as Rustexp. /// Each regex pattern must be 75 characters or less. /// [JsonProperty("regex_patterns", NullValueHandling = NullValueHandling.Ignore)] #pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. public List? RegexPatterns { get; set; } #pragma warning restore CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. /// /// The substrings which will be exempt from triggering the preset type. Maximum of 1000. /// [JsonProperty("allow_list", NullValueHandling = NullValueHandling.Ignore)] #pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. public List? AllowList { get; set; } #pragma warning restore CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. /// /// The total number of unique role and user mentions allowed per message. Maximum of 50. /// [JsonProperty("mention_total_limit", NullValueHandling = NullValueHandling.Ignore)] public int? MentionTotalLimit { get; set; } = null; + + /// + /// Whether to automatically detect mention raids. + /// + [JsonProperty("mention_raid_protection_enabled", NullValueHandling = NullValueHandling.Ignore), DiscordInExperiment] + public bool? MentionRaidProtectionEnabled { get; set; } = null; } }