Skip to content

Commit b17fa05

Browse files
committed
✨feat: unify ephemeral dialog responses with in perms files
1 parent c21560c commit b17fa05

2 files changed

Lines changed: 32 additions & 63 deletions

File tree

src/boost/coop_status_permission.go

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,19 @@ func ShowCoopStatusPermissionDialog(s *discordgo.Session, i *discordgo.Interacti
109109
func HandleCoopStatusPermissionButton(s *discordgo.Session, i *discordgo.InteractionCreate) {
110110
userID := bottools.GetInteractionUserID(i)
111111
customID := i.MessageComponentData().CustomID
112+
respondAndClose := func(content string) {
113+
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
114+
Type: discordgo.InteractionResponseUpdateMessage,
115+
Data: &discordgo.InteractionResponseData{
116+
Content: content,
117+
Flags: discordgo.MessageFlagsEphemeral,
118+
Components: []discordgo.MessageComponent{},
119+
},
120+
})
121+
if err != nil {
122+
log.Println("Error updating coop status permission dialog:", err)
123+
}
124+
}
112125

113126
// Extract the action part after the "#"
114127
parts := strings.Split(customID, "#")
@@ -123,46 +136,16 @@ func HandleCoopStatusPermissionButton(s *discordgo.Session, i *discordgo.Interac
123136
farmerstate.SetMiscSettingString(userID, CoopStatusPermissionKey, time.Now().Format(time.RFC3339))
124137
farmerstate.SetMiscSettingString(userID, CoopStatusPermissionSpanKey, "24h")
125138

126-
// Respond with ephemeral message asking to retry
127-
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
128-
Type: discordgo.InteractionResponseChannelMessageWithSource,
129-
Data: &discordgo.InteractionResponseData{
130-
Content: "Permission granted for 24 hours. You can now run your command again.",
131-
Flags: discordgo.MessageFlagsEphemeral,
132-
},
133-
})
134-
if err != nil {
135-
log.Println("Error responding to allow button:", err)
136-
}
139+
respondAndClose("Permission granted for 24 hours. You can now run your command again.")
137140

138141
case "allow7d":
139142
// Set the timestamp to now
140143
farmerstate.SetMiscSettingString(userID, CoopStatusPermissionKey, time.Now().Format(time.RFC3339))
141144
farmerstate.SetMiscSettingString(userID, CoopStatusPermissionSpanKey, "7d")
142145

143-
// Respond with ephemeral message asking to retry
144-
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
145-
Type: discordgo.InteractionResponseChannelMessageWithSource,
146-
Data: &discordgo.InteractionResponseData{
147-
Content: "Permission granted for 7 days. You can now run your command again.",
148-
Flags: discordgo.MessageFlagsEphemeral,
149-
},
150-
})
151-
if err != nil {
152-
log.Println("Error responding to allow 7d button:", err)
153-
}
146+
respondAndClose("Permission granted for 7 days. You can now run your command again.")
154147

155148
case "close":
156-
// Respond with ephemeral close message
157-
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
158-
Type: discordgo.InteractionResponseChannelMessageWithSource,
159-
Data: &discordgo.InteractionResponseData{
160-
Content: "Dialog closed. You can enable this permission later when you're ready.",
161-
Flags: discordgo.MessageFlagsEphemeral,
162-
},
163-
})
164-
if err != nil {
165-
log.Println("Error responding to close button:", err)
166-
}
149+
respondAndClose("Dialog closed. You can enable this permission later when you're ready.")
167150
}
168151
}

src/boost/leaderboard_permission.go

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,19 @@ func ShowLeaderboardPermissionDialog(s *discordgo.Session, i *discordgo.Interact
8888
func HandleLeaderboardPermissionButton(s *discordgo.Session, i *discordgo.InteractionCreate) {
8989
userID := bottools.GetInteractionUserID(i)
9090
customID := i.MessageComponentData().CustomID
91+
respondAndClose := func(content string) {
92+
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
93+
Type: discordgo.InteractionResponseUpdateMessage,
94+
Data: &discordgo.InteractionResponseData{
95+
Content: content,
96+
Flags: discordgo.MessageFlagsEphemeral,
97+
Components: []discordgo.MessageComponent{},
98+
},
99+
})
100+
if err != nil {
101+
log.Println("Error updating leaderboard permission dialog:", err)
102+
}
103+
}
91104

92105
parts := strings.Split(customID, "#")
93106
if len(parts) < 2 {
@@ -100,42 +113,15 @@ func HandleLeaderboardPermissionButton(s *discordgo.Session, i *discordgo.Intera
100113
farmerstate.SetMiscSettingString(userID, LeaderboardPermissionKey, time.Now().Format(time.RFC3339))
101114
farmerstate.SetMiscSettingString(userID, LeaderboardPermissionSpanKey, "24h")
102115

103-
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
104-
Type: discordgo.InteractionResponseChannelMessageWithSource,
105-
Data: &discordgo.InteractionResponseData{
106-
Content: "Permission granted for 24 hours. Please run your command again.",
107-
Flags: discordgo.MessageFlagsEphemeral,
108-
},
109-
})
110-
if err != nil {
111-
log.Println("Error responding to leaderboard allow24h button:", err)
112-
}
116+
respondAndClose("Permission granted for 24 hours. Please run your command again.")
113117

114118
case "allowforever":
115119
farmerstate.SetMiscSettingString(userID, LeaderboardPermissionKey, time.Now().Format(time.RFC3339))
116120
farmerstate.SetMiscSettingString(userID, LeaderboardPermissionSpanKey, "forever")
117121

118-
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
119-
Type: discordgo.InteractionResponseChannelMessageWithSource,
120-
Data: &discordgo.InteractionResponseData{
121-
Content: "Permission granted permanently. Please run your command again.",
122-
Flags: discordgo.MessageFlagsEphemeral,
123-
},
124-
})
125-
if err != nil {
126-
log.Println("Error responding to leaderboard allowforever button:", err)
127-
}
122+
respondAndClose("Permission granted permanently. Please run your command again.")
128123

129124
case "close":
130-
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
131-
Type: discordgo.InteractionResponseChannelMessageWithSource,
132-
Data: &discordgo.InteractionResponseData{
133-
Content: "Dialog closed.",
134-
Flags: discordgo.MessageFlagsEphemeral,
135-
},
136-
})
137-
if err != nil {
138-
log.Println("Error responding to leaderboard close button:", err)
139-
}
125+
respondAndClose("Dialog closed.")
140126
}
141127
}

0 commit comments

Comments
 (0)