From 2e9e82d43b3b39409e69c670539cc408db96da2c Mon Sep 17 00:00:00 2001 From: ScuroNeko Date: Wed, 4 Feb 2026 12:49:12 +0300 Subject: [PATCH] slices, queue and map now independent module --- README.md | 3 + bot.go | 11 +++- go.mod | 3 +- go.sum | 2 + map.go | 46 -------------- methods.go | 46 ++++++++++++++ queue.go | 71 --------------------- slice.go | 176 ----------------------------------------------------- types.go | 8 ++- 9 files changed, 67 insertions(+), 299 deletions(-) create mode 100644 README.md delete mode 100644 map.go delete mode 100644 queue.go delete mode 100644 slice.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..7e8892d --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Laniakea + +A lightweight, easy to use and performance Telegram API wrapper for bot development. \ No newline at end of file diff --git a/bot.go b/bot.go index f105fcf..0ba6f62 100644 --- a/bot.go +++ b/bot.go @@ -8,6 +8,7 @@ import ( "strings" "time" + "git.nix13.pw/scuroneko/extypes" "git.nix13.pw/scuroneko/slog" "github.com/redis/go-redis/v9" "github.com/vinovest/sqlx" @@ -39,7 +40,7 @@ type Bot struct { updateOffset int updateTypes []string - updateQueue *Queue[*Update] + updateQueue *extypes.Queue[*Update] } type BotSettings struct { @@ -73,7 +74,7 @@ func LoadPrefixesFromEnv() []string { return strings.Split(prefixesS, ";") } func NewBot(settings *BotSettings) *Bot { - updateQueue := CreateQueue[*Update](256) + updateQueue := extypes.CreateQueue[*Update](256) bot := &Bot{ updateOffset: 0, plugins: make([]Plugin, 0), debug: settings.Debug, errorTemplate: "%s", prefixes: settings.Prefixes, updateTypes: make([]string, 0), runners: make([]Runner, 0), @@ -117,6 +118,12 @@ func NewBot(settings *BotSettings) *Bot { } } + u, err := bot.GetMe() + if err != nil { + bot.logger.Fatal(err) + } + bot.logger.Infof("Authorized as %s\n", u.FirstName) + return bot } diff --git a/go.mod b/go.mod index 8c7f617..bf2cc14 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,9 @@ module git.nix13.pw/scuroneko/laniakea -go 1.25 +go 1.25.6 require ( + git.nix13.pw/scuroneko/extypes v1.0.2 git.nix13.pw/scuroneko/slog v1.0.2 github.com/redis/go-redis/v9 v9.17.3 github.com/vinovest/sqlx v1.7.1 diff --git a/go.sum b/go.sum index 75c96d6..e737cc0 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= +git.nix13.pw/scuroneko/extypes v1.0.2 h1:Qz1InLccaB9crXY33oGrSetPHePKfQAUqW/p/iYXmJk= +git.nix13.pw/scuroneko/extypes v1.0.2/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/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= diff --git a/map.go b/map.go deleted file mode 100644 index 0509887..0000000 --- a/map.go +++ /dev/null @@ -1,46 +0,0 @@ -package laniakea - -type HashMap[K comparable, V any] map[K]V - -func NewMapFrom[K comparable, V any](m map[K]V) HashMap[K, V] { - out := make(HashMap[K, V]) - for k, v := range m { - out[k] = v - } - return out -} -func (m HashMap[K, V]) Len() int { - return len(m) -} -func (m HashMap[K, V]) Add(key K, val V) HashMap[K, V] { - m[key] = val - return m -} -func (m HashMap[K, V]) Remove(key K) HashMap[K, V] { - delete(m, key) - return m -} -func (m HashMap[K, V]) Get(key K) V { - return m[key] -} -func (m HashMap[K, V]) GetOrDefault(key K, def V) V { - v, ok := m[key] - if !ok { - return def - } - return v -} -func (m HashMap[K, V]) Contains(key K) bool { - _, ok := m[key] - return ok -} -func (m HashMap[K, V]) Filter(f func(K, V) bool) HashMap[K, V] { - out := make(HashMap[K, V]) - for k, v := range m { - if !f(k, v) { - continue - } - out[k] = v - } - return out -} diff --git a/methods.go b/methods.go index f0cdcfb..81ef1b6 100644 --- a/methods.go +++ b/methods.go @@ -195,3 +195,49 @@ func (b *Bot) SendChatAction(params SendChatActionP) (bool, error) { } return *res, err } + +type SetMessageReactionP struct { + ChatId int `json:"chat_id"` + MessageId int `json:"message_id"` + IsBig bool `json:"is_big,omitempty"` +} +type SetMessageReactionEmojiP struct { + SetMessageReactionP + Reaction []ReactionTypeEmoji `json:"reaction"` +} + +func (b *Bot) SetMessageReactionEmoji(params SetMessageReactionEmojiP) (bool, error) { + req := NewRequest[bool]("setMessageReaction", params) + res, err := req.Do(b) + if err != nil { + return false, err + } + return *res, err +} + +type SetMessageReactionCustomEmojiP struct { + SetMessageReactionP + Reaction []ReactionTypeCustomEmoji `json:"reaction"` +} + +func (b *Bot) SetMessageReactionCustom(params SetMessageReactionCustomEmojiP) (bool, error) { + req := NewRequest[bool]("setMessageReaction", params) + res, err := req.Do(b) + if err != nil { + return false, err + } + return *res, err +} + +type SetMessageReactionPaidP struct { + SetMessageReactionP +} + +func (b *Bot) SetMessageReactionPaid(params SetMessageReactionPaidP) (bool, error) { + req := NewRequest[bool]("setMessageReaction", params) + res, err := req.Do(b) + if err != nil { + return false, err + } + return *res, err +} diff --git a/queue.go b/queue.go deleted file mode 100644 index d319ce7..0000000 --- a/queue.go +++ /dev/null @@ -1,71 +0,0 @@ -package laniakea - -import ( - "errors" - "sync" -) - -var QueueFullErr = errors.New("queue full") - -type Queue[T any] struct { - size uint64 - mu sync.RWMutex - queue []T -} - -func CreateQueue[T any](size uint64) *Queue[T] { - return &Queue[T]{ - queue: make([]T, 0), - size: size, - } -} - -func (q *Queue[T]) Enqueue(el T) error { - if q.IsFull() { - return QueueFullErr - } - q.queue = append(q.queue, el) - return nil -} - -func (q *Queue[T]) Peak() T { - q.mu.RLock() - defer q.mu.RUnlock() - return q.queue[0] -} - -func (q *Queue[T]) IsEmpty() bool { - return q.Length() == 0 -} - -func (q *Queue[T]) IsFull() bool { - return q.Length() == q.size -} - -func (q *Queue[T]) Length() uint64 { - q.mu.RLock() - defer q.mu.RUnlock() - return uint64(len(q.queue)) -} - -func (q *Queue[T]) Dequeue() T { - q.mu.RLock() - el := q.queue[0] - q.mu.RUnlock() - - if q.Length() == 1 { - q.mu.Lock() - q.queue = make([]T, 0) - q.mu.Unlock() - return el - } - - q.mu.Lock() - q.queue = q.queue[1:] - q.mu.Unlock() - return el -} - -func (q *Queue[T]) Raw() []T { - return q.queue -} diff --git a/slice.go b/slice.go deleted file mode 100644 index aa88a57..0000000 --- a/slice.go +++ /dev/null @@ -1,176 +0,0 @@ -package laniakea - -import "slices" - -type Slice[T comparable] []T - -func NewSliceFrom[T comparable](slice []T) Slice[T] { - s := make(Slice[T], len(slice)) - copy(s, slice) - return s -} -func (s Slice[T]) Len() int { - return len(s) -} -func (s Slice[T]) Cap() int { - return cap(s) -} -func (s Slice[T]) Get(index int) T { - return s[index] -} -func (s Slice[T]) Last() T { - return s.Get(s.Len() - 1) -} -func (s Slice[T]) Index(el T) int { - return slices.Index(s, el) -} - -// Swap is mutable func -func (s Slice[T]) Swap(i, j int) Slice[T] { - s[i], s[j] = s[j], s[i] - return s -} -func (s Slice[T]) Filter(f func(e T) bool) Slice[T] { - out := make(Slice[T], 0) - for _, v := range s { - if f(v) { - out = append(out, v) - } - } - return out -} -func (s Slice[T]) Map(f func(e T) T) Slice[T] { - out := make(Slice[T], s.Len()) - for i, v := range s { - out[i] = f(v) - } - return out -} -func (s Slice[T]) Pop(index int) Slice[T] { - if index == 0 { - return s[1:] - } - out := make(Slice[T], s.Len()-index) - for i, e := range s { - if i == index { - continue - } - out[i] = e - } - return out -} -func (s Slice[T]) Remove(el T) Slice[T] { - index := s.Index(el) - if index == -1 { - return s - } - return s.Pop(index) -} -func (s Slice[T]) Push(e T) Slice[T] { - return append(s, e) -} - -func (s Slice[T]) ToArray() []T { - out := make([]T, len(s)) - copy(out, s) - return out -} -func (s Slice[T]) ToAnyArray() []any { - out := make([]any, len(s)) - for i, v := range s { - out[i] = v - } - return out -} - -// ForEach is mutable func -func (s Slice[T]) ForEach(f func(e T) T) Slice[T] { - for i, v := range s { - s[i] = f(v) - } - return s -} - -type Set[T comparable] []T - -func NewSetFrom[T comparable](slice []T) Set[T] { - s := make(Set[T], 0) - for _, v := range slice { - if s.Index(v) >= 0 { - continue - } - s = append(s, v) - } - return s -} -func (s Set[T]) Len() int { - return len(s) -} -func (s Set[T]) Cap() int { - return cap(s) -} -func (s Set[T]) Get(index int) T { - return s[index] -} -func (s Set[T]) Last() T { - return s[s.Len()-1] -} -func (s Set[T]) Index(el T) int { - return slices.Index(s, el) -} -func (s Set[T]) Swap(i, j int) { - s[i], s[j] = s[j], s[i] -} -func (s Set[T]) Add(v T) Set[T] { - index := s.Index(v) - if index >= 0 { - return s - } - return append(s, v) -} -func (s Set[T]) Pop(index int) Set[T] { - if index == 0 { - return s[1:] - } - out := make(Set[T], s.Len()-index) - for i, e := range s { - if i == index { - continue - } - out[i] = e - } - return out -} -func (s Set[T]) Remove(el T) Set[T] { - index := s.Index(el) - if index == -1 { - return s - } - return s.Pop(index) -} -func (s Set[T]) Filter(f func(e T) bool) Set[T] { - out := make(Set[T], 0) - for _, v := range s { - if f(v) { - out = append(out, v) - } - } - return out -} -func (s Set[T]) ToSlice() Slice[T] { - out := make(Slice[T], s.Len()) - copy(out, s) - return out -} -func (s Set[T]) ToArray() []T { - out := make([]T, len(s)) - copy(out, s) - return out -} -func (s Set[T]) ToAnyArray() []any { - out := make([]any, len(s)) - for i, v := range s { - out[i] = v - } - return out -} diff --git a/types.go b/types.go index 8791e96..974f144 100644 --- a/types.go +++ b/types.go @@ -1,5 +1,7 @@ package laniakea +import "git.nix13.pw/scuroneko/extypes" + type Update struct { UpdateID int `json:"update_id"` Message *Message `json:"message"` @@ -54,9 +56,9 @@ type Message struct { Chat *Chat `json:"chat,omitempty"` Text string `json:"text"` - Photo Slice[*PhotoSize] `json:"photo,omitempty"` - Caption string `json:"caption,omitempty"` - ReplyToMessage *Message `json:"reply_to_message"` + Photo extypes.Slice[*PhotoSize] `json:"photo,omitempty"` + Caption string `json:"caption,omitempty"` + ReplyToMessage *Message `json:"reply_to_message"` ReplyMarkup *MessageReplyMarkup `json:"reply_markup,omitempty"` }