예제 #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());
        }
예제 #2
0
        public async Task <Message> PlayCoinflip(string argument, ulong userId)
        {
            var arguments = argument.ToUpper().TrimStart().Split(' ');

            if (arguments.Length != 2)
            {
                return(_messagesHelper.InvalidArguments(new[] { "<Head|Tail>", "<Bet>" }, "CoinFlip"));
            }

            var option = arguments[0] == "HEAD" || arguments[0] == "TAIL" ? arguments[0] : "";
            var amount = arguments[1].Trim();

            if (string.IsNullOrWhiteSpace(option) || string.IsNullOrWhiteSpace(amount))
            {
                return(_messagesHelper.InvalidArguments(new[] { "<Head|Tail>", "<Bet>" }, "CoinFlip"));
            }

            var bet = amount switch
            {
                "ALL" => await _usersConnector.GetCredits(userId),
                "HALF" => await _usersConnector.GetCredits(userId) / 2,
                _ => _betSizeParser.Parse(arguments[1])
            };

            if (bet == -1)
            {
                return(_messagesHelper.Error("Check your arguments (whether bet size does not equal 0 for example)!"));
            }

            if (await _usersConnector.TakeCredits(userId, bet))
            {
                var rnd    = new Random();
                var hasWon = (Convert.ToBoolean(rnd.Next(0, 2)) ? "HEAD" : "TAIL") == option;

                var exp       = ExpHelpers.CalculateBet((await _usersConnector.GetUser(userId)).Level, bet);
                var expResult = await _usersConnector.CommandGiveUserExp(userId, exp);

                if (hasWon)
                {
                    await _usersConnector.GiveCredits(userId, bet * 2);
                }

                var embed = hasWon
                    ? _messagesHelper.WinInformation(bet, title: "CoinFlip")
                    : _messagesHelper.LoseInformation(bet, title: "CoinFlip");

                _messagesHelper.AddCoinsBalanceInformation(embed, await _usersConnector.GetCredits(userId));
                if (expResult.DidUserLevelUp)
                {
                    embed = _messagesHelper.AddLevelUpMessage(embed);
                }

                await _gameHistoryConnector.AddGameHistory(new GameHistory
                {
                    GameName        = "coinflip",
                    HasWon          = hasWon,
                    CoinsDifference = hasWon ? bet : bet * -1,
                    UserId          = userId
                });

                return(embed);
            }

            return(_messagesHelper.NotEnoughCredits("CoinFlip"));
        }
    }