#Wiktors help
1 messages ยท Page 1 of 1 (latest)
ok
yes
any errors?
show me line 14 of bot.js
(async () => {
for (file of functions) {
require(`./functions/${file}`)(client);
}
})()```
I'm new to js, and I want the slash commands working and it's just encounting errors all the time..
I've been doing this command handler for really long, around 4 hours and I can't get to the end of this..
I've been following yt tutorials
(async () => {
for (let i in functions) {
require(`./functions/${functions[i]}`)(client);
}
})()``` try this
and I have been doing oauth2 for whole night
const {
REST
} = require('@discord.js/rest');
const {
Routes
} = require('discord-api-types/v9');
const fs = require('fs');
const clientId = '927300532635312158';
const guildId = '926897125529354281';
module.exports = (client) => {
client.handleCommands = async (commandsFolders, 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(`${path}/${folder}/${file}`);
client.commands.set(command.data.name, command);
client.commandArray.push(command.data.toJSON());
}
}
}
const rest = new REST({
version: '9'
}).setToken(OTI3MzAwNTMyNjM1MzEyMTU4.YdIN6w.UzNhr7btKODQv44uP_PCPfvq3uQ);
(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(error);
}
});
};```
this is the command handler
oh it has my token in there
i'll regenerate my token
Can you console.log functions and show your 2 files in the functions folder
done
I think it's an array, but good idea
ok try this
run it
Wdym?
add a console.log(functions)
Yeah
yea
Does it log functions
Do you guys have any ready code for me to paste and see it
Nope
is __dirname defined?
it is a variable, so did you declare it?
I don't think so
Wait a second
const Discord = require('discord.js');
const { Client, Intents, Collection} = require("discord.js");
const config = require("../config.json")
const fs = require(`fs`);
const bot = new Discord.Client({ intents: [Discord.Intents.FLAGS.GUILDS] })
bot.commands = new Collection();
const functions = fs.readdirSync(__dirname+ '/functions')
const eventFiles = fs.readdirSync(__dirname + "/events")
const commandFolders = fs.readdirSync(__dirname + "/commands")
console.log(functions)
(async () => {
for (let i in functions) {
require(`./functions/${functions[i]}`)(client);
}
})()
bot.handleEvents(eventFiles, "./src/events");
bot.handleCommands(commandFolders, "./src/commands")
bot.login(config.token);````
Add a semicolon after line 11 for a sec
Remove the console.log for a sec
try this```js
jsconst Discord = require('discord.js');
const { Client, Intents, Collection} = require("discord.js");
const config = require("../config.json")
const fs = require(fs);
const bot = new Discord.Client({ intents: [Discord.Intents.FLAGS.GUILDS] })
bot.commands = new Collection();
const functions = fs.readdirSync('./functions')
const eventFiles = fs.readdirSync("./events")
const commandFolders = fs.readdirSync("./commands")
console.log(functions)
(async () => {
for (let i in functions) {
require(./functions/${functions[i]})(client);
}
})()
bot.handleEvents(eventFiles, "./src/events");
bot.handleCommands(commandFolders, "./src/commands")
bot.login(config.token);```
^
Yes your bot doesn't have functions called handleEvents and handleCommands inside it
Hmm?
are they in discord.js?
Yes those are in your functions folder, but not a property of your bot variable
that's the files not the functions
const {
REST
} = require('@discord.js/rest');
const {
Routes
} = require('discord-api-types/v9');
const fs = require('fs');
const clientId = '927300532635312158';
const guildId = '926897125529354281';
module.exports = (client) => {
client.handleCommands = async (commandsFolders, 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(`${path}/${folder}/${file}`);
client.commands.set(command.data.name, command);
client.commandArray.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: client.commandArray
},
);
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
});
};```
thats the handlecommands file
And how do I add the function?
And where
do you know if they are in Discord JS?
You require() the function then call the function with client
or you write your own
I'm new to JS and I don't know how to do it? How do I require the function
Like the cod
e
@untold wharf
remove the bot. infront of them and at the top of the file put const { handleEvents, handleCommands } = require("discord.js") at the top
in what file?
bot.js
so instead of line 1 i put const { handleEvents, handleCommands } = require("discord.js")
yes
A complete guide on How To Make Handlers for Slash Commands, Events, and Functions, for a Discord Bot in Discord.JS v13. This tutorial goes over how to make handlers, with explanation so new Discord.JS coders are able to learn!
Run this command to install the packages needed:
npm install @discordjs/rest discord-api-types @discordjs/builders
๐...
should have just followed the simple copy-pastable tutorial at https://discordjs.guide
https://discordjs.guide/creating-your-bot/#creating-configuration-files it is alot of reading, but it's a quality guide