javascript - YouTube 查询功能不适用于我的 js bot 不和谐
问题描述
我需要有关此查询部分的帮助
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();
const query = command.slice(command.search(' '), command.search('-'));
if (command === 'ping') {
message.channel.send('pong!');
} else if (command === 'joke') {
message.channel.send('Your life');
} else if (command === 'youtube') {
message.channel.send('https://www.youtube.com/results?search_query=' + query);
message.channel.send(query);
} else if (command === 'commands') {
message.channel.send('commands so far are +ping, +joke, +youtube, +commands');
}
});
错误是这样的
(node:15016) UnhandledPromiseRejectionWarning: DiscordAPIError: Cannot send an empty message
at RequestHandler.execute (C:\Users\kalar\Desktop\JavaPr\JS_Bot\node_modules\discord.js\src\rest\RequestHandler.js:170:25)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:15016) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:15016) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我在更改之前就这样做了,我只会发送 YouTube 链接而不是我正在尝试进行的查询。
else if (command === 'youtube') {
message.channel.send('https://www.youtube.com/results?search_query=' + query);
解决方案
问题出在你的query
变量上。
// this is overly complicated and will not work:
const query = command.slice(command.search(' '), command.search('-'));
// instead:
const query = args.join(' ');
此外,在这一行:
message.channel.send('https://www.youtube.com/results?search_query=' + query);
您应该使用该encodeURI()
功能。
message.channel.send(
encodeURI(`https://www.youtube.com/results?search_query=${query}`)
);
推荐阅读
- python - 模块“encodings.utf_8”中的编解码器不兼容
- kubernetes - gitlab-runner 在创建挂载源路径“/usr/share/ca-certificates/mozilla”时出现 kubernetes 集群错误
- server - 如何为 openfire 服务器配置域名?我是否只是购买一个域并将其设置为我的 XMPP 域?
- python - 如何使用 Python 检查用户和密码是否已经在 MySQL 数据库中?
- dns - 结合多个 SPF 记录,如何决定应该使用什么限定符?
- java - 如何在 Spring Boot 中从单个控制器返回内容数据和页面名称
- spring - 如何将属性设置为与 thymeleaf 的会话?
- installation - 以非管理员身份运行 setup.exe 不会引发任何错误
- react-native - 如何在反应原生 webview 中启用浏览图像?
- php - 如何显示表格中的所有记录?