My discord js bot wont play audio in the voice channel even though it joins, I do not get any errors in the console.
I recently switched from using a windows machine to a macos system, does it have anything to do with permissions from the OS? I have fffmpeg installed and all the necessary dependancies that @/discordjs/voice needs and also all the right intents.
import {
VoiceConnection,
joinVoiceChannel,
AudioPlayer,
createAudioPlayer,
NoSubscriberBehavior,
createAudioResource,
} from "@discordjs/voice";
import { db } from "../../../database";
import { videos } from "../../../database/schema";
import type { VideoMetadataResult } from "yt-search";
import { Guild, VoiceBasedChannel } from "discord.js";
import { getVideos } from "../../../database/utils";
import playdl from "play-dl";
export class Player {
guild = null;
connection: VoiceConnection = null;
voiceChannel = null;
player = createAudioPlayer({
behaviors: {
noSubscriber: NoSubscriberBehavior.Stop,
},
});
constructor(guild: Guild, voiceChannel: VoiceBasedChannel) {
this.guild = guild;
this.voiceChannel = voiceChannel;
}
async queue(video: VideoMetadataResult) {
const videosCount = (await getVideos()).length;
await db.insert(videos).values({
url: video.url,
authorName: video.author.name,
title: video.title,
seconds: video.seconds,
index: videosCount,
});
}
createConnection() {
if (!this.voiceChannel) return;
this.connection = joinVoiceChannel({
channelId: this.voiceChannel.id,
guildId: this.guild.id,
selfDeaf: false,
adapterCreator: this.guild.voiceAdapterCreator,
});
}
join() {
if (!this.connection) this.createConnection();
}
async playNext() {
const allVideos = (await getVideos()).sort((a, b) => a.index - b.index);
this.join();
if (!allVideos[0]) return;
this.connection?.subscribe(this.player);
const stream = await playdl.stream(allVideos[0].url, {
discordPlayerCompatibility: true,
});
const audioSource = createAudioResource(stream.stream);
this.player.play(audioSource);
this.player.on("stateChange", (state) => {
console.log(state);
});
}
}