在 Discord 中播放音频文件时出现问题
Problems playing audio files in Discord
所以,花了两天的时间来弄清楚到底发生了什么......我正在为我的 Discord 频道创建一个机器人,它在编写命令时播放 audio.mp3,比如 !laugh 然后机器人应该进入语音通道并重现 laugh.mp3。我尝试了很多不同的方式,但机器人一直进入频道,绿色圆圈很快出现但没有播放声音...
const { join } = require('path');
const { joinVoiceChannel, createAudioPlayer, createAudioResource, getVoiceConnection, entersState, StreamType, AudioPlayerStatus, VoiceConnectionStatus, AudioResource } = require("@discordjs/voice");
module.exports = {
name: 'laugh',
aliases: ["l"],
run: async(client, message, args) => {
const player = createAudioPlayer()
const connection = joinVoiceChannel({
channelId: message.member.voice.channel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator
}).subscribe(player)
let resource = createAudioResource(join('./som/', 'laugh.mp3'));
player.play(resource)
player.on(AudioPlayerStatus.AutoPaused, () => {
player.stop();
});
}
}
我已经尝试过捕获错误,但显然没有任何问题,我的 index.js.
中已经包含了所有意图
那么,谁能帮我找到解决办法?
第 3 天过后,我决定寻找其他可以用来解决问题的库,结果我找到了使用 FS!
的解决方案
我没有只尝试播放文件,而是创建了一个流,然后我就播放了流...就性能而言,这不是有史以来最好的解决方案,但它确实有效!
事情是这样的:
const {createReadStream } = require('fs')
const { join } = require('path');
const { joinVoiceChannel, createAudioPlayer, createAudioResource,
getVoiceConnection, entersState, StreamType, AudioPlayerStatus,
VoiceConnectionStatus, AudioResource } = require("@discordjs/voice");
module.exports = {
name: 'laugh',
aliases: ["l"],
run: async(client, message, args) => {
const player = createAudioPlayer();
const connection = joinVoiceChannel({
channelId: message.member.voice.channel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator,
selfDeaf: false,
}).subscribe(player);
let resource = createAudioResource(createReadStream(join(__dirname, 'som/laugh.mp3')), {
inlineVolume : true
});
resource.volume.setVolume(0.9);
console.log(join(__dirname, 'som/laugh.mp3'));
player.play(resource)
player.on(AudioPlayerStatus.AutoPaused, () => {
player.stop();
});
我必须提醒的是 Replit 没有正确解析 Node.js 17 和其他依赖项,因此要使您的机器人 24/7 全天候运行,请使用 Heroku 和 GitHub。要完成 GUILD_VOICE_STATES
需要设置意图,我使用的是 32767,但不知何故这不起作用。
就是这样!
所以,花了两天的时间来弄清楚到底发生了什么......我正在为我的 Discord 频道创建一个机器人,它在编写命令时播放 audio.mp3,比如 !laugh 然后机器人应该进入语音通道并重现 laugh.mp3。我尝试了很多不同的方式,但机器人一直进入频道,绿色圆圈很快出现但没有播放声音...
const { join } = require('path');
const { joinVoiceChannel, createAudioPlayer, createAudioResource, getVoiceConnection, entersState, StreamType, AudioPlayerStatus, VoiceConnectionStatus, AudioResource } = require("@discordjs/voice");
module.exports = {
name: 'laugh',
aliases: ["l"],
run: async(client, message, args) => {
const player = createAudioPlayer()
const connection = joinVoiceChannel({
channelId: message.member.voice.channel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator
}).subscribe(player)
let resource = createAudioResource(join('./som/', 'laugh.mp3'));
player.play(resource)
player.on(AudioPlayerStatus.AutoPaused, () => {
player.stop();
});
}
}
我已经尝试过捕获错误,但显然没有任何问题,我的 index.js.
中已经包含了所有意图那么,谁能帮我找到解决办法?
第 3 天过后,我决定寻找其他可以用来解决问题的库,结果我找到了使用 FS!
的解决方案我没有只尝试播放文件,而是创建了一个流,然后我就播放了流...就性能而言,这不是有史以来最好的解决方案,但它确实有效!
事情是这样的:
const {createReadStream } = require('fs')
const { join } = require('path');
const { joinVoiceChannel, createAudioPlayer, createAudioResource,
getVoiceConnection, entersState, StreamType, AudioPlayerStatus,
VoiceConnectionStatus, AudioResource } = require("@discordjs/voice");
module.exports = {
name: 'laugh',
aliases: ["l"],
run: async(client, message, args) => {
const player = createAudioPlayer();
const connection = joinVoiceChannel({
channelId: message.member.voice.channel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator,
selfDeaf: false,
}).subscribe(player);
let resource = createAudioResource(createReadStream(join(__dirname, 'som/laugh.mp3')), {
inlineVolume : true
});
resource.volume.setVolume(0.9);
console.log(join(__dirname, 'som/laugh.mp3'));
player.play(resource)
player.on(AudioPlayerStatus.AutoPaused, () => {
player.stop();
});
我必须提醒的是 Replit 没有正确解析 Node.js 17 和其他依赖项,因此要使您的机器人 24/7 全天候运行,请使用 Heroku 和 GitHub。要完成 GUILD_VOICE_STATES
需要设置意图,我使用的是 32767,但不知何故这不起作用。
就是这样!