Phase 5: groups

Group wallets, group passes, spending rules and admin-only settings.

- Groups recorded from my_chat_member; admin rights verified with
  getChatMember and cached for five minutes
- Group context in entitlement resolution: group pass, group credits under
  the spending rule (anyone, admins only, capped per person per day) and
  the group's daily cap; refunds go back to the wallet that paid
- Group gate before any lookup: bot on or off, who may use it, allowed
  and blocked sites, plus the owner's maintenance mode
- /settings with inline toggles checked on every press, /grouplang,
  /autodetect, /allow, /block, /spending, /groupwallet, /grouppass,
  /groupstats
- Deep links with the target pre-selected: paying for a group happens in
  the private chat; a refused download in a group offers "Buy for me" and
  "Add group credits"
- Optional link detection, tidy mode, default quality and language per
  group; per-member usage
- /quality, /language, /app, /privacy, /terms, /support
- Remaining owner tools: /stats, /queue all, /user, /group, /ban, /unban,
  /grant (passes and gift codes), /credit, /refund, /blocklist,
  /broadcast with confirmation, /maint, /reload i18n

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Michilis
2026-11-18 09:46:32 +00:00
co-authored by Claude Fable 5.2
parent 7d4893bc3f
commit 51371da0e0
23 changed files with 3231 additions and 81 deletions
+41 -5
View File
@@ -305,6 +305,8 @@ type purchaseContext struct {
requestID string
profile string
deliveryID string
// groupID makes a group's wallet the target instead of the payer's.
groupID int64
}
// startPurchase creates the invoice and shows it in the private chat.
@@ -316,7 +318,12 @@ func (b *Bot) startPurchase(ctx context.Context, chat int64, act actor, product
_, _ = b.send(ctx, chat, 0, b.tr.HTML(lang, "buy.bad_amount", nil), nil)
return
}
w, err := b.bill.Wallet(ctx, db.OwnerUser, act.user.ID)
// Every purchase has a target: the payer's own wallet or a group's.
ownerType, ownerID := db.OwnerUser, act.user.ID
if pc.groupID != 0 {
ownerType, ownerID = db.OwnerGroup, pc.groupID
}
w, err := b.bill.Wallet(ctx, ownerType, ownerID)
if err != nil {
b.internalError(ctx, chat, act, err)
return
@@ -347,12 +354,30 @@ func (b *Bot) startPurchase(ctx context.Context, chat int64, act actor, product
func (b *Bot) invoiceText(p *db.Purchase) string {
lang := p.Lang
minutes := b.cfg.LNbits.InvoiceExpiry.D()
return b.tr.HTML(lang, "invoice.caption", i18n.Args{
text := b.tr.HTML(lang, "invoice.caption", i18n.Args{
"Title": b.productTitle(lang, p.Product),
"Amount": b.tr.Sats(lang, p.AmountSats),
"Invoice": p.Bolt11,
"Valid": b.tr.Span(lang, minutes),
})
if title := b.groupTitle(p); title != "" {
text = b.tr.HTML(lang, "invoice.for_group", i18n.Args{"Group": title}) + "\n" + text
}
return text
}
// groupTitle returns the title of the group a purchase is for, or "" when
// its target is the payer's own wallet.
func (b *Bot) groupTitle(p *db.Purchase) string {
w, err := b.store.GetWallet(b.baseCtx, p.WalletID)
if err != nil || w.OwnerType != db.OwnerGroup {
return ""
}
g, err := b.store.GetGroup(b.baseCtx, w.OwnerID)
if err != nil || g.Title == "" {
return strconv.FormatInt(w.OwnerID, 10)
}
return g.Title
}
func (b *Bot) invoiceKeyboard(p *db.Purchase) *telego.InlineKeyboardMarkup {
@@ -544,12 +569,18 @@ func (b *Bot) paidText(ctx context.Context, p *db.Purchase) string {
if err != nil {
return text
}
// The same facts, worded for whose wallet it is.
balanceKey, passKey := "invoice.balance", "invoice.pass_until"
if w.OwnerType == db.OwnerGroup {
balanceKey, passKey = "invoice.group_balance", "invoice.group_pass_until"
text = b.tr.HTML(lang, "invoice.for_group", i18n.Args{"Group": b.groupTitle(p)}) + "\n" + text
}
switch p.Product {
case billing.ProductCredits:
text += "\n" + b.tr.HTML(lang, "invoice.balance", i18n.Args{"Balance": b.tr.Sats(lang, w.Balance)})
text += "\n" + b.tr.HTML(lang, balanceKey, i18n.Args{"Balance": b.tr.Sats(lang, w.Balance)})
case billing.ProductPassDay, billing.ProductPassWeek, billing.ProductPassMonth:
if until, err := b.store.PassUntil(ctx, w.ID, db.NowMilli()); err == nil && until > 0 {
text += "\n" + b.tr.HTML(lang, "invoice.pass_until", i18n.Args{"Until": b.tr.DateTime(lang, time.UnixMilli(until))})
text += "\n" + b.tr.HTML(lang, passKey, i18n.Args{"Until": b.tr.DateTime(lang, time.UnixMilli(until))})
}
case billing.ProductDonation:
text += "\n" + b.tr.HTML(lang, "invoice.thanks", nil)
@@ -712,8 +743,13 @@ func (b *Bot) offerUpgrade(ctx context.Context, chat, replyTo int64, lang string
})
}
} else {
// Two targets, both pre-selected by the link: the requester's own
// wallet, or the group's.
link := "https://t.me/" + b.username + "?start=buy_" + req.ID + "_" + encodeProfile(string(profile))
rows = append(rows, []telego.InlineKeyboardButton{urlButton(b.tr.T(lang, "btn.buy_for_me", nil), link)})
rows = append(rows,
[]telego.InlineKeyboardButton{urlButton(b.tr.T(lang, "btn.buy_for_me", nil), link)},
[]telego.InlineKeyboardButton{urlButton(b.tr.T(lang, "btn.add_group_credits", nil), b.groupWalletLink(chat))},
)
}
_, _ = b.send(ctx, chat, replyTo, text, keyboard(rows...))
}