diff --git a/DisCatSharp/Entities/Guild/AuditLog/DiscordAuditEntryInfo.cs b/DisCatSharp/Entities/Guild/AuditLog/DiscordAuditEntryInfo.cs index 6a1c37576b..0e97fe8265 100644 --- a/DisCatSharp/Entities/Guild/AuditLog/DiscordAuditEntryInfo.cs +++ b/DisCatSharp/Entities/Guild/AuditLog/DiscordAuditEntryInfo.cs @@ -7,8 +7,21 @@ namespace DisCatSharp.Entities; /// /// Represents additional information about the . /// -public sealed class DiscordAuditEntryInfo +public sealed class DiscordAuditEntryInfo : ObservableApiObject { + /// + /// Gets or sets the guild in which the entities were targeted. + /// + [JsonIgnore] + internal DiscordGuild Guild + => (this.Discord.Guilds.TryGetValue(this.GuildId, out var guild) ? guild : null!)!; + + /// + /// Gets or sets the ID of the guild in which the entities were targeted. + /// + [JsonIgnore] + internal ulong GuildId { get; set; } + /// /// Gets or sets the ID of the app whose permissions were targeted. /// Event Type: . @@ -37,6 +50,13 @@ public sealed class DiscordAuditEntryInfo [JsonProperty("channel_id", NullValueHandling = NullValueHandling.Ignore)] public ulong? ChannelId { get; internal set; } = null; + /// + /// Gets the channel in which the entities were targeted. + /// + [JsonIgnore] + public DiscordChannel? Channel + => this.ChannelId.HasValue ? this.Guild.GetChannel(this.ChannelId.Value) : null; + /// /// Gets or sets the number of entities that were targeted. /// Event Types: , , , diff --git a/DisCatSharp/Entities/Guild/AuditLog/DiscordAuditLog.cs b/DisCatSharp/Entities/Guild/AuditLog/DiscordAuditLog.cs index 6176cf4556..c58b2a9d13 100644 --- a/DisCatSharp/Entities/Guild/AuditLog/DiscordAuditLog.cs +++ b/DisCatSharp/Entities/Guild/AuditLog/DiscordAuditLog.cs @@ -9,6 +9,12 @@ namespace DisCatSharp.Entities; /// public sealed class DiscordAuditLog : ObservableApiObject { + /// + /// Gets or sets the ID of the guild in which the entities were targeted. + /// + [JsonIgnore] + internal ulong GuildId { get; set; } + /// /// List of application commands referenced in the . /// diff --git a/DisCatSharp/Entities/Guild/AuditLog/DiscordAuditLogEntry.cs b/DisCatSharp/Entities/Guild/AuditLog/DiscordAuditLogEntry.cs index 1d38133be4..27930f4a18 100644 --- a/DisCatSharp/Entities/Guild/AuditLog/DiscordAuditLogEntry.cs +++ b/DisCatSharp/Entities/Guild/AuditLog/DiscordAuditLogEntry.cs @@ -30,6 +30,10 @@ public class DiscordAuditLogEntry : SnowflakeObject [JsonProperty("user_id", NullValueHandling = NullValueHandling.Ignore)] public ulong? UserId { get; internal set; } + [JsonIgnore] + public DiscordUser? User + => this.UserId.HasValue ? this.Discord.GetCachedOrEmptyUserInternal(this.UserId.Value) : null; + /// /// Gets the type of action that occurred in the audit log entry. /// diff --git a/DisCatSharp/Entities/Guild/DiscordGuild.cs b/DisCatSharp/Entities/Guild/DiscordGuild.cs index 96334cceb9..0a1a80e0fd 100644 --- a/DisCatSharp/Entities/Guild/DiscordGuild.cs +++ b/DisCatSharp/Entities/Guild/DiscordGuild.cs @@ -2306,6 +2306,7 @@ public async Task> GetAuditLogsAsync(int? limit = last = alr.Entries[^1].Id; alrs.Add(alr); } + return alrs; } diff --git a/DisCatSharp/Net/Rest/DiscordApiClient.cs b/DisCatSharp/Net/Rest/DiscordApiClient.cs index 94419656ed..1051a369c7 100644 --- a/DisCatSharp/Net/Rest/DiscordApiClient.cs +++ b/DisCatSharp/Net/Rest/DiscordApiClient.cs @@ -1174,6 +1174,16 @@ internal async Task GetAuditLogsAsync(ulong guildId, int limit, var res = await this.DoRequestAsync(this.Discord, bucket, url, RestRequestMethod.GET, route).ConfigureAwait(false); var auditLogData = DiscordJson.DeserializeObject(res.Response, this.Discord); + auditLogData.GuildId = guildId; + auditLogData.Entries.ForEach(x => + { + x.Discord = this.Discord; + if (x.Options is null) + return; + + x.Options.Discord = this.Discord; + x.Options.GuildId = auditLogData.GuildId; + }); return auditLogData; } diff --git a/DisCatSharp/Net/Serialization/DiscordJson.cs b/DisCatSharp/Net/Serialization/DiscordJson.cs index fdf217f9d7..d7862619b2 100644 --- a/DisCatSharp/Net/Serialization/DiscordJson.cs +++ b/DisCatSharp/Net/Serialization/DiscordJson.cs @@ -70,6 +70,7 @@ private static string SerializeObjectInternal(object value, Type type, JsonSeria jsonTextWriter.Formatting = jsonSerializer.Formatting; jsonSerializer.Serialize(jsonTextWriter, value, type); } + return stringWriter.ToString(); } @@ -81,25 +82,26 @@ private static T DeserializeObjectInternal(string json, BaseDiscordClient? di })!; if (discord == null) return obj; + obj.Discord = discord; if (!discord.Configuration.ReportMissingFields || !obj.AdditionalProperties.Any()) return obj; + var sentryMessage = "Found missing properties in api response for " + obj.GetType().Name; List sentryFields = new(); var vals = 0; foreach (var ap in obj.AdditionalProperties) { vals++; - if (obj.IgnoredJsonKeys.Count == 0 || !obj.IgnoredJsonKeys.Any(x => x == ap.Key)) + if (obj.IgnoredJsonKeys.Count == 0 || obj.IgnoredJsonKeys.All(x => x != ap.Key)) { if (vals == 1) - { if (discord.Configuration.EnableLibraryDeveloperMode) { discord.Logger.LogInformation("{sentry}", sentryMessage); discord.Logger.LogDebug("{json}", json); } - } + sentryFields.Add(ap.Key); if (discord.Configuration.EnableLibraryDeveloperMode) discord.Logger.LogInformation("Found field {field} on {object}", ap.Key, obj.GetType().Name); @@ -107,13 +109,12 @@ private static T DeserializeObjectInternal(string json, BaseDiscordClient? di } if (!discord.Configuration.EnableSentry || sentryFields.Count == 0) return obj; + var sentryJson = JsonConvert.SerializeObject(sentryFields); sentryMessage += "\n\nNew fields: " + sentryJson; SentryEvent sentryEvent = new() { - Level = SentryLevel.Warning, - Logger = nameof(DiscordJson), - Message = sentryMessage + Level = SentryLevel.Warning, Logger = nameof(DiscordJson), Message = sentryMessage }; sentryEvent.SetFingerprint("{{ default }}", "{{ module }}", sentryJson.GetHashCode().ToString()); sentryEvent.SetExtra("Found Fields", sentryJson); @@ -124,8 +125,12 @@ private static T DeserializeObjectInternal(string json, BaseDiscordClient? di Username = discord.CurrentUser.UsernameWithDiscriminator, Other = new Dictionary() { - { "developer", discord.Configuration.DeveloperUserId?.ToString() ?? "not_given" }, - { "email", discord.Configuration.FeedbackEmail ?? "not_given" } + { + "developer", discord.Configuration.DeveloperUserId?.ToString() ?? "not_given" + }, + { + "email", discord.Configuration.FeedbackEmail ?? "not_given" + } } }; var sid = discord.Sentry.CaptureEvent(sentryEvent); @@ -144,10 +149,12 @@ private static T DeserializeIEnumerableObjectInternal(string json, BaseDiscor })!; if (discord == null) return obj; + foreach (var ob in obj) ob.Discord = discord; if (!discord.Configuration.ReportMissingFields || !obj.Any(x => x.AdditionalProperties.Any())) return obj; + var first = obj.First(); var sentryMessage = "Found missing properties in api response for " + first.GetType().Name; List sentryFields = new(); @@ -158,13 +165,12 @@ private static T DeserializeIEnumerableObjectInternal(string json, BaseDiscor if (first.IgnoredJsonKeys.Count == 0 || !first.IgnoredJsonKeys.Any(x => x == ap.Key)) { if (vals == 1) - { if (discord.Configuration.EnableLibraryDeveloperMode) { discord.Logger.LogInformation("{sentry}", sentryMessage); discord.Logger.LogDebug("{json}", json); } - } + sentryFields.Add(ap.Key); if (discord.Configuration.EnableLibraryDeveloperMode) discord.Logger.LogInformation("Found field {field} on {object}", ap.Key, first.GetType().Name); @@ -172,13 +178,12 @@ private static T DeserializeIEnumerableObjectInternal(string json, BaseDiscor } if (!discord.Configuration.EnableSentry || sentryFields.Count == 0) return obj; + var sentryJson = JsonConvert.SerializeObject(sentryFields); sentryMessage += "\n\nNew fields: " + sentryJson; SentryEvent sentryEvent = new() { - Level = SentryLevel.Warning, - Logger = nameof(DiscordJson), - Message = sentryMessage + Level = SentryLevel.Warning, Logger = nameof(DiscordJson), Message = sentryMessage }; sentryEvent.SetFingerprint("{{ default }}", "{{ module }}", sentryJson.GetHashCode().ToString()); sentryEvent.SetExtra("Found Fields", sentryJson); @@ -189,8 +194,12 @@ private static T DeserializeIEnumerableObjectInternal(string json, BaseDiscor Username = discord.CurrentUser.UsernameWithDiscriminator, Other = new Dictionary() { - { "developer", discord.Configuration.DeveloperUserId?.ToString() ?? "not_given" }, - { "email", discord.Configuration.FeedbackEmail ?? "not_given" } + { + "developer", discord.Configuration.DeveloperUserId?.ToString() ?? "not_given" + }, + { + "email", discord.Configuration.FeedbackEmail ?? "not_given" + } } }; var sid = discord.Sentry.CaptureEvent(sentryEvent);