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

@ -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()
);
}