Milen/reactrole/listener.go

71 lines
1.4 KiB
Go

2020-05-11 05:30:07 +07:00
package reactrole
import (
"fmt"
2020-05-11 05:30:07 +07:00
"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))
}
}
}()
2020-05-12 10:35:02 +07:00
member, err := util.GetMember(dg, r.GuildID, r.UserID)
if err != nil {
util.ReportError(dg, err)
return
}
if member.User.Bot {
return
}
2020-05-11 05:30:07 +07:00
role := db.GetReactRole(r.MessageID, r.Emoji.APIName())
if role == "" {
return
}
2020-05-12 10:35:02 +07:00
err = dg.GuildMemberRoleAdd(r.GuildID, r.UserID, role)
2020-05-11 05:30:07 +07:00
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))
}
}
}()
2020-05-12 10:35:02 +07:00
member, err := util.GetMember(dg, r.GuildID, r.UserID)
if err != nil {
util.ReportError(dg, err)
return
}
if member.User.Bot {
return
}
2020-05-11 05:30:07 +07:00
role := db.GetReactRole(r.MessageID, r.Emoji.APIName())
if role == "" {
return
}
2020-05-12 10:35:02 +07:00
err = dg.GuildMemberRoleRemove(r.GuildID, r.UserID, role)
2020-05-11 05:30:07 +07:00
if err != nil {
util.ReportError(dg, err)
}
}