-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmessage_create.go
More file actions
67 lines (55 loc) · 1.58 KB
/
message_create.go
File metadata and controls
67 lines (55 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package bcr
import (
"fmt"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/diamondburned/arikawa/v3/gateway"
)
// MessageCreate gets called on new messages
// - makes sure the router has a bot user
// - checks if the message matches a prefix
// - runs commands
func (r *Router) MessageCreate(m *gateway.MessageCreateEvent) {
r.Logger.Debug("received new message (%v) in %v by %v#%v (%v)", m.ID, m.ChannelID, m.Author.Username, m.Author.Discriminator, m.Author.ID)
// set the bot user if not done already
if r.Bot == nil {
r.mustSetBotUser(m.GuildID)
r.Prefixes = append(r.Prefixes, fmt.Sprintf("<@%v>", r.Bot.ID), fmt.Sprintf("<@!%v>", r.Bot.ID))
}
// if the author is a bot, return
if m.Author.Bot {
return
}
// if the message does not start with any of the bot's prefixes (including mentions), return
if !r.MatchPrefix(m.Message) {
return
}
// get the context
ctx, err := r.NewContext(m)
if err != nil {
r.Logger.Error("getting context: %v", err)
return
}
err = r.Execute(ctx)
if err != nil {
r.Logger.Error("executing command: %v", err)
return
}
}
// mustSetBotUser sets the bot user in the router, panicking if it fails.
// This is intended to be used in MessageCreate to simplify error handling
func (r *Router) mustSetBotUser(guildID discord.GuildID) {
err := r.SetBotUser(guildID)
if err != nil {
panic(err)
}
}
// SetBotUser sets the router's bot user, returning any errors
func (r *Router) SetBotUser(guildID discord.GuildID) error {
s, _ := r.StateFromGuildID(guildID)
me, err := s.Me()
if err != nil {
return err
}
r.Bot = me
return nil
}