英文:
Args not being recognized anymore - Discord.js v14
问题
我最近发现当运行一个命令时,我的参数不再被识别了。
如果我不放 args,我的控制台记录为 undefined
,但当我放 args 时它不记录任何东西。
我的命令处理程序:
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);
},
};
如果你对某些部分有困难理解,我愿意解释,所以尽管问吧!还有一些特定要求的定义,但实际上可以忽略它们(如 command....
等等)。
我尝试将命令代码中的 args[0]
(用于调用参数的方法)替换为 args[1]
,但没有变化。
英文:
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.
答案1
得分: 0
使用这个修正后的代码:
const args = message.content.slice(PREFIX.length).trim().split(/ +/);
英文:
You have a space in the regex.
Use this fixed code
const args = message.content.slice(PREFIX.length).trim().split(/ +/);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论