Skip to content

Commit bd5dbc4

Browse files
committed
temp radio thing
1 parent ef1757b commit bd5dbc4

11 files changed

Lines changed: 127 additions & 4 deletions

File tree

src/commands/forceskip.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ module.exports = {
2222

2323
const connection = interaction.client.connections.get(interaction.guildId);
2424

25+
if (connection.mode == 'radio') {
26+
return interaction.editReply(constructEmbed({ color: 'RED', description: 'You cannot force skip while the player is in radio mode.' }));
27+
}
28+
2529
if (!connection) {
2630
return interaction.editReply(constructEmbed({ color: 'RED', description: 'No music is currently playing.' }));
2731
}

src/commands/loop.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ module.exports = {
2727

2828
const connection = interaction.client.connections.get(interaction.guildId);
2929

30+
if (connection.mode == 'radio') {
31+
return interaction.editReply(constructEmbed({ color: 'RED', description: 'You cannot change the loop mode while the player is in radio mode.' }));
32+
}
33+
3034
if (!connection) {
3135
return interaction.editReply(constructEmbed({ color: 'RED', description: 'No music is currently playing.' }));
3236
}

src/commands/pause.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ module.exports = {
2525

2626
const connection = interaction.client.connections.get(interaction.guildId);
2727

28+
if (connection.mode == 'radio') {
29+
return interaction.editReply(constructEmbed({ color: 'RED', description: 'You cannot pause while the player is in radio mode.' }));
30+
}
31+
2832
if (!connection || connection.audioPlayer.state.status === AudioPlayerStatus.Idle) {
2933
return interaction.editReply(constructEmbed({ color: 'RED', description: 'No music is currently playing.' }));
3034
}

src/commands/play.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ module.exports = {
4949
return interaction.editReply('An error occured while attempting to play music.');
5050
}
5151

52+
if (connection.mode == 'radio') {
53+
return interaction.editReply(constructEmbed({ color: 'RED', description: 'You cannot add music to the queue while the player is in radio mode.' }));
54+
}
55+
5256
const maxlength = (await interaction.client.db.getNumberAsync(`librenote:settings:${interaction.guild.id}:maxlength`) ?? 15);
5357
const playlistmax = (await interaction.client.db.getNumberAsync(`librenote:settings:${interaction.guild.id}:playlistmax`) ?? 50);
5458

src/commands/queue.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports = {
2626
}
2727

2828
let totalLength = 0;
29-
const current = `Playing **[${connection.currentTrack.title}](${connection.currentTrack.url})**`;
29+
const current = `Playing **[${connection.currentTrack ? connection.currentTrack.title : 'Unknown'}](${connection.currentTrack ? connection.currentTrack.url : 'Unknown'})**`;
3030
const queue = connection.queue
3131
.slice(0, 10)
3232
.map((track, index) => {

src/commands/radio.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const { SlashCommandBuilder } = require('@discordjs/builders');
2+
const { constructEmbed } = require('../utility/embedConstructor');
3+
const { joinVoiceChannel, createAudioResource } = require('@discordjs/voice');
4+
const { canPerformAction } = require('../utility/permissions');
5+
const MusicConnection = require('../utility/musicConnection');
6+
7+
module.exports = {
8+
// data of the command
9+
data: new SlashCommandBuilder()
10+
.setName('radio')
11+
.setDescription('radio test thing'),
12+
// array of guild ids, null for global command
13+
guilds: ['953064066576949348'],
14+
// method to run the command
15+
async run(interaction) {
16+
const member = interaction.member;
17+
18+
if (!member) return interaction.reply({ content: 'You can only execute this command within a guild.', ephemeral: true });
19+
20+
await interaction.deferReply();
21+
22+
if (!member.voice.channelId) return interaction.editReply(constructEmbed({ color: 'RED', description: 'You must be in a voice channel to run this command.' }));
23+
if (!member.voice.channel.joinable) return interaction.editReply(constructEmbed({ color: 'RED', description: 'I cannot join your voice channel.' }));
24+
25+
const canUseCommand = await canPerformAction(member);
26+
if (!canUseCommand) return interaction.editReply(constructEmbed({ color: 'RED', description: 'DJ only mode is enabled. You must be a DJ to run this command.' }));
27+
28+
let connection = interaction.client.connections.get(interaction.guildId);
29+
30+
if (!connection) {
31+
connection = new MusicConnection(
32+
joinVoiceChannel({
33+
channelId: member.voice.channelId,
34+
guildId: interaction.guild.id,
35+
adapterCreator: interaction.guild.voiceAdapterCreator,
36+
}),
37+
);
38+
connection.voiceConnection.on('error', console.warn);
39+
interaction.client.connections.set(interaction.guildId, connection);
40+
}
41+
42+
if (!connection) {
43+
return interaction.editReply('An error occured while attempting to play music.');
44+
}
45+
46+
if (connection.mode == 'radio') {
47+
// ask if they want to change radio station OR switch to queue mode via buttons
48+
49+
connection.changeMode('queue');
50+
return interaction.editReply(':asterisk: **Switched to queue mode.**');
51+
}
52+
else {
53+
connection.changeMode('radio');
54+
await interaction.followUp(':asterisk: **Switched to radio mode.**');
55+
56+
const resource = createAudioResource('http://listen-gbnews.sharp-stream.com/gbnews.mp3');
57+
connection.audioPlayer.play(resource);
58+
59+
return interaction.editReply(constructEmbed({ color: 'BLUE', description: 'display select radio station thing' }));
60+
}
61+
},
62+
};

src/commands/resume.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ module.exports = {
2525

2626
const connection = interaction.client.connections.get(interaction.guildId);
2727

28+
if (connection.mode == 'radio') {
29+
return interaction.editReply(constructEmbed({ color: 'RED', description: 'You cannot resume while the player is in radio mode.' }));
30+
}
31+
2832
if (!connection || connection.audioPlayer.state.status === AudioPlayerStatus.Idle) {
2933
return interaction.editReply(constructEmbed({ color: 'RED', description: 'No music is currently playing.' }));
3034
}

src/commands/shuffle.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ module.exports = {
2424

2525
const connection = interaction.client.connections.get(interaction.guildId);
2626

27+
if (connection.mode == 'radio') {
28+
return interaction.editReply(constructEmbed({ color: 'RED', description: 'You cannot shuffle while the player is in radio mode.' }));
29+
}
30+
2731
if (!connection) {
2832
return interaction.editReply(constructEmbed({ color: 'RED', description: 'No music is currently playing.' }));
2933
}

src/commands/skip.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ module.exports = {
2020

2121
const connection = interaction.client.connections.get(interaction.guildId);
2222

23+
if (connection.mode == 'radio') {
24+
return interaction.editReply(constructEmbed({ color: 'RED', description: 'You cannot skip while the player is in radio mode.' }));
25+
}
26+
2327
if (!connection) return interaction.editReply(constructEmbed({ color: 'RED', description: 'No music is currently playing.' }));
2428

2529
if (member.voice.channel.members.size > 2) {

src/utility/musicConnection.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ module.exports = class MusicConnection {
3939
this.queue = [];
4040
this.loop = 'off';
4141
this.shuffle = false;
42+
this.mode = 'queue';
43+
// mode is queue or radio
4244

4345
this.voiceConnection.on('stateChange', async (_, newState) => {
4446
if (newState.status === VoiceConnectionStatus.Disconnected) {
@@ -79,7 +81,7 @@ module.exports = class MusicConnection {
7981
});
8082

8183
this.audioPlayer.on('stateChange', async (oldState, newState) => {
82-
if (newState.status === AudioPlayerStatus.Idle && oldState.status !== AudioPlayerStatus.Idle) {
84+
if (newState.status === AudioPlayerStatus.Idle && oldState.status !== AudioPlayerStatus.Idle && oldState.resource && oldState.resource.metadata) {
8385
if ((this.loop === 'track') && this.currentTrack) {
8486
const resource = await this.currentTrack.createAudioResource();
8587
this.audioPlayer.play(resource);
@@ -93,7 +95,7 @@ module.exports = class MusicConnection {
9395
void this.processQueue();
9496
}
9597
}
96-
else if (newState.status === AudioPlayerStatus.Playing && oldState.status !== AudioPlayerStatus.Paused) {
98+
else if (newState.status === AudioPlayerStatus.Playing && oldState.status !== AudioPlayerStatus.Paused && this.mode != 'radio') {
9799
newState.resource.metadata.onStart();
98100
}
99101
});
@@ -128,7 +130,7 @@ module.exports = class MusicConnection {
128130
* @description Processes the queue, plays the next track if there is one.
129131
*/
130132
async processQueue() {
131-
if (this.queueLock || this.audioPlayer.state.status !== AudioPlayerStatus.Idle || (this.queue.length === 0 && !(this.loop === 'track' && this.currentTrack))) {
133+
if (this.mode == 'radio' || (this.queueLock || this.audioPlayer.state.status !== AudioPlayerStatus.Idle || (this.queue.length === 0 && !(this.loop === 'track' && this.currentTrack)))) {
132134
return;
133135
}
134136

@@ -163,4 +165,30 @@ module.exports = class MusicConnection {
163165
}
164166
}
165167
}
168+
169+
/**
170+
* @method changeMode
171+
* @description Change queue mode to radio or queue (in the future playlists?)
172+
*/
173+
async changeMode(mode) {
174+
const modes = ['queue', 'radio'];
175+
if (!mode || !modes.includes(mode)) {
176+
mode = 'queue';
177+
}
178+
179+
if (mode == 'radio') {
180+
this.mode = 'radio';
181+
this.loop = 'off';
182+
this.audioPlayer.stop(true);
183+
this.queueLock = false;
184+
this.currentTrack = undefined;
185+
}
186+
else if (mode == 'queue' && this.mode != 'queue') {
187+
this.mode = 'queue';
188+
this.audioPlayer.stop(true);
189+
this.queueLock = false;
190+
this.currentTrack = undefined;
191+
this.processQueue();
192+
}
193+
}
166194
};

0 commit comments

Comments
 (0)