Skip to content

Commit

Permalink
Fix: message timeout on discord
Browse files Browse the repository at this point in the history
  • Loading branch information
tr4cks committed Jan 10, 2025
1 parent a7e281d commit 60752e2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 34 deletions.
84 changes: 51 additions & 33 deletions discordbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,68 +70,86 @@ func (d *DiscordBot) powerOnHandler(s *discordgo.Session, i *discordgo.Interacti
logger := d.logger.With().Str("username", i.Member.User.Username).Logger()
logger.Info().Msg("A user attempts to switch on the server")

powerState, ledState := d.module.State()
var deferStack []func()

defer func() {
time.Sleep(10 * time.Second)
s.InteractionResponseDelete(i.Interaction)
if len(deferStack) > 0 {
time.Sleep(10 * time.Second)
}
for i := len(deferStack) - 1; i >= 0; i-- {
deferStack[i]()
}
}()

s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: discordgo.MessageFlagsEphemeral,
Content: "Server startup in progress... Please wait",
},
})

deferStack = append(deferStack, func() {
s.InteractionResponseDelete(i.Interaction)
})

powerState, ledState := d.module.State()

if powerState.Err != nil {
logger.Error().Err(powerState.Err).Msg("Failed to retrieve POWER state")
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: discordgo.MessageFlagsEphemeral,
Content: "A problem occurred when switching on the server",
},
followupMsg, _ := s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
Flags: discordgo.MessageFlagsEphemeral,
Content: "A problem occurred when switching on the server",
})
deferStack = append(deferStack, func() {
s.FollowupMessageDelete(i.Interaction, followupMsg.ID)
})
return
}
if ledState.Err != nil {
logger.Error().Err(ledState.Err).Msg("Failed to retrieve LED state")
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: discordgo.MessageFlagsEphemeral,
Content: "A problem occurred when switching on the server",
},
followupMsg, _ := s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
Flags: discordgo.MessageFlagsEphemeral,
Content: "A problem occurred when switching on the server",
})
deferStack = append(deferStack, func() {
s.FollowupMessageDelete(i.Interaction, followupMsg.ID)
})
return
}

if powerState.Value || ledState.Value {
logger.Info().Msg("The server is already switched on")
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: discordgo.MessageFlagsEphemeral,
Content: "The server is already switched on",
},
followupMsg, _ := s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
Flags: discordgo.MessageFlagsEphemeral,
Content: "The server is already switched on",
})
deferStack = append(deferStack, func() {
s.FollowupMessageDelete(i.Interaction, followupMsg.ID)
})
return
}

err := d.module.PowerOn()
if err != nil {
logger.Error().Err(err).Msg("A problem occurred when switching on the server")
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: discordgo.MessageFlagsEphemeral,
Content: "A problem occurred when switching on the server",
},
followupMsg, _ := s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
Flags: discordgo.MessageFlagsEphemeral,
Content: "A problem occurred when switching on the server",
})
deferStack = append(deferStack, func() {
s.FollowupMessageDelete(i.Interaction, followupMsg.ID)
})
return
}
logger.Info().Msg("Server switched on")

s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: discordgo.MessageFlagsEphemeral,
Content: "The server turns on, give it some time...",
},
followupMsg, _ := s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
Flags: discordgo.MessageFlagsEphemeral,
Content: "The server is now starting! This may take a few minutes",
})
deferStack = append(deferStack, func() {
s.FollowupMessageDelete(i.Interaction, followupMsg.ID)
})
}

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var (
rootCmd = &cobra.Command{
Use: appName,
Short: "All-in-one tool for remote server power control",
Version: "1.3.0",
Version: "1.3.1",
Args: cobra.NoArgs,
Run: run,
CompletionOptions: cobra.CompletionOptions{
Expand Down

0 comments on commit 60752e2

Please sign in to comment.