DiscordJS 音乐机器人在开始播放歌曲后立即断开连接
Posted
技术标签:
【中文标题】DiscordJS 音乐机器人在开始播放歌曲后立即断开连接【英文标题】:DiscordJS music bot disconnects immediately after starting to play the song 【发布时间】:2021-12-16 03:42:04 【问题描述】:我尝试播放 Youtube URL,但当它开始播放时,它会在 1 秒后停止。 mp3 文件可以正常工作,但当我尝试播放 Youtube 网址时它就无法正常工作。
这是我的代码:
case 'play':
const Channel = ['903334978199388271'];
for (const channelId of Channel)
joinChannel(channelId);
function joinChannel(channelId)
Client.channels
.fetch(channelId)
.then((channel) =>
const VoiceConnection = joinVoiceChannel(
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
);
const player = createAudioPlayer();
VoiceConnection.subscribe(player);
player.play(
createAudioResource(
ytdl('https://www.youtube.com/watch?v=sPPsOmQh76A'),
),
);
)
.catch(console.error);
break;
【问题讨论】:
控制台有错误吗?机器人发出了什么? 不,我没有任何错误 【参考方案1】:我之前也遇到过同样的问题,问题是ytdl-core
,所以你可以改用 ytdl-core-discord
。这是一个例子:
const Client, Intents, MessageEmbed = require('discord.js')
const ytdl = require('ytdl-core-discord')
const
joinVoiceChannel,
createAudioPlayer,
createAudioResource,
StreamType
= 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 :)
]
);
var prefix = '!'
client.on('ready', async () =>
console.log('Client is Ready...');
);
client.on('messageCreate', async (message) =>
if (message.content.trim().toLocaleLowerCase() === prefix + 'play')
const channel = client.channels.cache.get('Channel ID Here')
const connection = joinVoiceChannel(
channelId: channel.id,
guildId: channel.guildId,
adapterCreator: channel.guild.voiceAdapterCreator
);
const player = createAudioPlayer();
const resource = createAudioResource(await ytdl('https://www.youtube.com/watch?v=sPPsOmQh76A'), inputType: StreamType.Opus );
player.play(resource);
connection.subscribe(player);
);
client.login('Bot Token Here!');
【讨论】:
以上是关于DiscordJS 音乐机器人在开始播放歌曲后立即断开连接的主要内容,如果未能解决你的问题,请参考以下文章