feat: watchlist repository, remove ef core from all bot code

This commit is contained in:
sam 2024-10-28 02:14:41 +01:00
parent da4dfae27c
commit f0511a560c
Signed by: sam
GPG key ID: 5F3C3C1B3166639D
19 changed files with 155 additions and 97 deletions

View file

@ -17,6 +17,7 @@ using System.ComponentModel;
using Catalogger.Backend.Cache;
using Catalogger.Backend.Cache.InMemoryCache;
using Catalogger.Backend.Database;
using Catalogger.Backend.Database.Dapper.Repositories;
using Catalogger.Backend.Database.Queries;
using Catalogger.Backend.Extensions;
using Catalogger.Backend.Services;
@ -42,7 +43,7 @@ namespace Catalogger.Backend.Bot.Commands;
public class ChannelCommands(
ILogger logger,
Config config,
DatabaseContext db,
GuildRepository guildRepository,
GuildCache guildCache,
ChannelCache channelCache,
IMemberCache memberCache,
@ -207,7 +208,7 @@ public class ChannelCommands(
if (!guildCache.TryGet(guildId, out var guild))
throw new CataloggerError("Guild not in cache");
var guildChannels = channelCache.GuildChannels(guildId).ToList();
var guildConfig = await db.GetGuildAsync(guildId);
var guildConfig = await guildRepository.GetAsync(guildId);
var (embeds, components) = BuildRootMenu(guildChannels, guild, guildConfig);

View file

@ -14,8 +14,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
using Catalogger.Backend.Cache.InMemoryCache;
using Catalogger.Backend.Database;
using Catalogger.Backend.Database.Queries;
using Catalogger.Backend.Database.Dapper.Repositories;
using Catalogger.Backend.Extensions;
using Catalogger.Backend.Services;
using Remora.Discord.API.Abstractions.Objects;
@ -24,7 +23,6 @@ using Remora.Discord.API.Objects;
using Remora.Discord.Commands.Attributes;
using Remora.Discord.Commands.Contexts;
using Remora.Discord.Commands.Extensions;
using Remora.Discord.Commands.Feedback.Messages;
using Remora.Discord.Commands.Feedback.Services;
using Remora.Discord.Commands.Services;
using Remora.Discord.Interactivity;
@ -36,7 +34,7 @@ namespace Catalogger.Backend.Bot.Commands;
public class ChannelCommandsComponents(
ILogger logger,
DatabaseContext db,
GuildRepository guildRepository,
GuildCache guildCache,
ChannelCache channelCache,
ContextInjectionService contextInjection,
@ -62,7 +60,7 @@ public class ChannelCommandsComponents(
if (!guildCache.TryGet(guildId, out var guild))
throw new CataloggerError("Guild not in cache");
var guildChannels = channelCache.GuildChannels(guildId).ToList();
var guildConfig = await db.GetGuildAsync(guildId);
var guildConfig = await guildRepository.GetAsync(guildId);
var result = await dataService.LeaseDataAsync(msg.ID);
await using var lease = result.GetOrThrow();
@ -166,8 +164,7 @@ public class ChannelCommandsComponents(
throw new ArgumentOutOfRangeException();
}
db.Update(guildConfig);
await db.SaveChangesAsync();
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels);
goto case "return";
case "return":
var (e, c) = ChannelCommands.BuildRootMenu(guildChannels, guild, guildConfig);
@ -260,7 +257,7 @@ public class ChannelCommandsComponents(
throw new CataloggerError("No guild ID in context");
if (!guildCache.TryGet(guildId, out var guild))
throw new CataloggerError("Guild not in cache");
var guildConfig = await db.GetGuildAsync(guildId);
var guildConfig = await guildRepository.GetAsync(guildId);
var channelId = channels[0].ID.ToUlong();
var result = await dataService.LeaseDataAsync(msg.ID);
@ -354,8 +351,7 @@ public class ChannelCommandsComponents(
throw new ArgumentOutOfRangeException();
}
db.Update(guildConfig);
await db.SaveChangesAsync();
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels);
List<IEmbed> embeds =
[

View file

@ -16,8 +16,7 @@
using System.ComponentModel;
using Catalogger.Backend.Cache;
using Catalogger.Backend.Cache.InMemoryCache;
using Catalogger.Backend.Database;
using Catalogger.Backend.Database.Queries;
using Catalogger.Backend.Database.Dapper.Repositories;
using Catalogger.Backend.Extensions;
using Catalogger.Backend.Services;
using Remora.Commands.Attributes;
@ -38,7 +37,7 @@ namespace Catalogger.Backend.Bot.Commands;
[DiscordDefaultMemberPermissions(DiscordPermission.ManageGuild)]
public class IgnoreChannelCommands(
ILogger logger,
DatabaseContext db,
GuildRepository guildRepository,
IMemberCache memberCache,
GuildCache guildCache,
ChannelCache channelCache,
@ -66,7 +65,7 @@ public class IgnoreChannelCommands(
)
{
var (_, guildId) = contextInjection.GetUserAndGuild();
var guildConfig = await db.GetGuildAsync(guildId);
var guildConfig = await guildRepository.GetAsync(guildId);
if (guildConfig.Channels.IgnoredChannels.Contains(channel.ID.Value))
return await feedbackService.ReplyAsync(
@ -75,8 +74,7 @@ public class IgnoreChannelCommands(
);
guildConfig.Channels.IgnoredChannels.Add(channel.ID.Value);
db.Update(guildConfig);
await db.SaveChangesAsync();
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."
@ -90,7 +88,7 @@ public class IgnoreChannelCommands(
)
{
var (_, guildId) = contextInjection.GetUserAndGuild();
var guildConfig = await db.GetGuildAsync(guildId);
var guildConfig = await guildRepository.GetAsync(guildId);
if (!guildConfig.Channels.IgnoredChannels.Contains(channel.ID.Value))
return await feedbackService.ReplyAsync(
@ -99,8 +97,7 @@ public class IgnoreChannelCommands(
);
guildConfig.Channels.IgnoredChannels.Remove(channel.ID.Value);
db.Update(guildConfig);
await db.SaveChangesAsync();
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."
@ -116,7 +113,7 @@ public class IgnoreChannelCommands(
throw new CataloggerError("Guild not in cache");
var guildChannels = channelCache.GuildChannels(guildId).ToList();
var guildConfig = await db.GetGuildAsync(guildId);
var guildConfig = await guildRepository.GetAsync(guildId);
var member = await memberCache.TryGetAsync(guildId, userId);
if (member == null)

View file

@ -15,8 +15,7 @@
using System.ComponentModel;
using Catalogger.Backend.Cache.InMemoryCache;
using Catalogger.Backend.Database;
using Catalogger.Backend.Database.Queries;
using Catalogger.Backend.Database.Dapper.Repositories;
using Catalogger.Backend.Extensions;
using Remora.Commands.Attributes;
using Remora.Commands.Groups;
@ -33,7 +32,7 @@ namespace Catalogger.Backend.Bot.Commands;
[Group("key-roles")]
[DiscordDefaultMemberPermissions(DiscordPermission.ManageGuild)]
public class KeyRoleCommands(
DatabaseContext db,
GuildRepository guildRepository,
ContextInjectionService contextInjection,
IFeedbackService feedbackService,
GuildCache guildCache,
@ -48,7 +47,7 @@ public class KeyRoleCommands(
if (!guildCache.TryGet(guildId, out var guild))
throw new CataloggerError("Guild not in cache");
var guildRoles = roleCache.GuildRoles(guildId).ToList();
var guildConfig = await db.GetGuildAsync(guildId);
var guildConfig = await guildRepository.GetAsync(guildId);
if (guildConfig.KeyRoles.Length == 0)
return await feedbackService.ReplyAsync(
@ -85,17 +84,14 @@ public class KeyRoleCommands(
if (role == null)
throw new CataloggerError("Role is not cached");
var guildConfig = await db.GetGuildAsync(guildId);
if (guildConfig.KeyRoles.Any(id => role.ID == id))
var guildConfig = await guildRepository.GetAsync(guildId);
if (guildConfig.KeyRoles.Any(id => role.ID.Value == id))
return await feedbackService.ReplyAsync(
$"{role.Name} is already a key role.",
isEphemeral: true
);
guildConfig.KeyRoles = guildConfig.KeyRoles.Append(role.ID.Value).ToArray();
db.Update(guildConfig);
await db.SaveChangesAsync();
await guildRepository.AddKeyRoleAsync(guildId, role.ID);
return await feedbackService.ReplyAsync($"Added {role.Name} to this server's key roles!");
}
@ -110,17 +106,14 @@ public class KeyRoleCommands(
if (role == null)
throw new CataloggerError("Role is not cached");
var guildConfig = await db.GetGuildAsync(guildId);
var guildConfig = await guildRepository.GetAsync(guildId);
if (guildConfig.KeyRoles.All(id => role.ID != id))
return await feedbackService.ReplyAsync(
$"{role.Name} is already not a key role.",
isEphemeral: true
);
guildConfig.KeyRoles = guildConfig.KeyRoles.Except([role.ID.Value]).ToArray();
db.Update(guildConfig);
await db.SaveChangesAsync();
await guildRepository.RemoveKeyRoleAsync(guildId, role.ID);
return await feedbackService.ReplyAsync(
$"Removed {role.Name} from this server's key roles!"
);

View file

@ -15,8 +15,7 @@
using System.ComponentModel;
using Catalogger.Backend.Cache.InMemoryCache;
using Catalogger.Backend.Database;
using Catalogger.Backend.Database.Queries;
using Catalogger.Backend.Database.Dapper.Repositories;
using Catalogger.Backend.Extensions;
using Remora.Commands.Attributes;
using Remora.Commands.Groups;
@ -34,7 +33,7 @@ namespace Catalogger.Backend.Bot.Commands;
[Description("Commands for configuring log redirects.")]
[DiscordDefaultMemberPermissions(DiscordPermission.ManageGuild)]
public class RedirectCommands(
DatabaseContext db,
GuildRepository guildRepository,
GuildCache guildCache,
ChannelCache channelCache,
ContextInjectionService contextInjectionService,
@ -60,10 +59,9 @@ public class RedirectCommands(
)
{
var (_, guildId) = contextInjectionService.GetUserAndGuild();
var guildConfig = await db.GetGuildAsync(guildId);
var guildConfig = await guildRepository.GetAsync(guildId);
guildConfig.Channels.Redirects[source.ID.Value] = target.ID.Value;
db.Update(guildConfig);
await db.SaveChangesAsync();
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels);
var output =
$"Success! Edited and deleted messages from {FormatChannel(source)} will now be redirected to <#{target.ID}>.";
@ -100,9 +98,11 @@ public class RedirectCommands(
)
{
var (_, guildId) = contextInjectionService.GetUserAndGuild();
var guildConfig = await db.GetGuildAsync(guildId);
var guildConfig = await guildRepository.GetAsync(guildId);
var wasSet = guildConfig.Channels.Redirects.Remove(source.ID.Value);
await guildRepository.UpdateChannelConfigAsync(guildId, guildConfig.Channels);
var output = wasSet
? $"Removed the redirect for {FormatChannel(source)}! Message logs from"
+ $"{(source.Type == ChannelType.GuildCategory ? "that category's channels" : "that channel")}"
@ -132,9 +132,6 @@ public class RedirectCommands(
$"\nHowever, all channels in the {parentChannelName} category are being redirected to <#{parentRedirect}>.";
}
db.Update(guildConfig);
await db.SaveChangesAsync();
return await feedbackService.ReplyAsync(output);
}
@ -146,7 +143,7 @@ public class RedirectCommands(
if (!guildCache.TryGet(guildId, out var guild))
throw new CataloggerError("Guild was not cached");
var guildChannels = channelCache.GuildChannels(guildId).ToList();
var guildConfig = await db.GetGuildAsync(guildId);
var guildConfig = await guildRepository.GetAsync(guildId);
var fields = new List<IEmbedField>();

View file

@ -14,13 +14,9 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
using Catalogger.Backend.Cache;
using Catalogger.Backend.Database;
using Catalogger.Backend.Database.Dapper;
using Catalogger.Backend.Database.Dapper.Repositories;
using Catalogger.Backend.Database.Queries;
using Catalogger.Backend.Extensions;
using Catalogger.Backend.Services;
using Microsoft.EntityFrameworkCore;
using Remora.Discord.API.Abstractions.Gateway.Events;
using Remora.Discord.API.Abstractions.Rest;
using Remora.Discord.Extensions.Embeds;

View file

@ -15,13 +15,10 @@
using Catalogger.Backend.Cache;
using Catalogger.Backend.Cache.InMemoryCache;
using Catalogger.Backend.Database;
using Catalogger.Backend.Database.Dapper.Repositories;
using Catalogger.Backend.Database.Queries;
using Catalogger.Backend.Extensions;
using Catalogger.Backend.Services;
using Humanizer;
using Microsoft.EntityFrameworkCore;
using Remora.Discord.API;
using Remora.Discord.API.Abstractions.Gateway.Events;
using Remora.Discord.API.Abstractions.Objects;
@ -35,9 +32,9 @@ namespace Catalogger.Backend.Bot.Responders.Members;
public class GuildMemberAddResponder(
ILogger logger,
DatabaseContext db,
InviteRepository inviteRepository,
GuildRepository guildRepository,
WatchlistRepository watchlistRepository,
IMemberCache memberCache,
IInviteCache inviteCache,
UserCache userCache,
@ -159,7 +156,7 @@ public class GuildMemberAddResponder(
);
}
var watchlist = await db.GetWatchlistEntryAsync(member.GuildID, user.ID, ct);
var watchlist = await watchlistRepository.GetWatchlistEntryAsync(member.GuildID, user.ID);
if (watchlist != null)
{
var moderator = await userCache.GetUserAsync(

View file

@ -14,9 +14,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
using Catalogger.Backend.Cache.InMemoryCache;
using Catalogger.Backend.Database;
using Catalogger.Backend.Database.Dapper.Repositories;
using Catalogger.Backend.Database.Queries;
using Catalogger.Backend.Extensions;
using Catalogger.Backend.Services;
using Remora.Discord.API;
@ -32,7 +30,7 @@ namespace Catalogger.Backend.Bot.Responders.Messages;
public class MessageUpdateResponder(
ILogger logger,
DatabaseContext db,
GuildRepository guildRepository,
ChannelCache channelCache,
UserCache userCache,
MessageRepository messageRepository,
@ -57,7 +55,7 @@ public class MessageUpdateResponder(
return Result.Success;
}
var guildConfig = await db.GetGuildAsync(msg.GuildID.Value, false, ct);
var guildConfig = await guildRepository.GetAsync(msg.GuildID);
if (await messageRepository.IsMessageIgnoredAsync(msg.ID.Value))
{