Skip to content

Commit

Permalink
Merge pull request #437 from martincik/fix/undefean-the-bot-and-use-v…
Browse files Browse the repository at this point in the history
…oice-manager

fix: The bot is by default deafened and we don't want that
  • Loading branch information
lalalune authored Nov 20, 2024
2 parents 71371e1 + bc8f3f8 commit b31f600
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
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

0 comments on commit b31f600

Please sign in to comment.