I recently found out my arguments are not being recognized anymore when running a command.
If I don't put args my console logs undefined
, but when I put args it doesn't log anything.
My command handler :
const { readdirSync } = require("fs");
module.exports = (client, Discord) => {
const commandFolders = readdirSync(`./Commands/`);
for (const folder of commandFolders) {
const commands = readdirSync(`./Commands/${folder}`).filter((files) =>
files.endsWith(".js")
);
for (const file of commands) {
const command = require(`../Commands/${folder}/${file}`);
client.commands.set(command.name, command);
}
}
};
const {
Client,
Message,
MessageEmbed,
Collection,
PermissionFlagsBits,
} = require("discord.js");
const { PREFIX } = require("../../config.json");
const User = require("../../Models/User");
module.exports = {
name: "messageCreate",
/**
* @param {Client} client
* @param {Message} message
*/
async execute(message, client, Discord) {
if (!message.content.startsWith(PREFIX) || message.author.bot) {
return;
}
const args = message.content.slice(PREFIX.length).trim().split(/ + /);
const commandName = args.shift().toLowerCase();
const command =
client.commands.get(commandName) ||
client.commands.find(
(cmd) => cmd.aliases && cmd.aliases.includes(commandName)
);
if (!commandName) return;
if (!command) return;
if (command.bloxia && message.guild.id !== client.config.BLOXIAID) return;
if (command.developer && !client.config.DEVID.includes(message.author.id))
return message.reply({
content: ":x: This command is only available to developers.",
ephemeral: true,
});
let user = client.userSettings.get(message.author.id);
// If there is no user, create it in the Database as "newUser"
if (!user) {
const findUser = await User.findOne({ Id: message.author.id });
if (!findUser) {
const newUser = await User.create({ Id: message.author.id });
client.userSettings.set(message.author.id, newUser);
user = newUser;
} else return;
}
if (client.maintenanced && !command.maintenancebypass)
return message.reply(":x: Maintenance mode is on !");
if (command.premium && user && !user.isPremium) {
return message.reply({
content: `> \`${message.author.username}\` You are Not Premium User`,
});
}
if (command.owneronly && message.author.id !== client.config.OWNERID)
return message.reply({
content: ":x: This command is only available to the owner.",
ephemeral: true,
});
command.execute(message, args, commandName, client, Discord);
},
};
If you have any problems understanding some part I'm willing to explain so ask away !
There are also a bunch of definitions for certain requirement but ignore them really (if command....
) etc..
I tried replacing args[0]
(the method used to call args) in a command's code with args[1]
but it didn't change.
You have a space in the regex.
Use this fixed code
const args = message.content.slice(PREFIX.length).trim().split(/ +/);