连接错误.在discordjs v13中,Play不是一个功能



我试图创建一个机器人,我试图从youtube教程做,我认为这是因为我使用的是discord.js v13,所以如果有人能告诉我如何降级到较低的discord.js版本,也可以是一个解决方案,但如果有人知道如何解决这个问题,那么请审查我的代码并帮助我。我使用的依赖的版本是-

"@discordjs/opus": "^0.6.0",
"@discordjs/voice": "^0.6.0",
"@types/ws": "^7.4.7",
"discord-api-types": "^0.22.0",
"discord.js": "^13.1.0",
"ffmpeg": "^0.0.4",
"ffmpeg-static": "^4.4.0",
"tiny-typed-emitter": "^2.1.0",
"yt-search": "^2.10.1",
"ytdl-core": "^4.9.1"

keep get this message -

index.js中的代码=

const Discord = require("discord.js");
const fs = require("fs");
const config = require("./config.json");
const intents = new Discord.Intents(32767);
const client = new Discord.Client({ intents });
const prefix = "-";
client.on("ready", () => {
console.log("client is ready");
});
client.commands = new Discord.Collection();
// const commandFiles = fs
//   .readdirSync("./commands/")
//   .filter((file) => file.endsWith(`.js`));
// for (const file of commandFiles) {
//   const command = require(`./commands/${file}`);
//   client.commands.set(command.name, command);
// }
// client.commands.get(command).execute(client, message, args, command, Discord);
const commandFiles = fs
.readdirSync("./commands/")
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
console.log(file);
console.log(command);
client.commands.set(command.name, command);
}
client.on("messageCreate", (message) => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (command === "hello") {
message.channel.send("hey there chutiyon");
} else if (command === "ping") {
client.commands.get("ping").execute(message, args);
} else if (command === "play") {
console.log(client.commands);
client.commands.get("play").execute(message, args);
} else if (command === "leave") {
client.commands.get("leave").execute(message, args);
}
});
client.login(config.token);

play.js中的代码=

const ytSearch = require("yt-search");
const { joinVoiceChannel } = require("@discordjs/voice");
module.exports = {
name: "play",
description: "this is play command",
async execute(message, args) {
// const voiceChannel = message.member.voice.channel;
console.log(message);
console.log(args);
const connection = joinVoiceChannel({
channelId: message.member.voice.channel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator,
});
// await voiceChannel.join();
const VideoFinder = async (query) => {
const videoResult = await ytSearch(query);
return videoResult.videos.length > 1 ? videoResult.videos[0] : null;
};
console.log(VideoFinder);
const video = await VideoFinder(args.join(""));
console.log(video);
if (video) {
const stream = ytdl(video.url, { filter: "audioonly" });
console.log("---------------------------------------");
console.log(stream);
console.log(typeof connection.play);
await connection
.play(stream, { seek: 0, volume: 1, type: "opus" })
.on(finish, () => {
voiceChannel.leave();
});
await message.reply(`:thumbsup: nowplaying ----${video.title}---`);
} else {
message.channel.send("video not found");
}
},
};

.play(stream, { seek: 0, volume: 1, type: "opus" })
^
TypeError: connection.play is not a function
at Object.execute (C:UserspcWE_SOO_NOOBLA_BOTcommandsplay.js:30:10)
at processTicksAndRejections (node:internal/process/task_queues:96:5)

不能在voiceConnection上调用.play(),必须创建AudioPlayersubscribe对于这个播放器,你必须做一些基本的添加,如下所示:

const {
joinVoiceChannel,
createAudioPlayer,
createAudioResource
} = require('@discordjs/voice');
const connection = joinVoiceChannel({
channelId: message.member.voice.channel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator,
});
const stream = ytdl(video.url, {
filter: "audioonly"
});
const player = createAudioPlayer();
const resource = createAudioResource(stream);
async function play() {
await player.play(resource);
connection.subscribe(player);
}

他们只是让它过于复杂没有好的理由,无论如何,如果你想要降级,你可以创建一个名为main.sh的文件,其内容在以下代码块中,或者只是命令在代码块中

npm uninstall discord.js
npm install discord.js@12.5.3

最新更新