我正在尝试构建一个不和谐的音乐播放机器人。仅供参考,我在
我正在使用事件驱动设计系统来知道何时加载新视频,但是机器人没有加入语音通道,但没有给出错误消息。这是我尝试加入语音频道的地方
events.on(eventNames.PlayNextEvent, async () => {
logger.logInfo(eventNames.PlayNextEvent);
const item = await queue.next();
// establish voice channel connection
let voiceConnection = getVoiceConnection(item.guildId);
if (!voiceConnection) {
logger.logInfo(`creating voice connection in guild ${item.guildId} in channel ${item.voiceChannelId}...`);
let guild = client.guilds.cache.get(item.guildId);
voiceConnection = joinVoiceChannel({
channelId: item.voiceChannelId,
guild: item.guildId,
adapterCreator: guild.voiceAdapterCreator
});
}
const stream = await audioPlayer.buildStream(item);
if (!stream) {
logger.logError('stream is null');
return;
}
const resource = createAudioResource(stream.stream, {
inputType: stream.type
});
let discordPlayer = createAudioPlayer({
behaviors: {
noSubscriber: NoSubscriberBehavior.Stop
}
});
discordPlayer.play(resource);
voiceConnection.subscribe(discordPlayer);
logger.logInfo(`playing "${item.title}"`);
});
我已经验证该事件正在被调用,并且我在那里的所有日志消息都已被打印。我还尝试过将语音连接和播放器也设置为全局变量,但这不起作用。我还确认我提供给
guilId
的 voiceChannelId
和 joinVoiceChannel
是正确的,并且 guild.voiceAdapterCreator
不为空。另外,我已经用以下意图初始化了我的客户端
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildVoiceStates,
]
});
更多参考,这里是
buildStream
exports.buildStream = async (videoInfo) => {
try {
return await play.stream(videoInfo.info.url);
} catch (err) {
logger.logError('error bulding stream');
logger.logError(err);
return null;
}
};
并且
const item = await queue.next()
从 mongodb 队列中返回一个项目,我确认该项目正在工作并正确保存我需要的所有信息
以防万一其他人遇到这个问题,我在 joinVoiceChannel 中的 guild id 变量名称错误。我只是有公会,而它应该是 guildId。
voiceConnection = joinVoiceChannel({
channelId: item.voiceChannelId,
guildId: item.guildId,
adapterCreator: guild.voiceAdapterCreator
});