feat: replace buttons in /configure-channels with select menu

This commit is contained in:
sam 2024-11-05 16:20:24 +01:00
parent e7eaa9f13a
commit f0fcfd7bd3
Signed by: sam
GPG key ID: 5F3C3C1B3166639D
4 changed files with 186 additions and 267 deletions

View file

@ -45,6 +45,103 @@ public class ChannelCommandsComponents(
{
private readonly ILogger _logger = logger.ForContext<ChannelCommandsComponents>();
[SelectMenu("select-log-type")]
[SuppressInteractionResponse(true)]
public async Task<Result> OnMenuSelectionAsync(IReadOnlyList<string> values)
{
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 guildRepository.GetAsync(guildId);
var result = await dataService.LeaseDataAsync(msg.ID);
await using var lease = result.GetOrThrow();
if (lease.Data.UserId != userId)
{
return (Result)
await feedbackService.ReplyAsync(
"This is not your configuration menu.",
isEphemeral: true
);
}
var state = values[0];
if (!Enum.TryParse<LogChannelType>(state, out var logChannelType))
throw new CataloggerError($"Invalid config-channels state {state}");
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}>";
List<IEmbed> embeds =
[
new Embed(
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
),
];
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"
)
),
}
),
];
lease.Data = new ChannelCommandData(userId, CurrentPage: state);
return await interactionApi.UpdateMessageAsync(
ctx.Interaction,
new InteractionMessageCallbackData(Embeds: embeds, Components: components)
);
}
[Button("config-channels")]
[SuppressInteractionResponse(true)]
public async Task<Result> OnButtonPressedAsync(string state)
@ -176,71 +273,7 @@ public class ChannelCommandsComponents(
return Result.Success;
}
if (!Enum.TryParse<LogChannelType>(state, out var logChannelType))
throw new CataloggerError($"Invalid config-channels state {state}");
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}>";
List<IEmbed> embeds =
[
new Embed(
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
),
];
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"
)
),
}
),
];
lease.Data = new ChannelCommandData(userId, CurrentPage: state);
return await interactionApi.UpdateMessageAsync(
ctx.Interaction,
new InteractionMessageCallbackData(Embeds: embeds, Components: components)
);
return Result.Success;
}
[SelectMenu("config-channels")]