feat: Add leaderboard

master
Luther Wen Xu 2020-05-31 12:35:54 +07:00
parent 069da9f1c6
commit e4f8f523ae
Signed by: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
3 changed files with 48 additions and 1 deletions

@ -19,8 +19,9 @@ type command struct {
var commands = []command{
command{"autorole", "<message link> <emoji> <role to assign>", handleAutorole, 3, true},
command{"recalclevel", "", handleRecalculateLevel, 0, false},
command{"leaderboard", "", handleLeaderboard, 0, true},
command{"level", "", handleLevel, 0, false},
command{"recalclevel", "", handleRecalculateLevel, 0, false},
}
func Event(dg *discordgo.Session, m *discordgo.MessageCreate) {

@ -0,0 +1,40 @@
package commands
import (
"github.com/bwmarrin/discordgo"
"gitea.teamortix.com/chanbakjsd/Milen/db"
"gitea.teamortix.com/chanbakjsd/Milen/level"
"gitea.teamortix.com/chanbakjsd/Milen/util"
)
var leaderboardEmoji = []string{"🏆", "🥈", "🥉", "⬛", "◼️"}
func handleLeaderboard(dg *discordgo.Session, m *discordgo.MessageCreate, arguments []string) {
topFive := db.GetTopFive()
fields := make([]*discordgo.MessageEmbedField, 0, len(topFive))
for k, v := range topFive {
member, err := util.GetMember(dg, m.GuildID, v.UserID)
var displayName string
if err == nil {
if member.Nick != "" {
displayName = member.Nick + " (" + member.User.Username + "#" + member.User.Discriminator + ")"
} else {
displayName = member.User.Username + "#" + member.User.Discriminator
}
} else {
displayName = "??? (" + v.UserID + ")"
}
rank, level := level.GetLevelFromXP(v.XP)
fields = append(fields, &discordgo.MessageEmbedField{
Name: leaderboardEmoji[k] + " " + displayName,
Value: rank.String() + " " + util.FormatRomanNumeral(level) + " (" + util.FormatPlural(v.XP, "second", "seconds") + ")",
})
}
util.SendEmbed(dg, m.ChannelID, &discordgo.MessageEmbed{
Title: "Leaderboard",
Fields: fields,
})
}

@ -12,6 +12,12 @@ type PlayerLevel struct {
XP int64
}
func GetTopFive() []PlayerLevel {
var results []PlayerLevel
db.Model(&PlayerLevel{}).Order("xp desc").Limit(5).Find(&results)
return results
}
func GetXP(userID string) int64 {
result := PlayerLevel{
UserID: userID,