#How to defined set??

1 messages · Page 1 of 1 (latest)

hard aurora
#

you are reading the error wrong

#

the problem is that aliases is undefined

atomic spear
#

oh

#

hmm

atomic spear
#

because now it is foreach...

hard aurora
#

help.aliases is undefined, though is different code compared to the other one

atomic spear
#

i had changed with client.aliases = new Collection(); in my index.js and now in stead of set they say foreach undefined xd. So I am thinking how to fix this

hard aurora
#

well, this is the 3rd problem you're telling me

#

you could use console.log just before suspected code and convince yourself that it is indeed undefined

#

also, if you want to show me more code, post it, don't just talk about it

atomic spear
#
fs.readdirSync('./commands/').forEach(dir => {
    fs.readdir(`./commands/${dir}`, (err, files) => {
        if (err) throw err;

        var jsFiles = files.filter(f => f.split(".").pop() === 'js');

        jsFiles.forEach(file => {
            var fileGet = require(`./commands/${dir}/${file}`);

            try {
                client.commands.set(fileGet.help.name, fileGet);
                fileGet.help.aliases.forEach(alias => {
                    client.aliases.set(alias, fileGet.help.name);
                })
            } catch (err) {
                return console.log(err);
            };
        })
    })
})
#

This is my cmd handler

#
const discord = require("discord.js");

module.exports.run = async (client, message, args) => {

    var botEmbed = new discord.MessageEmbed()
        .setTitle("Botinfo")
        .setColor("#0fbfab")
        .addFields(
            { name: "Bot naam", value: client.user.username },
            { name: "Gemaakt door", value: "@Daniël#5215" },
            { name: "Aantal servers", value: client.guilds.cache.size.toString() },
            // { name: "Aantal leden in alle servers bij elkaar", value:  },
        )

        .setThumbnail("https://cdn.discordapp.com/avatars/730445459159777350/ceeef5f206851463879f667b7ef23c29.png?size=128")
        //.setImage("https://zeroserenity.com/wp-content/uploads/2019/07/Discord-Logo-Red.png")
        .setFooter("Bot is gemaakt door @Daniël#5215") //, "https://zeroserenity.com/wp-content/uploads/2019/07/Discord-Logo-Red.png")
        .setTimestamp();

    return message.channel.send({ embeds: [botEmbed] });
}

module.exports.help = {
    name: "botinfo",
    aliases: ['b'],
}
```This is for example my botinfo.js
#

I still don't know how to fix the error 😦

#

ah I have fixed this

#

Only I have one error more

hard aurora
#

tell it

#

(i was trying to search the problem for the previous one, but you found it)

atomic spear
hard aurora
#

at line 80, dir is botinfo.js, which is not a directory

atomic spear
#

yes

hard aurora
#

i took a fast look at readdirSync, and it DOES NOT returns only directories, but files too

#

you're trying to read a file as a directory, which is wrong

atomic spear
#

oof

#

How to write it then?

hard aurora
#

paste the code here, i'll make for you a snippet

#

the one from the last image

atomic spear
#

oh lol, I think I just have to make a folder in my slashcommands folder....

#

omg

#

yess

#

It works....

hard aurora
#

you do, and you also have to NOT HAVE any files in slashcommands

atomic spear
#
fs.readdirSync('./slashcommands/').forEach(dir => {
    fs.readdir(`./slashcommands/${dir}`, (err, files) => {
        if (err) throw err;

        var jsFiles = files.filter(f => f.split(".").pop() === 'js');

        jsFiles.forEach(file => {
            var fileGet = require(`./slashcommands/${dir}/${file}`);

            try {
                client.SlashCmds.set(fileGet.help.name, fileGet);
            } catch (err) {
                return console.log(err);
            };
        })
    })
})
#

Hmm, but it just works xd

hard aurora
#

you want to just load all the files in that dir right?

atomic spear
#

yes

#

oh I didn't look good

#

just error

hard aurora
#

ok, wait a sec for me to write code

atomic spear
#

yes thx for your time

hard aurora
#
let read_rec = dir => {
  let files = [];
  let x = dir => {
    for (let ent of fs.readdirSync(directory)) {
      let path = dir + "/" + ent;
      if (fs.statSync(path).isDirectory()) x(path);
      else files.push(path);
    }
  };
  x(dir);
  return files;
}

// fs.readdirSync('./slashcommands/').forEach(dir => {
//   fs.readdir(`./slashcommands/${dir}`, (err, files) => {
//       if (err) throw err;

//       var jsFiles = files.filter(f => f.split(".").pop() === 'js');

//       jsFiles.forEach(file => {
//           var fileGet = require(`./slashcommands/${dir}/${file}`);

//           try {
//               client.SlashCmds.set(fileGet.help.name, fileGet);
//           } catch (err) {
//               return console.log(err);
//           };
//       })
//   })
// })


for(let file of read_rec("./slashcommands/")) {
  if(file.split(".").pop() != "js") continue;

  var fileGet = require(file);
  try {
    client.SlashCmds.set(fileGet.help.name, fileGet);
  } catch (err) {return console.log(err);};
}
#

the code is not tested, but you should get the gist

atomic spear
#

I will try 🙂

#

(discord crashed)

#

I will try now

hard aurora
#

replace directory with dir

atomic spear
#

sure

#

It gives this

#

But I think that the problem is my slash commands

#
const { SlashCommandBuilder } = require('@discordjs/builders');
const discord = require("discord.js")

module.exports = {
    data: new SlashCommandBuilder()
        .setName('botinfo')
        .setDescription('Geeft info over de bot.'),
    async execute(client, interaction) {

        var botEmbed = new discord.MessageEmbed()
            .setTitle("Botinfo")
            .setColor("#0fbfab")
            .addFields(
                { name: "Bot naam", value: client.user.username },
                { name: "Gemaakt door", value: "@Daniël#5215" },
                { name: "Bot gemaakt", value: "08-07-2020" }
                // { name: "Aantal servers", value: client.guilds.cache.size.toString() },
                // { name: "Aantal leden in alle servers bij elkaar", value:  },
            )

            .setThumbnail("https://media.discordapp.net/attachments/738372224968425502/936339295457931274/giveaway_pf_maybe.png")
            //.setImage("https://zeroserenity.com/wp-content/uploads/2019/07/Discord-Logo-Red.png")
            .setFooter("Bot is gemaakt door @Daniël#5215") //, "https://zeroserenity.com/wp-content/uploads/2019/07/Discord-Logo-Red.png")
            .setTimestamp();

        return interaction.reply({ embeds: [botEmbed] });
    }
}
hard aurora
#

looks like it

#

there's no module.exports.help

atomic spear
#

I Will fix Tomorrow

atomic spear
#

It gives no error, but when I write / in my server, I don't see any slashcommands

#

@hard aurora

#

this is my slashcommand handler that works, but it is without folders, and I want with folders:

const commandSlashFiles = fs.readdirSync("./slashCommands").filter(file => file.endsWith(".js"));

for (const fileSlash of commandSlashFiles) {

    const commandSlash = require(`./slashCommands/${fileSlash}`);

    client.slashCommands.set(commandSlash.data.name, commandSlash);
    slashCommands.push(commandSlash.data.toJSON());

    console.log(`De file ${commandSlash.data.name}.js is geladen.`)

}
hard aurora
#

use my code bro

#

i literally wrote the code for you

#

my code do handles folders

#

what is in the image, is right, what is in the code block, maybe wrong

hard aurora
#

then what's the deal with the last code block you sent?

atomic spear
#

What do you mean?

hard aurora
#

you sent this:

#
const commandSlashFiles = fs.readdirSync("./slashCommands").filter(file => file.endsWith(".js"));

for (const fileSlash of commandSlashFiles) {

    const commandSlash = require(`./slashCommands/${fileSlash}`);

    client.slashCommands.set(commandSlash.data.name, commandSlash);
    slashCommands.push(commandSlash.data.toJSON());

    console.log(`De file ${commandSlash.data.name}.js is geladen.`)

}
atomic spear
#

Yes because I was trying things because your thing isn't working yet

hard aurora
#

tell me why my thing is not what you're expecting

#

it should handle all the files, no matter what folder they are in slashCommands

atomic spear
#

Me slash commands don't work. When I do / in the server, I see 0 slashcommands, while in my folder slashcommands there are slashcommands that work.

hard aurora
#

after line 91, add a console.log(fileGet) and post here the output

atomic spear
atomic spear
#
[nodemon] starting `node index.js`
{
  data: SlashCommandBuilder {
    options: [],
    name: 'help',
    description: 'Je krijgt alle informatie in je DM.'
  },
  execute: [AsyncFunction: execute]
}
{
  data: SlashCommandBuilder {
    options: [],
    name: 'botinfo',
    description: 'Geeft info over de bot.'
  },
  execute: [AsyncFunction: execute]
}
{
  data: SlashCommandBuilder {
    options: [],
    name: 'invite',
    description: 'Geeft je handige linkjes.'
  },
  execute: [AsyncFunction: execute]
}
Started refreshing application (/) commands.
Successfully reloaded application (/) commands.
Corona Depressie is online.
hard aurora
#

looks like my code works

#

there's some other piece in your project that is not behaving

atomic spear
#

ah nice

#

but

atomic spear
hard aurora
#

it's not my code that is wrong, but the thing you are doing with it

#

currently it is not working because you're doing inside the try:

#
client.SlashCmds.set(fileGet.help.name, fileGet);

(syntax is a bit off cause I didn't have from where to copy-paste)

#

and in the other one you're doing:

#
 client.slashCommands.set(commandSlash.data.name, commandSlash);
 slashCommands.push(commandSlash.data.toJSON());
#

just copy the last 2 lines, and put them inside my code

atomic spear
#

sure!

hard aurora
#

commandSlash should be fileGet

atomic spear
#

I wanted to say that xd

#

omg

#

thank you so much!