简体   繁体   English

如何让 discord 机器人发送抓取的消息?

[英]How to make discord bot send scraped message?

I want to make an a discord bot that sends a scraped message.我想制作一个发送抓取消息的 discord 机器人。 I expected it to send the message but it gave me an error:我希望它发送消息,但它给了我一个错误:

throw new DiscordAPIError(request.path, data, request.method, res.status); throw new DiscordAPIError(request.path, data, request.method, res.status); DiscordAPIError: Cannot send an empty message DiscordAPIError:无法发送空消息

I tried to use message.channel.send();我尝试使用 message.channel.send(); but it doesn't seem to work.但它似乎不起作用。 Code:代码:

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

The problem is that you shouldn't send the dictionary directly.问题是您不应该直接发送字典。 While message.channel.send only accepts StringResolvable or APIMessage , data as a dictionary is neither.虽然message.channel.send只接受StringResolvableAPIMessage ,但作为字典的data都不是。 For more information, see the documentation .有关详细信息,请参阅文档

Instead, you can convert data to a string first.相反,您可以先将data转换为字符串。 The following is one of the solutions.以下是解决方案之一。

// Convert using JSON.stringify
message.channel.send(JSON.stringify(data));

Full code example (I tried it on https://example.com and thus there are different queries):完整代码示例(我在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));

Message sent by the bot without errors being thrown:机器人发送的消息没有抛出错误:

{"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."}

If you expect a different message, just make sure that the argument you are passing to message.channel.send is acceptable or errors might be thrown.如果您希望收到不同的消息,只需确保您传递给message.channel.send的参数是可接受的,否则可能会引发错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM