2024-10-14 14:56:40 +02:00
|
|
|
// 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/>.
|
|
|
|
|
|
2024-09-02 15:06:22 +02:00
|
|
|
using System.ComponentModel;
|
|
|
|
|
using Catalogger.Backend.Cache.InMemoryCache;
|
2024-10-28 14:04:55 +01:00
|
|
|
using Catalogger.Backend.Database.Repositories;
|
2024-09-02 15:06:22 +02:00
|
|
|
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(
|
2024-10-28 02:14:41 +01:00
|
|
|
GuildRepository guildRepository,
|
2024-09-02 15:06:22 +02:00
|
|
|
ContextInjectionService contextInjection,
|
|
|
|
|
IFeedbackService feedbackService,
|
|
|
|
|
GuildCache guildCache,
|
2024-10-09 17:35:11 +02:00
|
|
|
RoleCache roleCache
|
|
|
|
|
) : CommandGroup
|
2024-09-02 15:06:22 +02:00
|
|
|
{
|
|
|
|
|
[Command("list")]
|
|
|
|
|
[Description("List this server's key roles.")]
|
|
|
|
|
public async Task<IResult> ListKeyRolesAsync()
|
|
|
|
|
{
|
|
|
|
|
var (_, guildId) = contextInjection.GetUserAndGuild();
|
2024-10-09 17:35:11 +02:00
|
|
|
if (!guildCache.TryGet(guildId, out var guild))
|
2025-03-20 15:24:23 +01:00
|
|
|
return CataloggerError.Result("Guild not in cache");
|
2024-09-02 15:06:22 +02:00
|
|
|
var guildRoles = roleCache.GuildRoles(guildId).ToList();
|
2024-10-28 02:14:41 +01:00
|
|
|
var guildConfig = await guildRepository.GetAsync(guildId);
|
2024-09-02 15:06:22 +02:00
|
|
|
|
2024-11-18 21:01:52 +01:00
|
|
|
if (guildConfig.KeyRoles.Count == 0)
|
2024-10-14 00:26:17 +02:00
|
|
|
return await feedbackService.ReplyAsync(
|
|
|
|
|
"There are no key roles to list. Add some with `/key-roles add`.",
|
|
|
|
|
isEphemeral: true
|
2024-10-09 17:35:11 +02:00
|
|
|
);
|
2024-09-02 15:06:22 +02:00
|
|
|
|
2024-10-09 17:35:11 +02:00
|
|
|
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}";
|
|
|
|
|
})
|
|
|
|
|
);
|
2024-09-02 15:06:22 +02:00
|
|
|
|
2024-10-09 17:35:11 +02:00
|
|
|
return await feedbackService.SendContextualEmbedAsync(
|
|
|
|
|
new Embed(
|
|
|
|
|
Title: $"Key roles for {guild.Name}",
|
|
|
|
|
Description: description,
|
|
|
|
|
Colour: DiscordUtils.Purple
|
|
|
|
|
)
|
|
|
|
|
);
|
2024-09-02 15:06:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Command("add")]
|
|
|
|
|
[Description("Add a new key role.")]
|
2024-10-06 02:00:59 +02:00
|
|
|
public async Task<IResult> AddKeyRoleAsync(
|
2024-10-29 22:18:30 +01:00
|
|
|
[Option("role")]
|
|
|
|
|
[Description("The role to add.")]
|
|
|
|
|
[DiscordTypeHint(TypeHint.Role)]
|
|
|
|
|
Snowflake roleId
|
2024-10-09 17:35:11 +02:00
|
|
|
)
|
2024-09-02 15:06:22 +02:00
|
|
|
{
|
|
|
|
|
var (_, guildId) = contextInjection.GetUserAndGuild();
|
|
|
|
|
var role = roleCache.GuildRoles(guildId).FirstOrDefault(r => r.ID == roleId);
|
2024-10-09 17:35:11 +02:00
|
|
|
if (role == null)
|
2025-03-20 15:24:23 +01:00
|
|
|
return CataloggerError.Result("Role is not cached");
|
2024-09-02 15:06:22 +02:00
|
|
|
|
2024-10-28 02:14:41 +01:00
|
|
|
var guildConfig = await guildRepository.GetAsync(guildId);
|
|
|
|
|
if (guildConfig.KeyRoles.Any(id => role.ID.Value == id))
|
2024-10-14 00:26:17 +02:00
|
|
|
return await feedbackService.ReplyAsync(
|
|
|
|
|
$"{role.Name} is already a key role.",
|
|
|
|
|
isEphemeral: true
|
|
|
|
|
);
|
2024-09-02 15:06:22 +02:00
|
|
|
|
2024-11-18 21:01:52 +01:00
|
|
|
guildConfig.KeyRoles.Add(role.ID.Value);
|
|
|
|
|
await guildRepository.UpdateConfigAsync(guildId, guildConfig);
|
2024-10-14 00:26:17 +02:00
|
|
|
return await feedbackService.ReplyAsync($"Added {role.Name} to this server's key roles!");
|
2024-09-02 15:06:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Command("remove")]
|
|
|
|
|
[Description("Remove a key role.")]
|
2024-10-06 02:00:59 +02:00
|
|
|
public async Task<IResult> RemoveKeyRoleAsync(
|
2024-10-29 22:18:30 +01:00
|
|
|
[Option("role")]
|
|
|
|
|
[Description("The role to remove.")]
|
|
|
|
|
[DiscordTypeHint(TypeHint.Role)]
|
|
|
|
|
Snowflake roleId
|
2024-10-09 17:35:11 +02:00
|
|
|
)
|
2024-09-02 15:06:22 +02:00
|
|
|
{
|
|
|
|
|
var (_, guildId) = contextInjection.GetUserAndGuild();
|
|
|
|
|
var role = roleCache.GuildRoles(guildId).FirstOrDefault(r => r.ID == roleId);
|
2024-10-09 17:35:11 +02:00
|
|
|
if (role == null)
|
2025-03-20 15:24:23 +01:00
|
|
|
return CataloggerError.Result("Role is not cached");
|
2024-09-02 15:06:22 +02:00
|
|
|
|
2024-10-28 02:14:41 +01:00
|
|
|
var guildConfig = await guildRepository.GetAsync(guildId);
|
2024-09-02 15:06:22 +02:00
|
|
|
if (guildConfig.KeyRoles.All(id => role.ID != id))
|
2024-10-14 00:26:17 +02:00
|
|
|
return await feedbackService.ReplyAsync(
|
|
|
|
|
$"{role.Name} is already not a key role.",
|
|
|
|
|
isEphemeral: true
|
2024-10-09 17:35:11 +02:00
|
|
|
);
|
2024-09-02 15:06:22 +02:00
|
|
|
|
2024-11-18 21:01:52 +01:00
|
|
|
guildConfig.KeyRoles.Remove(role.ID.Value);
|
|
|
|
|
await guildRepository.UpdateConfigAsync(guildId, guildConfig);
|
2024-10-14 00:26:17 +02:00
|
|
|
return await feedbackService.ReplyAsync(
|
2024-10-09 17:35:11 +02:00
|
|
|
$"Removed {role.Name} from this server's key roles!"
|
|
|
|
|
);
|
2024-09-02 15:06:22 +02:00
|
|
|
}
|
2024-10-09 17:35:11 +02:00
|
|
|
}
|