Example #1
0
 private async Task UwuAsync(SocketUserMessage message)
 {
     if (message.Content == "uwu") // ;)
     {
         OsuFriendsDb.Models.UserData user = _dbUserData.FindById(message.Author.Id);
         user.Uwu++;
         _dbUserData.Upsert(user);
         await message.Channel.SendMessageAsync("What's This?");
     }
 }
        public async Task <RuntimeResult> VerifyAsync(SocketGuildUser user, SocketCommandContext context = null)
        {
            _logger.LogTrace("Verifying user: {username}", user.Username);

            try
            {
                bool isVeryfying = AddVerifyingUser(user);
                if (isVeryfying)
                {
                    return(new VerificationLockError());
                }
                UserData dbUser = _dbUserData.FindById(user.Id);
                _logger.LogTrace("DbUser : {@dbUser} | Id : {@user} | Username: {@username}", dbUser, user.Id, user.Username);

                OsuUser osuUser = null;

                if (dbUser.OsuFriendsKey != null)
                {
                    osuUser = await CreateOsuUserFromUserDataAsync(dbUser);
                }

                if (osuUser == null)
                {
                    // If user doesn't exist in db
                    osuUser = await CreateOsuUserAsync();

                    if (osuUser == null)
                    {
                        return(new VerificationUserIdError());
                    }
                    await user.SendMessageAsync(embed : new VerifyEmbed(user, osuUser).Build());

                    // Retry
                    bool success = await WaitForVerificationStatusAsync(osuUser);

                    if (!success)
                    {
                        RemoveVerifyingUser(user);
                        return(new VerificationTimeoutError(user.Guild));
                    }
                    // Verification Success
                    dbUser.OsuFriendsKey = osuUser.Key;
                }
                // Success for both
                (List <SocketRole> grantedRoles, OsuUserDetails osuUserDetails) = await GrantUserRolesAsync(user, osuUser);
                await SendEmbedMsg(user, context, embed : new GrantedRolesEmbed(user, grantedRoles, osuUserDetails, dbUser).Build());

                dbUser.Std   = osuUserDetails.Std;
                dbUser.Taiko = osuUserDetails.Taiko;
                dbUser.Ctb   = osuUserDetails.Ctb;
                dbUser.Mania = osuUserDetails.Mania;
                _dbUserData.Upsert(dbUser);
            }
            catch (HttpException e)
            {
                RemoveVerifyingUser(user);
                _logger.LogTrace("httpCode: {httpCode} | discordCode: {discordCode}", e.HttpCode, e.DiscordCode);
                switch (e.DiscordCode)
                {
                case 50007:
                    return(new DirectMessageError());

                default:
                    break;
                }
                throw;
            }
            catch
            {
                RemoveVerifyingUser(user);
                throw;
            }
            RemoveVerifyingUser(user);
            return(new SuccessResult());
        }
Example #3
0
        public async Task VerifyAsync(SocketGuildUser user)
        {
            try
            {
                bool isVeryfying = false;
                lock (verifyingUsersLock)
                {
                    if (verifyingUsers.TryGetValue(user.Id, out ulong guild))
                    {
                        isVeryfying = guild == user.Guild.Id;
                    }
                    if (!isVeryfying)
                    {
                        verifyingUsers[user.Id] = user.Guild.Id;
                    }
                }
                if (isVeryfying)
                {
                    await user.SendMessageAsync("Complete your first verification before starting next one!");

                    return;
                }

                UserData dbUser = _dbUserData.FindById(user.Id);
                _logger.LogDebug("dbUser : {@dbUser}\n Id : {@user}\nUsername: {@username}", dbUser, user.Id, user.Username);

                EmbedBuilder embedBuilder;
                OsuUser      osuUser;

                if (dbUser == null)
                {
                    // If user doesn't exist in db
                    while (true)
                    {
                        osuUser = _osuFriends.CreateUser();
                        if ((await osuUser.GetStatusAsync()) == null)
                        {
                            break;
                        }
                    }

                    embedBuilder = new EmbedBuilder();
                    embedBuilder
                    .WithTitle($"Hi {user.Username}!")
                    .WithDescription($"Verify your osu! account to get cool roles on {user.Guild.Name}!")
                    .AddField("Link", osuUser.Url)
                    .WithThumbnailUrl("https://osufriends.ovh/img/favicon.gif");

                    await user.SendMessageAsync(embed : embedBuilder.Build());

                    // Retry
                    bool success = false;
                    for (int retry = 0; retry < 20; retry++)
                    {
                        if (await osuUser.GetStatusAsync() == Status.Completed)
                        {
                            success = true;
                            break;
                        }
                        await Task.Delay(TimeSpan.FromSeconds(3));
                    }
                    if (!success)
                    {
                        await user.SendMessageAsync($"Verification failed! Verify your account again with 'verify' command on {user.Guild.Name}");

                        return;
                    }
                    // Verification Success
                    _dbUserData.Upsert(new UserData {
                        UserId = user.Id, OsuFriendsKey = osuUser.Key
                    });
                }
                else
                {
                    // If user exist in db
                    osuUser = _osuFriends.CreateUser(dbUser.OsuFriendsKey);
                    if (await osuUser.GetStatusAsync() != Status.Completed)
                    {
                        await user.SendMessageAsync($"Refreshing failed! Refresh your account again with 'refresh' command on {user.Guild.Name}");

                        return;
                    }
                    // Refresh Success
                }
                // Success for both
                OsuUserDetails osuUserDetails = await osuUser.GetDetailsAsync();

                IReadOnlyCollection <SocketRole> guildRoles = user.Guild.Roles;

                List <SocketRole> roles = FindUserRoles(guildRoles, osuUserDetails);
                await user.RemoveRolesAsync(FindAllRoles(guildRoles).Where(role => user.Roles.Contains(role) && !roles.Contains(role)));

                await user.AddRolesAsync(roles.Where(role => !user.Roles.Contains(role)));

                try
                {
                    await user.ModifyAsync(properties => properties.Nickname = osuUserDetails.Username);
                }
                catch (HttpException)
                {
                }

                embedBuilder = new EmbedBuilder();
                embedBuilder
                .WithTitle($"Granted roles on {user.Guild.Name}:")
                .WithDescription(string.Join('\n', roles.Select(role => role.Name)))
                .WithThumbnailUrl(osuUserDetails.Avatar.ToString());
                await user.SendMessageAsync(embed : embedBuilder.Build());
            }
            finally
            {
                lock (verifyingUsersLock)
                {
                    verifyingUsers.Remove(user.Id);
                }
            }
        }