Added .shopreq - Shop items can now have a role requirement in order to purchase. Added .shopbuy alias for .buy

This commit is contained in:
Kwoth
2022-09-13 21:33:20 +02:00
parent d1a818542c
commit f23ffe0c67
16 changed files with 17490 additions and 3 deletions

View File

@@ -24,6 +24,7 @@ public class ShopEntry : DbEntity, IIndexed
//list
public HashSet<ShopEntryItem> Items { get; set; } = new();
public ulong? RoleRequirement { get; set; }
}
public class ShopEntryItem : DbEntity

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,25 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace NadekoBot.Migrations.Mysql
{
public partial class shoprolereq : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<ulong>(
name: "rolerequirement",
table: "shopentry",
type: "bigint unsigned",
nullable: true);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "rolerequirement",
table: "shopentry");
}
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,25 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace NadekoBot.Migrations.PostgreSql
{
public partial class shoprolereq : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<decimal>(
name: "rolerequirement",
table: "shopentry",
type: "numeric(20,0)",
nullable: true);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "rolerequirement",
table: "shopentry");
}
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,25 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace NadekoBot.Migrations
{
public partial class shoprolereq : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<ulong>(
name: "RoleRequirement",
table: "ShopEntry",
type: "INTEGER",
nullable: true);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "RoleRequirement",
table: "ShopEntry");
}
}
}

View File

@@ -1745,6 +1745,9 @@ namespace NadekoBot.Migrations
b.Property<string>("RoleName")
.HasColumnType("TEXT");
b.Property<ulong?>("RoleRequirement")
.HasColumnType("INTEGER");
b.Property<int>("Type")
.HasColumnType("INTEGER");

View File

@@ -38,4 +38,6 @@ public interface IShopService
/// <param name="toIndex">Destination index of the entry</param>
/// <returns>Whether swap was successful</returns>
Task<bool> MoveEntryAsync(ulong guildId, int fromIndex, int toIndex);
Task<bool> SetItemRoleRequirementAsync(ulong guildId, int index, ulong? roleId);
}

View File

@@ -98,6 +98,23 @@ public partial class Gambling
return;
}
if (entry.RoleRequirement is ulong reqRoleId)
{
var role = ctx.Guild.GetRole(reqRoleId);
if (role is null)
{
await ReplyErrorLocalizedAsync(strs.shop_item_req_role_not_found);
return;
}
var guser = (IGuildUser)ctx.User;
if (!guser.RoleIds.Contains(reqRoleId))
{
await ReplyErrorLocalizedAsync(strs.shop_item_req_role_unfulfilled(Format.Bold(role.ToString())));
return;
}
}
if (entry.Type == ShopEntryType.Role)
{
var guser = (IGuildUser)ctx.User;
@@ -412,6 +429,27 @@ public partial class Gambling
await ctx.ErrorAsync();
}
[Cmd]
[RequireContext(ContextType.Guild)]
[UserPerm(GuildPerm.Administrator)]
public async Task ShopReq(int itemIndex, [Leftover] IRole role = null)
{
if (--itemIndex < 0)
return;
var succ = await _service.SetItemRoleRequirementAsync(ctx.Guild.Id, itemIndex, role?.Id);
if (!succ)
{
await ReplyErrorLocalizedAsync(strs.shop_item_not_found);
return;
}
if (role is null)
await ReplyConfirmLocalizedAsync(strs.shop_item_role_no_req(itemIndex));
else
await ReplyConfirmLocalizedAsync(strs.shop_item_role_req(itemIndex + 1, role));
}
public IEmbedBuilder EntryToEmbed(ShopEntry entry)
{
var embed = _eb.Create().WithOkColor();
@@ -443,11 +481,17 @@ public partial class Gambling
public string EntryToString(ShopEntry entry)
{
var prepend = string.Empty;
if (entry.RoleRequirement is not null)
prepend = Format.Italics(GetText(strs.shop_item_requires_role($"<@&{entry.RoleRequirement}>")))
+ Environment.NewLine;
if (entry.Type == ShopEntryType.Role)
return GetText(strs.shop_role(Format.Bold(ctx.Guild.GetRole(entry.RoleId)?.Name ?? "MISSING_ROLE")));
return prepend
+ GetText(strs.shop_role(Format.Bold(ctx.Guild.GetRole(entry.RoleId)?.Name ?? "MISSING_ROLE")));
if (entry.Type == ShopEntryType.List)
return GetText(strs.unique_items_left(entry.Items.Count)) + "\n" + entry.Name;
return "";
return prepend + GetText(strs.unique_items_left(entry.Items.Count)) + "\n" + entry.Name;
return prepend;
}
}
}

View File

@@ -94,4 +94,20 @@ public class ShopService : IShopService, INService
await uow.SaveChangesAsync();
return true;
}
public async Task<bool> SetItemRoleRequirementAsync(ulong guildId, int index, ulong? roleId)
{
await using var uow = _db.GetDbContext();
var entries = GetEntriesInternal(uow, guildId);
if (index >= entries.Count)
return false;
var entry = entries[index];
entry.RoleRequirement = roleId;
await uow.SaveChangesAsync();
return true;
}
}

View File

@@ -1003,7 +1003,10 @@ shopswap:
shopmove:
- shopmove
buy:
- shopbuy
- buy
shopreq:
- shopreq
gamevoicechannel:
- gamevoicechannel
- gvc

View File

@@ -1742,6 +1742,11 @@ shopremove:
desc: "Removes an item from the shop by its ID."
args:
- "1"
shopreq:
desc: "Sets a role which will be required to buy the item on the specified index. Specify only index to remove the requirement."
args:
- "2 Gamers"
- "2"
shopchangename:
desc: "Change the name of a shop entry at the specified index. Only works for non-role items"
args:

View File

@@ -725,6 +725,11 @@
"shop_role_already_bought": "You already bought this role.",
"shop_role_purchase": "You've successfully purchased {0} role.",
"shop_role_purchase_error": "Error assigning role. Your purchase has been refunded.",
"shop_item_role_req": "Shop item #{0} will now require users to have a {1} role in order to purchase it.",
"shop_item_req_role_unfulfilled": "You don't have the required role '{0}' in order to buy this shop item.",
"shop_item_req_role_not_found": "The required role for this item doesn't exist anymore. Please contact server administrator.",
"shop_item_requires_role": "Requires {0} role to purchase",
"shop_item_role_no_req": "Shop item #{0} will no longer require a role.",
"unique_items_left": "{0} unique items left.",
"blocked_commands": "Blocked Commands",
"blocked_modules": "Blocked Modules",