简体   繁体   中英

How would i go about adding arguments to my discord.js commands?

What would be the best way to add arguments to my discord commands? eg, !ban {username} Any help would be greatly appreciated!

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

If you're trying to add args (arguments) you should first split the message into an array

    let messageArray = message.content.split(" ");

Now you just have an array full of arguments, the first argument in the messageArray variable is basically the command which is being executed, after that the rest are just the message args.

    let messageArray = message.content.split(" ");
    let cmd = messageArray[0]; // getting the command, this line can be removed
    let args = messageArray.slice(1)

In the example above we sliced the messageArray variable at its 2nd element which is the first argument of the message (not including the cmd).

So here is a small example how this works !mix 3845345343 4353943

3845345343 being args[0]
4353943 being args[1]

and so on if you have more args

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM