예제 #1
0
        public async Task <Message> Vote(ulong userId)
        {
            if (_config.BotSettings.TopGgToken == null)
            {
                return(_messagesHelper.Information(
                           "Top GG Token functionality is disabled on this server :disappointed:. Contact bots admin to turn it on :slight_smile:.",
                           "TopGGVote"));
            }

            var user = await _usersConnector.GetUser(userId);

            var httpRequestMessage = new HttpRequestMessage
            {
                Method     = HttpMethod.Get,
                RequestUri = new Uri($"https://top.gg/api/bots/688160933574475800/check?userId={userId}"),
                Headers    =
                {
                    { HttpRequestHeader.Authorization.ToString(), _config.BotSettings.TopGgToken }
                }
            };

            var response = await _client.SendAsync(httpRequestMessage);

            if (!response.IsSuccessStatusCode)
            {
                _logger.LogWarning("Error during http request! Verify the top gg token!!!", response);
                return(_messagesHelper.Error(
                           "Invalid Top GG Token Provided by the bot administrator! Please contact him!"));
            }

            var didUserVote = JsonConvert.DeserializeObject <Vote>(await response.Content.ReadAsStringAsync());

            if (didUserVote.Voted)
            {
                if (user.LastVoteReward.HasValue)
                {
                    var timeSpan = DateTime.Now - user.LastVoteReward.Value;
                    if (timeSpan >= TimeSpan.FromHours(12))
                    {
                        user.Credits       += Reward;
                        user.LastVoteReward = DateTime.Now;
                        await _usersConnector.UpdateUser(user);

                        return(_messagesHelper.CreditsGain(Reward, "TopGGVote"));
                    }

                    return(_messagesHelper.CommandCalledTooEarly(TimeSpan.FromHours(12) - timeSpan, "!vote",
                                                                 "TopGGVote"));
                }

                user.Credits       += Reward;
                user.LastVoteReward = DateTime.Now;
                await _usersConnector.UpdateUser(user);

                return(_messagesHelper.CreditsGain(Reward, "TopGGVote"));
            }

            return(_messagesHelper.NotVotedYet());
        }