Ben_10___
Ben_10___

Reputation: 13

My bot command handler is not working that well

For my command handler, the commands do actually work. But if I try adding arguments like $user-info @user instead of $user-info, It just says invalid command.

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 tried debugging it but have not found a solution, for the 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...');

but that just gave me some errors.

Upvotes: 1

Views: 116

Answers (1)

Viriato
Viriato

Reputation: 683

String.prototype.split()'s separator parameter can be a string or a regular expression. It looks like you're trying to use a regular expression in its literal form, so you can't have the quotation marks around it. Otherwise, it will treat it as a string to look for in the message. In your code, you are searching for the string "/ +/" instead of the pattern +. This will work:

const args = message.content.slice(prefix.length).trim().split(/ +/);

Upvotes: 1

Related Questions