I am using discordjs to build a bot and one slashcommand I want to use is a web scraping command to let users know about the latest releases on their favorite platform. The issue at the moment is that I get an error code saying the function "fetch" doesn't exist after importing node-fetch. It works as a script it self but not when used in the discord bot environment.
I would like to know if any of you ran into a similar issue and if so, what was the solution?
Thank you for your time.
const { load } = require("cheerio");
const { fetch } = require("node-fetch");
const fs = require("fs");
module.exports = {
data: new SlashCommandBuilder()
.setName("questnew")
.setDescription("Get Quest releases!"),
async execute(interaction, client) {
try {
const siteUrl = "http://www.vrdb.app/";
const response = await fetch(siteUrl);
const body = await response.text();
const games = [];
const $ = cheerio.load(body);
const elSelector = "#oculus-statistics > tbody > tr";
$(elSelector).each((parentIdx, parentEl) => {
if (parentIdx >= 0 && parentIdx <= 4) {
$(parentEl)
.children()
.each((childIdx, childEl) => {
if (childIdx === 0) {
games.push($(childEl).text());
}
});
}
});
await interaction.reply({
content: "There was an error while executing this command!" games,
ephemeral: true,
});
} catch (error) {
await interaction.reply({
content: "There was an error while executing this command!" error,
ephemeral: true,
});
}
},
};
CodePudding user response:
Wrong syntax
const fetch = require("node-fetch")