gpt4 book ai didi

javascript - 如何使基本的 YouTube 音乐机器人能够搜索标题而不是 URL

转载 作者:行者123 更新时间:2023-12-03 05:28:32 25 4
gpt4 key购买 nike

您好,我已按照本教程操作,并将此代码添加到我当前的机器人中,以使其具有音乐机器人功能。我想知道如何使以下代码与 youtube 搜索功能一起使用,例如现在我必须执行 !play URL 但我也希望能够执行 !play name of歌曲,然后机器人将搜索并播放最匹配的歌曲。

我是 javascript 新手,但我知道我不应该寻找讲义,但如果能提供一些帮助,我将不胜感激。

const Discord = require("discord.js");
const { prefix, token } = require("./config.json");
const ytdl = require("ytdl-core");

const client = new Discord.Client();

const queue = new Map();

client.once("ready", () => {
console.log("Ready!");
});

client.once("reconnecting", () => {
console.log("Reconnecting!");
});

client.once("disconnect", () => {
console.log("Disconnect!");
});

client.on("message", async message => {
if (message.author.bot) return;
if (!message.content.startsWith(prefix)) return;

const serverQueue = queue.get(message.guild.id);

if (message.content.startsWith(`${prefix}play`)) {
execute(message, serverQueue);
return;
} else if (message.content.startsWith(`${prefix}skip`)) {
skip(message, serverQueue);
return;
} else if (message.content.startsWith(`${prefix}stop`)) {
stop(message, serverQueue);
return;
} else {
message.channel.send("You need to enter a valid command!");
}
});

async function execute(message, serverQueue) {
const args = message.content.split(" ");

const voiceChannel = message.member.voice.channel;
if (!voiceChannel)
return message.channel.send(
"You need to be in a voice channel to play music!"
);
const permissions = voiceChannel.permissionsFor(message.client.user);
if (!permissions.has("CONNECT") || !permissions.has("SPEAK")) {
return message.channel.send(
"I need the permissions to join and speak in your voice channel!"
);
}

const songInfo = await ytdl.getInfo(args[1]);
const song = {
title: songInfo.title,
url: songInfo.video_url
};

if (!serverQueue) {
const queueContruct = {
textChannel: message.channel,
voiceChannel: voiceChannel,
connection: null,
songs: [],
volume: 5,
playing: true
};

queue.set(message.guild.id, queueContruct);

queueContruct.songs.push(song);

try {
var connection = await voiceChannel.join();
queueContruct.connection = connection;
play(message.guild, queueContruct.songs[0]);
} catch (err) {
console.log(err);
queue.delete(message.guild.id);
return message.channel.send(err);
}
} else {
serverQueue.songs.push(song);
return message.channel.send(`${song.title} has been added to the queue!`);
}
}

function skip(message, serverQueue) {
if (!message.member.voice.channel)
return message.channel.send(
"You have to be in a voice channel to stop the music!"
);
if (!serverQueue)
return message.channel.send("There is no song that I could skip!");
serverQueue.connection.dispatcher.end();
}

function stop(message, serverQueue) {
if (!message.member.voice.channel)
return message.channel.send(
"You have to be in a voice channel to stop the music!"
);
serverQueue.songs = [];
serverQueue.connection.dispatcher.end();
}

function play(guild, song) {
const serverQueue = queue.get(guild.id);
if (!song) {
serverQueue.voiceChannel.leave();
queue.delete(guild.id);
return;
}

const dispatcher = serverQueue.connection
.play(ytdl(song.url))
.on("finish", () => {
serverQueue.songs.shift();
play(guild, serverQueue.songs[0]);
})
.on("error", error => console.error(error));
dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
serverQueue.textChannel.send(`Start playing: **${song.title}**`);
}

client.login(token);

最佳答案

您可以使用yt-search :

const yts = require("yt-search");

// Searches YouTube with the message content (this joins the arguments
// together because songs can have spaces)
const {videos} = await yts(args.slice(1).join(" "));
if (!videos.length) return message.channel.send("No songs were found!");
const song = {
title: videos[0].title,
url: videos[0].url
};

// rest of code...

如果您想同时支持 URL 和搜索,可以使用 ytdl.validateURL 测试第一个参数是否为有效 URL。 :

let song;
if (ytdl.validateURL(args[1])) {
const songInfo = await ytdl.getInfo(args[1]);
song = {
title: songInfo.title,
url: songInfo.video_url
};
} else {
const {videos} = await yts(args.slice(1).join(" "));
if (!videos.length) return message.channel.send("No songs were found!");
song = {
title: videos[0].title,
url: videos[0].url
};
}

// rest of code...

这两个示例都替换

const songInfo = await ytdl.getInfo(args[1]);
const song = {
title: songInfo.title,
url: songInfo.video_url
};

除了 const yts = require("yt-search");,它应该与其他 require 一起放置在顶部。

关于javascript - 如何使基本的 YouTube 音乐机器人能够搜索标题而不是 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61934670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com