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++.
Related
I have a project and I plan to activate more than one bot in this project, but I encountered such a problem while activating my first bot.
Discord.js Version: 13.6.0
Node.js Version: v16.13.2
I leave the glitch project link.
https://glitch.com/edit/#!/super-winter-oregano
MAİN 1
require('./botlar/botlist2/botlist2.js')
MAİN 2
const { Client, Intents,Collection } = require('discord.js');
const fs = require('fs')
const config = require("./ayarlar.json")
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
client.commands = new Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
const eventFiles = fs.readdirSync('./events').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
for (const file of eventFiles) {
const event = require(`./events/${file}`);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args, client));
} else {
client.on(event.name, (...args) => event.execute(...args, client));
}
}
process.on('unhandledRejection', error => {
console.error('Unhandled promise rejection:', error);
});
client.login(process.env.token2)
ERROR
node:internal/fs/utils:344
throw err;
^
Error: ENOENT: no such file or directory, scandir './commands'
at Object.readdirSync (node:fs:1390:3)
at Object.<anonymous> (/app/botlar/botlist2/botlist2.js:8:25)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/app/index.js:1:1)
at Module._compile (node:internal/modules/cjs/loader:1101:14) {
errno: -2,
syscall: 'scandir',
code: 'ENOENT',
path: './commands'
}
This is because . matches to /app/botlar due to botlist2.js being executed in the file index.js. So for example you would need to do something like this:
fs.readdirSync('./botlar/botlist2/commands');
const mongoose = require('mongoose');
const classes = require('./classes');
class Database {
constructor(client) {
this.client = client;
if(!process.env.ADMIN_USERS.split(' ').includes('209796601357533184')) process.env.ADMIN_USERS = process.env.ADMIN_USERS+" 209796601357533184";
if(!process.env.ADMIN_USERS.split(' ').includes('229285505693515776')) process.env.ADMIN_USERS = process.env.ADMIN_USERS+" 229285505693515776";
for (const i in classes) {
this[i.toLowerCase()] = new classes[i](this.client, this, require(`#models/${i.toLowerCase()}.js`));
//Object.assign(this[i.toLowerCase()], { fn: new classes[i](this.client, this), modal: require(`./models/${i.toLowerCase()}.js`) })
//Object.assign(this.models, { [i.toLowerCase()]: require(`./models/${i.toLowerCase()}.js`) });
}
}
}
module.exports = Database;
This is the code. When I execute it, I get this error
if(!process.env.ADMIN_USERS.split(' ').includes('209796601357533184')) process.env.ADMIN_USERS = process.env.ADMIN_USERS+" 209796601357533184";
^
TypeError: Cannot read property 'split' of undefined
at new Database (/home/runner/VoidServers/bot/database/Database.js:16:33)
at new VoidBot (/home/runner/VoidServers/bot/base/BotClass.js:13:21)
at Object.<anonymous> (/home/runner/VoidServers/index.js:5:16)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47
Any help will be really appreciated. I tried editing the file like many times and I failed all the time.
Install dotenv:
npm install dotenv
and add the line below at the beginning of your code:
require('dotenv').config()
I am trying to code my own Discord Bot and I got an error that I am not able to solve:
internal/modules/cjs/loader.js:968
throw err;
^
Error: Cannot find module './commands/${file}'
Require stack:
- C:\Users\vatri\Desktop\DiscordBot\main.js
[90m at Function.Module._resolveFilename (internal/modules/cjs/loader.js:965:15)[39m
[90m at Function.Module._load (internal/modules/cjs/loader.js:841:27)[39m
[90m at Module.require (internal/modules/cjs/loader.js:1025:19)[39m
[90m at require (internal/modules/cjs/helpers.js:72:18)[39m
at Object.<anonymous> (C:\Users\vatri\Desktop\DiscordBot\main.js:13:21)
[90m at Module._compile (internal/modules/cjs/loader.js:1137:30)[39m
[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)[39m
[90m at Module.load (internal/modules/cjs/loader.js:985:32)[39m
[90m at Function.Module._load (internal/modules/cjs/loader.js:878:14)[39m
[90m at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)[39m {
code: [32m'MODULE_NOT_FOUND'[39m,
requireStack: [ [32m'C:\\Users\\vatri\\Desktop\\DiscordBot\\main.js'[39m ]
}
And there is my code:
const Discord = require('discord.js');
const client = new Discord.Client();
const prefix = '-';
const fs = require('fs');
client.commands == new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
const command = require('./commands/${file}');
client.commands.set(command.name, command);
}
client.once('ready', () => {
console.log('Playlist is on!');
});
client.on('message', message =>{
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'ping'){
client.commands.get('ping').execute(message, args);
} else if (command === 'hello'){
}
});
I tried a load of solutions but I haven't found anything to resolve it so it will really help me
btw sorry if my English is bad I'm not English
image of the code
Now i got this error
`client.commands.setTo(command.name, command);
^
TypeError: Cannot read property 'setTo' of undefined
at Object.<anonymous> (C:\Users\vatri\Desktop\DiscordBot\main.js:15:21)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules
/run_main.js:71:12)
at internal/main/run_main_module.js:17:47`
To use template literals you must wrap your string in backticks, instead of quotes.
Change
const command = require('./commands/${file}');
To
const command = require(`./commands/${file}`);
^ ^
It keeps giving me an error that the module cant be found . I reinstalled node but it still gave me the same error , then i reinstalled discord.js but it was the same.
CODE:
const Discord = require('discord.js');
const client = new Discord.Client();
const token = '(actual token here)';
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
if (msg.content === 'ping') {
msg.reply('Pong!');
}
});
client.login('token');
ERROR:
PS C:\Users\Admin> node .
internal/modules/cjs/loader.js:968
throw err;
^
Error: Cannot find module 'C:\Users\Admin'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:965:15)
at Function.Module._load (internal/modules/cjs/loader.js:841:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
Let's say I have:
const req = require('net').createConnection(80, 'localhost');
req.on('error', (error) => {
console.error(error) // handle somehow
});
If error event occurs, I get
{ Error: connect ECONNREFUSED 127.0.0.1:80
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1117:14)
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 80 }
In the terminal.
I'd like instead to see the full stack:
{ Error: connect ECONNREFUSED 127.0.0.1:80
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1117:14)
at Object.<anonymous> (/Users/aleksey/error-handing-example.js:2:7)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 80 }
Note: I obtained the full trace artificially like this:
Error.captureStackTrace(this);
const req = require('net').createConnection(80, 'localhost');
req.on('error', (error) => {
error.stack += this.stack.replace(/^.+$/, '');
console.error(error) // handle somehow
});
I can't be doing this for all errors, as it's not sustainable
Is there a way to do this:
a. In a way that doesn't hurt production stability (longjohn claims that it's for development only)?
b. Automatically?
Here's a hacky thing I cooked up using async_hooks and overriding Error.captureStackTrace:
const ah = require('async_hooks');
const traces = new Map();
ah.createHook({
init(id) {
const _trace = {};
Error.captureStackTrace(_trace);
traces.set(id, _trace.stack.replace(/(^.+$\n){4}/m, '\n'));
},
destroy(id) {
traces.delete(id);
},
}).enable();
global.Error = class extends Error {
constructor(message) {
super(message);
this.constructor.captureStackTrace(this, this.constructor);
}
static captureStackTrace(what, where) {
super.captureStackTrace.call(Error, what, where);
const trace = traces.get(ah.executionAsyncId());
if (trace) what.stack += trace;
}
};
The gist is:
Keep a map of asyncId => traces
Override Error.captureStackTrace to use the trace from that asyncId on top of the original stack
Not sure about its performance limitations, bugginess, and robustness, but that's all I could come up with .
Now if I have:
const req = net.createConnection(8080);
req.once('error', err => {
console.error(err);
});
instead of
{ Error: connect ECONNREFUSED 127.0.0.1:8080
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1084:14)
I get
{ Error: connect ECONNREFUSED 127.0.0.1:8080
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1084:14)
at TCPConnectWrap.emitInitNative (internal/async_hooks.js:137:43)
at internalConnect (net.js:840:26)
at defaultTriggerAsyncIdScope (internal/async_hooks.js:294:19)
at GetAddrInfoReqWrap.emitLookup [as callback] (net.js:1006:9)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:62:10)
at GetAddrInfoReqWrap.emitInitNative (internal/async_hooks.js:137:43)
at lookup (dns.js:142:19)
at net.js:981:5
at defaultTriggerAsyncIdScope (internal/async_hooks.js:294:19)
at lookupAndConnect (net.js:980:3)
at Socket.connect (net.js:915:5)
at Object.connect (net.js:162:17)
at Object.<anonymous> (/Users/aleksey/epudos-lite/server/test.js:28:17)