12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- 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 r.forcePrefixMethod && ctx.Method != PrefixMethod {
- return
- }
-
- 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)
- }
|