chore: format with csharpier

This commit is contained in:
sam 2024-10-09 17:35:11 +02:00
parent 2f516dcb73
commit 4f54077c68
59 changed files with 2000 additions and 942 deletions

View file

@ -27,7 +27,8 @@ public class ChannelCommandsComponents(
ContextInjectionService contextInjection,
IFeedbackService feedbackService,
IDiscordRestInteractionAPI interactionApi,
InMemoryDataService<Snowflake, ChannelCommandData> dataService) : InteractionGroup
InMemoryDataService<Snowflake, ChannelCommandData> dataService
) : InteractionGroup
{
private readonly ILogger _logger = logger.ForContext<ChannelCommandsComponents>();
@ -35,11 +36,16 @@ public class ChannelCommandsComponents(
[SuppressInteractionResponse(true)]
public async Task<Result> OnButtonPressedAsync(string state)
{
if (contextInjection.Context is not IInteractionCommandContext ctx) throw new CataloggerError("No context");
if (!ctx.TryGetUserID(out var userId)) throw new CataloggerError("No user ID in context");
if (!ctx.Interaction.Message.TryGet(out var msg)) throw new CataloggerError("No message ID in context");
if (!ctx.TryGetGuildID(out var guildId)) throw new CataloggerError("No guild ID in context");
if (!guildCache.TryGet(guildId, out var guild)) throw new CataloggerError("Guild not in cache");
if (contextInjection.Context is not IInteractionCommandContext ctx)
throw new CataloggerError("No context");
if (!ctx.TryGetUserID(out var userId))
throw new CataloggerError("No user ID in context");
if (!ctx.Interaction.Message.TryGet(out var msg))
throw new CataloggerError("No message ID in context");
if (!ctx.TryGetGuildID(out var guildId))
throw new CataloggerError("No guild ID in context");
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);
@ -47,20 +53,27 @@ public class ChannelCommandsComponents(
await using var lease = result.GetOrThrow();
if (lease.Data.UserId != userId)
{
return (Result)await feedbackService.SendContextualAsync("This is not your configuration menu.",
options: new FeedbackMessageOptions(MessageFlags: MessageFlags.Ephemeral));
return (Result)
await feedbackService.SendContextualAsync(
"This is not your configuration menu.",
options: new FeedbackMessageOptions(MessageFlags: MessageFlags.Ephemeral)
);
}
switch (state)
{
case "close":
return await interactionApi.UpdateMessageAsync(ctx.Interaction,
new InteractionMessageCallbackData(Components: Array.Empty<IMessageComponent>()));
return await interactionApi.UpdateMessageAsync(
ctx.Interaction,
new InteractionMessageCallbackData(Components: Array.Empty<IMessageComponent>())
);
case "reset":
if (lease.Data.CurrentPage == null)
throw new CataloggerError("CurrentPage was null in reset button callback");
if (!Enum.TryParse<LogChannelType>(lease.Data.CurrentPage, out var channelType))
throw new CataloggerError($"Invalid config-channels CurrentPage: '{lease.Data.CurrentPage}'");
throw new CataloggerError(
$"Invalid config-channels CurrentPage: '{lease.Data.CurrentPage}'"
);
// TODO: figure out some way to make this less verbose?
switch (channelType)
@ -140,8 +153,10 @@ public class ChannelCommandsComponents(
goto case "return";
case "return":
var (e, c) = ChannelCommands.BuildRootMenu(guildChannels, guild, guildConfig);
await interactionApi.UpdateMessageAsync(ctx.Interaction,
new InteractionMessageCallbackData(Embeds: e, Components: c));
await interactionApi.UpdateMessageAsync(
ctx.Interaction,
new InteractionMessageCallbackData(Embeds: e, Components: c)
);
lease.Data = new ChannelCommandData(userId, CurrentPage: null);
return Result.Success;
}
@ -151,9 +166,12 @@ public class ChannelCommandsComponents(
var channelId = WebhookExecutorService.GetDefaultLogChannel(guildConfig, logChannelType);
string? channelMention;
if (channelId is 0) channelMention = null;
else if (guildChannels.All(c => c.ID != channelId)) channelMention = $"unknown channel {channelId}";
else channelMention = $"<#{channelId}>";
if (channelId is 0)
channelMention = null;
else if (guildChannels.All(c => c.ID != channelId))
channelMention = $"unknown channel {channelId}";
else
channelMention = $"<#{channelId}>";
List<IEmbed> embeds =
[
@ -161,43 +179,69 @@ public class ChannelCommandsComponents(
Title: ChannelCommands.PrettyLogTypeName(logChannelType),
Description: channelMention == null
? "This event is not currently logged.\nTo start logging it somewhere, select a channel below."
: $"This event is currently set to log to {channelMention}." +
"\nTo change where it is logged, select a channel below." +
"\nTo disable logging this event entirely, select \"Stop logging\" below.",
Colour: DiscordUtils.Purple)
: $"This event is currently set to log to {channelMention}."
+ "\nTo change where it is logged, select a channel below."
+ "\nTo disable logging this event entirely, select \"Stop logging\" below.",
Colour: DiscordUtils.Purple
),
];
List<IMessageComponent> components =
[
new ActionRowComponent(new[]
{
new ChannelSelectComponent(CustomID: CustomIDHelpers.CreateSelectMenuID("config-channels"),
ChannelTypes: new[] { ChannelType.GuildText })
}),
new ActionRowComponent(new[]
{
new ButtonComponent(ButtonComponentStyle.Danger, Label: "Stop logging",
CustomID: CustomIDHelpers.CreateButtonIDWithState("config-channels", "reset"),
IsDisabled: channelMention == null),
new ButtonComponent(ButtonComponentStyle.Secondary, Label: "Return to menu",
CustomID: CustomIDHelpers.CreateButtonIDWithState("config-channels", "return"))
})
new ActionRowComponent(
new[]
{
new ChannelSelectComponent(
CustomID: CustomIDHelpers.CreateSelectMenuID("config-channels"),
ChannelTypes: new[] { ChannelType.GuildText }
),
}
),
new ActionRowComponent(
new[]
{
new ButtonComponent(
ButtonComponentStyle.Danger,
Label: "Stop logging",
CustomID: CustomIDHelpers.CreateButtonIDWithState(
"config-channels",
"reset"
),
IsDisabled: channelMention == null
),
new ButtonComponent(
ButtonComponentStyle.Secondary,
Label: "Return to menu",
CustomID: CustomIDHelpers.CreateButtonIDWithState(
"config-channels",
"return"
)
),
}
),
];
lease.Data = new ChannelCommandData(userId, CurrentPage: state);
return await interactionApi.UpdateMessageAsync(ctx.Interaction,
new InteractionMessageCallbackData(Embeds: embeds, Components: components));
return await interactionApi.UpdateMessageAsync(
ctx.Interaction,
new InteractionMessageCallbackData(Embeds: embeds, Components: components)
);
}
[SelectMenu("config-channels")]
[SuppressInteractionResponse(true)]
public async Task<Result> OnMenuSelectionAsync(IReadOnlyList<IPartialChannel> channels)
{
if (contextInjection.Context is not IInteractionCommandContext ctx) throw new CataloggerError("No context");
if (!ctx.TryGetUserID(out var userId)) throw new CataloggerError("No user ID in context");
if (!ctx.Interaction.Message.TryGet(out var msg)) throw new CataloggerError("No message ID in context");
if (!ctx.TryGetGuildID(out var guildId)) throw new CataloggerError("No guild ID in context");
if (!guildCache.TryGet(guildId, out var guild)) throw new CataloggerError("Guild not in cache");
if (contextInjection.Context is not IInteractionCommandContext ctx)
throw new CataloggerError("No context");
if (!ctx.TryGetUserID(out var userId))
throw new CataloggerError("No user ID in context");
if (!ctx.Interaction.Message.TryGet(out var msg))
throw new CataloggerError("No message ID in context");
if (!ctx.TryGetGuildID(out var guildId))
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 channelId = channels[0].ID.ToUlong();
@ -205,12 +249,17 @@ public class ChannelCommandsComponents(
await using var lease = result.GetOrThrow();
if (lease.Data.UserId != userId)
{
return (Result)await feedbackService.SendContextualAsync("This is not your configuration menu.",
options: new FeedbackMessageOptions(MessageFlags: MessageFlags.Ephemeral));
return (Result)
await feedbackService.SendContextualAsync(
"This is not your configuration menu.",
options: new FeedbackMessageOptions(MessageFlags: MessageFlags.Ephemeral)
);
}
if (!Enum.TryParse<LogChannelType>(lease.Data.CurrentPage, out var channelType))
throw new CataloggerError($"Invalid config-channels CurrentPage '{lease.Data.CurrentPage}'");
throw new CataloggerError(
$"Invalid config-channels CurrentPage '{lease.Data.CurrentPage}'"
);
switch (channelType)
{
@ -291,32 +340,53 @@ public class ChannelCommandsComponents(
[
new Embed(
Title: ChannelCommands.PrettyLogTypeName(channelType),
Description: $"This event is currently set to log to <#{channelId}>." +
"\nTo change where it is logged, select a channel below." +
"\nTo disable logging this event entirely, select \"Stop logging\" below.",
Colour: DiscordUtils.Purple)
Description: $"This event is currently set to log to <#{channelId}>."
+ "\nTo change where it is logged, select a channel below."
+ "\nTo disable logging this event entirely, select \"Stop logging\" below.",
Colour: DiscordUtils.Purple
),
];
List<IMessageComponent> components =
[
new ActionRowComponent(new[]
{
new ChannelSelectComponent(CustomID: CustomIDHelpers.CreateSelectMenuID("config-channels"),
ChannelTypes: new[] { ChannelType.GuildText })
}),
new ActionRowComponent(new[]
{
new ButtonComponent(ButtonComponentStyle.Danger, Label: "Stop logging",
CustomID: CustomIDHelpers.CreateButtonIDWithState("config-channels", "reset")),
new ButtonComponent(ButtonComponentStyle.Secondary, Label: "Return to menu",
CustomID: CustomIDHelpers.CreateButtonIDWithState("config-channels", "return"))
})
new ActionRowComponent(
new[]
{
new ChannelSelectComponent(
CustomID: CustomIDHelpers.CreateSelectMenuID("config-channels"),
ChannelTypes: new[] { ChannelType.GuildText }
),
}
),
new ActionRowComponent(
new[]
{
new ButtonComponent(
ButtonComponentStyle.Danger,
Label: "Stop logging",
CustomID: CustomIDHelpers.CreateButtonIDWithState(
"config-channels",
"reset"
)
),
new ButtonComponent(
ButtonComponentStyle.Secondary,
Label: "Return to menu",
CustomID: CustomIDHelpers.CreateButtonIDWithState(
"config-channels",
"return"
)
),
}
),
];
lease.Data = lease.Data with { UserId = userId };
return await interactionApi.UpdateMessageAsync(ctx.Interaction,
new InteractionMessageCallbackData(Embeds: embeds, Components: components));
return await interactionApi.UpdateMessageAsync(
ctx.Interaction,
new InteractionMessageCallbackData(Embeds: embeds, Components: components)
);
}
}
public record ChannelCommandData(Snowflake UserId, string? CurrentPage);
public record ChannelCommandData(Snowflake UserId, string? CurrentPage);