From 905e761527bd0e9b4e1aa82da225cb476ddec7dd Mon Sep 17 00:00:00 2001 From: Mark Ignacio Date: Sun, 11 Feb 2024 10:31:08 -0600 Subject: [PATCH] feat: deprecation warning on /gentei manage (#318) --- gentei/bot/commands.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/gentei/bot/commands.go b/gentei/bot/commands.go index a66ce59..0c57751 100644 --- a/gentei/bot/commands.go +++ b/gentei/bot/commands.go @@ -543,6 +543,7 @@ func (b *DiscordBot) handleManage(ctx context.Context, i *discordgo.InteractionC b.replyNoDM(i) return } + ctx = deprecatedContext(ctx) b.deferredReply(ctx, i, "manage", true, // the calling user has to be an admin of some sort to run this command func(logger zerolog.Logger) (*discordgo.WebhookEdit, error) { @@ -570,6 +571,23 @@ func (b *DiscordBot) handleManage(ctx context.Context, i *discordgo.InteractionC } // helpers +type ctxKey int + +// Context key whose value should specifically not be nil to be set +var manageCommandDeprecated ctxKey + +func deprecatedContext(ctx context.Context) context.Context { + return context.WithValue(ctx, manageCommandDeprecated, true) +} + +func isDeprecatedContext(ctx context.Context) bool { + value, _ := ctx.Value(manageCommandDeprecated).(bool) + return value +} + +const ( + manageDeprecatedPrefix = "⚠️ `/gentei manage` will be removed soon. Please use the replacement commands `/gentei-audit` and `/gentei-map`, which are admin-only by default! ⚠️" +) // deferredReply can only be called once, but it'll process each responseFunc in serial. If it gets a WebHookEdit payload, it sends that and stops processing later responseFuncs. func (b *DiscordBot) deferredReply(ctx context.Context, i *discordgo.InteractionCreate, commandName string, ephemeral bool, responseFuncs ...slashResponseFunc) { @@ -614,6 +632,9 @@ func (b *DiscordBot) deferredReply(ctx context.Context, i *discordgo.Interaction if response == nil { continue } + if isDeprecatedContext(ctx) && response.Content != nil { + response.Content = ptr(fmt.Sprintf("%s\n\n%s", manageDeprecatedPrefix, *response.Content)) + } _, err = b.session.InteractionResponseEdit(i.Interaction, response) if err != nil { logger.Err(err).Msg("error generating response")