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

@ -2,6 +2,7 @@
<project version="4"> <project version="4">
<component name="SqlDialectMappings"> <component name="SqlDialectMappings">
<file url="file://$PROJECT_DIR$/Catalogger.Backend/Database/Migrations/001_init.up.sql" dialect="PostgreSQL" /> <file url="file://$PROJECT_DIR$/Catalogger.Backend/Database/Migrations/001_init.up.sql" dialect="PostgreSQL" />
<file url="file://$PROJECT_DIR$/Catalogger.Backend/Database/Migrations/004_split_message_config.down.sql" dialect="PostgreSQL" />
<file url="PROJECT" dialect="PostgreSQL" /> <file url="PROJECT" dialect="PostgreSQL" />
</component> </component>
</project> </project>

View file

@ -53,7 +53,8 @@ public partial class GuildsController
await guildRepository.ImportConfigAsync( await guildRepository.ImportConfigAsync(
guildId.Value, guildId.Value,
export.Channels.ToGuildConfig(), export.Channels.ToChannelConfig(),
export.Channels.ToMessageConfig(),
export.BannedSystems, export.BannedSystems,
export.KeyRoles export.KeyRoles
); );
@ -91,7 +92,7 @@ public partial class GuildsController
return new ConfigExport( return new ConfigExport(
config.Id, config.Id,
ChannelsBackup.FromGuildConfig(config.Channels), ChannelsBackup.FromGuildConfig(config),
config.BannedSystems, config.BannedSystems,
config.KeyRoles, config.KeyRoles,
invites.Select(i => new InviteExport(i.Code, i.Name)), 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 (guildId, _) = await ParseGuildAsync(id);
var guildConfig = await guildRepository.GetAsync(guildId); var guildConfig = await guildRepository.GetAsync(guildId);
if (guildConfig.Channels.IgnoredChannels.Contains(channelId)) if (guildConfig.Messages.IgnoredChannels.Contains(channelId))
return NoContent(); return NoContent();
var channel = channelCache var channel = channelCache
@ -47,8 +47,8 @@ public partial class GuildsController
if (channel == null) if (channel == null)
return NoContent(); return NoContent();
guildConfig.Channels.IgnoredChannels.Add(channelId); guildConfig.Messages.IgnoredChannels.Add(channelId);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels); await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig);
return NoContent(); return NoContent();
} }
@ -59,8 +59,8 @@ public partial class GuildsController
var (guildId, _) = await ParseGuildAsync(id); var (guildId, _) = await ParseGuildAsync(id);
var guildConfig = await guildRepository.GetAsync(guildId); var guildConfig = await guildRepository.GetAsync(guildId);
guildConfig.Channels.IgnoredChannels.Remove(channelId); guildConfig.Messages.IgnoredChannels.Remove(channelId);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels); await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig);
return NoContent(); return NoContent();
} }

View file

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

View file

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

View file

@ -159,28 +159,6 @@ public partial class GuildsController(
.ToList(); .ToList();
var guildConfig = await guildRepository.GetAsync(guildId); 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 // i love repeating myself wheeeeee
if ( if (
req.GuildUpdate == null req.GuildUpdate == null
@ -334,12 +312,11 @@ public partial class GuildsController(
) )
guildConfig.Channels.MessageDeleteBulk = req.MessageDeleteBulk ?? 0; guildConfig.Channels.MessageDeleteBulk = req.MessageDeleteBulk ?? 0;
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels); await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig);
return Ok(guildConfig.Channels); return Ok(guildConfig.Channels);
} }
public record ChannelRequest( public record ChannelRequest(
ulong[]? IgnoredChannels = null,
ulong? GuildUpdate = null, ulong? GuildUpdate = null,
ulong? GuildEmojisUpdate = null, ulong? GuildEmojisUpdate = null,
ulong? GuildRoleCreate = null, ulong? GuildRoleCreate = null,

View file

@ -261,7 +261,7 @@ public class ChannelCommandsComponents(
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();
} }
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels); await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig);
goto case "return"; goto case "return";
case "return": case "return":
var (e, c) = ChannelCommands.BuildRootMenu(guildChannels, guild, guildConfig); var (e, c) = ChannelCommands.BuildRootMenu(guildChannels, guild, guildConfig);
@ -384,7 +384,7 @@ public class ChannelCommandsComponents(
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();
} }
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels); await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig);
List<IEmbed> embeds = 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 (_, guildId) = contextInjectionService.GetUserAndGuild();
var guildConfig = await guildRepository.GetAsync(guildId); var guildConfig = await guildRepository.GetAsync(guildId);
guildConfig.Channels.Redirects[source.ID.Value] = target.ID.Value; guildConfig.Channels.Redirects[source.ID.Value] = target.ID.Value;
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels); await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig);
var output = var output =
$"Success! Edited and deleted messages from {FormatChannel(source)} will now be redirected to <#{target.ID}>."; $"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 guildConfig = await guildRepository.GetAsync(guildId);
var wasSet = guildConfig.Channels.Redirects.Remove(source.ID.Value); var wasSet = guildConfig.Channels.Redirects.Remove(source.ID.Value);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels); await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig);
var output = wasSet var output = wasSet
? $"Removed the redirect for {FormatChannel(source)}! Message logs from" ? $"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.Database.Repositories;
using Catalogger.Backend.Extensions; using Catalogger.Backend.Extensions;
using Catalogger.Backend.Services; using Catalogger.Backend.Services;
using Microsoft.Extensions.Logging.Configuration;
using Remora.Discord.API.Abstractions.Gateway.Events; using Remora.Discord.API.Abstractions.Gateway.Events;
using Remora.Discord.API.Abstractions.Objects; using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.Extensions.Embeds; using Remora.Discord.Extensions.Embeds;
@ -97,8 +98,11 @@ public class ChannelCreateResponder(
var guildConfig = await guildRepository.GetAsync(ch.GuildID); var guildConfig = await guildRepository.GetAsync(ch.GuildID);
webhookExecutor.QueueLog( webhookExecutor.QueueLog(
guildConfig, webhookExecutor.GetLogChannel(
LogChannelType.ChannelCreate, guildConfig,
LogChannelType.ChannelCreate,
channelId: ch.ID
),
builder.Build().GetOrThrow() builder.Build().GetOrThrow()
); );
return Result.Success; return Result.Success;

View file

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

View file

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

View file

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

View file

@ -53,7 +53,13 @@ public class MessageCreateResponder(
var guild = await guildRepository.GetAsync(msg.GuildID); var guild = await guildRepository.GetAsync(msg.GuildID);
// The guild needs to have enabled at least one of the message logging events, // 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. // 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); await messageRepository.IgnoreMessageAsync(msg.ID.Value);
return Result.Success; return Result.Success;

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -24,18 +24,28 @@ public class Guild
public required ulong Id { get; init; } public required ulong Id { get; init; }
public ChannelConfig Channels { get; init; } = new(); public ChannelConfig Channels { get; init; } = new();
public MessageConfig Messages { get; init; } = new();
public string[] BannedSystems { get; set; } = []; public string[] BannedSystems { get; set; } = [];
public ulong[] KeyRoles { 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) => public bool IsSystemBanned(PluralkitApiService.PkSystem system) =>
BannedSystems.Contains(system.Id) || BannedSystems.Contains(system.Uuid.ToString()); 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 ( if (
Channels is { MessageDelete: 0, MessageUpdate: 0, MessageDeleteBulk: 0 } Channels is { MessageDelete: 0, MessageUpdate: 0, MessageDeleteBulk: 0 }
|| Channels.IgnoredChannels.Contains(channelId.ToUlong()) || Messages.IgnoredChannels.Contains(channelId.ToUlong())
|| (userId != null && Channels.IgnoredUsers.Contains(userId.Value.ToUlong())) || (userId != null && Messages.IgnoredUsers.Contains(userId.Value.ToUlong()))
|| (roleIds != null && roleIds.Any(r => Messages.IgnoredRoles.Any(id => r.Value == id)))
) )
return true; return true;
@ -43,7 +53,7 @@ public class Guild
return false; return false;
if ( if (
Channels.IgnoredUsersPerChannel.TryGetValue( Messages.IgnoredUsersPerChannel.TryGetValue(
channelId.ToUlong(), channelId.ToUlong(),
out var thisChannelIgnoredUsers out var thisChannelIgnoredUsers
) )
@ -53,11 +63,16 @@ public class Guild
return false; return false;
} }
public class ChannelConfig public class MessageConfig
{ {
public List<ulong> IgnoredChannels { get; set; } = []; public List<ulong> IgnoredChannels { get; set; } = [];
public List<ulong> IgnoredRoles { get; set; } = [];
public List<ulong> IgnoredUsers { get; init; } = []; public List<ulong> IgnoredUsers { get; init; } = [];
public Dictionary<ulong, List<ulong>> IgnoredUsersPerChannel { get; init; } = []; public Dictionary<ulong, List<ulong>> IgnoredUsersPerChannel { get; init; } = [];
}
public class ChannelConfig
{
public Dictionary<ulong, ulong> Redirects { get; init; } = []; public Dictionary<ulong, ulong> Redirects { get; init; } = [];
public ulong GuildUpdate { get; set; } 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 } 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( await conn.ExecuteAsync(
"update guilds set channels = @Channels::jsonb where id = @Id", "update guilds set channels = @Channels::jsonb, messages = @Messages::jsonb where id = @Id",
new { Id = id.Value, Channels = config } new
{
Id = id.Value,
config.Channels,
config.Messages,
}
); );
public async Task ImportConfigAsync( public async Task ImportConfigAsync(
ulong id, ulong id,
Guild.ChannelConfig channels, Guild.ChannelConfig channels,
Guild.MessageConfig messages,
string[] bannedSystems, string[] bannedSystems,
ulong[] keyRoles ulong[] keyRoles
) => ) =>
await conn.ExecuteAsync( 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 new
{ {
id, id,
channels, channels,
messages,
bannedSystems, bannedSystems,
keyRoles, keyRoles,
} }

View file

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

View file

@ -60,7 +60,14 @@ public class WebhookExecutorService(
/// </summary> /// </summary>
public void QueueLog(Guild guildConfig, LogChannelType logChannelType, IEmbed embed) 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) if (logChannel == null)
return; return;
@ -70,16 +77,16 @@ public class WebhookExecutorService(
/// <summary> /// <summary>
/// Queues a log embed for the given channel ID. /// Queues a log embed for the given channel ID.
/// </summary> /// </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; return;
var queue = _cache.GetOrAdd(channelId, []); var queue = _cache.GetOrAdd(channelId.Value, []);
queue.Enqueue(embed); queue.Enqueue(embed);
_cache[channelId] = queue; _cache[channelId.Value] = queue;
SetTimer(channelId, queue); SetTimer(channelId.Value, queue);
} }
/// <summary> /// <summary>
@ -251,14 +258,72 @@ public class WebhookExecutorService(
} }
public ulong? GetLogChannel( 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, Guild guild,
LogChannelType logChannelType, LogChannelType logChannelType,
Snowflake? channelId = null, Snowflake? channelId = null,
ulong? userId = 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) if (channelId == null)
return GetDefaultLogChannel(guild, logChannelType); return GetDefaultLogChannel(guild, logChannelType);
if (!channelCache.TryGet(channelId.Value, out var channel)) if (!channelCache.TryGet(channelId.Value, out var channel))
return null; return null;
@ -282,25 +347,23 @@ public class WebhookExecutorService(
categoryId = channel.ParentID.Value; 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 ( if (
guild.Channels.IgnoredChannels.Contains(channelId.Value.Value) guild.Messages.IgnoredChannels.Contains(channelId.Value.Value)
|| categoryId != null && guild.Channels.IgnoredChannels.Contains(categoryId.Value.Value) || categoryId != null && guild.Messages.IgnoredChannels.Contains(categoryId.Value.Value)
) )
return null; return null;
if (userId != null) if (userId != null)
{ {
if (guild.Channels.IgnoredUsers.Contains(userId.Value))
return null;
// Check the channel-local and category-local ignored users // Check the channel-local and category-local ignored users
var channelIgnoredUsers = var channelIgnoredUsers =
guild.Channels.IgnoredUsersPerChannel.GetValueOrDefault(channelId.Value.Value) guild.Messages.IgnoredUsersPerChannel.GetValueOrDefault(channelId.Value.Value)
?? []; ?? [];
var categoryIgnoredUsers = var categoryIgnoredUsers =
( (
categoryId != null categoryId != null
? guild.Channels.IgnoredUsersPerChannel.GetValueOrDefault( ? guild.Messages.IgnoredUsersPerChannel.GetValueOrDefault(
categoryId.Value.Value 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. // 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. // The events are only redirected if they're supposed to be logged in the first place.
if ( if (GetDefaultLogChannel(guild, logChannelType) == 0)
logChannelType return null;
is LogChannelType.MessageUpdate
or LogChannelType.MessageDelete
or LogChannelType.MessageDeleteBulk
)
{
if (GetDefaultLogChannel(guild, logChannelType) == 0)
return null;
var categoryRedirect = var categoryRedirect =
categoryId != null categoryId != null
? guild.Channels.Redirects.GetValueOrDefault(categoryId.Value.Value) ? guild.Channels.Redirects.GetValueOrDefault(categoryId.Value.Value)
: 0; : 0;
if ( if (guild.Channels.Redirects.TryGetValue(channelId.Value.Value, out var channelRedirect))
guild.Channels.Redirects.TryGetValue(channelId.Value.Value, out var channelRedirect) return channelRedirect;
) return categoryRedirect != 0
return channelRedirect; ? categoryRedirect
return categoryRedirect != 0 : GetDefaultLogChannel(guild, logChannelType);
? categoryRedirect
: GetDefaultLogChannel(guild, logChannelType);
}
return GetDefaultLogChannel(guild, logChannelType);
} }
public static ulong GetDefaultLogChannel(Guild guild, LogChannelType channelType) => public static ulong GetDefaultLogChannel(Guild guild, LogChannelType logChannelType) =>
channelType switch logChannelType switch
{ {
LogChannelType.GuildUpdate => guild.Channels.GuildUpdate, LogChannelType.GuildUpdate => guild.Channels.GuildUpdate,
LogChannelType.GuildEmojisUpdate => guild.Channels.GuildEmojisUpdate, LogChannelType.GuildEmojisUpdate => guild.Channels.GuildEmojisUpdate,
@ -364,7 +415,7 @@ public class WebhookExecutorService(
LogChannelType.MessageUpdate => guild.Channels.MessageUpdate, LogChannelType.MessageUpdate => guild.Channels.MessageUpdate,
LogChannelType.MessageDelete => guild.Channels.MessageDelete, LogChannelType.MessageDelete => guild.Channels.MessageDelete,
LogChannelType.MessageDeleteBulk => guild.Channels.MessageDeleteBulk, LogChannelType.MessageDeleteBulk => guild.Channels.MessageDeleteBulk,
_ => throw new ArgumentOutOfRangeException(nameof(channelType)), _ => throw new ArgumentOutOfRangeException(nameof(logChannelType)),
}; };
} }

View file

@ -62,10 +62,14 @@ public static class GuildImport
GoGuild guild GoGuild guild
) )
{ {
var channels = new Guild.ChannelConfig var messages = new Guild.MessageConfig
{ {
IgnoredChannels = guild.IgnoredChannels.ToList(), IgnoredChannels = guild.IgnoredChannels.ToList(),
IgnoredUsers = guild.IgnoredUsers.ToList(), IgnoredUsers = guild.IgnoredUsers.ToList(),
};
var channels = new Guild.ChannelConfig
{
GuildUpdate = guild.Channels.TryParse("GUILD_UPDATE"), GuildUpdate = guild.Channels.TryParse("GUILD_UPDATE"),
GuildEmojisUpdate = guild.Channels.TryParse("GUILD_EMOJIS_UPDATE"), GuildEmojisUpdate = guild.Channels.TryParse("GUILD_EMOJIS_UPDATE"),
GuildRoleCreate = guild.Channels.TryParse("GUILD_ROLE_CREATE"), GuildRoleCreate = guild.Channels.TryParse("GUILD_ROLE_CREATE"),
@ -97,13 +101,14 @@ public static class GuildImport
await conn.ExecuteAsync( await conn.ExecuteAsync(
""" """
insert into guilds (id, channels, banned_systems, key_roles) insert into guilds (id, channels, messages, banned_systems, key_roles)
values (@Id, @Channels::jsonb, @BannedSystems, @KeyRoles) values (@Id, @channels::jsonb, @messages::jsonb, @BannedSystems, @KeyRoles)
""", """,
new new
{ {
guild.Id, guild.Id,
Channels = channels, messages,
channels,
guild.BannedSystems, guild.BannedSystems,
guild.KeyRoles, guild.KeyRoles,
}, },