-
Notifications
You must be signed in to change notification settings - Fork 1
/
ningen.go
256 lines (206 loc) · 6.27 KB
/
ningen.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Package ningen contains a set of helpful functions and packages to aid in
// making a Discord client.
package ningen
import (
"sort"
"sync"
"github.com/diamondburned/arikawa/discord"
"github.com/diamondburned/arikawa/gateway"
"github.com/diamondburned/arikawa/state"
"github.com/diamondburned/arikawa/utils/handler"
"github.com/diamondburned/ningen/states/emoji"
"github.com/diamondburned/ningen/states/member"
"github.com/diamondburned/ningen/states/mute"
"github.com/diamondburned/ningen/states/note"
"github.com/diamondburned/ningen/states/read"
"github.com/diamondburned/ningen/states/relationship"
)
// Connected is an event that's sent on Ready or Resumed. The event arrives
// before all ningen's handlers are called.
type Connected struct {
Event gateway.Event
}
type State struct {
*state.State
*handler.Handler
// handler that is given to states; always synchronous
prehandler *handler.Handler
// handler that users can call
initd chan struct{} // nil after Open().
initm sync.Mutex
// nil before Open().
NoteState *note.State
ReadState *read.State
MutedState *mute.State
EmojiState *emoji.State
MemberState *member.State
RelationshipState *relationship.State
}
// FromState wraps a normal state.
func FromState(s *state.State) (*State, error) {
state := &State{
State: s,
Handler: handler.New(),
prehandler: handler.New(),
}
// This is required to avoid data race with future handlers.
state.prehandler.Synchronous = true
// Give our local states the synchronous prehandler.
state.NoteState = note.NewState(state.prehandler)
state.ReadState = read.NewState(s, state.prehandler)
state.MutedState = mute.NewState(s, state.prehandler)
state.EmojiState = emoji.NewState(s)
state.MemberState = member.NewState(s, state.prehandler)
state.RelationshipState = relationship.NewState(state.prehandler)
s.AddHandler(func(v interface{}) {
switch v := v.(type) {
case *gateway.SessionsReplaceEvent:
if u, err := s.Me(); err == nil {
s.PresenceSet(0, joinSession(*u, v))
}
}
// Synchronously run the handlers that our states use.
state.prehandler.Call(v)
switch v.(type) {
case *gateway.ReadyEvent, *gateway.ResumedEvent:
state.Handler.Call(&Connected{v})
}
// Call the external handler after we're done. This handler is
// asynchronuos, or at least it should be.
state.Handler.Call(v)
state.initm.Lock()
initd := state.initd
state.initm.Unlock()
// Send to channel that unblocks Open() so applications don't access nil
// states and avoid data race.
if initd != nil {
initd <- struct{}{}
}
})
return state, nil
}
func (s *State) Open() error {
// Make the channel so the ready handler can use it. This channel is
// 1-buffered in case the handler is faster than us.
s.initm.Lock()
s.initd = make(chan struct{}, 1)
s.initm.Unlock()
if err := s.State.Open(); err != nil {
return err
}
s.initm.Lock()
initd := s.initd
s.initm.Unlock()
<-initd
s.initm.Lock()
s.initd = nil // so future Ready events will never use this ch
s.initm.Unlock()
return nil
}
func (s *State) PrivateChannels() ([]discord.Channel, error) {
c, err := s.State.PrivateChannels()
if err != nil {
return nil, err
}
sort.SliceStable(c, func(i, j int) bool {
return c[i].LastMessageID > c[j].LastMessageID
})
return c, nil
}
func (s *State) MessageMentions(msg discord.Message) bool {
// Ignore own messages.
if u, err := s.Me(); err == nil && msg.Author.ID == u.ID {
return false
}
var mutedGuild *gateway.UserGuildSettings
// If there's guild:
if msg.GuildID.IsValid() {
if mutedGuild = s.MutedState.GuildSettings(msg.GuildID); mutedGuild != nil {
// We're only checking mutes and suppressions, as channels don't
// have these. Whatever channels have will override guilds.
// @everyone mentions still work if the guild is muted and @everyone
// is not suppressed.
if msg.MentionEveryone && !mutedGuild.SuppressEveryone {
return true
}
// TODO: roles
// If the guild is muted of all messages:
if mutedGuild.Muted {
return false
}
}
}
// Boolean on whether the message contains a self mention or not:
var mentioned = messageMentions(msg, s.Ready.User.ID)
// Check channel settings. Channel settings override guilds.
if mutedCh := s.MutedState.ChannelOverrides(msg.ChannelID); mutedCh != nil {
switch mutedCh.MessageNotifications {
case gateway.AllNotifications:
if mutedCh.Muted {
return false
}
case gateway.NoNotifications:
// If no notifications are allowed, not even mentions.
return false
case gateway.OnlyMentions:
// If mentions are allowed. We return early because this overrides
// the guild settings, even if Guild wants all messages.
return mentioned
}
}
if mutedGuild != nil {
switch mutedGuild.MessageNotifications {
case gateway.AllNotifications:
// If the guild is muted, but we can return early here. If we allow
// all notifications, we can return the opposite of muted.
// - If we're muted, we don't want a mention.
// - If we're not muted, we want a mention.
return !mutedGuild.Muted
case gateway.NoNotifications:
// If no notifications are allowed whatsoever.
return false
case gateway.OnlyMentions:
// We can return early here.
return mentioned
}
}
// Is this from a DM? TODO: get a better check.
if ch, err := s.Channel(msg.ChannelID); err == nil {
// True if the message is from DM or group.
return ch.Type == discord.DirectMessage || ch.Type == discord.GroupDM
}
return false
}
func messageMentions(msg discord.Message, uID discord.UserID) bool {
for _, user := range msg.Mentions {
if user.ID == uID {
return true
}
}
return false
}
func joinSession(me discord.User, r *gateway.SessionsReplaceEvent) discord.Presence {
ses := *r
var game *discord.Activity
var status discord.Status
var activities []discord.Activity
for i := len(ses) - 1; i >= 0; i-- {
presence := ses[i]
if presence.Game != nil {
game = presence.Game
}
if presence.Status != "" {
status = presence.Status
}
activities = append(activities, presence.Activities...)
}
if game == nil && len(activities) > 0 {
game = &activities[len(activities)-1]
}
return discord.Presence{
User: me,
Game: game,
Status: status,
Activities: activities,
}
}