src/music/commands.go (view raw)
1package music
2
3import (
4 "fmt"
5
6 gl "github.com/BiRabittoh/disgord/src/globals"
7 "github.com/bwmarrin/discordgo"
8 "github.com/kkdai/youtube/v2"
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 = youtube.Client{}
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 video information
43 video, err := yt.GetVideo(args[0])
44 if err != nil {
45 logger.Errorf("could not get video: %v", err)
46 return gl.MsgError
47 }
48
49 // Get the queue for the guild
50 q := GetOrCreateQueue(voice)
51
52 // Add video to the queue
53 q.AddVideo(video)
54
55 return fmt.Sprintf(MsgAddedToQueue, gl.FormatVideo(video))
56}
57
58func HandlePause(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {
59 r, g, vc := gl.GetVoiceChannelID(s, m)
60 if r != "" {
61 return r
62 }
63
64 q := GetQueue(g.ID)
65 if q == nil {
66 return MsgNothingIsPlaying
67 }
68
69 if vc != q.VoiceChannelID() {
70 return MsgSameVoiceChannel
71 }
72
73 q.Pause()
74
75 return MsgPaused
76}
77
78func HandleResume(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {
79 r, g, vc := gl.GetVoiceChannelID(s, m)
80 if r != "" {
81 return r
82 }
83
84 q := GetQueue(g.ID)
85 if q == nil {
86 return MsgNothingIsPlaying
87 }
88
89 if vc != q.VoiceChannelID() {
90 return MsgSameVoiceChannel
91 }
92
93 q.Resume()
94
95 return MsgResumed
96}
97
98func HandleSkip(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {
99 r, g, vc := gl.GetVoiceChannelID(s, m)
100 if r != "" {
101 return r
102 }
103
104 q := GetQueue(g.ID)
105 if q == nil {
106 return MsgNothingIsPlaying
107 }
108
109 if vc != q.VoiceChannelID() {
110 return MsgSameVoiceChannel
111 }
112
113 err := q.PlayNext()
114 if err != nil {
115 return MsgNothingIsPlaying
116 }
117
118 return MsgSkipped
119}
120
121func HandleQueue(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {
122 q := GetQueue(m.GuildID)
123 if q == nil {
124 return MsgNothingIsPlaying
125 }
126
127 var out string
128 videos := q.Videos()
129 for i, v := range videos {
130 out += fmt.Sprintf(MsgQueueLine, i, gl.FormatVideo(v))
131 }
132 return out
133}
134
135func HandleClear(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {
136 r, g, vc := gl.GetVoiceChannelID(s, m)
137 if r != "" {
138 return r
139 }
140
141 q := GetQueue(g.ID)
142 if q == nil {
143 return MsgNothingIsPlaying
144 }
145
146 if vc != q.VoiceChannelID() {
147 return MsgSameVoiceChannel
148 }
149
150 q.Clear()
151
152 return MsgCleared
153}
154
155func HandleLeave(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string {
156 r, g, vc := gl.GetVoiceChannelID(s, m)
157 if r != "" {
158 return r
159 }
160
161 q := GetQueue(g.ID)
162 if q == nil {
163 return MsgNothingIsPlaying
164 }
165
166 if vc != q.VoiceChannelID() {
167 return MsgSameVoiceChannel
168 }
169
170 err := q.Stop()
171 if err != nil {
172 return gl.MsgError
173 }
174
175 return MsgLeft
176}