30 lines
754 B
TypeScript
30 lines
754 B
TypeScript
import {
|
|
getVoiceConnection as defaultGetVoiceConnection,
|
|
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;
|
|
}
|