Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/commands/join.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ export default {

let channel = client.channels.cache.get(guildData.home);
if (!channel) return;

// Get currently connected members
let humanMembers = channel.members.filter(member => !member.user.bot);

// If no human members are left, destroy the connection
if (humanMembers.size === 0) {
console.log(`Did not join ${channel.name} as it is empty.`);
return;
}

try {
radio.playRadio(message, radioURL, channel, channel.guild);
Expand Down Expand Up @@ -51,6 +60,15 @@ export default {
if (!channel)
return message.channel.send(L._U(guildData.locale, "no_find_voice"));

// Get currently connected members
let humanMembers = channel.members.filter(member => !member.user.bot);

// If no human members are left, destroy the connection
if (humanMembers.size === 0) {
console.log(`Did not join ${channel.name} as it is empty.`);
return;
}

radioURL = guildData.url;
voiceChannel = channel;
} else if (args[0][0] === "test") {
Expand All @@ -76,4 +94,4 @@ export default {
message.channel.send(L._U(guildData.locale, "stream_error"));
}
},
};
};
25 changes: 24 additions & 1 deletion src/index.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import dotenv from "dotenv";
import Discord from "discord.js";
import { getVoiceConnection } from '@discordjs/voice';
import fs from "fs";
import * as Utils from "./utils/utils.mjs";
import * as GuildUtils from "./utils/guilds.mjs";
Expand Down Expand Up @@ -149,5 +150,27 @@ global.sleep = (ms) => {
});
};

client.on('voiceStateUpdate', (oldState, newState) => {
// Get the current voice connection for the guild
const connection = getVoiceConnection(oldState.guild.id);
if (!connection) return; // The bot is not connected to a voice channel

client.login(process.env.BOT_TOKEN);
// Get the ID of the voice channel the bot is connected to
const botChannelId = connection.joinConfig.channelId;

// Fetch the voice channel the bot is connected to
const botVoiceChannel = oldState.guild.channels.cache.get(botChannelId);
if (!botVoiceChannel) return; // Voice channel not found

// Filter out bots from the member list
const humanMembers = botVoiceChannel.members.filter(member => !member.user.bot);

// If no human members are left, destroy the connection
if (humanMembers.size === 0) {
connection.destroy();
console.log(`Disconnected from voice channel ${botVoiceChannel.name} as it is now empty.`);
}
});


client.login(process.env.BOT_TOKEN);