public async Task <IActionResult> EditBankItem(ulong guildId, int bankId, int itemId, [Bind(nameof(GuildBankItem.Name), nameof(GuildBankItem.Description), nameof(GuildBankItem.Value), nameof(GuildBankItem.ImageUrl), nameof(GuildBankItem.Quantity))] GuildBankItem item) { var userId = User.GetId(); var bank = await _bank.GetGuildBankAsync(guildId, bankId); if (bank == null) { throw new BankNotFoundException(); } if (!await ValidateWriteAccess(bank, userId)) { throw new UserNotGuildModeratorException(); } await _items.UpdateBankItemAsync(bank, itemId, userId, i => { i.Name = item.Name; i.Description = item.Description; i.Value = item.Value; i.ImageUrl = item.ImageUrl; i.Quantity = item.Quantity; }); return(Ok()); }
public async Task LogBankItemDeleteAsync(GuildBank bank, GuildBankItem item, ulong modId) { // Do not log if there is nowhere to log. if (bank.LogChannelId == null) { return; } var guild = await _client.GetGuildAsync(bank.GuildId); var channel = (ISocketMessageChannel)await guild.GetChannelAsync((ulong)bank.LogChannelId); var admin = await guild.GetUserAsync(modId); var logMessage = new EmbedBuilder() .WithAuthor(admin) .WithFooter($"Req. by {admin.Username}#{admin.Discriminator}"); logMessage.WithDescription($"The following item was deleted from bank **{bank.Name}**:"); logMessage .AddField("Name", item.Name, true) .AddField("Description", item.Description ?? "N/A", true) .AddField("Quantity", item.Quantity, true) .AddField("Value", item.Value, true); try { logMessage.WithThumbnailUrl(item.ImageUrl); } catch (ArgumentException) { /* URL is not well formed. Ignore error, will not display image as it wont work in the first place. */ } await channel.SendMessageAsync(embed : logMessage.Build()); }
public async Task LogBankItemQuantityUpdateAsync(GuildBank bank, GuildBankItem item, double amount, ulong modId, ulong?transactorId = null) { // Do not log if there is nowhere to log. if (bank.LogChannelId == null) { return; } var guild = await _client.GetGuildAsync(bank.GuildId); var channel = (ISocketMessageChannel)await guild.GetChannelAsync((ulong)bank.LogChannelId); var admin = await guild.GetUserAsync(modId); var transactor = await guild.GetUserAsync(transactorId ?? modId); var logMessage = new EmbedBuilder() .WithAuthor(transactor) .WithFooter($"Req. by {admin.Username}#{admin.Discriminator}"); try { logMessage.WithThumbnailUrl(item.ImageUrl); } catch (ArgumentException) { /* URL is not well formed. Ignore error, will not display image as it wont work in the first place. */ } var action = amount > 0 ? "Picked up" : "Deposited"; logMessage.WithDescription($"{action} **{amount}** **{item.Name}** from **{bank.Name}**. New Total: **{item.Quantity}**."); await channel.SendMessageAsync(embed : logMessage.Build()).ConfigureAwait(false); }
public async Task LogBankItemUpdateAsync(GuildBank bank, GuildBankItem oldItem, GuildBankItem newItem, ulong modId) { // Do not log if there is nowhere to log. if (bank.LogChannelId == null) { return; } var guild = await _client.GetGuildAsync(bank.GuildId); var channel = (ISocketMessageChannel)await guild.GetChannelAsync((ulong)bank.LogChannelId); var admin = await guild.GetUserAsync(modId); var logMessage = new EmbedBuilder() .WithAuthor(admin) .WithFooter($"Req. by {admin.Username}#{admin.Discriminator}"); logMessage.WithDescription($"The item **{oldItem.Name}** from bank **{bank.Name}** was modified:"); try { logMessage.WithThumbnailUrl(oldItem.ImageUrl); } catch (ArgumentException) { /* URL is not well formed. Ignore error, will not display image as it wont work in the first place. */ } var imgChanged = false; if (oldItem.ImageUrl != newItem.ImageUrl) { try { imgChanged = true; logMessage.WithImageUrl(newItem.ImageUrl); logMessage.AddField("Image", newItem.ImageUrl, true); } catch (ArgumentException) { /* URL is not well formed. Ignore error, will not display image as it wont work in the first place. */ } } if (oldItem.Name != newItem.Name) { logMessage.AddField("Name", $"{oldItem.Name} -> {newItem.Name}", true); } if (oldItem.Description != newItem.Description) { logMessage.AddField("Description", $"{oldItem.Description ?? "N/A"} -> {newItem.Description ?? "N/A"}", true); } if (Math.Abs(oldItem.Quantity - newItem.Quantity) > 0.000001) { logMessage.AddField("Quantity", $"{oldItem.Quantity} -> {newItem.Quantity}", true); } if (Math.Abs(oldItem.Value - newItem.Value) > 0.000001) { logMessage.AddField("Value", $"{oldItem.Value} -> {newItem.Value}", true); } if (logMessage.Fields.Count > 0 || imgChanged) { await channel.SendMessageAsync(embed : logMessage.Build()).ConfigureAwait(false); } }
public async Task <GuildBankItem> CreateBankItemAsync(GuildBank bank, GuildBankItem item, ulong adminId) { if (item.Name == null) { throw new NameNotProvidedException(); } // Ensure item is in the correct bank. item.GuildBankId = bank.Id; // Add the bank to database. _context.GuildBankItems.Add(item); // Log changes. await _transactions.LogBankItemCreateAsync(bank, item, adminId); // Save Changes await _context.SaveChangesAsync(); // Lastly return the item. return(item); }
public async Task <IActionResult> CreateBankItem(ulong guildId, int bankId, [Bind(nameof(GuildBankItem.Name), nameof(GuildBankItem.Description), nameof(GuildBankItem.Value), nameof(GuildBankItem.ImageUrl), nameof(GuildBankItem.Quantity))] GuildBankItem item) { var userId = User.GetId(); var bank = await _bank.GetGuildBankAsync(guildId, bankId); if (bank == null) { throw new BankNotFoundException(); } if (!await ValidateWriteAccess(bank, userId)) { throw new UserNotGuildModeratorException(); } var createdBank = await _items.CreateBankItemAsync(bank, item, userId); return(Content(new ApiGuildBankItem(createdBank))); }