mirror of
https://github.com/tiennm99/miti99bot.git
synced 2026-10-03 07:13:24 +00:00
refactor(modules): extract shared chathelper + champname packages
Phase 3 of the 2026-05-09 review remediation plan. Eliminates the helper drift (subjectFor / argAfterCommand / nowMillis / reply / replyHTML / winRate) that previously lived in 3-4 modules, plus the loldle-specific normalize / findChampion / findByExactName. - internal/modules/util/chathelper: SubjectFor, ArgAfterCommand, NowMillis, Reply, ReplyHTML, WinRate. Single canonical SubjectFor shape (group/supergroup -> chat ID, else user ID); WinRate uses math.Round to match JS Math.round (the truncation drift caught in Phase 5b/5c). - internal/champname: Normalize + generic Find[T] / FindByExactName[T] with name-extractor closure. Loldle and loldle-emoji both consume via Champion / EmojiChampion. Migrations: wordle, loldle, loldle-emoji, misc, util/info, util/stickerid. Module-local lookup.go + normalize.go in loldle and loldle-emoji deleted. go test -race -count=1 ./... clean across all 12 packages. Net ~290 lines removed across handler files.
This commit is contained in:
1 parent
29bbf30923
commit
576b6b6aec
16 files changed
+503
-546
No files matched your search
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/go-telegram/bot/models"
|
||||
|
||||
"github.com/tiennm99/miti99bot-go/internal/modules"
|
||||
"github.com/tiennm99/miti99bot-go/internal/modules/util/chathelper"
|
||||
"github.com/tiennm99/miti99bot-go/internal/storage"
|
||||
)
|
||||
|
||||
@@ -45,16 +46,15 @@ func pingCommand(deps modules.Deps) modules.Command {
|
||||
Visibility: modules.VisibilityPublic,
|
||||
Description: "Health check — replies pong and records last ping",
|
||||
Handler: func(ctx context.Context, b *bot.Bot, update *models.Update) error {
|
||||
if update.Message == nil {
|
||||
return nil
|
||||
}
|
||||
// Best-effort write — if KV is unavailable, still reply.
|
||||
payload := lastPing{At: time.Now().UTC().UnixMilli()}
|
||||
payload := lastPing{At: chathelper.NowMillis()}
|
||||
if err := deps.KV.PutJSON(ctx, lastPingKey, payload); err != nil {
|
||||
log.Printf("misc /ping: putJSON failed: %v", err)
|
||||
}
|
||||
_, err := b.SendMessage(ctx, &bot.SendMessageParams{
|
||||
ChatID: update.Message.Chat.ID,
|
||||
Text: "pong",
|
||||
})
|
||||
return err
|
||||
return chathelper.Reply(ctx, b, update.Message.Chat.ID, "pong")
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -65,6 +65,9 @@ func mstatsCommand(deps modules.Deps) modules.Command {
|
||||
Visibility: modules.VisibilityProtected,
|
||||
Description: "Show the timestamp of the last /ping",
|
||||
Handler: func(ctx context.Context, b *bot.Bot, update *models.Update) error {
|
||||
if update.Message == nil {
|
||||
return nil
|
||||
}
|
||||
var last lastPing
|
||||
text := "last ping: never"
|
||||
err := deps.KV.GetJSON(ctx, lastPingKey, &last)
|
||||
@@ -75,11 +78,7 @@ func mstatsCommand(deps modules.Deps) modules.Command {
|
||||
case err != nil && !errors.Is(err, storage.ErrNotFound):
|
||||
return fmt.Errorf("misc /mstats: %w", err)
|
||||
}
|
||||
_, err = b.SendMessage(ctx, &bot.SendMessageParams{
|
||||
ChatID: update.Message.Chat.ID,
|
||||
Text: text,
|
||||
})
|
||||
return err
|
||||
return chathelper.Reply(ctx, b, update.Message.Chat.ID, text)
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -90,11 +89,10 @@ func fortytwoCommand() modules.Command {
|
||||
Visibility: modules.VisibilityPrivate,
|
||||
Description: "Easter egg — the answer",
|
||||
Handler: func(ctx context.Context, b *bot.Bot, update *models.Update) error {
|
||||
_, err := b.SendMessage(ctx, &bot.SendMessageParams{
|
||||
ChatID: update.Message.Chat.ID,
|
||||
Text: "The answer.",
|
||||
})
|
||||
return err
|
||||
if update.Message == nil {
|
||||
return nil
|
||||
}
|
||||
return chathelper.Reply(ctx, b, update.Message.Chat.ID, "The answer.")
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user