Discord.js file error when activating multiple projects - javascript

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');

Related

TypeError: Cannot read properties of undefined (reading 'toJSON') - Discord.js slash commands

I tried making slash commands today on Discord.js, but my console threw some errors that it cannot read the property toJSON what can be the possible solution to it?
Here are my errors:
~/.../stuff/hazy $ node hazy.js
/storage/emulated/0/stuff/hazy/hazy.js:163
commands.push(command.data.toJSON());
^
TypeError: Cannot read properties of undefined (reading 'toJSON')
at Object.<anonymous> (/storage/emulated/0/stuff/hazy/hazy.js:163:30)
at Module._compile (node:internal/modules/cjs/loader:1099:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47
Node.js v17.9.0
Here is my code:
const { REST } = require('#discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { token } = require('./config.json');
const fs = require('node:fs');
const commands = [];
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
// Place your client and guild ids here
const clientId = '913559840780091453';
const guildId = '912552644462121050';
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
commands.push(command.data.toJSON());
}
const rest = new REST({ version: '9' }).setToken(token);
(async () => {
try {
console.log('Started refreshing application (/) commands.');
await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: commands },
);
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
})();
From the error you are receiving, I can see that the data property apparently doesn't exist on the command object. Have you defined/exported it in the respective command file?
Try logging the command.data object to see if anything pops up. That's probably the error.
Replace
commands.push(command.data.toJSON());
with
commands.push(JSON.stringify(command.data));
Thanks to Vincent for helping!

Discord.js client missing intents

I have 2 bots that I'm bringing back from deprecated, one works fine, the other one keeps giving me an error for client missing intents. Thing is I put the intents in there, like the other bot but it just won't work and I don't know why.
A helping hand please?
The bash:
Administrator#ServRemu MINGW64 /c/Bots/Rin-master
$ node index.js
C:\Bots\Rin-master\node_modules\discord.js\src\client\Client.js:544
throw new TypeError('CLIENT_MISSING_INTENTS');
^
TypeError [CLIENT_MISSING_INTENTS]: Valid intents must be provided for the Client.
at Client._validateOptions (C:\Bots\Rin-master\node_modules\←[4mdiscord.js←[24m\src\client\Client.js:544:13)
at new Client (C:\Bots\Rin-master\node_modules\←[4mdiscord.js←[24m\src\client\Client.js:73:10)
at Object.<anonymous> (C:\Bots\Rin-master\index.js:6:23)
←[90m at Module._compile (node:internal/modules/cjs/loader:1101:14)←[39m
←[90m at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)←[39m
←[90m at Module.load (node:internal/modules/cjs/loader:981:32)←[39m
←[90m at Function.Module._load (node:internal/modules/cjs/loader:822:12)←[39m
←[90m at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)←[39m
←[90m at node:internal/main/run_main_module:17:47←[39m {
[←[32mSymbol(code)←[39m]: ←[32m'CLIENT_MISSING_INTENTS'←[39m
}
and here's the whole index.js file:
require('dotenv').config();
const Discord = require('discord.js');
const config = require('./config');
const fs = require('fs');
const Enmap = require("enmap");
const discordClient = new Discord.Client();
const { Client, Intents } = require('discord.js');
const client = new Discord.Client({
intents: [
Discord.Intents.FLAGS.GUILDS,
Discord.Intents.FLAGS.GUILD_MESSAGES,
Discord.Intents.FLAGS.GUILD_MEMBERS,
Discord.Intents.FLAGS.GUILD_PRESENCES,
]
});
fs.readdir("./events/", (err, files) => {
if (err) return console.error(err);
files.forEach(file => {
const event = require(`./events/${file}`);
let eventName = file.split(".")[0];
discordClient.on(eventName, event.bind(null, discordClient));
});
});
discordClient.commands = new Enmap();
fs.readdir("./commands/", (err, files) => {
if (err) return console.error(err);
files.forEach(file => {
if (!file.endsWith(".js")) return;
let props = require(`./commands/${file}`);
let commandName = file.split(".")[0];
discordClient.commands.set(commandName, props);
});
});
discordClient.login(config.discordApiToken);

Require is not a function discord.js

I was coding an event handler (by Codelyon) but I ran into a new error. Here it is
C:\Users\shann\Desktop\DiscordBot\main.js:10
require(`./handlers/${handler}`)(client, Discord);
^
TypeError: require(...) is not a function
at C:\Users\shann\Desktop\DiscordBot\main.js:10:37
at Array.forEach (<anonymous>)
at Object.<anonymous> (C:\Users\shann\Desktop\DiscordBot\main.js:9:37)
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
My main coding is here. (main.js)
const Discord = require('discord.js');
const client = new Discord.Client();
require("dotenv").config();
const fs = require('fs');
client.commands = new Discord.Collection();
client.events = new Discord.Collection();
['command_handler','event_handler'].forEach(handler =>{
require(`./handlers/${handler}`)(client, Discord);
});
client.login(process.env.DISCORD_TOKEN);
I have all the necessary packages (along with others like images-scraper).
For my events, I have message and ready. (Ready tells me when my bot is running and message senses when the commands are said). I also have some commands. Tell me if you need the coding in those.
Here's the coding for my event handler:
const fs = require('fs');
module.exports = (client, Discord) =>{
const load_dir = (dirs) =>{
const event_files = fs.readdirSync(`./events/${dirs}`).filter(file => file.endsWith('.js'));
for(const file of event_files){
const event = require(`../events/${dirs}/${file}`);
const event_name = file.split('.')[0];
client.on(event_name, event.bind(null, Discord, client));
}
}
['client', 'guild'].forEach(e => load_dir(e));
}
And here's my coding for my command handler:
const fs = reqire('fs');
module.exports = (client, Discord) =>{
const command_files = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'))
for(const file of command_files){
const command = require(`../commands/${file}`)
if(command.name){
client.commands.set(command.name, command);
} else {
continue;
}
}
}
For anybody wondering about this question, it was just a typo. I spelled require wrong in my command_handler.

Discord Bot Integration dotenv is not defined

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++.

Im creating a discord bot , and when i try to run it(using the command node . )

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: []
}

Categories

Resources