njmango / sales-bot

0 stars 2 forks source link

modularise each command #1

Closed mcmcford closed 2 months ago

mcmcford commented 3 months ago

Currently all commands are handled in the client.on('messageCreate') event. This works perfectly fine but the method is nearly 300 lines long making it hard to quickly glance at or make modifications to.

A solution to this is having it check the msg for a given string if (content.startsWith('!edit')) then rather than having all the processing directly below that if/else statement, move it over to its own function. To clean this up even further, functions can be moved to their own files but for a simple bot like this, that is a bit excessive.

it should result in the messageCreate event looking something like this:

client.on('messageCreate', async (msg) => {
    if (msg.author.bot) return;
    const content = msg.content.toLowerCase();
    console.log("Message received:", content);

    if (content.startsWith('!help')) {
        helpCommand(msg);
    } else if (content.startsWith('!edit')) {
        editCommand(msg);
    } else if (content.startsWith('!sell') || content.startsWith('!buy')) {
        buyingSellingCommand(msg);
    } else if (['showlist', 'show', 'list'].includes(content.split(' ')[0].slice(1))) {
        await publishLists(msg.channel.id);
    } else if (content.startsWith('!clear')) {
        clearCommand(msg);
    } else  if (content.startsWith('!insert')) {
        insertCommand(msg);
    } else if (content.startsWith('!delete')) {
        deleteCommand(msg);
    } else if (content.startsWith('!price.all')) {
        priceAllCommand(msg);
    } else if (content.startsWith('!price.')) {
        specificPriceCommand(msg);
});

With all the appropriate async calls as needed etc. This will also make moving each command over to slash commands a little easier as they'll be split up into their own methods already

prithu7872 commented 3 months ago

Hey , I am really interested to work in this problem , @mcmcford can you please allow me ...

mcmcford commented 3 months ago

@prithu7872 Of course, njmango is using the repo as a way to get familiar with git and js so can you do me a favour - create a branch or a fork depending on what permissions you have, comment it well and then go head with the pull request so OP can see how they work

njmango commented 2 months ago

Fixed in current version.