Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

javascript discord bot errors

Hi i am currentely hosting a discord bot with discord.js. I try to save user data with a .json file. Somehow i get a lot of errors. (i am hosting it on heroku).
I am not a native english speaker and have no clue about warnings like this. What does it mean? how do i know which line is affected?

Errors:

node:internal/fs/utils:671
    throw new ERR_INVALID_ARG_TYPE(propName, ['string', 'Buffer', 'URL'], path);
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of Buffer or URL. Received an instance of Object
    at Object.openSync (node:fs:583:10)
    at Object.readFileSync (node:fs:459:35)
    at Object.<anonymous> (C:\Users\User\Documents\GitHub\Squidbot1\index.js:9:20)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Module._load (node:internal/modules/cjs/loader:827:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'ERR_INVALID_ARG_TYPE'
}

Node.js v18.3.0

bot code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const express = require("express");
const app = express();

const discord = require("discord.js");
const client = new discord.Client({intents:["GUILDS","GUILD_MESSAGES"]});

const fs = require("fs");
var data = require("./userdata.json");    
var read_data = fs.readFileSync(data);
var datafile = JSON.parse(read_data);

var bump_timeout = true;

var port = process.env.PORT || 3000;

app.listen(port, "0.0.0.0", function() {
console.log("Listening on Port 3000");
});


client.on("message", message => {
  var msg_value = message.content.toLowerCase();
  var username = message.author.username;
  var id = message.author.id;
  if(msg_value.includes('hello') && msg_value.includes('bot') || msg_value.includes('hi') && msg_value.includes('bot')){
    message.channel.send("hi dad");
  }
  if(message.content === "!d bump" && bump_timeout==true){
    if(!datafile[id]){
      datafile[id] = {coins: 100};
      fs.writeFileSync(data, JSON.stringify(read_data, null, 2));
      message.channel.send(`<@${id}> got 100 coins!`);
    }
    else{
      var chillcoins = Number(datafile.coins) + 100;
      datafile[id] = {coins: chillcoins};
      fs.writeFileSync(data, JSON.stringify(datafile, null, 2));
      message.channel.send(`<@${id}> got ${chillcoins} coins!`);
    };
    bump_timeout = false;
    setTimeout(bump_switchtimeout, 5000);
  }
})

function bump_switchtimeout(){
  bump_timeout = true;
}

client.on("ready", () => {
  console.log("bot is ready");
})


client.login(process.env.token);

can someone explain the error to me?
thx

>Solution :

You are passing a file rather than a path string. Try changing it to:

var read_data = fs.readFileSync("./userdata.json");
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading