feat: add key role commands

This commit is contained in:
sam 2024-09-02 15:06:22 +02:00
parent 6e45b0f5b5
commit 086eb95452
Signed by: sam
GPG key ID: 5F3C3C1B3166639D
2 changed files with 90 additions and 0 deletions

View file

@ -0,0 +1,89 @@
using System.ComponentModel;
using Catalogger.Backend.Cache.InMemoryCache;
using Catalogger.Backend.Database;
using Catalogger.Backend.Database.Queries;
using Catalogger.Backend.Extensions;
using Remora.Commands.Attributes;
using Remora.Commands.Groups;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.API.Objects;
using Remora.Discord.Commands.Attributes;
using Remora.Discord.Commands.Feedback.Services;
using Remora.Discord.Commands.Services;
using Remora.Rest.Core;
using IResult = Remora.Results.IResult;
namespace Catalogger.Backend.Bot.Commands;
[Group("key-roles")]
[DiscordDefaultMemberPermissions(DiscordPermission.ManageGuild)]
public class KeyRoleCommands(
DatabaseContext db,
ContextInjectionService contextInjection,
IFeedbackService feedbackService,
GuildCache guildCache,
RoleCache roleCache) : CommandGroup
{
[Command("list")]
[Description("List this server's key roles.")]
public async Task<IResult> ListKeyRolesAsync()
{
var (_, guildId) = contextInjection.GetUserAndGuild();
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);
if (guildConfig.KeyRoles.Count == 0)
return await feedbackService.SendContextualAsync(
"There are no key roles to list. Add some with `/key-roles add`.");
var description = string.Join("\n", guildConfig.KeyRoles.Select(id =>
{
var role = guildRoles.FirstOrDefault(r => r.ID.Value == id);
return role != null ? $"- {role.Name} <@&{role.ID}>" : $"- unknown role {id}";
}));
return await feedbackService.SendContextualEmbedAsync(new Embed(
Title: $"Key roles for {guild.Name}",
Description: description,
Colour: DiscordUtils.Purple));
}
[Command("add")]
[Description("Add a new key role.")]
public async Task<IResult> AddKeyRoleAsync([DiscordTypeHint(TypeHint.Role)] Snowflake roleId)
{
var (_, guildId) = contextInjection.GetUserAndGuild();
var role = roleCache.GuildRoles(guildId).FirstOrDefault(r => r.ID == roleId);
if (role == null) throw new CataloggerError("Role is not cached");
var guildConfig = await db.GetGuildAsync(guildId);
if (guildConfig.KeyRoles.Any(id => role.ID == id))
return await feedbackService.SendContextualAsync($"{role.Name} is already a key role.");
guildConfig.KeyRoles.Add(role.ID.Value);
db.Update(guildConfig);
await db.SaveChangesAsync();
return await feedbackService.SendContextualAsync($"Added {role.Name} to this server's key roles!");
}
[Command("remove")]
[Description("Remove a key role.")]
public async Task<IResult> RemoveKeyRoleAsync([DiscordTypeHint(TypeHint.Role)] Snowflake roleId)
{
var (_, guildId) = contextInjection.GetUserAndGuild();
var role = roleCache.GuildRoles(guildId).FirstOrDefault(r => r.ID == roleId);
if (role == null) throw new CataloggerError("Role is not cached");
var guildConfig = await db.GetGuildAsync(guildId);
if (guildConfig.KeyRoles.All(id => role.ID != id))
return await feedbackService.SendContextualAsync($"{role.Name} is already not a key role.");
guildConfig.KeyRoles.Remove(role.ID.Value);
db.Update(guildConfig);
await db.SaveChangesAsync();
return await feedbackService.SendContextualAsync($"Removed {role.Name} from this server's key roles!");
}
}

View file

@ -45,6 +45,7 @@ builder.Host
// Start command tree
.WithCommandGroup<MetaCommands>()
.WithCommandGroup<ChannelCommands>()
.WithCommandGroup<KeyRoleCommands>()
// End command tree
.Finish()
.AddPagination()