Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Done. #131

Closed
wants to merge 1 commit into from
Closed

Done. #131

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions cmd/fsb/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ import (
"go.uber.org/zap"
)

var runCmd = &cobra.Command{
Use: "run",
Short: "Run the bot with the given configuration.",
DisableSuggestions: false,
Run: runApp,
}

var startTime time.Time = time.Now()
var (
runCmd = &cobra.Command{
Use: "run",
Short: "Run the bot with the given configuration.",
DisableSuggestions: false,
Run: runApp,
}
startTime = time.Now()
)

func runApp(cmd *cobra.Command, args []string) {
func runApp(cmd *cobra.Command, _ []string) {
utils.InitLogger()
log := utils.Logger
mainLogger := log.Named("Main")
Expand Down
2 changes: 1 addition & 1 deletion cmd/fsb/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func init() {
sessionCmd.MarkFlagRequired("api-hash")
}

func generateSession(cmd *cobra.Command, args []string) {
func generateSession(cmd *cobra.Command, _ []string) {
loginType, _ := cmd.Flags().GetString("login-type")
apiId, _ := cmd.Flags().GetInt32("api-id")
apiHash, _ := cmd.Flags().GetString("api-hash")
Expand Down
29 changes: 15 additions & 14 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,25 @@ import (
"go.uber.org/zap"
)

var ValueOf = &config{}
var (
ValueOf = &config{}
botTokenRegex = regexp.MustCompile(`MULTI_TOKEN\d+=(.*)`)
)

type config struct {
ApiID int32 `envconfig:"API_ID" required:"true"`
ApiHash string `envconfig:"API_HASH" required:"true"`
BotToken string `envconfig:"BOT_TOKEN" required:"true"`
LogChannelID int64 `envconfig:"LOG_CHANNEL" required:"true"`
Dev bool `envconfig:"DEV" default:"false"`
Port int `envconfig:"PORT" default:"8080"`
Host string `envconfig:"HOST" default:"http://localhost:8080"`
HashLength int `envconfig:"HASH_LENGTH" default:"6"`
UseSessionFile bool `envconfig:"USE_SESSION_FILE" default:"true"`
UserSession string `envconfig:"USER_SESSION"`
MultiTokens []string
Dev bool `envconfig:"DEV" default:"false"`
UseSessionFile bool `envconfig:"USE_SESSION_FILE" default:"true"`
ApiID int32 `envconfig:"API_ID" required:"true"`
HashLength int `envconfig:"HASH_LENGTH" default:"6"`
Port int `envconfig:"PORT" default:"8080"`
ApiHash string `envconfig:"API_HASH" required:"true"`
UserSession string `envconfig:"USER_SESSION"`
BotToken string `envconfig:"BOT_TOKEN" required:"true"`
LogChannelID int64 `envconfig:"LOG_CHANNEL" required:"true"`
MultiTokens []string `envconfig:"MULTI_TOKENS"`
Host string `envconfig:"HOST" default:"http://localhost:8080"`
}

var botTokenRegex = regexp.MustCompile(`MULTI\_TOKEN\d+=(.*)`)

func (c *config) loadFromEnvFile(log *zap.Logger) {
envPath := filepath.Clean("fsb.env")
log.Sugar().Infof("Trying to load ENV vars from %s", envPath)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module EverythingSuckz/fsb

go 1.21.3
go 1.21.5

require (
github.com/celestix/gotgproto v1.0.0-beta13.0.20231201105236-366763b5cc2e
Expand Down
2 changes: 1 addition & 1 deletion internal/bot/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"golang.org/x/time/rate"
)

func GetFloodMiddleware(log *zap.Logger) []telegram.Middleware {
func GetFloodMiddleware(_ *zap.Logger) []telegram.Middleware {
waiter := floodwait.NewSimpleWaiter().WithMaxRetries(10)
ratelimiter := ratelimit.New(rate.Every(time.Millisecond*100), 5)
return []telegram.Middleware{
Expand Down
6 changes: 3 additions & 3 deletions internal/bot/userbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type UserBotStruct struct {
client *gotgproto.Client
}

var UserBot *UserBotStruct = &UserBotStruct{}
var UserBot = &UserBotStruct{}

func StartUserBot(l *zap.Logger) {
log := l.Named("USERBOT")
Expand All @@ -42,7 +42,7 @@ func StartUserBot(l *zap.Logger) {
UserBot.log = log
UserBot.client = client
log.Info("Userbot started", zap.String("username", client.Self.Username), zap.String("FirstName", client.Self.FirstName), zap.String("LastName", client.Self.LastName))
if err := UserBot.AddBotsAsAdmins(); err != nil {
if err = UserBot.AddBotsAsAdmins(); err != nil {
log.Error("Failed to add bots as admins", zap.Error(err))
return
}
Expand All @@ -68,7 +68,7 @@ func (u *UserBotStruct) AddBotsAsAdmins() error {
return errors.New("no channels found")
}
inputChannel := channelInfos.GetChats()[0].(*tg.Channel).AsInput()
currentAdmins := []int64{}
var currentAdmins []int64
admins, err := u.client.API().ChannelsGetParticipants(ctx, &tg.ChannelsGetParticipantsRequest{
Channel: inputChannel,
Filter: &tg.ChannelParticipantsAdmins{},
Expand Down
4 changes: 2 additions & 2 deletions internal/bot/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type BotWorkers struct {
log *zap.Logger
}

var Workers *BotWorkers = &BotWorkers{
var Workers = &BotWorkers{
log: nil,
Bots: make([]*Worker, 0),
}
Expand Down Expand Up @@ -66,7 +66,7 @@ func (w *BotWorkers) incStarting() {

func (w *BotWorkers) Add(token string) (err error) {
w.incStarting()
var botID int = w.starting
var botID = w.starting
client, err := startWorker(w.log, token, botID)
if err != nil {
return err
Expand Down
6 changes: 2 additions & 4 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,10 @@ func (c *Cache) Set(key string, value *types.File, expireSeconds int) error {
defer c.mu.Unlock()
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(value)
if err != nil {
if err := enc.Encode(value); err != nil {
return err
}
cache.cache.Set([]byte(key), buf.Bytes(), expireSeconds)
return nil
return cache.cache.Set([]byte(key), buf.Bytes(), expireSeconds)
}

func (c *Cache) Delete(key string) error {
Expand Down
6 changes: 3 additions & 3 deletions internal/routes/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"net/http"
"strconv"

range_parser "github.com/quantumsheep/range-parser"
rangeparser "github.com/quantumsheep/range-parser"
"go.uber.org/zap"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -67,7 +67,7 @@ func getStreamRoute(ctx *gin.Context) {
end = file.FileSize - 1
w.WriteHeader(http.StatusOK)
} else {
ranges, err := range_parser.Parse(file.FileSize, r.Header.Get("Range"))
ranges, err := rangeparser.Parse(file.FileSize, r.Header.Get("Range"))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
Expand Down Expand Up @@ -103,7 +103,7 @@ func getStreamRoute(ctx *gin.Context) {
return
}
lr, _ := utils.NewTelegramReader(ctx, worker.Client, file.Location, start, end, contentLength)
if _, err := io.CopyN(w, lr, contentLength); err != nil {
if _, err = io.CopyN(w, lr, contentLength); err != nil {
log.Error("Error while copying stream", zap.Error(err))
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/types/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
)

type File struct {
Location *tg.InputDocumentFileLocation
ID int64
FileSize int64
FileName string
MimeType string
ID int64
Location *tg.InputDocumentFileLocation
}

type HashableFileStruct struct {
Expand Down