public async Task <ActionResult <ServerLogItem> > CreateServerLogItem(ServerLogItemCreateDto serverLogItemCreateDto) { var serverLogItemModel = _mapper.Map <ServerLogItem>(serverLogItemCreateDto); await _serverLogItemRepo.CreateServerLogItem(serverLogItemModel); await _serverLogItemRepo.SaveChanges(); return(CreatedAtRoute(nameof(GetServerLogItemById), new { Id = serverLogItemModel.Id }, serverLogItemModel)); }
public async Task <HttpStatusCode> SaveServerLogItem(ServerLogItemCreateDto item) { string jsonString = JsonSerializer.Serialize(item); var statusCode = await _client.PostAsync("/api/ServerLogItems/", item); if (statusCode == HttpStatusCode.Unauthorized) { throw new UnauthorizedAccessException("Your username or password is incorrect!"); } _logger.LogDebug("Posting JSON\n {json}\nresult: {result}", jsonString, (int)statusCode); return(statusCode); }
public static async Task <IMessage> SendLogAsync( this ITextChannel channel, string title, string description, Color color, IApiService apiService, string thumbnailUrl = null) { var embed = new EmbedBuilder() .WithTitle(title) .WithDescription(description) .WithColor(color) .WithCurrentTimestamp(); if (thumbnailUrl != null) { embed.WithThumbnailUrl(thumbnailUrl); } IMessage message = null; try { message = await channel.SendMessageAsync(embed : embed.Build()); } catch (Discord.Net.HttpException) { // The bot does not have access to the logging channel, WUT SHOULD WE DO? // We don't really have any good options, the only thing we can send a message // to is the channel we don't have access to. // So for now I think we will just eat the exception... YUMMY! } ServerLogItemCreateDto serverLogItem = new ServerLogItemCreateDto { Guild = new GuildCreateDto { GuildId = channel.Guild.Id, GuildName = channel.Guild.Name }, Channel = new ChannelCreateDto { ChannelId = channel.Id, ChannelName = channel.Name }, Title = title, Description = description, ThumbnailUrl = thumbnailUrl, Date = DateTimeOffset.UtcNow }; if (apiService != null && apiService.ApiIsEnabled) { try { await apiService.ServerLogItemApi.SaveServerLogItem(serverLogItem); } catch (Exception e) { Log.Warning(e, "An exception occured while trying to contact the API"); } } else if (apiService == null) { throw new ArgumentNullException(nameof(apiService)); } return(message); }