【问题标题】:YouTube query function not working for my js bot discordYouTube 查询功能不适用于我的 js bot 不和谐
【发布时间】:2020-09-08 15:07:35
【问题描述】:

我在这个查询部分需要帮助

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);

【问题讨论】:

  • 出了什么问题?你收到错误了吗?实际问题是什么? “我需要帮助”是什么意思?
  • srry 我忘了输入错误,因为我有点忙,我把它添加进去

标签: javascript node.js youtube discord.js


【解决方案1】:

问题在于您的 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}`)
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-19
    • 2021-05-09
    • 2021-08-26
    • 2021-01-01
    • 2012-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多