Handling error?

I guess its an error in my command handler. as No commands will register
26 Replies
d.js toolkit
d.js toolkit•3y ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
a
aOP•3y ago
v14/11 v20 No error
const { run } = require("./util/taskMngr");



client.on("ready", () => {
debug(`Logged in as ${client.user.tag}!`);
run();
});

client.commands = new discord.Collection();

const commandFiles = fs
.readdirSync("./commands")
.filter((file) => file.endsWith(".js"));

for (const file of commandFiles) {
const filePath = path.join(__dirname, "commands", file);
const command = require(filePath);
client.commands.set(command.data.name, command);

if ("data" && "execute" in command) {
debug(`Loaded command ${command.data.name}`);
} else {
debug(`Failed to load command ${command.data.name}`);
}
}

client.on("messageCreate", async (message) => {

if (!message.content.startsWith(process.env.PREFIX) || message.author.bot) return;

const args = message.content
.slice(process.env.PREFIX.length)
.trim()
.split(/ +/);
const command = args.shift().toLowerCase();
if (!command) return;
try {
await command.execute(message);
} catch (error) {
debug(error);
message.reply("there was an error trying to execute that command!");
}
});

client.login(process.env.TOKEN);
const { run } = require("./util/taskMngr");



client.on("ready", () => {
debug(`Logged in as ${client.user.tag}!`);
run();
});

client.commands = new discord.Collection();

const commandFiles = fs
.readdirSync("./commands")
.filter((file) => file.endsWith(".js"));

for (const file of commandFiles) {
const filePath = path.join(__dirname, "commands", file);
const command = require(filePath);
client.commands.set(command.data.name, command);

if ("data" && "execute" in command) {
debug(`Loaded command ${command.data.name}`);
} else {
debug(`Failed to load command ${command.data.name}`);
}
}

client.on("messageCreate", async (message) => {

if (!message.content.startsWith(process.env.PREFIX) || message.author.bot) return;

const args = message.content
.slice(process.env.PREFIX.length)
.trim()
.split(/ +/);
const command = args.shift().toLowerCase();
if (!command) return;
try {
await command.execute(message);
} catch (error) {
debug(error);
message.reply("there was an error trying to execute that command!");
}
});

client.login(process.env.TOKEN);
a
aOP•3y ago
a
aOP•3y ago
initiates commands fine But as when you try to use them, it does nothing Here would be a command example
module.exports = {
data: {
name: 'help',
description: 'Help command',
},
async execute(message, args) {
const { MessageEmbed } = require('discord.js');
const embed = new MessageEmbed()
.setTitle('Help')
.setDescription('This is a list of all the commands')
.addField('claim', 'Claim a username')
.addField('help', 'Help command')

message.channel.send({ embeds: [embed] });
module.exports = {
data: {
name: 'help',
description: 'Help command',
},
async execute(message, args) {
const { MessageEmbed } = require('discord.js');
const embed = new MessageEmbed()
.setTitle('Help')
.setDescription('This is a list of all the commands')
.addField('claim', 'Claim a username')
.addField('help', 'Help command')

message.channel.send({ embeds: [embed] });
Danial
Danial•3y ago
Where are you registering the commands?
a
aOP•3y ago
Inside a folder called commands.
Danial
Danial•3y ago
Do you have a deployment script or something?
a
aOP•3y ago
Its finds the path just fine No, its ran locally Not dockerized or anything
Danial
Danial•3y ago
You're not registering the commands anywhere though, only finding the files in the commands folder and putting them in the client.commands collection
a
aOP•3y ago
does .set not register them?
d.js docs
d.js docs•3y ago
guide Creating Your Bot: Registering slash commands read more
a
aOP•3y ago
Im not using slash. Message commands hence the messageCreate not interaction
Danial
Danial•3y ago
Oh, I see
a
aOP•3y ago
i see no issue with the code but it wont work?
Danial
Danial•3y ago
What do you mean by register though, you don't need to register commands when using message commands By the way, MessageEmbed was renamed to EmbedBuilder in v14
a
aOP•3y ago
As in defining them
Danial
Danial•3y ago
Also, what intents do you have, can you show your client constructor?
a
aOP•3y ago
Should not matter, it is not reading any message I send All, 32057 bitfield I think that should be all anyway
d.js docs
d.js docs•3y ago
We highly recommend only specifying the intents you actually need. - Note, that 98303, 32767 or whatever other magic number you read that represents "all intents", gets outdated as soon as new intents are introduced. - The number will always represent the same set of intents, and will not include new ones. There is no magic "all intents" bit.
Danial
Danial•3y ago
That's not all
a
aOP•3y ago
I swear if that was the reason i was erroring
Danial
Danial•3y ago
Please only have the intents you need, use GatewayIntentBits enum
d.js docs
d.js docs•3y ago
If you aren't getting content, embeds or attachments of a message, make sure you have the MessageContent intent enabled in the Developer Portal and provide it to your client:
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent]
});
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent]
});
Danial
Danial•3y ago
I know but you might wanna fix that as well
a
aOP•3y ago
yeah I will it was an intent error thank you so much
Danial
Danial•3y ago
Of course

Did you find this page helpful?