Exemple #1
0
        public async Task SellKeyAsync(IContext e)
        {
            var unit = e.GetService <IUnitOfWork>();
            var transactionService = e.GetService <ITransactionService>();
            var keyRepository      = unit.GetRepository <DonatorKey>();

            if (e.GetArgumentPack().Take(out Guid guid))
            {
                DonatorKey key = await DonatorKey.GetKeyAsync(keyRepository, guid);

                await keyRepository.DeleteAsync(key);

                await unit.CommitAsync();

                await transactionService.CreateTransactionAsync(
                    new TransactionRequest.Builder()
                    .WithAmount(KeyBuybackPrice)
                    .WithReceiver((long)e.GetAuthor().Id)
                    .WithSender(AppProps.Currency.BankId)
                    .Build());

                await e.SuccessEmbed(
                    e.GetLocale().GetString("key_sold_success", KeyBuybackPrice))
                .QueueAsync(e, e.GetChannel());
            }
        }
        public async Task SellKeyAsync(CommandContext e)
        {
            var context = e.GetService <MikiDbContext>();

            long id = (long)e.Author.Id;

            if (e.Arguments.Take(out Guid guid))
            {
                DonatorKey key = await DonatorKey.GetKeyAsync(context, guid);

                User u = await User.GetAsync(context, id, e.Author.Username);

                await u.AddCurrencyAsync(30000, e.Channel);

                context.DonatorKey.Remove(key);

                await context.SaveChangesAsync();

                await Utils.SuccessEmbed(e, e.Locale.GetString("key_sold_success", 30000))
                .QueueToChannelAsync(e.Channel);
            }
        }
Exemple #3
0
        public async Task RedeemKeyAsync(IContext e)
        {
            if (e.HasFeatureEnabled("redeemkey_using_payment_service"))
            {
                await RedeemKeyV2Async(e);

                return;
            }

            var unit = e.GetService <IUnitOfWork>();

            var donatorRepository = unit.GetRepository <IsDonator>();
            var keyRepository     = unit.GetRepository <DonatorKey>();
            var locale            = e.GetLocale();

            long id = (long)e.GetAuthor().Id;

            if (!e.GetArgumentPack().Take(out Guid guid))
            {
                throw new InvalidKeyFormatException();
            }

            DonatorKey key = await DonatorKey.GetKeyAsync(keyRepository, guid);

            IsDonator donatorStatus = await donatorRepository.GetAsync(id);

            if (donatorStatus == null)
            {
                donatorStatus = new IsDonator
                {
                    UserId       = id,
                    KeysRedeemed = 1,
                    ValidUntil   = DateTime.UtcNow + key.StatusTime
                };
                await donatorRepository.AddAsync(donatorStatus);
            }
            else
            {
                donatorStatus.KeysRedeemed++;

                if (donatorStatus.ValidUntil > DateTime.Now)
                {
                    donatorStatus.ValidUntil += key.StatusTime;
                }
                else
                {
                    donatorStatus.ValidUntil = DateTime.Now + key.StatusTime;
                }
                await donatorRepository.EditAsync(donatorStatus);
            }
            await keyRepository.DeleteAsync(key);

            await unit.CommitAsync();

            await new EmbedBuilder
            {
                Title        = $"🎉 {locale.GetString("common_success", e.GetAuthor().Username)}",
                Color        = new Color(226, 46, 68),
                Description  = locale.GetString("key_redeem_success", $"**{key.StatusTime.TotalDays}**"),
                ThumbnailUrl = "https://i.imgur.com/OwwA5fV.png"
            }.AddInlineField("When does my status expire?", donatorStatus.ValidUntil.ToLongDateString())
            .ToEmbed().QueueAsync(e, e.GetChannel());

            var achievementManager = e.GetService <AchievementService>();
            var donatorAchievement = achievementManager.GetAchievement(AchievementIds.DonatorId);

            if (donatorStatus.KeysRedeemed >= 1 &&
                donatorStatus.KeysRedeemed < 5)
            {
                await achievementManager.UnlockAsync(donatorAchievement, e.GetAuthor().Id);
            }
            else if (donatorStatus.KeysRedeemed >= 5 &&
                     donatorStatus.KeysRedeemed < 25)
            {
                await achievementManager.UnlockAsync(donatorAchievement, e.GetAuthor().Id, 1)
                .ConfigureAwait(false);
            }
            else if (donatorStatus.KeysRedeemed >= 25)
            {
                await achievementManager.UnlockAsync(donatorAchievement, e.GetAuthor().Id, 2);
            }
        }
        public async Task RedeemKeyAsync(EventContext e)
        {
            using (var context = new MikiContext())
            {
                long id = (long)e.Author.Id;
                if (e.Arguments.Take(out Guid guid))
                {
                    DonatorKey key = await DonatorKey.GetKeyAsync(context, guid);

                    IsDonator donatorStatus = await context.IsDonator.FindAsync(id);

                    if (donatorStatus == null)
                    {
                        donatorStatus = (await context.IsDonator.AddAsync(new IsDonator()
                        {
                            UserId = id
                        })).Entity;
                    }

                    donatorStatus.KeysRedeemed++;

                    if (donatorStatus.ValidUntil > DateTime.Now)
                    {
                        donatorStatus.ValidUntil += key.StatusTime;
                    }
                    else
                    {
                        donatorStatus.ValidUntil = DateTime.Now + key.StatusTime;
                    }

                    await new EmbedBuilder()
                    {
                        Title        = ($"🎉 Congratulations, {e.Author.Username}"),
                        Color        = new Color(226, 46, 68),
                        Description  = ($"You have successfully redeemed a donator key, I've given you **{key.StatusTime.TotalDays}** days of donator status."),
                        ThumbnailUrl = ("https://i.imgur.com/OwwA5fV.png")
                    }.AddInlineField("When does my status expire?", donatorStatus.ValidUntil.ToLongDateString())
                    .ToEmbed().QueueToChannelAsync(e.Channel);

                    context.DonatorKey.Remove(key);
                    await context.SaveChangesAsync();

                    // cheap hack.
                    var achievementManager = AchievementManager.Instance;
                    var achievements       = achievementManager.GetContainerById("donator").Achievements;

                    if (donatorStatus.KeysRedeemed == 1)
                    {
                        await achievementManager.UnlockAsync(achievements[0], e.Channel, e.Author, 0);
                    }
                    else if (donatorStatus.KeysRedeemed == 5)
                    {
                        await achievementManager.UnlockAsync(achievements[1], e.Channel, e.Author, 1);
                    }
                    else if (donatorStatus.KeysRedeemed == 25)
                    {
                        await achievementManager.UnlockAsync(achievements[2], e.Channel, e.Author, 2);
                    }
                }
            }
        }