add json diagnostics + temp fix for clan and primary_guild

This commit is contained in:
yuzu 2025-05-15 19:18:27 -05:00
parent e99f3d65fa
commit 07370abeae
3 changed files with 31 additions and 46 deletions

View File

@ -253,7 +253,11 @@ fn readMessage(self: *Self, _: anytype) !void {
// must allocate to avoid race conditions
const payload = try self.allocator.create(std.json.Value);
const raw = try std.json.parseFromSlice(GatewayPayloadType, self.allocator, decompressed, .{
// needed for diagnostics
var scanner = json.Scanner.initCompleteInput(self.allocator, decompressed);
errdefer scanner.deinit();
const raw = try std.json.parseFromTokenSource(GatewayPayloadType, self.allocator, &scanner, .{
.ignore_unknown_fields = true,
.max_value_len = 0x1000,
});
@ -273,7 +277,7 @@ fn readMessage(self: *Self, _: anytype) !void {
// run thread pool
if (self.sharder_pool) |sharder_pool| {
try sharder_pool.spawn(handleEventNoError, .{ self, name, payload });
try sharder_pool.spawn(handleEventNoError, .{ self, name, payload, &scanner });
} else try self.handleEvent(name, payload.*);
}
},
@ -431,12 +435,29 @@ pub fn send(self: *Self, _: bool, data: anytype) SendError!void {
try self.client.write(try string.toOwnedSlice());
}
pub fn handleEventNoError(self: *Self, name: []const u8, payload_ptr: *json.Value) void {
pub fn handleEventNoError(self: *Self, name: []const u8, payload_ptr: *json.Value, scanner: *json.Scanner) void {
var diagnostics = json.Diagnostics{};
scanner.enableDiagnostics(&diagnostics);
// log to make sure this executes
self.logif("Shard {d} dispatching {s}\n", .{self.id, name});
self.logif("Shard {d} dispatching {s}", .{self.id, name});
const stdout = std.io.getStdOut().writer();
self.handleEvent(name, payload_ptr.*) catch |err| {
self.logif("Shard {d} error: {s}\n", .{self.id, @errorName(err)});
self.logif(
\\Shard {d} error: {s}
\\on column {d} and line {d} offset {d}
, .{
self.id,
@errorName(err),
diagnostics.getColumn(),
diagnostics.getLine(),
diagnostics.getByteOffset(),
});
std.json.stringify(payload_ptr, .{
.whitespace = .indent_4
}, stdout) catch {};
};
}

View File

@ -41,20 +41,11 @@
id: Snowflake,
/// id of the channel the message was sent in
channel_id: Snowflake,
///
/// id of the guild the message was sent in
/// Note: For MESSAGE_CREATE and MESSAGE_UPDATE events, the message object may not contain a guild_id or member field since the events are sent directly to the receiving user and the bot who sent the message, rather than being sent through the guild like non-ephemeral messages.,
///
guild_id: ?Snowflake = null,
///
/// The author of this message (not guaranteed to be a valid user)
/// Note: The author object follows the structure of the user object, but is only a valid user in the case where the message is generated by a user or bot user. If the message is generated by a webhook, the author object corresponds to the webhook's id, username, and avatar. You can tell if a message is generated by a webhook by checking for the webhook_id on the message object.,
///
/// The author of this message
author: User,
///
/// Member properties for this message's author
/// Note: The member object exists in `MESSAGE_CREATE` and `MESSAGE_UPDATE` events from text-based guild channels. This allows bots to obtain real-time member data without requiring bots to store member state in memory.,
///
member: ?Member = null,
/// Contents of the message
content: ?[]const u8 = null,
@ -66,17 +57,11 @@
tts: bool,
/// Whether this message mentions everyone
mention_everyone: bool,
///
/// Users specifically mentioned in the message
/// Note: The user objects in the mentions array will only have the partial member field present in `MESSAGE_CREATE` and `MESSAGE_UPDATE` events from text-based guild channels.,
///
mentions: []User,
/// Roles specifically mentioned in this message
mention_roles: ?[][]const u8 = null,
///
/// Channels specifically mentioned in this message
/// Note: Not all channel mentions in a message will appear in `mention_channels`. Only textual channels that are visible to everyone in a discoverable guild will ever be included. Only crossposted messages (via Channel Following) currently include `mention_channels` at all. If no mentions in the message meet these requirements, this field will not be sent.,
///
mention_channels: ?[]ChannelMention = null,
/// Any attached files
attachments: []Attachment,
@ -84,8 +69,6 @@
embeds: []Embed,
/// Reactions to the message
reactions: ?[]Reaction = null,
// Used for validating a message was sent
// nonce: ?union(enum) {int: isize,string: []const u8,} = null,
/// Whether this message is pinned
pinned: bool,
/// If the message is generated by a webhook, this is the webhook's id
@ -96,39 +79,21 @@
activity: ?MessageActivity = null,
/// Sent with Rich Presence-related chat embeds
application: ?Partial(Application) = null,
/// if the message is an Interaction or application-owned webhook, this is the id of the application
/// Application id if the message is an Interaction or application-owned webhook
application_id: ?Snowflake = null,
// Data showing the source of a crosspost, channel follow add, pin, or reply message
// message_reference: ?Omit(MessageReference, .{"failIfNotExists"}) = null,
/// Message flags combined as a bitfield
flags: ?MessageFlags = null,
///
/// The stickers sent with the message (bots currently can only receive messages with stickers, not send)
/// @deprecated
///
/// The stickers sent with the message
stickers: ?[]Sticker = null,
///
/// The message associated with the `message_reference`
/// Note: This field is only returned for messages with a `type` of `19` (REPLY). If the message is a reply but the `referenced_message` field is not present, the backend did not attempt to fetch the message that was being replied to, so its state is unknown. If the field exists but is null, the referenced message was deleted.,
/// TAKES A POINTER
/// The referenced message (for replies)
referenced_message: ?*Message = null,
/// The message associated with the `message_reference`. This is a minimal subset of fields in a message (e.g. `author` is excluded.)
message_snapshots: ?[]MessageSnapshot = null,
/// sent if the message is sent as a result of an interaction
interaction_metadata: ?MessageInteractionMetadata = null,
///
/// Sent if the message is a response to an Interaction
///
/// @deprecated Deprecated in favor of {@link interaction_metadata};
///
interaction: ?MessageInteraction = null,
// The thread that was started from this message, includes thread member object
// thread: ?Omit(Channel, .{"member"}), //& { member: ThreadMember }; = null,
/// The components related to this message
components: ?[]MessageComponent = null,
/// Sent if the message contains stickers
sticker_items: ?[]StickerItem = null,
/// A generally increasing integer (there may be gaps or duplicates) that represents the approximate position of the message in a thread, it can be used to estimate the relative position of the message in a thread in company with `total_message_sent` on parent thread
/// Approximate position of the message in a thread
position: ?isize = null,
/// The poll object
poll: ?Poll = null,

View File

@ -58,7 +58,6 @@
banner: ?[]const u8 = null,
/// data for the user's avatar decoration
avatar_decoration_data: ?AvatarDecorationData = null,
clan: ?[]const u8 = null,
};
/// https://discord.com/developers/docs/resources/user#avatar-decoration-data-object