55 "errors"
66 "fmt"
77
8+ "git.handmade.network/hmn/hmn/src/config"
89 "git.handmade.network/hmn/hmn/src/hmndata"
910
1011 "git.handmade.network/hmn/hmn/src/db"
@@ -18,6 +19,8 @@ const SlashCommandProfile = "profile"
1819const ProfileOptionUser = "user"
1920
2021const SlashCommandManifesto = "manifesto"
22+ const SlashCommandHMHReplay = "hmhreplay"
23+ const HMHReplayOptionToggle = "toggle"
2124
2225// User command names
2326const UserCommandProfile = "HMN Profile"
@@ -54,6 +57,22 @@ func (bot *botInstance) createApplicationCommands(ctx context.Context) {
5457 Name : SlashCommandManifesto ,
5558 Description : "Read the Handmade manifesto" ,
5659 }))
60+
61+ dmPermission := false
62+ doOrWarn (CreateGuildApplicationCommand (ctx , CreateGuildApplicationCommandRequest {
63+ Type : ApplicationCommandTypeChatInput ,
64+ Name : SlashCommandHMHReplay ,
65+ Description : "Join the Handmade Hero Replay" ,
66+ DMPermission : & dmPermission ,
67+ Options : []ApplicationCommandOption {
68+ {
69+ Type : ApplicationCommandOptionTypeBoolean ,
70+ Name : HMHReplayOptionToggle ,
71+ Description : "Add or remove the HMH replay role" ,
72+ Required : false ,
73+ },
74+ },
75+ }))
5776}
5877
5978func (bot * botInstance ) doInteraction (ctx context.Context , i * Interaction ) {
@@ -87,6 +106,41 @@ func (bot *botInstance) doInteraction(ctx context.Context, i *Interaction) {
87106 if err != nil {
88107 logging .ExtractLogger (ctx ).Error ().Err (err ).Msg ("failed to send manifesto response" )
89108 }
109+ case SlashCommandHMHReplay :
110+ join := true
111+ toggleOpt , ok := getInteractionOption (i .Data .Options , HMHReplayOptionToggle )
112+ if ok {
113+ join = toggleOpt .Value .(bool )
114+ }
115+
116+ if join {
117+ logging .ExtractLogger (ctx ).Debug ().Interface ("interaction" , i ).Msg ("Got hmh replay" )
118+ err := AddGuildMemberRole (ctx , i .Member .User .ID , config .Config .Discord .HMHReplayRoleID )
119+ if err != nil {
120+ err = sendEphemeralMessageForInteraction (ctx , i , "We failed to set the role. Please inform an admin." )
121+ if err != nil {
122+ logging .ExtractLogger (ctx ).Error ().Err (err ).Msg ("failed to send hmh replay add failure response" )
123+ }
124+ } else {
125+ err = sendEphemeralMessageForInteraction (ctx , i , "You're in!" )
126+ if err != nil {
127+ logging .ExtractLogger (ctx ).Error ().Err (err ).Msg ("failed to send hmh replay add response" )
128+ }
129+ }
130+ } else {
131+ err := RemoveGuildMemberRole (ctx , i .Member .User .ID , config .Config .Discord .HMHReplayRoleID )
132+ if err != nil {
133+ err = sendEphemeralMessageForInteraction (ctx , i , "We failed to remove the role. Please inform an admin." )
134+ if err != nil {
135+ logging .ExtractLogger (ctx ).Error ().Err (err ).Msg ("failed to send hmh replay remove failure response" )
136+ }
137+ } else {
138+ err = sendEphemeralMessageForInteraction (ctx , i , "You're out!" )
139+ if err != nil {
140+ logging .ExtractLogger (ctx ).Error ().Err (err ).Msg ("failed to send hmh replay remove response" )
141+ }
142+ }
143+ }
90144 default :
91145 logging .ExtractLogger (ctx ).Warn ().Str ("name" , i .Data .Name ).Msg ("didn't recognize Discord interaction name" )
92146 }
@@ -173,6 +227,18 @@ func (bot *botInstance) handleProfileCommand(ctx context.Context, i *Interaction
173227 }
174228}
175229
230+ func sendEphemeralMessageForInteraction (ctx context.Context , i * Interaction , content string ) error {
231+ err := CreateInteractionResponse (ctx , i .ID , i .Token , InteractionResponse {
232+ Type : InteractionCallbackTypeChannelMessageWithSource ,
233+ Data : & InteractionCallbackData {
234+ Content : content ,
235+ Flags : FlagEphemeral ,
236+ },
237+ })
238+
239+ return err
240+ }
241+
176242func getInteractionOption (opts []ApplicationCommandInteractionDataOption , name string ) (ApplicationCommandInteractionDataOption , bool ) {
177243 for _ , opt := range opts {
178244 if opt .Name == name {
0 commit comments