-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
177 lines (148 loc) · 4.27 KB
/
main.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
package main
import (
"fmt"
"math"
"math/rand"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"github.com/bwmarrin/discordgo"
)
func main() {
dg, err := discordgo.New("Bot " + os.Getenv("SHUFFLEBOT_TOKEN"))
if err != nil {
fmt.Println("Error creating Discord bot: ", err)
return
}
dg.AddHandler(messageHandler)
dg.Open()
if err != nil {
fmt.Println("Error opening WebSocket connection: ", err)
return
}
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
dg.Close()
}
func sendReply(s *discordgo.Session, m *discordgo.MessageCreate, str string) {
sendMessage := fmt.Sprintf("<@!%s> ", m.Author.ID)
sendMessage += str
s.ChannelMessageSend(m.ChannelID, sendMessage)
}
func isContain(needle string, haystack []string) bool {
for _, v := range haystack {
if v == needle {
return true
}
}
return false
}
func messageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
gid := m.GuildID
if gid == "" {
// Invoked from user chat directly
s.ChannelMessageSend(m.ChannelID, "Please send after connecting and joining some voice channel!")
return
}
// Invoked from Server (Guild)
if !strings.HasPrefix(m.Content, "!!teams") {
return
}
args := strings.Split(m.Content, " ")
if len(args) <= 1 {
sendReply(s, m, "Usage: `!!teams <number of teams to create> [skip username ...]`")
return
}
var skipUsernames []string
if len(args) > 2 {
skipUsernames = args[2:len(args)]
}
_nTeams, err := strconv.ParseInt(args[1], 10, 32)
if err != nil {
fmt.Println("Error while parsing user specified value: ", err)
sendReply(s, m, "Please specify in number!!!")
return
}
nTeams := int(_nTeams)
if nTeams <= 0 || nTeams >= 100 {
if gid == "223518751650217994" {
// for internal uses
sendReply(s, m, "<:kakattekoi:461046115257679872>")
} else {
sendReply(s, m, "Please specify in *realistic* number!!!!!")
}
return
}
guild, err := s.State.Guild(gid)
if err != nil {
fmt.Println("Error while fetching guild: ", err)
return
}
// find users voice channel & fetch connected users
voiceChannelUsers := map[string][]string{}
var sourceVoiceChannel string
for _, vs := range guild.VoiceStates {
if vs.UserID == m.Author.ID {
sourceVoiceChannel = vs.ChannelID
}
var user *discordgo.User
member, err := s.State.Member(gid, vs.UserID)
if err == nil {
user = member.User
} else {
if err == discordgo.ErrNilState || err == discordgo.ErrStateNotFound {
user, err = s.User(vs.UserID)
if err != nil {
fmt.Printf("Error while fetching username from API: %+v", err)
sendReply(s, m, "Error: temporary error.")
return
}
} else {
fmt.Printf("Error while fetching username from State: %+v", err)
sendReply(s, m, "Error: unknown error.")
return
}
}
if !isContain(user.Username, skipUsernames) {
voiceChannelUsers[vs.ChannelID] =
append(voiceChannelUsers[vs.ChannelID], user.Username)
}
}
// not found in any voice channel
if sourceVoiceChannel == "" {
sendReply(s, m, "Please connect some voice channel!")
return
}
// check nTeams
totalUserCount := len(voiceChannelUsers[sourceVoiceChannel])
nMembers := int(math.Round(float64(totalUserCount) / float64(nTeams)))
if totalUserCount < nTeams {
sendReply(s, m, fmt.Sprintf("More member required to make %d team(s) by %d member(s)!", nTeams, nMembers))
return
}
// shuffle by connected users
idx := rand.Perm(totalUserCount)
var shuffledUsers []string
for _, newIdx := range idx {
shuffledUsers = append(shuffledUsers, voiceChannelUsers[sourceVoiceChannel][newIdx])
}
// devide into {nTeams} teams
result := make([][]string, nTeams)
for i := 0; i < nTeams-1; i++ {
result[i] = shuffledUsers[i*nMembers : (i+1)*nMembers]
}
result[nTeams-1] = shuffledUsers[(nTeams-1)*nMembers : len(shuffledUsers)]
fmt.Println(result)
// send message
outputString := fmt.Sprintf("created %d team(s)!\n", nTeams)
for i := 0; i < nTeams; i++ {
outputString += fmt.Sprintf("Team%d: %s\n", i+1, strings.Join(result[i], ", "))
}
sendReply(s, m, outputString)
}