| 47 | } |
| 48 | |
| 49 | func (u *UserBotStruct) AddBotsAsAdmins() error { |
| 50 | u.log.Info("Preparing to add bots as admins") |
| 51 | ctx := u.client.CreateContext() |
| 52 | channel := config.ValueOf.LogChannelID |
| 53 | channelInfos, err := u.client.API().ChannelsGetChannels( |
| 54 | ctx, |
| 55 | []tg.InputChannelClass{ |
| 56 | &tg.InputChannel{ |
| 57 | ChannelID: channel, |
| 58 | }, |
| 59 | }, |
| 60 | ) |
| 61 | if err != nil { |
| 62 | u.log.Error("Failed to get channel info", zap.Error(err)) |
| 63 | return errors.New("failed to get channel info") |
| 64 | } |
| 65 | if len(channelInfos.GetChats()) == 0 { |
| 66 | return errors.New("no channels found") |
| 67 | } |
| 68 | inputChannel := channelInfos.GetChats()[0].(*tg.Channel).AsInput() |
| 69 | currentAdmins := []int64{} |
| 70 | admins, err := u.client.API().ChannelsGetParticipants(ctx, &tg.ChannelsGetParticipantsRequest{ |
| 71 | Channel: inputChannel, |
| 72 | Filter: &tg.ChannelParticipantsAdmins{}, |
| 73 | Offset: 0, |
| 74 | Limit: 100, |
| 75 | }) |
| 76 | if err != nil { |
| 77 | u.log.Error("Failed to get admins", zap.Error(err)) |
| 78 | return err |
| 79 | } |
| 80 | for _, admin := range admins.(*tg.ChannelsChannelParticipants).Participants { |
| 81 | if user, ok := admin.(*tg.ChannelParticipantAdmin); ok { |
| 82 | currentAdmins = append(currentAdmins, user.UserID) |
| 83 | } |
| 84 | } |
| 85 | for _, bot := range Workers.Bots { |
| 86 | isAdmin := false |
| 87 | for _, admin := range currentAdmins { |
| 88 | if admin == bot.Self.ID { |
| 89 | u.log.Sugar().Infof("Bot @%s is already an admin", bot.Self.Username) |
| 90 | isAdmin = true |
| 91 | continue |
| 92 | } |
| 93 | } |
| 94 | if isAdmin { |
| 95 | continue |
| 96 | } |
| 97 | botInfo, err := ctx.ResolveUsername(bot.Self.Username) |
| 98 | if err != nil { |
| 99 | u.log.Warn(err.Error()) |
| 100 | } |
| 101 | _, err = u.client.API().ChannelsEditAdmin( |
| 102 | u.client.CreateContext().Context, |
| 103 | &tg.ChannelsEditAdminRequest{ |
| 104 | Channel: inputChannel, |
| 105 | UserID: botInfo.GetInputUser(), |
| 106 | AdminRights: tg.ChatAdminRights{ |