Beispiel #1
0
        public async Task Verify()
        {
            if (!(Context.User is SocketGuildUser guildUser))
            {
                return;
            }

            var discordId          = guildUser.Id;
            var guildId            = Context.Guild.Id;
            var verificationResult = await _verificationStorage.GetVerification(guildId, discordId);

            if (verificationResult != null)
            {
                await ReplyAsync("You are already verified in this server.");

                return;
            }

            var dmChannel = await Context.User.GetOrCreateDMChannelAsync();

            var verification = new Verification {
                GuildId = guildId, DiscordId = discordId
            };

            try
            {
                await _verificationService.VerifyCode(async message =>
                {
                    try
                    {
                        await dmChannel.SendMessageAsync("After you authenticate with your corp account, we will collect and store your department, alias, " +
                                                         "and your corp user id. This data will only be used to validate your current status for the purpose of managing the verified role on this server.");
                        await dmChannel.SendMessageAsync(message);
                        await ReplyAsync($"{Context.User.Mention}, check your DMs for instructions.");
                    }
                    catch (HttpException ex) when(ex.DiscordCode == 50007)
                    {
                        await ReplyAsync($"{Context.User.Mention}, Please temporarily allow DMs from this server and try again.");
                        return;
                    }
                });

                await _verificationService.LoadUserDetails(Context.Configuration.Organization);

                if (_verificationService.Alias.Contains("#EXT#"))
                {
                    await dmChannel.SendMessageAsync("This account is external to Microsoft and is not eligible for validation.");

                    return;
                }

                if (Context.Configuration.RequiresOrganization && !_verificationService.Organization.Equals(Context.Configuration.Organization))
                {
                    await dmChannel.SendMessageAsync($"We see that you are a current Microsoft employee, however, this server requires that you be in a specific org in order to receive the validated status.");

                    return;
                }

                if (!Context.Configuration.AllowedUserTypesFlag.HasFlag(_verificationService.UserType))
                {
                    await dmChannel.SendMessageAsync($"Verification requires that you be one of the following: {Context.Configuration.AllowedUserTypesFlag}");

                    return;
                }

                var corpUserId = Guid.Parse(_verificationService.UserId);

                verification.CorpUserId  = corpUserId;
                verification.Alias       = _verificationService.Alias;
                verification.ValidatedOn = DateTimeOffset.UtcNow;
                verification.Department  = _verificationService.Department;
                await _verificationStorage.SaveVerification(verification);

                await dmChannel.SendMessageAsync($"Thanks for validating your status with Microsoft. You can unlink your accounts at any time with the `{Context.Configuration.Prefix}microsoft leave` command.");

                var role = Context.Guild.Roles.SingleOrDefault(a => a.Id == ulong.Parse(Context.Configuration.RoleId));
                await guildUser.AddRoleAsync(role);
            }
            catch (VerificationException ex) when(ex.ErrorCode == "code_expired")
            {
                _logger.LogInformation($"Code expired for {Context.User.Username}#{Context.User.Discriminator}");
                await dmChannel.SendMessageAsync("Your code has expired.");
            }
            catch (Exception ex)
            {
                await dmChannel.SendMessageAsync("An error occurred saving your validation. Please try again later.");

                _logger.LogCritical(ex, ex.Message);
            }
        }