[Feature] AutoMod support (#2578)

* initial implementation

* update models

* somewhat working auto mod action executed event

* made some properties optional

* comments, rest entity, guild methods

* add placeholder methods

* started working on rule cache

* working events

* started working on rule builder

* working state

* fix null issue

* commentsssss

* public automod rules collection in a socketgulild

* forgot nullability

* update limits

* add Download func to cacheable user

* Apply suggestions from code review

* Update src/Discord.Net.Rest/DiscordRestApiClient.cs

* missing xml doc

* reworkkkk

* fix the `;` lol

---------

Co-authored-by: Quin Lynch <lynchquin@gmail.com>
Co-authored-by: Casmir <68127614+csmir@users.noreply.github.com>
This commit is contained in:
Misha133
2023-02-16 19:08:47 +03:00
committed by GitHub
parent 0c27395efd
commit 673b02dd36
26 changed files with 1478 additions and 1 deletions

View File

@@ -0,0 +1,86 @@
using Discord.Rest;
namespace Discord.WebSocket;
public class AutoModActionExecutedData
{
/// <summary>
/// Gets the id of the rule which action belongs to.
/// </summary>
public Cacheable<IAutoModRule, ulong> Rule { get; }
/// <summary>
/// Gets the trigger type of rule which was triggered.
/// </summary>
public AutoModTriggerType TriggerType { get; }
/// <summary>
/// Gets the user which generated the content which triggered the rule.
/// </summary>
public Cacheable<SocketGuildUser, ulong> User { get; }
/// <summary>
/// Gets the channel in which user content was posted.
/// </summary>
public Cacheable<ISocketMessageChannel, ulong> Channel { get; }
/// <summary>
/// Gets the message that triggered the action.
/// </summary>
/// <remarks>
/// This property will be <see langword="null"/> if the message was blocked by the automod.
/// </remarks>
public Cacheable<IUserMessage, ulong>? Message { get; }
/// <summary>
/// Gets the id of the system auto moderation messages posted as a result of this action.
/// </summary>
/// <remarks>
/// This property will be <see langword="null"/> if this event does not correspond to an action
/// with type <see cref="AutoModActionType.SendAlertMessage"/>.
/// </remarks>
public ulong AlertMessageId { get; }
/// <summary>
/// Gets the user-generated text content.
/// </summary>
/// <remarks>
/// This property will be empty if <see cref="GatewayIntents.MessageContent"/> is disabled.
/// </remarks>
public string Content { get; }
/// <summary>
/// Gets the substring in content that triggered the rule.
/// </summary>
/// <remarks>
/// This property will be empty if <see cref="GatewayIntents.MessageContent"/> is disabled.
/// </remarks>
public string MatchedContent { get; }
/// <summary>
/// Gets the word or phrase configured in the rule that triggered the rule.
/// </summary>
public string MatchedKeyword { get; }
internal AutoModActionExecutedData(Cacheable<IAutoModRule, ulong> rule,
AutoModTriggerType triggerType,
Cacheable<SocketGuildUser, ulong> user,
Cacheable<ISocketMessageChannel, ulong> channel,
Cacheable<IUserMessage, ulong>? message,
ulong alertMessageId,
string content,
string matchedContent,
string matchedKeyword
)
{
Rule = rule;
TriggerType = triggerType;
User = user;
Channel = channel;
Message = message;
AlertMessageId = alertMessageId;
Content = content;
MatchedContent = matchedContent;
MatchedKeyword = matchedKeyword;
}
}

View File

@@ -0,0 +1,122 @@
using Discord.Rest;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using Model = Discord.API.AutoModerationRule;
namespace Discord.WebSocket
{
public class SocketAutoModRule : SocketEntity<ulong>, IAutoModRule
{
/// <summary>
/// Gets the guild that this rule is in.
/// </summary>
public SocketGuild Guild { get; }
/// <inheritdoc/>
public string Name { get; private set; }
/// <summary>
/// Gets the creator of this rule.
/// </summary>
public SocketGuildUser Creator { get; private set; }
/// <inheritdoc/>
public AutoModEventType EventType { get; private set; }
/// <inheritdoc/>
public AutoModTriggerType TriggerType { get; private set; }
/// <inheritdoc/>
public IReadOnlyCollection<string> KeywordFilter { get; private set; }
/// <inheritdoc/>
public IReadOnlyCollection<string> RegexPatterns { get; private set; }
/// <inheritdoc/>
public IReadOnlyCollection<string> AllowList { get; private set; }
/// <inheritdoc/>
public IReadOnlyCollection<KeywordPresetTypes> Presets { get; private set; }
/// <inheritdoc/>
public IReadOnlyCollection<AutoModRuleAction> Actions { get; private set; }
/// <inheritdoc/>
public int? MentionTotalLimit { get; private set; }
/// <inheritdoc/>
public bool Enabled { get; private set; }
/// <summary>
/// Gets the roles that are exempt from this rule.
/// </summary>
public IReadOnlyCollection<SocketRole> ExemptRoles { get; private set; }
/// <summary>
/// Gets the channels that are exempt from this rule.
/// </summary>
public IReadOnlyCollection<SocketGuildChannel> ExemptChannels { get; private set; }
/// <inheritdoc/>
public DateTimeOffset CreatedAt
=> SnowflakeUtils.FromSnowflake(Id);
private ulong _creatorId;
internal SocketAutoModRule(DiscordSocketClient discord, ulong id, SocketGuild guild)
: base(discord, id)
{
Guild = guild;
}
internal static SocketAutoModRule Create(DiscordSocketClient discord, SocketGuild guild, Model model)
{
var entity = new SocketAutoModRule(discord, model.Id, guild);
entity.Update(model);
return entity;
}
internal void Update(Model model)
{
Name = model.Name;
_creatorId = model.CreatorId;
Creator ??= Guild.GetUser(_creatorId);
EventType = model.EventType;
TriggerType = model.TriggerType;
KeywordFilter = model.TriggerMetadata.KeywordFilter.GetValueOrDefault(Array.Empty<string>()).ToImmutableArray();
Presets = model.TriggerMetadata.Presets.GetValueOrDefault(Array.Empty<KeywordPresetTypes>()).ToImmutableArray();
RegexPatterns = model.TriggerMetadata.RegexPatterns.GetValueOrDefault(Array.Empty<string>()).ToImmutableArray();
AllowList = model.TriggerMetadata.AllowList.GetValueOrDefault(Array.Empty<string>()).ToImmutableArray();
MentionTotalLimit = model.TriggerMetadata.MentionLimit.IsSpecified
? model.TriggerMetadata.MentionLimit.Value
: null;
Actions = model.Actions.Select(x => new AutoModRuleAction(x.Type, x.Metadata.GetValueOrDefault()?.ChannelId.ToNullable(), x.Metadata.GetValueOrDefault()?.DurationSeconds.ToNullable())).ToImmutableArray();
Enabled = model.Enabled;
ExemptRoles = model.ExemptRoles.Select(x => Guild.GetRole(x)).ToImmutableArray();
ExemptChannels = model.ExemptChannels.Select(x => Guild.GetChannel(x)).ToImmutableArray();
}
/// <inheritdoc/>
public async Task ModifyAsync(Action<AutoModRuleProperties> func, RequestOptions options = null)
{
var model = await GuildHelper.ModifyRuleAsync(Discord, this, func, options);
Guild.AddOrUpdateAutoModRule(model);
}
/// <inheritdoc/>
public Task DeleteAsync(RequestOptions options = null)
=> GuildHelper.DeleteRuleAsync(Discord, this, options);
internal SocketAutoModRule Clone() => MemberwiseClone() as SocketAutoModRule;
#region IAutoModRule
IReadOnlyCollection<ulong> IAutoModRule.ExemptRoles => ExemptRoles.Select(x => x.Id).ToImmutableArray();
IReadOnlyCollection<ulong> IAutoModRule.ExemptChannels => ExemptChannels.Select(x => x.Id).ToImmutableArray();
ulong IAutoModRule.GuildId => Guild.Id;
ulong IAutoModRule.CreatorId => _creatorId;
#endregion
}
}

View File

@@ -11,6 +11,7 @@ using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AutoModRuleModel = Discord.API.AutoModerationRule;
using ChannelModel = Discord.API.Channel;
using EmojiUpdateModel = Discord.API.Gateway.GuildEmojiUpdateEvent;
using EventModel = Discord.API.GuildScheduledEvent;
@@ -43,6 +44,7 @@ namespace Discord.WebSocket
private ConcurrentDictionary<ulong, SocketVoiceState> _voiceStates;
private ConcurrentDictionary<ulong, SocketCustomSticker> _stickers;
private ConcurrentDictionary<ulong, SocketGuildEvent> _events;
private ConcurrentDictionary<ulong, SocketAutoModRule> _automodRules;
private ImmutableArray<GuildEmote> _emotes;
private AudioClient _audioClient;
@@ -391,6 +393,7 @@ namespace Discord.WebSocket
{
_audioLock = new SemaphoreSlim(1, 1);
_emotes = ImmutableArray.Create<GuildEmote>();
_automodRules = new ConcurrentDictionary<ulong, SocketAutoModRule>();
}
internal static SocketGuild Create(DiscordSocketClient discord, ClientState state, ExtendedModel model)
{
@@ -1809,6 +1812,78 @@ namespace Discord.WebSocket
internal SocketGuild Clone() => MemberwiseClone() as SocketGuild;
#endregion
#region AutoMod
internal SocketAutoModRule AddOrUpdateAutoModRule(AutoModRuleModel model)
{
if (_automodRules.TryGetValue(model.Id, out var rule))
{
rule.Update(model);
return rule;
}
var socketRule = SocketAutoModRule.Create(Discord, this, model);
_automodRules.TryAdd(model.Id, socketRule);
return socketRule;
}
/// <summary>
/// Gets a single rule configured in a guild from cache. Returns <see langword="null"/> if the rule was not found.
/// </summary>
public SocketAutoModRule GetAutoModRule(ulong id)
{
return _automodRules.TryGetValue(id, out var rule) ? rule : null;
}
internal SocketAutoModRule RemoveAutoModRule(ulong id)
{
return _automodRules.TryRemove(id, out var rule) ? rule : null;
}
internal SocketAutoModRule RemoveAutoModRule(AutoModRuleModel model)
{
if (_automodRules.TryRemove(model.Id, out var rule))
{
rule.Update(model);
}
return rule ?? SocketAutoModRule.Create(Discord, this, model);
}
/// <inheritdoc cref="IGuild.GetAutoModRuleAsync"/>
public async Task<SocketAutoModRule> GetAutoModRuleAsync(ulong ruleId, RequestOptions options = null)
{
var rule = await GuildHelper.GetAutoModRuleAsync(ruleId, this, Discord, options);
return AddOrUpdateAutoModRule(rule);
}
/// <inheritdoc cref="IGuild.GetAutoModRulesAsync"/>
public async Task<SocketAutoModRule[]> GetAutoModRulesAsync(RequestOptions options = null)
{
var rules = await GuildHelper.GetAutoModRulesAsync(this, Discord, options);
return rules.Select(AddOrUpdateAutoModRule).ToArray();
}
/// <inheritdoc cref="IGuild.CreateAutoModRuleAsync"/>
public async Task<SocketAutoModRule> CreateAutoModRuleAsync(Action<AutoModRuleProperties> props, RequestOptions options = null)
{
var rule = await GuildHelper.CreateAutoModRuleAsync(this, props, Discord, options);
return AddOrUpdateAutoModRule(rule);
}
/// <summary>
/// Gets the auto moderation rules defined in this guild.
/// </summary>
/// <remarks>
/// This property may not always return all auto moderation rules if they haven't been cached.
/// </remarks>
public IReadOnlyCollection<SocketAutoModRule> AutoModRules => _automodRules.ToReadOnlyCollection();
#endregion
#region IGuild
/// <inheritdoc />
ulong? IGuild.AFKChannelId => AFKChannelId;
@@ -2053,6 +2128,19 @@ namespace Discord.WebSocket
_audioLock?.Dispose();
_audioClient?.Dispose();
}
/// <inheritdoc/>
async Task<IAutoModRule> IGuild.GetAutoModRuleAsync(ulong ruleId, RequestOptions options)
=> await GetAutoModRuleAsync(ruleId, options).ConfigureAwait(false);
/// <inheritdoc/>
async Task<IAutoModRule[]> IGuild.GetAutoModRulesAsync(RequestOptions options)
=> await GetAutoModRulesAsync(options).ConfigureAwait(false);
/// <inheritdoc/>
async Task<IAutoModRule> IGuild.CreateAutoModRuleAsync(Action<AutoModRuleProperties> props, RequestOptions options)
=> await CreateAutoModRuleAsync(props, options).ConfigureAwait(false);
#endregion
}
}