Home > database >  while loop, specify arguments in any order?
while loop, specify arguments in any order?

Time:02-25

    const logs = [
        "channelCreate",
        "channelDelete",
        "channelPinsUpdate",
        "channelUpdate",
    ]

    let bee = logs.length;

    for (let i = 0; i < bee; i  ) {
        while (args[1] == logs[i]) {
            if (args[1].startsWith("channelCreate")) {
                db.remove_logging(message.guildId, args[1])
                args.splice(1, 1);
            } else if (args[1].startsWith("channelDelete")) {
                db.remove_logging(message.guildId, args[1])
                args.splice(1, 1);
            } else if (args[1].startsWith("channelPinsUpdate")) {
                db.remove_logging(message.guildId, args[1])
                args.splice(1, 1);
            } else if (args[1].startsWith("channelUpdate")) {
                db.remove_logging(message.guildId, args[1])
                args.splice(1, 1);
            } else {
                return message.channel.send({ content: "log typ error" })
            }
        }
    }

I'm trying to build a command, that looks something like this: p!set log channelCreate channelDelete channelPinsUpdate channelUpdate

I can type these arguments channelCreate channelDelete channelPinsUpdate channelUpdate but only in the correct order, so that everything is removed from the database. But I want that it doesn't matter how to specify the arguments, e.g. channelPinsUpdate channelCreate channelUpdate channelDelete

I suspect I am doing something wrong, but unfortunately I don't know what

CodePudding user response:

All you are doing is removing the log from db with same arguments so you can just do this instead:

const logs = [
    "channelCreate",
    "channelDelete",
    "channelPinsUpdate",
    "channelUpdate",
];

if (!logs.includes(args[1])) return message.channel.send({ content: "log typ error" });

db.remove_logging(message.guildId, args[1]);

The code checks for valid log from logs array and if args[1] is invalid, it returns error. Otherwise, it executes remove_logging method of db.

CodePudding user response:

It's the logic of your loops that isn't consistent. You must first loop over the arguments and thus test if each of them is a log and thus execute your deletion. In this way, all the arguments will be compared to the logs and it doesn't matter the order of the arguments. Try this:

for (let i = 1, arg; arg = args[i];   i) {
    if (arg.startsWith("channelCreate")) {
        db.remove_logging(message.guildId, arg)
    } else if (arg.startsWith("channelDelete")) {
        db.remove_logging(message.guildId, arg)
    } else if (arg.startsWith("channelPinsUpdate")) {
        db.remove_logging(message.guildId, arg)
    } else if (arg.startsWith("channelUpdate")) {
        db.remove_logging(message.guildId, arg)
    } else {
        return message.channel.send({ content: "log typ error" })
    }
}
  • Related