From 1078cf3deb94ef22c65510b42c8e7f0dc50a036a Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Sun, 14 Apr 2024 17:11:31 +0200 Subject: [PATCH] feat(xp): xp and level gain --- commands/rank.go | 7 +++++++ go.mod | 2 +- go.sum | 4 ++++ main.go | 27 +++++++++++++++++++++++++-- xp/events.go | 40 ++++++++++++++++++++++++++++++++++++++++ xp/level.go | 9 +++++++++ xp/member.go | 22 ++++++++++++++++++++++ 7 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 commands/rank.go create mode 100644 xp/events.go create mode 100644 xp/level.go create mode 100644 xp/member.go diff --git a/commands/rank.go b/commands/rank.go new file mode 100644 index 0000000..e5b760b --- /dev/null +++ b/commands/rank.go @@ -0,0 +1,7 @@ +package commands + +import "github.com/bwmarrin/discordgo" + +func Rank(s *discordgo.Session, i *discordgo.InteractionCreate) { + +} diff --git a/go.mod b/go.mod index e223243..aaf4054 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,7 @@ module github.com/anhgelus/les-copaings-bot go 1.22 -require github.com/anhgelus/gokord v0.1.3 +require github.com/anhgelus/gokord v0.1.5 require ( github.com/bwmarrin/discordgo v0.28.1 // indirect diff --git a/go.sum b/go.sum index b3daa2f..0725808 100644 --- a/go.sum +++ b/go.sum @@ -20,6 +20,10 @@ github.com/anhgelus/gokord v0.1.3-0.20240414132927-426d437f7613 h1:mbZ5nKVtCo9pI github.com/anhgelus/gokord v0.1.3-0.20240414132927-426d437f7613/go.mod h1:lRYqODauBGzkmjuIrklmCkUt9OMxnNvb1af8ifbjRIs= github.com/anhgelus/gokord v0.1.3 h1:27OS08egivF/KSIYNb+L0/xCv+4ENW0Rk4nLywc2bWY= github.com/anhgelus/gokord v0.1.3/go.mod h1:CRyk26IhIZ/0Mkc5/5WOU8C08mGCOqzKzR6eDFfPisI= +github.com/anhgelus/gokord v0.1.4 h1:hoe87eCcf+Y22WDKwesQuyvxna4SWXrjpYNpdwH8pbI= +github.com/anhgelus/gokord v0.1.4/go.mod h1:CRyk26IhIZ/0Mkc5/5WOU8C08mGCOqzKzR6eDFfPisI= +github.com/anhgelus/gokord v0.1.5 h1:yFFKK6B1hCZU3mvoLAW2nZnjTkXJv7J7uqi/bWMuJbA= +github.com/anhgelus/gokord v0.1.5/go.mod h1:CRyk26IhIZ/0Mkc5/5WOU8C08mGCOqzKzR6eDFfPisI= github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= diff --git a/main.go b/main.go index cb3c33a..7197396 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,8 @@ package main import ( "flag" "github.com/anhgelus/gokord" + "github.com/anhgelus/les-copaings-bot/xp" + "github.com/bwmarrin/discordgo" ) var token string @@ -18,6 +20,21 @@ func main() { panic(err) } + err = gokord.DB.AutoMigrate(&xp.Copaing{}) + if err != nil { + panic(err) + } + + //rankCmd := gokord.NewCommand("rank", "Affiche le niveau d'une personne"). + // HasOption(). + // AddOption(gokord.NewOption( + // discordgo.ApplicationCommandOptionUser, + // "copaing", + // "Le niveau du Copaing que vous souhaitez obtenir", + // )). + // SetHandler(commands.Rank). + // ToCmd() + bot := gokord.Bot{ Token: token, Status: []*gokord.Status{ @@ -27,8 +44,14 @@ func main() { Url: "", }, }, - Commands: nil, - Handlers: nil, + Commands: []*gokord.Cmd{ + //rankCmd, + }, + AfterInit: afterInit, } bot.Start() } + +func afterInit(dg *discordgo.Session) { + dg.AddHandler(xp.OnMessage) +} diff --git a/xp/events.go b/xp/events.go new file mode 100644 index 0000000..c82c489 --- /dev/null +++ b/xp/events.go @@ -0,0 +1,40 @@ +package xp + +import ( + "github.com/anhgelus/gokord/utils" + "github.com/bwmarrin/discordgo" +) + +func OnMessage(s *discordgo.Session, m *discordgo.MessageCreate) { + c := Copaing{DiscordID: m.Author.ID, GuildID: m.GuildID} + c.Load() + // add xp + pastLevel := Level(c.XP) + trimmed := utils.TrimMessage(m.Content) + c.XP += XPMessage(uint(len(trimmed)), calcDiversity(trimmed)) + c.Save() + newLevel := Level(c.XP) + // handle new level + if pastLevel < newLevel { + if err := s.MessageReactionAdd(m.ChannelID, m.Message.ID, "⬆"); err != nil { + utils.SendAlert("xp/events.go - reaction add new level", "cannot add the reaction: "+err.Error()) + } + onNewLevel(s, newLevel) + } +} + +func calcDiversity(msg string) uint { + var chars []rune + for _, c := range []rune(msg) { + toAdd := true + for _, ch := range chars { + if ch == c { + toAdd = false + } + } + if toAdd { + chars = append(chars, c) + } + } + return uint(len(chars)) +} diff --git a/xp/level.go b/xp/level.go new file mode 100644 index 0000000..67ee5a5 --- /dev/null +++ b/xp/level.go @@ -0,0 +1,9 @@ +package xp + +import ( + "github.com/bwmarrin/discordgo" +) + +func onNewLevel(s *discordgo.Session, level uint) { + // check roles +} diff --git a/xp/member.go b/xp/member.go new file mode 100644 index 0000000..751bbd7 --- /dev/null +++ b/xp/member.go @@ -0,0 +1,22 @@ +package xp + +import ( + "github.com/anhgelus/gokord" + "gorm.io/gorm" +) + +type Copaing struct { + gorm.Model + DiscordID string + XP uint + GuildID string +} + +func (c *Copaing) Load() *Copaing { + gokord.DB.Where("discord_id = ? and guild_id = ?", c.DiscordID, c.GuildID).FirstOrCreate(c) + return c +} + +func (c *Copaing) Save() { + gokord.DB.Save(c) +}