Skip to content

Commit

Permalink
fix(giveaway): Use message ID instead of timestamp for button CustomID
Browse files Browse the repository at this point in the history
  • Loading branch information
Paranoia8972 committed Dec 13, 2024
1 parent 7a7f7a3 commit 8cef92d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
26 changes: 19 additions & 7 deletions internal/pkg/commands/giveaway.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"time"

"github.com/Paranoia8972/PixelBot/internal/db"
"github.com/Paranoia8972/PixelBot/internal/pkg/utils"

"github.com/bwmarrin/discordgo"
"go.mongodb.org/mongo-driver/bson"
)
Expand Down Expand Up @@ -47,9 +49,9 @@ func startGiveaway(s *discordgo.Session, i *discordgo.InteractionCreate, options
winnersCount := int(options[1].IntValue())
prize := options[2].StringValue()

duration, err := time.ParseDuration(durationStr)
duration, err := utils.ParseDuration(durationStr)
if err != nil {
RespondWithMessage(s, i, "Invalid duration format.")
RespondWithMessage(s, i, "Invalid duration format. Use format like: 2d, 4h, 30m")
return
}

Expand All @@ -64,23 +66,33 @@ func startGiveaway(s *discordgo.Session, i *discordgo.InteractionCreate, options
},
},
},
Components: []discordgo.MessageComponent{
}

msg, err := s.ChannelMessageSendComplex(i.ChannelID, msgSend)
if err != nil {
RespondWithMessage(s, i, "Failed to send giveaway message.")
return
}

msgEdit := &discordgo.MessageEdit{
ID: msg.ID,
Channel: i.ChannelID,
Components: &[]discordgo.MessageComponent{
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.Button{
Label: "Enter Giveaway",
CustomID: "giveaway_enter_" + strconv.FormatInt(time.Now().Unix(), 10),
CustomID: "giveaway_enter_" + msg.ID,
Style: discordgo.PrimaryButton,
},
},
},
},
}

msg, err := s.ChannelMessageSendComplex(i.ChannelID, msgSend)

_, err = s.ChannelMessageEditComplex(msgEdit)
if err != nil {
RespondWithMessage(s, i, "Failed to send giveaway message.")
RespondWithMessage(s, i, "Failed to add button to giveaway message.")
return
}

Expand Down
39 changes: 39 additions & 0 deletions internal/pkg/utils/giveaway.go
Original file line number Diff line number Diff line change
@@ -1 +1,40 @@
package utils

import (
"fmt"
"strconv"
"strings"
"time"
)

type Giveaway struct {
MessageID string `bson:"message_id"`
ChannelID string `bson:"channel_id"`
EndTime time.Time `bson:"end_time"`
WinnersCount int `bson:"winners_count"`
Prize string `bson:"prize"`
Participants []string `bson:"participants"`
Winners []string `bson:"winners"`
Ended bool `bson:"ended"`
}

func ParseDuration(durationStr string) (time.Duration, error) {
value := durationStr[:len(durationStr)-1]
unit := durationStr[len(durationStr)-1:]

numeric, err := strconv.Atoi(value)
if err != nil {
return 0, fmt.Errorf("invalid duration format")
}

switch strings.ToLower(unit) {
case "d":
return time.Duration(numeric) * 24 * time.Hour, nil
case "h":
return time.Duration(numeric) * time.Hour, nil
case "m":
return time.Duration(numeric) * time.Minute, nil
default:
return 0, fmt.Errorf("invalid duration unit")
}
}

0 comments on commit 8cef92d

Please sign in to comment.