ResourcesLib
With lib we can manage any resources in bot.

Resource can be

  • balance (in USD, BTC or any other)
  • any game resources: gold, woods, stone, etc
  • etc, any float values

User's resource

1
let res = Libs.ResourcesLib.userRes("money");
2
Bot.sendMessage("Cur your money: " + res.value());
Copied!
Res name is case sensitive. The resources β€œmoney”, β€œMoney” and β€œMONEY” do not match. These are 3 separate resources.
One user can have same chats with bot.
For example: private and group chat.
But anywhere he have simular resources

Chat's resource

1
let res = Libs.ResourcesLib.chatRes("money");
2
Bot.sendMessage("Cur your money: " + res.value());
Copied!
One user can have same chats with bot.
For example: private and group chat.
But he have diffent resources for each chats.
​

Methots for user's and chat resources

All methods can be for user's or chat's resources.
1
// get res
2
let res = Libs.ResourcesLib.userRes("money");
Copied!
res.name - current res name. For example:
1
Libs.ResourcesLib.chatRes("BTC").name // is "BTC"
Copied!

Basic functions

Current res amount

res.value()

Set amount for this res

res.set(amount)
for example: Libs.ResourcesLib.userRes("wood").set(10);

Add amount for this res

res.add(amount)

Res have such amount?

res.have(amount)- if res value equal amount or more return true

Take away amount from resource

res.remove(amount) - if have it res.removeAnyway(amount) - take away amount anyway.

Access to another resources

Access to another user's resources

1
// telegramid - it is telegram id for another user
2
let res = Libs.ResourcesLib.anotherUserRes("money", telegramid);
3
Bot.sendMessage("Cur your money: " + res.value());
Copied!

Access to another chat's resources

1
// another chat's resources
2
// chatid - it is telegram id for another chat
3
let res = Libs.ResourcesLib.anotherChatRes("money", chatid);
4
Bot.sendMessage("Cur your money: " + res.value());
Copied!
​

Resource transfering

1
let res = Libs.ResourcesLib.userRes("gold");
2
// telegramid - it is telegram id for another user
3
let anotherRes = Libs.ResourcesLib.anotherUserRes("gold", telegramid);
Copied!

If have resource...

1
res.takeFromAnother(anotherRes, amount);
2
res.transferTo(anotherRes, amount)
Copied!

...or anyway, even resource is not enough

1
res.takeFromAnotherAnyway(anotherRes, amount)
2
res.transferToAnyway(anotherRes, amount)
Copied!
​

Can exchange different resources

For example "gold" for "wood":
res.exchangeTo(anotherRes, { remove_amount: 10, add_amount:23 } )
​

Growth for resource.

Resource can have growth.
For example simple growth:
add 5 every 10 secs to res
1
let health = Libs.ResourcesLib.userRes("health");
2
health.set(1);
3
health.growth.add({value: 5, interval:10 });
Copied!
Interval - it is value in seconds. Value is added every interval

Add 5 every hour with max value 100.

1
//Max value: 100
2
let secs_in_hour = 1 * 60 * 60;
3
health.growth.add({
4
value: 5,
5
interval: secs_in_hour,
6
max: 100
7
});
Copied!

Value can be negative. Remove 5 every 30 hours.

1
//Min value: -20
2
let secs_in_30hours = 1 * 60 * 60 * 30;
3
health.growth.add({
4
value: -5, // just add negative value
5
interval: secs_in_30hours,
6
min: -20
7
});
Copied!
​

Can limit max iteration count

1
health.growth.add(
2
{value: 5,
3
interval: secs_in_30hours,
4
max_iterations_count: 3
5
});
Copied!

Can growh by percent.

For example add 15% every month for 100 USD
1
let usd = Libs.ResourcesLib.userRes("usd");
2
usd.set(100);
3
let secs_in_month = 60 * 60 * 24 * 31;
4
usd.growth.addPercent({
5
value: 15,
6
interval: secs_in_month
7
});
Copied!
​

Can grow by compound interest.

For example add 0.8% every day for 0.5 BTC with reinvest
1
let btc = Libs.ResourcesLib.userRes("BTC");
2
btc.set(0.5);
3
let secs_in_day = 1 * 60 * 60 * 24;
4
usd.growth.addCompoundInterest({
5
value: 0.8,
6
interval: secs_in_day
7
});
Copied!
You can get initial res value by: res.baseValue()

Other methods for res.growth:

res.growth.info() - get info for current growth
res.growth.title() - get title. For example "add 5 once at 15 secs"
res.growth.isEnabled() - return true if is enabled
res.growth.stop() - stop growth
res.growth.progress() - current progress for next iteration
res.growth.willCompletedAfter() - will completed iteration after this time in seconds

​

How to add growth to another resources?

For example we have:
  • bank deposit 100$ with yearly growth 10%
  • and simple wallet - 500$
Every year we add bank growth to wallet.

Init: on /start command (or any other command)

1
let wallet = Libs.ResourcesLib.userRes("wallet");
2
wallet.set(500);
3
​
4
let bankDeposit = Libs.ResourcesLib.userRes("deposit");
5
bankDeposit.set(100);
6
let secs_in_year = 1 * 60 * 60 * 24 * 365;
7
​
8
bankDeposit.growth.addPercent({
9
value: 10,
10
interval: secs_in_year
11
});
Copied!

On /wallet command or etc

We can run this command every 1 year. It is possible for example, with Auto Retry​
Or user can run it manually in anytime.
1
let wallet = Libs.ResourcesLib.userRes("wallet");
2
let bankDeposit = Libs.ResourcesLib.userRes("deposit");
3
​
4
// it is initial res value
5
let baseValue = bankDeposit.baseValue();
6
​
7
// total income by percent
8
let delta = bankDeposit.value() - baseValue;
9
​
10
// add all income to wallet
11
wallet.add(delta);
12
// and remove it from bank deposit
13
bankDeposit.set(baseValue);
Copied!
​
​

How to

Q: How to give to referrer 5% of referral user deposit?

​

Q: How to give a bonus to all users every day?

For example add 10 to user's balance every day
Command /start
1
let balance = Libs.ResourcesLib.userRes("balance");
2
balance.set(0);
3
​
4
Bot.run( {
5
command: "/addBonus",
6
run_after: 1*60*60*24, // add bonus after 1 day
7
} )
Copied!
Command /addBonus
1
if(request){
2
// user can not run this command manually
3
Bot.sendMessage("Restricted!")
4
return
5
}
6
​
7
let balance = Libs.ResourcesLib.userRes("balance");
8
balance.add(10);
9
​
10
// and repeat this command again after 1 day
11
Bot.run( {
12
command: "/addBonus",
13
run_after: 1*60*60*24, // after one day
14
} )
15
​
16
Bot.sendMessage("Bonus for you: 10")
Copied!
Command /addBonus will be executed for each user. It spend 1 iteration every day for each user.
For example, for 100 user - it will be 100 iterations per day.

​

Q: How to make time limit bonus?

Use Cooldown Lib! It is more simple!
For example, user can get bonus once at 5 hours.
Command /bonus
1
let bonusCooldown = Libs.ResourcesLib.userRes("bonusCooldown");
2
​
3
// cooldown in seconds
4
let totalCooldown = 60*60*5; // 60*60*5 seconds = 5 hours
5
​
6
function resetCooldown(){
7
bonusCooldown.set(totalCooldown);
8
}
9
​
10
function setupCooldown(){
11
if(bonusCooldown.growth.isEnabled()){
12
// already setupped
13
return
14
}
15
16
bonusCooldown.growth.add({
17
value: -1, // just add negative value
18
interval: 1, // -1 once at 1 sec
19
min: 0
20
});
21
}
22
​
23
setupCooldown();
24
​
25
if(bonusCooldown.value() > 0){
26
Bot.sendMessage("Please wait: " + bonusCooldown.value() + " secs" );
27
return
28
}
29
30
// can give bonus now
31
resetCooldown(); // need to reset cooldown
32
​
33
Bot.sendMessage("You have bonus now");
34
// your other code here
35
//..
36
​
Copied!
​
Last modified 2mo ago