#Help a noob

1 messages · Page 1 of 1 (latest)

pine ridge
#

Hi there, I'm someone who recently started coding and don't know how to fix some of the errors I receive. Would you like to help me? Thanks in advance 😄

#

So this is my index:

#
const keepAlive = require(`./server`);
const { Client, Intents, Collection, ClientUser } = require('discord.js');
const client = new Client({ intents: 32767 });
const fs = require('fs'); //fs.readdirSync(`./commands`) must be fs.readdirSync(`./${process.cmd()}/commands`)

client.commands = new Collection();
require('dotenv').config();

const functions = fs.readdirSync(`./functions`).filter(file => file.endsWith(".js"));
const eventFiles = fs.readdirSync(`./events`).filter(file => file.endsWith(".js"));
const commandFolders = fs.readdirSync(`./commands`);

client.on('ready', async () => {
  console.log(`${ClientUser} is now successfully online!`);
  client.user.setPresence({ activities: [{ name: `with Discord.js v16.13.2`, type: `PLAYING` }], status: `dnd` });
  await client.guilds.cache
    .get(process.env['serverId'])
});

(async () => {
  for (file of functions) {
    require(`./functions/${file}`)(client);
  }
  client.handleEvents(eventFiles, "./events");
  client.handleCommands(commandFolders, "./commands");
  client.login(process.env['token']);
})();
keepAlive();
#

This is the server.js file I created:

const express = require('express');
const server = express();

server.all(`/`, (req, res) => {
    res.send(`Result: [OK].`);
});

function keepAlive() {
    server.listen(3000, () => {
        console.log(`Server is now ready! | ` + Date.now());
    });
}

module.exports = keepAlive;
#

handleCommands.js file

const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const fs = require('fs');

const clientId = process.env['botId'];
const guildId = process.env['serverId'];

module.exports = (client) => {
  client.handleCommands = async (commandFolders, path) => {
    client.commandArray = [];
    for (folder of commandFolders) {
      const commandFiles = fs.readdirSync(`./${path}/${folder}`).filter(file => file.endsWith(".js"));
      for (const file of commandFiles) {
        const command = require(`./commands/${folder}/${file}`);
        client.commands.set(command.data.name, command);
        client.commandArray.push(command.data.toJSON());
      }
    }

    const rest = new REST({
      version: '9'
    }).setToken(process.env['token']);

    (async () => {
      try {
        console.log('Started refreshing application (/) commands.');
        await rest.put(
          Routes.applicationGuildCommands(clientId, guildId), {
            body: client.commandArray
          },
        );
        console.log('Successfully reloaded application (/) commands.');
      } catch (error) {
        console.error("Something went wrong: ", error);
      }
    })();
  };
};
#

handleEvents.js file

module.exports = (client) => {
  client.handleEvents = async (eventFiles, path) => {
    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));
      }
    }
  };
}
#

ready.js file

module.exports = {
  name: 'ready',
  once: true,
  async execute() {
    console.log('✔️')
  },
};