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

@ -53,7 +53,8 @@ public partial class GuildsController
await guildRepository.ImportConfigAsync(
guildId.Value,
export.Channels.ToGuildConfig(),
export.Channels.ToChannelConfig(),
export.Channels.ToMessageConfig(),
export.BannedSystems,
export.KeyRoles
);
@ -91,7 +92,7 @@ public partial class GuildsController
return new ConfigExport(
config.Id,
ChannelsBackup.FromGuildConfig(config.Channels),
ChannelsBackup.FromGuildConfig(config),
config.BannedSystems,
config.KeyRoles,
invites.Select(i => new InviteExport(i.Code, i.Name)),

View file

@ -29,7 +29,7 @@ public partial class GuildsController
var (guildId, _) = await ParseGuildAsync(id);
var guildConfig = await guildRepository.GetAsync(guildId);
if (guildConfig.Channels.IgnoredChannels.Contains(channelId))
if (guildConfig.Messages.IgnoredChannels.Contains(channelId))
return NoContent();
var channel = channelCache
@ -47,8 +47,8 @@ public partial class GuildsController
if (channel == null)
return NoContent();
guildConfig.Channels.IgnoredChannels.Add(channelId);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels);
guildConfig.Messages.IgnoredChannels.Add(channelId);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig);
return NoContent();
}
@ -59,8 +59,8 @@ public partial class GuildsController
var (guildId, _) = await ParseGuildAsync(id);
var guildConfig = await guildRepository.GetAsync(guildId);
guildConfig.Channels.IgnoredChannels.Remove(channelId);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels);
guildConfig.Messages.IgnoredChannels.Remove(channelId);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig);
return NoContent();
}

View file

@ -61,7 +61,7 @@ public partial class GuildsController
);
guildConfig.Channels.Redirects[source.ID.Value] = target.ID.Value;
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig);
return NoContent();
}
@ -80,7 +80,7 @@ public partial class GuildsController
);
guildConfig.Channels.Redirects.Remove(channelId, out _);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig);
return NoContent();
}

View file

@ -37,7 +37,7 @@ public partial class GuildsController
var guildConfig = await guildRepository.GetAsync(guildId);
var output = new List<IgnoredUser>();
foreach (var userId in guildConfig.Channels.IgnoredUsers)
foreach (var userId in guildConfig.Messages.IgnoredUsers)
{
if (cts.Token.IsCancellationRequested)
break;
@ -72,11 +72,11 @@ public partial class GuildsController
if (user == null)
throw new ApiError(HttpStatusCode.NotFound, ErrorCode.BadRequest, "User not found");
if (guildConfig.Channels.IgnoredUsers.Contains(user.ID.Value))
if (guildConfig.Messages.IgnoredUsers.Contains(user.ID.Value))
return Ok(new IgnoredUser(user.ID.Value, user.Tag()));
guildConfig.Channels.IgnoredUsers.Add(user.ID.Value);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels);
guildConfig.Messages.IgnoredUsers.Add(user.ID.Value);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig);
return Ok(new IgnoredUser(user.ID.Value, user.Tag()));
}
@ -87,8 +87,8 @@ public partial class GuildsController
var (guildId, _) = await ParseGuildAsync(id);
var guildConfig = await guildRepository.GetAsync(guildId);
guildConfig.Channels.IgnoredUsers.Remove(userId);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels);
guildConfig.Messages.IgnoredUsers.Remove(userId);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig);
return NoContent();
}

View file

@ -159,28 +159,6 @@ public partial class GuildsController(
.ToList();
var guildConfig = await guildRepository.GetAsync(guildId);
if (req.IgnoredChannels != null)
{
var categories = channelCache
.GuildChannels(guildId)
.Where(c => c.Type is ChannelType.GuildCategory)
.ToList();
if (
req.IgnoredChannels.Any(cId =>
guildChannels.All(c => c.ID.Value != cId)
&& categories.All(c => c.ID.Value != cId)
)
)
throw new ApiError(
HttpStatusCode.BadRequest,
ErrorCode.BadRequest,
"One or more ignored channels are unknown"
);
guildConfig.Channels.IgnoredChannels = req.IgnoredChannels.ToList();
}
// i love repeating myself wheeeeee
if (
req.GuildUpdate == null
@ -334,12 +312,11 @@ public partial class GuildsController(
)
guildConfig.Channels.MessageDeleteBulk = req.MessageDeleteBulk ?? 0;
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig);
return Ok(guildConfig.Channels);
}
public record ChannelRequest(
ulong[]? IgnoredChannels = null,
ulong? GuildUpdate = null,
ulong? GuildEmojisUpdate = null,
ulong? GuildRoleCreate = null,

View file

@ -261,7 +261,7 @@ public class ChannelCommandsComponents(
throw new ArgumentOutOfRangeException();
}
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig);
goto case "return";
case "return":
var (e, c) = ChannelCommands.BuildRootMenu(guildChannels, guild, guildConfig);
@ -384,7 +384,7 @@ public class ChannelCommandsComponents(
throw new ArgumentOutOfRangeException();
}
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig);
List<IEmbed> embeds =
[

View file

@ -1,205 +0,0 @@
// Copyright (C) 2021-present sam (starshines.gay)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
using System.ComponentModel;
using Catalogger.Backend.Cache;
using Catalogger.Backend.Cache.InMemoryCache;
using Catalogger.Backend.Database.Repositories;
using Catalogger.Backend.Extensions;
using Catalogger.Backend.Services;
using Remora.Commands.Attributes;
using Remora.Commands.Groups;
using Remora.Discord.API;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.Commands.Attributes;
using Remora.Discord.Commands.Feedback.Services;
using Remora.Discord.Commands.Services;
using Remora.Discord.Extensions.Embeds;
using Remora.Rest.Core;
using IResult = Remora.Results.IResult;
namespace Catalogger.Backend.Bot.Commands;
[Group("ignored-channels")]
[Description("Manage channels ignored for logging.")]
[DiscordDefaultMemberPermissions(DiscordPermission.ManageGuild)]
public class IgnoreChannelCommands(
ILogger logger,
GuildRepository guildRepository,
IMemberCache memberCache,
GuildCache guildCache,
ChannelCache channelCache,
PermissionResolverService permissionResolver,
ContextInjectionService contextInjection,
FeedbackService feedbackService
) : CommandGroup
{
private readonly ILogger _logger = logger.ForContext<IgnoreChannelCommands>();
[Command("add")]
[Description("Add a channel to the list of ignored channels.")]
public async Task<IResult> AddIgnoredChannelAsync(
[ChannelTypes(
ChannelType.GuildCategory,
ChannelType.GuildText,
ChannelType.GuildAnnouncement,
ChannelType.GuildForum,
ChannelType.GuildMedia,
ChannelType.GuildVoice,
ChannelType.GuildStageVoice
)]
[Description("The channel to ignore")]
IChannel channel
)
{
var (_, guildId) = contextInjection.GetUserAndGuild();
var guildConfig = await guildRepository.GetAsync(guildId);
if (guildConfig.Channels.IgnoredChannels.Contains(channel.ID.Value))
return await feedbackService.ReplyAsync(
"That channel is already being ignored.",
isEphemeral: true
);
guildConfig.Channels.IgnoredChannels.Add(channel.ID.Value);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels);
return await feedbackService.ReplyAsync(
$"Successfully added {(channel.Type == ChannelType.GuildCategory ? channel.Name : $"<#{channel.ID}>")} to the list of ignored channels."
);
}
[Command("remove")]
[Description("Remove a channel from the list of ignored channels.")]
public async Task<IResult> RemoveIgnoredChannelAsync(
[Description("The channel to stop ignoring")] IChannel channel
)
{
var (_, guildId) = contextInjection.GetUserAndGuild();
var guildConfig = await guildRepository.GetAsync(guildId);
if (!guildConfig.Channels.IgnoredChannels.Contains(channel.ID.Value))
return await feedbackService.ReplyAsync(
"That channel is already not ignored.",
isEphemeral: true
);
guildConfig.Channels.IgnoredChannels.Remove(channel.ID.Value);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels);
return await feedbackService.ReplyAsync(
$"Successfully removed {(channel.Type == ChannelType.GuildCategory ? channel.Name : $"<#{channel.ID}>")} from the list of ignored channels."
);
}
[Command("list")]
[Description("List channels ignored for logging.")]
public async Task<IResult> ListIgnoredChannelsAsync()
{
var (userId, guildId) = contextInjection.GetUserAndGuild();
if (!guildCache.TryGet(guildId, out var guild))
throw new CataloggerError("Guild not in cache");
var guildChannels = channelCache.GuildChannels(guildId).ToList();
var guildConfig = await guildRepository.GetAsync(guildId);
var member = await memberCache.TryGetAsync(guildId, userId);
if (member == null)
throw new CataloggerError("Executing member not found");
var ignoredChannels = guildConfig
.Channels.IgnoredChannels.Select(id =>
{
var channel = guildChannels.FirstOrDefault(c => c.ID.Value == id);
if (channel == null)
return new IgnoredChannel(IgnoredChannelType.Unknown, DiscordSnowflake.New(id));
var type = channel.Type switch
{
ChannelType.GuildCategory => IgnoredChannelType.Category,
_ => IgnoredChannelType.Base,
};
return new IgnoredChannel(
type,
channel.ID,
permissionResolver
.GetChannelPermissions(guildId, member, channel)
.HasPermission(DiscordPermission.ViewChannel)
);
})
.ToList();
var embed = new EmbedBuilder()
.WithTitle($"Ignored channels in {guild.Name}")
.WithColour(DiscordUtils.Purple);
var nonVisibleCategories = ignoredChannels.Count(c =>
c is { Type: IgnoredChannelType.Category, CanSee: false }
);
var visibleCategories = ignoredChannels
.Where(c => c is { Type: IgnoredChannelType.Category, CanSee: true })
.ToList();
if (nonVisibleCategories != 0 || visibleCategories.Count != 0)
{
var value = string.Join("\n", visibleCategories.Select(c => $"<#{c.Id}>"));
if (nonVisibleCategories != 0)
value +=
$"\n\n{nonVisibleCategories} channel(s) are not shown as you do not have access to them.";
embed.AddField("Categories", value);
}
var nonVisibleBase = ignoredChannels.Count(c =>
c is { Type: IgnoredChannelType.Base, CanSee: false }
);
var visibleBase = ignoredChannels
.Where(c => c is { Type: IgnoredChannelType.Base, CanSee: true })
.ToList();
if (nonVisibleBase != 0 || visibleBase.Count != 0)
{
var value = string.Join("\n", visibleBase.Select(c => $"<#{c.Id}>"));
if (nonVisibleBase != 0)
value +=
$"\n\n{nonVisibleBase} channel(s) are not shown as you do not have access to them.";
embed.AddField("Channels", value);
}
var unknownChannels = string.Join(
"\n",
ignoredChannels
.Where(c => c.Type == IgnoredChannelType.Unknown)
.Select(c => $"{c.Id} <#{c.Id}>")
);
if (!string.IsNullOrWhiteSpace(unknownChannels))
{
embed.AddField("Unknown", unknownChannels);
}
return await feedbackService.ReplyAsync(embeds: [embed.Build().GetOrThrow()]);
}
private record struct IgnoredChannel(IgnoredChannelType Type, Snowflake Id, bool CanSee = true);
private enum IgnoredChannelType
{
Unknown,
Base,
Category,
}
}

View file

@ -0,0 +1,214 @@
// Copyright (C) 2021-present sam (starshines.gay)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
using System.ComponentModel;
using Catalogger.Backend.Cache;
using Catalogger.Backend.Cache.InMemoryCache;
using Catalogger.Backend.Database.Repositories;
using Catalogger.Backend.Extensions;
using Catalogger.Backend.Services;
using Remora.Commands.Attributes;
using Remora.Commands.Groups;
using Remora.Discord.API;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.Commands.Attributes;
using Remora.Discord.Commands.Feedback.Services;
using Remora.Discord.Commands.Services;
using Remora.Discord.Extensions.Embeds;
using Remora.Rest.Core;
using IResult = Remora.Results.IResult;
namespace Catalogger.Backend.Bot.Commands;
[Group("ignore-messages")]
[Description("Manage users, roles, and channels whose messages are not logged.")]
[DiscordDefaultMemberPermissions(DiscordPermission.ManageGuild)]
public partial class IgnoreMessageCommands : CommandGroup
{
[Group("channels")]
public class Channels(
GuildRepository guildRepository,
IMemberCache memberCache,
GuildCache guildCache,
ChannelCache channelCache,
PermissionResolverService permissionResolver,
ContextInjectionService contextInjection,
FeedbackService feedbackService
) : CommandGroup
{
[Command("add")]
[Description("Add a channel to the list of ignored channels.")]
[SuppressInteractionResponse(true)]
public async Task<IResult> AddIgnoredChannelAsync(
[ChannelTypes(
ChannelType.GuildCategory,
ChannelType.GuildText,
ChannelType.GuildAnnouncement,
ChannelType.GuildForum,
ChannelType.GuildMedia,
ChannelType.GuildVoice,
ChannelType.GuildStageVoice
)]
[Description("The channel to ignore")]
IChannel channel
)
{
var (_, guildId) = contextInjection.GetUserAndGuild();
var guildConfig = await guildRepository.GetAsync(guildId);
if (guildConfig.Messages.IgnoredChannels.Contains(channel.ID.Value))
return await feedbackService.ReplyAsync(
"That channel is already being ignored.",
isEphemeral: true
);
guildConfig.Messages.IgnoredChannels.Add(channel.ID.Value);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig);
return await feedbackService.ReplyAsync(
$"Successfully added {(channel.Type == ChannelType.GuildCategory ? channel.Name : $"<#{channel.ID}>")} to the list of ignored channels."
);
}
[Command("remove")]
[Description("Remove a channel from the list of ignored channels.")]
public async Task<IResult> RemoveIgnoredChannelAsync(
[Description("The channel to stop ignoring")] IChannel channel
)
{
var (_, guildId) = contextInjection.GetUserAndGuild();
var guildConfig = await guildRepository.GetAsync(guildId);
if (!guildConfig.Messages.IgnoredChannels.Contains(channel.ID.Value))
return await feedbackService.ReplyAsync(
"That channel is already not ignored.",
isEphemeral: true
);
guildConfig.Messages.IgnoredChannels.Remove(channel.ID.Value);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig);
return await feedbackService.ReplyAsync(
$"Successfully removed {(channel.Type == ChannelType.GuildCategory ? channel.Name : $"<#{channel.ID}>")} from the list of ignored channels."
);
}
[Command("list")]
[Description("List channels ignored for logging.")]
public async Task<IResult> ListIgnoredChannelsAsync()
{
var (userId, guildId) = contextInjection.GetUserAndGuild();
if (!guildCache.TryGet(guildId, out var guild))
throw new CataloggerError("Guild not in cache");
var guildChannels = channelCache.GuildChannels(guildId).ToList();
var guildConfig = await guildRepository.GetAsync(guildId);
var member = await memberCache.TryGetAsync(guildId, userId);
if (member == null)
throw new CataloggerError("Executing member not found");
var ignoredChannels = guildConfig
.Messages.IgnoredChannels.Select(id =>
{
var channel = guildChannels.FirstOrDefault(c => c.ID.Value == id);
if (channel == null)
return new IgnoredChannel(
IgnoredChannelType.Unknown,
DiscordSnowflake.New(id)
);
var type = channel.Type switch
{
ChannelType.GuildCategory => IgnoredChannelType.Category,
_ => IgnoredChannelType.Base,
};
return new IgnoredChannel(
type,
channel.ID,
permissionResolver
.GetChannelPermissions(guildId, member, channel)
.HasPermission(DiscordPermission.ViewChannel)
);
})
.ToList();
var embed = new EmbedBuilder()
.WithTitle($"Ignored channels in {guild.Name}")
.WithColour(DiscordUtils.Purple);
var nonVisibleCategories = ignoredChannels.Count(c =>
c is { Type: IgnoredChannelType.Category, CanSee: false }
);
var visibleCategories = ignoredChannels
.Where(c => c is { Type: IgnoredChannelType.Category, CanSee: true })
.ToList();
if (nonVisibleCategories != 0 || visibleCategories.Count != 0)
{
var value = string.Join("\n", visibleCategories.Select(c => $"<#{c.Id}>"));
if (nonVisibleCategories != 0)
value +=
$"\n\n{nonVisibleCategories} channel(s) are not shown as you do not have access to them.";
embed.AddField("Categories", value);
}
var nonVisibleBase = ignoredChannels.Count(c =>
c is { Type: IgnoredChannelType.Base, CanSee: false }
);
var visibleBase = ignoredChannels
.Where(c => c is { Type: IgnoredChannelType.Base, CanSee: true })
.ToList();
if (nonVisibleBase != 0 || visibleBase.Count != 0)
{
var value = string.Join("\n", visibleBase.Select(c => $"<#{c.Id}>"));
if (nonVisibleBase != 0)
value +=
$"\n\n{nonVisibleBase} channel(s) are not shown as you do not have access to them.";
embed.AddField("Channels", value);
}
var unknownChannels = string.Join(
"\n",
ignoredChannels
.Where(c => c.Type == IgnoredChannelType.Unknown)
.Select(c => $"{c.Id} <#{c.Id}>")
);
if (!string.IsNullOrWhiteSpace(unknownChannels))
{
embed.AddField("Unknown", unknownChannels);
}
return await feedbackService.ReplyAsync(embeds: [embed.Build().GetOrThrow()]);
}
private record struct IgnoredChannel(
IgnoredChannelType Type,
Snowflake Id,
bool CanSee = true
);
private enum IgnoredChannelType
{
Unknown,
Base,
Category,
}
}
}

View file

@ -0,0 +1,122 @@
// Copyright (C) 2021-present sam (starshines.gay)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
using System.ComponentModel;
using Catalogger.Backend.Cache.InMemoryCache;
using Catalogger.Backend.Database.Repositories;
using Catalogger.Backend.Extensions;
using Remora.Commands.Attributes;
using Remora.Commands.Groups;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.Commands.Feedback.Services;
using Remora.Discord.Commands.Services;
using Remora.Discord.Extensions.Embeds;
using IResult = Remora.Results.IResult;
namespace Catalogger.Backend.Bot.Commands;
public partial class IgnoreMessageCommands
{
[Group("roles")]
public class Roles(
GuildRepository guildRepository,
GuildCache guildCache,
RoleCache roleCache,
ContextInjectionService contextInjection,
FeedbackService feedbackService
) : CommandGroup
{
[Command("add")]
[Description("Add a role to the list of ignored roles.")]
public async Task<IResult> AddIgnoredRoleAsync(
[Description("The role to ignore")] IRole role
)
{
var (_, guildId) = contextInjection.GetUserAndGuild();
var guildConfig = await guildRepository.GetAsync(guildId);
if (guildConfig.Messages.IgnoredRoles.Contains(role.ID.Value))
return await feedbackService.ReplyAsync(
"That role is already being ignored.",
isEphemeral: true
);
guildConfig.Messages.IgnoredRoles.Add(role.ID.Value);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig);
return await feedbackService.ReplyAsync(
$"Successfully added {role.Name} to the list of ignored roles."
);
}
[Command("remove")]
[Description("Remove a role from the list of ignored roles.")]
public async Task<IResult> RemoveIgnoredRoleAsync(
[Description("The role to stop ignoring")] IRole role
)
{
var (_, guildId) = contextInjection.GetUserAndGuild();
var guildConfig = await guildRepository.GetAsync(guildId);
if (!guildConfig.Messages.IgnoredRoles.Contains(role.ID.Value))
return await feedbackService.ReplyAsync(
"That role is already not ignored.",
isEphemeral: true
);
guildConfig.Messages.IgnoredRoles.Remove(role.ID.Value);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig);
return await feedbackService.ReplyAsync(
$"Successfully removed {role.Name} from the list of ignored roles."
);
}
[Command("list")]
[Description("List roles ignored for logging.")]
public async Task<IResult> ListIgnoredRolesAsync()
{
var (_, guildId) = contextInjection.GetUserAndGuild();
if (!guildCache.TryGet(guildId, out var guild))
throw new CataloggerError("Guild not in cache");
var guildConfig = await guildRepository.GetAsync(guildId);
var roles = roleCache
.GuildRoles(guildId)
.Where(r => guildConfig.Messages.IgnoredRoles.Contains(r.ID.Value))
.OrderByDescending(r => r.Position)
.Select(r => $"<@&{r.ID}>")
.ToList();
if (roles.Count == 0)
return await feedbackService.ReplyAsync(
"No roles are being ignored right now.",
isEphemeral: true
);
return await feedbackService.ReplyAsync(
embeds:
[
new EmbedBuilder()
.WithTitle($"Ignored roles in {guild.Name}")
.WithDescription(string.Join("\n", roles))
.WithColour(DiscordUtils.Purple)
.Build()
.GetOrThrow(),
]
);
}
}
}

View file

@ -0,0 +1,124 @@
// Copyright (C) 2021-present sam (starshines.gay)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
using System.ComponentModel;
using Catalogger.Backend.Cache;
using Catalogger.Backend.Cache.InMemoryCache;
using Catalogger.Backend.Database.Repositories;
using Catalogger.Backend.Extensions;
using Remora.Commands.Attributes;
using Remora.Commands.Groups;
using Remora.Discord.API;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.Commands.Feedback.Services;
using Remora.Discord.Commands.Services;
using Remora.Discord.Pagination.Extensions;
using Remora.Rest.Core;
using IResult = Remora.Results.IResult;
namespace Catalogger.Backend.Bot.Commands;
public partial class IgnoreMessageCommands
{
[Group("users")]
public class Users(
GuildRepository guildRepository,
IMemberCache memberCache,
GuildCache guildCache,
UserCache userCache,
ContextInjectionService contextInjection,
FeedbackService feedbackService
) : CommandGroup
{
[Command("add")]
[Description("Add a user to the list of ignored users.")]
public async Task<IResult> AddIgnoredUserAsync(
[Description("The user to ignore")] IUser user
)
{
var (_, guildId) = contextInjection.GetUserAndGuild();
var guildConfig = await guildRepository.GetAsync(guildId);
if (guildConfig.Messages.IgnoredUsers.Contains(user.ID.Value))
return await feedbackService.ReplyAsync(
"That user is already being ignored.",
isEphemeral: true
);
guildConfig.Messages.IgnoredUsers.Add(user.ID.Value);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig);
return await feedbackService.ReplyAsync(
$"Successfully added {user.PrettyFormat()} to the list of ignored users."
);
}
[Command("remove")]
[Description("Remove a user from the list of ignored users.")]
public async Task<IResult> RemoveIgnoredUserAsync(
[Description("The user to stop ignoring")] IUser user
)
{
var (_, guildId) = contextInjection.GetUserAndGuild();
var guildConfig = await guildRepository.GetAsync(guildId);
if (!guildConfig.Messages.IgnoredUsers.Contains(user.ID.Value))
return await feedbackService.ReplyAsync(
"That user is already not ignored.",
isEphemeral: true
);
guildConfig.Messages.IgnoredUsers.Remove(user.ID.Value);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig);
return await feedbackService.ReplyAsync(
$"Successfully removed {user.PrettyFormat()} from the list of ignored users."
);
}
[Command("list")]
[Description("List currently ignored users.")]
public async Task<IResult> ListIgnoredUsersAsync()
{
var (userId, guildId) = contextInjection.GetUserAndGuild();
if (!guildCache.TryGet(guildId, out var guild))
throw new CataloggerError("Guild was not cached");
var guildConfig = await guildRepository.GetAsync(guildId);
if (guildConfig.Messages.IgnoredUsers.Count == 0)
return await feedbackService.ReplyAsync("No users are being ignored right now.");
var users = new List<string>();
foreach (var id in guildConfig.Messages.IgnoredUsers)
{
var user = await TryGetUserAsync(guildId, DiscordSnowflake.New(id));
users.Add(user?.PrettyFormat() ?? $"*(unknown user {id})* <@{id}>");
}
return await feedbackService.SendContextualPaginatedMessageAsync(
userId,
DiscordUtils.PaginateStrings(
users,
$"Ignored users for {guild.Name} ({users.Count})"
)
);
}
private async Task<IUser?> TryGetUserAsync(Snowflake guildId, Snowflake userId) =>
(await memberCache.TryGetAsync(guildId, userId))?.User.Value
?? await userCache.GetUserAsync(userId);
}
}

View file

@ -1,117 +0,0 @@
// Copyright (C) 2021-present sam (starshines.gay)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
using System.ComponentModel;
using Catalogger.Backend.Cache;
using Catalogger.Backend.Cache.InMemoryCache;
using Catalogger.Backend.Database.Repositories;
using Catalogger.Backend.Extensions;
using Remora.Commands.Attributes;
using Remora.Commands.Groups;
using Remora.Discord.API;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.Commands.Feedback.Services;
using Remora.Discord.Commands.Services;
using Remora.Discord.Pagination.Extensions;
using Remora.Rest.Core;
using IResult = Remora.Results.IResult;
namespace Catalogger.Backend.Bot.Commands;
[Group("ignored-users")]
[Description("Manage users ignored for logging.")]
public class IgnoreUserCommands(
GuildRepository guildRepository,
GuildCache guildCache,
IMemberCache memberCache,
UserCache userCache,
ContextInjectionService contextInjection,
FeedbackService feedbackService
) : CommandGroup
{
[Command("add")]
[Description("Add a user to the list of ignored users.")]
public async Task<IResult> AddIgnoredUserAsync([Description("The user to ignore")] IUser user)
{
var (_, guildId) = contextInjection.GetUserAndGuild();
var guildConfig = await guildRepository.GetAsync(guildId);
if (guildConfig.Channels.IgnoredUsers.Contains(user.ID.Value))
return await feedbackService.ReplyAsync(
"That user is already being ignored.",
isEphemeral: true
);
guildConfig.Channels.IgnoredUsers.Add(user.ID.Value);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels);
return await feedbackService.ReplyAsync(
$"Successfully added {user.PrettyFormat()} to the list of ignored users."
);
}
[Command("remove")]
[Description("Remove a user from the list of ignored users.")]
public async Task<IResult> RemoveIgnoredUserAsync(
[Description("The user to stop ignoring")] IUser user
)
{
var (_, guildId) = contextInjection.GetUserAndGuild();
var guildConfig = await guildRepository.GetAsync(guildId);
if (!guildConfig.Channels.IgnoredUsers.Contains(user.ID.Value))
return await feedbackService.ReplyAsync(
"That user is already not ignored.",
isEphemeral: true
);
guildConfig.Channels.IgnoredUsers.Remove(user.ID.Value);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels);
return await feedbackService.ReplyAsync(
$"Successfully removed {user.PrettyFormat()} from the list of ignored users."
);
}
[Command("list")]
[Description("List currently ignored users.")]
public async Task<IResult> ListIgnoredUsersAsync()
{
var (userId, guildId) = contextInjection.GetUserAndGuild();
if (!guildCache.TryGet(guildId, out var guild))
throw new CataloggerError("Guild was not cached");
var guildConfig = await guildRepository.GetAsync(guildId);
if (guildConfig.Channels.IgnoredUsers.Count == 0)
return await feedbackService.ReplyAsync("No users are being ignored right now.");
var users = new List<string>();
foreach (var id in guildConfig.Channels.IgnoredUsers)
{
var user = await TryGetUserAsync(guildId, DiscordSnowflake.New(id));
users.Add(user?.PrettyFormat() ?? $"*(unknown user {id})* <@{id}>");
}
return await feedbackService.SendContextualPaginatedMessageAsync(
userId,
DiscordUtils.PaginateStrings(users, $"Ignored users for {guild.Name} ({users.Count})")
);
}
private async Task<IUser?> TryGetUserAsync(Snowflake guildId, Snowflake userId) =>
(await memberCache.TryGetAsync(guildId, userId))?.User.Value
?? await userCache.GetUserAsync(userId);
}

View file

@ -61,7 +61,7 @@ public class RedirectCommands(
var (_, guildId) = contextInjectionService.GetUserAndGuild();
var guildConfig = await guildRepository.GetAsync(guildId);
guildConfig.Channels.Redirects[source.ID.Value] = target.ID.Value;
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig);
var output =
$"Success! Edited and deleted messages from {FormatChannel(source)} will now be redirected to <#{target.ID}>.";
@ -101,7 +101,7 @@ public class RedirectCommands(
var guildConfig = await guildRepository.GetAsync(guildId);
var wasSet = guildConfig.Channels.Redirects.Remove(source.ID.Value);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig);
var output = wasSet
? $"Removed the redirect for {FormatChannel(source)}! Message logs from"

View file

@ -17,6 +17,7 @@ using Catalogger.Backend.Cache.InMemoryCache;
using Catalogger.Backend.Database.Repositories;
using Catalogger.Backend.Extensions;
using Catalogger.Backend.Services;
using Microsoft.Extensions.Logging.Configuration;
using Remora.Discord.API.Abstractions.Gateway.Events;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.Extensions.Embeds;
@ -97,8 +98,11 @@ public class ChannelCreateResponder(
var guildConfig = await guildRepository.GetAsync(ch.GuildID);
webhookExecutor.QueueLog(
guildConfig,
LogChannelType.ChannelCreate,
webhookExecutor.GetLogChannel(
guildConfig,
LogChannelType.ChannelCreate,
channelId: ch.ID
),
builder.Build().GetOrThrow()
);
return Result.Success;

View file

@ -68,8 +68,11 @@ public class ChannelDeleteResponder(
embed.AddField("Description", topic);
webhookExecutor.QueueLog(
guildConfig,
LogChannelType.ChannelDelete,
webhookExecutor.GetLogChannel(
guildConfig,
LogChannelType.ChannelDelete,
channelId: channel.ID
),
embed.Build().GetOrThrow()
);
return Result.Success;

View file

@ -180,16 +180,14 @@ public class ChannelUpdateResponder(
if (builder.Fields.Count == 0)
return Result.Success;
var logChannel = webhookExecutor.GetLogChannel(
guildConfig,
LogChannelType.ChannelUpdate,
channelId: evt.ID,
userId: null
webhookExecutor.QueueLog(
webhookExecutor.GetLogChannel(
guildConfig,
LogChannelType.ChannelUpdate,
channelId: evt.ID
),
builder.Build().GetOrThrow()
);
if (logChannel == null)
return Result.Success;
webhookExecutor.QueueLog(logChannel.Value, builder.Build().GetOrThrow());
return Result.Success;
}

View file

@ -315,19 +315,20 @@ public class GuildMemberUpdateResponder(
.WithFooter($"User ID: {member.User.ID}")
.WithCurrentTimestamp();
var addedRoles = member.Roles.Except(oldRoles).Select(s => s.Value).ToList();
var removedRoles = oldRoles.Except(member.Roles).Select(s => s.Value).ToList();
var addedRoles = member.Roles.Except(oldRoles).ToList();
var removedRoles = oldRoles.Except(member.Roles).ToList();
if (addedRoles.Count != 0)
{
roleUpdate.AddField("Added", string.Join(", ", addedRoles.Select(id => $"<@&{id}>")));
// Add all added key roles to the log
if (!addedRoles.Except(guildConfig.KeyRoles).Any())
if (!addedRoles.Select(s => s.Value).Except(guildConfig.KeyRoles).Any())
{
var value = string.Join(
"\n",
addedRoles
.Select(s => s.Value)
.Where(guildConfig.KeyRoles.Contains)
.Select(id =>
{
@ -348,11 +349,12 @@ public class GuildMemberUpdateResponder(
);
// Add all removed key roles to the log
if (!removedRoles.Except(guildConfig.KeyRoles).Any())
if (!removedRoles.Select(s => s.Value).Except(guildConfig.KeyRoles).Any())
{
var value = string.Join(
"\n",
removedRoles
.Select(s => s.Value)
.Where(guildConfig.KeyRoles.Contains)
.Select(id =>
{
@ -369,8 +371,12 @@ public class GuildMemberUpdateResponder(
if (roleUpdate.Fields.Count != 0)
{
webhookExecutor.QueueLog(
guildConfig,
LogChannelType.GuildMemberUpdate,
webhookExecutor.GetLogChannel(
guildConfig,
LogChannelType.GuildMemberUpdate,
// Check for all added and removed roles
roleIds: addedRoles.Concat(removedRoles).ToList()
),
roleUpdate.Build().GetOrThrow()
);
}

View file

@ -53,7 +53,13 @@ public class MessageCreateResponder(
var guild = await guildRepository.GetAsync(msg.GuildID);
// The guild needs to have enabled at least one of the message logging events,
// and the channel must not be ignored, to store the message.
if (guild.IsMessageIgnored(msg.ChannelID, msg.Author.ID))
if (
guild.IsMessageIgnored(
msg.ChannelID,
msg.Author.ID,
msg.Member.OrDefault()?.Roles.OrDefault()
)
)
{
await messageRepository.IgnoreMessageAsync(msg.ID.Value);
return Result.Success;

View file

@ -42,7 +42,7 @@ public class MessageDeleteBulkResponder(
public async Task<Result> RespondAsync(IMessageDeleteBulk evt, CancellationToken ct = default)
{
var guild = await guildRepository.GetAsync(evt.GuildID);
if (guild.IsMessageIgnored(evt.ChannelID, null))
if (guild.IsMessageIgnored(evt.ChannelID, null, null))
return Result.Success;
var logChannel = webhookExecutor.GetLogChannel(

View file

@ -64,22 +64,15 @@ public class MessageDeleteResponder(
return Result.Success;
var guild = await guildRepository.GetAsync(evt.GuildID);
if (guild.IsMessageIgnored(evt.ChannelID, evt.ID))
if (guild.IsMessageIgnored(evt.ChannelID, null, null))
return Result.Success;
var logChannel = webhookExecutor.GetLogChannel(
guild,
LogChannelType.MessageDelete,
evt.ChannelID
);
var msg = await messageRepository.GetMessageAsync(evt.ID.Value, ct);
// Sometimes a message that *should* be logged isn't stored in the database, notify the user of that
if (msg == null)
{
if (logChannel == null)
return Result.Success;
webhookExecutor.QueueLog(
logChannel.Value,
webhookExecutor.GetLogChannel(guild, LogChannelType.MessageDelete, evt.ChannelID),
new Embed(
Title: "Message deleted",
Description: $"A message not found in the database was deleted in <#{evt.ChannelID}> ({evt.ChannelID}).",
@ -107,7 +100,7 @@ public class MessageDeleteResponder(
}
}
logChannel = webhookExecutor.GetLogChannel(
var logChannel = webhookExecutor.GetLogChannel(
guild,
LogChannelType.MessageDelete,
evt.ChannelID,
@ -173,7 +166,7 @@ public class MessageDeleteResponder(
builder.AddField("Attachments", attachmentInfo, false);
}
webhookExecutor.QueueLog(logChannel.Value, builder.Build().GetOrThrow());
webhookExecutor.QueueLog(logChannel, builder.Build().GetOrThrow());
return Result.Success;
}
}

View file

@ -54,8 +54,11 @@ public class RoleCreateResponder(
}
webhookExecutor.QueueLog(
guildConfig,
LogChannelType.GuildRoleCreate,
webhookExecutor.GetLogChannel(
guildConfig,
LogChannelType.GuildRoleCreate,
roleId: evt.Role.ID
),
embed.Build().GetOrThrow()
);

View file

@ -70,8 +70,11 @@ public class RoleDeleteResponder(
}
webhookExecutor.QueueLog(
guildConfig,
LogChannelType.GuildRoleDelete,
webhookExecutor.GetLogChannel(
guildConfig,
LogChannelType.GuildRoleDelete,
roleId: role.ID
),
embed.Build().GetOrThrow()
);
}

View file

@ -96,8 +96,11 @@ public class RoleUpdateResponder(
var guildConfig = await guildRepository.GetAsync(evt.GuildID);
webhookExecutor.QueueLog(
guildConfig,
LogChannelType.GuildRoleUpdate,
webhookExecutor.GetLogChannel(
guildConfig,
LogChannelType.GuildRoleUpdate,
roleId: evt.Role.ID
),
embed.Build().GetOrThrow()
);
}

View file

@ -116,6 +116,7 @@ public class DatabasePool
SqlMapper.AddTypeHandler(new PassthroughTypeHandler<Instant>());
SqlMapper.AddTypeHandler(new JsonTypeHandler<Guild.ChannelConfig>());
SqlMapper.AddTypeHandler(new JsonTypeHandler<Guild.MessageConfig>());
}
// Copied from PluralKit:

View file

@ -0,0 +1,3 @@
update guilds set channels = (channels || messages) - 'IgnoredRoles';
alter table guilds drop column messages;

View file

@ -0,0 +1,12 @@
alter table guilds
add column messages jsonb not null default '{}';
-- Extract the current message-related configuration options into the new "messages" column
-- noinspection SqlWithoutWhere
update guilds
set messages = jsonb_build_object('IgnoredUsers', channels['IgnoredUsers'], 'IgnoredChannels',
channels['IgnoredChannels'], 'IgnoredUsersPerChannel',
channels['IgnoredUsersPerChannel']);
-- We don't update the "channels" column as it will be cleared out automatically over time,
-- as channel configurations are updated by the bot

View file

@ -19,6 +19,7 @@ public class ChannelsBackup
{
public List<ulong> IgnoredChannels { get; init; } = [];
public List<ulong> IgnoredUsers { get; init; } = [];
public List<ulong> IgnoredRoles { get; init; } = [];
public Dictionary<ulong, List<ulong>> IgnoredUsersPerChannel { get; init; } = [];
public Dictionary<ulong, ulong> Redirects { get; init; } = [];
@ -46,12 +47,18 @@ public class ChannelsBackup
public ulong MessageDelete { get; init; }
public ulong MessageDeleteBulk { get; init; }
public Guild.ChannelConfig ToGuildConfig() =>
public Guild.MessageConfig ToMessageConfig() =>
new()
{
IgnoredChannels = IgnoredChannels,
IgnoredUsers = IgnoredUsers,
IgnoredRoles = IgnoredRoles,
IgnoredUsersPerChannel = IgnoredUsersPerChannel,
};
public Guild.ChannelConfig ToChannelConfig() =>
new()
{
Redirects = Redirects,
GuildUpdate = GuildUpdate,
GuildEmojisUpdate = GuildEmojisUpdate,
@ -78,35 +85,36 @@ public class ChannelsBackup
MessageDeleteBulk = MessageDeleteBulk,
};
public static ChannelsBackup FromGuildConfig(Guild.ChannelConfig channels) =>
public static ChannelsBackup FromGuildConfig(Guild guild) =>
new()
{
IgnoredChannels = channels.IgnoredChannels,
IgnoredUsers = channels.IgnoredUsers,
IgnoredUsersPerChannel = channels.IgnoredUsersPerChannel,
Redirects = channels.Redirects,
GuildUpdate = channels.GuildUpdate,
GuildEmojisUpdate = channels.GuildEmojisUpdate,
GuildRoleCreate = channels.GuildRoleCreate,
GuildRoleUpdate = channels.GuildRoleUpdate,
GuildRoleDelete = channels.GuildRoleDelete,
ChannelCreate = channels.ChannelCreate,
ChannelUpdate = channels.ChannelUpdate,
ChannelDelete = channels.ChannelDelete,
GuildMemberAdd = channels.GuildMemberAdd,
GuildMemberUpdate = channels.GuildMemberUpdate,
GuildKeyRoleUpdate = channels.GuildKeyRoleUpdate,
GuildMemberNickUpdate = channels.GuildMemberNickUpdate,
GuildMemberAvatarUpdate = channels.GuildMemberAvatarUpdate,
GuildMemberTimeout = channels.GuildMemberTimeout,
GuildMemberRemove = channels.GuildMemberRemove,
GuildMemberKick = channels.GuildMemberKick,
GuildBanAdd = channels.GuildBanAdd,
GuildBanRemove = channels.GuildBanRemove,
InviteCreate = channels.InviteCreate,
InviteDelete = channels.InviteDelete,
MessageUpdate = channels.MessageUpdate,
MessageDelete = channels.MessageDelete,
MessageDeleteBulk = channels.MessageDeleteBulk,
IgnoredChannels = guild.Messages.IgnoredChannels,
IgnoredUsers = guild.Messages.IgnoredUsers,
IgnoredRoles = guild.Messages.IgnoredRoles,
IgnoredUsersPerChannel = guild.Messages.IgnoredUsersPerChannel,
Redirects = guild.Channels.Redirects,
GuildUpdate = guild.Channels.GuildUpdate,
GuildEmojisUpdate = guild.Channels.GuildEmojisUpdate,
GuildRoleCreate = guild.Channels.GuildRoleCreate,
GuildRoleUpdate = guild.Channels.GuildRoleUpdate,
GuildRoleDelete = guild.Channels.GuildRoleDelete,
ChannelCreate = guild.Channels.ChannelCreate,
ChannelUpdate = guild.Channels.ChannelUpdate,
ChannelDelete = guild.Channels.ChannelDelete,
GuildMemberAdd = guild.Channels.GuildMemberAdd,
GuildMemberUpdate = guild.Channels.GuildMemberUpdate,
GuildKeyRoleUpdate = guild.Channels.GuildKeyRoleUpdate,
GuildMemberNickUpdate = guild.Channels.GuildMemberNickUpdate,
GuildMemberAvatarUpdate = guild.Channels.GuildMemberAvatarUpdate,
GuildMemberTimeout = guild.Channels.GuildMemberTimeout,
GuildMemberRemove = guild.Channels.GuildMemberRemove,
GuildMemberKick = guild.Channels.GuildMemberKick,
GuildBanAdd = guild.Channels.GuildBanAdd,
GuildBanRemove = guild.Channels.GuildBanRemove,
InviteCreate = guild.Channels.InviteCreate,
InviteDelete = guild.Channels.InviteDelete,
MessageUpdate = guild.Channels.MessageUpdate,
MessageDelete = guild.Channels.MessageDelete,
MessageDeleteBulk = guild.Channels.MessageDeleteBulk,
};
}

View file

@ -24,18 +24,28 @@ public class Guild
public required ulong Id { get; init; }
public ChannelConfig Channels { get; init; } = new();
public MessageConfig Messages { get; init; } = new();
public string[] BannedSystems { get; set; } = [];
public ulong[] KeyRoles { get; set; } = [];
// These channels and roles are ignored for channel/role update/delete events.
public ulong[] IgnoredChannels { get; set; } = [];
public ulong[] IgnoredRoles { get; set; } = [];
public bool IsSystemBanned(PluralkitApiService.PkSystem system) =>
BannedSystems.Contains(system.Id) || BannedSystems.Contains(system.Uuid.ToString());
public bool IsMessageIgnored(Snowflake channelId, Snowflake? userId)
public bool IsMessageIgnored(
Snowflake channelId,
Snowflake? userId,
IReadOnlyList<Snowflake>? roleIds
)
{
if (
Channels is { MessageDelete: 0, MessageUpdate: 0, MessageDeleteBulk: 0 }
|| Channels.IgnoredChannels.Contains(channelId.ToUlong())
|| (userId != null && Channels.IgnoredUsers.Contains(userId.Value.ToUlong()))
|| Messages.IgnoredChannels.Contains(channelId.ToUlong())
|| (userId != null && Messages.IgnoredUsers.Contains(userId.Value.ToUlong()))
|| (roleIds != null && roleIds.Any(r => Messages.IgnoredRoles.Any(id => r.Value == id)))
)
return true;
@ -43,7 +53,7 @@ public class Guild
return false;
if (
Channels.IgnoredUsersPerChannel.TryGetValue(
Messages.IgnoredUsersPerChannel.TryGetValue(
channelId.ToUlong(),
out var thisChannelIgnoredUsers
)
@ -53,11 +63,16 @@ public class Guild
return false;
}
public class ChannelConfig
public class MessageConfig
{
public List<ulong> IgnoredChannels { get; set; } = [];
public List<ulong> IgnoredRoles { get; set; } = [];
public List<ulong> IgnoredUsers { get; init; } = [];
public Dictionary<ulong, List<ulong>> IgnoredUsersPerChannel { get; init; } = [];
}
public class ChannelConfig
{
public Dictionary<ulong, ulong> Redirects { get; init; } = [];
public ulong GuildUpdate { get; set; }

View file

@ -131,24 +131,31 @@ public class GuildRepository(ILogger logger, DatabaseConnection conn)
new { GuildId = guildId.Value, RoleId = roleId.Value }
);
public async Task UpdateChannelConfigAsync(Snowflake id, Guild.ChannelConfig config) =>
public async Task UpdateChannelConfigAsync(Snowflake id, Guild config) =>
await conn.ExecuteAsync(
"update guilds set channels = @Channels::jsonb where id = @Id",
new { Id = id.Value, Channels = config }
"update guilds set channels = @Channels::jsonb, messages = @Messages::jsonb where id = @Id",
new
{
Id = id.Value,
config.Channels,
config.Messages,
}
);
public async Task ImportConfigAsync(
ulong id,
Guild.ChannelConfig channels,
Guild.MessageConfig messages,
string[] bannedSystems,
ulong[] keyRoles
) =>
await conn.ExecuteAsync(
"update guilds set channels = @channels::jsonb, banned_systems = @bannedSystems, key_roles = @keyRoles where id = @id",
"update guilds set channels = @channels::jsonb, messages = @messages::jsonb, banned_systems = @bannedSystems, key_roles = @keyRoles where id = @id",
new
{
id,
channels,
messages,
bannedSystems,
keyRoles,
}

View file

@ -27,6 +27,7 @@ using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.API.Gateway.Commands;
using Remora.Discord.API.Objects;
using Remora.Discord.Commands.Extensions;
using Remora.Discord.Commands.Responders;
using Remora.Discord.Extensions.Extensions;
using Remora.Discord.Gateway;
using Remora.Discord.Interactivity.Extensions;
@ -83,6 +84,7 @@ builder
]
);
})
.Configure<InteractionResponderOptions>(opts => opts.SuppressAutomaticResponses = true)
.AddDiscordCommands(
enableSlash: true,
useDefaultCommandResponder: false,
@ -94,10 +96,12 @@ builder
.WithCommandGroup<ChannelCommands>()
.WithCommandGroup<KeyRoleCommands>()
.WithCommandGroup<InviteCommands>()
.WithCommandGroup<IgnoreChannelCommands>()
.WithCommandGroup<IgnoreMessageCommands>()
.WithCommandGroup<IgnoreMessageCommands.Channels>()
.WithCommandGroup<IgnoreMessageCommands.Users>()
.WithCommandGroup<IgnoreMessageCommands.Roles>()
.WithCommandGroup<RedirectCommands>()
.WithCommandGroup<WatchlistCommands>()
.WithCommandGroup<IgnoreUserCommands>()
// End command tree
.Finish()
.AddPagination()

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)),
};
}