feat(xp): xp and level gain
This commit is contained in:
parent
1f68e69899
commit
1078cf3deb
7 changed files with 108 additions and 3 deletions
7
commands/rank.go
Normal file
7
commands/rank.go
Normal file
|
@ -0,0 +1,7 @@
|
|||
package commands
|
||||
|
||||
import "github.com/bwmarrin/discordgo"
|
||||
|
||||
func Rank(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
|
||||
}
|
2
go.mod
2
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
|
||||
|
|
4
go.sum
4
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=
|
||||
|
|
27
main.go
27
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)
|
||||
}
|
||||
|
|
40
xp/events.go
Normal file
40
xp/events.go
Normal file
|
@ -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))
|
||||
}
|
9
xp/level.go
Normal file
9
xp/level.go
Normal file
|
@ -0,0 +1,9 @@
|
|||
package xp
|
||||
|
||||
import (
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
func onNewLevel(s *discordgo.Session, level uint) {
|
||||
// check roles
|
||||
}
|
22
xp/member.go
Normal file
22
xp/member.go
Normal file
|
@ -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)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue