From e09435da37377a2d3dd31559bb056d7a103a2454 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Sat, 19 Mar 2022 21:19:22 +0100 Subject: [PATCH] Moved some ICurrencyService methods to extensions to simplify use --- src/NadekoBot/Modules/Gambling/Gambling.cs | 3 +- .../Services/Currency/CurrencyService.cs | 15 ---------- .../Currency/CurrencyServiceExtensions.cs | 28 +++++++++++++++++++ .../Services/Currency/ICurrencyService.cs | 7 ----- 4 files changed, 29 insertions(+), 24 deletions(-) create mode 100644 src/NadekoBot/Services/Currency/CurrencyServiceExtensions.cs diff --git a/src/NadekoBot/Modules/Gambling/Gambling.cs b/src/NadekoBot/Modules/Gambling/Gambling.cs index 4e64588cf..f6be45831 100644 --- a/src/NadekoBot/Modules/Gambling/Gambling.cs +++ b/src/NadekoBot/Modules/Gambling/Gambling.cs @@ -65,8 +65,7 @@ public partial class Gambling : GamblingModule public async Task GetBalanceStringAsync(ulong userId) { - await using var wallet = await _cs.GetWalletAsync(userId); - var bal = await wallet.GetBalance(); + var bal = await _cs.GetBalanceAsync(userId); return N(bal); } diff --git a/src/NadekoBot/Services/Currency/CurrencyService.cs b/src/NadekoBot/Services/Currency/CurrencyService.cs index c8b050a75..2ceac7ace 100644 --- a/src/NadekoBot/Services/Currency/CurrencyService.cs +++ b/src/NadekoBot/Services/Currency/CurrencyService.cs @@ -99,19 +99,4 @@ public class CurrencyService : ICurrencyService, INService await using var wallet = await GetWalletAsync(user.Id); return await wallet.Take(amount, txData); } - - public async Task TransferAsync( - ulong fromId, - ulong toId, - long amount, - string fromName, - string note) - { - await using var fromWallet = await GetWalletAsync(fromId); - await using var toWallet = await GetWalletAsync(toId); - - var extra = new TxData("gift", fromName, note, fromId); - - return await fromWallet.Transfer(amount, toWallet, extra); - } } \ No newline at end of file diff --git a/src/NadekoBot/Services/Currency/CurrencyServiceExtensions.cs b/src/NadekoBot/Services/Currency/CurrencyServiceExtensions.cs new file mode 100644 index 000000000..8bbafdcb5 --- /dev/null +++ b/src/NadekoBot/Services/Currency/CurrencyServiceExtensions.cs @@ -0,0 +1,28 @@ +using NadekoBot.Services.Currency; + +namespace NadekoBot.Services; + +public static class CurrencyServiceExtensions +{ + public static async Task GetBalanceAsync(this ICurrencyService cs, ulong userId) + { + await using var wallet = await cs.GetWalletAsync(userId); + return await wallet.GetBalance(); + } + + public static async Task TransferAsync( + this ICurrencyService cs, + ulong fromId, + ulong toId, + long amount, + string fromName, + string note) + { + await using var fromWallet = await cs.GetWalletAsync(fromId); + await using var toWallet = await cs.GetWalletAsync(toId); + + var extra = new TxData("gift", fromName, note, fromId); + + return await fromWallet.Transfer(amount, toWallet, extra); + } +} \ No newline at end of file diff --git a/src/NadekoBot/Services/Currency/ICurrencyService.cs b/src/NadekoBot/Services/Currency/ICurrencyService.cs index 556f2194a..5b7bf7c78 100644 --- a/src/NadekoBot/Services/Currency/ICurrencyService.cs +++ b/src/NadekoBot/Services/Currency/ICurrencyService.cs @@ -38,11 +38,4 @@ public interface ICurrencyService IUser user, long amount, TxData txData); - - Task TransferAsync( - ulong from, - ulong to, - long amount, - string fromName, - string note); } \ No newline at end of file