Compare commits

...

4 Commits

Author SHA1 Message Date
Kwoth
f68f219a25 fix: ytdataapiv3 searches will no longer duplicate youtube urls 2024-10-30 07:02:13 +00:00
Kwoth
8f16b11d02 api: finance api implementation 2024-10-29 08:15:53 +00:00
Kwoth
df5eced904 change: Error sending greet dm will now be a warning
change: initial canvas price down to 3 from 10, 10 is way too expensive
2024-10-29 01:53:42 +00:00
Kwoth
1dcd158f43 fix: Bot will now not accept .aar Role if that Role is higher than or equal to bot's role. Previously bot would just fail silently, now there is a proper error message. 2024-10-28 21:42:05 +00:00
8 changed files with 204 additions and 13 deletions

View File

@@ -0,0 +1,60 @@
syntax = "proto3";
option csharp_namespace = "NadekoBot.GrpcApi";
import "google/protobuf/timestamp.proto";
package fin;
service GrpcFin {
rpc GetTransactions(GetTransactionsRequest) returns (GetTransactionsReply);
rpc GetHoldings(GetHoldingsRequest) returns (GetHoldingsReply);
rpc Withdraw(WithdrawRequest) returns (WithdrawReply);
rpc Deposit(DepositRequest) returns (DepositReply);
}
message GetTransactionsRequest {
int32 page = 1;
uint64 userId = 2;
}
message GetTransactionsReply {
repeated TransactionReply transactions = 1;
int32 total = 2;
}
message TransactionReply {
int64 amount = 1;
string note = 2;
string type = 3;
string extra = 4;
google.protobuf.Timestamp timestamp = 5;
string id = 6;
}
message GetHoldingsRequest {
uint64 userId = 1;
}
message GetHoldingsReply {
int64 cash = 1;
int64 bank = 2;
}
message WithdrawRequest {
uint64 userId = 1;
int64 amount = 2;
}
message WithdrawReply {
bool success = 1;
}
message DepositRequest {
uint64 userId = 1;
int64 amount = 2;
}
message DepositReply {
bool success = 1;
}

View File

@@ -24,6 +24,13 @@ public partial class Administration
await Response().Error(strs.hierarchy).SendAsync();
return;
}
// the user can't aar the role which is greater or equal to the bot's highest role
if (role.Position >= ((SocketGuild)ctx.Guild).CurrentUser.GetRoles().Max(x => x.Position))
{
await Response().Error(strs.hierarchy).SendAsync();
return;
}
var roles = await _service.ToggleAarAsync(ctx.Guild.Id, role.Id);
if (roles.Count == 0)

View File

@@ -339,7 +339,7 @@ public class GreetService : INService, IReadyExecutor
}
catch (Exception ex)
{
Log.Error(ex, "Error sending greet dm");
Log.Warning(ex, "Unable to send Greet DM. Probably the user has closed DMs");
return false;
}

View File

@@ -20,7 +20,7 @@ public sealed class NCanvasService : INCanvasService, IReadyExecutor, INService
public const int CANVAS_WIDTH = 500;
public const int CANVAS_HEIGHT = 350;
public const int INITIAL_PRICE = 10;
public const int INITIAL_PRICE = 3;
public NCanvasService(
DbService db,
@@ -110,7 +110,7 @@ public sealed class NCanvasService : INCanvasService, IReadyExecutor, INService
var wallet = await _cs.GetWalletAsync(userId);
var paid = await wallet.Take(price, new("canvas", "pixel", $"Bought pixel #{position}"));
var paid = await wallet.Take(price, new("canvas", "pixel-buy", $"Bought pixel {new kwum(position)}"));
if (!paid)
{
return SetPixelResult.NotEnoughMoney;
@@ -138,7 +138,7 @@ public sealed class NCanvasService : INCanvasService, IReadyExecutor, INService
if (!success)
{
await wallet.Add(price, new("canvas", "pixel-refund", $"Refund pixel #{position} purchase"));
await wallet.Add(price, new("canvas", "pixel-refund", $"Refund pixel {new kwum(position)} purchase"));
}
return success ? SetPixelResult.Success : SetPixelResult.InsufficientPayment;

View File

@@ -18,6 +18,9 @@ public sealed class YoutubeDataApiSearchService : IYoutubeSearchService, INServi
if(results.Count == 0)
return null;
return results.Map(r => new VideoInfo(r));
return results.Map(r => new VideoInfo()
{
Url = r
});
}
}

View File

@@ -0,0 +1,89 @@
using Google.Protobuf.WellKnownTypes;
using Grpc.Core;
using NadekoBot.Db.Models;
using NadekoBot.Modules.Gambling.Bank;
using NadekoBot.Modules.NadekoExpressions;
using NadekoBot.Modules.Utility;
namespace NadekoBot.GrpcApi;
public class FinSvc : GrpcFin.GrpcFinBase, IGrpcSvc, INService
{
private readonly ICurrencyService _cs;
private readonly IBankService _bank;
public FinSvc(ICurrencyService cs, IBankService bank)
{
_cs = cs;
_bank = bank;
}
public ServerServiceDefinition Bind()
=> GrpcFin.BindService(this);
[GrpcNoAuthRequired]
public override async Task<DepositReply> Deposit(DepositRequest request, ServerCallContext context)
{
if (request.Amount <= 0)
throw new RpcException(new Status(StatusCode.InvalidArgument, "Amount must be greater than 0"));
var succ = await _bank.DepositAsync(request.UserId, request.Amount);
return new DepositReply
{
Success = succ
};
}
[GrpcNoAuthRequired]
public override async Task<WithdrawReply> Withdraw(WithdrawRequest request, ServerCallContext context)
{
if (request.Amount <= 0)
throw new RpcException(new Status(StatusCode.InvalidArgument, "Amount must be greater than 0"));
var succ = await _bank.WithdrawAsync(request.UserId, request.Amount);
return new WithdrawReply
{
Success = succ
};
}
[GrpcNoAuthRequired]
public override async Task<GetHoldingsReply> GetHoldings(GetHoldingsRequest request, ServerCallContext context)
{
return new GetHoldingsReply
{
Bank = await _bank.GetBalanceAsync(request.UserId),
Cash = await _cs.GetBalanceAsync(request.UserId)
};
}
[GrpcNoAuthRequired]
public override async Task<GetTransactionsReply> GetTransactions(
GetTransactionsRequest request,
ServerCallContext context)
{
if (request.Page < 1)
throw new RpcException(new Status(StatusCode.InvalidArgument, "Page must be greater than 0"));
var trs = await _cs.GetTransactionsAsync(request.UserId, request.Page - 1);
var reply = new GetTransactionsReply
{
Total = await _cs.GetTransactionsCountAsync(request.UserId)
};
reply.Transactions.AddRange(trs.Select(x => new TransactionReply()
{
Id = new kwum(x.Id).ToString(),
Timestamp = Timestamp.FromDateTime(DateTime.UtcNow),
Amount = x.Amount,
Extra = x.Extra ?? string.Empty,
Note = x.Note ?? string.Empty,
Type = x.Type ?? string.Empty,
}));
return reply;
}
}

View File

@@ -40,4 +40,11 @@ public interface ICurrencyService
TxData? txData);
Task<IReadOnlyList<DiscordUser>> GetTopRichest(ulong ignoreId, int page = 0, int perPage = 9);
Task<IReadOnlyList<CurrencyTransaction>> GetTransactionsAsync(
ulong userId,
int page,
int perPage = 15);
Task<int> GetTransactionsCountAsync(ulong userId);
}

View File

@@ -55,14 +55,14 @@ public sealed class CurrencyService : ICurrencyService, INService
{
await using var ctx = _db.GetDbContext();
await ctx
.GetTable<DiscordUser>()
.Where(x => userIds.Contains(x.UserId))
.UpdateAsync(du => new()
{
CurrencyAmount = du.CurrencyAmount >= amount
? du.CurrencyAmount - amount
: 0
});
.GetTable<DiscordUser>()
.Where(x => userIds.Contains(x.UserId))
.UpdateAsync(du => new()
{
CurrencyAmount = du.CurrencyAmount >= amount
? du.CurrencyAmount - amount
: 0
});
await ctx.SaveChangesAsync();
return;
}
@@ -112,4 +112,29 @@ public sealed class CurrencyService : ICurrencyService, INService
await using var uow = _db.GetDbContext();
return await uow.Set<DiscordUser>().GetTopRichest(ignoreId, page, perPage);
}
public async Task<IReadOnlyList<CurrencyTransaction>> GetTransactionsAsync(
ulong userId,
int page,
int perPage = 15)
{
await using var uow = _db.GetDbContext();
var trs = await uow.GetTable<CurrencyTransaction>()
.Where(x => x.UserId == userId)
.OrderByDescending(x => x.DateAdded)
.Skip(perPage * page)
.Take(perPage)
.ToListAsyncLinqToDB();
return trs;
}
public async Task<int> GetTransactionsCountAsync(ulong userId)
{
await using var uow = _db.GetDbContext();
return await uow.GetTable<CurrencyTransaction>()
.Where(x => x.UserId == userId)
.CountAsyncLinqToDB();
}
}