add interactive button to cancel last minute

new-sql-view
William Perron 11 months ago
parent b292db1050
commit 07252229e0
Signed by: wperron
GPG Key ID: BFDB4EF72D73C5F2

@ -188,6 +188,12 @@ func main() {
Description: "Get the schedule for the following weeks.", Description: "Get the schedule for the following weeks.",
Type: discordgo.ChatApplicationCommand, 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", Name: "absent",
Description: "Mark yourself as absent for a session", Description: "Mark yourself as absent for a session",
@ -561,6 +567,18 @@ func main() {
log.Error().Err(err).Msg("failed to respond to interaction") 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) { "absent": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
var rawDate string var rawDate string
if len(i.ApplicationCommandData().Options) == 0 { if len(i.ApplicationCommandData().Options) == 0 {
@ -661,13 +679,28 @@ func main() {
} }
var msg string var msg string
var components []discordgo.MessageComponent
if len(absentees) == 0 { if len(absentees) == 0 {
msg = "Everybody can make it next Monday, see you then!" 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 { } else {
msg = fmt.Sprintf("%s can't make it next Monday. :sad:", themis.FormatStringSlice(absentees)) 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 { if err != nil {
log.Error().Err(err).Msg("failed to send scheduled notification") log.Error().Err(err).Msg("failed to send scheduled notification")
} }
@ -743,6 +776,32 @@ func registerHandlers(sess *discordgo.Session, handlers map[string]Handler) {
} }
return 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")
}
}
} }
}) })
} }

@ -26,15 +26,6 @@ DISCORD_BOT_CHANNEL_ID = "1018997240968265768"
soft_limit = 20 soft_limit = 20
type = "connections" type = "connections"
[[services.ports]]
force_https = true
handlers = ["http"]
port = 80
[[services.ports]]
handlers = ["tls", "http"]
port = 443
[[services.tcp_checks]] [[services.tcp_checks]]
grace_period = "1s" grace_period = "1s"
interval = "15s" interval = "15s"

@ -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()) { func (n *Notifier) NotifyFunc(ctx context.Context, f func()) {
for { for {
select { select {

Loading…
Cancel
Save