Skip to content

Commit

Permalink
allow bot to join the specific voice channel
Browse files Browse the repository at this point in the history
  • Loading branch information
tcm390 committed Nov 27, 2024
1 parent e2514dc commit 9aa244c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Discord Configuration
DISCORD_APPLICATION_ID=
DISCORD_API_TOKEN= # Bot token
DISCORD_VOICE_CHANNEL_ID= # The ID of the voice channel the bot should join (optional)

# AI Model API Keys
OPENAI_API_KEY= # OpenAI API key, starting with sk-
Expand Down
48 changes: 34 additions & 14 deletions packages/client-discord/src/voice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -889,24 +889,44 @@ export class VoiceManager extends EventEmitter {
}

async scanGuild(guild: Guild) {
const channels = (await guild.channels.fetch()).filter(
(channel) => channel?.type == ChannelType.GuildVoice
);
let chosenChannel: BaseGuildVoiceChannel | null = null;

for (const [, channel] of channels) {
const voiceChannel = channel as BaseGuildVoiceChannel;
if (
voiceChannel.members.size > 0 &&
(chosenChannel === null ||
voiceChannel.members.size > chosenChannel.members.size)
) {
chosenChannel = voiceChannel;
try {
const channelId = this.runtime.getSetting(
"DISCORD_VOICE_CHANNEL_ID"
) as string;
if (channelId) {
const channel = await guild.channels.fetch(channelId);
if (channel?.isVoiceBased()) {
chosenChannel = channel as BaseGuildVoiceChannel;
}
}
}

if (chosenChannel != null) {
this.joinChannel(chosenChannel);
if (!chosenChannel) {
const channels = (await guild.channels.fetch()).filter(
(channel) => channel?.type == ChannelType.GuildVoice
);
for (const [, channel] of channels) {
const voiceChannel = channel as BaseGuildVoiceChannel;
if (
voiceChannel.members.size > 0 &&
(chosenChannel === null ||
voiceChannel.members.size >
chosenChannel.members.size)
) {
chosenChannel = voiceChannel;
}
}
}

if (chosenChannel) {
console.log(`Joining channel: ${chosenChannel.name}`);
await this.joinChannel(chosenChannel);
} else {
console.warn("No suitable voice channel found to join.");
}
} catch (error) {
console.error("Error selecting or joining a voice channel:", error);
}
}

Expand Down

0 comments on commit 9aa244c

Please sign in to comment.