From 980e09cc73d352f88fd75250a0b2e36bade76f36 Mon Sep 17 00:00:00 2001 From: Den Stroebel Date: Fri, 22 Nov 2024 11:29:23 +0000 Subject: [PATCH 1/4] Add disconnect when empty --- src/index.mjs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/index.mjs b/src/index.mjs index 5481e30..64fcc31 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -1,5 +1,6 @@ import dotenv from "dotenv"; -import Discord from "discord.js"; +import { Client, Intents, 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"; @@ -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); \ No newline at end of file + // 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); From cc519b878d2abe128d4a16e7b3de55caa7c33d93 Mon Sep 17 00:00:00 2001 From: Den Stroebel Date: Fri, 22 Nov 2024 11:32:42 +0000 Subject: [PATCH 2/4] Reject joining when empty --- src/commands/join.mjs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/commands/join.mjs b/src/commands/join.mjs index b4e505e..a435029 100644 --- a/src/commands/join.mjs +++ b/src/commands/join.mjs @@ -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); @@ -76,4 +85,4 @@ export default { message.channel.send(L._U(guildData.locale, "stream_error")); } }, -}; \ No newline at end of file +}; From 079afe5375e59582c2aae792d394da9d3fffa9bf Mon Sep 17 00:00:00 2001 From: Den Stroebel Date: Fri, 22 Nov 2024 11:33:41 +0000 Subject: [PATCH 3/4] Reject joining home --- src/commands/join.mjs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/commands/join.mjs b/src/commands/join.mjs index a435029..3131e06 100644 --- a/src/commands/join.mjs +++ b/src/commands/join.mjs @@ -60,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") { From c69a7193ef55b58783bb9dd1dbd67a66a8690009 Mon Sep 17 00:00:00 2001 From: Den Stroebel Date: Fri, 22 Nov 2024 11:38:29 +0000 Subject: [PATCH 4/4] Remove incorrect import --- src/index.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.mjs b/src/index.mjs index 64fcc31..ac4eab8 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -1,5 +1,5 @@ import dotenv from "dotenv"; -import { Client, Intents, Discord } from "discord.js"; +import Discord from "discord.js"; import { getVoiceConnection } from '@discordjs/voice'; import fs from "fs"; import * as Utils from "./utils/utils.mjs";