public void Setup() { _context = new BotDbContext(); _context.Database.EnsureCreated(); var piter = new Human("Piter", 19); _context.Add(piter); _context.SaveChanges(); }
public async Task PrefixSet([Name("Prefix")] string prefix) { var config = _dbContext.Configurations.SingleOrDefault(c => c.GuildId == Context.Guild.Id); if (config is null) { _dbContext.Add(new ServerConfiguration(Context.Guild.Id, prefix)); } else { config.Prefix = prefix; } await _dbContext.SaveChangesAsync(); await ReplyAsync("Prefix set!"); }
public async Task LeagueSet([Name("Username")] string summonerName, [Name("Region")] string region) { var user = _context.LeagueSummoners.SingleOrDefault(u => u.DiscordId == Context.User.Id); if (user is null) { _context.Add(new LeagueSummoner(Context.User.Id, summonerName, region)); await ReplyAsync("User registered!"); return; } user.SummonerName = summonerName; user.Region = region; await _context.SaveChangesAsync(); await ReplyAsync("User updated!"); }
public async Task OsuSet( [Name("Username")] string username, [Name("Default game mode")] int gameMode = 0 ) { var user = _context.OsuUsers.SingleOrDefault(u => u.DiscordId == Context.User.Id); if (user is null) { _context.Add(new OsuUser(Context.User.Id, username, gameMode)); await _context.SaveChangesAsync(); await ReplyAsync("User registered!"); return; } user.OsuUsername = username; user.DefaultGameMode = gameMode; await _context.SaveChangesAsync(); await ReplyAsync("User updated!"); }
/// <summary>Withdraws given amount of money to specified address.</summary> /// <exception cref="CommandExecutionException">Thrown when user supplied invalid input data.</exception> public void Withdraw(IUser user, decimal amount, string address) { this.logger.Trace("({0}:{1},{2}:{3},{4}:'{5}')", nameof(user), user.Id, nameof(amount), amount, nameof(address), address); this.AssertAmountPositive(amount); if (amount < this.settings.MinWithdrawAmount) { this.logger.Trace("(-)[MIN_WITHDRAW_AMOUNT]"); throw new CommandExecutionException($"Minimal withdraw amount is {this.settings.MinWithdrawAmount} {this.settings.Ticker}."); } using (BotDbContext context = this.contextFactory.CreateContext()) { DiscordUserModel discordUser = this.GetOrCreateUser(context, user); // Don't allow withdrawals to deposit address. if (discordUser.DepositAddress != null && discordUser.DepositAddress == address) { this.logger.Trace("(-)[WITHDRAWAL_TO_DEPOSIT_ADDR]"); throw new CommandExecutionException("You can't withdraw to your own deposit address!"); } decimal amountToSend = amount - this.settings.NetworkFee; this.logger.Trace("(The amount after fee: {0})", amountToSend); this.AssertBalanceIsSufficient(discordUser, amountToSend); try { var withdrawResult = this.nodeIntegration.Withdraw(amountToSend, address); discordUser.Balance -= amount; context.Update(discordUser); context.SaveChanges(); var withdrawHistoryItem = new WithdrawHistory { DiscordUserId = discordUser.DiscordUserId, Amount = amountToSend, ToAddress = address, TransactionId = withdrawResult.TransactionId, WithdrawTime = DateTime.UtcNow }; context.Add(withdrawHistoryItem); context.SaveChanges(); this.logger.Debug("User '{0}' withdrew {1} to address '{2}'. After fee subtracted '{3}'", discordUser, amount, address, amountToSend); } catch (InvalidAddressException) { this.logger.Trace("(-)[INVALID_ADDRESS]"); throw new CommandExecutionException("Address specified is invalid."); } catch (Exception ex) { this.logger.Trace("(-)[FAILED_WITHDRAW]"); this.logger.Debug($"(WITHDRAW_ERROR)[{ex.Message}]"); throw new CommandExecutionException($"Failed to withdraw. Contact {this.settings.Discord.SupportUsername}"); } } this.logger.Trace("(-)"); }