BirthdayBot/ApplicationCommands/ModalResponder.cs
Noi c8d6a87966 Use a modal to configure announcement messages
In addition, wrote a new static class just for receiving ModalSubmitted events in case any more modals may be handled in the future
2022-03-13 21:24:17 -07:00

39 lines
1.8 KiB
C#

namespace BirthdayBot.ApplicationCommands;
/// <summary>
/// An instance-less class meant to handle incoming submitted modals.
/// </summary>
static class ModalResponder {
private delegate Task Responder(SocketModal modal, SocketGuildChannel channel,
Dictionary<string, SocketMessageComponentData> data);
internal static async Task DiscordClient_ModalSubmitted(ShardInstance inst, SocketModal arg) {
Responder handler = arg.Data.CustomId switch {
ConfigModule.SubCmdsConfigAnnounce.ModalCidAnnounce => ConfigModule.SubCmdsConfigAnnounce.CmdSetMessageResponse,
_ => DefaultHandler
};
var data = arg.Data.Components.ToDictionary(k => k.CustomId);
if (arg.Channel is not SocketGuildChannel channel) {
inst.Log(nameof(ModalResponder), $"Modal of type `{arg.Data.CustomId}` but channel data unavailable. " +
$"Sender ID {arg.User.Id}, name {arg.User}.");
await arg.RespondAsync(":x: Invalid request. Are you trying this command from a channel the bot can't see?")
.ConfigureAwait(false);
return;
}
try {
inst.Log(nameof(ModalResponder), $"Modal of type `{arg.Data.CustomId}` at {channel.Guild}!{arg.User}.");
await handler(arg, channel, data).ConfigureAwait(false);
} catch (Exception e) {
inst.Log(nameof(ModalResponder), $"Unhandled exception. {e}");
// TODO when implementing proper application error logging, see here
await arg.RespondAsync(ShardInstance.InternalError);
}
}
private static async Task DefaultHandler(SocketModal modal, SocketGuildChannel channel,
Dictionary<string, SocketMessageComponentData> data)
=> await modal.RespondAsync(":x: ...???");
}