- Added a simple bank system. Users can deposit, withdraw and check the balance of their currency in the bank.

- Users can't check other user's bank balances.
- Added a button on a .$ command which, when clicked, sends you a message with your bank balance that only you can see.
- Updated pagination, it now uses buttons instead of reactions
- using .h <command group> (atm only .bank is a proper group) will list commands with their descriptions in that group
This commit is contained in:
Kwoth
2022-04-29 06:47:00 +02:00
parent 3b6b3bcf07
commit f132aa2624
25 changed files with 10588 additions and 78 deletions

View File

@@ -0,0 +1,61 @@
using NadekoBot.Modules.Gambling.Bank;
using NadekoBot.Modules.Gambling.Common;
using NadekoBot.Modules.Gambling.Services;
namespace NadekoBot.Modules.Gambling;
// todo .h [group] should show commands in that group
public partial class Gambling
{
[Name("Bank")]
[Group("bank")]
public partial class BankCommands : GamblingModule<IBankService>
{
private readonly IBankService _bank;
public BankCommands(GamblingConfigService gcs, IBankService bank) : base(gcs)
{
_bank = bank;
}
[Cmd]
public async partial Task BankDeposit(ShmartNumber amount)
{
if (amount <= 0)
return;
if (await _bank.DepositAsync(ctx.User.Id, amount))
{
await ReplyConfirmLocalizedAsync(strs.bank_deposited(N(amount)));
}
else
{
await ReplyErrorLocalizedAsync(strs.not_enough(CurrencySign));
}
}
[Cmd]
public async partial Task BankWithdraw(ShmartNumber amount)
{
if (amount <= 0)
return;
if (await _bank.WithdrawAsync(ctx.User.Id, amount))
{
await ReplyConfirmLocalizedAsync(strs.bank_withdrew(N(amount)));
}
else
{
await ReplyErrorLocalizedAsync(strs.bank_withdraw_insuff(CurrencySign));
}
}
[Cmd]
public async partial Task BankBalance()
{
var bal = await _bank.GetBalanceAsync(ctx.User.Id);
await ReplyConfirmLocalizedAsync(strs.bank_balance(N(bal)));
}
}
}

View File

@@ -0,0 +1,77 @@
using LinqToDB;
using LinqToDB.EntityFrameworkCore;
namespace NadekoBot.Modules.Gambling.Bank;
public sealed class BankService : IBankService, INService
{
private readonly ICurrencyService _cur;
private readonly DbService _db;
public BankService(ICurrencyService cur, DbService db)
{
_cur = cur;
_db = db;
}
public async Task<bool> DepositAsync(ulong userId, long amount)
{
if (amount <= 0)
throw new ArgumentOutOfRangeException(nameof(amount));
if (!await _cur.RemoveAsync(userId, amount, new("bank", "deposit")))
return false;
await using var ctx = _db.GetDbContext();
await ctx.BankUsers
.ToLinqToDBTable()
.InsertOrUpdateAsync(() => new()
{
UserId = userId,
Balance = amount
},
(old) => new()
{
Balance = old.Balance + amount
},
() => new()
{
UserId = userId
});
return true;
}
public async Task<bool> WithdrawAsync(ulong userId, long amount)
{
if (amount <= 0)
throw new ArgumentOutOfRangeException(nameof(amount));
await using var ctx = _db.GetDbContext();
var rows = await ctx.BankUsers
.ToLinqToDBTable()
.Where(x => x.UserId == userId && x.Balance >= amount)
.UpdateAsync((old) => new()
{
Balance = old.Balance - amount
});
if (rows > 0)
{
await _cur.AddAsync(userId, amount, new("bank", "withdraw"));
return true;
}
return false;
}
public async Task<long> GetBalanceAsync(ulong userId)
{
await using var ctx = _db.GetDbContext();
return (await ctx.BankUsers
.ToLinqToDBTable()
.FirstOrDefaultAsync(x => x.UserId == userId))
?.Balance
?? 0;
}
}

View File

@@ -0,0 +1,8 @@
namespace NadekoBot.Modules.Gambling.Bank;
public interface IBankService
{
Task<bool> DepositAsync(ulong userId, long amount);
Task<bool> WithdrawAsync(ulong userId, long amount);
Task<long> GetBalanceAsync(ulong userId);
}

View File

@@ -3,6 +3,7 @@ using LinqToDB;
using LinqToDB.EntityFrameworkCore;
using NadekoBot.Db;
using NadekoBot.Db.Models;
using NadekoBot.Modules.Gambling.Bank;
using NadekoBot.Modules.Gambling.Common;
using NadekoBot.Modules.Gambling.Services;
using NadekoBot.Services.Currency;
@@ -40,6 +41,7 @@ public partial class Gambling : GamblingModule<GamblingService>
private readonly NumberFormatInfo _enUsCulture;
private readonly DownloadTracker _tracker;
private readonly GamblingConfigService _configService;
private readonly IBankService _bank;
private IUserMessage rdMsg;
@@ -49,13 +51,16 @@ public partial class Gambling : GamblingModule<GamblingService>
IDataCache cache,
DiscordSocketClient client,
DownloadTracker tracker,
GamblingConfigService configService)
GamblingConfigService configService,
IBankService bank)
: base(configService)
{
_db = db;
_cs = currency;
_cache = cache;
_client = client;
_bank = bank;
_enUsCulture = new CultureInfo("en-US", false).NumberFormat;
_enUsCulture.NumberDecimalDigits = 0;
_enUsCulture.NumberGroupSeparator = "";
@@ -312,6 +317,31 @@ public partial class Gambling : GamblingModule<GamblingService>
_ => $"{type.Titleize()} - {subType.Titleize()}"
};
public sealed class CashInteraction : NadekoInteraction
{
public override string Name
=> "CASH_OPEN_BANK";
public override IEmote Emote
=> new Emoji("🏦");
public CashInteraction(
[NotNull] DiscordSocketClient client,
ulong authorId,
Func<SocketMessageComponent, Task> onAction)
: base(client, authorId, onAction)
{
}
public static CashInteraction Create(
DiscordSocketClient client,
ulong userId,
Func<SocketMessageComponent, Task> onAction)
=> new(client, userId, onAction);
}
[Cmd]
[Priority(0)]
public async partial Task Cash(ulong userId)
@@ -319,14 +349,30 @@ public partial class Gambling : GamblingModule<GamblingService>
var cur = await GetBalanceStringAsync(userId);
await ReplyConfirmLocalizedAsync(strs.has(Format.Code(userId.ToString()), cur));
}
private async Task BankAction(SocketMessageComponent smc)
{
var balance = await _bank.GetBalanceAsync(ctx.User.Id);
await smc.RespondAsync(GetText(strs.bank_balance(N(balance))), ephemeral: true);
}
[Cmd]
[Priority(1)]
public async partial Task Cash([Leftover] IUser user = null)
{
user ??= ctx.User;
var cur = await GetBalanceStringAsync(user.Id);
await ConfirmLocalizedAsync(strs.has(Format.Bold(user.ToString()), cur));
if (user == ctx.User)
{
var inter = CashInteraction.Create(_client, ctx.User.Id, BankAction);
await ConfirmLocalizedAsync(strs.has(Format.Bold(user.ToString()), cur), inter);
}
else
{
await ConfirmLocalizedAsync(strs.has(Format.Bold(user.ToString()), cur));
}
}
[Cmd]

View File

@@ -266,6 +266,20 @@ public partial class Help : NadekoModule<HelpService>
await ctx.Channel.EmbedAsync(embed);
}
private async Task Group(ModuleInfo group)
{
var eb = _eb.Create(ctx)
.WithTitle(GetText(strs.cmd_group_commands(group.Name)))
.WithOkColor();
foreach (var cmd in group.Commands)
{
eb.AddField(prefix + cmd.Aliases.First(), cmd.RealSummary(_strings, _medusae, Culture, prefix));
}
await ctx.Channel.EmbedAsync(eb);
}
[Cmd]
[Priority(0)]
public async partial Task H([Leftover] string fail)
@@ -278,6 +292,20 @@ public partial class Help : NadekoModule<HelpService>
return;
}
if (fail.StartsWith(prefix))
fail = fail.Substring(prefix.Length);
var group = _cmds.Modules
.SelectMany(x => x.Submodules)
.Where(x => !string.IsNullOrWhiteSpace(x.Group))
.FirstOrDefault(x => x.Group.Equals(fail, StringComparison.InvariantCultureIgnoreCase));
if (group is not null)
{
await Group(group);
return;
}
await ReplyErrorLocalizedAsync(strs.command_not_found);
}