refactor: return CataloggerError as Results instead of throwing in commands

This commit is contained in:
sam 2025-03-20 15:24:23 +01:00
parent 84c3b42874
commit 8a4e3ff184
Signed by: sam
GPG key ID: 5F3C3C1B3166639D
11 changed files with 57 additions and 36 deletions

View file

@ -43,6 +43,7 @@ public class ChannelCommands(
Config config,
GuildRepository guildRepository,
GuildCache guildCache,
GuildFetchService guildFetchService,
ChannelCache channelCache,
IMemberCache memberCache,
IFeedbackService feedbackService,
@ -68,8 +69,11 @@ public class ChannelCommands(
public async Task<IResult> CheckPermissionsAsync()
{
var (userId, guildId) = contextInjection.GetUserAndGuild();
if (!guildCache.TryGet(guildId, out var guild))
throw new CataloggerError("Guild not in cache");
{
return CataloggerError.Result($"Guild {guildId} not in cache");
}
var embed = new EmbedBuilder().WithTitle($"Permission check for {guild.Name}");
@ -78,8 +82,18 @@ public class ChannelCommands(
DiscordSnowflake.New(config.Discord.ApplicationId)
);
var currentUser = await memberCache.TryGetAsync(guildId, userId);
if (botUser == null || currentUser == null)
throw new CataloggerError("Bot member or invoking member not found in cache");
{
// If this happens, something has gone wrong when fetching members. Refetch the guild's members.
guildFetchService.EnqueueGuild(guildId);
_logger.Error(
"Either our own user {BotId} or the invoking user {UserId} is not in cache, aborting permission check",
config.Discord.ApplicationId,
userId
);
return CataloggerError.Result("Bot member or invoking member not found in cache");
}
// We don't want to check categories or threads
var guildChannels = channelCache
@ -204,7 +218,7 @@ public class ChannelCommands(
{
var (userId, guildId) = contextInjection.GetUserAndGuild();
if (!guildCache.TryGet(guildId, out var guild))
throw new CataloggerError("Guild not in cache");
return CataloggerError.Result("Guild not in cache");
var guildChannels = channelCache.GuildChannels(guildId).ToList();
var guildConfig = await guildRepository.GetAsync(guildId);