AutoMod custom block message (#2613)
This commit is contained in:
@@ -21,16 +21,23 @@ namespace Discord
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public ulong? ChannelId { get; }
|
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>
|
/// <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.
|
/// 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>
|
/// </summary>
|
||||||
public TimeSpan? TimeoutDuration { get; }
|
public TimeSpan? TimeoutDuration { get; }
|
||||||
|
|
||||||
internal AutoModRuleAction(AutoModActionType type, ulong? channelId, int? duration)
|
internal AutoModRuleAction(AutoModActionType type, ulong? channelId, int? duration, string customMessage)
|
||||||
{
|
{
|
||||||
Type = type;
|
Type = type;
|
||||||
ChannelId = channelId;
|
ChannelId = channelId;
|
||||||
TimeoutDuration = duration.HasValue ? TimeSpan.FromSeconds(duration.Value) : null;
|
TimeoutDuration = duration.HasValue ? TimeSpan.FromSeconds(duration.Value) : null;
|
||||||
|
CustomMessage = customMessage;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,6 +66,12 @@ namespace Discord
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public const int MaxTimeoutSeconds = 2419200;
|
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>
|
/// <summary>
|
||||||
/// Gets or sets the name for the rule.
|
/// Gets or sets the name for the rule.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -146,6 +152,11 @@ namespace Discord
|
|||||||
/// Gets or sets the duration of which a user will be timed out for breaking this rule.
|
/// Gets or sets the duration of which a user will be timed out for breaking this rule.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public TimeSpan? TimeoutDuration { get; set; }
|
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; }
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,5 +14,8 @@ namespace Discord.API
|
|||||||
|
|
||||||
[JsonProperty("duration_seconds")]
|
[JsonProperty("duration_seconds")]
|
||||||
public Optional<int> DurationSeconds { get; set; }
|
public Optional<int> DurationSeconds { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("custom_message")]
|
||||||
|
public Optional<string> CustomMessage { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1071,6 +1071,8 @@ namespace Discord.Rest
|
|||||||
var args = new AutoModRuleProperties();
|
var args = new AutoModRuleProperties();
|
||||||
func(args);
|
func(args);
|
||||||
|
|
||||||
|
#region Validations
|
||||||
|
|
||||||
if (!args.TriggerType.IsSpecified)
|
if (!args.TriggerType.IsSpecified)
|
||||||
throw new ArgumentException(message: $"AutoMod rule must have a specified type.", paramName: nameof(args.TriggerType));
|
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");
|
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.RegexPatterns.IsSpecified)
|
||||||
{
|
{
|
||||||
if (args.TriggerType.Value is not AutoModTriggerType.Keyword)
|
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)
|
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));
|
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.MentionLimit.IsSpecified)
|
||||||
{
|
{
|
||||||
if (args.TriggerType.Value is AutoModTriggerType.MentionSpam)
|
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))
|
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));
|
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
|
var props = new CreateAutoModRuleParams
|
||||||
{
|
{
|
||||||
EventType = args.EventType.GetValueOrDefault(AutoModEventType.MessageSend),
|
EventType = args.EventType.GetValueOrDefault(AutoModEventType.MessageSend),
|
||||||
@@ -1167,7 +1170,8 @@ namespace Discord.Rest
|
|||||||
Metadata = new ActionMetadata
|
Metadata = new ActionMetadata
|
||||||
{
|
{
|
||||||
ChannelId = x.ChannelId ?? Optional<ulong>.Unspecified,
|
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
|
Type = x.Type
|
||||||
}).ToArray(),
|
}).ToArray(),
|
||||||
@@ -1203,7 +1207,8 @@ namespace Discord.Rest
|
|||||||
Metadata = x.ChannelId.HasValue || x.TimeoutDuration.HasValue ? new API.ActionMetadata
|
Metadata = x.ChannelId.HasValue || x.TimeoutDuration.HasValue ? new API.ActionMetadata
|
||||||
{
|
{
|
||||||
ChannelId = x.ChannelId ?? Optional<ulong>.Unspecified,
|
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
|
} : Optional<API.ActionMetadata>.Unspecified
|
||||||
}).ToArray() : Optional<API.AutoModAction[]>.Unspecified,
|
}).ToArray() : Optional<API.AutoModAction[]>.Unspecified,
|
||||||
Enabled = args.Enabled,
|
Enabled = args.Enabled,
|
||||||
|
|||||||
@@ -82,7 +82,16 @@ public class RestAutoModRule : RestEntity<ulong>, IAutoModRule
|
|||||||
MentionTotalLimit = model.TriggerMetadata.MentionLimit.IsSpecified
|
MentionTotalLimit = model.TriggerMetadata.MentionLimit.IsSpecified
|
||||||
? model.TriggerMetadata.MentionLimit.Value
|
? model.TriggerMetadata.MentionLimit.Value
|
||||||
: null;
|
: 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;
|
Enabled = model.Enabled;
|
||||||
ExemptRoles = model.ExemptRoles.ToImmutableArray();
|
ExemptRoles = model.ExemptRoles.ToImmutableArray();
|
||||||
ExemptChannels = model.ExemptChannels.ToImmutableArray();
|
ExemptChannels = model.ExemptChannels.ToImmutableArray();
|
||||||
|
|||||||
@@ -2942,8 +2942,14 @@ namespace Discord.WebSocket
|
|||||||
? data.Action.Metadata.Value.DurationSeconds.IsSpecified
|
? data.Action.Metadata.Value.DurationSeconds.IsSpecified
|
||||||
? data.Action.Metadata.Value.DurationSeconds.Value
|
? data.Action.Metadata.Value.DurationSeconds.Value
|
||||||
: null
|
: null
|
||||||
|
: null,
|
||||||
|
data.Action.Metadata.IsSpecified
|
||||||
|
? data.Action.Metadata.Value.CustomMessage.IsSpecified
|
||||||
|
? data.Action.Metadata.Value.CustomMessage.Value
|
||||||
|
: null
|
||||||
: null);
|
: null);
|
||||||
|
|
||||||
|
|
||||||
var member = guild.GetUser(data.UserId);
|
var member = guild.GetUser(data.UserId);
|
||||||
|
|
||||||
var cacheableUser = new Cacheable<SocketGuildUser, ulong>(member,
|
var cacheableUser = new Cacheable<SocketGuildUser, ulong>(member,
|
||||||
@@ -2965,7 +2971,7 @@ namespace Discord.WebSocket
|
|||||||
channel != null,
|
channel != null,
|
||||||
async () =>
|
async () =>
|
||||||
{
|
{
|
||||||
if(data.ChannelId.IsSpecified)
|
if (data.ChannelId.IsSpecified)
|
||||||
return await GetChannelAsync(data.ChannelId.Value).ConfigureAwait(false) as ISocketMessageChannel;
|
return await GetChannelAsync(data.ChannelId.Value).ConfigureAwait(false) as ISocketMessageChannel;
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
@@ -2980,7 +2986,7 @@ namespace Discord.WebSocket
|
|||||||
cachedMsg is not null,
|
cachedMsg is not null,
|
||||||
async () =>
|
async () =>
|
||||||
{
|
{
|
||||||
if(data.MessageId.IsSpecified)
|
if (data.MessageId.IsSpecified)
|
||||||
return (await channel!.GetMessageAsync(data.MessageId.Value).ConfigureAwait(false)) as IUserMessage;
|
return (await channel!.GetMessageAsync(data.MessageId.Value).ConfigureAwait(false)) as IUserMessage;
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -93,7 +93,16 @@ namespace Discord.WebSocket
|
|||||||
MentionTotalLimit = model.TriggerMetadata.MentionLimit.IsSpecified
|
MentionTotalLimit = model.TriggerMetadata.MentionLimit.IsSpecified
|
||||||
? model.TriggerMetadata.MentionLimit.Value
|
? model.TriggerMetadata.MentionLimit.Value
|
||||||
: null;
|
: 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;
|
Enabled = model.Enabled;
|
||||||
ExemptRoles = model.ExemptRoles.Select(x => Guild.GetRole(x)).ToImmutableArray();
|
ExemptRoles = model.ExemptRoles.Select(x => Guild.GetRole(x)).ToImmutableArray();
|
||||||
ExemptChannels = model.ExemptChannels.Select(x => Guild.GetChannel(x)).ToImmutableArray();
|
ExemptChannels = model.ExemptChannels.Select(x => Guild.GetChannel(x)).ToImmutableArray();
|
||||||
|
|||||||
Reference in New Issue
Block a user