This repository was archived by the owner on Sep 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathontyping.go
More file actions
228 lines (187 loc) · 3.69 KB
/
Copy pathontyping.go
File metadata and controls
228 lines (187 loc) · 3.69 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
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
package main
import (
"time"
"github.com/diamondburned/discordgo"
)
// TypingUsers is a store for all typing users
type TypingUsers struct {
Store []*typingEvent
}
type typingEvent struct {
*discordgo.TypingStart
Meta *typingMeta
}
type typingMeta struct {
Name string
Time time.Time
}
var (
typing = &TypingUsers{}
updateTyping = make(chan struct{})
)
func onTyping(s *discordgo.Session, ts *discordgo.TypingStart) {
if Channel == nil {
return
}
if ts.ChannelID != Channel.ID {
return
}
if ts.UserID == d.State.User.ID {
return
}
go typing.AddUser(ts)
}
func getTypingMeta(typing *discordgo.TypingStart) *typingMeta {
if typing.GuildID != 0 {
_, user := us.GetUser(
typing.GuildID, typing.UserID,
)
m := &typingMeta{
Time: time.Now(),
}
if user == nil {
m, err := d.State.Member(typing.GuildID, typing.UserID)
if err != nil {
return nil
}
user = us.UpdateUser(
m.GuildID,
m.User.ID,
m.User.Username,
m.Nick,
m.User.Discriminator,
getUserColor(m.GuildID, m.Roles),
)
}
if user.Nick == "" {
m.Name = user.Name
} else {
m.Name = user.Nick
}
return m
}
ch, err := d.State.Channel(Channel.ID)
if err != nil {
return nil
}
for _, r := range ch.Recipients {
if r.ID == typing.UserID {
return &typingMeta{
Name: r.Username,
Time: time.Now(),
}
}
}
return nil
}
func renderCallback() {
var (
animation uint
tick = time.Tick(time.Second)
dots = [6]string{
" ", "· ", "·· ",
"···", " ··", " ·",
}
)
for {
var mems = make([]string, 0, len(typing.Store))
select { // 500ms or instant
case <-updateTyping:
case <-tick:
}
if len(typing.Store) < 1 {
animation = 0
} else {
animation++
if animation > 5 {
animation = 0
}
}
for _, t := range typing.Store {
if t.Meta != nil {
mems = append(mems, t.Meta.Name)
}
}
text := cfg.Prop.DefaultStatus
switch {
case len(mems) > 3:
text = "Several people are typing" + dots[animation]
case len(mems) == 1:
text = HumanizeStrings(mems) + " is typing" + dots[animation]
case len(mems) > 1:
text = HumanizeStrings(mems) + " are typing" + dots[animation]
}
if text != input.GetPlaceholder() && !messagesView.HasFocus() {
input.SetPlaceholder(text)
app.Draw()
}
}
}
func getAnimation(i uint) string {
switch i {
case 0:
return " "
case 1:
return "· "
case 2:
return "·· "
case 3:
return "···"
case 4:
return " ··"
case 5:
return " ·"
}
return " "
}
// Reset resets the store
func (tu *TypingUsers) Reset() {
tu.Store = []*typingEvent{}
updateTyping <- struct{}{}
}
// AddUser this function needs to run in a goroutine
func (tu *TypingUsers) AddUser(ts *discordgo.TypingStart) {
defer func() {
if r := recover(); r != nil {
return
}
}()
for _, s := range tu.Store {
if s.UserID == ts.UserID && s.Meta != nil {
s.Meta.Time = time.Now()
return
}
}
ev := &typingEvent{
TypingStart: ts,
Meta: getTypingMeta(ts),
}
tu.Store = append(tu.Store, ev)
updateTyping <- struct{}{}
time.Sleep(time.Second * 10)
// should always pass UNLESS there's another AddUser call bumping the
// time up
for {
t := ev.Meta.Time
if t.Add(10 * time.Second).Before(time.Now()) {
tu.RemoveUser(ts)
break
}
time.Sleep(time.Second * 1)
}
}
// RemoveUser removes a user from a store array
// true is returned when a user is found and removed
func (tu *TypingUsers) RemoveUser(ts *discordgo.TypingStart) bool {
for i, d := range tu.Store {
if d.UserID == ts.UserID {
tu.Store = append(
tu.Store[:i],
tu.Store[i+1:]...,
)
updateTyping <- struct{}{}
return true
}
}
return false
}