I’ve been raking my brain on this for 20 mins

this discord bot is supposed to join the users voice channel and play music, everything works except for it not transmitting audio. from what I can gather the error is happening somewhere between it playing the audio file and it being transmitted. I’ve been trying to use chat-GPT but to no avail.

I’ve tried chat-GPT, stack overflow and reddit but couldn’t figure it out, stack overflow and reddit but couldn’t figure it out.

const { Client, GatewayIntentBits, SlashCommandBuilder, MessageEmbed } = require("discord.js");
const { joinVoiceChannel, createAudioPlayer, NoSubscriberBehavior, createAudioResource } = require('@discordjs/voice');
const { token } = require("./config.json");
const fs = require('fs');

const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions] });

client.once("ready", () => {
    console.log("Bot active.");

    const ping = new SlashCommandBuilder()
        .setName('ping')
        .setDescription('Replies with pong!');

    const hello = new SlashCommandBuilder()
        .setName('hello')
        .setDescription('Says hello!')
        .addUserOption(option =>
            option
                .setName('user')
                .setDescription('The user to say hi to')
                .setRequired(false)
        );

    const echo = new SlashCommandBuilder()
        .setName('echo')
        .setDescription("Echo's a message")
        .addStringOption(option =>
            option
                .setName('text')
                .setDescription('The text to repeat')
                .setRequired(true)
        );

    const mata = new SlashCommandBuilder()
        .setName('mata')
        .setDescription('Green, ma-ta is here!');

    const eddy = new SlashCommandBuilder()
        .setName('eddy')
        .setDescription('Original reggae music!')
        .addStringOption(option =>
            option
                .setName('local_audio')
                .setDescription('Provide the name of the local audio file')
                .setRequired(true)
        );

    const disconnect = new SlashCommandBuilder()
        .setName('disconnect')
        .setDescription('Disconnects the bot from the voice channel');

    const commands = [ping, hello, echo, mata, eddy, disconnect];

    commands.forEach(command => client.guilds.cache.forEach(guild => guild.commands.create(command)));


});

client.on('interactionCreate', async interaction => {
    console.log("Interaction received.");
    
    if (!interaction.isCommand()) {
        console.log("Not a command interaction.");
        return;
    }

    const { commandName, options, member, guild } = interaction;
    console.log("Command name:", commandName);

    if (commandName === 'ping') {
        console.log("Ping command executed.");
        await interaction.reply("Pong!");
    } else if (commandName === 'hello') {
        console.log("Hello command executed.");
        let user = options.getUser('user') || member.user;
        await interaction.reply(`Hello ${user.username}!`);
    } else if (commandName === 'echo') {
        console.log("Echo command executed.");
        const text = options.getString('text');
        await interaction.reply(text);
    } else if (commandName === 'mata') {
        console.log("Mata command executed.");
        const embed = new MessageEmbed()
            .setColor('#00FF00')
            .setTitle('This is mata!')
            .setDescription('She is very ugly!')
            .setImage('https://scottbarrykaufman.com/wp-content/uploads/2011/08/pig-ugly-woman-fat-face.jpg');

        await interaction.reply({ embeds:  });
    } else if (commandName === 'eddy') {
        console.log("Eddy command executed.");

        const voiceChannel = member.voice.channel;
        if (!voiceChannel) {
            await interaction.reply("You need to be in a voice channel to use this command.");
            return;
        }

        const connection = joinVoiceChannel({
            channelId: voiceChannel.id,
            guildId: guild.id,
            adapterCreator: guild.voiceAdapterCreator,
        });

        connection.on('stateChange', (state) => {
            console.log(`Connection state changed to ${state.status}`);
        });

        connection.on('error', (error) => {
            console.error('Connection error:', error);
        });

        const localAudioFile = options.getString('local_audio');
        const filePath = `./audio/Gorillaz.mp3`;

        console.log("File path:", filePath);

        if (!fs.existsSync(filePath)) {
            console.log("File does not exist.");
            await interaction.reply("The specified local audio file does not exist.");
            return;
        }

        const audioPlayer = createAudioPlayer({
            behaviors: {
                noSubscriber: NoSubscriberBehavior.Pause,
            },
        });

        const stream = fs.createReadStream(filePath);
        const resource = createAudioResource(stream);
        audioPlayer.play(resource);

        connection.subscribe(audioPlayer);

        console.log("Audio playback started.");
        await interaction.reply("Now playing audio in your voice channel!");
    } else if (commandName === 'disconnect') {
        console.log("Disconnect command executed.");
        const voiceChannel = member.voice.channel;

        if (!voiceChannel) {
            await interaction.reply("The bot is not in a voice channel.");
            return;
        }

        const connection = joinVoiceChannel({
            channelId: voiceChannel.id,
            guildId: guild.id,
            adapterCreator: guild.voiceAdapterCreator,
        });

        connection.on('stateChange', (state) => {
            console.log(`Connection state changed to ${state.status}`);
        });

        connection.on('error', (error) => {
            console.error('Connection error:', error);
        });

        if (connection) {
            connection.destroy();
            console.log("Disconnected from the voice channel.");
        }
    }
});

client.login(token);

New contributor

R5B3NBB is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật