make patron rewards automatic in WPD

master
Aevann 2023-01-23 10:42:38 +02:00
parent e18b5fd29d
commit d34f836052
2 changed files with 44 additions and 57 deletions

View File

@ -42,9 +42,8 @@ def cron(every_5m, every_1h, every_1d, every_1mo):
site_stats = stats.stats(SITE_NAME)
cache.set(f'{SITE}_stats', site_stats)
if every_1mo:
if KOFI_LINK: _give_monthly_marseybux_task_kofi()
else: _give_monthly_marseybux_task()
if every_1mo and not KOFI_LINK:
_give_monthly_marseybux_task()
g.db.commit()
g.db.close()
@ -160,29 +159,3 @@ def _give_monthly_marseybux_task():
g.db.add(ma)
return True
def _give_monthly_marseybux_task_kofi():
month = datetime.datetime.now() + datetime.timedelta(days=5)
month = month.strftime('%B')
tx_emails = [x[0] for x in g.db.query(Transaction.email).distinct().all()]
for u in g.db.query(User).filter(User.patron > 0, User.patron_utc == 0).all():
g.db.add(u)
if not (u.is_activated and u.email in tx_emails):
u.patron = 0
continue
marseybux_reward = marseybux_li[u.patron]
u.pay_account('marseybux', marseybux_reward)
send_repeatable_notification(u.id, f"@AutoJanny has given you {marseybux_reward} Marseybux for the month of {month}! You can use them to buy awards in the [shop](/shop).")
ma = ModAction(
kind="monthly",
user_id=AUTOJANNY_ID,
)
g.db.add(ma)
return True

View File

@ -1271,6 +1271,42 @@ def bid_list(v:User, bid):
)
kofi_tiers={
5: 1,
10: 2,
20: 3,
50: 4,
100: 5,
200: 6
}
def claim_rewards(v):
g.db.flush()
transactions = g.db.query(Transaction).filter_by(email=v.email, claimed=None).all()
highest_tier = 0
marseybux = 0
for transaction in transactions:
tier = kofi_tiers[transaction.amount]
marseybux += marseybux_li[tier]
if transaction.type == 'Subscription' and tier > highest_tier:
highest_tier = tier
transaction.claimed = True
g.db.add(transaction)
v.pay_account('marseybux', marseybux)
send_repeatable_notification(v.id, f"You have received {marseybux} Marseybux! You can use them to buy awards in the [shop](/shop).")
g.db.add(v)
if highest_tier > v.patron:
v.patron = highest_tier
for badge in g.db.query(Badge).filter(Badge.user_id == v.id, Badge.badge_id > 20, Badge.badge_id < 28).all():
g.db.delete(badge)
badge_grant(badge_id=20+highest_tier, user=v)
@app.post("/kofi")
def kofi():
if not KOFI_TOKEN: abort(404)
@ -1297,16 +1333,13 @@ def kofi():
)
g.db.add(transaction)
user = g.db.query(User).filter_by(email=email, is_activated=True).order_by(User.truescore.desc()).first()
if user:
claim_rewards(user)
return ''
kofi_tiers={
5: 1,
10: 2,
20: 3,
50: 4,
100: 5,
200: 6
}
@app.post("/settings/kofi")
@limiter.limit(DEFAULT_RATELIMIT_SLOWER)
@ -1323,26 +1356,7 @@ def settings_kofi(v:User):
if not transactions:
abort(400, f"{patron} rewards already claimed")
highest_tier = 0
marseybux = 0
for transaction in transactions:
tier = kofi_tiers[transaction.amount]
marseybux += marseybux_li[tier]
if transaction.type == 'Subscription' and tier > highest_tier:
highest_tier = tier
transaction.claimed = True
g.db.add(transaction)
v.pay_account('marseybux', marseybux)
send_repeatable_notification(v.id, f"You have received {marseybux} Marseybux! You can use them to buy awards in the [shop](/shop).")
g.db.add(v)
if highest_tier > v.patron:
v.patron = highest_tier
for badge in g.db.query(Badge).filter(Badge.user_id == v.id, Badge.badge_id > 20, Badge.badge_id < 28).all():
g.db.delete(badge)
badge_grant(badge_id=20+highest_tier, user=v)
claim_rewards(v)
return {"message": f"{patron} rewards claimed!"}