-
Notifications
You must be signed in to change notification settings - Fork 1
/
voiceState.go
205 lines (173 loc) · 4.49 KB
/
voiceState.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
package main
import (
"fmt"
"sync"
"github.com/bwmarrin/discordgo"
"github.com/kmc-jp/DiscordSlackSynchronizer/slack_webhook"
)
var voiceChannels DiscordVoiceChannels = DiscordVoiceChannels{
map[string]*VoiceChannels{},
sync.Mutex{},
}
type DiscordVoiceChannels struct {
Guilds map[string]*VoiceChannels
Mutex sync.Mutex
}
type VoiceChannels struct {
Channels map[string]*VoiceChannel
}
type VoiceChannel struct {
Users map[string]*VoiceState
Channel *discordgo.Channel
}
type VoiceState struct {
Muted bool
Deafened bool
Member *discordgo.Member
}
// Join returns already user exits
func (v *VoiceChannels) Join(channel *discordgo.Channel, member *discordgo.Member) bool {
if v == nil || v.Channels == nil {
v.Channels = map[string]*VoiceChannel{}
}
if _, ok := v.Channels[channel.ID]; !ok {
v.Channels[channel.ID] = &VoiceChannel{
Users: map[string]*VoiceState{},
Channel: channel,
}
}
ch, ok := v.FindChannelHasUser(member.User.ID)
exists := false
if ok {
if ch == channel.ID {
exists = true
} else {
// User changed channels
v.Leave(member.User.ID)
}
}
v.Channels[channel.ID].Users[member.User.ID] = &VoiceState{
Muted: false, Deafened: false, Member: member,
}
return exists
}
func (v *VoiceChannels) Leave(userID string) {
if v == nil || v.Channels == nil {
v.Channels = map[string]*VoiceChannel{}
}
for _, channel := range v.Channels {
_, ok := channel.Users[userID]
if ok {
delete(channel.Users, userID)
}
}
}
func (v *VoiceChannels) FindChannelHasUser(userID string) (string, bool) {
if v == nil || v.Channels == nil {
v.Channels = map[string]*VoiceChannel{}
}
for _, channel := range v.Channels {
_, ok := channel.Users[userID]
if ok {
return channel.Channel.ID, true
}
}
return "", false
}
func (v *VoiceChannels) Muted(userID string) {
if v == nil || v.Channels == nil {
v.Channels = map[string]*VoiceChannel{}
}
for _, channel := range v.Channels {
user, ok := channel.Users[userID]
if ok {
user.Muted = true
user.Deafened = false
}
}
}
func (v *VoiceChannels) Deafened(userID string) {
if v == nil || v.Channels == nil {
v.Channels = map[string]*VoiceChannel{}
}
for _, channel := range v.Channels {
user, ok := channel.Users[userID]
if ok {
user.Muted = true
user.Deafened = true
}
}
}
func (v VoiceChannels) SlackBlocksMultiChannel() ([]slack_webhook.BlockBase, error) {
var blocks = []slack_webhook.BlockBase{}
for _, channel := range v.Channels {
if len(channel.Users) == 0 {
// skip no user channels
continue
}
var channelBlocks = channel.SlackBlocksSingleChannel()
blocks = append(blocks, channelBlocks...)
}
if len(blocks) <= 1 {
element := slack_webhook.MrkdwnElement("誰もいない", false)
var block = slack_webhook.ContextBlock(element)
blocks = append(blocks, block)
}
return blocks, nil
}
func (c VoiceChannel) SlackBlocksSingleChannel() []slack_webhook.BlockBase {
var blocks = []slack_webhook.BlockBase{}
channelText := fmt.Sprintf("<https://discord.com/channels/%s|%s: >", c.Channel.GuildID, c.Channel.Name)
var channelNameElement = slack_webhook.MrkdwnElement(channelText, false)
blocks = append(
blocks,
slack_webhook.ContextBlock(channelNameElement),
)
// Sort By User State
normal := []*VoiceState{}
muted := []*VoiceState{}
deafened := []*VoiceState{}
for _, user := range c.Users {
if user.Deafened {
deafened = append(deafened, user)
} else if user.Muted {
muted = append(muted, user)
} else {
normal = append(normal, user)
}
}
users := append(normal, muted...)
users = append(users, deafened...)
var userCount int
var elements = []slack_webhook.BlockElement{}
for _, user := range users {
userImage := user.Member.User.AvatarURL("")
username := user.Member.Nick
if username == "" {
username = user.Member.User.Username
}
var imageElm = slack_webhook.ImageElement(userImage, username)
emoji := ""
if user.Muted {
emoji = ":discord_muted:"
}
if user.Deafened {
emoji = ":discord_deafened:"
}
text := fmt.Sprintf("%s%s ", emoji, username)
var userElm = slack_webhook.MrkdwnElement(text, false)
elements = append(elements, imageElm, userElm)
userCount++
if userCount%4 == 0 {
var block = slack_webhook.ContextBlock(elements...)
blocks = append(blocks, block)
elements = []slack_webhook.BlockElement{}
}
}
if userCount%4 > 0 {
var block = slack_webhook.ContextBlock(elements...)
blocks = append(blocks, block)
}
blocks = append(blocks, slack_webhook.DividerBlock())
return blocks
}