Skip to content

Commit 7dd47a6

Browse files
author
Theodoros Gerakitis
committed
progress
1 parent 3459535 commit 7dd47a6

File tree

5 files changed

+1936
-67
lines changed

5 files changed

+1936
-67
lines changed

commands/play.js

+71-34
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { GuildMember } = require('discord.js');
1+
const { GuildMember, Interaction, Message } = require('discord.js');
22
const { QueryType } = require('discord-player');
33

44
module.exports = {
@@ -12,66 +12,103 @@ module.exports = {
1212
required: true,
1313
},
1414
],
15-
async execute(interaction, player) {
15+
async execute(messageOrInteraction, player) {
16+
const isInteraction = messageOrInteraction instanceof Interaction;
1617
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();
2226
}
2327

2428
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
2731
) {
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);
3240
}
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+
}
3349

34-
await interaction.deferReply();
50+
}
3551
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+
}
3758
const searchResult = await player
3859
.search(query, {
39-
requestedBy: interaction.user,
60+
requestedBy: requestedBy,
4061
searchEngine: QueryType.AUTO,
4162
})
4263
.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, {
4772
ytdlOptions: {
4873
quality: "highest",
4974
filter: "audioonly",
5075
highWaterMark: 1 << 25,
5176
dlChunkSize: 0,
5277
},
53-
metadata: interaction.channel,
78+
metadata: messageOrInteraction.channel,
5479
});
5580

5681
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,
6297
});
98+
} else {
99+
await messageOrInteraction.channel.send(loadingMessage);
63100
}
64-
65-
await interaction.followUp({
66-
content: `⏱ | Loading your ${searchResult.playlist ? 'playlist' : 'track'}...`,
67-
});
68101
searchResult.playlist ? queue.addTracks(searchResult.tracks) : queue.addTrack(searchResult.tracks[0]);
69102
if (!queue.playing) await queue.play();
70103
} catch (error) {
71104
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);
75112
}
76-
},
113+
}
77114
};

docker-compose.yaml

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
version: '3.3'
22

3+
volumes:
4+
node-modules:
35
services:
46
discord-bot:
57
build:
6-
context: ./
7-
dockerfile: Dockerfile
8+
context: ./
9+
dockerfile: Dockerfile
810
environment:
911
DISCORD_TOKEN: ${DISCORD_TOKEN}
10-
container_name: discord-bot
11-
restart: always
12+
restart: always
13+
command: [ "npm", "run", "debug" ]
14+
ports:
15+
- 9229:9229
16+
volumes:
17+
- ./:/usr/src/app
18+
- node-modules:/usr/src/app/node_modules

index.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ for (const file of commandFiles) {
2020
client.commands.set(command.name, command);
2121
}
2222

23-
console.log(client.commands);
24-
2523
const player = new Player(client);
2624

2725
player.on('error', (queue, error) => {
@@ -68,6 +66,7 @@ client.once('disconnect', () => {
6866
console.log('Disconnect!');
6967
});
7068

69+
7170
client.on('messageCreate', async message => {
7271
if (message.author.bot || !message.guild) return;
7372
if (!client.application?.owner) await client.application?.fetch();
@@ -83,6 +82,12 @@ client.on('messageCreate', async message => {
8382
console.error(err);
8483
});
8584
}
85+
86+
if (message.content.startsWith('!play')) {
87+
const play = require('./commands/play');
88+
//command.execute(interaction, player);
89+
play.execute(message, player);
90+
}
8691
});
8792

8893
client.on('interactionCreate', async interaction => {

0 commit comments

Comments
 (0)