commands+reactrole: Ignore bot events

master
Luther Wen Xu 2020-05-12 18:35:02 +07:00
parent 0bd76d9c7b
commit 24a3887288
Signed by: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
3 changed files with 34 additions and 2 deletions

@ -21,6 +21,9 @@ var commands = []command{
} }
func Event(dg *discordgo.Session, m *discordgo.MessageCreate) { func Event(dg *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.Bot {
return
}
if !strings.HasPrefix(m.Content, "milen ") { if !strings.HasPrefix(m.Content, "milen ") {
return return
} }

@ -8,22 +8,38 @@ import (
) )
func EventAdd(dg *discordgo.Session, r *discordgo.MessageReactionAdd) { func EventAdd(dg *discordgo.Session, r *discordgo.MessageReactionAdd) {
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()) role := db.GetReactRole(r.MessageID, r.Emoji.APIName())
if role == "" { if role == "" {
return return
} }
err := dg.GuildMemberRoleAdd(r.GuildID, r.UserID, role) err = dg.GuildMemberRoleAdd(r.GuildID, r.UserID, role)
if err != nil { if err != nil {
util.ReportError(dg, err) util.ReportError(dg, err)
} }
} }
func EventRemove(dg *discordgo.Session, r *discordgo.MessageReactionRemove) { func EventRemove(dg *discordgo.Session, r *discordgo.MessageReactionRemove) {
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()) role := db.GetReactRole(r.MessageID, r.Emoji.APIName())
if role == "" { if role == "" {
return return
} }
err := dg.GuildMemberRoleRemove(r.GuildID, r.UserID, role) err = dg.GuildMemberRoleRemove(r.GuildID, r.UserID, role)
if err != nil { if err != nil {
util.ReportError(dg, err) util.ReportError(dg, err)
} }

@ -0,0 +1,13 @@
package util
import (
"github.com/bwmarrin/discordgo"
)
func GetMember(dg *discordgo.Session, guildID string, userID string) (*discordgo.Member, error) {
member, err := dg.State.Member(guildID, userID)
if err == nil {
return member, nil
}
return dg.GuildMember(guildID, userID)
}