1
- const { GuildMember } = require ( 'discord.js' ) ;
1
+ const { GuildMember, Interaction , Message } = require ( 'discord.js' ) ;
2
2
const { QueryType } = require ( 'discord-player' ) ;
3
3
4
4
module . exports = {
@@ -12,66 +12,103 @@ module.exports = {
12
12
required : true ,
13
13
} ,
14
14
] ,
15
- async execute ( interaction , player ) {
15
+ async execute ( messageOrInteraction , player ) {
16
+ const isInteraction = messageOrInteraction instanceof Interaction ;
16
17
try {
17
- if ( ! ( interaction . member instanceof GuildMember ) || ! interaction . member . voice . channel ) {
18
- return void interaction . reply ( {
19
- content : 'You are not in a voice channel!' ,
20
- ephemeral : true ,
21
- } ) ;
18
+ if ( isInteraction ) {
19
+ if ( ! ( messageOrInteraction . member instanceof GuildMember ) || ! messageOrInteraction . member . voice . channel ) {
20
+ return void messageOrInteraction . reply ( {
21
+ content : 'You are not in a voice channel!' ,
22
+ ephemeral : true ,
23
+ } ) ;
24
+ }
25
+ await messageOrInteraction . deferReply ( ) ;
22
26
}
23
27
24
28
if (
25
- interaction . guild . me . voice . channelId &&
26
- interaction . member . voice . channelId !== interaction . guild . me . voice . channelId
29
+ messageOrInteraction . guild . me . voice . channelId &&
30
+ messageOrInteraction . member . voice . channelId !== messageOrInteraction . guild . me . voice . channelId
27
31
) {
28
- return void interaction . reply ( {
29
- content : 'You are not in my voice channel!' ,
30
- ephemeral : true ,
31
- } ) ;
32
+ const reply = 'You are not in my voice channel!' ;
33
+ if ( isInteraction ) {
34
+ return void messageOrInteraction . followUp ( {
35
+ content : reply ,
36
+ ephemeral : true ,
37
+ } ) ;
38
+ }
39
+ return void messageOrInteraction . channel . send ( reply ) ;
32
40
}
41
+ let query = '' ;
42
+ if ( isInteraction ) {
43
+ query = messageOrInteraction . options . get ( 'query' ) . value ;
44
+ } else {
45
+ query = messageOrInteraction . content . replace ( '!play' , '' ) . trim ( ) ;
46
+ if ( query === '' ) {
47
+ return void messageOrInteraction . channel . send ( 'empty search query!' ) ;
48
+ }
33
49
34
- await interaction . deferReply ( ) ;
50
+ }
35
51
player . use ( "YOUTUBE_DL" , require ( "@discord-player/downloader" ) . Downloader ) ;
36
- const query = interaction . options . get ( 'query' ) . value ;
52
+ let requestedBy = '' ;
53
+ if ( isInteraction ) {
54
+ requestedBy = messageOrInteraction . user ;
55
+ } else {
56
+ requestedBy = messageOrInteraction . author ;
57
+ }
37
58
const searchResult = await player
38
59
. search ( query , {
39
- requestedBy : interaction . user ,
60
+ requestedBy : requestedBy ,
40
61
searchEngine : QueryType . AUTO ,
41
62
} )
42
63
. catch ( ( ) => { } ) ;
43
- if ( ! searchResult || ! searchResult . tracks . length )
44
- return void interaction . followUp ( { content : 'No results were found!' } ) ;
45
-
46
- const queue = await player . createQueue ( interaction . guild , {
64
+ if ( ! searchResult || ! searchResult . tracks . length ) {
65
+ const reply = 'No results were found!' ;
66
+ if ( isInteraction ) {
67
+ return void messageOrInteraction . followUp ( { content : reply } ) ;
68
+ }
69
+ return void messageOrInteraction . channel . send ( reply ) ;
70
+ }
71
+ const queue = await player . createQueue ( messageOrInteraction . guild , {
47
72
ytdlOptions : {
48
73
quality : "highest" ,
49
74
filter : "audioonly" ,
50
75
highWaterMark : 1 << 25 ,
51
76
dlChunkSize : 0 ,
52
77
} ,
53
- metadata : interaction . channel ,
78
+ metadata : messageOrInteraction . channel ,
54
79
} ) ;
55
80
56
81
try {
57
- if ( ! queue . connection ) await queue . connect ( interaction . member . voice . channel ) ;
58
- } catch {
59
- void player . deleteQueue ( interaction . guildId ) ;
60
- return void interaction . followUp ( {
61
- content : 'Could not join your voice channel!' ,
82
+ if ( ! queue . connection ) await queue . connect ( messageOrInteraction . member . voice . channel ) ;
83
+ } catch ( error ) {
84
+ void player . deleteQueue ( messageOrInteraction . guildId ) ;
85
+ const reply = 'Could not join your voice channel!' ;
86
+ if ( isInteraction ) {
87
+ return void messageOrInteraction . followUp ( {
88
+ content : reply ,
89
+ } ) ;
90
+ }
91
+ return void messageOrInteraction . channel . send ( reply ) ;
92
+ }
93
+ const loadingMessage = `⏱ | Loading your ${ searchResult . playlist ? 'playlist' : 'track' } ...` ;
94
+ if ( isInteraction ) {
95
+ await messageOrInteraction . followUp ( {
96
+ content : loadingMessage ,
62
97
} ) ;
98
+ } else {
99
+ await messageOrInteraction . channel . send ( loadingMessage ) ;
63
100
}
64
-
65
- await interaction . followUp ( {
66
- content : `⏱ | Loading your ${ searchResult . playlist ? 'playlist' : 'track' } ...` ,
67
- } ) ;
68
101
searchResult . playlist ? queue . addTracks ( searchResult . tracks ) : queue . addTrack ( searchResult . tracks [ 0 ] ) ;
69
102
if ( ! queue . playing ) await queue . play ( ) ;
70
103
} catch ( error ) {
71
104
console . log ( error ) ;
72
- interaction . followUp ( {
73
- content : 'There was an error trying to execute that command: ' + error . message ,
74
- } ) ;
105
+ const reply = 'There was an error trying to execute that command: ' + error . message ;
106
+ if ( isInteraction ) {
107
+ messageOrInteraction . followUp ( {
108
+ content : reply ,
109
+ } ) ;
110
+ }
111
+ return void messageOrInteraction . channel . send ( reply ) ;
75
112
}
76
- } ,
113
+ }
77
114
} ;
0 commit comments