Fixed .bank take, added .bank seize alias. Added .bank award

This commit is contained in:
Kwoth
2022-08-11 15:14:08 +02:00
parent 1affeb0683
commit a065189023
5 changed files with 74 additions and 3 deletions

View File

@@ -75,7 +75,21 @@ public partial class Gambling
private async Task BankTakeInternalAsync(long amount, ulong userId)
{
if (await _bank.WithdrawAsync(userId, amount))
if (await _bank.TakeAsync(userId, amount))
{
await ReplyErrorLocalizedAsync(strs.take_fail(N(amount),
_client.GetUser(userId)?.ToString()
?? userId.ToString(),
CurrencySign));
return;
}
await ctx.OkAsync();
}
private async Task BankAwardInternalAsync(long amount, ulong userId)
{
if (await _bank.AwardAsync(userId, amount))
{
await ReplyErrorLocalizedAsync(strs.take_fail(N(amount),
_client.GetUser(userId)?.ToString()
@@ -89,7 +103,7 @@ public partial class Gambling
[Cmd]
[OwnerOnly]
[Priority(-1)]
[Priority(1)]
public async Task BankTake(long amount, [Leftover] IUser user)
=> await BankTakeInternalAsync(amount, user.Id);
@@ -98,5 +112,10 @@ public partial class Gambling
[Priority(0)]
public async Task BankTake(long amount, ulong userId)
=> await BankTakeInternalAsync(amount, userId);
[Cmd]
[OwnerOnly]
public async Task BankAward(long amount, [Leftover] IUser user)
=> await BankAwardInternalAsync(amount, user.Id);
}
}

View File

@@ -15,6 +15,48 @@ public sealed class BankService : IBankService, INService
_db = db;
}
public async Task<bool> AwardAsync(ulong userId, long amount)
{
if (amount <= 0)
throw new ArgumentOutOfRangeException(nameof(amount));
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> TakeAsync(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
});
return rows > 0;
}
public async Task<bool> DepositAsync(ulong userId, long amount)
{
if (amount <= 0)

View File

@@ -5,4 +5,6 @@ public interface IBankService
Task<bool> DepositAsync(ulong userId, long amount);
Task<bool> WithdrawAsync(ulong userId, long amount);
Task<long> GetBalanceAsync(ulong userId);
Task<bool> AwardAsync(ulong userId, long amount);
Task<bool> TakeAsync(ulong userId, long amount);
}

View File

@@ -374,6 +374,7 @@ award:
- award
take:
- take
- seize
betroll:
- betroll
- br
@@ -1323,6 +1324,9 @@ bankbalance:
- bal
banktake:
- take
- seize
bankaward:
- award
# Patron
patron:
- patron

View File

@@ -2238,9 +2238,13 @@ bankbalance:
args:
- ""
banktake:
desc: "Takes the specified from a user's bank"
desc: "Takes the specified amount of currency from a user's bank"
args:
- "500 @MoniLaunder"
bankaward:
desc: "Award the specified amount of currency to a user's bank"
args:
- "99999 @Bestie"
patron:
desc: "Check your patronage status and command usage quota. Bot owners can check targeted user's patronage status."
args: