簡體   English   中英

Discordjs 13 bot 不播放本地 mp3 文件

[英]Discordjs 13 bot not playing local mp3 file

我不知道為什么我的機器人不播放音樂。 這是我的代碼。

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 文件。 有任何想法嗎?

我終於解決了我的問題。
我缺少的是客戶的意圖。 它是 GUILD_VOICE_STATES。

試試這個代碼:

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

另請查看此處以了解您已成功安裝了哪些依賴項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM