Ejemplo n.º 1
0
        public UserInfo GetUserById(string userId)
        {
            var userName = UsersById.Get(userId);

            if (userName != null)
            {
                return(userName);
            }

            try
            {
                var user = ApiPool.GetApi().V5.Users.GetUserByIDAsync(userId).Result;
                if (user == null)
                {
                    return(null);
                }

                var userInfo = new UserInfo(userId, user.Name, user.DisplayName);
                Store(userInfo);

                return(userInfo);
            }
            catch (Exception e)
            {
                Logger.Instance.Error($"Errors: {e.Info()}");
                return(null);
            }
        }
Ejemplo n.º 2
0
        public Dictionary <string, List <UserInfo> > GetUsersByNames(List <string> userNames)
        {
            var cached = UsersByName.Where(_ =>
                                           _.Value.Any(_2 => userNames.Any(_3 => _3.ToLower() == _2.Name.ToLower())));

            var uncached =
                userNames.Where(_ => !cached.Any(_2 => _2.Value.Any(_3 => _3.Name.ToLower() == _.ToLower())));

            var result = new Dictionary <string, List <UserInfo> >();

            foreach (var name in userNames)
            {
                result[name.ToLower()] = new List <UserInfo>();
            }

            if (uncached.Any())
            {
                List <UserInfo> requested = new List <UserInfo>();
                try
                {
                    var apiResult = ApiPool.GetApi().V5.Users.GetUsersByNameAsync(uncached.ToList());
                    if (apiResult.Result.Total > 0)
                    {
                        requested.AddRange(apiResult.Result.Matches.Select(_ => new UserInfo(_.Id, _.Name, _.DisplayName)));
                    }
                }
                catch (Exception e)
                {
                    Logger.Instance.Error($"Error : {e.Info()}");
                }

                foreach (var info in requested)
                {
                    Store(info);

                    if (!result.ContainsKey(info.Name.ToLower()))
                    {
                        result[info.Name.ToLower()] = new List <UserInfo>();
                    }

                    result[info.Name.ToLower()].Add(info);
                }
            }

            foreach (var c in cached)
            {
                result[c.Key] = c.Value;
            }

            foreach (var name in userNames)
            {
                if (!result.ContainsKey(name.ToLower()))
                {
                    result.Add(name, new List <UserInfo>());
                }
            }

            return(result);
        }
Ejemplo n.º 3
0
        private void UpdateChannel()
        {
            var oldOnline = IsBroadcasting;

            IsBroadcasting = CheckIsBroadcasting();

            if (needNotify || IsBroadcasting && oldOnline != IsBroadcasting)
            {
                var stream = ApiPool.GetApi().V5.Streams.GetStreamByUserAsync(Channel.Id).Result;
                if (stream.Stream != null)
                {
                    NotifyOnline(stream.Stream);
                }
            }
            else
            {
                needNotify = false;
            }
        }
Ejemplo n.º 4
0
        private List <WatchedChannel> LoadWatchedChannels()
        {
            var watchedChannels = new Dictionary <string, WatchedChannel>();

            try
            {
                using (var command = new MySqlCommand("SELECT bwc.channel_id, btn.telegram_channel_id, bwc.commands_enabled != 0, bwc.install_date FROM bot_watched_channels bwc " +
                                                      "LEFT JOIN bot_telegram_notify btn ON bwc.channel_id = btn.channel_id AND bwc.bot_id = btn.bot_id " +
                                                      "WHERE bwc.bot_id = @bot_id AND bwc.enabled != 0", DbConnection.GetConnection()))
                {
                    command.Parameters.AddWithValue("@bot_id", _settings.Id);

                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var channelId = reader.GetString(0);

                            if (watchedChannels.TryGetValue(channelId, out var channel))
                            {
                                if (!reader.IsDBNull(1))
                                {
                                    channel.TelegramNotifyChannels.Add(reader.GetString(1));
                                }
                            }
                            else
                            {
                                channel = new WatchedChannel
                                {
                                    BotId           = _settings.Id,
                                    Id              = channelId,
                                    CommandsEnabled = reader.GetBoolean(2),
                                    InstallDate     = reader.GetInt32(3),
                                };
                                if (!reader.IsDBNull(1))
                                {
                                    channel.TelegramNotifyChannels.Add(reader.GetString(1));
                                }

                                watchedChannels[channelId] = channel;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Instance.Error($"Faled to load watched channels: {e.Info()}");

                return(new List <WatchedChannel>());
            }

            var channels = watchedChannels.Select(_ => _.Value).ToList();

            foreach (var channel in channels)
            {
                try
                {
                    var res = ApiPool.GetApi().V5.Channels.GetChannelByIDAsync(channel.Id).Result;
                    if (res == null)
                    {
                        Logger.Instance.Warn($"Failed to resolve channel with id '{channel.Id}");
                        continue;
                    }

                    channel.Name = res.Name.ToLower();
                }
                catch (Exception e)
                {
                    Logger.Instance.Error($"Failed to add watched channel: {e.Info()}");
                    continue;
                }
            }

            return(channels);
        }
Ejemplo n.º 5
0
 private bool CheckIsBroadcasting()
 {
     return(ApiPool.GetApi().V5.Streams.BroadcasterOnlineAsync(Channel.Id).Result);
 }