From 6fe2f1f6ee0cab782182c061952b7defb4d75920 Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Fri, 1 Sep 2023 12:25:19 +0000 Subject: [PATCH 01/10] Updated src_modules/voice_commands/index.ts --- src_modules/voice_commands/index.ts | 31 +++++++++++++++++------------ 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src_modules/voice_commands/index.ts b/src_modules/voice_commands/index.ts index ef38c7b..63e84e4 100644 --- a/src_modules/voice_commands/index.ts +++ b/src_modules/voice_commands/index.ts @@ -139,21 +139,26 @@ export default class VoiceCommandsModule extends Module { } private async onVoiceStateUpdate(oldState: VoiceState, newState: VoiceState) { - if (oldState.id !== this.bot.client!.user!.id || newState.id !== this.bot.client!.user!.id) { - return; - } - - if (oldState.channel === null && newState.channel !== null) { - // joined a channel - const listener = await this.getListener(newState.guild); - const connection = this.bot.voice.getConnection(newState.guild); - - if (connection !== undefined && listener !== undefined) { - newState.selfDeaf = false; - listener.startListening(connection); + if (oldState.id !== this.bot.client!.user!.id || newState.id !== this.bot.client!.user!.id) { + return; + } + + if (oldState.channel === null && newState.channel !== null && !newState.member.user.bot) { + // joined a channel + setTimeout(async () => { + const nonBotUsers = newState.channel.members.filter(member => !member.user.bot); + if (nonBotUsers.size > 0) { + const listener = await this.getListener(newState.guild); + const connection = this.bot.voice.getConnection(newState.guild); + + if (connection !== undefined && listener !== undefined) { + newState.selfDeaf = false; + listener.startListening(connection); + } + } + }, 5000); } } - } private onReady() { if (this.voiceStateUpdateHandler !== undefined) { From eeb4109d7a7f4b46d3e845ba1c20b63f81692cb0 Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Fri, 1 Sep 2023 13:19:57 +0000 Subject: [PATCH 02/10] Updated src_modules/voice_commands/index.ts --- src_modules/voice_commands/index.ts | 30 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src_modules/voice_commands/index.ts b/src_modules/voice_commands/index.ts index 63e84e4..9e5b496 100644 --- a/src_modules/voice_commands/index.ts +++ b/src_modules/voice_commands/index.ts @@ -139,26 +139,24 @@ export default class VoiceCommandsModule extends Module { } private async onVoiceStateUpdate(oldState: VoiceState, newState: VoiceState) { - if (oldState.id !== this.bot.client!.user!.id || newState.id !== this.bot.client!.user!.id) { - return; - } + if (oldState.id !== this.bot.client!.user!.id || newState.id !== this.bot.client!.user!.id) { + return; + } - if (oldState.channel === null && newState.channel !== null && !newState.member.user.bot) { - // joined a channel - setTimeout(async () => { - const nonBotUsers = newState.channel.members.filter(member => !member.user.bot); - if (nonBotUsers.size > 0) { - const listener = await this.getListener(newState.guild); - const connection = this.bot.voice.getConnection(newState.guild); + if (oldState.channel === null && newState.channel !== null && !newState.member.user.bot) { + // joined a channel + const nonBotUsers = newState.channel.members.filter(member => !member.user.bot); + if (nonBotUsers.size > 0) { + const listener = await this.getListener(newState.guild); + const connection = this.bot.voice.getConnection(newState.guild); - if (connection !== undefined && listener !== undefined) { - newState.selfDeaf = false; - listener.startListening(connection); - } - } - }, 5000); + if (connection !== undefined && listener !== undefined) { + newState.selfDeaf = false; + connection.join(); + } } } + } private onReady() { if (this.voiceStateUpdateHandler !== undefined) { From f248da2da67aa71343cb6d2d0501a0ec8305265a Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Fri, 1 Sep 2023 13:33:09 +0000 Subject: [PATCH 03/10] Updated src_modules/voice_commands/index.ts --- src_modules/voice_commands/index.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src_modules/voice_commands/index.ts b/src_modules/voice_commands/index.ts index 9e5b496..583331d 100644 --- a/src_modules/voice_commands/index.ts +++ b/src_modules/voice_commands/index.ts @@ -147,13 +147,18 @@ export default class VoiceCommandsModule extends Module { // joined a channel const nonBotUsers = newState.channel.members.filter(member => !member.user.bot); if (nonBotUsers.size > 0) { - const listener = await this.getListener(newState.guild); - const connection = this.bot.voice.getConnection(newState.guild); - - if (connection !== undefined && listener !== undefined) { - newState.selfDeaf = false; - connection.join(); - } + setTimeout(async () => { + const nonBotUsers = newState.channel.members.filter(member => !member.user.bot); + if (nonBotUsers.size > 0) { + const listener = await this.getListener(newState.guild); + const connection = await this.getConnection(this.bot, newState.channel); + + if (connection !== undefined && listener !== undefined) { + newState.selfDeaf = false; + connection.join(); + } + } + }, 5000); } } } From 4b68e9154e131a503ddd3163f120f5fc46b04476 Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Fri, 1 Sep 2023 19:27:34 +0000 Subject: [PATCH 04/10] Updated src_modules/voice_commands/index.ts --- src_modules/voice_commands/index.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src_modules/voice_commands/index.ts b/src_modules/voice_commands/index.ts index 583331d..266b157 100644 --- a/src_modules/voice_commands/index.ts +++ b/src_modules/voice_commands/index.ts @@ -1,9 +1,10 @@ // extcord module // requires fastest-levenshtein@^1.0.16 lru-cache@^10.0.1 -import { GatewayIntentBits, Guild, VoiceState } from "discord.js"; +import { GatewayIntentBits, Guild, VoiceState, VoiceConnection } from "discord.js"; +import { getVoiceConnection } from "@discordjs/voice"; -import { BooleanGuildConfigEntry, Bot, Logger, Module, NumberConfigEntry, SimplePhrase, StringConfigEntry } from "../.."; +import { BooleanGuildConfigEntry, Bot, Logger, Module, SimplePhrase, StringConfigEntry, NumberConfigEntry } from "../.."; import { GuildListener } from "./GuildListener"; import { VoiceBackendClient } from "./VoiceBackendClient"; @@ -162,6 +163,10 @@ export default class VoiceCommandsModule extends Module { } } } + + public getConnection(guild: Guild): VoiceConnection | undefined { + return getVoiceConnection(guild.id); + } private onReady() { if (this.voiceStateUpdateHandler !== undefined) { From 2b795bdb5ab3c644bf942c79a967fea4d4c5e548 Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Sat, 2 Sep 2023 11:22:52 +0000 Subject: [PATCH 05/10] Updated src_modules/voice_commands/index.ts --- src_modules/voice_commands/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src_modules/voice_commands/index.ts b/src_modules/voice_commands/index.ts index 266b157..fa2a882 100644 --- a/src_modules/voice_commands/index.ts +++ b/src_modules/voice_commands/index.ts @@ -164,8 +164,8 @@ export default class VoiceCommandsModule extends Module { } } - public getConnection(guild: Guild): VoiceConnection | undefined { - return getVoiceConnection(guild.id); + private async getConnection(bot: Bot, voiceChannel: VoiceChannel): Promise { + return bot.voice.getOrCreateConnection(voiceChannel); } private onReady() { From 84dfd64230d99082b245ad904ba0c2dcc5855789 Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Sat, 2 Sep 2023 11:45:24 +0000 Subject: [PATCH 06/10] Updated src_modules/voice_commands/index.ts --- src_modules/voice_commands/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src_modules/voice_commands/index.ts b/src_modules/voice_commands/index.ts index fa2a882..4d67023 100644 --- a/src_modules/voice_commands/index.ts +++ b/src_modules/voice_commands/index.ts @@ -1,8 +1,8 @@ // extcord module // requires fastest-levenshtein@^1.0.16 lru-cache@^10.0.1 -import { GatewayIntentBits, Guild, VoiceState, VoiceConnection } from "discord.js"; -import { getVoiceConnection } from "@discordjs/voice"; +import { GatewayIntentBits, Guild, VoiceState } from "discord.js"; +import { getVoiceConnection, VoiceConnection } from "@discordjs/voice"; import { BooleanGuildConfigEntry, Bot, Logger, Module, SimplePhrase, StringConfigEntry, NumberConfigEntry } from "../.."; From 224cc2e6736623a9929956b6c37b3bfc0da8bb11 Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Sat, 2 Sep 2023 11:57:03 +0000 Subject: [PATCH 07/10] Updated src_modules/voice_commands/index.ts --- src_modules/voice_commands/index.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src_modules/voice_commands/index.ts b/src_modules/voice_commands/index.ts index 4d67023..fc4f698 100644 --- a/src_modules/voice_commands/index.ts +++ b/src_modules/voice_commands/index.ts @@ -162,6 +162,17 @@ export default class VoiceCommandsModule extends Module { }, 5000); } } + + if (oldState.channel === null && newState.channel !== null) { + // Bot joined a channel + const listener = await this.getListener(newState.guild); + const connection = this.bot.voice.getConnection(newState.guild); + + if (connection !== undefined && listener !== undefined) { + newState.selfDeaf = false; + listener.startListening(connection); + } + } } private async getConnection(bot: Bot, voiceChannel: VoiceChannel): Promise { From ef11ceefb55a3a5f26685c6df26191861e6aa539 Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Sat, 2 Sep 2023 12:01:55 +0000 Subject: [PATCH 08/10] Updated src_modules/voice_commands/index.ts --- src_modules/voice_commands/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src_modules/voice_commands/index.ts b/src_modules/voice_commands/index.ts index fc4f698..36cfc12 100644 --- a/src_modules/voice_commands/index.ts +++ b/src_modules/voice_commands/index.ts @@ -1,7 +1,7 @@ // extcord module // requires fastest-levenshtein@^1.0.16 lru-cache@^10.0.1 -import { GatewayIntentBits, Guild, VoiceState } from "discord.js"; +import { GatewayIntentBits, Guild, VoiceState, VoiceChannel } from "discord.js"; import { getVoiceConnection, VoiceConnection } from "@discordjs/voice"; import { BooleanGuildConfigEntry, Bot, Logger, Module, SimplePhrase, StringConfigEntry, NumberConfigEntry } from "../.."; From 00954c20f36004ee854932b9505ded3dd84dcc22 Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Sat, 2 Sep 2023 12:02:03 +0000 Subject: [PATCH 09/10] Updated src_modules/voice_commands/index.ts --- src_modules/voice_commands/index.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src_modules/voice_commands/index.ts b/src_modules/voice_commands/index.ts index 36cfc12..83a71d4 100644 --- a/src_modules/voice_commands/index.ts +++ b/src_modules/voice_commands/index.ts @@ -1,7 +1,7 @@ // extcord module // requires fastest-levenshtein@^1.0.16 lru-cache@^10.0.1 -import { GatewayIntentBits, Guild, VoiceState, VoiceChannel } from "discord.js"; +import { GatewayIntentBits, Guild, VoiceState } from "discord.js"; import { getVoiceConnection, VoiceConnection } from "@discordjs/voice"; import { BooleanGuildConfigEntry, Bot, Logger, Module, SimplePhrase, StringConfigEntry, NumberConfigEntry } from "../.."; @@ -140,10 +140,6 @@ export default class VoiceCommandsModule extends Module { } private async onVoiceStateUpdate(oldState: VoiceState, newState: VoiceState) { - if (oldState.id !== this.bot.client!.user!.id || newState.id !== this.bot.client!.user!.id) { - return; - } - if (oldState.channel === null && newState.channel !== null && !newState.member.user.bot) { // joined a channel const nonBotUsers = newState.channel.members.filter(member => !member.user.bot); @@ -163,6 +159,10 @@ export default class VoiceCommandsModule extends Module { } } + if (oldState.id !== this.bot.client!.user!.id || newState.id !== this.bot.client!.user!.id) { + return; + } + if (oldState.channel === null && newState.channel !== null) { // Bot joined a channel const listener = await this.getListener(newState.guild); From 175e7e5791432d6ec7187f4b763227bf607ee4e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lassi=20S=C3=A4ike?= Date: Sat, 2 Sep 2023 15:23:13 +0300 Subject: [PATCH 10/10] Fix implementation --- src_modules/voice_commands/index.ts | 34 +++++++++++++---------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/src_modules/voice_commands/index.ts b/src_modules/voice_commands/index.ts index 83a71d4..fa8cd72 100644 --- a/src_modules/voice_commands/index.ts +++ b/src_modules/voice_commands/index.ts @@ -1,8 +1,8 @@ // extcord module // requires fastest-levenshtein@^1.0.16 lru-cache@^10.0.1 -import { GatewayIntentBits, Guild, VoiceState } from "discord.js"; -import { getVoiceConnection, VoiceConnection } from "@discordjs/voice"; +import { GatewayIntentBits, Guild, VoiceState, VoiceChannel } from "discord.js"; +import { VoiceConnection } from "@discordjs/voice"; import { BooleanGuildConfigEntry, Bot, Logger, Module, SimplePhrase, StringConfigEntry, NumberConfigEntry } from "../.."; @@ -140,41 +140,37 @@ export default class VoiceCommandsModule extends Module { } private async onVoiceStateUpdate(oldState: VoiceState, newState: VoiceState) { - if (oldState.channel === null && newState.channel !== null && !newState.member.user.bot) { - // joined a channel - const nonBotUsers = newState.channel.members.filter(member => !member.user.bot); - if (nonBotUsers.size > 0) { + if (oldState.channel === null && newState.channel !== null && newState.channel instanceof VoiceChannel && !newState.member?.user.bot) { + const channel = newState.channel; + + // Someone joined a voice channel + const nonBotUsers = channel.members.filter(member => !member.user.bot); + if (nonBotUsers.size > 0 && this.bot.voice.getConnection(channel.guild) === undefined) { setTimeout(async () => { - const nonBotUsers = newState.channel.members.filter(member => !member.user.bot); - if (nonBotUsers.size > 0) { - const listener = await this.getListener(newState.guild); - const connection = await this.getConnection(this.bot, newState.channel); - - if (connection !== undefined && listener !== undefined) { - newState.selfDeaf = false; - connection.join(); - } + const nonBotUsers = channel.members.filter(member => !member.user.bot); + if (nonBotUsers.size > 0 && this.bot.voice.getConnection(channel.guild) === undefined) { + await this.getConnection(this.bot, channel); } }, 5000); } } - + if (oldState.id !== this.bot.client!.user!.id || newState.id !== this.bot.client!.user!.id) { return; } - + if (oldState.channel === null && newState.channel !== null) { // Bot joined a channel const listener = await this.getListener(newState.guild); const connection = this.bot.voice.getConnection(newState.guild); - + if (connection !== undefined && listener !== undefined) { newState.selfDeaf = false; listener.startListening(connection); } } } - + private async getConnection(bot: Bot, voiceChannel: VoiceChannel): Promise { return bot.voice.getOrCreateConnection(voiceChannel); }