feat: leveling system
This commit is contained in:
@@ -30,15 +30,18 @@ var LeaderboardCommand = &discordgo.ApplicationCommand{
|
||||
Description: "Top 10 levels in this server",
|
||||
}
|
||||
|
||||
var RewardsCommand = &discordgo.ApplicationCommand{
|
||||
Name: "rewards",
|
||||
Description: "View level rewards in this server",
|
||||
}
|
||||
|
||||
func HandleRank(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
if services.Global == nil || services.Global.Level == nil {
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "Leveling service is not available.",
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
if !requireLevelServices(s, i) {
|
||||
return
|
||||
}
|
||||
|
||||
if i.GuildID != "" && !requireLevelSystemEnabled(s, i) {
|
||||
respondEphemeral(s, i, "Leveling system is disabled on this server.")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -58,18 +61,20 @@ func HandleRank(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
return
|
||||
}
|
||||
|
||||
guildID, err := strconv.ParseInt(i.GuildID, 10, 64)
|
||||
if err != nil {
|
||||
guildID, ok := parseGuildID(s, i)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
userID, err := strconv.ParseInt(target.ID, 10, 64)
|
||||
if err != nil {
|
||||
respondEphemeral(s, i, "Invalid user ID.")
|
||||
return
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
lvl, err := services.Global.Level.GetLevel(ctx, guildID, userID)
|
||||
if err != nil {
|
||||
respondEphemeral(s, i, "Failed to load level.")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -84,13 +89,7 @@ func HandleRank(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
Avatar: avatarURL,
|
||||
})
|
||||
if err != nil {
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "Failed to render rank card.",
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
respondEphemeral(s, i, "Failed to render rank card.")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -108,61 +107,31 @@ func HandleRank(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
}
|
||||
|
||||
func HandleLeaderboard(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
if i.GuildID == "" {
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "This command can only be used in a server.",
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
if !requireGuild(s, i) {
|
||||
return
|
||||
}
|
||||
|
||||
if services.Global == nil || services.Global.Level == nil {
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "Leveling service is not available.",
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
if !requireLevelServices(s, i) {
|
||||
return
|
||||
}
|
||||
|
||||
guildID, err := strconv.ParseInt(i.GuildID, 10, 64)
|
||||
if err != nil {
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "Invalid guild ID.",
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
guildID, ok := parseGuildID(s, i)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
if !requireLevelSystemEnabled(s, i) {
|
||||
return
|
||||
}
|
||||
top, err := services.Global.Level.TopLevels(ctx, guildID, 10)
|
||||
if err != nil {
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "Failed to load leaderboard.",
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
respondEphemeral(s, i, "Failed to load leaderboard.")
|
||||
return
|
||||
}
|
||||
|
||||
if len(top) == 0 {
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "No leaderboard data yet.",
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
respondEphemeral(s, i, "No leaderboard data yet.")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -194,3 +163,56 @@ func HandleLeaderboard(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func HandleRewards(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
if !requireGuild(s, i) {
|
||||
return
|
||||
}
|
||||
|
||||
if !requireLevelSettingsService(s, i) {
|
||||
return
|
||||
}
|
||||
|
||||
guildID, ok := parseGuildID(s, i)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
if !requireLevelSystemEnabled(s, i) {
|
||||
return
|
||||
}
|
||||
rewards, err := services.Global.LevelSettings.ListRoleRewards(ctx, guildID, 25)
|
||||
if err != nil {
|
||||
respondEphemeral(s, i, "Failed to load rewards.")
|
||||
return
|
||||
}
|
||||
|
||||
if len(rewards) == 0 {
|
||||
respondEphemeral(s, i, "No rewards configured yet.")
|
||||
return
|
||||
}
|
||||
|
||||
fields := make([]*discordgo.MessageEmbedField, 0, len(rewards))
|
||||
for _, r := range rewards {
|
||||
fields = append(fields, &discordgo.MessageEmbedField{
|
||||
Name: fmt.Sprintf("Level %d", r.Level),
|
||||
Value: fmt.Sprintf("<@&%d>", r.RoleID),
|
||||
Inline: true,
|
||||
})
|
||||
}
|
||||
|
||||
embed := &discordgo.MessageEmbed{
|
||||
Title: "Level Rewards",
|
||||
Description: "Roles granted when you reach a level.",
|
||||
Fields: fields,
|
||||
Color: 0xF59E0B,
|
||||
}
|
||||
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Embeds: []*discordgo.MessageEmbed{embed},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user