Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
2e9e82d43b
|
|||
|
55d4065259
|
|||
| b89f27574f |
3
README.md
Normal file
3
README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# Laniakea
|
||||
|
||||
A lightweight, easy to use and performance Telegram API wrapper for bot development.
|
||||
11
bot.go
11
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
|
||||
}
|
||||
|
||||
|
||||
3
go.mod
3
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
|
||||
|
||||
2
go.sum
2
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=
|
||||
|
||||
46
methods.go
46
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
|
||||
}
|
||||
|
||||
71
queue.go
71
queue.go
@@ -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
|
||||
}
|
||||
151
slice.go
151
slice.go
@@ -1,151 +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]) 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 := slices.Index(s, 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
|
||||
}
|
||||
|
||||
type Set[T comparable] []T
|
||||
|
||||
func NewSetFrom[T comparable](slice []T) Set[T] {
|
||||
s := make(Set[T], 0)
|
||||
for _, v := range slice {
|
||||
if slices.Index(slice, 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]) Swap(i, j int) {
|
||||
s[i], s[j] = s[j], s[i]
|
||||
}
|
||||
func (s Set[T]) Add(v T) Set[T] {
|
||||
index := slices.Index(s, 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 := slices.Index(s, el)
|
||||
if index == -1 {
|
||||
return s
|
||||
}
|
||||
return s.Pop(index)
|
||||
}
|
||||
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
|
||||
}
|
||||
4
types.go
4
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,7 +56,7 @@ type Message struct {
|
||||
Chat *Chat `json:"chat,omitempty"`
|
||||
Text string `json:"text"`
|
||||
|
||||
Photo Slice[*PhotoSize] `json:"photo,omitempty"`
|
||||
Photo extypes.Slice[*PhotoSize] `json:"photo,omitempty"`
|
||||
Caption string `json:"caption,omitempty"`
|
||||
ReplyToMessage *Message `json:"reply_to_message"`
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package laniakea
|
||||
|
||||
const (
|
||||
VersionString = "0.3.8"
|
||||
VersionString = "0.3.9"
|
||||
VersionMajor = 0
|
||||
VersionMinor = 3
|
||||
VersionPatch = 8
|
||||
VersionPatch = 9
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user