When it comes to my command handler, the commands function properly. However, when I attempt to include arguments like $user-info @user instead of just $user-info, it returns an error stating that the command is invalid.
Code
//handler
const prefix = '$';
const fs = require('fs');
const { args } = require('./commands/utility/user-info');
client.commands = new Discord.Collection();
const commandFolders = fs.readdirSync('./commands');
for (const folder of commandFolders) {
const commandFiles = fs.readdirSync(`./commands/${folder}`).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${folder}/${file}`);
client.commands.set(command.name, command);
}
}
client.on('message', message => {
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);
if (!client.commands.has(commandName)) return message.channel.send('I don\'t think that\'s a valid command...');
if (command.args && !args.length) {
return message.channel.send(`You didn't provide any arguments!`);
}
if (command.guildOnly && message.channel.type === 'dm') {
return message.reply('I can\'t execute that command inside DMs!');
}
try {
client.commands.get(commandName).execute(message, args);
} catch (error) {
console.error(error);
message.reply('there was an error trying to execute that command!');
}
});
//user-info
const { MessageEmbed } = require("discord.js");
module.exports = {
name: "user-info",
description: "Returns user information",
args: true,
async execute(message, args){
let member = await message.mentions.members.first() || message.guild.members.cache.get(args[0]) || message.guild.members.cache.find(r => r.user.username.toLowerCase() === args.join(' ').toLocaleLowerCase()) || message.guild.members.cache.find(r => r.displayName.toLowerCase() === args.join(' ').toLocaleLowerCase()) || message.member;
if(!member)
return message.channel.send("**Enter A Valid User!**");
const joined = formatDate(member.joinedAt);
const roles = member.roles.cache
.filter(r => r.id !== message.guild.id)
.map(r => r.name).join(", ") || 'none';
const created = formatDate(member.user.createdAt);
const embed = new MessageEmbed()
.setTitle("User Info")
.setFooter(message.guild.name, message.guild.iconURL())
.setThumbnail(member.user.displayAvatarURL({ dynamic: true}))
.setColor("GREEN")
.addField("**User information**", `${member.displayName}`)
.addField("**ID**", `${member.user.id}`)
.addField("**Username**",`${member.user.username}`)
.addField("**Tag**", `${member.user.tag}`)
.addField("**Created at**", `${created}`)
.addField("**Joined at**", `${joined}`)
.addField("**Roles**", `${roles}`, true)
.setTimestamp()
member.presence.activities.forEach((activity) => {
if (activity.type === 'PLAYING') {
embed.addField('Currently playing',`\n**${activity.name}**`)
}
})
message.channel.send(embed);
}
}
That's all the code. I have attempted to debug it but haven't found a solution yet. For debugging, I tried:
if (!client.commands.startsWith(commandName)) return message.channel.send('I don't think that's a valid command...');
Instead of:
if (!client.commands.has(commandName)) return message.channel.send('I don't think that's a valid command...');
However, this resulted in some errors.