1 javascript bots discord discord.js
一般来说,我对编码很陌生,所以预计会有新手行为。
我的 Discord Bot 通过命令节点上线。我尝试了 !ping 命令,一切正常。但是,当我尝试声明的命令 !play 来播放来自 YT 的视频时,但当我按 Enter 键时,机器人崩溃并显示以下日志:
const connection = await voiceChannel.join();
^
TypeError: voiceChannel.join is not a function
at Object.execute (D:\Discord Bot\commands\play.js:43:48)
at Client.<anonymous> (D:\Discord Bot\main.js:33:41)
at Client.emit (node:events:394:28)
at MessageCreateAction.handle (D:\Discord Bot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:18)
at Object.module.exports [as MESSAGE_CREATE] (D:\Discord Bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (D:\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:345:31)
at WebSocketShard.onPacket (D:\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:443:22)
at WebSocketShard.onMessage (D:\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:300:10)
at WebSocket.onMessage (D:\Discord Bot\node_modules\ws\lib\event-target.js:132:16)
at WebSocket.emit (node:events:394:28)
Run Code Online (Sandbox Code Playgroud)
这是主要代码:
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] });
const prefix = '!';
const fs = require('fs');
const { join } = require('path');
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.once('ready', () => {
console.log('Mein Mussig Bot ist nun online!');
});
client.on('message', 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 === 'ping'){
client.commands.get('ping').execute(message,args);
} else if (command === 'play'){
client.commands.get('play').execute(message,args);
} else if (command === 'stop'){
client.commands.get('stop').execute(message,args);
}
});
client.login('token')
Run Code Online (Sandbox Code Playgroud)
这是播放命令代码:
const ytdl = require('ytdl-core');
const ytSearch = require('yt-search');
module.exports = {
name: 'play',
description: 'Joins and plays a video from youtube',
async execute(message, args) {
const voiceChannel = message.member.voice.channel;
if (!voiceChannel) return message.channel.send('You need to be in a channel to execute this command!');
const permissions = voiceChannel.permissionsFor(message.client.user);
if (!permissions.has('CONNECT')) return message.channel.send('You dont have the correct permissins');
if (!permissions.has('SPEAK')) return message.channel.send('You dont have the correct permissins');
if (!args.length) return message.channel.send('You need to send the second argument!');
const validURL = (str) =>{
var regex = /(http|https):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%!\-\/]))?/;
if(!regex.test(str)){
return false;
} else {
return true;
}
}
if(validURL(args[0])){
const connection = await voiceChannel.join()
.catch(console.error);
const stream = ytdl(args[0], {filter: 'audioonly'});
connection.play(stream, {seek: 0, volume: 1})
.on('finish', () =>{
voiceChannel.leave();
message.channel.send('leaving channel');
});
await message.reply(`:thumbsup: Now Playing ***Your Link!***`)
return
}
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.channel.send('No video results found');
}
}
}
Run Code Online (Sandbox Code Playgroud)
请帮忙
我知道我晚了一个月,但无论谁看到这个,在 Discord JS v13 新更新后,加入语音频道现在是这样的:
import { joinVoiceChannel } from "@discordjs/voice";
const connection = joinVoiceChannel(
{
channelId: message.member.voice.channel,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator
});
Run Code Online (Sandbox Code Playgroud)
有关 @discordjs/voice 的更多信息请参见此处。运行npm i @discordjs/voice安装。
| 归档时间: |
|
| 查看次数: |
9926 次 |
| 最近记录: |