add invite repository to replace ef core
This commit is contained in:
parent
5891f28f7c
commit
64b4c26d93
12 changed files with 112 additions and 301 deletions
|
|
@ -15,12 +15,10 @@
|
|||
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.InteropServices;
|
||||
using Catalogger.Backend.Cache;
|
||||
using Catalogger.Backend.Cache.InMemoryCache;
|
||||
using Catalogger.Backend.Database;
|
||||
using Catalogger.Backend.Database.Dapper.Repositories;
|
||||
using Catalogger.Backend.Extensions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Remora.Commands.Attributes;
|
||||
using Remora.Commands.Groups;
|
||||
using Remora.Discord.API.Abstractions.Objects;
|
||||
|
|
@ -33,7 +31,6 @@ using Remora.Discord.Commands.Feedback.Services;
|
|||
using Remora.Discord.Commands.Services;
|
||||
using Remora.Discord.Pagination.Extensions;
|
||||
using Remora.Rest.Core;
|
||||
using Invite = Catalogger.Backend.Database.Models.Invite;
|
||||
using IResult = Remora.Results.IResult;
|
||||
|
||||
namespace Catalogger.Backend.Bot.Commands;
|
||||
|
|
@ -43,7 +40,7 @@ namespace Catalogger.Backend.Bot.Commands;
|
|||
[DiscordDefaultMemberPermissions(DiscordPermission.ManageGuild)]
|
||||
public class InviteCommands(
|
||||
ILogger logger,
|
||||
DatabaseContext db,
|
||||
InviteRepository inviteRepository,
|
||||
GuildCache guildCache,
|
||||
IInviteCache inviteCache,
|
||||
IDiscordRestChannelAPI channelApi,
|
||||
|
|
@ -63,7 +60,7 @@ public class InviteCommands(
|
|||
if (!guildCache.TryGet(guildId, out var guild))
|
||||
throw new CataloggerError("Guild not in cache");
|
||||
|
||||
var dbInvites = await db.Invites.Where(i => i.GuildId == guildId.Value).ToListAsync();
|
||||
var dbInvites = await inviteRepository.GetGuildInvitesAsync(guildId);
|
||||
|
||||
var fields = guildInvites
|
||||
.Select(i => new PartialNamedInvite(
|
||||
|
|
@ -153,14 +150,7 @@ public class InviteCommands(
|
|||
+ $"\nLink: https://discord.gg/{inviteResult.Entity.Code}"
|
||||
);
|
||||
|
||||
var dbInvite = new Invite
|
||||
{
|
||||
GuildId = guildId.Value,
|
||||
Code = inviteResult.Entity.Code,
|
||||
Name = name,
|
||||
};
|
||||
db.Add(dbInvite);
|
||||
await db.SaveChangesAsync();
|
||||
await inviteRepository.SetInviteNameAsync(guildId, inviteResult.Entity.Code, name);
|
||||
|
||||
return await feedbackService.ReplyAsync(
|
||||
$"Created a new invite in <#{channel.ID}> with the name **{name}**!"
|
||||
|
|
@ -188,39 +178,18 @@ public class InviteCommands(
|
|||
);
|
||||
}
|
||||
|
||||
var namedInvite = await db
|
||||
.Invites.Where(i => i.GuildId == guildId.Value && i.Code == invite)
|
||||
.FirstOrDefaultAsync();
|
||||
if (namedInvite == null)
|
||||
{
|
||||
if (name == null)
|
||||
return await feedbackService.ReplyAsync($"Invite `{invite}` already has no name.");
|
||||
|
||||
namedInvite = new Invite
|
||||
{
|
||||
GuildId = guildId.Value,
|
||||
Code = invite,
|
||||
Name = name,
|
||||
};
|
||||
db.Add(namedInvite);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
return await feedbackService.ReplyAsync(
|
||||
$"New name set! The invite `{invite}` will now show up as **{name}** in logs."
|
||||
);
|
||||
}
|
||||
var namedInvite = await inviteRepository.GetInviteAsync(guildId, invite);
|
||||
if (namedInvite == null && name == null)
|
||||
return await feedbackService.ReplyAsync($"Invite `{invite}` already has no name.");
|
||||
|
||||
if (name == null)
|
||||
{
|
||||
db.Invites.Remove(namedInvite);
|
||||
await db.SaveChangesAsync();
|
||||
await inviteRepository.DeleteInviteAsync(guildId, invite);
|
||||
|
||||
return await feedbackService.ReplyAsync($"Removed the name for `{invite}`.");
|
||||
}
|
||||
|
||||
namedInvite.Name = name;
|
||||
db.Update(namedInvite);
|
||||
await db.SaveChangesAsync();
|
||||
await inviteRepository.SetInviteNameAsync(guildId, invite, name);
|
||||
|
||||
return await feedbackService.ReplyAsync(
|
||||
$"New name set! The invite `{invite}` will now show up as **{name}** in logs."
|
||||
|
|
@ -230,7 +199,7 @@ public class InviteCommands(
|
|||
|
||||
public class InviteAutocompleteProvider(
|
||||
ILogger logger,
|
||||
DatabaseContext db,
|
||||
InviteRepository inviteRepository,
|
||||
IInviteCache inviteCache,
|
||||
ContextInjectionService contextInjection
|
||||
) : IAutocompleteProvider
|
||||
|
|
@ -262,13 +231,13 @@ public class InviteAutocompleteProvider(
|
|||
return [];
|
||||
}
|
||||
|
||||
var namedInvites = await db
|
||||
.Invites.Where(i =>
|
||||
i.GuildId == guildId.Value && i.Name.ToLower().StartsWith(userInput.ToLower())
|
||||
)
|
||||
// We're filtering and ordering on the client side because a guild won't have infinite invites
|
||||
// (the maximum on Discord's end is 1500-ish)
|
||||
// and this way we don't need an index on (guild_id, name) for this *one* case.
|
||||
var namedInvites = (await inviteRepository.GetGuildInvitesAsync(guildId))
|
||||
.Where(i => i.Name.StartsWith(userInput, StringComparison.InvariantCultureIgnoreCase))
|
||||
.OrderBy(i => i.Name)
|
||||
.Take(25)
|
||||
.ToListAsync(ct);
|
||||
.ToList();
|
||||
|
||||
if (namedInvites.Count != 0)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue