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,安装了所有必需的模块...机器人加入了语音频道,但它没有播放我本地文件夹中的歌曲。编辑:控制台没有 return 错误,机器人具有管理员权限,并且没有静音或震耳欲聋。
编辑 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);
}
仍然没有控制台错误,bot 加入了语音频道但没有播放.mp3 文件。有什么想法吗?
试试这个代码:
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, so I don't need to use ffmpeg-static
同时检查 here 以找出您已成功安装的依赖项。
我终于解决了我的问题。
我缺少的是客户的意图。这是 GUILD_VOICE_STATES。
我不明白为什么我的机器人不播放音乐。 这是我的代码。
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,安装了所有必需的模块...机器人加入了语音频道,但它没有播放我本地文件夹中的歌曲。编辑:控制台没有 return 错误,机器人具有管理员权限,并且没有静音或震耳欲聋。
编辑 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);
}
仍然没有控制台错误,bot 加入了语音频道但没有播放.mp3 文件。有什么想法吗?
试试这个代码:
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, so I don't need to use ffmpeg-static
同时检查 here 以找出您已成功安装的依赖项。
我终于解决了我的问题。
我缺少的是客户的意图。这是 GUILD_VOICE_STATES。