Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: The bot is by default deafened and we don't want that #437

Merged
Merged
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
28 changes: 16 additions & 12 deletions packages/client-discord/src/actions/joinvoice.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// @ts-nocheck
// src/actions/joinVoice
import { joinVoiceChannel } from "@discordjs/voice";
import {
Action,
ActionExample, composeContext, IAgentRuntime,
Memory,
State
} from "@ai16z/eliza";
import {
Channel,
ChannelType,
Expand All @@ -9,14 +14,6 @@ import {
Guild,
GuildMember,
} from "discord.js";
import { composeContext } from "@ai16z/eliza";
import {
Action,
ActionExample,
IAgentRuntime,
Memory,
State,
} from "@ai16z/eliza";

export default {
name: "JOIN_VOICE",
Expand Down Expand Up @@ -115,8 +112,15 @@ export default {
);
});

if (!state.voiceManager) {
state.voiceManager = new VoiceManager({
client: state.discordClient,
runtime: runtime,
});
}

if (targetChannel) {
joinVoiceChannel({
state.voiceManager.joinVoiceChannel({
channelId: targetChannel.id,
guildId: (discordMessage as DiscordMessage).guild?.id as string,
adapterCreator: (client.guilds.cache.get(id) as Guild)
Expand All @@ -127,7 +131,7 @@ export default {
const member = (discordMessage as DiscordMessage)
.member as GuildMember;
if (member?.voice?.channel) {
joinVoiceChannel({
state.voiceManager.joinVoiceChannel({
channelId: member.voice.channel.id,
guildId: (discordMessage as DiscordMessage).guild
?.id as string,
Expand Down Expand Up @@ -197,7 +201,7 @@ You should only respond with the name of the voice channel or none, no commentar
});

if (targetChannel) {
joinVoiceChannel({
state.voiceManager.joinVoiceChannel({
channelId: targetChannel.id,
guildId: (discordMessage as DiscordMessage).guild
?.id as string,
Expand Down
43 changes: 26 additions & 17 deletions packages/client-discord/src/voice.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
import {
Content,
HandlerCallback,
IAgentRuntime,
ISpeechService,
ITranscriptionService,
Memory,
ModelClass,
ServiceType,
State,
UUID, composeContext, elizaLogger, embeddingZeroVector, generateMessageResponse, messageCompletionFooter, stringToUuid
} from "@ai16z/eliza";
import {
AudioReceiveStream,
NoSubscriberBehavior,
Expand All @@ -20,23 +32,6 @@ import {
import EventEmitter from "events";
import prism from "prism-media";
import { Readable, pipeline } from "stream";
import { composeContext, elizaLogger } from "@ai16z/eliza";
import { generateMessageResponse } from "@ai16z/eliza";
import { embeddingZeroVector } from "@ai16z/eliza";
import {
Content,
HandlerCallback,
IAgentRuntime,
ISpeechService,
ITranscriptionService,
Memory,
ModelClass,
ServiceType,
State,
UUID,
} from "@ai16z/eliza";
import { stringToUuid } from "@ai16z/eliza";
import { messageCompletionFooter } from "@ai16z/eliza";
import { DiscordClient } from "./index.ts";

export function getWavHeader(
Expand Down Expand Up @@ -222,6 +217,9 @@ export class VoiceManager extends EventEmitter {
if (oldConnection) {
try {
oldConnection.destroy();
// Remove all associated streams and monitors
this.streams.clear();
this.activeMonitors.clear();
} catch (error) {
console.error("Error leaving voice channel:", error);
}
Expand All @@ -234,12 +232,23 @@ export class VoiceManager extends EventEmitter {
selfMute: false,
});

// Explicitly undeafen and unmute the bot
const me = channel.guild.members.me;
if (me?.voice) {
await me.voice.setDeaf(false);
await me.voice.setMute(false);
}

for (const [, member] of channel.members) {
if (!member.user.bot) {
this.monitorMember(member, channel);
}
}

connection.on('error', (error) => {
console.error('Voice connection error:', error);
});

connection.receiver.speaking.on("start", (userId: string) => {
const user = channel.members.get(userId);
if (!user?.user.bot) {
Expand Down