v1.0.0 beta 1
This commit is contained in:
91
bot.go
91
bot.go
@@ -1,16 +1,19 @@
|
|||||||
package laniakea
|
package laniakea
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"sync"
|
||||||
|
|
||||||
"git.nix13.pw/scuroneko/extypes"
|
"git.nix13.pw/scuroneko/extypes"
|
||||||
"git.nix13.pw/scuroneko/laniakea/tgapi"
|
"git.nix13.pw/scuroneko/laniakea/tgapi"
|
||||||
"git.nix13.pw/scuroneko/slog"
|
"git.nix13.pw/scuroneko/slog"
|
||||||
|
"github.com/alitto/pond/v2"
|
||||||
"golang.org/x/time/rate"
|
"golang.org/x/time/rate"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -87,13 +90,14 @@ type Bot[T DbContext] struct {
|
|||||||
dbContext *T
|
dbContext *T
|
||||||
l10n *L10n
|
l10n *L10n
|
||||||
|
|
||||||
updateOffset int
|
updateOffsetMu sync.Mutex
|
||||||
updateTypes []tgapi.UpdateType
|
updateOffset int
|
||||||
updateQueue *extypes.Queue[*tgapi.Update]
|
updateTypes []tgapi.UpdateType
|
||||||
|
updateQueue chan *tgapi.Update
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBot[T any](opts *BotOpts) *Bot[T] {
|
func NewBot[T any](opts *BotOpts) *Bot[T] {
|
||||||
updateQueue := extypes.CreateQueue[*tgapi.Update](512)
|
updateQueue := make(chan *tgapi.Update, 512)
|
||||||
|
|
||||||
var limiter *rate.Limiter
|
var limiter *rate.Limiter
|
||||||
if opts.RateLimit > 0 {
|
if opts.RateLimit > 0 {
|
||||||
@@ -185,13 +189,20 @@ func (bot *Bot[T]) initLoggers(opts *BotOpts) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bot *Bot[T]) GetUpdateOffset() int { return bot.updateOffset }
|
func (bot *Bot[T]) GetUpdateOffset() int {
|
||||||
func (bot *Bot[T]) SetUpdateOffset(offset int) { bot.updateOffset = offset }
|
bot.updateOffsetMu.Lock()
|
||||||
func (bot *Bot[T]) GetUpdateTypes() []tgapi.UpdateType { return bot.updateTypes }
|
defer bot.updateOffsetMu.Unlock()
|
||||||
func (bot *Bot[T]) GetQueue() *extypes.Queue[*tgapi.Update] { return bot.updateQueue }
|
return bot.updateOffset
|
||||||
func (bot *Bot[T]) GetLogger() *slog.Logger { return bot.logger }
|
}
|
||||||
func (bot *Bot[T]) GetDBContext() *T { return bot.dbContext }
|
func (bot *Bot[T]) SetUpdateOffset(offset int) {
|
||||||
func (bot *Bot[T]) L10n(lang, key string) string { return bot.l10n.Translate(lang, key) }
|
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) }
|
||||||
|
|
||||||
type DbLogger[T DbContext] func(db *T) slog.LoggerWriter
|
type DbLogger[T DbContext] func(db *T) slog.LoggerWriter
|
||||||
|
|
||||||
@@ -235,7 +246,7 @@ func (bot *Bot[T]) Debug(debug bool) *Bot[T] {
|
|||||||
func (bot *Bot[T]) AddPlugins(plugin ...*Plugin[T]) *Bot[T] {
|
func (bot *Bot[T]) AddPlugins(plugin ...*Plugin[T]) *Bot[T] {
|
||||||
for _, p := range plugin {
|
for _, p := range plugin {
|
||||||
bot.plugins = append(bot.plugins, *p)
|
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
|
return bot
|
||||||
}
|
}
|
||||||
@@ -266,7 +277,15 @@ func (bot *Bot[T]) AddL10n(l *L10n) *Bot[T] {
|
|||||||
return bot
|
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 {
|
if len(bot.prefixes) == 0 {
|
||||||
bot.logger.Fatalln("no prefixes defined")
|
bot.logger.Fatalln("no prefixes defined")
|
||||||
return
|
return
|
||||||
@@ -282,26 +301,36 @@ func (bot *Bot[T]) Run() {
|
|||||||
bot.logger.Infoln("Bot running. Press CTRL+C to exit.")
|
bot.logger.Infoln("Bot running. Press CTRL+C to exit.")
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
_, err := bot.Updates()
|
select {
|
||||||
if err != nil {
|
case <-ctx.Done():
|
||||||
bot.logger.Errorln(err)
|
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 {
|
pool := pond.NewPool(16)
|
||||||
queue := bot.updateQueue
|
for update := range bot.updateQueue {
|
||||||
if queue.IsEmpty() {
|
update := update
|
||||||
time.Sleep(time.Millisecond * 25)
|
log.Println(update)
|
||||||
continue
|
pool.Submit(func() {
|
||||||
}
|
bot.handle(update)
|
||||||
|
})
|
||||||
u := queue.Dequeue()
|
|
||||||
if u == nil {
|
|
||||||
bot.logger.Errorln("update is nil")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
bot.handle(u)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func (bot *Bot[T]) Run() {
|
||||||
|
bot.RunWithContext(context.Background())
|
||||||
|
}
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ func generateBotCommand[T any](cmd Command[T]) tgapi.BotCommand {
|
|||||||
|
|
||||||
func generateBotCommandForPlugin[T any](pl Plugin[T]) []tgapi.BotCommand {
|
func generateBotCommandForPlugin[T any](pl Plugin[T]) []tgapi.BotCommand {
|
||||||
commands := make([]tgapi.BotCommand, 0)
|
commands := make([]tgapi.BotCommand, 0)
|
||||||
for _, cmd := range pl.Commands {
|
for _, cmd := range pl.commands {
|
||||||
if cmd.skipAutoCmd {
|
if cmd.skipAutoCmd {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -46,6 +46,10 @@ func (bot *Bot[T]) AutoGenerateCommands() error {
|
|||||||
|
|
||||||
commands := make([]tgapi.BotCommand, 0)
|
commands := make([]tgapi.BotCommand, 0)
|
||||||
for _, pl := range bot.plugins {
|
for _, pl := range bot.plugins {
|
||||||
|
if pl.skipAutoCmd {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
commands = append(commands, generateBotCommandForPlugin(pl)...)
|
commands = append(commands, generateBotCommandForPlugin(pl)...)
|
||||||
}
|
}
|
||||||
if len(commands) > 100 {
|
if len(commands) > 100 {
|
||||||
|
|||||||
4
go.mod
4
go.mod
@@ -3,13 +3,13 @@ module git.nix13.pw/scuroneko/laniakea
|
|||||||
go 1.26
|
go 1.26
|
||||||
|
|
||||||
require (
|
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
|
git.nix13.pw/scuroneko/slog v1.0.2
|
||||||
golang.org/x/time v0.14.0
|
golang.org/x/time v0.14.0
|
||||||
|
github.com/alitto/pond/v2 v2.6.2
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/alitto/pond/v2 v2.6.2 // indirect
|
|
||||||
github.com/fatih/color v1.18.0 // indirect
|
github.com/fatih/color v1.18.0 // indirect
|
||||||
github.com/mattn/go-colorable v0.1.14 // indirect
|
github.com/mattn/go-colorable v0.1.14 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
|
|||||||
1
go.sum
1
go.sum
@@ -1,5 +1,6 @@
|
|||||||
git.nix13.pw/scuroneko/extypes v1.2.0 h1:2n2hD6KsMAted+6MGhAyeWyli2Qzc9G2y+pQNB7C1dM=
|
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.0/go.mod h1:uZVs8Yo3RrYAG9dMad6qR6lsYY67t+459D9c65QAYAw=
|
||||||
|
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 h1:vZyUROygxC2d5FJHUQM/30xFEHY1JT/aweDZXA4rm2g=
|
||||||
git.nix13.pw/scuroneko/slog v1.0.2/go.mod h1:3Qm2wzkR5KjwOponMfG7TcGSDjmYaFqRAmLvSPTuWJI=
|
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=
|
github.com/alitto/pond/v2 v2.6.2 h1:Sphe40g0ILeM1pA2c2K+Th0DGU+pt0A/Kprr+WB24Pw=
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ func (bot *Bot[T]) handleMessage(update *tgapi.Update, ctx *MsgContext) {
|
|||||||
text = strings.TrimSpace(text[len(prefix):])
|
text = strings.TrimSpace(text[len(prefix):])
|
||||||
|
|
||||||
for _, plugin := range bot.plugins {
|
for _, plugin := range bot.plugins {
|
||||||
for cmd := range plugin.Commands {
|
for cmd := range plugin.commands {
|
||||||
if !strings.HasPrefix(text, cmd) {
|
if !strings.HasPrefix(text, cmd) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -96,7 +96,7 @@ func (bot *Bot[T]) handleCallback(update *tgapi.Update, ctx *MsgContext) {
|
|||||||
ctx.Args = data.Args
|
ctx.Args = data.Args
|
||||||
|
|
||||||
for _, plugin := range bot.plugins {
|
for _, plugin := range bot.plugins {
|
||||||
_, ok := plugin.Payloads[data.Command]
|
_, ok := plugin.payloads[data.Command]
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
13
methods.go
13
methods.go
@@ -19,14 +19,8 @@ func (bot *Bot[T]) Updates() ([]tgapi.Update, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, u := range updates {
|
if bot.RequestLogger != nil {
|
||||||
bot.SetUpdateOffset(u.UpdateID + 1)
|
for _, u := range updates {
|
||||||
err = bot.GetQueue().Enqueue(&u)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if bot.RequestLogger != nil {
|
|
||||||
j, err := json.Marshal(u)
|
j, err := json.Marshal(u)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
bot.GetLogger().Error(err)
|
bot.GetLogger().Error(err)
|
||||||
@@ -34,5 +28,8 @@ func (bot *Bot[T]) Updates() ([]tgapi.Update, error) {
|
|||||||
bot.RequestLogger.Debugf("UPDATE %s\n", j)
|
bot.RequestLogger.Debugf("UPDATE %s\n", j)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if len(updates) > 0 {
|
||||||
|
bot.SetUpdateOffset(updates[len(updates)-1].UpdateID + 1)
|
||||||
|
}
|
||||||
return updates, err
|
return updates, err
|
||||||
}
|
}
|
||||||
|
|||||||
27
plugins.go
27
plugins.go
@@ -93,37 +93,42 @@ func (c *Command[T]) validateArgs(args []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Plugin[T DbContext] struct {
|
type Plugin[T DbContext] struct {
|
||||||
Name string
|
name string
|
||||||
Commands map[string]Command[T]
|
commands map[string]Command[T]
|
||||||
Payloads map[string]Command[T]
|
payloads map[string]Command[T]
|
||||||
Middlewares extypes.Slice[Middleware[T]]
|
middlewares extypes.Slice[Middleware[T]]
|
||||||
|
skipAutoCmd bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPlugin[T DbContext](name string) *Plugin[T] {
|
func NewPlugin[T DbContext](name string) *Plugin[T] {
|
||||||
return &Plugin[T]{
|
return &Plugin[T]{
|
||||||
name, map[string]Command[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] {
|
func (p *Plugin[T]) AddCommand(command *Command[T]) *Plugin[T] {
|
||||||
p.Commands[command.command] = *command
|
p.commands[command.command] = *command
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
func (p *Plugin[T]) NewCommand(exec CommandExecutor[T], command string, args ...CommandArg) *Command[T] {
|
func (p *Plugin[T]) NewCommand(exec CommandExecutor[T], command string, args ...CommandArg) *Command[T] {
|
||||||
return NewCommand(exec, command, args...)
|
return NewCommand(exec, command, args...)
|
||||||
}
|
}
|
||||||
func (p *Plugin[T]) AddPayload(command *Command[T]) *Plugin[T] {
|
func (p *Plugin[T]) AddPayload(command *Command[T]) *Plugin[T] {
|
||||||
p.Payloads[command.command] = *command
|
p.payloads[command.command] = *command
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
func (p *Plugin[T]) AddMiddleware(middleware Middleware[T]) *Plugin[T] {
|
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
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Plugin[T]) executeCmd(cmd string, ctx *MsgContext, dbContext *T) {
|
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 {
|
if err := command.validateArgs(ctx.Args); err != nil {
|
||||||
ctx.error(err)
|
ctx.error(err)
|
||||||
return
|
return
|
||||||
@@ -131,7 +136,7 @@ func (p *Plugin[T]) executeCmd(cmd string, ctx *MsgContext, dbContext *T) {
|
|||||||
command.exec(ctx, dbContext)
|
command.exec(ctx, dbContext)
|
||||||
}
|
}
|
||||||
func (p *Plugin[T]) executePayload(payload string, ctx *MsgContext, dbContext *T) {
|
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 {
|
if err := pl.validateArgs(ctx.Args); err != nil {
|
||||||
ctx.error(err)
|
ctx.error(err)
|
||||||
return
|
return
|
||||||
@@ -139,7 +144,7 @@ func (p *Plugin[T]) executePayload(payload string, ctx *MsgContext, dbContext *T
|
|||||||
pl.exec(ctx, dbContext)
|
pl.exec(ctx, dbContext)
|
||||||
}
|
}
|
||||||
func (p *Plugin[T]) executeMiddlewares(ctx *MsgContext, db *T) bool {
|
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) {
|
if !m.Execute(ctx, db) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
const (
|
const (
|
||||||
VersionString = "0.8.0-beta.4"
|
VersionString = "1.0.0-beta.1"
|
||||||
VersionMajor = 0
|
VersionMajor = 1
|
||||||
VersionMinor = 8
|
VersionMinor = 0
|
||||||
VersionPatch = 0
|
VersionPatch = 0
|
||||||
Beta = 4
|
Beta = 1
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user