NightStrang6r / FunPayServer

Lightweight CLI bot for FunPay / Простой консольный бот для FunPay
Other
174 stars 26 forks source link

Messgae rate limit from funpay #18

Closed WhZzi closed 11 months ago

WhZzi commented 1 year ago

Currently, Auto delivery software sends activation codes one by one, which means when a user purchases 50 activation codes, Auto delivery software needs to send 50 separate messages. This triggers the platform's sending rate limit.

Need a fix for this issue. One possible solution is to group activation codes together when sending them out. For example, when a user requires 50 activation codes, Auto delivery software can send groups of 15 activation codes per message. If the user purchased less than 15 activation codes, they would be sent together in the same group.

This approach will increase the sending efficiency of Auto delivery software and reduce the frequency of messages sent, which will help avoid triggering the platform's sending rate limit.

thanks for fixing!

WhZzi commented 1 year ago

part of Sale.js: ` const groupSize = 15; let groupCount = Math.ceil(order.count / groupSize);

        for(let i = 0; i < groupCount; i++) {
            let groupOrderCount = Math.min(groupSize, order.count - i * groupSize);
            await issueGood(order.buyerId, order.buyerName, order.name, 'id', groupOrderCount);
        }  
        // for(let i = 0; i < order.count; i++) {
        //     await issueGood(order.buyerId, order.buyerName, order.name, 'id');
        // }`

`async function issueGood(buyerIdOrNode, buyerName, goodName, type = 'id',orderCount=1) { let result = false;

try {
    goods = await load(goodsfilePath);
    let messages = [];

    for(let i = 0; i < goods.length; i++) {
        if(goodName.includes(goods[i].name)) {
            for(let j = 0; j < orderCount; j++) {
                if(goods[i].message != undefined) {
                    messages.push(goods[i].message);
                    break;
                } 
                else
                if(goods[i].nodes != undefined) {
                    let notInStock = true;

                    for(let j = 0; j < goods[i].nodes.length; j++) {
                        const node = goods[i].nodes[j];
                        goods[i].nodes.shift();
                        messages.push(node);
                        notInStock = false;
                        break;
                    }

                    if(notInStock) {
                        log(`Похоже, товар "${goodName}" закончился, выдавать нечего.`);
                        return 'notInStock';
                    }
                }
            }
            await updateFile(goods, goodsfilePath);
            break;
        }
    }

    if(messages.length > 0) {
        let node = buyerIdOrNode;
        let customNode = false;

        if(type == 'id') {
            customNode = true;
        }

        result = await sendMessage(node, messages.join('\n'), customNode);

        if(result) {
            log(`Товар "${c.yellowBright(goodName)}" выдан покупателю ${c.yellowBright(buyerName)} с сообщением:`);
            log(messages.join('\n'));

            if(global.telegramBot && settings.deliveryNotification) {
                global.telegramBot.sendDeliveryNotification(buyerName, goodName, messages.join('\n'), node);
            }

        } else {
            log(`Не удалось отправить товар "${goodName}" покупателю ${buyerName}.`, 'r');
        }
    } else {
        log(`Товара "${c.yellowBright(goodName)}" нет в списке автовыдачи, пропускаю.`, 'y');
    }
} catch (err) {
    log(`Ошибка при выдаче товара: ${err}`, 'r');
}

return result;

}` I fixing this function, these are codes, can be used as a reference code, thanks for your time!