feat: split ignores into 'ignore messages' and 'ignore entities'

This commit is contained in:
sam 2024-11-18 00:47:27 +01:00
parent d48ab7e16e
commit 0cac964aa6
Signed by: sam
GPG key ID: 5F3C3C1B3166639D
32 changed files with 730 additions and 488 deletions

View file

@ -60,7 +60,14 @@ public class WebhookExecutorService(
/// </summary>
public void QueueLog(Guild guildConfig, LogChannelType logChannelType, IEmbed embed)
{
var logChannel = GetLogChannel(guildConfig, logChannelType, channelId: null, userId: null);
var logChannel = GetLogChannel(
guildConfig,
logChannelType,
channelId: null,
userId: null,
roleId: null,
roleIds: null
);
if (logChannel == null)
return;
@ -70,16 +77,16 @@ public class WebhookExecutorService(
/// <summary>
/// Queues a log embed for the given channel ID.
/// </summary>
public void QueueLog(ulong channelId, IEmbed embed)
public void QueueLog(ulong? channelId, IEmbed embed)
{
if (channelId == 0)
if (channelId is null or 0)
return;
var queue = _cache.GetOrAdd(channelId, []);
var queue = _cache.GetOrAdd(channelId.Value, []);
queue.Enqueue(embed);
_cache[channelId] = queue;
_cache[channelId.Value] = queue;
SetTimer(channelId, queue);
SetTimer(channelId.Value, queue);
}
/// <summary>
@ -251,14 +258,72 @@ public class WebhookExecutorService(
}
public ulong? GetLogChannel(
Guild guild,
LogChannelType logChannelType,
Snowflake? channelId = null,
ulong? userId = null,
Snowflake? roleId = null,
IReadOnlyList<Snowflake>? roleIds = null
)
{
var isMessageLog =
logChannelType
is LogChannelType.MessageUpdate
or LogChannelType.MessageDelete
or LogChannelType.MessageDeleteBulk;
// Check if we're getting the channel for a channel log
var isChannelLog =
channelId != null
&& logChannelType
is LogChannelType.ChannelCreate
or LogChannelType.ChannelDelete
or LogChannelType.ChannelUpdate;
// Check if we're getting the channel for a role log
var isRoleLog =
roleId != null
&& logChannelType
is LogChannelType.GuildRoleCreate
or LogChannelType.GuildRoleUpdate
or LogChannelType.GuildRoleDelete;
// Check if we're getting the channel for a member update log
var isMemberRoleUpdateLog =
roleIds != null && logChannelType is LogChannelType.GuildMemberUpdate;
if (isMessageLog)
return GetMessageLogChannel(guild, logChannelType, channelId, userId);
if (isChannelLog && guild.IgnoredChannels.Contains(channelId!.Value.Value))
return null;
if (isRoleLog && guild.IgnoredRoles.Contains(roleId!.Value.Value))
return null;
// Member update logs are only ignored if *all* updated roles are ignored
if (isMemberRoleUpdateLog && roleIds!.All(r => guild.IgnoredRoles.Contains(r.Value)))
return null;
// If nothing is ignored, return the correct log channel!
return GetDefaultLogChannel(guild, logChannelType);
}
private ulong? GetMessageLogChannel(
Guild guild,
LogChannelType logChannelType,
Snowflake? channelId = null,
ulong? userId = null
)
{
// Check if the user is ignored globally
if (userId != null && guild.Messages.IgnoredUsers.Contains(userId.Value))
return null;
// If the user isn't ignored and we didn't get a channel ID, return the default log channel
if (channelId == null)
return GetDefaultLogChannel(guild, logChannelType);
if (!channelCache.TryGet(channelId.Value, out var channel))
return null;
@ -282,25 +347,23 @@ public class WebhookExecutorService(
categoryId = channel.ParentID.Value;
}
// Check if the channel, or its category, or the user is ignored
// Check if the channel or its category is ignored
if (
guild.Channels.IgnoredChannels.Contains(channelId.Value.Value)
|| categoryId != null && guild.Channels.IgnoredChannels.Contains(categoryId.Value.Value)
guild.Messages.IgnoredChannels.Contains(channelId.Value.Value)
|| categoryId != null && guild.Messages.IgnoredChannels.Contains(categoryId.Value.Value)
)
return null;
if (userId != null)
{
if (guild.Channels.IgnoredUsers.Contains(userId.Value))
return null;
// Check the channel-local and category-local ignored users
var channelIgnoredUsers =
guild.Channels.IgnoredUsersPerChannel.GetValueOrDefault(channelId.Value.Value)
guild.Messages.IgnoredUsersPerChannel.GetValueOrDefault(channelId.Value.Value)
?? [];
var categoryIgnoredUsers =
(
categoryId != null
? guild.Channels.IgnoredUsersPerChannel.GetValueOrDefault(
? guild.Messages.IgnoredUsersPerChannel.GetValueOrDefault(
categoryId.Value.Value
)
: []
@ -310,36 +373,24 @@ public class WebhookExecutorService(
}
// These three events can be redirected to other channels. Redirects can be on a channel or category level.
// Obviously, the events are only redirected if they're supposed to be logged in the first place.
if (
logChannelType
is LogChannelType.MessageUpdate
or LogChannelType.MessageDelete
or LogChannelType.MessageDeleteBulk
)
{
if (GetDefaultLogChannel(guild, logChannelType) == 0)
return null;
// The events are only redirected if they're supposed to be logged in the first place.
if (GetDefaultLogChannel(guild, logChannelType) == 0)
return null;
var categoryRedirect =
categoryId != null
? guild.Channels.Redirects.GetValueOrDefault(categoryId.Value.Value)
: 0;
var categoryRedirect =
categoryId != null
? guild.Channels.Redirects.GetValueOrDefault(categoryId.Value.Value)
: 0;
if (
guild.Channels.Redirects.TryGetValue(channelId.Value.Value, out var channelRedirect)
)
return channelRedirect;
return categoryRedirect != 0
? categoryRedirect
: GetDefaultLogChannel(guild, logChannelType);
}
return GetDefaultLogChannel(guild, logChannelType);
if (guild.Channels.Redirects.TryGetValue(channelId.Value.Value, out var channelRedirect))
return channelRedirect;
return categoryRedirect != 0
? categoryRedirect
: GetDefaultLogChannel(guild, logChannelType);
}
public static ulong GetDefaultLogChannel(Guild guild, LogChannelType channelType) =>
channelType switch
public static ulong GetDefaultLogChannel(Guild guild, LogChannelType logChannelType) =>
logChannelType switch
{
LogChannelType.GuildUpdate => guild.Channels.GuildUpdate,
LogChannelType.GuildEmojisUpdate => guild.Channels.GuildEmojisUpdate,
@ -364,7 +415,7 @@ public class WebhookExecutorService(
LogChannelType.MessageUpdate => guild.Channels.MessageUpdate,
LogChannelType.MessageDelete => guild.Channels.MessageDelete,
LogChannelType.MessageDeleteBulk => guild.Channels.MessageDeleteBulk,
_ => throw new ArgumentOutOfRangeException(nameof(channelType)),
_ => throw new ArgumentOutOfRangeException(nameof(logChannelType)),
};
}