feat: Add level roles

master
Luther Wen Xu 2020-05-30 22:50:26 +07:00
parent 879b482b7a
commit d273102aa2
Signed by: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
5 changed files with 56 additions and 0 deletions

@ -9,6 +9,7 @@ import (
type config struct {
Token string
ReportTarget string `toml:"report_target"`
LevelRoles []string `toml:"level_roles"`
MacroReplace map[string]string `toml:"macro_replace"`
Macro map[string]string
}

@ -31,4 +31,9 @@ func Event(dg *discordgo.Session, m *discordgo.MessageCreate) {
xp = 0
}
db.IncrementXP(m.Author.ID, xp, nextTime)
if _, ok := ShouldCheck[m.GuildID]; !ok {
ShouldCheck[m.GuildID] = make(map[string]bool)
}
ShouldCheck[m.GuildID][m.Author.ID] = true
}

@ -0,0 +1,47 @@
package level
import (
"time"
"github.com/bwmarrin/discordgo"
"gitea.teamortix.com/chanbakjsd/Milen/db"
"gitea.teamortix.com/chanbakjsd/Milen/util"
)
var Roles []string
var ShouldCheck = make(map[string]map[string]bool)
func QueueRoles(dg *discordgo.Session) {
for {
time.Sleep(5 * time.Second)
for guildID, v := range ShouldCheck {
for userID, _ := range v {
rank, _ := GetLevelFromXP(db.GetXP(userID))
AssignRoles(dg, guildID, userID, rank)
delete(v, userID)
}
}
}
}
func AssignRoles(dg *discordgo.Session, guildID string, userID string, rank Rank) {
member, err := util.GetMember(dg, guildID, userID)
if err != nil {
util.ReportError(dg, err)
return
}
OuterLoop:
for i := 0; i <= int(rank); i++ {
if i >= len(Roles) {
return
}
for _, v := range member.Roles {
if v == Roles[i] {
continue OuterLoop
}
}
dg.GuildMemberRoleAdd(guildID, userID, Roles[i])
}
}

@ -19,6 +19,7 @@ func main() {
cfg := LoadConfig()
commands.CommonReplacement = cfg.MacroReplace
commands.Macro = cfg.Macro
level.Roles = cfg.LevelRoles
util.ReportTarget = cfg.ReportTarget
dg, err := discordgo.New("Bot " + cfg.Token)
@ -32,6 +33,8 @@ func main() {
dg.AddHandler(reactrole.EventAdd)
dg.AddHandler(reactrole.EventRemove)
go level.QueueRoles(dg)
err = dg.Open()
if err != nil {
fmt.Println("error opening connection,", err)