예제 #1
0
        // User joined a guild
        private async Task OnUserJoined(SocketGuildUser userJoining)
        {
            await AutoRoleHelper.AssignAutoRoles(_autoRoleService, userJoining);

            Task.Run(async() => await ShowWelcomeMessage(userJoining));

            // TODO Make into own method
            var guild  = userJoining.Guild;
            var server = await ServerHelper.GetOrAddServer(guild.Id, _serverRepository);

            var dbinvites = await _serverInviteRepository.GetServerInvites(server.Id);

            var invites = await guild.GetInvitesAsync();

            if (server.TrackInvites)
            {
                var inviteUsed =
                    from ginvite in invites
                    join invite in dbinvites
                    on ginvite.Code equals invite.Code
                    where invite.Uses < ginvite.Uses
                    select ginvite;

                if (inviteUsed.Count() > 1)
                {
                    _logger.LogWarning("More than one invite matches!");
                    await _servers.SendLogsAsync(userJoining.Guild, "Invite Error!", $"More than one invite matched for user {userJoining.Username}!");

                    return;
                }
                if (inviteUsed.Count() < 1)
                {
                    _logger.LogWarning("No invite matches!");
                    await _servers.SendLogsAsync(userJoining.Guild, "Invite Error!", $"Could not find matching invite for user {userJoining.Username}!");

                    return;
                }

                var invused = inviteUsed.FirstOrDefault();

                var inviter = await _userRepository.GetByUserId(invused.Inviter.Id);

                if (inviter == null)
                {
                    inviter = new Models.User
                    {
                        UserId   = invused.Inviter.Id,
                        UserName = invused.Inviter.Username
                    };

                    await _userRepository.AddAsync(inviter);
                }

                var userInvite = await _inviteRepository.GetInviteByUser(inviter.Id, server.Id);

                if (userInvite == null)
                {
                    userInvite = new Invite
                    {
                        Count    = 0,
                        UserId   = inviter.Id,
                        ServerId = server.Id
                    };
                    await _inviteRepository.AddAsync(userInvite);
                }

                userInvite.Count++;
                await _inviteRepository.EditAsync(userInvite);

                var updateInv = dbinvites.Where(x => x.Code == invused.Code).FirstOrDefault();
                updateInv.Uses = invused?.Uses ?? 0;
                await _serverInviteRepository.EditAsync(updateInv);

                await UpdateInvites();

                if (server.LoggingChannel != 0)
                {
                    var user = _userRepository.GetByUserId(userJoining.Id);
                    await _servers.SendLogsAsync(guild, "User invited", $"{inviter.UserName} invited {userJoining.Username} with invite link {invused.Url}.");
                }
            }
        }