Skip to content

Commit

Permalink
aaaaaaaaaaa
Browse files Browse the repository at this point in the history
  • Loading branch information
Lulalaby committed Oct 17, 2023
1 parent 95b19c8 commit 22ff750
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 16 deletions.
22 changes: 21 additions & 1 deletion DisCatSharp/Entities/Guild/AuditLog/DiscordAuditEntryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,21 @@ namespace DisCatSharp.Entities;
/// <summary>
/// Represents additional information about the <see cref="DiscordAuditLogEntry"/>.
/// </summary>
public sealed class DiscordAuditEntryInfo
public sealed class DiscordAuditEntryInfo : ObservableApiObject
{
/// <summary>
/// Gets or sets the guild in which the entities were targeted.
/// </summary>
[JsonIgnore]
internal DiscordGuild Guild
=> (this.Discord.Guilds.TryGetValue(this.GuildId, out var guild) ? guild : null!)!;

/// <summary>
/// Gets or sets the ID of the guild in which the entities were targeted.
/// </summary>
[JsonIgnore]
internal ulong GuildId { get; set; }

/// <summary>
/// Gets or sets the ID of the app whose permissions were targeted.
/// Event Type: <see cref="AuditLogActionType.ApplicationCommandPermissionUpdate"/>.
Expand Down Expand Up @@ -37,6 +50,13 @@ public sealed class DiscordAuditEntryInfo
[JsonProperty("channel_id", NullValueHandling = NullValueHandling.Ignore)]
public ulong? ChannelId { get; internal set; } = null;

/// <summary>
/// Gets the channel in which the entities were targeted.
/// </summary>
[JsonIgnore]
public DiscordChannel? Channel
=> this.ChannelId.HasValue ? this.Guild.GetChannel(this.ChannelId.Value) : null;

/// <summary>
/// Gets or sets the number of entities that were targeted.
/// Event Types: <see cref="AuditLogActionType.MessageDelete"/>, <see cref="AuditLogActionType.MessageBulkDelete"/>, <see cref="AuditLogActionType.MemberDisconnect"/>, <see cref="AuditLogActionType.MemberMove"/>
Expand Down
6 changes: 6 additions & 0 deletions DisCatSharp/Entities/Guild/AuditLog/DiscordAuditLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ namespace DisCatSharp.Entities;
/// </summary>
public sealed class DiscordAuditLog : ObservableApiObject
{
/// <summary>
/// Gets or sets the ID of the guild in which the entities were targeted.
/// </summary>
[JsonIgnore]
internal ulong GuildId { get; set; }

/// <summary>
/// List of application commands referenced in the <see cref="DiscordAuditLog"/>.
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions DisCatSharp/Entities/Guild/AuditLog/DiscordAuditLogEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// Gets the type of action that occurred in the audit log entry.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions DisCatSharp/Entities/Guild/DiscordGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2306,6 +2306,7 @@ public async Task<IReadOnlyList<DiscordAuditLog>> GetAuditLogsAsync(int? limit =
last = alr.Entries[^1].Id;
alrs.Add(alr);
}

return alrs;
}

Expand Down
10 changes: 10 additions & 0 deletions DisCatSharp/Net/Rest/DiscordApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,16 @@ internal async Task<DiscordAuditLog> GetAuditLogsAsync(ulong guildId, int limit,
var res = await this.DoRequestAsync(this.Discord, bucket, url, RestRequestMethod.GET, route).ConfigureAwait(false);

var auditLogData = DiscordJson.DeserializeObject<DiscordAuditLog>(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;
}
Expand Down
39 changes: 24 additions & 15 deletions DisCatSharp/Net/Serialization/DiscordJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -81,39 +82,39 @@ private static T DeserializeObjectInternal<T>(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<string> 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);
}
}

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);
Expand All @@ -124,8 +125,12 @@ private static T DeserializeObjectInternal<T>(string json, BaseDiscordClient? di
Username = discord.CurrentUser.UsernameWithDiscriminator,
Other = new Dictionary<string, string>()
{
{ "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);
Expand All @@ -144,10 +149,12 @@ private static T DeserializeIEnumerableObjectInternal<T>(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<string> sentryFields = new();
Expand All @@ -158,27 +165,25 @@ private static T DeserializeIEnumerableObjectInternal<T>(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);
}
}

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);
Expand All @@ -189,8 +194,12 @@ private static T DeserializeIEnumerableObjectInternal<T>(string json, BaseDiscor
Username = discord.CurrentUser.UsernameWithDiscriminator,
Other = new Dictionary<string, string>()
{
{ "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);
Expand Down

0 comments on commit 22ff750

Please sign in to comment.