Added .shopadd cmd

This commit is contained in:
Kwoth
2024-04-23 14:18:30 +00:00
parent 1aebca14e1
commit 74690c73d6
22 changed files with 18474 additions and 6275 deletions

View File

@@ -1,4 +1,6 @@
#nullable disable
using Nadeko.Bot.Db.Models;
namespace NadekoBot.Modules.Gambling.Services;
public interface IShopService
@@ -40,4 +42,5 @@ public interface IShopService
Task<bool> MoveEntryAsync(ulong guildId, int fromIndex, int toIndex);
Task<bool> SetItemRoleRequirementAsync(ulong guildId, int index, ulong? roleId);
Task<ShopEntry> AddShopCommandAsync(ulong guildId, ulong userId, int price, string command);
}

View File

@@ -4,6 +4,7 @@ using NadekoBot.Db;
using NadekoBot.Modules.Gambling.Common;
using NadekoBot.Modules.Gambling.Services;
using Nadeko.Bot.Db.Models;
using NadekoBot.Modules.Administration;
namespace NadekoBot.Modules.Gambling;
@@ -22,6 +23,12 @@ public partial class Gambling
Role
}
public enum Command
{
Command,
Cmd
}
private readonly DbService _db;
private readonly ICurrencyService _cs;
@@ -39,8 +46,8 @@ public partial class Gambling
using var uow = _db.GetDbContext();
var entries = uow.GuildConfigsForId(ctx.Guild.Id,
set => set.Include(x => x.ShopEntries).ThenInclude(x => x.Items))
.ShopEntries.ToIndexed();
set => set.Include(x => x.ShopEntries).ThenInclude(x => x.Items))
.ShopEntries.ToIndexed();
return ctx.SendPaginatedConfirmAsync(page,
curPage =>
{
@@ -156,7 +163,7 @@ public partial class Gambling
return;
}
if (entry.Type == ShopEntryType.List)
else if (entry.Type == ShopEntryType.List)
{
if (entry.Items.Count == 0)
{
@@ -177,11 +184,11 @@ public partial class Gambling
try
{
await ctx.User.EmbedAsync(_eb.Create()
.WithOkColor()
.WithTitle(GetText(strs.shop_purchase(ctx.Guild.Name)))
.AddField(GetText(strs.item), item.Text)
.AddField(GetText(strs.price), entry.Price.ToString(), true)
.AddField(GetText(strs.name), entry.Name, true));
.WithOkColor()
.WithTitle(GetText(strs.shop_purchase(ctx.Guild.Name)))
.AddField(GetText(strs.item), item.Text)
.AddField(GetText(strs.price), entry.Price.ToString(), true)
.AddField(GetText(strs.name), entry.Name, true));
await _cs.AddAsync(entry.AuthorId,
GetProfitAmount(entry.Price),
@@ -193,9 +200,9 @@ public partial class Gambling
await using (var uow = _db.GetDbContext())
{
var entries = new IndexedCollection<ShopEntry>(uow.GuildConfigsForId(ctx.Guild.Id,
set => set.Include(x => x.ShopEntries)
.ThenInclude(x => x.Items))
.ShopEntries);
set => set.Include(x => x.ShopEntries)
.ThenInclude(x => x.Items))
.ShopEntries);
entry = entries.ElementAtOrDefault(index);
if (entry is not null)
{
@@ -213,11 +220,79 @@ public partial class Gambling
else
await ReplyErrorLocalizedAsync(strs.not_enough(CurrencySign));
}
else if (entry.Type == ShopEntryType.Command)
{
var guild = ctx.Guild as SocketGuild;
var channel = ctx.Channel as ISocketMessageChannel;
var msg = ctx.Message as SocketUserMessage;
var user = await ctx.Guild.GetUserAsync(entry.AuthorId);
if (guild is null || channel is null || msg is null || user is null)
{
await ReplyErrorLocalizedAsync(strs.shop_command_invalid_context);
return;
}
if (!await _cs.RemoveAsync(ctx.User.Id, entry.Price, new("shop", "buy", entry.Type.ToString())))
{
await ReplyErrorLocalizedAsync(strs.not_enough(CurrencySign));
return;
}
else
{
var cmd = entry.Command.Replace("%you%", ctx.User.Id.ToString());
var eb = _eb.Create()
.WithPendingColor()
.WithTitle("Executing shop command")
.WithDescription(cmd);
var msgTask = ctx.Channel.EmbedAsync(eb);
await _cs.AddAsync(entry.AuthorId,
GetProfitAmount(entry.Price),
new("shop", "sell", entry.Name));
await _cmdHandler.TryRunCommand(guild,
channel,
new DoAsUserMessage(
msg,
user,
cmd
));
try
{
var pendingMsg = await msgTask;
await pendingMsg.EditAsync(SmartEmbedText.FromEmbed(eb
.WithOkColor()
.WithTitle("Shop command executed")
.Build()));
}
catch
{
}
}
}
}
private static long GetProfitAmount(int price)
=> (int)Math.Ceiling(0.90 * price);
[Cmd]
[RequireContext(ContextType.Guild)]
[UserPerm(GuildPerm.Administrator)]
[BotPerm(GuildPerm.ManageRoles)]
public async Task ShopAdd(Command _, int price, [Leftover] string command)
{
if (price < 1)
return;
var entry = await _service.AddShopCommandAsync(ctx.Guild.Id, ctx.User.Id, price, command);
await ctx.Channel.EmbedAsync(EntryToEmbed(entry).WithTitle(GetText(strs.shop_item_add)));
}
[Cmd]
[RequireContext(ContextType.Guild)]
[UserPerm(GuildPerm.Administrator)]
@@ -239,9 +314,9 @@ public partial class Gambling
await using (var uow = _db.GetDbContext())
{
var entries = new IndexedCollection<ShopEntry>(uow.GuildConfigsForId(ctx.Guild.Id,
set => set.Include(x => x.ShopEntries)
.ThenInclude(x => x.Items))
.ShopEntries)
set => set.Include(x => x.ShopEntries)
.ThenInclude(x => x.Items))
.ShopEntries)
{
entry
};
@@ -271,9 +346,9 @@ public partial class Gambling
await using (var uow = _db.GetDbContext())
{
var entries = new IndexedCollection<ShopEntry>(uow.GuildConfigsForId(ctx.Guild.Id,
set => set.Include(x => x.ShopEntries)
.ThenInclude(x => x.Items))
.ShopEntries)
set => set.Include(x => x.ShopEntries)
.ThenInclude(x => x.Items))
.ShopEntries)
{
entry
};
@@ -302,9 +377,9 @@ public partial class Gambling
await using (var uow = _db.GetDbContext())
{
var entries = new IndexedCollection<ShopEntry>(uow.GuildConfigsForId(ctx.Guild.Id,
set => set.Include(x => x.ShopEntries)
.ThenInclude(x => x.Items))
.ShopEntries);
set => set.Include(x => x.ShopEntries)
.ThenInclude(x => x.Items))
.ShopEntries);
entry = entries.ElementAtOrDefault(index);
if (entry is not null && (rightType = entry.Type == ShopEntryType.List))
{
@@ -456,19 +531,27 @@ public partial class Gambling
if (entry.Type == ShopEntryType.Role)
{
return embed
.AddField(GetText(strs.name),
GetText(strs.shop_role(Format.Bold(ctx.Guild.GetRole(entry.RoleId)?.Name
?? "MISSING_ROLE"))),
true)
.AddField(GetText(strs.price), N(entry.Price), true)
.AddField(GetText(strs.type), entry.Type.ToString(), true);
.AddField(GetText(strs.name),
GetText(strs.shop_role(Format.Bold(ctx.Guild.GetRole(entry.RoleId)?.Name
?? "MISSING_ROLE"))),
true)
.AddField(GetText(strs.price), N(entry.Price), true)
.AddField(GetText(strs.type), entry.Type.ToString(), true);
}
if (entry.Type == ShopEntryType.List)
{
return embed.AddField(GetText(strs.name), entry.Name, true)
.AddField(GetText(strs.price), N(entry.Price), true)
.AddField(GetText(strs.type), GetText(strs.random_unique_item), true);
.AddField(GetText(strs.price), N(entry.Price), true)
.AddField(GetText(strs.type), GetText(strs.random_unique_item), true);
}
else if (entry.Type == ShopEntryType.Command)
{
return embed
.AddField(GetText(strs.name), Format.Code(entry.Command), true)
.AddField(GetText(strs.price), N(entry.Price), true)
.AddField(GetText(strs.type), entry.Type.ToString(), true);
}
//else if (entry.Type == ShopEntryType.Infinite_List)
@@ -490,6 +573,9 @@ public partial class Gambling
+ GetText(strs.shop_role(Format.Bold(ctx.Guild.GetRole(entry.RoleId)?.Name ?? "MISSING_ROLE")));
if (entry.Type == ShopEntryType.List)
return prepend + GetText(strs.unique_items_left(entry.Items.Count)) + "\n" + entry.Name;
if (entry.Type == ShopEntryType.Command)
return prepend + Format.Code(entry.Command);
return prepend;
}
}

View File

@@ -1,7 +1,6 @@
#nullable disable
using Microsoft.EntityFrameworkCore;
using NadekoBot.Db;
using Nadeko.Bot.Db.Models;
namespace NadekoBot.Modules.Gambling.Services;
@@ -14,8 +13,10 @@ public class ShopService : IShopService, INService
=> _db = db;
private IndexedCollection<ShopEntry> GetEntriesInternal(DbContext uow, ulong guildId)
=> uow.GuildConfigsForId(guildId, set => set.Include(x => x.ShopEntries).ThenInclude(x => x.Items))
.ShopEntries.ToIndexed();
=> uow.GuildConfigsForId(guildId,
set => set.Include(x => x.ShopEntries)
.ThenInclude(x => x.Items))
.ShopEntries.ToIndexed();
public async Task<bool> ChangeEntryPriceAsync(ulong guildId, int index, int newPrice)
{
@@ -109,4 +110,24 @@ public class ShopService : IShopService, INService
await uow.SaveChangesAsync();
return true;
}
public async Task<ShopEntry> AddShopCommandAsync(ulong guildId, ulong userId, int price, string command)
{
await using var uow = _db.GetDbContext();
var entries = GetEntriesInternal(uow, guildId);
var entry = new ShopEntry()
{
AuthorId = userId,
Command = command,
Type = ShopEntryType.Command,
Price = price,
};
entries.Add(entry);
uow.GuildConfigsForId(guildId, set => set).ShopEntries = entries;
await uow.SaveChangesAsync();
return entry;
}
}