123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- package mux
- import (
- "fmt"
- "regexp"
- "strings"
- "git.mgmcomp.net/thisnthat/discordgo"
- )
- func (r *Router) OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
- var err error
-
- if m.Author.ID == s.State.User.ID {
- return
- }
-
- ctx := &Context{
- Content: strings.TrimSpace(m.Content),
- }
- var channel *discordgo.Channel
- channel, err = s.State.Channel(m.ChannelID)
- if err != nil {
- channel, err = s.Channel(m.ChannelID)
- if err != nil {
- } else {
- _ = s.State.ChannelAdd(channel)
- }
- }
- var isDirectMessage bool
- var isCommand bool
- if channel != nil {
- if channel.Type == discordgo.ChannelTypeDM {
- isDirectMessage = true
- isCommand = true
- ctx.Method = DirectMethod
- }
- }
- if !isDirectMessage {
- reg := regexp.MustCompile(fmt.Sprintf("<@!?(%s)>", s.State.User.ID))
- for _, mention := range m.Mentions {
- if mention.ID == s.State.User.ID {
-
- if reg.FindStringIndex(ctx.Content)[0] == 0 {
- isCommand = true
- ctx.Method = MentionMethod
- }
- ctx.Content = reg.ReplaceAllString(ctx.Content, "")
- }
- }
-
- if len(r.prefix) > 0 {
-
- if strings.HasPrefix(ctx.Content, r.prefix) {
- isCommand = true
- ctx.Method = PrefixMethod
-
- ctx.Content = strings.TrimPrefix(ctx.Content, r.prefix)
- }
- }
- }
-
- if !isCommand {
- return
- }
- route, fields, args := r.findRoute(ctx.Content, MessageRoute)
- if route != nil {
- ctx.Fields = fields
- ctx.Args = args
- route.Run(s, m.Message, ctx)
- return
- }
- r.helpRoute.Run(s, m.Message, ctx)
- }
|