【发布时间】:2021-10-01 03:26:59
【问题描述】:
这是我的 play.js。我不是编码员,所以我不理解大部分代码。它需要进行一些故障排除才能正常工作。
const ytdl = require('ytdl-core');
const ytSearch = require('yt-search');
module.exports = {
name: 'play',
description: 'plays a song or whatever.',
async execute(message, args) {
const voiceChannel = message.member.voice.channel;
if(!voiceChannel) return message.channel.send('lol u need to be in a voice channel first');
const permissions = voiceChannel.permissionsFor(message.client.user);
if(!permissions.has('CONNECT')) return message.channel.send('u dont have the correct permissions smh');
if(!permissions.has('SPEAK')) return message.channel.send('u dont have the correct permissions smh');
if(!args.length) return message.channel.send('ok, but u need to tell me what u want to play');
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();
const stream = ytdl(args[0], {filter: 'audioonly'});
connection.play(stream, {seek: 0, volume: 1})
.on('finish', () =>{
voiceChannel.leave();
message.channel.send('ok fine ill leave')
});
await message.reply(`ok ill play whatever that is`)
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: 0.5})
.on('finish', () =>{
voiceChannel.leave();
});
await message.reply(`ok im now playing ***${video.title}***`)
} else {
message.channel.send('lol i cant find the video ur looking for.')
}
}
}
这是我的 pause.js。我对这个有一点问题,但我花了一些时间和一些搜索来解决它。
name: 'pause',
description: 'makes me pause or whatever',
async execute(message,args, command, client, Discord){
const ytdl = require('ytdl-core');
const stream = ytdl(args[0], {filter: 'audioonly'});
const voiceChannel = message.member.voice.channel;
const connection = await voiceChannel.join();
const streamOptions = {seek:0, volume:1}
DJ = connection.play(stream, streamOptions)
DJ.pause();
}
}
最后,我的 resume.js(它不起作用。)
name: 'resume',
description: 'makes me resume or whatever',
async execute(message,args, command, client, Discord){
const ytdl = require('ytdl-core');
const stream = ytdl(args[0], {filter: 'audioonly'});
const voiceChannel = message.member.voice.channel;
const connection = await voiceChannel.join();
const streamOptions = {seek:0, volume:1}
DJ = connection.play(stream, streamOptions)
DJ.resume();
}
}
我观看了有关如何制作音乐机器人的教程。但是,他们没有提到如何制作播放/暂停命令。每当我运行 resume.js 时,它都会显示错误:未找到视频 ID:未定义。这是我完成音乐机器人之前的最后一个命令(到目前为止)如果有人能回答我,将不胜感激。谢谢!
【问题讨论】:
标签: javascript discord discord.js bots