Jacob
DIAdiscord.js - Imagine an app
•Created by Jacob on 2/7/2025 in #djs-questions
Issue wth js
const { token } = require('./config.json');
const {Client, Events, GatewayIntentBits, SlashCommandBuilder, InteractionCallbackResponse } = require('discord.js')
const fs = require('node:fs')
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.commands = getCommands('./commands');
client.once(Events.ClientReady, (c) => {
console.log(`Logged in as ${c.user.tag}`);
});
client.on(Events.InteractionCreate, interaction => {
if(!interaction.isChatInputCommand()) return;
let command = client.commands.get(interaction.commandName);
try{
if(interaction.replied) return;
command.execute(interaction);
} catch (error){
console.error(error);
}
});
client.login(token);
function getCommands(dir) {
let commands = new Collection();
const commandsFiles = getFiles(dir);
for(const commandFile of commandFiles) {
const command = require(commandFile);
commands.set(command.data.toJSON().name, command);
}
return commands;
}
function getFiles(dir) {
const files = fs.readdirStync(dir, {
withFileTypes: true
});
let commandFiles = [];
for(const files of files) {
if(file.isDirectory()){
commandFiles = [
...commandFiles,
...getFiles('${dir}/${file.name}'),
]
} else if (file.name.endsWith(".js")) {
commandFiles.push(`${dir}/${file.name}`);
}
}
return commandFiles}
const { token } = require('./config.json');
const {Client, Events, GatewayIntentBits, SlashCommandBuilder, InteractionCallbackResponse } = require('discord.js')
const fs = require('node:fs')
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.commands = getCommands('./commands');
client.once(Events.ClientReady, (c) => {
console.log(`Logged in as ${c.user.tag}`);
});
client.on(Events.InteractionCreate, interaction => {
if(!interaction.isChatInputCommand()) return;
let command = client.commands.get(interaction.commandName);
try{
if(interaction.replied) return;
command.execute(interaction);
} catch (error){
console.error(error);
}
});
client.login(token);
function getCommands(dir) {
let commands = new Collection();
const commandsFiles = getFiles(dir);
for(const commandFile of commandFiles) {
const command = require(commandFile);
commands.set(command.data.toJSON().name, command);
}
return commands;
}
function getFiles(dir) {
const files = fs.readdirStync(dir, {
withFileTypes: true
});
let commandFiles = [];
for(const files of files) {
if(file.isDirectory()){
commandFiles = [
...commandFiles,
...getFiles('${dir}/${file.name}'),
]
} else if (file.name.endsWith(".js")) {
commandFiles.push(`${dir}/${file.name}`);
}
}
return commandFiles}
33 replies