对于我的命令处理程序,命令确实有效。但是如果我尝试添加像 $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...');
但这只是给了我一些错误。
答案 0 :(得分:1)
String.prototype.split()
的 separator
参数可以是字符串或正则表达式。看起来您正在尝试以其文字形式使用正则表达式,因此您不能在其周围加上引号。否则,它会将其视为要在消息中查找的 字符串。在您的代码中,您正在搜索字符串 "/ +/"
而不是模式 +
。这将起作用:
const args = message.content.slice(prefix.length).trim().split(/ +/);