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:
co-authored by
Claude Fable 5.2
parent
7d4893bc3f
commit
51371da0e0
+181
-62
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/mymmrac/telego"
|
||||
|
||||
"github.com/noderunners/dlbot/internal/db"
|
||||
"github.com/noderunners/dlbot/internal/groups"
|
||||
"github.com/noderunners/dlbot/internal/i18n"
|
||||
"github.com/noderunners/dlbot/internal/links"
|
||||
"github.com/noderunners/dlbot/internal/media"
|
||||
@@ -33,79 +34,55 @@ func (b *Bot) handleMessage(ctx context.Context, m *telego.Message) {
|
||||
return
|
||||
}
|
||||
private := isPrivate(m.Chat.Type)
|
||||
inGroup := groups.IsGroup(m.Chat.Type)
|
||||
var gset *db.GroupSettings
|
||||
if inGroup {
|
||||
if gset, err = b.store.GetGroupSettings(ctx, m.Chat.ID); err != nil {
|
||||
b.log.Error("load group settings", "chat_id", m.Chat.ID, "err", err)
|
||||
return
|
||||
}
|
||||
// A group can pin its own language for everything said in it.
|
||||
if gset.Lang != "" {
|
||||
act.lang = gset.Lang
|
||||
}
|
||||
}
|
||||
cmd, forUs, isCmd := b.parseCommand(m)
|
||||
if isCmd {
|
||||
if !forUs {
|
||||
return // addressed to another bot in the group
|
||||
}
|
||||
switch cmd {
|
||||
case "start":
|
||||
b.cmdStart(ctx, m, act)
|
||||
case "help":
|
||||
b.cmdHelp(ctx, m, act)
|
||||
case "dl":
|
||||
b.cmdDownload(ctx, m, act, "")
|
||||
case "audio":
|
||||
b.cmdDownload(ctx, m, act, media.MP3)
|
||||
case "thumb":
|
||||
b.cmdDownload(ctx, m, act, media.Thumb)
|
||||
case "fresh":
|
||||
b.cmdLink(ctx, m, act, linkRequest{fresh: true})
|
||||
case "subs":
|
||||
b.cmdLink(ctx, m, act, linkRequest{subs: true})
|
||||
case "history":
|
||||
b.cmdHistory(ctx, m, act)
|
||||
case "purge":
|
||||
if b.isOwner(m) {
|
||||
b.cmdPurge(ctx, m, act)
|
||||
}
|
||||
case "cache":
|
||||
if b.isOwner(m) {
|
||||
b.cmdCache(ctx, m, act)
|
||||
}
|
||||
case "workers":
|
||||
if b.isOwner(m) {
|
||||
b.cmdWorkers(ctx, m, act)
|
||||
}
|
||||
case "drain":
|
||||
if b.isOwner(m) {
|
||||
b.cmdDrain(ctx, m, act)
|
||||
}
|
||||
case "ytdlp":
|
||||
if b.isOwner(m) {
|
||||
b.cmdYtDlp(ctx, m, act)
|
||||
}
|
||||
case "info":
|
||||
b.cmdInfo(ctx, m, act)
|
||||
case "queue":
|
||||
b.cmdQueue(ctx, m, act)
|
||||
case "cancel":
|
||||
b.cmdCancel(ctx, m, act)
|
||||
case "buy":
|
||||
b.cmdBuy(ctx, m, act)
|
||||
case "account":
|
||||
b.cmdAccount(ctx, m, act)
|
||||
case "limits":
|
||||
b.cmdLimits(ctx, m, act)
|
||||
case "credits":
|
||||
b.cmdCredits(ctx, m, act)
|
||||
case "donate":
|
||||
b.cmdDonate(ctx, m, act)
|
||||
case "redeem":
|
||||
b.cmdRedeem(ctx, m, act, commandArgs(m))
|
||||
case "referral":
|
||||
b.cmdReferral(ctx, m, act)
|
||||
default:
|
||||
if private {
|
||||
_, _ = b.send(ctx, m.Chat.ID, int64(m.MessageID), b.tr.HTML(act.lang, "cmd.unknown", nil), nil)
|
||||
if inGroup {
|
||||
b.noteGroup(ctx, m.Chat, act.user.ID)
|
||||
// A group that switched the bot off only hears from it when an
|
||||
// admin opens the settings to switch it back on.
|
||||
if !gset.Enabled && cmd != "settings" {
|
||||
return
|
||||
}
|
||||
}
|
||||
if b.ownerCommand(ctx, m, act, cmd) {
|
||||
return
|
||||
}
|
||||
b.runCommand(ctx, m, act, cmd, gset)
|
||||
return
|
||||
}
|
||||
urls := messageLinks(m)
|
||||
if inGroup {
|
||||
// Without a command the bot only acts when the admins turned on
|
||||
// link detection. It then needs to see ordinary messages, which
|
||||
// means group privacy mode off or the bot being an admin.
|
||||
if !gset.Enabled || !gset.AutoDetect || len(urls) == 0 {
|
||||
return
|
||||
}
|
||||
b.noteGroup(ctx, m.Chat, act.user.ID)
|
||||
b.startLink(ctx, linkRequest{
|
||||
chatID: m.Chat.ID, chatType: m.Chat.Type, sourceID: int64(m.MessageID), act: act, url: urls[0],
|
||||
preset: presetFor(act, gset),
|
||||
})
|
||||
return
|
||||
}
|
||||
if !private {
|
||||
return // groups are driven by commands; auto-detect is an admin opt-in
|
||||
return
|
||||
}
|
||||
urls := messageLinks(m)
|
||||
if len(urls) == 0 {
|
||||
_, _ = b.send(ctx, m.Chat.ID, 0, b.tr.HTML(act.lang, "hint.send_link", nil), nil)
|
||||
return
|
||||
@@ -116,10 +93,131 @@ func (b *Bot) handleMessage(ctx context.Context, m *telego.Message) {
|
||||
for _, u := range urls {
|
||||
b.startLink(ctx, linkRequest{
|
||||
chatID: m.Chat.ID, chatType: m.Chat.Type, sourceID: int64(m.MessageID), act: act, url: u,
|
||||
preset: presetFor(act, nil),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// presetFor returns the format to start without showing the card: the
|
||||
// group's default quality in a group, the user's own in a private chat.
|
||||
func presetFor(act actor, gset *db.GroupSettings) media.Profile {
|
||||
q := act.user.Quality
|
||||
if gset != nil {
|
||||
q = gset.Quality
|
||||
}
|
||||
if p, err := media.Parse(q); err == nil {
|
||||
return p
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// runCommand dispatches the commands everyone can use.
|
||||
func (b *Bot) runCommand(ctx context.Context, m *telego.Message, act actor, cmd string, gset *db.GroupSettings) {
|
||||
// Commands that carry a link. In a group their message can be removed
|
||||
// afterwards when the admins asked for a tidy chat.
|
||||
tidy := func() {
|
||||
if gset != nil && gset.DeleteCmds {
|
||||
b.deleteMessage(ctx, m.Chat.ID, int64(m.MessageID))
|
||||
}
|
||||
}
|
||||
switch cmd {
|
||||
case "start":
|
||||
b.cmdStart(ctx, m, act)
|
||||
case "help":
|
||||
b.cmdHelp(ctx, m, act)
|
||||
case "dl":
|
||||
b.cmdDownload(ctx, m, act, presetFor(act, gset))
|
||||
tidy()
|
||||
case "audio":
|
||||
b.cmdDownload(ctx, m, act, media.MP3)
|
||||
tidy()
|
||||
case "thumb":
|
||||
b.cmdDownload(ctx, m, act, media.Thumb)
|
||||
tidy()
|
||||
case "fresh":
|
||||
b.cmdLink(ctx, m, act, linkRequest{fresh: true})
|
||||
tidy()
|
||||
case "subs":
|
||||
b.cmdLink(ctx, m, act, linkRequest{subs: true})
|
||||
tidy()
|
||||
case "info":
|
||||
b.cmdInfo(ctx, m, act)
|
||||
case "history":
|
||||
b.cmdHistory(ctx, m, act)
|
||||
case "queue":
|
||||
if b.isOwner(m) && strings.TrimSpace(commandArgs(m)) == "all" {
|
||||
b.cmdQueueAll(ctx, m, act)
|
||||
return
|
||||
}
|
||||
b.cmdQueue(ctx, m, act)
|
||||
case "cancel":
|
||||
b.cmdCancel(ctx, m, act)
|
||||
case "quality":
|
||||
b.cmdQuality(ctx, m, act)
|
||||
case "language":
|
||||
b.cmdLanguage(ctx, m, act)
|
||||
case "app":
|
||||
b.cmdApp(ctx, m, act)
|
||||
case "buy":
|
||||
b.cmdBuy(ctx, m, act)
|
||||
case "account":
|
||||
b.cmdAccount(ctx, m, act)
|
||||
case "limits":
|
||||
b.cmdLimits(ctx, m, act)
|
||||
case "credits":
|
||||
b.cmdCredits(ctx, m, act)
|
||||
case "donate":
|
||||
b.cmdDonate(ctx, m, act)
|
||||
case "redeem":
|
||||
b.cmdRedeem(ctx, m, act, commandArgs(m))
|
||||
case "referral":
|
||||
b.cmdReferral(ctx, m, act)
|
||||
case "privacy", "terms", "support":
|
||||
if b.privateOnly(ctx, m, act) {
|
||||
b.cmdStatic(ctx, m, act, "static."+cmd)
|
||||
}
|
||||
case "settings":
|
||||
b.cmdSettings(ctx, m, act)
|
||||
case "grouplang":
|
||||
b.cmdGroupLang(ctx, m, act)
|
||||
case "autodetect":
|
||||
b.cmdAutoDetect(ctx, m, act)
|
||||
case "allow":
|
||||
b.cmdSiteList(ctx, m, act, true)
|
||||
case "block":
|
||||
b.cmdSiteList(ctx, m, act, false)
|
||||
case "spending":
|
||||
b.cmdSpending(ctx, m, act)
|
||||
case "groupwallet":
|
||||
b.cmdGroupWallet(ctx, m, act, false)
|
||||
case "grouppass":
|
||||
b.cmdGroupWallet(ctx, m, act, true)
|
||||
case "groupstats":
|
||||
b.cmdGroupStats(ctx, m, act)
|
||||
default:
|
||||
if isPrivate(m.Chat.Type) {
|
||||
_, _ = b.send(ctx, m.Chat.ID, int64(m.MessageID), b.tr.HTML(act.lang, "cmd.unknown", nil), nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// cmdApp points to the Mini App. In a private chat the button opens it in
|
||||
// place; in a group it has to be a plain link, because web app buttons do
|
||||
// not work there.
|
||||
func (b *Bot) cmdApp(ctx context.Context, m *telego.Message, act actor) {
|
||||
if !b.cfg.MiniAppEnabled() {
|
||||
_, _ = b.send(ctx, m.Chat.ID, int64(m.MessageID), b.tr.HTML(act.lang, "app.unavailable", nil), nil)
|
||||
return
|
||||
}
|
||||
label := b.tr.T(act.lang, "btn.open_app", nil)
|
||||
btn := urlButton(label, "https://t.me/"+b.username+"?startapp")
|
||||
if isPrivate(m.Chat.Type) {
|
||||
btn = telego.InlineKeyboardButton{Text: label, WebApp: &telego.WebAppInfo{URL: b.cfg.PublicURL + "/app/"}}
|
||||
}
|
||||
_, _ = b.send(ctx, m.Chat.ID, int64(m.MessageID), b.tr.HTML(act.lang, "app.open", nil),
|
||||
keyboard([]telego.InlineKeyboardButton{btn}))
|
||||
}
|
||||
|
||||
// commandArgs returns the text after the command of a message.
|
||||
func commandArgs(m *telego.Message) string {
|
||||
for _, e := range m.Entities {
|
||||
@@ -184,6 +282,12 @@ func (b *Bot) cmdStart(ctx context.Context, m *telego.Message, act actor) {
|
||||
case strings.HasPrefix(payload, "skip_"):
|
||||
b.startSkipLink(ctx, m, act, strings.TrimPrefix(payload, "skip_"))
|
||||
return
|
||||
case strings.HasPrefix(payload, "gw_"):
|
||||
b.startGroupLink(ctx, m, act, strings.TrimPrefix(payload, "gw_"), false)
|
||||
return
|
||||
case strings.HasPrefix(payload, "gs_"):
|
||||
b.startGroupLink(ctx, m, act, strings.TrimPrefix(payload, "gs_"), true)
|
||||
return
|
||||
case payload == "buy":
|
||||
b.showBuy(ctx, m.Chat.ID, act)
|
||||
return
|
||||
@@ -380,8 +484,23 @@ func (b *Bot) handleCallback(ctx context.Context, q *telego.CallbackQuery) {
|
||||
b.answer(ctx, q, "", false)
|
||||
return
|
||||
}
|
||||
if q.Message != nil && groups.IsGroup(q.Message.GetChat().Type) {
|
||||
if gset, err := b.store.GetGroupSettings(ctx, q.Message.GetChat().ID); err == nil && gset.Lang != "" {
|
||||
act.lang = gset.Lang
|
||||
}
|
||||
}
|
||||
action, id, arg := parseCallback(q.Data)
|
||||
switch action {
|
||||
case cbGroupSetting:
|
||||
b.cbSetting(ctx, q, act, id)
|
||||
case cbGroupBuy:
|
||||
b.cbGroupBuyProduct(ctx, q, act, id, arg)
|
||||
case cbQuality:
|
||||
b.cbSetQuality(ctx, q, act, id)
|
||||
case cbLanguage:
|
||||
b.cbSetLanguage(ctx, q, act, id)
|
||||
case cbBroadcast:
|
||||
b.cbBroadcastSend(ctx, q, act, id)
|
||||
case cbDownload:
|
||||
b.cbDownload(ctx, q, act, id, arg)
|
||||
case cbCancelCard:
|
||||
|
||||
Reference in New Issue
Block a user