forked from abourget/slick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreaction.go
133 lines (111 loc) · 3.28 KB
/
reaction.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
package slick
import (
"time"
"github.com/nlopes/slack"
)
type ReactionListener struct {
ListenUntil time.Time
ListenDuration time.Duration
FromUser *slack.User
Emoji string
Type reaction
HandlerFunc func(listen *ReactionListener, event *ReactionEvent)
TimeoutFunc func(*ReactionListener)
listener *Listener
}
func (reactListen *ReactionListener) newListener() *Listener {
newListen := &Listener{}
if !reactListen.ListenUntil.IsZero() {
newListen.ListenUntil = reactListen.ListenUntil
}
if reactListen.ListenDuration != time.Duration(0) {
newListen.ListenDuration = reactListen.ListenDuration
}
if reactListen.TimeoutFunc != nil {
newListen.TimeoutFunc = func(listen *Listener) {
reactListen.TimeoutFunc(reactListen)
}
}
reactListen.listener = newListen
return newListen
}
func (listen *ReactionListener) filterReaction(re *ReactionEvent) bool {
if listen.Emoji != "" && re.Emoji != listen.Emoji {
return false
}
if listen.FromUser != nil && re.User != listen.FromUser.ID {
return false
}
if int(listen.Type) != 0 && re.Type != listen.Type {
return false
}
return true
}
func (listen *ReactionListener) Close() {
listen.listener.Close()
}
func (listen *ReactionListener) ResetNewDuration(d time.Duration) {
listen.listener.ListenDuration = d
listen.listener.ResetDuration()
}
func (listen *ReactionListener) ResetDuration() {
listen.listener.ResetDuration()
}
type ReactionEvent struct {
// Type can be `ReactionAdded` or `ReactionRemoved`
Type reaction
User string
Emoji string
Timestamp string
Item struct {
Type string `json:"type"`
Channel string `json:"channel,omitempty"`
File string `json:"file,omitempty"`
FileComment string `json:"file_comment,omitempty"`
Timestamp string `json:"ts,omitempty"`
}
// Original objects regarding the reaction, when called on a `Reply`.
OriginalReply *Reply
OriginalAckMessage *slack.AckMessage
// When called on `Message`
OriginalMessage *Message
// Listener is a reference to the thing listening for incoming Reactions
// you can call .Close() on it after a certain amount of time or after
// the user you were interested in processed its things.
Listener *ReactionListener
}
type reaction int
// ReactionAdded is used as the `Type` field of `ReactionEvent` (which
// you can register with `Reply.OnReaction()`)
const ReactionAdded = reaction(2)
// ReactionRemoved is the flipside of `ReactionAdded`.
const ReactionRemoved = reaction(1)
func ParseReactionEvent(event interface{}) *ReactionEvent {
var re ReactionEvent
switch ev := event.(type) {
case *slack.ReactionAddedEvent:
re.Type = ReactionAdded
re.Emoji = ev.Reaction
re.User = ev.User
re.Item.Type = ev.Item.Type
re.Item.Channel = ev.Item.Channel
re.Item.File = ev.Item.File
re.Item.FileComment = ev.Item.FileComment
re.Item.Timestamp = ev.Item.Timestamp
re.Timestamp = ev.EventTimestamp
case *slack.ReactionRemovedEvent:
re.Type = ReactionRemoved
re.Emoji = ev.Reaction
re.User = ev.User
re.Item = ev.Item
re.Item.Type = ev.Item.Type
re.Item.Channel = ev.Item.Channel
re.Item.File = ev.Item.File
re.Item.FileComment = ev.Item.FileComment
re.Item.Timestamp = ev.Item.Timestamp
re.Timestamp = ev.EventTimestamp
default:
return nil
}
return &re
}