我有一个音乐机器人项目,我从YouTube那里得到了它,我给它添加了一些我自己的东西。
问题是每次我发送?play (songname)
命令时,它都会发送一个错误给connection.play is not a function
。我该怎么办?
我所犯的错误:
connection.play(stream, {seek: 0, volume: 1})
^
TypeError: connection.play is not a function
at Object.execute (C:\Users\Pauli.Salminen\Desktop\DiscordBotPRojects\reactionroles\commands\play.js:41:24)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
这是我的play.js
const ytdl = require('ytdl-core');
const ytSearch = require('yt-search');
const { joinVoiceChannel } = require('@discordjs/voice');
module.exports = {
name: 'play',
description: 'joins aadn plays muusic',
async execute(kaoru, message, args) {
const voiceChannel = message.member.voice.channel;
if (!voiceChannel)
return message.channel.send('> **You need to join voicechannel first!**');
const permissions = voiceChannel.permissionsFor(message.client.user);
if (!permissions.has('CONNECT'))
return message.channel.send('> **You dont have right permissions!**');
if (!permissions.has('SPEAK'))
return message.channel.send('> **You dont have right permissions!**');
if (!args.length)
return message.channel.send('> **You need to insert name of the song!**');
const connection = joinVoiceChannel({
channelId: message.member.voice.channel,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator,
});
const videoFinder = async (query) => {
const videoResult = await ytSearch(query);
return videoResult.videos.length > 1 ? videoResult.videos[0] : null;
};
const video = await videoFinder(args.join(' '));
if (video) {
const stream = ytdl(video.url, { filter: 'audioonly' });
connection.play(stream, { seek: 0, volume: 1 }).on('finish', () => {
voiceChannel.leave();
});
await message.reply(`:thumbsup: Now playing ***${video.title}***`);
} else {
message.channel.send('No video results found');
}
}
};
发布于 2022-03-28 21:14:04
discord.js v13中有一些更改,您必须使用@discordjs/voice
模块。
您的第一个错误是您没有在channelId: message.member.voice.channel
提供ID。通道ID应该是message.member.voice.channel.id
。
第二,play()
在connection
上不再可用。在v13中,您必须首先使用createAudioPlayer()
方法创建音频播放器,然后创建音频资源。音频资源包含音频,可由音频播放器播放到语音连接。要创建一个参数,您可以使用createAudioResource()
方法并传递您的stream
作为参数。
一旦资源被创建,您可以使用player.play()
在音频播放器上播放它们。您还需要将您的connection
订阅到player
,以便连接将广播您的player
正在播放的任何内容。为此,使用播放机作为参数,在您的语音subscribe()
上调用connection
方法。
此外,也没有connection.on
侦听器。不过,您可以使用player.on
。要检查歌曲是否已经完成,您可以订阅AudioPlayerStatus.Idle
事件。
最后一件事,离开一个通道,而不是voiceChannel.leave()
,您应该使用connection.disconnect()
或connection.destroy()
。
您可以在下面找到工作代码:
const {
AudioPlayerStatus,
createAudioPlayer,
createAudioResource,
joinVoiceChannel,
} = require('@discordjs/voice');
const ytdl = require('ytdl-core');
const ytSearch = require('yt-search');
module.exports = {
name: 'play',
description: 'joins aadn plays muusic',
async execute(kaoru, message, args) {
const voiceChannel = message.member.voice.channel;
if (!voiceChannel)
return message.channel.send('> **You need to join voicechannel first!**');
const permissions = voiceChannel.permissionsFor(message.client.user);
if (!permissions.has('CONNECT'))
return message.channel.send('> **You dont have right permissions!**');
if (!permissions.has('SPEAK'))
return message.channel.send('> **You dont have right permissions!**');
if (!args.length)
return message.channel.send('> **You need to insert name of the song!**');
const connection = joinVoiceChannel({
channelId: message.member.voice.channel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator,
});
const videoFinder = async (query) => {
const videoResult = await ytSearch(query);
return videoResult.videos.length > 1 ? videoResult.videos[0] : null;
};
const video = await videoFinder(args.join(' '));
if (video) {
const stream = ytdl(video.url, { filter: 'audioonly' });
const player = createAudioPlayer();
const resource = createAudioResource(stream);
await player.play(resource);
connection.subscribe(player);
player.on('error', (error) => console.error(error));
player.on(AudioPlayerStatus.Idle, () => {
console.log(`song's finished`);
connection.disconnect();
});
await message.reply(`:thumbsup: Now playing ***${video.title}***`);
} else {
message.channel.send('No video results found');
}
},
};
PS:确保启用了GUILD_VOICE_STATES
意图。没有这一点,你的机器人将无法连接到一个语音频道。
https://stackoverflow.com/questions/71652080
复制相似问题