Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: using InteractionRespond to respond to user interaction. #34

Merged
merged 5 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion discord/embeds.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func helpEmbed(s *discordgo.Session) *discordgo.MessageEmbed {

func claimEmbed(s *discordgo.Session, i *discordgo.InteractionCreate, result string) *discordgo.MessageEmbed {
return &discordgo.MessageEmbed{
Title: "claim Pactus Coins",
Title: "claim",
Description: result,
}
}
Expand Down
146 changes: 129 additions & 17 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"
)
Expand All @@ -11,29 +12,96 @@ func helpCommandHandler(db *DiscordBot, s *discordgo.Session, i *discordgo.Inter
return
}

_, _ = s.ChannelMessageSendEmbedReply(i.ChannelID, helpEmbed(s), i.Message.MessageReference)
embed := helpEmbed(s)
response := &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{embed},
},
}

err := s.InteractionRespond(i.Interaction, response)
if err != nil {
fmt.Println(err)
}
}

func claimCommandHandler(db *DiscordBot, s *discordgo.Session, i *discordgo.InteractionCreate) {
if !checkMessage(i, s, db.GuildID, i.Member.User.ID) {
// Check if db, s, or i is nil
if db == nil || s == nil || i == nil {
fmt.Println("One or more arguments are nil")
return
}

discordID := i.Member.User.ID
testnetAddr := i.ApplicationCommandData().Options[0].StringValue()
mainnetAddr := i.ApplicationCommandData().Options[1].StringValue()

command := fmt.Sprintf("claim %s %s %s", discordID, testnetAddr, mainnetAddr)

result, err := db.BotEngine.Run(command)
if err != nil {
msg := fmt.Sprintf("an error occurred while claiming: %v", err)
// Get the application command data
data := i.ApplicationCommandData()
if data.Options == nil {
msg := "Command options are missing or invalid."
_, _ = s.ChannelMessageSend(i.ChannelID, msg)
return
}

// Check if the required number of options are present
if data.Options == nil || len(data.Options) < 3 {
msg := "Not enough options provided for the claim command."
_, _ = s.ChannelMessageSend(i.ChannelID, msg)
return
}

_, _ = s.ChannelMessageSendEmbedReply(i.ChannelID, claimEmbed(s, i, result), i.Message.MessageReference)
// Extract the options
var discordId, testnetAddr, mainnetAddr string
if data.Options[0] != nil {
discordId = data.Options[0].StringValue()
}
if data.Options[1] != nil {
testnetAddr = data.Options[1].StringValue()
}
if data.Options[2] != nil {
mainnetAddr = data.Options[2].StringValue()
}

// Remove the "discord-id:" prefix from the discordId
discordId = strings.TrimPrefix(discordId, "discord-id:")

// Remove the "testnet-addr:" and "mainnet-addr:" prefixes from the addresses
testnetAddr = strings.TrimPrefix(testnetAddr, "testnet-addr:")
mainnetAddr = strings.TrimPrefix(mainnetAddr, "mainnet-addr:")

if testnetAddr != "" && mainnetAddr != "" {
command := fmt.Sprintf("claim %s %s %s", discordId, testnetAddr, mainnetAddr)

// Check if db or db.BotEngine is nil
// if db == nil || db.BotEngine == nil {
// msg := "db or bot engine is nil."
// _, _ = s.ChannelMessageSend(i.ChannelID, msg)
// return
// }
if db.BotEngine == nil {
msg := "bot engine is nil."
_, _ = s.ChannelMessageSend(i.ChannelID, msg)
return
}

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

embed := claimEmbed(s, i, result)
response := &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{embed},
},
}

err = s.InteractionRespond(i.Interaction, response)
if err != nil {
fmt.Println(err)
}
}
}

func claimerInfoCommandHandler(db *DiscordBot, s *discordgo.Session, i *discordgo.InteractionCreate) {
Expand All @@ -46,13 +114,24 @@ func claimerInfoCommandHandler(db *DiscordBot, s *discordgo.Session, i *discordg

result, err := db.BotEngine.Run(command)
if err != nil {
msg := fmt.Sprintf("an error occured while checking, please try again: %v", err)
msg := fmt.Sprintf("Wallet address not found, please try again, %v", err)
_, _ = s.ChannelMessageSend(i.ChannelID, msg)

return
}

_, _ = s.ChannelMessageSendEmbedsReply(i.ChannelID, []*discordgo.MessageEmbed{claimerInfoEmbed(s, i, result)}, i.Message.MessageReference)
embed := claimerInfoEmbed(s, i, result)
response := &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{embed},
},
}

err = s.InteractionRespond(i.Interaction, response)
if err != nil {
fmt.Println(err)
}
}

func nodeInfoCommandHandler(db *DiscordBot, s *discordgo.Session, i *discordgo.InteractionCreate) {
Expand All @@ -71,7 +150,18 @@ func nodeInfoCommandHandler(db *DiscordBot, s *discordgo.Session, i *discordgo.I
return
}

_, _ = s.ChannelMessageSendEmbedsReply(i.ChannelID, []*discordgo.MessageEmbed{nodeInfoEmbed(s, i, result)}, i.Message.MessageReference)
embed := nodeInfoEmbed(s, i, result)
response := &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{embed},
},
}

err = s.InteractionRespond(i.Interaction, response)
if err != nil {
fmt.Println(err)
}
}

func networkHealthCommandHandler(db *DiscordBot, s *discordgo.Session, i *discordgo.InteractionCreate) {
Expand All @@ -89,7 +179,18 @@ func networkHealthCommandHandler(db *DiscordBot, s *discordgo.Session, i *discor
return
}

_, _ = s.ChannelMessageSendEmbedsReply(i.ChannelID, []*discordgo.MessageEmbed{networkHealthEmbed(s, i, result)}, i.Message.MessageReference)
embed := networkHealthEmbed(s, i, result)
response := &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{embed},
},
}

err = s.InteractionRespond(i.Interaction, response)
if err != nil {
fmt.Println(err)
}
}

func networkStatusCommandHandler(db *DiscordBot, s *discordgo.Session, i *discordgo.InteractionCreate) {
Expand All @@ -107,5 +208,16 @@ func networkStatusCommandHandler(db *DiscordBot, s *discordgo.Session, i *discor
return
}

_, _ = s.ChannelMessageSendEmbedsReply(i.ChannelID, []*discordgo.MessageEmbed{networkStatusEmbed(s, i, result)}, i.Message.MessageReference)
embed := networkStatusEmbed(s, i, result)
response := &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{embed},
},
}

err = s.InteractionRespond(i.Interaction, response)
if err != nil {
fmt.Println(err)
}
}
10 changes: 5 additions & 5 deletions engine/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
)

const (
CmdClaim = "claim" //!
CmdClaimerInfo = "claimer-info" //!
CmdNodeInfo = "node-info" //!
CmdNetworkStatus = "network" //!
CmdNetworkHealth = "health" //!
CmdClaim = "claim" //!
CmdClaimerInfo = "claimer-info" //!
CmdNodeInfo = "node-info" //!
CmdNetworkStatus = "network" //!
CmdNetworkHealth = "network-health" //!
)

// The input is always string.
Expand Down
Loading