我的bot命令处理程序工作得不太好

velaa5lx  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(192)

对于我的命令处理程序,这些命令确实有效。但如果我尝试添加参数,比如$user info@user,而不是$user info,它只会说命令无效。
代码

//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);
}
}

这就是所有的代码,我尝试过调试它,但没有找到解决方案,对于我尝试过的调试

if (!client.commands.startsWith(commandName)) return message.channel.send('I don\'t think that\'s a valid command...');

而不是

if (!client.commands.has(commandName)) return message.channel.send('I don\'t think that\'s a valid command...');

但这只是给了我一些错误。

tquggr8v

tquggr8v1#

String.prototype.split()separator 参数可以是字符串或正则表达式。看起来您正试图使用文本形式的正则表达式,因此不能在其周围加引号,否则它会将其视为字符串中要查找的字符序列,而不是模式。这将有助于:

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

相关问题