Compare commits
5 Commits
v0.3.1
...
6d6f5738cd
| Author | SHA1 | Date | |
|---|---|---|---|
| 6d6f5738cd | |||
| 95516480b0 | |||
| 9da4115fe7 | |||
| fef718438a | |||
| 7f248fff62 |
96
bot.go
96
bot.go
@@ -1,7 +1,6 @@
|
|||||||
package laniakea
|
package laniakea
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
@@ -180,19 +179,20 @@ func (b *Bot) AddPlugins(plugin ...*Plugin) *Bot {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
func (b *Bot) AddMiddleware(middleware ...*Middleware) *Bot {
|
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...)
|
b.middlewares = append(b.middlewares, middleware...)
|
||||||
for _, m := range middleware {
|
for _, m := range middleware {
|
||||||
b.logger.Debugln(fmt.Sprintf("middleware with name \"%s\" registered", m.Name))
|
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
|
return b
|
||||||
}
|
}
|
||||||
func (b *Bot) AddRunner(runner Runner) *Bot {
|
func (b *Bot) AddRunner(runner Runner) *Bot {
|
||||||
@@ -243,6 +243,7 @@ func (b *Bot) Run() {
|
|||||||
b.logger.Errorln("update is nil")
|
b.logger.Errorln("update is nil")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := &MsgContext{
|
ctx := &MsgContext{
|
||||||
Bot: b, Update: u,
|
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
79
handler.go
Normal 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
|
||||||
|
}
|
||||||
2
queue.go
2
queue.go
@@ -5,7 +5,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
var QueueFullErr = errors.New("queue is full")
|
var QueueFullErr = errors.New("queue full")
|
||||||
|
|
||||||
type Queue[T any] struct {
|
type Queue[T any] struct {
|
||||||
size uint64
|
size uint64
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package laniakea
|
package laniakea
|
||||||
|
|
||||||
const (
|
const (
|
||||||
VersionString = "0.3.0"
|
VersionString = "0.3.2"
|
||||||
VersionMajor = 0
|
VersionMajor = 0
|
||||||
VersionMinor = 3
|
VersionMinor = 3
|
||||||
VersionPatch = 0
|
VersionPatch = 2
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user