简体   繁体   中英

Discordjs 13 bot not playing local mp3 file

I can't figure out why my bot is not playing music. This is my code.

if (command === 'song') {
    const player = createAudioPlayer();

    joinVoiceChannel({
        channelId: message.member.voice.channel.id,
        guildId: message.guild.id,
        adapterCreator: message.guild.voiceAdapterCreator
    }).subscribe(player);

    message.guild.me.voice.setRequestToSpeak(true);
    const resource = createAudioResource('music/song.mp3');
    player.play(resource);
}

I'm using Discordjs 13, installed all the required modules... the bot joins the voice channel but it doesn't play the song in my local folder. Edit: The console does not return errors, the bot has administrator permissions and it is not muted or deafened.

Edit 2: This is the report I got from the console when I use generateDependenciesReport()

--------------------------------------------------
Core Dependencies
- @discordjs/voice: 0.7.4
- prism-media: 1.3.2

Opus Libraries
- @discordjs/opus: 0.5.3
- opusscript: not found

Encryption Libraries
- sodium: not found
- libsodium-wrappers: 0.7.9
- tweetnacl: not found

FFmpeg
- version: 4.4.1-essentials_build-www.gyan.dev
- libopus: yes
--------------------------------------------------

And I think that everything's okay but the bot is still not playing music

Edit 3:
I edited my code to this

if (!message.member.voice.channel) {
                return
            } else if (message.member.voice.channel) {

                const connection = joinVoiceChannel({
                    channelId: message.member.voice.channel.id,
                    guildId: message.guild.id,
                    adapterCreator: message.guild.voiceAdapterCreator
                });

                const player = createAudioPlayer();
                const resource = createAudioResource('./music/song.mp3');
                //play the song resource
                player.play(resource);
                connection.subscribe(player);
            }

Still no console errors, the bot joins the voice channel but it doesn't play the.mp3 file. Any ideas?

I finally solved my problem.
The thing I was missing was an intent of the client. It was GUILD_VOICE_STATES.

Try this code:

const { Client, Intents } = require('discord.js');
const {
    joinVoiceChannel,
    createAudioPlayer,
    createAudioResource
} = require('@discordjs/voice');

const client = new Client({
    intents: [
        Intents.FLAGS.GUILDS,
        Intents.FLAGS.GUILD_MEMBERS,
        Intents.FLAGS.GUILD_MESSAGES,
        Intents.FLAGS.GUILD_VOICE_STATES // <= Don't miss this :)
    ]
});

client.on('ready', () => {
    console.log(`Logged in as ${client.user.username}`);
})

client.on('messageCreate', async (message) => {
    if (message.content === '!play') {
        if (!message.member.voice?.channel) return message.channel.send('You need to be a voice channel to execute this command')

        const connection = joinVoiceChannel({
            channelId: message.member.voice.channelId,
            guildId: message.guildId,
            adapterCreator: message.guild.voiceAdapterCreator
        })

        const player = createAudioPlayer()
        const resource = createAudioResource('./music/song.mp3')

        connection.subscribe(player)

        player.play(resource)
    }
})

client.login('Token Here')

Here's my package.json file:

{
  "name": "discord_bot",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@discordjs/opus": "^0.5.3",
    "@discordjs/voice": "^0.7.5",
    "discord.js": "^13.5.0",
    "libsodium-wrappers": "^0.7.9"
  }
}

Note: I have already installed ffmpeg , so I don't need to use ffmpeg-static

Also check here to find out which dependencies you've successfully installed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM