kimaki 0.4.21 → 0.4.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/dist/channel-management.js +92 -0
  2. package/dist/cli.js +10 -2
  3. package/dist/database.js +130 -0
  4. package/dist/discord-bot.js +381 -0
  5. package/dist/discord-utils.js +151 -0
  6. package/dist/discordBot.js +60 -31
  7. package/dist/escape-backticks.test.js +1 -1
  8. package/dist/fork.js +163 -0
  9. package/dist/format-tables.js +93 -0
  10. package/dist/format-tables.test.js +418 -0
  11. package/dist/interaction-handler.js +750 -0
  12. package/dist/markdown.js +3 -3
  13. package/dist/message-formatting.js +188 -0
  14. package/dist/model-command.js +293 -0
  15. package/dist/opencode.js +135 -0
  16. package/dist/session-handler.js +467 -0
  17. package/dist/system-message.js +92 -0
  18. package/dist/tools.js +3 -5
  19. package/dist/utils.js +31 -0
  20. package/dist/voice-handler.js +528 -0
  21. package/dist/voice.js +257 -35
  22. package/package.json +3 -2
  23. package/src/channel-management.ts +145 -0
  24. package/src/cli.ts +10 -2
  25. package/src/database.ts +155 -0
  26. package/src/discord-bot.ts +506 -0
  27. package/src/discord-utils.ts +208 -0
  28. package/src/escape-backticks.test.ts +1 -1
  29. package/src/fork.ts +224 -0
  30. package/src/format-tables.test.ts +440 -0
  31. package/src/format-tables.ts +106 -0
  32. package/src/interaction-handler.ts +1000 -0
  33. package/src/markdown.ts +3 -3
  34. package/src/message-formatting.ts +227 -0
  35. package/src/model-command.ts +380 -0
  36. package/src/opencode.ts +180 -0
  37. package/src/session-handler.ts +601 -0
  38. package/src/system-message.ts +92 -0
  39. package/src/tools.ts +3 -5
  40. package/src/utils.ts +37 -0
  41. package/src/voice-handler.ts +745 -0
  42. package/src/voice.ts +354 -36
  43. package/src/discordBot.ts +0 -3643
@@ -0,0 +1,745 @@
1
+ import {
2
+ VoiceConnectionStatus,
3
+ EndBehaviorType,
4
+ joinVoiceChannel,
5
+ entersState,
6
+ type VoiceConnection,
7
+ } from '@discordjs/voice'
8
+ import { exec } from 'node:child_process'
9
+ import fs, { createWriteStream } from 'node:fs'
10
+ import { mkdir } from 'node:fs/promises'
11
+ import path from 'node:path'
12
+ import { promisify } from 'node:util'
13
+ import { Transform, type TransformCallback } from 'node:stream'
14
+ import * as prism from 'prism-media'
15
+ import dedent from 'string-dedent'
16
+ import {
17
+ PermissionsBitField,
18
+ Events,
19
+ type Client,
20
+ type Message,
21
+ type ThreadChannel,
22
+ type VoiceChannel,
23
+ type VoiceState,
24
+ } from 'discord.js'
25
+ import { createGenAIWorker, type GenAIWorker } from './genai-worker-wrapper.js'
26
+ import { getDatabase } from './database.js'
27
+ import { sendThreadMessage, escapeDiscordFormatting, SILENT_MESSAGE_FLAGS } from './discord-utils.js'
28
+ import { transcribeAudio } from './voice.js'
29
+ import { createLogger } from './logger.js'
30
+
31
+ const voiceLogger = createLogger('VOICE')
32
+
33
+ export type VoiceConnectionData = {
34
+ connection: VoiceConnection
35
+ genAiWorker?: GenAIWorker
36
+ userAudioStream?: fs.WriteStream
37
+ }
38
+
39
+ export const voiceConnections = new Map<string, VoiceConnectionData>()
40
+
41
+ export function convertToMono16k(buffer: Buffer): Buffer {
42
+ const inputSampleRate = 48000
43
+ const outputSampleRate = 16000
44
+ const ratio = inputSampleRate / outputSampleRate
45
+ const inputChannels = 2
46
+ const bytesPerSample = 2
47
+
48
+ const inputSamples = buffer.length / (bytesPerSample * inputChannels)
49
+ const outputSamples = Math.floor(inputSamples / ratio)
50
+ const outputBuffer = Buffer.alloc(outputSamples * bytesPerSample)
51
+
52
+ for (let i = 0; i < outputSamples; i++) {
53
+ const inputIndex = Math.floor(i * ratio) * inputChannels * bytesPerSample
54
+
55
+ if (inputIndex + 3 < buffer.length) {
56
+ const leftSample = buffer.readInt16LE(inputIndex)
57
+ const rightSample = buffer.readInt16LE(inputIndex + 2)
58
+ const monoSample = Math.round((leftSample + rightSample) / 2)
59
+
60
+ outputBuffer.writeInt16LE(monoSample, i * bytesPerSample)
61
+ }
62
+ }
63
+
64
+ return outputBuffer
65
+ }
66
+
67
+ export async function createUserAudioLogStream(
68
+ guildId: string,
69
+ channelId: string,
70
+ ): Promise<fs.WriteStream | undefined> {
71
+ if (!process.env.DEBUG) return undefined
72
+
73
+ const timestamp = new Date().toISOString().replace(/[:.]/g, '-')
74
+ const audioDir = path.join(
75
+ process.cwd(),
76
+ 'discord-audio-logs',
77
+ guildId,
78
+ channelId,
79
+ )
80
+
81
+ try {
82
+ await mkdir(audioDir, { recursive: true })
83
+
84
+ const inputFileName = `user_${timestamp}.16.pcm`
85
+ const inputFilePath = path.join(audioDir, inputFileName)
86
+ const inputAudioStream = createWriteStream(inputFilePath)
87
+ voiceLogger.log(`Created user audio log: ${inputFilePath}`)
88
+
89
+ return inputAudioStream
90
+ } catch (error) {
91
+ voiceLogger.error('Failed to create audio log directory:', error)
92
+ return undefined
93
+ }
94
+ }
95
+
96
+ export function frameMono16khz(): Transform {
97
+ const FRAME_BYTES =
98
+ (100 * 16_000 * 1 * 2) / 1000
99
+ let stash: Buffer = Buffer.alloc(0)
100
+ let offset = 0
101
+
102
+ return new Transform({
103
+ readableObjectMode: false,
104
+ writableObjectMode: false,
105
+
106
+ transform(chunk: Buffer, _enc: BufferEncoding, cb: TransformCallback) {
107
+ if (offset > 0) {
108
+ stash = stash.subarray(offset)
109
+ offset = 0
110
+ }
111
+
112
+ stash = stash.length ? Buffer.concat([stash, chunk]) : chunk
113
+
114
+ while (stash.length - offset >= FRAME_BYTES) {
115
+ this.push(stash.subarray(offset, offset + FRAME_BYTES))
116
+ offset += FRAME_BYTES
117
+ }
118
+
119
+ if (offset === stash.length) {
120
+ stash = Buffer.alloc(0)
121
+ offset = 0
122
+ }
123
+
124
+ cb()
125
+ },
126
+
127
+ flush(cb: TransformCallback) {
128
+ stash = Buffer.alloc(0)
129
+ offset = 0
130
+ cb()
131
+ },
132
+ })
133
+ }
134
+
135
+ export async function setupVoiceHandling({
136
+ connection,
137
+ guildId,
138
+ channelId,
139
+ appId,
140
+ discordClient,
141
+ }: {
142
+ connection: VoiceConnection
143
+ guildId: string
144
+ channelId: string
145
+ appId: string
146
+ discordClient: Client
147
+ }) {
148
+ voiceLogger.log(
149
+ `Setting up voice handling for guild ${guildId}, channel ${channelId}`,
150
+ )
151
+
152
+ const channelDirRow = getDatabase()
153
+ .prepare(
154
+ 'SELECT directory FROM channel_directories WHERE channel_id = ? AND channel_type = ?',
155
+ )
156
+ .get(channelId, 'voice') as { directory: string } | undefined
157
+
158
+ if (!channelDirRow) {
159
+ voiceLogger.log(
160
+ `Voice channel ${channelId} has no associated directory, skipping setup`,
161
+ )
162
+ return
163
+ }
164
+
165
+ const directory = channelDirRow.directory
166
+ voiceLogger.log(`Found directory for voice channel: ${directory}`)
167
+
168
+ const voiceData = voiceConnections.get(guildId)
169
+ if (!voiceData) {
170
+ voiceLogger.error(`No voice data found for guild ${guildId}`)
171
+ return
172
+ }
173
+
174
+ voiceData.userAudioStream = await createUserAudioLogStream(guildId, channelId)
175
+
176
+ const apiKeys = getDatabase()
177
+ .prepare('SELECT gemini_api_key FROM bot_api_keys WHERE app_id = ?')
178
+ .get(appId) as { gemini_api_key: string | null } | undefined
179
+
180
+ const genAiWorker = await createGenAIWorker({
181
+ directory,
182
+ guildId,
183
+ channelId,
184
+ appId,
185
+ geminiApiKey: apiKeys?.gemini_api_key,
186
+ systemMessage: dedent`
187
+ You are Kimaki, an AI similar to Jarvis: you help your user (an engineer) controlling his coding agent, just like Jarvis controls Ironman armor and machines. Speak fast.
188
+
189
+ You should talk like Jarvis, British accent, satirical, joking and calm. Be short and concise. Speak fast.
190
+
191
+ After tool calls give a super short summary of the assistant message, you should say what the assistant message writes.
192
+
193
+ Before starting a new session ask for confirmation if it is not clear if the user finished describing it. ask "message ready, send?"
194
+
195
+ NEVER repeat the whole tool call parameters or message.
196
+
197
+ Your job is to manage many opencode agent chat instances. Opencode is the agent used to write the code, it is similar to Claude Code.
198
+
199
+ For everything the user asks it is implicit that the user is asking for you to proxy the requests to opencode sessions.
200
+
201
+ You can
202
+ - start new chats on a given project
203
+ - read the chats to report progress to the user
204
+ - submit messages to the chat
205
+ - list files for a given projects, so you can translate imprecise user prompts to precise messages that mention filename paths using @
206
+
207
+ Common patterns
208
+ - to get the last session use the listChats tool
209
+ - when user asks you to do something you submit a new session to do it. it's implicit that you proxy requests to the agents chat!
210
+ - when you submit a session assume the session will take a minute or 2 to complete the task
211
+
212
+ Rules
213
+ - never spell files by mentioning dots, letters, etc. instead give a brief description of the filename
214
+ - NEVER spell hashes or IDs
215
+ - never read session ids or other ids
216
+
217
+ Your voice is calm and monotone, NEVER excited and goofy. But you speak without jargon or bs and do veiled short jokes.
218
+ You speak like you knew something other don't. You are cool and cold.
219
+ `,
220
+ onAssistantOpusPacket(packet) {
221
+ if (connection.state.status !== VoiceConnectionStatus.Ready) {
222
+ voiceLogger.log('Skipping packet: connection not ready')
223
+ return
224
+ }
225
+
226
+ try {
227
+ connection.setSpeaking(true)
228
+ connection.playOpusPacket(Buffer.from(packet))
229
+ } catch (error) {
230
+ voiceLogger.error('Error sending packet:', error)
231
+ }
232
+ },
233
+ onAssistantStartSpeaking() {
234
+ voiceLogger.log('Assistant started speaking')
235
+ connection.setSpeaking(true)
236
+ },
237
+ onAssistantStopSpeaking() {
238
+ voiceLogger.log('Assistant stopped speaking (natural finish)')
239
+ connection.setSpeaking(false)
240
+ },
241
+ onAssistantInterruptSpeaking() {
242
+ voiceLogger.log('Assistant interrupted while speaking')
243
+ genAiWorker.interrupt()
244
+ connection.setSpeaking(false)
245
+ },
246
+ onToolCallCompleted(params) {
247
+ const text = params.error
248
+ ? `<systemMessage>\nThe coding agent encountered an error while processing session ${params.sessionId}: ${params.error?.message || String(params.error)}\n</systemMessage>`
249
+ : `<systemMessage>\nThe coding agent finished working on session ${params.sessionId}\n\nHere's what the assistant wrote:\n${params.markdown}\n</systemMessage>`
250
+
251
+ genAiWorker.sendTextInput(text)
252
+ },
253
+ async onError(error) {
254
+ voiceLogger.error('GenAI worker error:', error)
255
+ const textChannelRow = getDatabase()
256
+ .prepare(
257
+ `SELECT cd2.channel_id FROM channel_directories cd1
258
+ JOIN channel_directories cd2 ON cd1.directory = cd2.directory
259
+ WHERE cd1.channel_id = ? AND cd1.channel_type = 'voice' AND cd2.channel_type = 'text'`,
260
+ )
261
+ .get(channelId) as { channel_id: string } | undefined
262
+
263
+ if (textChannelRow) {
264
+ try {
265
+ const textChannel = await discordClient.channels.fetch(
266
+ textChannelRow.channel_id,
267
+ )
268
+ if (textChannel?.isTextBased() && 'send' in textChannel) {
269
+ await textChannel.send({ content: `⚠️ Voice session error: ${error}`, flags: SILENT_MESSAGE_FLAGS })
270
+ }
271
+ } catch (e) {
272
+ voiceLogger.error('Failed to send error to text channel:', e)
273
+ }
274
+ }
275
+ },
276
+ })
277
+
278
+ if (voiceData.genAiWorker) {
279
+ voiceLogger.log('Stopping existing GenAI worker before creating new one')
280
+ await voiceData.genAiWorker.stop()
281
+ }
282
+
283
+ genAiWorker.sendTextInput(
284
+ `<systemMessage>\nsay "Hello boss, how we doing today?"\n</systemMessage>`,
285
+ )
286
+
287
+ voiceData.genAiWorker = genAiWorker
288
+
289
+ const receiver = connection.receiver
290
+
291
+ receiver.speaking.removeAllListeners('start')
292
+
293
+ let speakingSessionCount = 0
294
+
295
+ receiver.speaking.on('start', (userId) => {
296
+ voiceLogger.log(`User ${userId} started speaking`)
297
+
298
+ speakingSessionCount++
299
+ const currentSessionCount = speakingSessionCount
300
+ voiceLogger.log(`Speaking session ${currentSessionCount} started`)
301
+
302
+ const audioStream = receiver.subscribe(userId, {
303
+ end: { behavior: EndBehaviorType.AfterSilence, duration: 500 },
304
+ })
305
+
306
+ const decoder = new prism.opus.Decoder({
307
+ rate: 48000,
308
+ channels: 2,
309
+ frameSize: 960,
310
+ })
311
+
312
+ decoder.on('error', (error) => {
313
+ voiceLogger.error(`Opus decoder error for user ${userId}:`, error)
314
+ })
315
+
316
+ const downsampleTransform = new Transform({
317
+ transform(chunk: Buffer, _encoding, callback) {
318
+ try {
319
+ const downsampled = convertToMono16k(chunk)
320
+ callback(null, downsampled)
321
+ } catch (error) {
322
+ callback(error as Error)
323
+ }
324
+ },
325
+ })
326
+
327
+ const framer = frameMono16khz()
328
+
329
+ const pipeline = audioStream
330
+ .pipe(decoder)
331
+ .pipe(downsampleTransform)
332
+ .pipe(framer)
333
+
334
+ pipeline
335
+ .on('data', (frame: Buffer) => {
336
+ if (currentSessionCount !== speakingSessionCount) {
337
+ return
338
+ }
339
+
340
+ if (!voiceData.genAiWorker) {
341
+ voiceLogger.warn(
342
+ `[VOICE] Received audio frame but no GenAI worker active for guild ${guildId}`,
343
+ )
344
+ return
345
+ }
346
+
347
+ voiceData.userAudioStream?.write(frame)
348
+
349
+ voiceData.genAiWorker.sendRealtimeInput({
350
+ audio: {
351
+ mimeType: 'audio/pcm;rate=16000',
352
+ data: frame.toString('base64'),
353
+ },
354
+ })
355
+ })
356
+ .on('end', () => {
357
+ if (currentSessionCount === speakingSessionCount) {
358
+ voiceLogger.log(
359
+ `User ${userId} stopped speaking (session ${currentSessionCount})`,
360
+ )
361
+ voiceData.genAiWorker?.sendRealtimeInput({
362
+ audioStreamEnd: true,
363
+ })
364
+ } else {
365
+ voiceLogger.log(
366
+ `User ${userId} stopped speaking (session ${currentSessionCount}), but skipping audioStreamEnd because newer session ${speakingSessionCount} exists`,
367
+ )
368
+ }
369
+ })
370
+ .on('error', (error) => {
371
+ voiceLogger.error(`Pipeline error for user ${userId}:`, error)
372
+ })
373
+
374
+ audioStream.on('error', (error) => {
375
+ voiceLogger.error(`Audio stream error for user ${userId}:`, error)
376
+ })
377
+
378
+ downsampleTransform.on('error', (error) => {
379
+ voiceLogger.error(`Downsample transform error for user ${userId}:`, error)
380
+ })
381
+
382
+ framer.on('error', (error) => {
383
+ voiceLogger.error(`Framer error for user ${userId}:`, error)
384
+ })
385
+ })
386
+ }
387
+
388
+ export async function cleanupVoiceConnection(guildId: string) {
389
+ const voiceData = voiceConnections.get(guildId)
390
+ if (!voiceData) return
391
+
392
+ voiceLogger.log(`Starting cleanup for guild ${guildId}`)
393
+
394
+ try {
395
+ if (voiceData.genAiWorker) {
396
+ voiceLogger.log(`Stopping GenAI worker...`)
397
+ await voiceData.genAiWorker.stop()
398
+ voiceLogger.log(`GenAI worker stopped`)
399
+ }
400
+
401
+ if (voiceData.userAudioStream) {
402
+ voiceLogger.log(`Closing user audio stream...`)
403
+ await new Promise<void>((resolve) => {
404
+ voiceData.userAudioStream!.end(() => {
405
+ voiceLogger.log('User audio stream closed')
406
+ resolve()
407
+ })
408
+ setTimeout(resolve, 2000)
409
+ })
410
+ }
411
+
412
+ if (
413
+ voiceData.connection.state.status !== VoiceConnectionStatus.Destroyed
414
+ ) {
415
+ voiceLogger.log(`Destroying voice connection...`)
416
+ voiceData.connection.destroy()
417
+ }
418
+
419
+ voiceConnections.delete(guildId)
420
+ voiceLogger.log(`Cleanup complete for guild ${guildId}`)
421
+ } catch (error) {
422
+ voiceLogger.error(`Error during cleanup for guild ${guildId}:`, error)
423
+ voiceConnections.delete(guildId)
424
+ }
425
+ }
426
+
427
+ export async function processVoiceAttachment({
428
+ message,
429
+ thread,
430
+ projectDirectory,
431
+ isNewThread = false,
432
+ appId,
433
+ sessionMessages,
434
+ }: {
435
+ message: Message
436
+ thread: ThreadChannel
437
+ projectDirectory?: string
438
+ isNewThread?: boolean
439
+ appId?: string
440
+ sessionMessages?: string
441
+ }): Promise<string | null> {
442
+ const audioAttachment = Array.from(message.attachments.values()).find(
443
+ (attachment) => attachment.contentType?.startsWith('audio/'),
444
+ )
445
+
446
+ if (!audioAttachment) return null
447
+
448
+ voiceLogger.log(
449
+ `Detected audio attachment: ${audioAttachment.name} (${audioAttachment.contentType})`,
450
+ )
451
+
452
+ await sendThreadMessage(thread, '🎤 Transcribing voice message...')
453
+
454
+ const audioResponse = await fetch(audioAttachment.url)
455
+ const audioBuffer = Buffer.from(await audioResponse.arrayBuffer())
456
+
457
+ voiceLogger.log(`Downloaded ${audioBuffer.length} bytes, transcribing...`)
458
+
459
+ let transcriptionPrompt = 'Discord voice message transcription'
460
+
461
+ if (projectDirectory) {
462
+ try {
463
+ voiceLogger.log(`Getting project file tree from ${projectDirectory}`)
464
+ const execAsync = promisify(exec)
465
+ const { stdout } = await execAsync('git ls-files | tree --fromfile -a', {
466
+ cwd: projectDirectory,
467
+ })
468
+ const result = stdout
469
+
470
+ if (result) {
471
+ transcriptionPrompt = `Discord voice message transcription. Project file structure:\n${result}\n\nPlease transcribe file names and paths accurately based on this context.`
472
+ voiceLogger.log(`Added project context to transcription prompt`)
473
+ }
474
+ } catch (e) {
475
+ voiceLogger.log(`Could not get project tree:`, e)
476
+ }
477
+ }
478
+
479
+ let geminiApiKey: string | undefined
480
+ if (appId) {
481
+ const apiKeys = getDatabase()
482
+ .prepare('SELECT gemini_api_key FROM bot_api_keys WHERE app_id = ?')
483
+ .get(appId) as { gemini_api_key: string | null } | undefined
484
+
485
+ if (apiKeys?.gemini_api_key) {
486
+ geminiApiKey = apiKeys.gemini_api_key
487
+ }
488
+ }
489
+
490
+ const transcription = await transcribeAudio({
491
+ audio: audioBuffer,
492
+ prompt: transcriptionPrompt,
493
+ geminiApiKey,
494
+ directory: projectDirectory,
495
+ sessionMessages,
496
+ })
497
+
498
+ voiceLogger.log(
499
+ `Transcription successful: "${transcription.slice(0, 50)}${transcription.length > 50 ? '...' : ''}"`,
500
+ )
501
+
502
+ if (isNewThread) {
503
+ const threadName = transcription.replace(/\s+/g, ' ').trim().slice(0, 80)
504
+ if (threadName) {
505
+ try {
506
+ await Promise.race([
507
+ thread.setName(threadName),
508
+ new Promise((resolve) => setTimeout(resolve, 2000)),
509
+ ])
510
+ voiceLogger.log(`Updated thread name to: "${threadName}"`)
511
+ } catch (e) {
512
+ voiceLogger.log(`Could not update thread name:`, e)
513
+ }
514
+ }
515
+ }
516
+
517
+ await sendThreadMessage(
518
+ thread,
519
+ `📝 **Transcribed message:** ${escapeDiscordFormatting(transcription)}`,
520
+ )
521
+ return transcription
522
+ }
523
+
524
+ export function registerVoiceStateHandler({
525
+ discordClient,
526
+ appId,
527
+ }: {
528
+ discordClient: Client
529
+ appId: string
530
+ }) {
531
+ discordClient.on(Events.VoiceStateUpdate, async (oldState: VoiceState, newState: VoiceState) => {
532
+ try {
533
+ const member = newState.member || oldState.member
534
+ if (!member) return
535
+
536
+ const guild = newState.guild || oldState.guild
537
+ const isOwner = member.id === guild.ownerId
538
+ const isAdmin = member.permissions.has(
539
+ PermissionsBitField.Flags.Administrator,
540
+ )
541
+ const canManageServer = member.permissions.has(
542
+ PermissionsBitField.Flags.ManageGuild,
543
+ )
544
+ const hasKimakiRole = member.roles.cache.some(
545
+ (role) => role.name.toLowerCase() === 'kimaki',
546
+ )
547
+
548
+ if (!isOwner && !isAdmin && !canManageServer && !hasKimakiRole) {
549
+ return
550
+ }
551
+
552
+ if (oldState.channelId !== null && newState.channelId === null) {
553
+ voiceLogger.log(
554
+ `Admin user ${member.user.tag} left voice channel: ${oldState.channel?.name}`,
555
+ )
556
+
557
+ const guildId = guild.id
558
+ const voiceData = voiceConnections.get(guildId)
559
+
560
+ if (
561
+ voiceData &&
562
+ voiceData.connection.joinConfig.channelId === oldState.channelId
563
+ ) {
564
+ const voiceChannel = oldState.channel as VoiceChannel
565
+ if (!voiceChannel) return
566
+
567
+ const hasOtherAdmins = voiceChannel.members.some((m) => {
568
+ if (m.id === member.id || m.user.bot) return false
569
+ return (
570
+ m.id === guild.ownerId ||
571
+ m.permissions.has(PermissionsBitField.Flags.Administrator) ||
572
+ m.permissions.has(PermissionsBitField.Flags.ManageGuild) ||
573
+ m.roles.cache.some((role) => role.name.toLowerCase() === 'kimaki')
574
+ )
575
+ })
576
+
577
+ if (!hasOtherAdmins) {
578
+ voiceLogger.log(
579
+ `No other admins in channel, bot leaving voice channel in guild: ${guild.name}`,
580
+ )
581
+
582
+ await cleanupVoiceConnection(guildId)
583
+ } else {
584
+ voiceLogger.log(
585
+ `Other admins still in channel, bot staying in voice channel`,
586
+ )
587
+ }
588
+ }
589
+ return
590
+ }
591
+
592
+ if (
593
+ oldState.channelId !== null &&
594
+ newState.channelId !== null &&
595
+ oldState.channelId !== newState.channelId
596
+ ) {
597
+ voiceLogger.log(
598
+ `Admin user ${member.user.tag} moved from ${oldState.channel?.name} to ${newState.channel?.name}`,
599
+ )
600
+
601
+ const guildId = guild.id
602
+ const voiceData = voiceConnections.get(guildId)
603
+
604
+ if (
605
+ voiceData &&
606
+ voiceData.connection.joinConfig.channelId === oldState.channelId
607
+ ) {
608
+ const oldVoiceChannel = oldState.channel as VoiceChannel
609
+ if (oldVoiceChannel) {
610
+ const hasOtherAdmins = oldVoiceChannel.members.some((m) => {
611
+ if (m.id === member.id || m.user.bot) return false
612
+ return (
613
+ m.id === guild.ownerId ||
614
+ m.permissions.has(PermissionsBitField.Flags.Administrator) ||
615
+ m.permissions.has(PermissionsBitField.Flags.ManageGuild) ||
616
+ m.roles.cache.some((role) => role.name.toLowerCase() === 'kimaki')
617
+ )
618
+ })
619
+
620
+ if (!hasOtherAdmins) {
621
+ voiceLogger.log(
622
+ `Following admin to new channel: ${newState.channel?.name}`,
623
+ )
624
+ const voiceChannel = newState.channel as VoiceChannel
625
+ if (voiceChannel) {
626
+ voiceData.connection.rejoin({
627
+ channelId: voiceChannel.id,
628
+ selfDeaf: false,
629
+ selfMute: false,
630
+ })
631
+ }
632
+ } else {
633
+ voiceLogger.log(
634
+ `Other admins still in old channel, bot staying put`,
635
+ )
636
+ }
637
+ }
638
+ }
639
+ }
640
+
641
+ if (oldState.channelId === null && newState.channelId !== null) {
642
+ voiceLogger.log(
643
+ `Admin user ${member.user.tag} (Owner: ${isOwner}, Admin: ${isAdmin}) joined voice channel: ${newState.channel?.name}`,
644
+ )
645
+ }
646
+
647
+ if (newState.channelId === null) return
648
+
649
+ const voiceChannel = newState.channel as VoiceChannel
650
+ if (!voiceChannel) return
651
+
652
+ const existingVoiceData = voiceConnections.get(newState.guild.id)
653
+ if (
654
+ existingVoiceData &&
655
+ existingVoiceData.connection.state.status !==
656
+ VoiceConnectionStatus.Destroyed
657
+ ) {
658
+ voiceLogger.log(
659
+ `Bot already connected to a voice channel in guild ${newState.guild.name}`,
660
+ )
661
+
662
+ if (
663
+ existingVoiceData.connection.joinConfig.channelId !== voiceChannel.id
664
+ ) {
665
+ voiceLogger.log(
666
+ `Moving bot from channel ${existingVoiceData.connection.joinConfig.channelId} to ${voiceChannel.id}`,
667
+ )
668
+ existingVoiceData.connection.rejoin({
669
+ channelId: voiceChannel.id,
670
+ selfDeaf: false,
671
+ selfMute: false,
672
+ })
673
+ }
674
+ return
675
+ }
676
+
677
+ try {
678
+ voiceLogger.log(
679
+ `Attempting to join voice channel: ${voiceChannel.name} (${voiceChannel.id})`,
680
+ )
681
+
682
+ const connection = joinVoiceChannel({
683
+ channelId: voiceChannel.id,
684
+ guildId: newState.guild.id,
685
+ adapterCreator: newState.guild.voiceAdapterCreator,
686
+ selfDeaf: false,
687
+ debug: true,
688
+ daveEncryption: false,
689
+ selfMute: false,
690
+ })
691
+
692
+ voiceConnections.set(newState.guild.id, { connection })
693
+
694
+ await entersState(connection, VoiceConnectionStatus.Ready, 30_000)
695
+ voiceLogger.log(
696
+ `Successfully joined voice channel: ${voiceChannel.name} in guild: ${newState.guild.name}`,
697
+ )
698
+
699
+ await setupVoiceHandling({
700
+ connection,
701
+ guildId: newState.guild.id,
702
+ channelId: voiceChannel.id,
703
+ appId,
704
+ discordClient,
705
+ })
706
+
707
+ connection.on(VoiceConnectionStatus.Disconnected, async () => {
708
+ voiceLogger.log(
709
+ `Disconnected from voice channel in guild: ${newState.guild.name}`,
710
+ )
711
+ try {
712
+ await Promise.race([
713
+ entersState(connection, VoiceConnectionStatus.Signalling, 5_000),
714
+ entersState(connection, VoiceConnectionStatus.Connecting, 5_000),
715
+ ])
716
+ voiceLogger.log(`Reconnecting to voice channel`)
717
+ } catch (error) {
718
+ voiceLogger.log(`Failed to reconnect, destroying connection`)
719
+ connection.destroy()
720
+ voiceConnections.delete(newState.guild.id)
721
+ }
722
+ })
723
+
724
+ connection.on(VoiceConnectionStatus.Destroyed, async () => {
725
+ voiceLogger.log(
726
+ `Connection destroyed for guild: ${newState.guild.name}`,
727
+ )
728
+ await cleanupVoiceConnection(newState.guild.id)
729
+ })
730
+
731
+ connection.on('error', (error) => {
732
+ voiceLogger.error(
733
+ `Connection error in guild ${newState.guild.name}:`,
734
+ error,
735
+ )
736
+ })
737
+ } catch (error) {
738
+ voiceLogger.error(`Failed to join voice channel:`, error)
739
+ await cleanupVoiceConnection(newState.guild.id)
740
+ }
741
+ } catch (error) {
742
+ voiceLogger.error('Error in voice state update handler:', error)
743
+ }
744
+ })
745
+ }