all repos — disgord @ 788b481ce887db1dccfc707bc18e508405fdd809

A simple Discord bot in Go.

src/music/commands.go (view raw)

  1package music
  2
  3import (
  4	gl "github.com/BiRabittoh/disgord/src/globals"
  5	"github.com/bwmarrin/discordgo"
  6)
  7
  8func HandlePlay(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {
  9	r, _, vc := gl.GetVoiceChannelID(s, m)
 10	if r != "" {
 11		return r
 12	}
 13
 14	if len(args) == 0 {
 15		return "Please, provide a YouTube URL."
 16	}
 17
 18	voice, err := s.ChannelVoiceJoin(m.GuildID, vc, false, true)
 19	if err != nil {
 20		logger.Errorf("could not join voice channel: %v", err)
 21		return gl.MsgError
 22	}
 23
 24	// Get the video information
 25	video, err := gl.YT.GetVideo(args[0])
 26	if err != nil {
 27		logger.Errorf("could not get video: %v", err)
 28		return gl.MsgError
 29	}
 30
 31	// Get the queue for the guild
 32	q := GetOrCreateQueue(voice)
 33
 34	// Add video to the queue
 35	q.AddVideo(video)
 36
 37	return "Added to queue: " + gl.FormatVideo(video)
 38}
 39
 40func HandlePause(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {
 41	r, g, vc := gl.GetVoiceChannelID(s, m)
 42	if r != "" {
 43		return r
 44	}
 45
 46	q := GetQueue(g.ID)
 47	if q == nil {
 48		return "Nothing is playing."
 49	}
 50
 51	if vc != q.VoiceChannelID() {
 52		return "You need to be in the same voice channel to use this command."
 53	}
 54
 55	q.Pause()
 56
 57	return "Paused."
 58}
 59
 60func HandleResume(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {
 61	r, g, vc := gl.GetVoiceChannelID(s, m)
 62	if r != "" {
 63		return r
 64	}
 65
 66	q := GetQueue(g.ID)
 67	if q == nil {
 68		return "Nothing is playing."
 69	}
 70
 71	if vc != q.VoiceChannelID() {
 72		return "You need to be in the same voice channel to use this command."
 73	}
 74
 75	q.Resume()
 76
 77	return "Resumed."
 78}
 79
 80func HandleSkip(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {
 81	r, g, vc := gl.GetVoiceChannelID(s, m)
 82	if r != "" {
 83		return r
 84	}
 85
 86	q := GetQueue(g.ID)
 87	if q == nil {
 88		return "Nothing is playing."
 89	}
 90
 91	if vc != q.VoiceChannelID() {
 92		return "You need to be in the same voice channel to use this command."
 93	}
 94
 95	err := q.PlayNext()
 96	if err != nil {
 97		return "Nothing is playing."
 98	}
 99
100	return "Skipped."
101}
102
103func HandleQueue(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {
104	q := GetQueue(m.GuildID)
105	if q == nil {
106		return "Nothing is playing."
107	}
108
109	var out string
110	videos := q.Videos()
111	for _, v := range videos {
112		out += gl.FormatVideo(v) + "\n"
113	}
114	return out
115}
116
117func HandleClear(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {
118	r, g, vc := gl.GetVoiceChannelID(s, m)
119	if r != "" {
120		return r
121	}
122
123	q := GetQueue(g.ID)
124	if q == nil {
125		return "Nothing is playing."
126	}
127
128	if vc != q.VoiceChannelID() {
129		return "You need to be in the same voice channel to use this command."
130	}
131
132	q.Clear()
133
134	return "Cleared."
135}
136
137func HandleLeave(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {
138	r, g, vc := gl.GetVoiceChannelID(s, m)
139	if r != "" {
140		return r
141	}
142
143	q := GetQueue(g.ID)
144	if q == nil {
145		return "Nothing is playing."
146	}
147
148	if vc != q.VoiceChannelID() {
149		return "You need to be in the same voice channel to use this command."
150	}
151
152	err := q.Stop()
153	if err != nil {
154		return gl.MsgError
155	}
156
157	return "Cleared."
158}