skip to Main Content

I’m trying to run a bot via Visual Studio Code, my bot works fine for a couple of minutes before completely breaking with an error that idk how to fix.

My Code:

const { Client, GatewayIntentBits} = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
const TOKEN = ':P';
setInterval(function () {
    var User = [
        "User1",
        "User2"
    ]


    client.on('ready', client => {
        const someFunc = () => {
            var randomUser = User[Math.floor(Math.random() * User.length)];
            var randomUser2 = User[Math.floor(Math.random() * User.length)];
            var randomUser3 = User[Math.floor(Math.random() * User.length)];
            var randomUser4 = User[Math.floor(Math.random() * User.length)];

            var Action = [
                { content: (randomUser) + " rembered 😀", files: [{ attachment: "./Image/" + (randomUser) + ".png" }] },
                { content: (randomUser) + " forgor 💀", files: [{ attachment: "./Image/" + (randomUser) + ".png" }] }


            ]

            client.channels.cache.get('1246634621874213007').send(Action[Math.floor(Math.random() * Action.length)]);

            setTimeout(someFunc, 60000);
        }
        setTimeout(someFunc, 60000); 
    }),
    client.login(TOKEN) 
}, 60000);

client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`)
})

The Error:

node:internal/fs/promises:1026
    binding.stat(pathModule.toNamespacedPath(path),
            ^

Error: ENOENT: no such file or directory, stat
    at Object.stat (node:internal/fs/promises:1026:13)
    at DataResolver.resolveFile (J:DesktopFolderR&R Anything 2.0node_modulesdiscord.jssrcutilDataResolver.js:131:30)
    at MessagePayload.resolveFile (J:DesktopFolderR&R Anything 2.0node_modulesdiscord.jssrcstructuresMessagePayload.js:261:54)
    at J:DesktopFolderR&R Anything 2.0node_modulesdiscord.jssrcstructuresMessagePayload.js:225:85
    at Array.map (<anonymous>)
    at MessagePayload.resolveFiles (J:DesktopFolderR&R Anything 2.0node_modulesdiscord.jssrcstructuresMessagePayload.js:225:56)
    at TextChannel.send (J:DesktopFolderR&R Anything 2.0node_modulesdiscord.jssrcstructuresinterfacesTextBasedChannel.js:175:50)
    at Timeout.someFunc [as _onTimeout] (J:DesktopFolderR&R Anything 2.0index.js:136:62)
    at listOnTimeout (node:internal/timers:573:17)
    at process.processTimers (node:internal/timers:514:7) {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'stat'
}

It worked as expected for about 4-5 minutes before throwing an error I don’t understand.

2

Answers


  1. Error: ENOENT: no such file or directory, stat discord.js

    I also got this error multiple times in my case this error will code when I install any new packages when my node server is running I have fixed just restating my noed server.

    Login or Signup to reply.
  2. Move client.login(TOKEN) outside the setInterval function: You should only log in once, not every interval.
    Avoid nesting client.on(‘ready’) inside setInterval: The ready event should be set up once, not repeatedly.
    File existence check: Before sending a file, you should ensure it exists.
    Use client.once for the ready event: This ensures the event is only handled once.

    const { Client, GatewayIntentBits } = require('discord.js');
    const fs = require('fs');
    const path = require('path');
    const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
    const TOKEN = ':P';
    
    // List of users
    const Users = ["User1", "User2"];
    
    client.once('ready', () => {
        console.log(`Logged in as ${client.user.tag}!`);
    
        const sendRandomMessage = () => {
            const randomUser = Users[Math.floor(Math.random() * Users.length)];
            const actions = [
                { content: `${randomUser} remembered 😀`, file: `./Image/${randomUser}.png` },
                { content: `${randomUser} forgor 💀`, file: `./Image/${randomUser}.png` }
            ];
    
            const action = actions[Math.floor(Math.random() * actions.length)];
    
            // Check if file exists before sending
            const filePath = path.resolve(action.file);
            if (fs.existsSync(filePath)) {
                client.channels.cache.get('1246634621874213007').send({
                    content: action.content,
                    files: [{ attachment: filePath }]
                }).catch(console.error);
            } else {
                console.error('File not found:', filePath);
            }
    
            // Set the function to run again in 60 seconds
            setTimeout(sendRandomMessage, 60000);
        };
    
        // Start the interval
        setTimeout(sendRandomMessage, 60000);
    });
    
    client.login(TOKEN);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search