Milen/reactrole/listener.go

71 lines
1.4 KiB
Go

package reactrole
import (
"fmt"
"github.com/bwmarrin/discordgo"
"gitea.teamortix.com/chanbakjsd/Milen/db"
"gitea.teamortix.com/chanbakjsd/Milen/util"
)
func EventAdd(dg *discordgo.Session, r *discordgo.MessageReactionAdd) {
defer func() {
if r := recover(); r != nil {
// Oh crap. We recovered from a panic.
if val, ok := r.(error); ok {
util.ReportError(dg, val)
} else {
util.ReportError(dg, fmt.Errorf("%T: %v", r, r))
}
}
}()
member, err := util.GetMember(dg, r.GuildID, r.UserID)
if err != nil {
util.ReportError(dg, err)
return
}
if member.User.Bot {
return
}
role := db.GetReactRole(r.MessageID, r.Emoji.APIName())
if role == "" {
return
}
err = dg.GuildMemberRoleAdd(r.GuildID, r.UserID, role)
if err != nil {
util.ReportError(dg, err)
}
}
func EventRemove(dg *discordgo.Session, r *discordgo.MessageReactionRemove) {
defer func() {
if r := recover(); r != nil {
// Oh crap. We recovered from a panic.
if val, ok := r.(error); ok {
util.ReportError(dg, val)
} else {
util.ReportError(dg, fmt.Errorf("%T: %v", r, r))
}
}
}()
member, err := util.GetMember(dg, r.GuildID, r.UserID)
if err != nil {
util.ReportError(dg, err)
return
}
if member.User.Bot {
return
}
role := db.GetReactRole(r.MessageID, r.Emoji.APIName())
if role == "" {
return
}
err = dg.GuildMemberRoleRemove(r.GuildID, r.UserID, role)
if err != nil {
util.ReportError(dg, err)
}
}