5 Commits
v0.3.1 ... main

Author SHA1 Message Date
6d6f5738cd Merge pull request 'v0.3.2' (#3) from dev into main
Reviewed-on: #3
2026-01-29 11:47:29 +03:00
95516480b0 license; v0.3.2 2026-01-29 11:46:54 +03:00
9da4115fe7 license 2026-01-29 11:45:35 +03:00
fef718438a v0.3.0 2026-01-29 09:51:50 +03:00
7f248fff62 fix 2025-11-05 11:38:09 +03:00
4 changed files with 93 additions and 88 deletions

96
bot.go
View File

@@ -1,7 +1,6 @@
package laniakea
import (
"encoding/json"
"fmt"
"log"
"os"
@@ -180,19 +179,20 @@ func (b *Bot) AddPlugins(plugin ...*Plugin) *Bot {
return b
}
func (b *Bot) AddMiddleware(middleware ...*Middleware) *Bot {
sort.Slice(middleware, func(a, b int) bool {
first := middleware[a]
second := middleware[b]
if first.Order == second.Order {
return first.Name < second.Name
}
return middleware[a].Order < middleware[b].Order
})
b.middlewares = append(b.middlewares, middleware...)
for _, m := range middleware {
b.logger.Debugln(fmt.Sprintf("middleware with name \"%s\" registered", m.Name))
}
sort.Slice(&b.middlewares, func(i, j int) bool {
first := b.middlewares[i]
second := b.middlewares[j]
if first.Order == second.Order {
return first.Name < second.Name
}
return first.Order < second.Order
})
return b
}
func (b *Bot) AddRunner(runner Runner) *Bot {
@@ -243,6 +243,7 @@ func (b *Bot) Run() {
b.logger.Errorln("update is nil")
continue
}
ctx := &MsgContext{
Bot: b, Update: u,
}
@@ -263,78 +264,3 @@ func (b *Bot) Run() {
}
}
}
// {"callback_query":{"chat_instance":"6202057960757700762","data":"aboba","from":{"first_name":"scuroneko","id":314834933,"is_bot":false,"language_code":"ru","username":"scuroneko"},"id":"1352205741990111553","message":{"chat":{"first_name":"scuroneko","id":314834933,"type":"private","username":"scuroneko"},"date":1734338107,"from":{"first_name":"Kurumi","id":7718900880,"is_bot":true,"username":"kurumi_game_bot"},"message_id":19,"reply_markup":{"inline_keyboard":[[{"callback_data":"aboba","text":"Test"},{"callback_data":"another","text":"Another"}]]},"text":"Aboba"}},"update_id":350979488}
func (b *Bot) handleMessage(update *Update, ctx *MsgContext) {
if update.Message == nil {
return
}
var text string
if len(update.Message.Text) > 0 {
text = update.Message.Text
} else {
text = update.Message.Caption
}
text = strings.TrimSpace(text)
prefix, hasPrefix := b.checkPrefixes(text)
if !hasPrefix {
return
}
ctx.Prefix = prefix
ctx.FromID = update.Message.From.ID
ctx.From = update.Message.From
ctx.Msg = update.Message
text = strings.TrimSpace(text[len(prefix):])
for _, plugin := range b.plugins {
// Check every command
for cmd := range plugin.Commands {
if !strings.HasPrefix(text, cmd) {
continue
}
ctx.Text = strings.TrimSpace(text[len(cmd):])
ctx.Args = strings.Split(ctx.Text, " ")
go plugin.Execute(cmd, ctx, b.dbContext)
return
}
}
}
func (b *Bot) handleCallback(update *Update, ctx *MsgContext) {
data := new(CallbackData)
err := json.Unmarshal([]byte(update.CallbackQuery.Data), data)
if err != nil {
b.logger.Errorln(err)
return
}
ctx.FromID = update.CallbackQuery.From.ID
ctx.From = update.CallbackQuery.From
ctx.Msg = update.CallbackQuery.Message
ctx.CallbackMsgId = update.CallbackQuery.Message.MessageID
ctx.Args = data.Args
for _, plugin := range b.plugins {
_, ok := plugin.Payloads[data.Command]
if !ok {
continue
}
go plugin.ExecutePayload(data.Command, ctx, b.dbContext)
return
}
}
func (b *Bot) checkPrefixes(text string) (string, bool) {
for _, prefix := range b.prefixes {
if strings.HasPrefix(text, prefix) {
return prefix, true
}
}
return "", false
}

79
handler.go Normal file
View File

@@ -0,0 +1,79 @@
package laniakea
import (
"encoding/json"
"strings"
)
func (b *Bot) handleMessage(update *Update, ctx *MsgContext) {
if update.Message == nil {
return
}
var text string
if len(update.Message.Text) > 0 {
text = update.Message.Text
} else {
text = update.Message.Caption
}
text = strings.TrimSpace(text)
prefix, hasPrefix := b.checkPrefixes(text)
if !hasPrefix {
return
}
ctx.Prefix = prefix
ctx.FromID = update.Message.From.ID
ctx.From = update.Message.From
ctx.Msg = update.Message
text = strings.TrimSpace(text[len(prefix):])
for _, plugin := range b.plugins {
// Check every command
for cmd := range plugin.Commands {
if !strings.HasPrefix(text, cmd) {
continue
}
ctx.Text = strings.TrimSpace(text[len(cmd):])
ctx.Args = strings.Split(ctx.Text, " ")
go plugin.Execute(cmd, ctx, b.dbContext)
return
}
}
}
func (b *Bot) handleCallback(update *Update, ctx *MsgContext) {
data := new(CallbackData)
err := json.Unmarshal([]byte(update.CallbackQuery.Data), data)
if err != nil {
b.logger.Errorln(err)
return
}
ctx.FromID = update.CallbackQuery.From.ID
ctx.From = update.CallbackQuery.From
ctx.Msg = update.CallbackQuery.Message
ctx.CallbackMsgId = update.CallbackQuery.Message.MessageID
ctx.Args = data.Args
for _, plugin := range b.plugins {
_, ok := plugin.Payloads[data.Command]
if !ok {
continue
}
go plugin.ExecutePayload(data.Command, ctx, b.dbContext)
return
}
}
func (b *Bot) checkPrefixes(text string) (string, bool) {
for _, prefix := range b.prefixes {
if strings.HasPrefix(text, prefix) {
return prefix, true
}
}
return "", false
}

View File

@@ -5,7 +5,7 @@ import (
"sync"
)
var QueueFullErr = errors.New("queue is full")
var QueueFullErr = errors.New("queue full")
type Queue[T any] struct {
size uint64

View File

@@ -1,8 +1,8 @@
package laniakea
const (
VersionString = "0.3.0"
VersionString = "0.3.2"
VersionMajor = 0
VersionMinor = 3
VersionPatch = 0
VersionPatch = 2
)