-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
214 lines (195 loc) · 5.08 KB
/
commands.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
package main
import (
"fmt"
"strings"
"github.com/bwmarrin/discordgo"
"path/filepath"
)
func processCommand(s *discordgo.Session, m *discordgo.Message) {
if len(m.Content) < 1 {
return
}
parts := strings.Split(m.Content, " ")
if len(parts) < 1 {
return
}
//TODO
col := data.Soundcollections[parts[0]]
if col != nil {
var sound *Sound
if len(parts) > 1 {
sound = col.GetSound(parts[1])
}
if sound == nil {
sound = col.GetRandomSound()
}
channel, _ := s.State.Channel(m.ChannelID)
guild, err := s.State.Guild(channel.GuildID)
if guild == nil || err != nil {
return
}
vc := getCurrentVoiceChannel(s, m.Author, guild)
if vc != nil {
enqueueSound(s, vc.GuildID, vc.ID, sound)
}
return
}
if parts[0] == "stopwaiting" {
if foreignCommand {
s.ChannelMessageSend(m.ChannelID, "Ok, no waiting")
}
resetForeignCommand()
}
for _, c := range commands {
if parts[0] == c.Command {
c.Function(s, m, parts)
}
}
}
func resetForeignCommand() {
foreignCommand = false
foreignCommandType = ""
foreignCommandUser = nil
}
func onForeignCommand(s *discordgo.Session, m *discordgo.Message) {
switch foreignCommandType {
case "image":
if len(m.Attachments) > 0 {
if len(foreignCommandUser) == 0 {
foreignAvatarChange(s, m)
return
}
for _, n := range foreignCommandUser {
if n == m.Author.ID {
foreignAvatarChange(s, m)
return
}
}
}
}
}
func foreignAvatarChange(s *discordgo.Session, m *discordgo.Message) {
err := changeAvatarByURL(s, m.Attachments[0].URL)
if err != nil {
s.ChannelMessageSend(m.ChannelID, "Damn, something went wrong.")
}
resetForeignCommand()
}
func addSoundToLibrary(file string, collection string, name string) {
sound := NewSound(name, file)
sound.Load()
data.sclock.Lock()
defer data.sclock.Unlock()
col := data.Soundcollections[collection]
if col == nil {
col = &SoundCollection{Name: collection}
data.Soundcollections[collection] = col
} else {
for _, s := range col.Sounds {
if s.Name == name {
name += "_d"
}
}
}
col.Sounds = append(col.Sounds, sound)
data.changed = true
}
func Set(s *discordgo.Session, m *discordgo.Message, parts []string) {
if m.Author.ID != OWNER {
return
}
if len(parts) < 2 {
return
}
switch parts[1] {
case "avatar", "image":
setAvatar(s, m, parts)
}
}
func setAvatar(s *discordgo.Session, m *discordgo.Message, parts []string) {
if len(m.Attachments) > 0 {
err := changeAvatarByURL(s, m.Attachments[0].URL)
if err != nil {
s.ChannelMessageSend(m.ChannelID, "Sorry, I got rekt up")
} else {
s.ChannelMessageSend(m.ChannelID, "Done!")
}
} else {
foreignCommand = true
foreignCommandType = "image"
if len(parts) > 2 {
for _, mention := range m.Mentions {
foreignCommandUser = append(foreignCommandUser, mention.ID)
}
}
s.ChannelMessageSend(m.ChannelID, "Ok, waiting for an image.")
}
}
func Get(s *discordgo.Session, m *discordgo.Message, parts []string) {
if len(parts) < 2 {
return
}
switch strings.ToLower(parts[1]) {
case "id":
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%v's ID: %v", generateTag(m.Author.ID), m.Author.ID))
case "owner", "master":
WhoIsOwner(s, m, parts)
}
}
func WhoIsOwner(s *discordgo.Session, m *discordgo.Message, parts []string) {
if m.Author.ID == OWNER {
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("You are my master %v", generateTag(OWNER)))
} else {
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("My master is %v", generateTag(OWNER)))
}
}
func Kill(s *discordgo.Session, m *discordgo.Message, parts []string) {
if m.Author.ID == OWNER {
s.ChannelMessageSend(m.ChannelID, "Yes, Sir!\nI will kill myself.")
exit <- true
}
}
func PlayGame(s *discordgo.Session, m *discordgo.Message, parts []string) {
if len(parts) > 1 {
s.UpdateStatus(0, generateString(parts[1:]...))
}
}
func SoundsHelp(s *discordgo.Session, m *discordgo.Message, parts []string) {
output := "I can play these collections:\n"
for _, c := range data.Soundcollections {
output += fmt.Sprintf("-!%v\n", c.Name)
}
s.ChannelMessageSend(m.ChannelID, output)
}
func addCommand(cmd string, function commandFunc) {
commands = append(commands, command{Command: cmd, Function: function})
}
func AddSound(s *discordgo.Session, m *discordgo.Message, parts []string) {
if m.Author.ID != OWNER {
return
}
if len(m.Attachments) > 0 {
if len(parts) > 2 {
path := filepath.Join(BASEPATH, RELATIVE_SOUNDS_PATH)
file, err := GetSoundByURL(m.Attachments[0].URL, path, parts[1]+"-"+parts[2])
if err != nil {
s.ChannelMessageSend(m.ChannelID, "Sorry, I got rekt up")
} else {
addSoundToLibrary(file, parts[1], parts[2])
s.ChannelMessageSend(m.ChannelID, "Done!")
}
} else {
s.ChannelMessageSend(m.ChannelID, "Wrong Parameter")
}
} else {
// foreignCommand = true
// foreignCommandType = "sound"
// if len(parts) > 2 {
// for _, mention := range m.Mentions {
// foreignCommandUser = append(foreignCommandUser, mention.ID)
// }
// }
// s.ChannelMessageSend(m.ChannelID, "Ok, waiting for an image.")
s.ChannelMessageSend(m.ChannelID, "You forgot the file...")
}
}