all repos — disgord @ 0769e5ef6b52a3e567e837fe232691bc6e57f912

A simple Discord bot in Go.

src/music/commands.go (view raw)

  1package music
  2
  3import (
  4	"fmt"
  5
  6	gl "github.com/birabittoh/disgord/src/globals"
  7	"github.com/birabittoh/rabbitpipe"
  8	"github.com/bwmarrin/discordgo"
  9)
 10
 11const (
 12	MsgNoURL            = "Please, provide a YouTube URL."
 13	MsgAddedToQueue     = "Added to queue: %s."
 14	MsgNothingIsPlaying = "Nothing is playing."
 15	MsgSameVoiceChannel = "You need to be in the same voice channel to use this command."
 16	MsgPaused           = "Paused."
 17	MsgResumed          = "Resumed."
 18	MsgSkipped          = "Skipped."
 19	MsgCleared          = "Cleared."
 20	MsgLeft             = "Left."
 21	MsgQueueLine        = "%d. %s\n"
 22)
 23
 24var yt = rabbitpipe.New()
 25
 26func HandlePlay(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {
 27	r, _, vc := gl.GetVoiceChannelID(s, m)
 28	if r != "" {
 29		return r
 30	}
 31
 32	if len(args) == 0 {
 33		return MsgNoURL
 34	}
 35
 36	voice, err := s.ChannelVoiceJoin(m.GuildID, vc, false, true)
 37	if err != nil {
 38		logger.Errorf("could not join voice channel: %v", err)
 39		return gl.MsgError
 40	}
 41
 42	// Get the queue for the guild
 43	q := GetOrCreateQueue(voice)
 44
 45	// Get the video information
 46	video, err := getVideo(args)
 47	if err != nil {
 48		logger.Errorf("could not get video: %v", err)
 49		if q.nowPlaying == nil {
 50			voice.Disconnect()
 51		}
 52		return gl.MsgError
 53	}
 54
 55	// Add video to the queue
 56	q.AddVideo(video)
 57
 58	return fmt.Sprintf(MsgAddedToQueue, gl.FormatVideo(video))
 59}
 60
 61func HandlePause(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {
 62	r, g, vc := gl.GetVoiceChannelID(s, m)
 63	if r != "" {
 64		return r
 65	}
 66
 67	q := GetQueue(g.ID)
 68	if q == nil {
 69		return MsgNothingIsPlaying
 70	}
 71
 72	if vc != q.VoiceChannelID() {
 73		return MsgSameVoiceChannel
 74	}
 75
 76	q.Pause()
 77
 78	return MsgPaused
 79}
 80
 81func HandleResume(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {
 82	r, g, vc := gl.GetVoiceChannelID(s, m)
 83	if r != "" {
 84		return r
 85	}
 86
 87	q := GetQueue(g.ID)
 88	if q == nil {
 89		return MsgNothingIsPlaying
 90	}
 91
 92	if vc != q.VoiceChannelID() {
 93		return MsgSameVoiceChannel
 94	}
 95
 96	q.Resume()
 97
 98	return MsgResumed
 99}
100
101func HandleSkip(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {
102	r, g, vc := gl.GetVoiceChannelID(s, m)
103	if r != "" {
104		return r
105	}
106
107	q := GetQueue(g.ID)
108	if q == nil {
109		return MsgNothingIsPlaying
110	}
111
112	if vc != q.VoiceChannelID() {
113		return MsgSameVoiceChannel
114	}
115
116	err := q.PlayNext()
117	if err != nil {
118		return MsgNothingIsPlaying
119	}
120
121	return MsgSkipped
122}
123
124func HandleQueue(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {
125	q := GetQueue(m.GuildID)
126	if q == nil {
127		return MsgNothingIsPlaying
128	}
129
130	var out string
131	videos := q.Videos()
132	for i, v := range videos {
133		out += fmt.Sprintf(MsgQueueLine, i, gl.FormatVideo(v))
134	}
135	return out
136}
137
138func HandleClear(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {
139	r, g, vc := gl.GetVoiceChannelID(s, m)
140	if r != "" {
141		return r
142	}
143
144	q := GetQueue(g.ID)
145	if q == nil {
146		return MsgNothingIsPlaying
147	}
148
149	if vc != q.VoiceChannelID() {
150		return MsgSameVoiceChannel
151	}
152
153	q.Clear()
154
155	return MsgCleared
156}
157
158func HandleLeave(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {
159	r, g, vc := gl.GetVoiceChannelID(s, m)
160	if r != "" {
161		return r
162	}
163
164	q := GetQueue(g.ID)
165	if q == nil {
166		return MsgNothingIsPlaying
167	}
168
169	if vc != q.VoiceChannelID() {
170		return MsgSameVoiceChannel
171	}
172
173	err := q.Stop()
174	if err != nil {
175		return gl.MsgError
176	}
177
178	return MsgLeft
179}
180
181func HandleBotVSU(vsu *discordgo.VoiceStateUpdate) {
182	if vsu.BeforeUpdate == nil {
183		// user joined a voice channel
184		return
185	}
186
187	queue := GetQueue(vsu.GuildID)
188	if queue == nil {
189		// no queue for this guild
190		return
191	}
192
193	if queue.NowPlaying() == nil {
194		// song has ended naturally
195		return
196	}
197
198	vc := queue.VoiceConnection()
199	if vc == nil {
200		return
201	}
202
203	if vsu.ChannelID == "" && vsu.BeforeUpdate.ChannelID == vc.ChannelID {
204		logger.Println("Bot disconnected from voice channel, stopping audio playback.")
205		queue.Stop()
206	}
207}