tuhinpal / WhatsBot

Modular Userbot for Whatsapp. Supports MultiDevice authentication on non persistent server.
https://tuhinpal.github.io/WhatsBot/
Apache License 2.0
322 stars 403 forks source link

Command Handler and Code Restructuring #49

Closed AnshumanMahato closed 2 years ago

AnshumanMahato commented 2 years ago

Changelog

Command Handler

Added a Command Handler function in place of the current if-else based command management structure. This makes command handling dynamic and also allows for better code management. Though the current if-else system is functional, it has some drawbacks:

The command handler resolves these issues. It dynamically loads all the command files at runtime from the command folder in a Map.

client.commands = new Map();

fs.readdir("./commands", (err, files) => {
    if (err) 
        return console.error(e);
    files.forEach(commandFile => {
        if(commandFile.endsWith('.js')){
            let commandName = commandFile.replace(".js", "");
            const command = require(`./commands/${commandName}`);
            client.commands.set(commandName,command);
        }
    });
});

When the user gives the command message, the handler separates the commands and the arguments. Then it checks for the command and executes it if available. If it's not, then the user is notified.

client.on('message_create', async msg => {
    if(msg.fromMe && msg.body.startsWith('!')) {
        let args = msg.body.slice(1).trim().split(/ +/g);
        let command = args.shift().toLowerCase();

        console.log({command,args});

        if (client.commands.has(command)) {
            try {
                await client.commands.get(command).execute(client,msg,args);
            } catch (error) {
                console.log(error);
            }
        }

        else {
            await client.sendMessage(msg.to,'No such command found');
            console.log(client.commands);
        }
    }
});

Hence we don't need to update the main.js to add new commands. All we need to do is create a command file and add it to the command folder. We don't need to update the main.js and help.js every time. "main.js" does not require any updating in most cases, and help.js is automatically generated. It makes the system more modular as now commands are separate independent entities. The template for command files is present in the command_template.js

Issues

Note

Previous version of the code is saved in the Legacy Code directory for reference purposes...

tuhinpal commented 2 years ago

Awesome, I will look on it tomorrow. Good job !

tuhinpal commented 2 years ago

@AnshumanMahato Delete everything inside legacy code (include the folder)

AnshumanMahato commented 2 years ago

@cachecleanerjeet sure. Will do it tomorrow 😊

AnshumanMahato commented 2 years ago

@cachecleanerjeet, I removed the Legacy Code directory as was asked by you

@AnshumanMahato Delete everything inside legacy code (include the folder)

tuhinpal commented 2 years ago

@AnshumanMahato Please change the version in package.json (1.5.0 > 2.0.0)

AnshumanMahato commented 2 years ago

@cachecleanerjeet done

AnshumanMahato commented 2 years ago

@cachecleanerjeet