Beispiel #1
0
        private GuildTracker GetGuildTracker(IGuild guild)
        {
            var guildTracker = _guilds.FirstOrDefault(g => g.GuildId == guild.Id);

            if (guildTracker == null)
            {
                guildTracker = new GuildTracker
                {
                    GuildId   = guild.Id,
                    GuildName = guild.Name
                };
                _guilds.Add(guildTracker);
                SaveJson();
            }

            return(guildTracker);
        }
Beispiel #2
0
        private void UpdateUser(IGuild guild, IUser oldUser, IUser newUser)
        {
            if (newUser.Username.ToLower().Trim() != oldUser.Username.ToLower().Trim())
            {
                GuildTracker guildTracker = GetGuildTracker(guild);
                // get the user state for the old user
                var oldUserState = guildTracker.Users.FirstOrDefault(u => u.UserId == oldUser.Id);
                // get the user state for the new user
                var newUserState = guildTracker.Users.FirstOrDefault(u => u.UserId == newUser.Id);

                // if the new user state is null, and the old user state exists
                if (newUserState == null && oldUserState != null)
                {
                    // update the old user state to have the new user information
                    oldUserState.UserId   = newUser.Id;
                    oldUserState.Username = newUser.Username;

                    // check if there is a transition where a name changed from this old username already
                    var ut = guildTracker.Transitions.FirstOrDefault(t => t.FromUserName.ToLower().Trim() == oldUser.Username.ToLower().Trim());
                    if (ut != null)
                    {
                        // if it does, the chain is no longer valid - so update it to be for the current transition
                        ut.UserId     = newUser.Id;
                        ut.ToUserName = newUser.Username;
                    }
                    else
                    {
                        // if it does not, create a new user transition
                        ut = new UserTransition
                        {
                            UserId       = newUser.Id,
                            FromUserName = oldUser.Username,
                            ToUserName   = newUser.Username
                        };
                        guildTracker.Transitions.Add(ut);
                    }
                }
                SaveJson();
            }
        }