74 lines
1.7 KiB
Go
74 lines
1.7 KiB
Go
package public
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
|
|
"velox-bot/internal/commands/fun/shared"
|
|
"velox-bot/internal/db/services"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
)
|
|
|
|
var RpsStats = &discordgo.ApplicationCommand{
|
|
Name: "rpsstats",
|
|
Description: "Shows your RPS stats",
|
|
Contexts: &[]discordgo.InteractionContextType{
|
|
discordgo.InteractionContextGuild,
|
|
},
|
|
}
|
|
|
|
func RpsStatsHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
if i.GuildID == "" {
|
|
shared.Respond(s, i, "This command can only be used in a server.")
|
|
return
|
|
}
|
|
if services.Global == nil || services.Global.RPS == nil {
|
|
shared.RespondEphemeral(s, i, "RPS service is not available.")
|
|
return
|
|
}
|
|
guildID, err := strconv.ParseInt(i.GuildID, 10, 64)
|
|
if err != nil {
|
|
shared.RespondEphemeral(s, i, "Invalid guild.")
|
|
return
|
|
}
|
|
user := i.User
|
|
if i.Member != nil && i.Member.User != nil {
|
|
user = i.Member.User
|
|
}
|
|
if user == nil {
|
|
return
|
|
}
|
|
userID, err := strconv.ParseInt(user.ID, 10, 64)
|
|
if err != nil {
|
|
shared.RespondEphemeral(s, i, "Invalid user.")
|
|
return
|
|
}
|
|
|
|
score, ok, err := services.Global.RPS.GetScore(context.Background(), guildID, userID)
|
|
if err != nil {
|
|
shared.RespondEphemeral(s, i, "Failed to load stats.")
|
|
return
|
|
}
|
|
if !ok {
|
|
shared.RespondEphemeral(s, i, "You haven't played RPS yet!")
|
|
return
|
|
}
|
|
|
|
embed := &discordgo.MessageEmbed{
|
|
Title: "RPS Stats",
|
|
Description: user.Mention(),
|
|
Color: 0x3B82F6,
|
|
Fields: []*discordgo.MessageEmbedField{
|
|
{Name: "Score", Value: strconv.Itoa(score), Inline: true},
|
|
},
|
|
}
|
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
Data: &discordgo.InteractionResponseData{
|
|
Embeds: []*discordgo.MessageEmbed{embed},
|
|
},
|
|
})
|
|
}
|
|
|