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
|
package commands
import (
"ai/types"
"ai/utils/logger"
"ai/utils/music"
"fmt"
"strings"
"github.com/bwmarrin/discordgo"
)
func Play(s *discordgo.Session, i *discordgo.InteractionCreate) {
options := i.ApplicationCommandData().Options
input := options[0].StringValue()
if input == "min_chars" {
respondWithError(s, i, "Enter at least 3 characters to search.")
return
}
if input == "no_results" || input == "search_error" {
respondWithError(s, i, "No results found for your query. Try a different search term.")
return
}
guildID := i.GuildID
userID := i.Member.User.ID
isSameVC, userChannelID := music.IsUserInSameVC(s, guildID, userID)
if userChannelID == "" {
respondWithError(s, i, "You must be in a voice channel to use this command.")
return
}
voice, exists := music.VoiceConnection[guildID]
if exists && !isSameVC {
channel, err := s.Channel(voice.ChannelID)
if err == nil {
respondWithError(s, i, fmt.Sprintf("I'm already in the voice channel **%s**. You must be in the same voice channel to control playback.", channel.Name))
} else {
respondWithError(s, i, "I'm already in a different voice channel. You must be in the same voice channel to control playback.")
}
return
}
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
})
var trackURL, trackID, trackTitle string
var sourceType types.SourceType
if strings.Contains(input, "|") {
parts := strings.Split(input, "|")
if len(parts) >= 3 {
sourceType = types.SourceType(parts[0])
trackID = parts[1]
trackURL = parts[2]
trackInfo, err := music.GetTrackInfo(trackID, sourceType)
if err != nil {
trackTitle = "Selected track"
} else {
trackTitle = trackInfo.Title
}
if sourceType == types.Spotify {
ytTrack, err := music.GetYouTubeForSpotify(trackInfo.Title, trackInfo.Artist)
if err != nil {
updateResponse(s, i, "❌ Error fetching YouTube equivalent for Spotify track.")
return
}
trackURL = ytTrack.URL
trackID = ytTrack.ID
}
} else {
updateResponse(s, i, "❌ Invalid track selection. Please try again.")
return
}
} else {
if music.IsYouTubeURL(input) {
trackInfo, err := music.GetYouTubeInfo(input)
if err != nil {
updateResponse(s, i, "❌ Failed to get information for this YouTube URL.")
return
}
trackURL = input
trackID = trackInfo.ID
trackTitle = trackInfo.Title
sourceType = types.YouTube
} else if music.IsSpotifyURL(input) {
trackInfo, err := music.GetSpotifyInfo(input)
if err != nil {
updateResponse(s, i, "❌ Failed to get information for this Spotify URL.")
return
}
ytTrack, err := music.GetYouTubeForSpotify(trackInfo.Title, trackInfo.Artist)
if err != nil {
updateResponse(s, i, "❌ Error fetching YouTube equivalent for Spotify track.")
return
}
trackURL = ytTrack.URL
trackID = ytTrack.ID
trackTitle = trackInfo.Title
sourceType = types.Spotify
} else {
results, err := music.Search(input, 1)
if err != nil || len(results) == 0 {
updateResponse(s, i, "❌ No results found for your search query.")
return
}
result := results[0]
trackTitle = result.Title
trackID = result.ID
sourceType = result.SourceType
if result.SourceType == types.Spotify {
ytTrack, err := music.GetYouTubeForSpotify(result.Title, result.Artist)
if err != nil {
updateResponse(s, i, "❌ Error fetching YouTube equivalent for Spotify track.")
return
}
trackURL = ytTrack.URL
trackID = ytTrack.ID
} else {
trackURL = result.URL
}
}
}
voice, err := music.JoinVoiceChannel(s, guildID, userChannelID)
if err != nil {
logger.Log(fmt.Sprintf("Failed to join voice channel: %v", err), types.LogOptions{
Prefix: "Play Command",
Level: types.Error,
})
updateResponse(s, i, "❌ Failed to join your voice channel.")
return
}
updateResponse(s, i, fmt.Sprintf("🎵 Now playing: **%s**", trackTitle))
go func() {
err := voice.PlayYouTube(trackURL, trackID)
if err != nil {
logger.Log(fmt.Sprintf("Failed to play track: %v", err), types.LogOptions{
Prefix: "Play Command",
Level: types.Error,
})
updateResponse(s, i, fmt.Sprintf("❌ Error playing **%s**: %v", trackTitle, err))
}
}()
}
|