【问题标题】:Discordjs 13 bot not playing local mp3 fileDiscordjs 13 bot 不播放本地 mp3 文件
【发布时间】:2022-01-08 20:14:24
【问题描述】:

我不明白为什么我的机器人不播放音乐。 这是我的代码。

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);
}

我正在使用 Discordjs 13,安装了所有必需的模块......机器人加入了语音频道,但它没有在我的本地文件夹中播放歌曲。编辑:控制台不返回错误,机器人具有管理员权限,并且没有静音或耳聋。

编辑 2: 这是我使用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
--------------------------------------------------

我认为一切正常,但机器人仍然不播放音乐

编辑 3:
我把我的代码编辑成了这个

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);
            }

仍然没有控制台错误,机器人加入了语音频道,但它不播放 .mp3 文件。有什么想法吗?

【问题讨论】:

  • 我真的检查了所有的 stackoverflow 问题,但没有一个有效。还检查了所有 youtube 视频,但没有一个有效。我是 JS 和 Discord.js 的新手。
  • 你用哪个包播放mp3?我第一次使用 opusscript 但后来我切换到 @discordjs/opus 因为有时我的机器人完全按照你在问题中描述的那样做。检查控制台或日志中是否有错误。
  • 机器人是聋了还是静音了?
  • 控制台中没有错误,我安装了 ffmpeg、@discordjs/voice 和 @discordjs/opus,因为大多数 youtube 视频都建议编辑:该机器人具有管理员权限,它没有被静音,它是没聋
  • @Davide 所以你正试图在非阶段.setRequestToSpeak()。删除那个

标签: javascript discord discord.js bots audio-player


【解决方案1】:

我终于解决了我的问题。
我缺少的是客户的意图。是 GUILD_VOICE_STATES。

【讨论】:

    【解决方案2】:

    试试这个代码:

    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')
    

    这是我的package.json 文件:

    {
      "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"
      }
    }
    
    

    注意:我已经安装了ffmpeg,所以我不需要使用ffmpeg-static

    同时检查here 以找出您已成功安装的依赖项。

    【讨论】:

    • 您应该解释 OP 做错了什么以及如何修复它。你所做的只是转储你自己的代码。
    • @PewPew 感谢您花费时间和精力帮助我。我会检查我的所有依赖项是否都正常,希望这能解决我的问题
    • 添加了依赖项的控制台输出编辑,我还尝试了您的代码@PewPew,但没有任何效果
    • @PewPew 谢谢你,我错过了我的客户端中的意图 GUILD_VOICE_STATES,我确定我说的,谢谢
    猜你喜欢
    • 2021-11-05
    • 1970-01-01
    • 2015-04-23
    • 2014-11-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多