我使用Discord.js在DiscordAPI工作,但我遇到了一个问题。使用XMLHttpRequest时,我在控制台中收到错误消息:
(node:4) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): DiscordAPIError: Cannot send an empty message
这是处理所有内容的代码块:
const imgcommand = ["!g img"];
const imgcommandcut = message.content;
const imgsearchparam = imgcommandcut.replace(/!g img /, "");
const imgsearchcut = imgsearchparam.replace(/ /g, "%20")
var imgparams = imgsearchcut;
if( imgcommand.some(word => message.content.includes(word)) ) {
xhr.open("GET", "https://www.googleapis.com/customsearch/v1?key=AIzaSyBZEJ2dYHtnsn4DJLq6QzXJo4umiHlam5M&cx=017268685753925817424:6rgr_rfrawg&q=" + "?" + imgparams + "&searchType=image&fileType=jpg&imgSize=xlarge&alt=json", false);
xhr.send();
message.channel.send(xhr.response);
}
我已将该链接粘贴到Chrome中并获得了一个JSON文本块,因此链接可以正常工作。有任何想法吗?
谢谢
编辑V3:我已根据@ Blundering的建议更新了代码,这就是我所拥有的
const imgcommand = ["!g img"];
const imgcommandcut = message.content;
const imgsearchparam = imgcommandcut.replace(/!g img /, "");
const imgsearchcut = imgsearchparam.replace(/ /g, "%20")
var imgparams = imgsearchcut;
var xhr = new XMLHttpRequest();
xhr.onload = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
console.log("XHR GET SUCCESSFUL")
} else {
console.log("XHR NOT SUCCESSFUL " + xhr.readyState + "and " + xhr.status)
}
};
xhr.open("GET", 'https://www.googleapis.com/customsearch/v1?key=AIzaSyBZEJ2dYHtnsn4DJLq6QzXJo4umiHlam5M&cx=017268685753925817424:6rgr_rfrawg&q=dog&searchType=image&fileType=jpg&imgSize=xlarge&alt=json', true);
xhr.addEventListener("load", function() {
if( imgcommand.some(word => message.content.includes(word)) ) {
console.log(xhr.reponse)
let msg = xhr.response;
if (!msg) msg == '';
message.channel.send(msg)
.catch(err => {
console.log(err);
});
}
});
xhr.send();
答案 0 :(得分:-1)
首先:解决UnhandledPromiseRejectionWarning
错误:
不和谐函数message.channel.send("<message>")
会返回一个承诺,因此您收到的错误告诉您xhr.response
(您发送到服务器的那个)是空的。
要正确处理此错误(作为Discord请求),请在.catch
函数之后添加.send
调用,如下所示:
message.channel.send(xhr.response)
.catch(err => {
console.log(err);
});
第二:解决DiscordAPIError
:
{ DiscordAPIError: Cannot send an empty message
at item.request.gen.end (/app/node_modules/discord.js/src/client/rest/RequestHandlers/Sequential.js:68:65)
at then (/app/node_modules/snekfetch/src/index.js:218:21)
at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7)
name: 'DiscordAPIError',
message: 'Cannot send an empty message',
code: 50006 }
这是由message.channel.send(...)
功能中发送的空值引起的。您必须在send
函数中放入的值必须可解析为字符串(如文档here中所示),这意味着.send
函数必须能够找出要制作的字符串。您的问题是xhr.response
为空,.send
不知道如何从中创建字符串。为避免错误,您应确保在将消息发送到消息之前填充该变量。像这样:
let msg = xhr.response;
// if there's nothing stored in xhr.response, set msg to empty string
if (!msg) msg = '';
// then you can send msg without worrying about errors
message.channel.send( msg );
第三:要解决xhr.response
为undefined
的问题:
现在您正在进行异步xhr.open
调用,在发送请求后,您不能只获得xhr.response
几行。相反,您必须设置一个侦听器,因此当数据返回时您可以处理它(在xhr.response
或xhr.responseText
中)。
以下是根据文档中的.addEventListener
方法改编的示例:
xhr.open("GET", '<your url>', true);
xhr.addEventListener("load", function() {
if( imgcommand.some(word => message.content.includes(word)) ) {
let msg = xhr.response;
if (!msg) msg == '';
message.channel.send(msg)
.catch(err => {
console.log(err);
});
}
});
xhr.send();