UNPKG

rgrefer

Version:

An easy to use referral system implmentation library for grammy

55 lines (50 loc) 2.29 kB
const { Context, NextFunction } = require("grammy"); /** * This library is a middleware for the grammy bot library. It allows you to create a referral system for your bot. * Simple to use and easy to understand. * Straight to the point. */ /** * The main function that creates the middleware for the referral system * @param {object} options The options for the referral system * @param {integer} options.referrerBonus The amount of money the referrer gets when someone uses their referral link * @param {integer} options.referreeBonus The amount of money the referree gets when they use a referral link * @param {integer} options.startupBonus The amount of money the user gets when they start the bot * @param {function} options.userExists A function that checks if a user exists in the database * @param {function} options.addBalance A function that adds balance to a user * @param {function} options.onReferral A function that is called when a user uses a referral link * @param {function} options.onStartUp A function that is called when a user starts the bot * * @returns {function(ctx, next): void} The middleware function that can be used in the bot */ function refer(options = { referrerBonus: 0, referreeBonus: 0, startupBonus: 0, userExists: (id), addBalance: (id, amount), onReferral: (referree, ctx), onStartUp: (ctx) }) { const middlewareFn = async (ctx, next) => { if (!ctx.hasChatType("private")) return await next() if (!ctx.hasCommand("start")) return await next() if (await options.userExists(ctx.from?.id)) { await next() return; } else { await options.onStartUp(ctx); } const referrer = parseInt(ctx.msg?.text?.split(" ")[1] || ""); if (isNaN(referrer) || !await options.userExists(referrer)) { await options.addBalance(ctx.from?.id, options.startupBonus); } else { await options.addBalance(referrer, options.referrerBonus); await options.onReferral(referrer, ctx); await options.addBalance(referrer, options.referreeBonus); } await next() } return middlewareFn; } module.exports = refer;