简体   繁体   中英

How to make discord bot send scraped message?

I want to make an a discord bot that sends a scraped message. I expected it to send the message but it gave me an error:

throw new DiscordAPIError(request.path, data, request.method, res.status); DiscordAPIError: Cannot send an empty message

I tried to use 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. For more information, see the documentation .

Instead, you can convert data to a string first. 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):

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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