codebydante's picture
Upload 114 files
bd894b3 verified
Raw History Blame
1.42 kB
package bot
import (
"EverythingSuckz/fsb/config"
"EverythingSuckz/fsb/internal/commands"
"context"
"time"
"go.uber.org/zap"
"github.com/celestix/gotgproto"
"github.com/celestix/gotgproto/sessionMaker"
"github.com/glebarez/sqlite"
)
var Bot *gotgproto.Client
func StartClient(log *zap.Logger) (*gotgproto.Client, error) {
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
defer cancel()
resultChan := make(chan struct {
client *gotgproto.Client
err error
})
go func(ctx context.Context) {
client, err := gotgproto.NewClient(
int(config.ValueOf.ApiID),
config.ValueOf.ApiHash,
gotgproto.ClientTypeBot(config.ValueOf.BotToken),
&gotgproto.ClientOpts{
Session: sessionMaker.SqlSession(
sqlite.Open("fsb.session"),
),
DisableCopyright: true,
},
)
resultChan <- struct {
client *gotgproto.Client
err error
}{client, err}
}(ctx)
select {
case <-ctx.Done():
return nil, ctx.Err()
case result := <-resultChan:
if result.err != nil {
return nil, result.err
}
commands.Load(log, result.client.Dispatcher)
if err := commands.RegisterCommands(); err != nil {
log.Error("Failed to register commands", zap.Error(err))
}
log.Info("Client started", zap.String("username", result.client.Self.Username))
Bot = result.client
return result.client, nil
}
}