public async Task GetBalanceAsync(CommandContext ctx, [Description("User.")] DiscordUser user = null) { user = user ?? ctx.User; long?balance; using (DatabaseContext db = this.Database.CreateContext()) { DatabaseBankAccount account = await db.BankAccounts.FindAsync((long)ctx.Guild.Id, (long)user.Id); balance = account?.Balance; } var emb = new DiscordEmbedBuilder { Title = $"{StaticDiscordEmoji.MoneyBag} Bank account for {user.Username}", Color = this.ModuleColor, ThumbnailUrl = user.AvatarUrl }; if (balance.HasValue) { emb.WithDescription($"Account value: {Formatter.Bold(balance.Value.ToWords())} {this.Shared.GetGuildConfig(ctx.Guild.Id).Currency ?? "credits"}"); emb.AddField("Numeric value", $"{balance.Value:n0}"); } else { emb.WithDescription($"No existing account! Use command {Formatter.InlineCode("bank register")} to open an account."); } emb.WithFooter("\"Your money is safe in our hands.\" - WM Bank"); await ctx.RespondAsync(embed : emb.Build()); }
public static async Task ModifyBankAccountAsync(this DatabaseContext db, ulong uid, ulong gid, Func <long, long> balanceModifier) { bool created = false; DatabaseBankAccount account = await db.BankAccounts.FindAsync((long)gid, (long)uid); if (account is null) { account = new DatabaseBankAccount { GuildId = gid, UserId = uid }; created = true; } account.Balance = balanceModifier(account.Balance); if (account.Balance < 0) { account.Balance = 0; } if (created) { db.BankAccounts.Add(account); } else { db.BankAccounts.Update(account); } }
public static async Task <bool> TryDecreaseBankAccountAsync(this DatabaseContext db, ulong uid, ulong gid, long amount) { DatabaseBankAccount account = await db.BankAccounts.FindAsync((long)gid, (long)uid); if (account is null || account.Balance < amount) { return(false); } account.Balance -= amount; db.BankAccounts.Update(account); return(true); }
public async Task GrantAsync(CommandContext ctx, [Description("User.")] DiscordUser user, [Description("Amount.")] long amount) { if (amount < 0 || amount > 1_000_000_000_000) { throw new InvalidCommandUsageException($"Invalid amount! Needs to be in range [1, {1_000_000_000_000:n0}]"); } using (DatabaseContext db = this.Database.CreateContext()) { DatabaseBankAccount account = await db.BankAccounts.FindAsync((long)ctx.Guild.Id, (long)user.Id); if (account is null) { throw new CommandFailedException("Given user does not have a WM bank account!"); } account.Balance += amount; db.BankAccounts.Update(account); await db.SaveChangesAsync(); } await this.InformAsync(ctx, StaticDiscordEmoji.MoneyBag, $"{Formatter.Bold(user.Mention)} won {Formatter.Bold($"{amount:n0}")} {this.Shared.GetGuildConfig(ctx.Guild.Id).Currency ?? "credits"} on the lottery! (seems legit)"); }