From 07252229e0886adab4c8db16ba8d3a82f9e9bde2 Mon Sep 17 00:00:00 2001 From: William Perron Date: Sun, 24 Dec 2023 12:14:25 -0500 Subject: [PATCH] add interactive button to cancel last minute --- cmd/themis-server/main.go | 61 ++++++++++++++++++++++++++++++++++++++- fly.toml | 9 ------ notify.go | 5 ++++ 3 files changed, 65 insertions(+), 10 deletions(-) diff --git a/cmd/themis-server/main.go b/cmd/themis-server/main.go index cfc011f..9e004fb 100644 --- a/cmd/themis-server/main.go +++ b/cmd/themis-server/main.go @@ -188,6 +188,12 @@ func main() { Description: "Get the schedule for the following weeks.", Type: discordgo.ChatApplicationCommand, }, + { + Name: "send-schedule", + Description: "Trigger the scheduled message. Admins only", + Type: discordgo.ChatApplicationCommand, + DefaultMemberPermissions: new(int64), // default 0 == admins only + }, { Name: "absent", Description: "Mark yourself as absent for a session", @@ -561,6 +567,18 @@ func main() { log.Error().Err(err).Msg("failed to respond to interaction") } }, + "send-schedule": func(s *discordgo.Session, i *discordgo.InteractionCreate) { + notifier.Send() + + if err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "Done.", + }, + }); err != nil { + log.Error().Err(err).Msg("failed to respond to interaction") + } + }, "absent": func(s *discordgo.Session, i *discordgo.InteractionCreate) { var rawDate string if len(i.ApplicationCommandData().Options) == 0 { @@ -661,13 +679,28 @@ func main() { } var msg string + var components []discordgo.MessageComponent if len(absentees) == 0 { msg = "Everybody can make it next Monday, see you then!" + components = []discordgo.MessageComponent{ + discordgo.ActionsRow{ + Components: []discordgo.MessageComponent{ + discordgo.Button{ + CustomID: "schedule-response", + Label: "I Can't Make It", + Style: discordgo.DangerButton, + }, + }, + }, + } } else { msg = fmt.Sprintf("%s can't make it next Monday. :sad:", themis.FormatStringSlice(absentees)) } - _, err = discord.ChannelMessageSend(channelId, msg) + _, err = discord.ChannelMessageSendComplex(channelId, &discordgo.MessageSend{ + Content: msg, + Components: components, + }) if err != nil { log.Error().Err(err).Msg("failed to send scheduled notification") } @@ -743,6 +776,32 @@ func registerHandlers(sess *discordgo.Session, handlers map[string]Handler) { } return } + case discordgo.InteractionMessageComponent: + switch i.MessageComponentData().CustomID { + case "schedule-response": + userId := i.Member.User.ID + if err := store.AddAbsence(context.TODO(), themis.NextMonday(), userId); err != nil { + if err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "something went wrong recording your absence, check logs for more info.", + }, + }); err != nil { + log.Error().Err(err).Msg("failed to respond to interaction") + } + return + } + + err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: fmt.Sprintf("Looks like <@%s> can't make it after all.", userId), + }, + }) + if err != nil { + log.Error().Err(err).Msg("failed to respond to interaction") + } + } } }) } diff --git a/fly.toml b/fly.toml index 921e62a..138107e 100644 --- a/fly.toml +++ b/fly.toml @@ -26,15 +26,6 @@ DISCORD_BOT_CHANNEL_ID = "1018997240968265768" soft_limit = 20 type = "connections" - [[services.ports]] - force_https = true - handlers = ["http"] - port = 80 - - [[services.ports]] - handlers = ["tls", "http"] - port = 443 - [[services.tcp_checks]] grace_period = "1s" interval = "15s" diff --git a/notify.go b/notify.go index a3c59ff..737ce5d 100644 --- a/notify.go +++ b/notify.go @@ -63,6 +63,11 @@ func (n *Notifier) Start(ctx context.Context) { } } +// Trigger the notifier manually. Should be used for testing purposes only. +func (n *Notifier) Send() { + n.c <- struct{}{} +} + func (n *Notifier) NotifyFunc(ctx context.Context, f func()) { for { select {