예제 #1
0
        public Task AddUser(IUser user, [Remainder] string twitchUrl)
        {
            if (user.IsBot)
            {
                return(Task.CompletedTask);
            }

            if (string.IsNullOrWhiteSpace(twitchUrl))
            {
                return(Task.CompletedTask);
            }

            LiteCollection <Streamer> streamers = null;

            lock (dbLock)
            {
                streamers = database.GetCollection <Streamer>("streamers");
            }

            var urlState = twitch.GetTwitchUrlState(twitchUrl);

            if (urlState.Result != Twitch.TwitchUrlState.Ok)
            {
                ReplyAsync("Twitch user is not valid");
                return(Task.CompletedTask);
            }


            Dictionary <string, JToken> dict = twitch.TwitchQuery(Twitch.HelixStrings.Users, twitchUrl).Result;

            if (streamers.FindOne(x => x.Id == user.Id) != null)
            {
                ReplyAsync("User already exists in database");
                return(Task.CompletedTask);
            }

            var newUser = new Streamer
            {
                Id        = user.Id,
                Name      = (string)dict["login"],
                StreamUrl = "https://www.twitch.tv/" + dict["login"],
                AvatarUrl = (string)dict["profile_image_url"]
            };

            bool update = false;

            lock (dbLock)
            {
                update = streamers.Upsert(newUser);
            }


            if (!update)
            {
                ReplyAsync("Could not add user to the database");
                return(Task.CompletedTask);
            }

            ReplyAsync($"Added {user.Mention} to the database");

            return(Task.CompletedTask);
        }