我想制作一个发送废弃消息的discordbot。我本以为它会发送消息,但它给了我一个错误:
抛出新的DiscordAPIError(request.path,data,request.method,res.status);DiscordAPIError:无法发送空消息
我尝试使用message.channel.send();但它似乎不起作用。代码:
let data = await page.evaluate(() => {
let Name = document.querySelector('div[id="title"]').innerText;
let Description = document.querySelector('div[id="content"]').innerText;
return {
Name,
Description
}
});
console.log(data);
message.channel.send(data);
debugger;
await browser.close();
问题是不应该直接发送字典。虽然message.channel.send
只接受StringResolvable
或APIMessage
,但作为字典的data
两者都不接受。有关更多信息,请参阅文档。
相反,您可以先将data
转换为字符串。以下是解决方案之一。
// Convert using JSON.stringify
message.channel.send(JSON.stringify(data));
完整的代码示例(我试过了https://example.com因此有不同的查询):
let data = await page.evaluate(() => {
let Name = document.querySelector('h1').innerText;
let Description = document.querySelector('p').innerText;
return {
Name,
Description
}
});
console.log(data);
message.channel.send(JSON.stringify(data));
机器人程序发送的消息没有抛出错误:
{"Name":"Example Domain","Description":"This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission."}
如果您希望得到不同的消息,请确保传递给message.channel.send
的参数是可接受的,否则可能会引发错误。