|
| 1 | +package bot |
| 2 | + |
| 3 | +import ( |
| 4 | + "EverythingSuckz/fsb/config" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "sync" |
| 9 | + |
| 10 | + "github.com/celestix/gotgproto" |
| 11 | + "github.com/celestix/gotgproto/sessionMaker" |
| 12 | + "github.com/gotd/td/tg" |
| 13 | + "go.uber.org/zap" |
| 14 | +) |
| 15 | + |
| 16 | +type Worker struct { |
| 17 | + ID int |
| 18 | + Client *gotgproto.Client |
| 19 | + Self *tg.User |
| 20 | + log *zap.Logger |
| 21 | +} |
| 22 | + |
| 23 | +func (w *Worker) String() string { |
| 24 | + return fmt.Sprintf("{Worker (%d|@%s)}", w.ID, w.Self.Username) |
| 25 | +} |
| 26 | + |
| 27 | +type BotWorkers struct { |
| 28 | + Bots []*Worker |
| 29 | + starting int |
| 30 | + index int |
| 31 | + mut sync.Mutex |
| 32 | + log *zap.Logger |
| 33 | +} |
| 34 | + |
| 35 | +var Workers *BotWorkers = &BotWorkers{ |
| 36 | + log: nil, |
| 37 | + Bots: make([]*Worker, 0), |
| 38 | +} |
| 39 | + |
| 40 | +func (w *BotWorkers) Init(log *zap.Logger) { |
| 41 | + w.log = log.Named("Workers") |
| 42 | +} |
| 43 | + |
| 44 | +func (w *BotWorkers) AddDefaultClient(client *gotgproto.Client, self *tg.User) { |
| 45 | + if w.Bots == nil { |
| 46 | + w.Bots = make([]*Worker, 0) |
| 47 | + } |
| 48 | + w.incStarting() |
| 49 | + w.Bots = append(w.Bots, &Worker{ |
| 50 | + Client: client, |
| 51 | + ID: w.starting, |
| 52 | + Self: self, |
| 53 | + }) |
| 54 | + w.log.Sugar().Info("Default bot loaded") |
| 55 | +} |
| 56 | + |
| 57 | +func (w *BotWorkers) incStarting() { |
| 58 | + w.mut.Lock() |
| 59 | + defer w.mut.Unlock() |
| 60 | + w.starting++ |
| 61 | +} |
| 62 | + |
| 63 | +func (w *BotWorkers) Add(token string) (err error) { |
| 64 | + w.incStarting() |
| 65 | + var botID int = w.starting |
| 66 | + client, err := startWorker(w.log, token, botID) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + w.log.Sugar().Infof("Bot @%s loaded with ID %d", client.Self.Username, botID) |
| 71 | + w.Bots = append(w.Bots, &Worker{ |
| 72 | + Client: client, |
| 73 | + ID: botID, |
| 74 | + Self: client.Self, |
| 75 | + log: w.log, |
| 76 | + }) |
| 77 | + return nil |
| 78 | +} |
| 79 | + |
| 80 | +func GetNextWorker() *Worker { |
| 81 | + Workers.mut.Lock() |
| 82 | + defer Workers.mut.Unlock() |
| 83 | + index := (Workers.index + 1) % len(Workers.Bots) |
| 84 | + Workers.index = index |
| 85 | + worker := Workers.Bots[index] |
| 86 | + Workers.log.Sugar().Infof("Using worker %d", worker.ID) |
| 87 | + return worker |
| 88 | +} |
| 89 | + |
| 90 | +func StartWorkers(log *zap.Logger) { |
| 91 | + log.Sugar().Info("Starting workers") |
| 92 | + Workers.Init(log) |
| 93 | + if config.ValueOf.UseSessionFile { |
| 94 | + log.Sugar().Info("Using session file for workers") |
| 95 | + newpath := filepath.Join(".", "sessions") |
| 96 | + err := os.MkdirAll(newpath, os.ModePerm) |
| 97 | + if err != nil { |
| 98 | + log.Error("Failed to create sessions directory", zap.Error(err)) |
| 99 | + return |
| 100 | + } |
| 101 | + } |
| 102 | + c := make(chan struct{}) |
| 103 | + for i := 0; i < len(config.ValueOf.MultiTokens); i++ { |
| 104 | + go func(i int) { |
| 105 | + err := Workers.Add(config.ValueOf.MultiTokens[i]) |
| 106 | + if err != nil { |
| 107 | + log.Error("Failed to start worker", zap.Error(err)) |
| 108 | + return |
| 109 | + } |
| 110 | + c <- struct{}{} |
| 111 | + }(i) |
| 112 | + } |
| 113 | + // wait for all workers to start |
| 114 | + log.Sugar().Info("Waiting for all workers to start") |
| 115 | + for i := 0; i < len(config.ValueOf.MultiTokens); i++ { |
| 116 | + <-c |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func startWorker(l *zap.Logger, botToken string, index int) (*gotgproto.Client, error) { |
| 121 | + log := l.Named("Worker").Sugar() |
| 122 | + log.Infof("Starting worker with index - %d", index) |
| 123 | + var sessionType *sessionMaker.SqliteSessionConstructor |
| 124 | + if config.ValueOf.UseSessionFile { |
| 125 | + sessionType = sessionMaker.SqliteSession(fmt.Sprintf("sessions/worker-%d", index)) |
| 126 | + } else { |
| 127 | + sessionType = sessionMaker.SqliteSession(":memory:") |
| 128 | + } |
| 129 | + client, err := gotgproto.NewClient( |
| 130 | + int(config.ValueOf.ApiID), |
| 131 | + config.ValueOf.ApiHash, |
| 132 | + gotgproto.ClientType{ |
| 133 | + BotToken: botToken, |
| 134 | + }, |
| 135 | + &gotgproto.ClientOpts{ |
| 136 | + Session: sessionType, |
| 137 | + DisableCopyright: true, |
| 138 | + }, |
| 139 | + ) |
| 140 | + if err != nil { |
| 141 | + return nil, err |
| 142 | + } |
| 143 | + return client, nil |
| 144 | +} |
0 commit comments