REPOSITORY / ScuroNeko/Laniakea
Compare commits
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7101aba548 | ||
|
|
2de46a27c8 | ||
|
|
ae7426c36a | ||
|
|
61562e8a3b | ||
|
|
a84e24ff25
|
||
|
|
c0a26024f4
|
||
|
|
786da652e6
|
@@ -1,16 +1,18 @@
|
||||
package laniakea
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"sync"
|
||||
|
||||
"git.nix13.pw/scuroneko/extypes"
|
||||
"git.nix13.pw/scuroneko/laniakea/tgapi"
|
||||
"git.nix13.pw/scuroneko/slog"
|
||||
"github.com/alitto/pond/v2"
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
@@ -82,18 +84,20 @@ type Bot[T DbContext] struct {
|
||||
prefixes []string
|
||||
runners []Runner[T]
|
||||
|
||||
api *tgapi.API
|
||||
uploader *tgapi.Uploader
|
||||
dbContext *T
|
||||
l10n *L10n
|
||||
api *tgapi.API
|
||||
uploader *tgapi.Uploader
|
||||
dbContext *T
|
||||
l10n *L10n
|
||||
draftProvider *DraftProvider
|
||||
|
||||
updateOffset int
|
||||
updateTypes []tgapi.UpdateType
|
||||
updateQueue *extypes.Queue[*tgapi.Update]
|
||||
updateOffsetMu sync.Mutex
|
||||
updateOffset int
|
||||
updateTypes []tgapi.UpdateType
|
||||
updateQueue chan *tgapi.Update
|
||||
}
|
||||
|
||||
func NewBot[T any](opts *BotOpts) *Bot[T] {
|
||||
updateQueue := extypes.CreateQueue[*tgapi.Update](512)
|
||||
updateQueue := make(chan *tgapi.Update, 512)
|
||||
|
||||
var limiter *rate.Limiter
|
||||
if opts.RateLimit > 0 {
|
||||
@@ -119,6 +123,7 @@ func NewBot[T any](opts *BotOpts) *Bot[T] {
|
||||
runners: make([]Runner[T], 0),
|
||||
extraLoggers: make([]*slog.Logger, 0),
|
||||
l10n: &L10n{},
|
||||
draftProvider: NewRandomDraftProvider(api),
|
||||
}
|
||||
bot.extraLoggers = bot.extraLoggers.Push(api.GetLogger()).Push(uploader.GetLogger())
|
||||
|
||||
@@ -185,13 +190,24 @@ func (bot *Bot[T]) initLoggers(opts *BotOpts) {
|
||||
}
|
||||
}
|
||||
|
||||
func (bot *Bot[T]) GetUpdateOffset() int { return bot.updateOffset }
|
||||
func (bot *Bot[T]) SetUpdateOffset(offset int) { bot.updateOffset = offset }
|
||||
func (bot *Bot[T]) GetUpdateTypes() []tgapi.UpdateType { return bot.updateTypes }
|
||||
func (bot *Bot[T]) GetQueue() *extypes.Queue[*tgapi.Update] { return bot.updateQueue }
|
||||
func (bot *Bot[T]) GetLogger() *slog.Logger { return bot.logger }
|
||||
func (bot *Bot[T]) GetDBContext() *T { return bot.dbContext }
|
||||
func (bot *Bot[T]) L10n(lang, key string) string { return bot.l10n.Translate(lang, key) }
|
||||
func (bot *Bot[T]) GetUpdateOffset() int {
|
||||
bot.updateOffsetMu.Lock()
|
||||
defer bot.updateOffsetMu.Unlock()
|
||||
return bot.updateOffset
|
||||
}
|
||||
func (bot *Bot[T]) SetUpdateOffset(offset int) {
|
||||
bot.updateOffsetMu.Lock()
|
||||
defer bot.updateOffsetMu.Unlock()
|
||||
bot.updateOffset = offset
|
||||
}
|
||||
func (bot *Bot[T]) GetUpdateTypes() []tgapi.UpdateType { return bot.updateTypes }
|
||||
func (bot *Bot[T]) GetLogger() *slog.Logger { return bot.logger }
|
||||
func (bot *Bot[T]) GetDBContext() *T { return bot.dbContext }
|
||||
func (bot *Bot[T]) L10n(lang, key string) string { return bot.l10n.Translate(lang, key) }
|
||||
func (bot *Bot[T]) SetDraftProvider(p *DraftProvider) *Bot[T] {
|
||||
bot.draftProvider = p
|
||||
return bot
|
||||
}
|
||||
|
||||
type DbLogger[T DbContext] func(db *T) slog.LoggerWriter
|
||||
|
||||
@@ -235,7 +251,7 @@ func (bot *Bot[T]) Debug(debug bool) *Bot[T] {
|
||||
func (bot *Bot[T]) AddPlugins(plugin ...*Plugin[T]) *Bot[T] {
|
||||
for _, p := range plugin {
|
||||
bot.plugins = append(bot.plugins, *p)
|
||||
bot.logger.Debugln(fmt.Sprintf("plugins with name \"%s\" registered", p.Name))
|
||||
bot.logger.Debugln(fmt.Sprintf("plugins with name \"%s\" registered", p.name))
|
||||
}
|
||||
return bot
|
||||
}
|
||||
@@ -266,7 +282,15 @@ func (bot *Bot[T]) AddL10n(l *L10n) *Bot[T] {
|
||||
return bot
|
||||
}
|
||||
|
||||
func (bot *Bot[T]) Run() {
|
||||
func (bot *Bot[T]) enqueueUpdate(u *tgapi.Update) error {
|
||||
select {
|
||||
case bot.updateQueue <- u:
|
||||
return nil
|
||||
default:
|
||||
return extypes.QueueFullErr
|
||||
}
|
||||
}
|
||||
func (bot *Bot[T]) RunWithContext(ctx context.Context) {
|
||||
if len(bot.prefixes) == 0 {
|
||||
bot.logger.Fatalln("no prefixes defined")
|
||||
return
|
||||
@@ -282,26 +306,35 @@ func (bot *Bot[T]) Run() {
|
||||
bot.logger.Infoln("Bot running. Press CTRL+C to exit.")
|
||||
go func() {
|
||||
for {
|
||||
_, err := bot.Updates()
|
||||
if err != nil {
|
||||
bot.logger.Errorln(err)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
default:
|
||||
updates, err := bot.Updates()
|
||||
if err != nil {
|
||||
bot.logger.Errorln(err)
|
||||
continue
|
||||
}
|
||||
|
||||
for _, u := range updates {
|
||||
select {
|
||||
case bot.updateQueue <- new(u):
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
for {
|
||||
queue := bot.updateQueue
|
||||
if queue.IsEmpty() {
|
||||
time.Sleep(time.Millisecond * 25)
|
||||
continue
|
||||
}
|
||||
|
||||
u := queue.Dequeue()
|
||||
if u == nil {
|
||||
bot.logger.Errorln("update is nil")
|
||||
continue
|
||||
}
|
||||
|
||||
bot.handle(u)
|
||||
pool := pond.NewPool(16)
|
||||
for update := range bot.updateQueue {
|
||||
update := update
|
||||
pool.Submit(func() {
|
||||
bot.handle(update)
|
||||
})
|
||||
}
|
||||
}
|
||||
func (bot *Bot[T]) Run() {
|
||||
bot.RunWithContext(context.Background())
|
||||
}
|
||||
|
||||
+5
-1
@@ -27,7 +27,7 @@ func generateBotCommand[T any](cmd Command[T]) tgapi.BotCommand {
|
||||
|
||||
func generateBotCommandForPlugin[T any](pl Plugin[T]) []tgapi.BotCommand {
|
||||
commands := make([]tgapi.BotCommand, 0)
|
||||
for _, cmd := range pl.Commands {
|
||||
for _, cmd := range pl.commands {
|
||||
if cmd.skipAutoCmd {
|
||||
continue
|
||||
}
|
||||
@@ -46,6 +46,10 @@ func (bot *Bot[T]) AutoGenerateCommands() error {
|
||||
|
||||
commands := make([]tgapi.BotCommand, 0)
|
||||
for _, pl := range bot.plugins {
|
||||
if pl.skipAutoCmd {
|
||||
continue
|
||||
}
|
||||
|
||||
commands = append(commands, generateBotCommandForPlugin(pl)...)
|
||||
}
|
||||
if len(commands) > 100 {
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
package laniakea
|
||||
|
||||
import (
|
||||
"math"
|
||||
"math/rand/v2"
|
||||
"sync/atomic"
|
||||
|
||||
"git.nix13.pw/scuroneko/laniakea/tgapi"
|
||||
)
|
||||
|
||||
type draftIdGenerator interface {
|
||||
Next() uint64
|
||||
}
|
||||
|
||||
type RandomDraftIdGenerator struct {
|
||||
draftIdGenerator
|
||||
}
|
||||
|
||||
func (g *RandomDraftIdGenerator) Next() uint64 {
|
||||
return rand.Uint64N(math.MaxUint64)
|
||||
}
|
||||
|
||||
type LinearDraftIdGenerator struct {
|
||||
draftIdGenerator
|
||||
lastId uint64
|
||||
}
|
||||
|
||||
func (g *LinearDraftIdGenerator) Next() uint64 {
|
||||
return atomic.AddUint64(&g.lastId, 1)
|
||||
}
|
||||
|
||||
type DraftProvider struct {
|
||||
api *tgapi.API
|
||||
|
||||
chatID int
|
||||
messageThreadID int
|
||||
parseMode tgapi.ParseMode
|
||||
entities []tgapi.MessageEntity
|
||||
|
||||
drafts map[uint64]*Draft
|
||||
generator draftIdGenerator
|
||||
}
|
||||
type Draft struct {
|
||||
api *tgapi.API
|
||||
|
||||
chatID int
|
||||
messageThreadID int
|
||||
parseMode tgapi.ParseMode
|
||||
entities []tgapi.MessageEntity
|
||||
|
||||
ID uint64
|
||||
Message string
|
||||
}
|
||||
|
||||
func NewRandomDraftProvider(api *tgapi.API) *DraftProvider {
|
||||
return &DraftProvider{
|
||||
api: api, generator: &RandomDraftIdGenerator{},
|
||||
drafts: make(map[uint64]*Draft),
|
||||
}
|
||||
}
|
||||
func NewLinearDraftProvider(api *tgapi.API, startValue uint64) *DraftProvider {
|
||||
return &DraftProvider{
|
||||
api: api,
|
||||
generator: &LinearDraftIdGenerator{lastId: startValue},
|
||||
drafts: make(map[uint64]*Draft),
|
||||
}
|
||||
}
|
||||
func (d *DraftProvider) NewDraft() *Draft {
|
||||
id := d.generator.Next()
|
||||
draft := &Draft{d.api, d.chatID, d.messageThreadID, d.parseMode, d.entities, id, ""}
|
||||
d.drafts[id] = draft
|
||||
return draft
|
||||
}
|
||||
|
||||
func (d *Draft) Push(newText string) error {
|
||||
d.Message += newText
|
||||
params := tgapi.SendMessageDraftP{
|
||||
ChatID: d.chatID,
|
||||
DraftID: d.ID,
|
||||
Text: d.Message,
|
||||
ParseMode: d.parseMode,
|
||||
Entities: d.entities,
|
||||
}
|
||||
if d.messageThreadID > 0 {
|
||||
params.MessageThreadID = d.messageThreadID
|
||||
}
|
||||
_, err := d.api.SendMessageDraft(params)
|
||||
return err
|
||||
}
|
||||
func (d *Draft) Flush() error {
|
||||
if d.Message == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
params := tgapi.SendMessageP{
|
||||
ChatID: d.chatID,
|
||||
ParseMode: d.parseMode,
|
||||
Entities: d.entities,
|
||||
Text: d.Message,
|
||||
}
|
||||
if d.messageThreadID > 0 {
|
||||
params.MessageThreadID = d.messageThreadID
|
||||
}
|
||||
_, err := d.api.SendMessage(params)
|
||||
return err
|
||||
}
|
||||
@@ -3,13 +3,13 @@ module git.nix13.pw/scuroneko/laniakea
|
||||
go 1.26
|
||||
|
||||
require (
|
||||
git.nix13.pw/scuroneko/extypes v1.2.0
|
||||
git.nix13.pw/scuroneko/extypes v1.2.1
|
||||
git.nix13.pw/scuroneko/slog v1.0.2
|
||||
github.com/alitto/pond/v2 v2.6.2
|
||||
golang.org/x/time v0.14.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/alitto/pond/v2 v2.6.2 // indirect
|
||||
github.com/fatih/color v1.18.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.14 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
git.nix13.pw/scuroneko/extypes v1.2.0 h1:2n2hD6KsMAted+6MGhAyeWyli2Qzc9G2y+pQNB7C1dM=
|
||||
git.nix13.pw/scuroneko/extypes v1.2.0/go.mod h1:uZVs8Yo3RrYAG9dMad6qR6lsYY67t+459D9c65QAYAw=
|
||||
git.nix13.pw/scuroneko/extypes v1.2.1 h1:IYrOjnWKL2EAuJYtYNa+luB1vBe6paE8VY/YD+5/RpQ=
|
||||
git.nix13.pw/scuroneko/extypes v1.2.1/go.mod h1:uZVs8Yo3RrYAG9dMad6qR6lsYY67t+459D9c65QAYAw=
|
||||
git.nix13.pw/scuroneko/slog v1.0.2 h1:vZyUROygxC2d5FJHUQM/30xFEHY1JT/aweDZXA4rm2g=
|
||||
git.nix13.pw/scuroneko/slog v1.0.2/go.mod h1:3Qm2wzkR5KjwOponMfG7TcGSDjmYaFqRAmLvSPTuWJI=
|
||||
github.com/alitto/pond/v2 v2.6.2 h1:Sphe40g0ILeM1pA2c2K+Th0DGU+pt0A/Kprr+WB24Pw=
|
||||
|
||||
+9
-3
@@ -9,7 +9,13 @@ import (
|
||||
)
|
||||
|
||||
func (bot *Bot[T]) handle(u *tgapi.Update) {
|
||||
ctx := &MsgContext{Update: *u, Api: bot.api, botLogger: bot.logger, errorTemplate: bot.errorTemplate, l10n: bot.l10n}
|
||||
ctx := &MsgContext{
|
||||
Update: *u, Api: bot.api,
|
||||
botLogger: bot.logger,
|
||||
errorTemplate: bot.errorTemplate,
|
||||
l10n: bot.l10n,
|
||||
draftProvider: bot.draftProvider,
|
||||
}
|
||||
for _, middleware := range bot.middlewares {
|
||||
middleware.Execute(ctx, bot.dbContext)
|
||||
}
|
||||
@@ -46,7 +52,7 @@ func (bot *Bot[T]) handleMessage(update *tgapi.Update, ctx *MsgContext) {
|
||||
text = strings.TrimSpace(text[len(prefix):])
|
||||
|
||||
for _, plugin := range bot.plugins {
|
||||
for cmd := range plugin.Commands {
|
||||
for cmd := range plugin.commands {
|
||||
if !strings.HasPrefix(text, cmd) {
|
||||
continue
|
||||
}
|
||||
@@ -96,7 +102,7 @@ func (bot *Bot[T]) handleCallback(update *tgapi.Update, ctx *MsgContext) {
|
||||
ctx.Args = data.Args
|
||||
|
||||
for _, plugin := range bot.plugins {
|
||||
_, ok := plugin.Payloads[data.Command]
|
||||
_, ok := plugin.payloads[data.Command]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
+5
-8
@@ -19,14 +19,8 @@ func (bot *Bot[T]) Updates() ([]tgapi.Update, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, u := range updates {
|
||||
bot.SetUpdateOffset(u.UpdateID + 1)
|
||||
err = bot.GetQueue().Enqueue(&u)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if bot.RequestLogger != nil {
|
||||
if bot.RequestLogger != nil {
|
||||
for _, u := range updates {
|
||||
j, err := json.Marshal(u)
|
||||
if err != nil {
|
||||
bot.GetLogger().Error(err)
|
||||
@@ -34,5 +28,8 @@ func (bot *Bot[T]) Updates() ([]tgapi.Update, error) {
|
||||
bot.RequestLogger.Debugf("UPDATE %s\n", j)
|
||||
}
|
||||
}
|
||||
if len(updates) > 0 {
|
||||
bot.SetUpdateOffset(updates[len(updates)-1].UpdateID + 1)
|
||||
}
|
||||
return updates, err
|
||||
}
|
||||
|
||||
+31
-23
@@ -24,6 +24,7 @@ type MsgContext struct {
|
||||
errorTemplate string
|
||||
botLogger *slog.Logger
|
||||
l10n *L10n
|
||||
draftProvider *DraftProvider
|
||||
}
|
||||
|
||||
type AnswerMessage struct {
|
||||
@@ -77,6 +78,7 @@ func (ctx *MsgContext) editPhotoText(messageId int, text string, kb *InlineKeybo
|
||||
if kb != nil {
|
||||
params.ReplyMarkup = kb.Get()
|
||||
}
|
||||
|
||||
msg, _, err := ctx.Api.EditMessageCaption(params)
|
||||
if err != nil {
|
||||
ctx.botLogger.Errorln(err)
|
||||
@@ -105,6 +107,12 @@ func (ctx *MsgContext) answer(text string, keyboard *InlineKeyboard) *AnswerMess
|
||||
if keyboard != nil {
|
||||
params.ReplyMarkup = keyboard.Get()
|
||||
}
|
||||
if ctx.Msg.MessageThreadID > 0 {
|
||||
params.MessageThreadID = ctx.Msg.MessageThreadID
|
||||
}
|
||||
if ctx.Msg.DirectMessageTopic != nil {
|
||||
params.DirectMessagesTopicID = ctx.Msg.DirectMessageTopic.TopicID
|
||||
}
|
||||
|
||||
msg, err := ctx.Api.SendMessage(params)
|
||||
if err != nil {
|
||||
@@ -135,6 +143,10 @@ func (ctx *MsgContext) answerPhoto(photoId, text string, kb *InlineKeyboard) *An
|
||||
if kb != nil {
|
||||
params.ReplyMarkup = kb.Get()
|
||||
}
|
||||
if ctx.Msg.MessageThreadID > 0 {
|
||||
params.MessageThreadID = ctx.Msg.MessageThreadID
|
||||
}
|
||||
|
||||
msg, err := ctx.Api.SendPhoto(params)
|
||||
if err != nil {
|
||||
ctx.botLogger.Errorln(err)
|
||||
@@ -162,12 +174,8 @@ func (ctx *MsgContext) delete(messageId int) {
|
||||
ctx.botLogger.Errorln(err)
|
||||
}
|
||||
}
|
||||
func (m *AnswerMessage) Delete() {
|
||||
m.ctx.delete(m.MessageID)
|
||||
}
|
||||
func (ctx *MsgContext) CallbackDelete() {
|
||||
ctx.delete(ctx.CallbackMsgId)
|
||||
}
|
||||
func (m *AnswerMessage) Delete() { m.ctx.delete(m.MessageID) }
|
||||
func (ctx *MsgContext) CallbackDelete() { ctx.delete(ctx.CallbackMsgId) }
|
||||
|
||||
func (ctx *MsgContext) answerCallbackQuery(url, text string, showAlert bool) {
|
||||
if len(ctx.CallbackQueryId) == 0 {
|
||||
@@ -181,23 +189,19 @@ func (ctx *MsgContext) answerCallbackQuery(url, text string, showAlert bool) {
|
||||
ctx.botLogger.Errorln(err)
|
||||
}
|
||||
}
|
||||
func (ctx *MsgContext) AnswerCbQuery() {
|
||||
ctx.answerCallbackQuery("", "", false)
|
||||
}
|
||||
func (ctx *MsgContext) AnswerCbQueryText(text string) {
|
||||
ctx.answerCallbackQuery("", text, false)
|
||||
}
|
||||
func (ctx *MsgContext) AnswerCbQueryAlert(text string) {
|
||||
ctx.answerCallbackQuery("", text, true)
|
||||
}
|
||||
func (ctx *MsgContext) AnswerCbQueryUrl(u string) {
|
||||
ctx.answerCallbackQuery(u, "", false)
|
||||
}
|
||||
func (ctx *MsgContext) AnswerCbQuery() { ctx.answerCallbackQuery("", "", false) }
|
||||
func (ctx *MsgContext) AnswerCbQueryText(text string) { ctx.answerCallbackQuery("", text, false) }
|
||||
func (ctx *MsgContext) AnswerCbQueryAlert(text string) { ctx.answerCallbackQuery("", text, true) }
|
||||
func (ctx *MsgContext) AnswerCbQueryUrl(u string) { ctx.answerCallbackQuery(u, "", false) }
|
||||
|
||||
func (ctx *MsgContext) SendAction(action tgapi.ChatActionType) {
|
||||
_, err := ctx.Api.SendChatAction(tgapi.SendChatActionP{
|
||||
params := tgapi.SendChatActionP{
|
||||
ChatID: ctx.Msg.Chat.ID, Action: action,
|
||||
})
|
||||
}
|
||||
if ctx.Msg.MessageThreadID > 0 {
|
||||
params.MessageThreadID = ctx.Msg.MessageThreadID
|
||||
}
|
||||
_, err := ctx.Api.SendChatAction(params)
|
||||
if err != nil {
|
||||
ctx.botLogger.Errorln(err)
|
||||
}
|
||||
@@ -213,10 +217,14 @@ func (ctx *MsgContext) error(err error) {
|
||||
}
|
||||
ctx.botLogger.Errorln(err)
|
||||
}
|
||||
func (ctx *MsgContext) Error(err error) {
|
||||
ctx.error(err)
|
||||
}
|
||||
func (ctx *MsgContext) Error(err error) { ctx.error(err) }
|
||||
|
||||
func (ctx *MsgContext) NewDraft() *Draft {
|
||||
draft := ctx.draftProvider.NewDraft()
|
||||
draft.chatID = ctx.Msg.Chat.ID
|
||||
draft.messageThreadID = ctx.Msg.MessageThreadID
|
||||
return draft
|
||||
}
|
||||
func (ctx *MsgContext) Translate(key string) string {
|
||||
if ctx.From == nil {
|
||||
return key
|
||||
|
||||
+16
-11
@@ -93,37 +93,42 @@ func (c *Command[T]) validateArgs(args []string) error {
|
||||
}
|
||||
|
||||
type Plugin[T DbContext] struct {
|
||||
Name string
|
||||
Commands map[string]Command[T]
|
||||
Payloads map[string]Command[T]
|
||||
Middlewares extypes.Slice[Middleware[T]]
|
||||
name string
|
||||
commands map[string]Command[T]
|
||||
payloads map[string]Command[T]
|
||||
middlewares extypes.Slice[Middleware[T]]
|
||||
skipAutoCmd bool
|
||||
}
|
||||
|
||||
func NewPlugin[T DbContext](name string) *Plugin[T] {
|
||||
return &Plugin[T]{
|
||||
name, map[string]Command[T]{},
|
||||
map[string]Command[T]{}, extypes.Slice[Middleware[T]]{},
|
||||
map[string]Command[T]{}, extypes.Slice[Middleware[T]]{}, false,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Plugin[T]) AddCommand(command *Command[T]) *Plugin[T] {
|
||||
p.Commands[command.command] = *command
|
||||
p.commands[command.command] = *command
|
||||
return p
|
||||
}
|
||||
func (p *Plugin[T]) NewCommand(exec CommandExecutor[T], command string, args ...CommandArg) *Command[T] {
|
||||
return NewCommand(exec, command, args...)
|
||||
}
|
||||
func (p *Plugin[T]) AddPayload(command *Command[T]) *Plugin[T] {
|
||||
p.Payloads[command.command] = *command
|
||||
p.payloads[command.command] = *command
|
||||
return p
|
||||
}
|
||||
func (p *Plugin[T]) AddMiddleware(middleware Middleware[T]) *Plugin[T] {
|
||||
p.Middlewares = p.Middlewares.Push(middleware)
|
||||
p.middlewares = p.middlewares.Push(middleware)
|
||||
return p
|
||||
}
|
||||
func (p *Plugin[T]) SkipCommandAutoGen() *Plugin[T] {
|
||||
p.skipAutoCmd = true
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *Plugin[T]) executeCmd(cmd string, ctx *MsgContext, dbContext *T) {
|
||||
command := p.Commands[cmd]
|
||||
command := p.commands[cmd]
|
||||
if err := command.validateArgs(ctx.Args); err != nil {
|
||||
ctx.error(err)
|
||||
return
|
||||
@@ -131,7 +136,7 @@ func (p *Plugin[T]) executeCmd(cmd string, ctx *MsgContext, dbContext *T) {
|
||||
command.exec(ctx, dbContext)
|
||||
}
|
||||
func (p *Plugin[T]) executePayload(payload string, ctx *MsgContext, dbContext *T) {
|
||||
pl := p.Payloads[payload]
|
||||
pl := p.payloads[payload]
|
||||
if err := pl.validateArgs(ctx.Args); err != nil {
|
||||
ctx.error(err)
|
||||
return
|
||||
@@ -139,7 +144,7 @@ func (p *Plugin[T]) executePayload(payload string, ctx *MsgContext, dbContext *T
|
||||
pl.exec(ctx, dbContext)
|
||||
}
|
||||
func (p *Plugin[T]) executeMiddlewares(ctx *MsgContext, db *T) bool {
|
||||
for _, m := range p.Middlewares {
|
||||
for _, m := range p.middlewares {
|
||||
if !m.Execute(ctx, db) {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -56,6 +56,7 @@ type PromoteChatMember struct {
|
||||
CanPinMessages bool `json:"can_pin_messages,omitempty"`
|
||||
CanManageTopics bool `json:"can_manage_topics,omitempty"`
|
||||
CanManageDirectMessages bool `json:"can_manage_direct_messages,omitempty"`
|
||||
CanManageTags bool `json:"can_manage_tags,omitempty"`
|
||||
}
|
||||
|
||||
func (api *API) PromoteChatMember(params PromoteChatMember) (bool, error) {
|
||||
@@ -74,6 +75,17 @@ func (api *API) SetChatAdministratorCustomTitle(params SetChatAdministratorCusto
|
||||
return req.Do(api)
|
||||
}
|
||||
|
||||
type SetChatMemberTagP struct {
|
||||
ChatID int `json:"chat_id"`
|
||||
UserID int `json:"user_id"`
|
||||
Tag string `json:"tag,omitempty"`
|
||||
}
|
||||
|
||||
func (api *API) SetChatMemberTag(params SetChatMemberTagP) (bool, error) {
|
||||
req := NewRequest[bool]("setChatMemberTag", params)
|
||||
return req.Do(api)
|
||||
}
|
||||
|
||||
type BanChatSenderChatP struct {
|
||||
ChatID int `json:"chat_id"`
|
||||
SenderChatID int `json:"sender_chat_id"`
|
||||
|
||||
@@ -99,6 +99,7 @@ type ChatPermissions struct {
|
||||
CanSendPolls bool `json:"can_send_polls"`
|
||||
CanSendOtherMessages bool `json:"can_send_other_messages"`
|
||||
CanAddWebPagePreview bool `json:"can_add_web_page_preview"`
|
||||
CatEditTag bool `json:"cat_edit_tag"`
|
||||
CanChangeInfo bool `json:"can_change_info"`
|
||||
CanInviteUsers bool `json:"can_invite_users"`
|
||||
CanPinMessages bool `json:"can_pin_messages"`
|
||||
@@ -137,6 +138,7 @@ const (
|
||||
type ChatMember struct {
|
||||
Status ChatMemberStatusType `json:"status"`
|
||||
User User `json:"user"`
|
||||
Tag string `json:"tag,omitempty"`
|
||||
|
||||
// Owner
|
||||
IsAnonymous *bool `json:"is_anonymous"`
|
||||
@@ -160,6 +162,7 @@ type ChatMember struct {
|
||||
CanPinMessages *bool `json:"can_pin_messages,omitempty"`
|
||||
CanManageTopics *bool `json:"can_manage_topics,omitempty"`
|
||||
CanManageDirectMessages *bool `json:"can_manage_direct_messages,omitempty"`
|
||||
CanManageTags *bool `json:"can_manage_tags,omitempty"`
|
||||
|
||||
// Member
|
||||
UntilDate *int `json:"until_date,omitempty"`
|
||||
@@ -175,6 +178,7 @@ type ChatMember struct {
|
||||
CanSendPolls *bool `json:"can_send_polls,omitempty"`
|
||||
CanSendOtherMessages *bool `json:"can_send_other_messages,omitempty"`
|
||||
CanAddWebPagePreview *bool `json:"can_add_web_page_preview,omitempty"`
|
||||
CanEditTag *bool `json:"can_edit_tag,omitempty"`
|
||||
}
|
||||
|
||||
type ChatBoostSource struct {
|
||||
@@ -215,6 +219,7 @@ type ChatAdministratorRights struct {
|
||||
CanPinMessages *bool `json:"can_pin_messages,omitempty"`
|
||||
CanManageTopics *bool `json:"can_manage_topics,omitempty"`
|
||||
CanManageDirectMessages *bool `json:"can_manage_direct_messages,omitempty"`
|
||||
CanManageTags *bool `json:"can_manage_tags,omitempty"`
|
||||
}
|
||||
|
||||
type ChatBoostUpdated struct {
|
||||
|
||||
@@ -4,7 +4,7 @@ type SendMessageP struct {
|
||||
BusinessConnectionID string `json:"business_connection_id,omitempty"`
|
||||
ChatID int `json:"chat_id"`
|
||||
MessageThreadID int `json:"message_thread_id,omitempty"`
|
||||
DirectMessagesTopicID int `json:"direct_messages_topic_id,omitempty"`
|
||||
DirectMessagesTopicID int64 `json:"direct_messages_topic_id,omitempty"`
|
||||
|
||||
Text string `json:"text"`
|
||||
ParseMode ParseMode `json:"parse_mode,omitempty"`
|
||||
@@ -268,7 +268,7 @@ func (api *API) SendDice(params SendDiceP) (Message, error) {
|
||||
type SendMessageDraftP struct {
|
||||
ChatID int `json:"chat_id"`
|
||||
MessageThreadID int `json:"message_thread_id,omitempty"`
|
||||
DraftID int `json:"draft_id"`
|
||||
DraftID uint64 `json:"draft_id"`
|
||||
Text string `json:"text"`
|
||||
ParseMode ParseMode `json:"parse_mode,omitempty"`
|
||||
Entities []MessageEntity `json:"entities,omitempty"`
|
||||
|
||||
+20
-9
@@ -6,16 +6,23 @@ type MessageReplyMarkup struct {
|
||||
InlineKeyboard [][]InlineKeyboardButton `json:"inline_keyboard"`
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
MessageID int `json:"message_id"`
|
||||
MessageThreadID int `json:"message_thread_id,omitempty"`
|
||||
BusinessConnectionId string `json:"business_connection_id,omitempty"`
|
||||
From *User `json:"from,omitempty"`
|
||||
type DirectMessageTopic struct {
|
||||
TopicID int64 `json:"topic_id"`
|
||||
User *User `json:"user,omitempty"`
|
||||
}
|
||||
|
||||
SenderChat *Chat `json:"sender_chat,omitempty"`
|
||||
SenderBoostCount int `json:"sender_boost_count,omitempty"`
|
||||
SenderBusinessBot *User `json:"sender_business_bot,omitempty"`
|
||||
Chat *Chat `json:"chat,omitempty"`
|
||||
type Message struct {
|
||||
MessageID int `json:"message_id"`
|
||||
MessageThreadID int `json:"message_thread_id,omitempty"`
|
||||
DirectMessageTopic *DirectMessageTopic `json:"direct_message_topic,omitempty"`
|
||||
BusinessConnectionId string `json:"business_connection_id,omitempty"`
|
||||
From *User `json:"from,omitempty"`
|
||||
|
||||
SenderChat *Chat `json:"sender_chat,omitempty"`
|
||||
SenderBoostCount int `json:"sender_boost_count,omitempty"`
|
||||
SenderBusinessBot *User `json:"sender_business_bot,omitempty"`
|
||||
SenderTag string `json:"sender_tag,omitempty"`
|
||||
Chat *Chat `json:"chat,omitempty"`
|
||||
|
||||
IsTopicMessage bool `json:"is_topic_message,omitempty"`
|
||||
IsAutomaticForward bool `json:"is_automatic_forward,omitempty"`
|
||||
@@ -74,6 +81,7 @@ const (
|
||||
MessageEntityTextLink MessageEntityType = "text_link"
|
||||
MessageEntityTextMention MessageEntityType = "text_mention"
|
||||
MessageEntityCustomEmoji MessageEntityType = "custom_emoji"
|
||||
MessageEntityDateTime MessageEntityType = "date_time"
|
||||
)
|
||||
|
||||
type MessageEntity struct {
|
||||
@@ -85,6 +93,9 @@ type MessageEntity struct {
|
||||
User *User `json:"user,omitempty"`
|
||||
Language string `json:"language,omitempty"`
|
||||
CustomEmojiID string `json:"custom_emoji_id,omitempty"`
|
||||
|
||||
UnixTime int `json:"unix_time,omitempty"`
|
||||
DateTimeFormat string `json:"date_time_format,omitempty"`
|
||||
}
|
||||
|
||||
type ReplyParameters struct {
|
||||
|
||||
+4
-4
@@ -1,9 +1,9 @@
|
||||
package utils
|
||||
|
||||
const (
|
||||
VersionString = "0.8.0-beta.4"
|
||||
VersionMajor = 0
|
||||
VersionMinor = 8
|
||||
VersionString = "1.0.0-beta.6"
|
||||
VersionMajor = 1
|
||||
VersionMinor = 0
|
||||
VersionPatch = 0
|
||||
Beta = 4
|
||||
Beta = 6
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user