AutoMod custom block message (#2613)

This commit is contained in:
Misha133
2023-02-26 22:47:13 +03:00
committed by GitHub
parent 11481a159e
commit 709364aaef
8 changed files with 64 additions and 14 deletions

View File

@@ -21,16 +21,23 @@ namespace Discord
/// </summary>
public ulong? ChannelId { get; }
/// <summary>
/// Gets the custom message that will be shown to members whenever their message is blocked.
/// <see langword="null"/> if no message has been set.
/// </summary>
public Optional<string> CustomMessage { get; set; }
/// <summary>
/// Gets the duration of which a user will be timed out for breaking this rule. <see langword="null"/> if no timeout duration has been provided.
/// </summary>
public TimeSpan? TimeoutDuration { get; }
internal AutoModRuleAction(AutoModActionType type, ulong? channelId, int? duration)
internal AutoModRuleAction(AutoModActionType type, ulong? channelId, int? duration, string customMessage)
{
Type = type;
ChannelId = channelId;
TimeoutDuration = duration.HasValue ? TimeSpan.FromSeconds(duration.Value) : null;
CustomMessage = customMessage;
}
}
}

View File

@@ -66,6 +66,12 @@ namespace Discord
/// </summary>
public const int MaxTimeoutSeconds = 2419200;
/// <summary>
/// Returns the max custom message length AutoMod rule action allowed by Discord.
/// </summary>
public const int MaxCustomBlockMessageLength = 50;
/// <summary>
/// Gets or sets the name for the rule.
/// </summary>
@@ -146,6 +152,11 @@ namespace Discord
/// Gets or sets the duration of which a user will be timed out for breaking this rule.
/// </summary>
public TimeSpan? TimeoutDuration { get; set; }
/// <summary>
/// Gets or sets the custom message that will be shown to members whenever their message is blocked.
/// </summary>
public Optional<string> CustomMessage { get; set; }
}
}

View File

@@ -14,5 +14,8 @@ namespace Discord.API
[JsonProperty("duration_seconds")]
public Optional<int> DurationSeconds { get; set; }
[JsonProperty("custom_message")]
public Optional<string> CustomMessage { get; set; }
}
}

View File

@@ -1071,6 +1071,8 @@ namespace Discord.Rest
var args = new AutoModRuleProperties();
func(args);
#region Validations
if (!args.TriggerType.IsSpecified)
throw new ArgumentException(message: $"AutoMod rule must have a specified type.", paramName: nameof(args.TriggerType));
@@ -1079,8 +1081,6 @@ namespace Discord.Rest
Preconditions.AtLeast(1, args.Actions.GetValueOrDefault(Array.Empty<AutoModRuleActionProperties>()).Length, nameof(args.Actions), "Auto moderation rule must have at least 1 action");
#region Keyword Validations
if (args.RegexPatterns.IsSpecified)
{
if (args.TriggerType.Value is not AutoModTriggerType.Keyword)
@@ -1125,8 +1125,6 @@ namespace Discord.Rest
if (args.TriggerType.Value is not AutoModTriggerType.KeywordPreset && args.Presets.IsSpecified)
throw new ArgumentException(message: $"Keyword presets scan only be used with 'KeywordPreset' trigger type.", paramName: nameof(args.Presets));
#endregion
if (args.MentionLimit.IsSpecified)
{
if (args.TriggerType.Value is AutoModTriggerType.MentionSpam)
@@ -1154,6 +1152,11 @@ namespace Discord.Rest
if (args.Actions.Value.Any(x => x.TimeoutDuration.GetValueOrDefault().TotalSeconds > AutoModRuleProperties.MaxTimeoutSeconds))
throw new ArgumentException(message: $"Field count must be less than or equal to {AutoModRuleProperties.MaxTimeoutSeconds}.", paramName: nameof(AutoModRuleActionProperties.TimeoutDuration));
if (args.Actions.Value.Any(x => x.CustomMessage.IsSpecified && x.CustomMessage.Value.Length > AutoModRuleProperties.MaxCustomBlockMessageLength))
throw new ArgumentException(message: $"Custom message length must be less than or equal to {AutoModRuleProperties.MaxCustomBlockMessageLength}.", paramName: nameof(AutoModRuleActionProperties.CustomMessage));
#endregion
var props = new CreateAutoModRuleParams
{
EventType = args.EventType.GetValueOrDefault(AutoModEventType.MessageSend),
@@ -1167,7 +1170,8 @@ namespace Discord.Rest
Metadata = new ActionMetadata
{
ChannelId = x.ChannelId ?? Optional<ulong>.Unspecified,
DurationSeconds = (int?)x.TimeoutDuration?.TotalSeconds ?? Optional<int>.Unspecified
DurationSeconds = (int?)x.TimeoutDuration?.TotalSeconds ?? Optional<int>.Unspecified,
CustomMessage = x.CustomMessage,
},
Type = x.Type
}).ToArray(),
@@ -1203,7 +1207,8 @@ namespace Discord.Rest
Metadata = x.ChannelId.HasValue || x.TimeoutDuration.HasValue ? new API.ActionMetadata
{
ChannelId = x.ChannelId ?? Optional<ulong>.Unspecified,
DurationSeconds = x.TimeoutDuration.HasValue ? (int)Math.Floor(x.TimeoutDuration.Value.TotalSeconds) : Optional<int>.Unspecified
DurationSeconds = x.TimeoutDuration.HasValue ? (int)Math.Floor(x.TimeoutDuration.Value.TotalSeconds) : Optional<int>.Unspecified,
CustomMessage = x.CustomMessage,
} : Optional<API.ActionMetadata>.Unspecified
}).ToArray() : Optional<API.AutoModAction[]>.Unspecified,
Enabled = args.Enabled,

View File

@@ -82,7 +82,16 @@ public class RestAutoModRule : RestEntity<ulong>, IAutoModRule
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();
Actions = model.Actions.Select(x => new AutoModRuleAction(
x.Type,
x.Metadata.GetValueOrDefault()?.ChannelId.ToNullable(),
x.Metadata.GetValueOrDefault()?.DurationSeconds.ToNullable(),
x.Metadata.IsSpecified
? x.Metadata.Value.CustomMessage.IsSpecified
? x.Metadata.Value.CustomMessage.Value
: null
: null
)).ToImmutableArray();
Enabled = model.Enabled;
ExemptRoles = model.ExemptRoles.ToImmutableArray();
ExemptChannels = model.ExemptChannels.ToImmutableArray();

View File

@@ -2942,8 +2942,14 @@ namespace Discord.WebSocket
? data.Action.Metadata.Value.DurationSeconds.IsSpecified
? data.Action.Metadata.Value.DurationSeconds.Value
: null
: null,
data.Action.Metadata.IsSpecified
? data.Action.Metadata.Value.CustomMessage.IsSpecified
? data.Action.Metadata.Value.CustomMessage.Value
: null
: null);
var member = guild.GetUser(data.UserId);
var cacheableUser = new Cacheable<SocketGuildUser, ulong>(member,
@@ -2965,7 +2971,7 @@ namespace Discord.WebSocket
channel != null,
async () =>
{
if(data.ChannelId.IsSpecified)
if (data.ChannelId.IsSpecified)
return await GetChannelAsync(data.ChannelId.Value).ConfigureAwait(false) as ISocketMessageChannel;
return null;
});
@@ -2980,7 +2986,7 @@ namespace Discord.WebSocket
cachedMsg is not null,
async () =>
{
if(data.MessageId.IsSpecified)
if (data.MessageId.IsSpecified)
return (await channel!.GetMessageAsync(data.MessageId.Value).ConfigureAwait(false)) as IUserMessage;
return null;
});

View File

@@ -93,7 +93,16 @@ namespace Discord.WebSocket
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();
Actions = model.Actions.Select(x => new AutoModRuleAction(
x.Type,
x.Metadata.GetValueOrDefault()?.ChannelId.ToNullable(),
x.Metadata.GetValueOrDefault()?.DurationSeconds.ToNullable(),
x.Metadata.IsSpecified
? x.Metadata.Value.CustomMessage.IsSpecified
? x.Metadata.Value.CustomMessage.Value
: null
: null
)).ToImmutableArray();
Enabled = model.Enabled;
ExemptRoles = model.ExemptRoles.Select(x => Guild.GetRole(x)).ToImmutableArray();
ExemptChannels = model.ExemptChannels.Select(x => Guild.GetChannel(x)).ToImmutableArray();