我将如何为我的 discord.js 命令添加参数?
How would i go about adding arguments to my discord.js commands?
向我的 discord 命令添加参数的最佳方式是什么?
例如,禁止{用户名}
任何帮助将不胜感激!
const Client = new Discord.Client({
intents: ["GUILD_MESSAGES", "GUILDS"],
});
const botOptions = {
token: "",
prefix: ['!','.'],
commands: [
{
name: ['joined','j'],
roles: ['@everyone'],
channels: [],
method: (msg) => require('./userJoined')(msg),
},
],
};
Client.on("messageCreate", (msg) => {
const prefix = msg.content.split('')[0];
const commandName = msg.content.split(prefix)[1].toLowerCase();
const checkPrefix = botOptions.prefix.some((x) => x === prefix);
const findCommand = botOptions.commands.find(command => command.name.includes(commandName));
if (typeof findCommand !== 'undefined' && checkPrefix) {
const { name, roles, channels, method } = findCommand;
const rolePermission = msg.member.roles.cache.some(role => roles.includes(role.name.toLowerCase()));
const channelPermission = channels.includes(msg.channelId);
if (channelPermission || channels.length === 0 && rolePermission) {
method(msg);
}
}
});
如果您尝试添加 args(参数),您应该首先将消息拆分为一个数组
let messageArray = message.content.split(" ");
现在你只有一个充满参数的数组,messageArray 变量中的第一个参数基本上是正在执行的命令,之后其余的只是消息参数。
let messageArray = message.content.split(" ");
let cmd = messageArray[0]; // getting the command, this line can be removed
let args = messageArray.slice(1)
在上面的示例中,我们在 messageArray 变量的第 2 个元素处切片,这是消息的第一个参数(不包括 cmd)。
所以这是一个小例子这是如何工作的
!mix 3845345343 4353943
3845345343
正在 args[0]
4353943
正在 args[1]
如果你有更多参数,依此类推
向我的 discord 命令添加参数的最佳方式是什么? 例如,禁止{用户名} 任何帮助将不胜感激!
const Client = new Discord.Client({
intents: ["GUILD_MESSAGES", "GUILDS"],
});
const botOptions = {
token: "",
prefix: ['!','.'],
commands: [
{
name: ['joined','j'],
roles: ['@everyone'],
channels: [],
method: (msg) => require('./userJoined')(msg),
},
],
};
Client.on("messageCreate", (msg) => {
const prefix = msg.content.split('')[0];
const commandName = msg.content.split(prefix)[1].toLowerCase();
const checkPrefix = botOptions.prefix.some((x) => x === prefix);
const findCommand = botOptions.commands.find(command => command.name.includes(commandName));
if (typeof findCommand !== 'undefined' && checkPrefix) {
const { name, roles, channels, method } = findCommand;
const rolePermission = msg.member.roles.cache.some(role => roles.includes(role.name.toLowerCase()));
const channelPermission = channels.includes(msg.channelId);
if (channelPermission || channels.length === 0 && rolePermission) {
method(msg);
}
}
});
如果您尝试添加 args(参数),您应该首先将消息拆分为一个数组
let messageArray = message.content.split(" ");
现在你只有一个充满参数的数组,messageArray 变量中的第一个参数基本上是正在执行的命令,之后其余的只是消息参数。
let messageArray = message.content.split(" ");
let cmd = messageArray[0]; // getting the command, this line can be removed
let args = messageArray.slice(1)
在上面的示例中,我们在 messageArray 变量的第 2 个元素处切片,这是消息的第一个参数(不包括 cmd)。
所以这是一个小例子这是如何工作的
!mix 3845345343 4353943
3845345343
正在 args[0]
4353943
正在 args[1]
如果你有更多参数,依此类推