-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
96 lines (80 loc) · 2.52 KB
/
session.go
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package dmux
import (
"github.com/bwmarrin/discordgo"
)
type DiscordSession struct {
*discordgo.Session
handlers []Handler
}
func (s *DiscordSession) MessageChannel(c Channel, message Message) (Message, error) {
m, err := s.Session.ChannelMessageSend(c.ID(), message.Content())
return DiscordMessage{Message: m}, err
}
func (s *DiscordSession) AddHandler(h Handler) {
s.handlers = append(s.handlers, h)
}
func (s *DiscordSession) Open() {
s.Session.AddHandler(s.SessionRouter())
s.Session.Open()
}
func (s *DiscordSession) RawSession() *discordgo.Session {
return s.Session
}
/*
Base router for all discord event types
DiscordGo passes a variety of types to its message handlers
When a message is recieved g will be a discordgo.MessageCreate object, the
data from this can be added to the context that will be passed to upcoming
handlers.
The function then finds the handler that matches the event context and runs it
*/
func (s *DiscordSession) SessionRouter() func(*discordgo.Session, interface{}) {
return func(d *discordgo.Session, g interface{}) {
var handlerType HandlerType = -1
context := DiscordGoContext{}
switch g.(type) {
case *discordgo.MessageCreate:
handlerType = MessageHandler
context.messageContext = DiscordMessageContext{
g.(*discordgo.MessageCreate),
}
}
for _, handler := range s.handlers {
if handler.HandlerType() == handlerType {
if handler.Pattern(context) {
handler.Handle(&DiscordSession{d, []Handler{}}, context)
return
}
}
}
}
}
func (s *DiscordSession) Close() {
s.Session.Close()
}
func (s *DiscordSession) GuildRoles(g Guild) ([]Role, error) {
r, err := s.Session.GuildRoles(g.ID())
if err != nil {
return nil, err
}
roles := make([]Role, len(r))
for i, role := range r {
roles[i] = DiscordRole{Role: role}
}
return roles, nil
}
func (s *DiscordSession) UserPermissions(u User, c Channel) (Permissions, error) {
p, err := s.Session.UserChannelPermissions(u.ID(), c.ID())
return DiscordPermissions{p}, err
}
func (s *DiscordSession) GuildMemberRoleAdd(g Guild, u User, r Role) error {
return s.Session.GuildMemberRoleAdd(g.ID(), u.ID(), r.ID())
}
func (s *DiscordSession) GuildRoleCreate(guild string, role string) (Role, error) {
newrole, err := s.Session.GuildRoleCreate(guild)
s.Session.GuildRoleEdit(guild, newrole.ID, role, 0, false, 0, false)
return DiscordRole{Role: newrole}, err
}
func (s *DiscordSession) GuildMemberRoleRemove(g Guild, u User, r Role) error {
return s.Session.GuildMemberRoleRemove(g.ID(), u.ID(), r.ID())
}