/
commands.go
158 lines (142 loc) · 4.48 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
package piccolo
import (
"fmt"
"sort"
"strings"
"github.com/bwmarrin/discordgo"
"github.com/jatgam/goutils"
"github.com/jatgam/goutils/log"
)
type (
command func(b *Bot, m *discordgo.MessageCreate)
commandMap map[string]command
commandHandler struct {
commands commandMap
}
)
var (
cmdHandler *commandHandler
)
func init() {
cmdHandler = &commandHandler{make(commandMap)}
cmdHandler.addCommand("help", help)
cmdHandler.addCommand("version", botVersion)
cmdHandler.addCommand("play", play)
cmdHandler.addCommand("skipSong", skipSong)
cmdHandler.addCommand("savePlaylist", savePlaylist)
cmdHandler.addCommand("showPlaylist", printPlaylist)
}
func (h commandHandler) addCommand(name string, c command) {
h.commands[name] = c
}
func (h commandHandler) getAllCommands() commandMap {
return h.commands
}
func (h commandHandler) get(name string) (*command, bool) {
cmd, found := h.commands[name]
return &cmd, found
}
func help(b *Bot, m *discordgo.MessageCreate) {
var msg string
var cmdListStr string
var cmdList []string
for cmdN := range cmdHandler.getAllCommands() {
cmdList = append(cmdList, b.conf.CommandPrefix+cmdN)
}
sort.Strings(cmdList)
cmdListStr = fmt.Sprintf("```%s```", goutils.StrJoin(cmdList, " "))
msg = fmt.Sprintf("<@%s>, **Commands**\n%s\n%s", m.Author.ID, cmdListStr, "https://github.com/shawnsilva/piccolo/wiki/Commands")
b.reply(msg, m)
}
func botVersion(b *Bot, m *discordgo.MessageCreate) {
msg := fmt.Sprintf("<@%s>, `VERSION: %s`", m.Author.ID, b.version.GetVersionString())
b.reply(msg, m)
}
func play(b *Bot, m *discordgo.MessageCreate) {
if _, ok := b.textChannelLookup[m.ChannelID]; !ok {
log.WithFields(log.Fields{
"channel": m.ChannelID,
}).Error("Failed to find controller from channel id")
return
}
splitString := strings.SplitN(m.Content, " ", 2)
if len(splitString) <= 1 {
log.WithFields(log.Fields{
"command": m.Content,
}).Warn("!play doesn't appear to contain a search string")
b.reply(fmt.Sprintf("<@%s> - Sorry, your command didn't appear to have a song to search for: **%s**", m.Author.ID, m.Content), m)
return
}
song := splitString[1]
result, err := b.yt.SearchFirstResult(song)
if err != nil {
log.WithFields(log.Fields{
"song": song,
"error": err,
}).Debug("Failed to find song")
b.reply(fmt.Sprintf("<@%s> - Sorry, couldn't find a result for: **%s**", m.Author.ID, song), m)
return
}
b.textChannelLookup[m.ChannelID].player.playlist.addSong(m.Author, m.ChannelID, result.ID.VideoID, result.Snippet.Title)
go b.textChannelLookup[m.ChannelID].player.downloadNextSong()
b.reply(fmt.Sprintf("<@%s> - Enqueued **%s** to be played.", m.Author.ID, result.Snippet.Title), m)
}
func skipSong(b *Bot, m *discordgo.MessageCreate) {
if _, ok := b.textChannelLookup[m.ChannelID]; !ok {
log.WithFields(log.Fields{
"channel": m.ChannelID,
}).Error("Failed to find controller from channel id")
return
}
textChannelInfo, err := b.dg.Channel(m.ChannelID)
if err != nil {
log.Error("Failed to determine text channel info")
return
}
gID := textChannelInfo.GuildID
guildInfo, err := b.dg.Guild(gID)
if err != nil {
log.Error("Failed to determine guild info")
return
}
numListeners := 0
foundRequester := false
for _, vs := range guildInfo.VoiceStates {
if b.textChannelLookup[m.ChannelID].voiceChannelID != vs.ChannelID {
continue
}
numListeners = numListeners + 1
if m.Author.ID == vs.UserID {
foundRequester = true
}
}
if !foundRequester {
b.reply(fmt.Sprintf("<@%s> - You are not even listening, you don't get to skip!", m.Author.ID), m)
return
}
message := b.textChannelLookup[m.ChannelID].player.Skip(numListeners-1, m.Author.ID)
b.reply(message, m)
}
func savePlaylist(b *Bot, m *discordgo.MessageCreate) {
if _, ok := b.textChannelLookup[m.ChannelID]; !ok {
log.WithFields(log.Fields{
"channel": m.ChannelID,
}).Error("Failed to find controller from channel id")
return
}
err := b.textChannelLookup[m.ChannelID].player.playlist.savePlaylist()
if err == nil {
b.reply(fmt.Sprintf("<@%s> - Saved current playlist to disk.", m.Author.ID), m)
} else {
b.reply(fmt.Sprintf("<@%s> - %s.", m.Author.ID, err.Error()), m)
}
}
func printPlaylist(b *Bot, m *discordgo.MessageCreate) {
if _, ok := b.textChannelLookup[m.ChannelID]; !ok {
log.WithFields(log.Fields{
"channel": m.ChannelID,
}).Error("Failed to find controller from channel id")
return
}
b.reply(fmt.Sprintf("<@%s> - **Current Playlist**\n\n%s", m.Author.ID, b.textChannelLookup[m.ChannelID].player.playlist), m)
}