0

Working on creating just a simplistic working Discord bot. It is within Discord currently but upon running with node index.js I get the following error:

C:\Users\terra\Desktop\SleepyBot>node index.js
{
  error: Error: ENOENT: no such file or directory, open 'C:\Users\terra\Desktop\SleepyBot\.env'
      at Object.openSync (fs.js:474:3)
      at Object.readFileSync (fs.js:375:35)
      at Object.config (C:\Users\terra\Desktop\SleepyBot\node_modules\dotenv\lib\main.js:96:29)
      at Object.<anonymous> (C:\Users\terra\Desktop\SleepyBot\index.js:1:31)
      at Module._compile (internal/modules/cjs/loader.js:1063:30)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
      at Module.load (internal/modules/cjs/loader.js:928:32)
      at Function.Module._load (internal/modules/cjs/loader.js:769:14)
      at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
      at internal/main/run_main_module.js:17:47 {
    errno: -4058,
    syscall: 'open',
    code: 'ENOENT',
    path: 'C:\\Users\\terra\\Desktop\\SleepyBot\\.env'
  }
}

Index.js

const Discord = require('discord.js');
const bot = new Discord.Client();
const TOKEN = dotenv.env.TOKEN;

bot.login(TOKEN);

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

bot.on('message', msg => {
  if (msg.content === 'ping') {
    msg.reply('pong');
    msg.channel.send('pong');

  } else if (msg.content.startsWith('!kick')) {
    if (msg.mentions.users.size) {
      const taggedUser = msg.mentions.users.first();
      msg.channel.send(`You wanted to kick: ${taggedUser.username}`);
    } else {
      msg.reply('Please tag a valid user!');
    }
  }
});

Pictures for Reference:

My discord bot folder.

Any and all advice is greatly appreciated! I'm new to javascript, but familiar with Java and C++.

  • Welcome to SO :) Please don't post code as images, as they are not accessible. – jake2389 Nov 29 '20 at 20:32
  • I apologize, I was unaware. Is block text the way to go in this instance? – Monkeymurf Nov 29 '20 at 20:34
  • [Here's a link on how to format code blocks](https://meta.stackoverflow.com/questions/251361/how-do-i-format-my-code-blocks) – jake2389 Nov 29 '20 at 20:34
  • The error here is that `dotenv` (used in line 2) is not defined as a variable. You have to declare it. Presumably you're trying to read from the `dotenv.env` file in your directory, but not sure how you're going about it. – jake2389 Nov 29 '20 at 20:35
  • I was following a tutorial from sharepoint which I can link, but I found the code posted to be incorrect so I have been modifying it. How would one declare a dotenv? Also I updated so the code is shown properly thank you :) – Monkeymurf Nov 29 '20 at 20:41

0 Answers0