Skip to content

Commit

Permalink
feat(discord): using Embed and interaction respond for error cases (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
izikdepth authored Jan 31, 2024
1 parent 413e518 commit 7f0d534
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 11 deletions.
14 changes: 13 additions & 1 deletion discord/embeds.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package discord

import "github.com/bwmarrin/discordgo"
import (
"fmt"

"github.com/bwmarrin/discordgo"
)

func helpEmbed(s *discordgo.Session) *discordgo.MessageEmbed {
return &discordgo.MessageEmbed{
Expand Down Expand Up @@ -56,3 +60,11 @@ func botWalletEmbed(s *discordgo.Session, i *discordgo.InteractionCreate, result
Description: result,
}
}

func errorEmbedMessage(reason string) *discordgo.MessageEmbed {
return &discordgo.MessageEmbed{
Title: "Error",
Description: fmt.Sprintf("An error occurred, please try again! : %s", reason),
Color: 0xFF0000, // Red color
}
}
64 changes: 54 additions & 10 deletions discord/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package discord

import (
"fmt"
"strings"

"github.com/bwmarrin/discordgo"
"github.com/kehiy/RoboPac/log"
Expand Down Expand Up @@ -37,12 +38,23 @@ func claimCommandHandler(db *DiscordBot, s *discordgo.Session, i *discordgo.Inte

log.Info("new claim request", "discordID", i.Member.User.ID, "mainNetAddr", mainnetAddr, "testNetAddr", testnetAddr)

testnetAddr = strings.TrimPrefix(testnetAddr, "testnet-addr:")
mainnetAddr = strings.TrimPrefix(mainnetAddr, "mainnet-addr:")

command := fmt.Sprintf("claim %s %s %s", i.Member.User.ID, testnetAddr, mainnetAddr)

result, err := db.BotEngine.Run(command)
if err != nil {
msg := fmt.Sprintf("an error occurred while claiming: %v", err)
_, _ = s.ChannelMessageSend(i.ChannelID, msg)
errorEmbed := errorEmbedMessage(err.Error())

response := &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{errorEmbed},
},
}

_ = s.InteractionRespond(i.Interaction, response)

return
}
Expand All @@ -68,8 +80,16 @@ func claimerInfoCommandHandler(db *DiscordBot, s *discordgo.Session, i *discordg

result, err := db.BotEngine.Run(command)
if err != nil {
msg := fmt.Sprintf("an error occurred : %v", err)
_, _ = s.ChannelMessageSend(i.ChannelID, msg)
errorEmbed := errorEmbedMessage(err.Error())

response := &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{errorEmbed},
},
}

_ = s.InteractionRespond(i.Interaction, response)

return
}
Expand All @@ -95,8 +115,16 @@ func nodeInfoCommandHandler(db *DiscordBot, s *discordgo.Session, i *discordgo.I

result, err := db.BotEngine.Run(command)
if err != nil {
msg := fmt.Sprintf("an error occurred : %v", err)
_, _ = s.ChannelMessageSend(i.ChannelID, msg)
errorEmbed := errorEmbedMessage(err.Error())

response := &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{errorEmbed},
},
}

_ = s.InteractionRespond(i.Interaction, response)

return
}
Expand All @@ -121,8 +149,16 @@ func networkHealthCommandHandler(db *DiscordBot, s *discordgo.Session, i *discor

result, err := db.BotEngine.Run(command)
if err != nil {
msg := fmt.Sprintf("an error occurred : %v", err)
_, _ = s.ChannelMessageSend(i.ChannelID, msg)
errorEmbed := errorEmbedMessage(err.Error())

response := &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{errorEmbed},
},
}

_ = s.InteractionRespond(i.Interaction, response)

return
}
Expand All @@ -145,8 +181,16 @@ func networkStatusCommandHandler(db *DiscordBot, s *discordgo.Session, i *discor

result, err := db.BotEngine.Run("network")
if err != nil {
msg := fmt.Sprintf("an error occurred : %v", err)
_, _ = s.ChannelMessageSend(i.ChannelID, msg)
errorEmbed := errorEmbedMessage(err.Error())

response := &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{errorEmbed},
},
}

_ = s.InteractionRespond(i.Interaction, response)

return
}
Expand Down

0 comments on commit 7f0d534

Please sign in to comment.