yaejunyang/packages/bot/util.ts
2026-02-09 18:31:56 +00:00

25 lines
853 B
TypeScript

import { getVoiceConnection as defaultGetVoiceConnection, EndBehaviorType, joinVoiceChannel, VoiceConnection } from "@discordjs/voice";
import { Guild } from "discord.js";
export async function getOrCreateVoiceConnection(guild: Guild): Promise<VoiceConnection | undefined> {
let connection = defaultGetVoiceConnection(guild.id);
if (!connection) {
if (!guild.members.me?.voice.channel)
return;
const channel = guild.members.me.voice.channel;
connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
selfDeaf: false,
selfMute: false,
});
await new Promise((resolve) => setTimeout(resolve, 2000));
}
return connection;
}