【发布时间】:2021-11-03 22:47:04
【问题描述】:
const ytdl = require('ytdl-core');
const ytSearch = require('yt-search');
module.exports = {
name: 'play',
description: "plays music in voice chat",
async execute(message, args, Discord){
const voiceChannel = message.member.voice.channel;
if (!voiceChannel) return message.channel.send('You need to be in a voice channel to listen to music!');
const permissions = voiceChannel.permissionsFor(message.client.user);
if (!permissions.has('CONNECT')) return message.reply("You don't have the correct permissions to request music!");
if (!permissions.has('SPEAK')) return message.reply("You don't have the correct permissions to request music!");
if (!args.length) return message.channel.send("You Can't Request for no Music Silly >_<");
const connection = await voiceChannel.join();
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.reply('No Video Results Found :(');
}
}
}
我的错误信息是 const connection = await voiceChannel.join();
TypeError: voiceChannel.join 不是函数
如果您有任何可行的建议,请随时进行编辑。它也只能在机器人启动时检测是否有人在语音聊天中,因此如果您在机器人启动时进行语音聊天,它会在您进入和退出语音聊天时发送语音聊天消息
【问题讨论】:
-
你使用的是什么版本的 Discord.js?我对不和谐机器人了解不多,但从他们的大多数示例来看,他们似乎已经切换到 TS 并且还将“语音”抽象到自己的包中。如果您使用的是较新版本,我建议您学习一点 TS 并使用它。它会在编译之前发现错误 :) 话虽如此,看起来“语音”包没有“加入”方法,而是使用“订阅”对象。见下文:github.com/discordjs/voice/blob/main/examples/music-bot/src/…
-
以上假设您使用的是此软件包的较新版本之一,因为 OP 中没有另外提及。
-
我使用的是最新版本的 discord.js,不知道如何查看我通过 npm 下载的发行版本号
-
这样的话,我也遇到过TS版本。我建议不要尽可能多地使用 JS,并尽可能切换到使用 TS。这些类型的错误会在编译之前被提前发现。我在上面分享的 repo 中有一些示例,以及 google 上的大量 TS 资源,希望对您有所帮助。绝对欢迎您继续使用纯 JS,但恐怕您以后可能会遇到许多类似的问题。
-
@NaturalTwitch 请给这个问题一个更具体的标题,以便在以后的搜索中更容易找到它。
标签: javascript discord.js