public async Task ShopRemove(int index)
            {
                index -= 1;
                if (index < 0)
                {
                    return;
                }
                ShopEntry removed;

                using (var uow = _db.GetDbContext())
                {
                    var config = uow.GuildConfigs.ForId(ctx.Guild.Id, set => set
                                                        .Include(x => x.ShopEntries)
                                                        .ThenInclude(x => x.Items));

                    var entries = new IndexedCollection <ShopEntry>(config.ShopEntries);
                    removed = entries.ElementAtOrDefault(index);
                    if (removed != null)
                    {
                        uow._context.RemoveRange(removed.Items);
                        uow._context.Remove(removed);
                        uow.SaveChanges();
                    }
                }

                if (removed == null)
                {
                    await ReplyErrorLocalizedAsync("shop_item_not_found").ConfigureAwait(false);
                }
                else
                {
                    await ctx.Channel.EmbedAsync(EntryToEmbed(removed)
                                                 .WithTitle(GetText("shop_item_rm"))).ConfigureAwait(false);
                }
            }
            public async Task ShopRemove(int index)
            {
                index -= 1;
                if (index < 0)
                {
                    return;
                }

                var gc = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.ShopEntries).ThenInclude(x => x.Items));

                var entries       = new IndexedCollection <ShopEntry>(gc.ShopEntries);
                var entryToRemove = entries.ElementAtOrDefault(index);

                if (entryToRemove != null)
                {
                    entries.Remove(entryToRemove);

                    gc.ShopEntries = entries;
                    await uow.SaveChangesAsync(false).ConfigureAwait(false);

                    await Context.Channel.EmbedAsync(EntryToEmbed(entryToRemove).WithTitle(GetText("shop_item_rm")));
                }
                else
                {
                    await ReplyErrorLocalized("shop_item_not_found").ConfigureAwait(false);
                }
            }
Exemple #3
0
            public async Task ShopListAdd(int index, [Remainder] string itemText)
            {
                index -= 1;
                if (index < 0)
                {
                    return;
                }
                var item = new ShopEntryItem()
                {
                    Text = itemText
                };
                ShopEntry entry;
                bool      rightType = false;
                bool      added     = false;

                using (var uow = _db.UnitOfWork)
                {
                    var entries = new IndexedCollection <ShopEntry>(uow.GuildConfigs.For(Context.Guild.Id,
                                                                                         set => set.Include(x => x.ShopEntries)
                                                                                         .ThenInclude(x => x.Items)).ShopEntries);
                    entry = entries.ElementAtOrDefault(index);
                    if (entry != null && (rightType = (entry.Type == ShopEntryType.List)))
                    {
                        if (added = entry.Items.Add(item))
                        {
                            uow.Complete();
                        }
                    }
                }
                if (entry == null)
                {
                    await ReplyErrorLocalized("shop_item_not_found").ConfigureAwait(false);
                }
                else if (!rightType)
                {
                    await ReplyErrorLocalized("shop_item_wrong_type").ConfigureAwait(false);
                }
                else if (added == false)
                {
                    await ReplyErrorLocalized("shop_list_item_not_unique").ConfigureAwait(false);
                }
                else
                {
                    await ReplyConfirmLocalized("shop_list_item_added").ConfigureAwait(false);
                }
            }
            public async Task ShopListAdd(int index, [Remainder] string itemText)
            {
                index -= 1;
                if (index < 0)
                {
                    return;
                }

                var item = new ShopEntryItem {
                    Text = itemText
                };
                var entries = new IndexedCollection <ShopEntry>(uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.ShopEntries).ThenInclude(x => x.Items)).ShopEntries);
                var entry   = entries.ElementAtOrDefault(index);

                if (entry != null)
                {
                    if (entry.Type == ShopEntryType.List)
                    {
                        if (entry.Items.Add(item))
                        {
                            await uow.SaveChangesAsync(false).ConfigureAwait(false);

                            await ReplyConfirmLocalized("shop_list_item_added").ConfigureAwait(false);
                        }
                        else
                        {
                            await ReplyErrorLocalized("shop_list_item_not_unique").ConfigureAwait(false);
                        }
                    }
                    else
                    {
                        await ReplyErrorLocalized("shop_item_wrong_type").ConfigureAwait(false);
                    }
                }
                else
                {
                    await ReplyErrorLocalized("shop_item_not_found").ConfigureAwait(false);
                }
            }
Exemple #5
0
            public async Task ShopRemove(int index)
            {
                index -= 1;
                if (index < 0)
                {
                    return;
                }
                ShopEntry removed;

                using (var uow = DbHandler.UnitOfWork())
                {
                    var config = uow.GuildConfigs.For(Context.Guild.Id, set => set
                                                      .Include(x => x.ShopEntries)
                                                      .ThenInclude(x => x.Items));

                    var entries = new IndexedCollection <ShopEntry>(config.ShopEntries);
                    removed = entries.ElementAtOrDefault(index);
                    if (removed != null)
                    {
                        entries.Remove(removed);

                        config.ShopEntries = entries;
                        uow.Complete();
                    }
                }

                if (removed == null)
                {
                    await ReplyErrorLocalized("shop_item_not_found").ConfigureAwait(false);
                }
                else
                {
                    await Context.Channel.EmbedAsync(EntryToEmbed(removed)
                                                     .WithTitle(GetText("shop_item_rm")));
                }
            }
Exemple #6
0
            public async Task Buy(int index, [Remainder] string message = null)
            {
                index -= 1;
                if (index < 0)
                {
                    return;
                }
                ShopEntry entry;

                using (var uow = _db.UnitOfWork)
                {
                    var config = uow.GuildConfigs.For(Context.Guild.Id, set => set
                                                      .Include(x => x.ShopEntries)
                                                      .ThenInclude(x => x.Items));
                    var entries = new IndexedCollection <ShopEntry>(config.ShopEntries);
                    entry = entries.ElementAtOrDefault(index);
                    uow.Complete();
                }

                if (entry == null)
                {
                    await ReplyErrorLocalized("shop_item_not_found").ConfigureAwait(false);

                    return;
                }

                if (entry.Type == ShopEntryType.Role)
                {
                    var guser = (IGuildUser)Context.User;
                    var role  = Context.Guild.GetRole(entry.RoleId);

                    if (role == null)
                    {
                        await ReplyErrorLocalized("shop_role_not_found").ConfigureAwait(false);

                        return;
                    }

                    if (await _cs.RemoveAsync(Context.User.Id, $"Shop purchase - {entry.Type}", entry.Price))
                    {
                        try
                        {
                            await guser.AddRoleAsync(role).ConfigureAwait(false);
                        }
                        catch (Exception ex)
                        {
                            _log.Warn(ex);
                            await _cs.AddAsync(Context.User.Id, $"Shop error refund", entry.Price);
                            await ReplyErrorLocalized("shop_role_purchase_error").ConfigureAwait(false);

                            return;
                        }
                        var profit = GetProfitAmount(entry.Price);
                        await _cs.AddAsync(entry.AuthorId, $"Shop sell item - {entry.Type}", profit);

                        await _cs.AddAsync(Context.Client.CurrentUser.Id, $"Shop sell item - cut", entry.Price - profit);
                        await ReplyConfirmLocalized("shop_role_purchase", Format.Bold(role.Name)).ConfigureAwait(false);

                        return;
                    }
                    else
                    {
                        await ReplyErrorLocalized("not_enough", _bc.BotConfig.CurrencySign).ConfigureAwait(false);

                        return;
                    }
                }
                else if (entry.Type == ShopEntryType.List)
                {
                    if (entry.Items.Count == 0)
                    {
                        await ReplyErrorLocalized("out_of_stock").ConfigureAwait(false);

                        return;
                    }

                    var item = entry.Items.ToArray()[new NadekoRandom().Next(0, entry.Items.Count)];

                    if (await _cs.RemoveAsync(Context.User.Id, $"Shop purchase - {entry.Type}", entry.Price))
                    {
                        int removed;
                        using (var uow = _db.UnitOfWork)
                        {
                            var x = uow._context.Set <ShopEntryItem>().Remove(item);

                            removed = uow.Complete();
                        }
                        try
                        {
                            await(await Context.User.GetOrCreateDMChannelAsync())
                            .EmbedAsync(new EmbedBuilder().WithOkColor()
                                        .WithTitle(GetText("shop_purchase", Context.Guild.Name))
                                        .AddField(efb => efb.WithName(GetText("item")).WithValue(item.Text).WithIsInline(false))
                                        .AddField(efb => efb.WithName(GetText("price")).WithValue(entry.Price.ToString()).WithIsInline(true))
                                        .AddField(efb => efb.WithName(GetText("name")).WithValue(entry.Name).WithIsInline(true)))
                            .ConfigureAwait(false);

                            await _cs.AddAsync(entry.AuthorId,
                                               $"Shop sell item - {entry.Name}",
                                               GetProfitAmount(entry.Price)).ConfigureAwait(false);
                        }
                        catch
                        {
                            await _cs.AddAsync(Context.User.Id,
                                               $"Shop error refund - {entry.Name}",
                                               entry.Price).ConfigureAwait(false);

                            using (var uow = _db.UnitOfWork)
                            {
                                uow._context.Set <ShopEntryItem>().Add(item);
                                uow.Complete();
                            }
                            await ReplyErrorLocalized("shop_buy_error").ConfigureAwait(false);

                            return;
                        }
                        await ReplyConfirmLocalized("shop_item_purchase").ConfigureAwait(false);
                    }
                    else
                    {
                        await ReplyErrorLocalized("not_enough", _bc.BotConfig.CurrencySign).ConfigureAwait(false);

                        return;
                    }
                }
            }