ENOENT: no such file or directory, scandir './events/client' - javascript

so this is my code
//event handler
exports.run = (client, msg, args) => {
const prefix = '-';
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
const command = client.commands.get(cmd);
if(command) command.execute(client, message, cmd. args, Discord);
}
if(command === 'pula'){
client.commands.get('baned').execute(message, args);
}
//command handler
const fs = require('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;
}
}
}
//message.js
exports.run = (client, msg, args) => {
const prefix = '-'; ``
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
const command = client.commands.get(cmd);
if(command) command.execute(client, message, cmd. args, Discord);
}
}
//ready.js
module.exports = () => {
console.log('S-a Trezit Lucreita!');
`
And it just gaves me that error, i dont really know what to do, i beg you to help me!I searched on formus, did everything they said and it didnt work.I just tryed everything, i dont have even a clue, im just a noobie being honest and i hope that someone might help me

Related

I'm trying to create a discord bot with commands that only work with the right permissions. When I tried it, I came across an erorr

if(command.permissions.length){
let invalidPerms = []
for(const perm of command.permissions){
if(!validPermissions.includes(perm)){
return console.log(`Invalid Permissions ${perm}`);
}
if(!message.member.hasPermission(perm)){
invalidPerms.push(perm);
break;
}
}
if (invalidPerms.length){
return message.channel.send(`Missing Permissions: \`${invalidPerms}\``);
}
}
Error: ReferenceError: command is not defined Can anyone tell me why I'm getting this error? I checked all the code I have and it seems right.
main.js Code
const Discord = require('discord.js');
const client = new Discord.Client({intents: ["GUILDS","GUILD_MESSAGES"]});
const prefix = '-';
const fs = require('fs');
const event_handler = require('./handlers/event_handler');
client.commands = new Discord.Collection();
client.events = new Discord.Collection();
['command_handler', 'event_handler'].forEach(handler =>{
require(`./handlers/${handler}`)(client, Discord);
})
client.login('Discord Login ID');
event handler code
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));
}
command handler code
const fs = require('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;
}
}
}
message guild code
module.exports = (Discord, client, message) =>{
const prefix = '-';
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const cmd= args.shift().toLowerCase();
const command = client.commands.get(cmd);
if(command) command.execute(client, message, args, Discord);
}
^Thats with the command permissions
Added more of my code. Can anyone find the error? All of the code looks right, I checked again and all spelling is correct. You could check again but I think the it has to do with the code and not the spelling.
Your issue is that command is defined inside the module export, and cannot be referenced in that top bit of code.

Aliases - discord.js

Well, I have a doubt. I want to put "aliases" in my commands in discord.js, but I am stuck without knowing what to do. Help me if something is missing, please.
This is my index.js code
bot.commands = new Discord.Collection();
const commandFolders = fs.readdirSync('./commands')
for (const folder of commandFolders) {
const commandFiles = fs.readdirSync(`./commands/${folder}`).filter(file => file.endsWith('.js'))
for (const file of commandFiles) {
const command = require(`./commands/${folder}/${file}`)
bot.commands.set(command.name, command)
}
}
code 2:
bot.on('message', async (message) => {
if(!message.content.startsWith(process.env.PREFIX) || message.author.bot) return;
if (message.channel.type === 'dm') return;
const args = message.content.slice(process.env.PREFIX.length).split(' ');
const command = args.shift().toLowerCase();
try {
bot.commands.get(command).execute(bot, message, args)
} catch (e) {
message.channel.send('Utilize ``' + `${process.env.PREFIX}` + 'help`` para ver meus comandos.')
.then(message => setTimeout(() => message.delete(), 10000))
}
});
This is the part of the code of the commands, I put the aliases there but it is not working.
const execute = async (bot, message, args) => {
//code
};
module.exports ={
name: "ban",
aliases: ['b', 'banir'],
execute,
};
You need to check client.commands for a module that either has the name of args.shift().toLowerCase() or it's aliases array contains said argument
bot.on('message', async (message) => {
if(!message.content.startsWith(process.env.PREFIX) || message.author.bot) return;
if (message.channel.type === 'dm') return;
const args = message.content.slice(process.env.PREFIX.length).split(' ');
// Aliases Implementing starts here
const commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName) || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
if (!command) return;
try {
bot.commands.get(command).execute(bot, message, args)
} catch (e) {
message.channel.send('Utilize ``' + `${process.env.PREFIX}` + 'help`` para ver meus comandos.')
.then(message => setTimeout(() => message.delete(), 10000))
}
});
Here's an official aliases guide by Discordjs.guide
Simply register your aliases for a command along with the command name, like this:
for (const folder of commandFolders) {
const commandFiles = fs.readdirSync(`./commands/${folder}`).filter(file => file.endsWith('.js'))
for (const file of commandFiles) {
const command = require(`./commands/${folder}/${file}`)
bot.commands.set(command.name, command)
// Register aliases
for (let alias of command.aliases) {
bot.commands.set(alias, command)
}
}
}

const commandFolders = readdirSync('./commands'); ReferenceError: Cannot access 'readdirSync' before initialization at Object.<anonymous> error

I got this error today trying to change a little bit of my command handler from an youtube video.The video is from april and it seems to work for the guy but for me it doesnt.I tried many things but i didnt get any clue.Im not pretty good with this but i want to have an start like many others and this comunity is so good an helps me so much, thank you so much for the support you offer to me!!I hope i will get past this problem.
const Timeout = new Discord.Collection();
const bot = new Discord.Client();
const prefix = '$';
bot.commands = new Discord.Collection();
const commandFolders = readdirSync('./commands');
const ms = require('ms');
const {readdirSync , read} = require('fs');
for(const folder of commandsFolders){
const commandFiles = readdirSync(`./commands/${folder}`).filter(file => file.endsWith('.js'));
for(const file of commandFiles){
const command = require(`./commands/${folder}/${files}`);
bot.commands.set(command.name, command);
}
}
bot.on("error" , console.error);
bot.once('ready' , () => {
console.log('M-am trezit din morti!');
});
bot.on("message" , async (message) =>{
if(message.author.bot) return;
if(message.channel.type === 'dm') return;
if(message.content.startsWith(prefix)){
const args = message.content.slice(prefix.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
const command = bot.command.get(commandName) || bot.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
if(!command) return;
if (command) {
if(command.cooldown){
if(Timeout.has(`${command.name}${message.author.id}`)) return message.channel.send(`Te rog asteapta! \`${ms(Timeout.get (`${command.name}${message.author.id}`) - Date.now(), {long: true})}\`Pana folosesti comanda din nou!`);
command.run(bot, message, args)
Timeout.set(`${command.name}${message.author.id}` , Date.now() + command.cooldown)
setTimeout(() =>{
Timeout.delete(`${coomand.name}${message.author.id}`)
}, command.cooldown)
} else command.run(bot, message, args);
}
}
})
So what you did wrong is, that you want to use readdirSync before you included the fs library in your code. Just move the require statement over the readdirSync and it should work:
const bot = new Discord.Client();
const prefix = '$';
const {readdirSync , read} = require('fs');
bot.commands = new Discord.Collection();
const commandFolders = readdirSync('./commands');
const ms = require('ms');
for(const folder of commandsFolders){
const commandFiles = readdirSync(`./commands/${folder}`).filter(file => file.endsWith('.js'));
for(const file of commandFiles){
const command = require(`./commands/${folder}/${files}`);
bot.commands.set(command.name, command);
}
}
bot.on("error" , console.error);
bot.once('ready' , () => {
console.log('M-am trezit din morti!');
});
bot.on("message" , async (message) =>{
if(message.author.bot) return;
if(message.channel.type === 'dm') return;
if(message.content.startsWith(prefix)){
const args = message.content.slice(prefix.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
const command = bot.command.get(commandName) || bot.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
if(!command) return;
if (command) {
if(command.cooldown){
if(Timeout.has(`${command.name}${message.author.id}`)) return message.channel.send(`Te rog asteapta! \`${ms(Timeout.get (`${command.name}${message.author.id}`) - Date.now(), {long: true})}\`Pana folosesti comanda din nou!`);
command.run(bot, message, args)
Timeout.set(`${command.name}${message.author.id}` , Date.now() + command.cooldown)
setTimeout(() =>{
Timeout.delete(`${coomand.name}${message.author.id}`)
}, command.cooldown)
} else command.run(bot, message, args);
}
}
})

SyntaxError: illlegal return statement [Discord.js]

The following line throws the error:
if (!client.commands.has(command)) return;
I'm following this tutorial= Tutorial
const fs = require('fs');
const Discord = require('discord.js');
const { prefix, token } = require("./config.json")
const client = new Discord.Client();
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.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (message.content === `${prefix}serverinfo`) {
message.channel.send(message.guild.name)
message.channel.send(`Total Members: ${message.guild.memberCount}`)
} else if (message.content === `${prefix}me`) {
message.channel.send(`Username: ${message.author.username}`)
message.channel.send(`ID: ${message.author.id}`)
} else if (message.content === `${prefix}boi`) {
message.channel.send('BOI')
}
});
if (!client.commands.has(command)) return;
You can't return in the top scope, return must be inside a function.
You may either put the logic inside an if statement
if (client.commands.has(command)) {
const command = client.commands.get(command);
try {
command.execute(message, args);
} catch (error) {
console.error(error);
message.reply("There was an issue executing that command!")
}
client.login(token);
}
Or wrap the logic inside a function, an IIFE may be a good choice:
(() => {
if (!client.commands.has(command)) return;
const command = client.commands.get(command);
try {
command.execute(message, args);
} catch (error) {
console.error(error);
message.reply("There was an issue executing that command!")
}
client.login(token);
})();

Cannot read property 'execute' of undefined - discord

I am coding a discord bot. The commands I'm having issues with is temprole and kick.
The ping command seems to work fine.
main.js
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('Bot is online!');
});
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 == 'temprole') {
client.commands.get('temprole').execute(message, args);
}
});
client.login('token')
temprole.js
module.exports = {
name: 'youtube',
description: "give the member a temprole",
execute(message, args) {
if (message.member.roles.cache.has('798180573998612512')) {
} else {
message.channel.send('You dont have access to this command!')
}
}
}
kick.js
module.exports = {
name: 'kick',
description: "This command kicks a member!",
execute(messages, args) {
const member = message.mentions.users.first();
if (member) {
const memberTarger = message.guild.members.cache.get(member.id);
memberTarger.kick();
message.channel, send('User has been kicked');
} else {
message.channel.send('You coundt kick that member');
}
}
}
In your temprole.js, the "name" property does not match temprole, therefore the commands collection will set the command name as "youtube". You then try to get the "temprole" command from the collection but there aren't any. The fix is to change your command name's property to "temprole".

Categories