feat: watchlist repository, remove ef core from all bot code
This commit is contained in:
parent
da4dfae27c
commit
f0511a560c
19 changed files with 155 additions and 97 deletions
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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 =
|
||||
[
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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!"
|
||||
);
|
||||
|
|
|
|||
|
|
@ -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>();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue