add snowflake type
This commit is contained in:
parent
0303579074
commit
8c6a7f3919
@ -2,12 +2,13 @@ const zmpl = @import("zmpl");
|
|||||||
const Discord = @import("types.zig");
|
const Discord = @import("types.zig");
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const mem = std.mem;
|
const mem = std.mem;
|
||||||
|
const Snowflake = @import("shared.zig").Snowflake;
|
||||||
|
|
||||||
pub fn parseUser(_: mem.Allocator, obj: *zmpl.Data.Object) !Discord.User {
|
pub fn parseUser(_: mem.Allocator, obj: *zmpl.Data.Object) !Discord.User {
|
||||||
const avatar_decoration_data_obj = obj.getT(.object, "avatar_decoration_data");
|
const avatar_decoration_data_obj = obj.getT(.object, "avatar_decoration_data");
|
||||||
const user = Discord.User{
|
const user = Discord.User{
|
||||||
.clan = null,
|
.clan = null,
|
||||||
.id = obj.getT(.string, "id").?,
|
.id = try Snowflake.fromRaw(obj.getT(.string, "id").?),
|
||||||
.bot = obj.getT(.boolean, "bot") orelse false,
|
.bot = obj.getT(.boolean, "bot") orelse false,
|
||||||
.username = obj.getT(.string, "username").?,
|
.username = obj.getT(.string, "username").?,
|
||||||
.accent_color = if (obj.getT(.integer, "accent_color")) |ac| @as(isize, @intCast(ac)) else null,
|
.accent_color = if (obj.getT(.integer, "accent_color")) |ac| @as(isize, @intCast(ac)) else null,
|
||||||
@ -27,7 +28,7 @@ pub fn parseUser(_: mem.Allocator, obj: *zmpl.Data.Object) !Discord.User {
|
|||||||
.discriminator = obj.getT(.string, "discriminator").?,
|
.discriminator = obj.getT(.string, "discriminator").?,
|
||||||
.avatar_decoration_data = if (avatar_decoration_data_obj) |add| Discord.AvatarDecorationData{
|
.avatar_decoration_data = if (avatar_decoration_data_obj) |add| Discord.AvatarDecorationData{
|
||||||
.asset = add.getT(.string, "asset").?,
|
.asset = add.getT(.string, "asset").?,
|
||||||
.sku_id = add.getT(.string, "sku_id").?,
|
.sku_id = try Snowflake.fromRaw(add.getT(.string, "sku_id").?),
|
||||||
} else null,
|
} else null,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -51,7 +52,7 @@ pub fn parseMember(_: mem.Allocator, obj: *zmpl.Data.Object) !Discord.Member {
|
|||||||
.flags = @as(isize, @intCast(obj.getT(.integer, "flags").?)),
|
.flags = @as(isize, @intCast(obj.getT(.integer, "flags").?)),
|
||||||
.avatar_decoration_data = if (avatar_decoration_data_member_obj) |addm| Discord.AvatarDecorationData{
|
.avatar_decoration_data = if (avatar_decoration_data_member_obj) |addm| Discord.AvatarDecorationData{
|
||||||
.asset = addm.getT(.string, "asset").?,
|
.asset = addm.getT(.string, "asset").?,
|
||||||
.sku_id = addm.getT(.string, "sku_id").?,
|
.sku_id = try Snowflake.fromRaw(addm.getT(.string, "sku_id").?),
|
||||||
} else null,
|
} else null,
|
||||||
};
|
};
|
||||||
return member;
|
return member;
|
||||||
@ -90,17 +91,17 @@ pub fn parseMessage(allocator: mem.Allocator, obj: *zmpl.Data.Object) !Discord.M
|
|||||||
// parse message
|
// parse message
|
||||||
const message = Discord.Message{
|
const message = Discord.Message{
|
||||||
// the id
|
// the id
|
||||||
.id = obj.getT(.string, "id").?,
|
.id = try Snowflake.fromRaw(obj.getT(.string, "id").?),
|
||||||
.tts = obj.getT(.boolean, "tts").?,
|
.tts = obj.getT(.boolean, "tts").?,
|
||||||
.mention_everyone = obj.getT(.boolean, "mention_everyone").?,
|
.mention_everyone = obj.getT(.boolean, "mention_everyone").?,
|
||||||
.pinned = obj.getT(.boolean, "pinned").?,
|
.pinned = obj.getT(.boolean, "pinned").?,
|
||||||
.type = @as(Discord.MessageTypes, @enumFromInt(obj.getT(.integer, "type").?)),
|
.type = @as(Discord.MessageTypes, @enumFromInt(obj.getT(.integer, "type").?)),
|
||||||
.channel_id = obj.getT(.string, "channel_id").?,
|
.channel_id = try Snowflake.fromRaw(obj.getT(.string, "channel_id").?),
|
||||||
.author = author,
|
.author = author,
|
||||||
.member = member,
|
.member = member,
|
||||||
.content = obj.getT(.string, "content"),
|
.content = obj.getT(.string, "content"),
|
||||||
.timestamp = obj.getT(.string, "timestamp").?,
|
.timestamp = obj.getT(.string, "timestamp").?,
|
||||||
.guild_id = obj.getT(.string, "guild_id"),
|
.guild_id = try Snowflake.fromMaybe(obj.getT(.string, "guild_id")),
|
||||||
.attachments = &[0]Discord.Attachment{},
|
.attachments = &[0]Discord.Attachment{},
|
||||||
.edited_timestamp = null,
|
.edited_timestamp = null,
|
||||||
.mentions = try mentions.toOwnedSlice(),
|
.mentions = try mentions.toOwnedSlice(),
|
||||||
@ -109,10 +110,10 @@ pub fn parseMessage(allocator: mem.Allocator, obj: *zmpl.Data.Object) !Discord.M
|
|||||||
.embeds = &[0]Discord.Embed{},
|
.embeds = &[0]Discord.Embed{},
|
||||||
.reactions = &[0]?Discord.Reaction{},
|
.reactions = &[0]?Discord.Reaction{},
|
||||||
.nonce = .{ .string = obj.getT(.string, "nonce").? },
|
.nonce = .{ .string = obj.getT(.string, "nonce").? },
|
||||||
.webhook_id = obj.getT(.string, "webhook_id"),
|
.webhook_id = try Snowflake.fromMaybe(obj.getT(.string, "webhook_id")),
|
||||||
.activity = null,
|
.activity = null,
|
||||||
.application = null,
|
.application = null,
|
||||||
.application_id = obj.getT(.string, "application_id"),
|
.application_id = try Snowflake.fromMaybe(obj.getT(.string, "application_id")),
|
||||||
.message_reference = null,
|
.message_reference = null,
|
||||||
.flags = if (obj.getT(.integer, "flags")) |fs| @as(Discord.MessageFlags, @bitCast(@as(u15, @intCast(fs)))) else null,
|
.flags = if (obj.getT(.integer, "flags")) |fs| @as(Discord.MessageFlags, @bitCast(@as(u15, @intCast(fs)))) else null,
|
||||||
.stickers = &[0]?Discord.Sticker{},
|
.stickers = &[0]?Discord.Sticker{},
|
||||||
|
@ -541,7 +541,7 @@ pub fn handleEvent(self: *Self, name: []const u8, payload: []const u8) !void {
|
|||||||
.redirect_uris = null,
|
.redirect_uris = null,
|
||||||
.interactions_endpoint_url = null,
|
.interactions_endpoint_url = null,
|
||||||
.flags = @as(Discord.ApplicationFlags, @bitCast(@as(u25, @intCast(application.getT(.integer, "flags").?)))),
|
.flags = @as(Discord.ApplicationFlags, @bitCast(@as(u25, @intCast(application.getT(.integer, "flags").?)))),
|
||||||
.id = application.getT(.string, "id").?,
|
.id = try Shared.Snowflake.fromRaw(application.getT(.string, "id").?),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -562,9 +562,9 @@ pub fn handleEvent(self: *Self, name: []const u8, payload: []const u8) !void {
|
|||||||
|
|
||||||
const obj = attempt.getT(.object, "d").?;
|
const obj = attempt.getT(.object, "d").?;
|
||||||
const data = Discord.MessageDelete{
|
const data = Discord.MessageDelete{
|
||||||
.id = obj.getT(.string, "id").?,
|
.id = try Shared.Snowflake.fromRaw(obj.getT(.string, "id").?),
|
||||||
.channel_id = obj.getT(.string, "channel_id").?,
|
.channel_id = try Shared.Snowflake.fromRaw(obj.getT(.string, "channel_id").?),
|
||||||
.guild_id = obj.getT(.string, "guild_id"),
|
.guild_id = try Shared.Snowflake.fromMaybe(obj.getT(.string, "guild_id")),
|
||||||
};
|
};
|
||||||
|
|
||||||
if (self.handler.message_delete) |event| event(data);
|
if (self.handler.message_delete) |event| event(data);
|
||||||
@ -582,9 +582,9 @@ pub fn handleEvent(self: *Self, name: []const u8, payload: []const u8) !void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const data = Discord.MessageDeleteBulk{
|
const data = Discord.MessageDeleteBulk{
|
||||||
.ids = ids.items,
|
.ids = try Shared.Snowflake.fromMany(try ids.toOwnedSlice()),
|
||||||
.channel_id = obj.getT(.string, "channel_id").?,
|
.channel_id = try Shared.Snowflake.fromRaw(obj.getT(.string, "channel_id").?),
|
||||||
.guild_id = obj.getT(.string, "guild_id"),
|
.guild_id = try Shared.Snowflake.fromMaybe(obj.getT(.string, "guild_id")),
|
||||||
};
|
};
|
||||||
|
|
||||||
if (self.handler.message_delete_bulk) |event| event(data);
|
if (self.handler.message_delete_bulk) |event| event(data);
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
|
||||||
pub const IdentifyProperties = struct {
|
pub const IdentifyProperties = struct {
|
||||||
///
|
///
|
||||||
/// Operating system the shard runs on.
|
/// Operating system the shard runs on.
|
||||||
@ -47,3 +49,35 @@ pub const GatewayBotInfo = struct {
|
|||||||
///
|
///
|
||||||
session_start_limit: ?GatewaySessionStartLimit,
|
session_start_limit: ?GatewaySessionStartLimit,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub const Snowflake = struct {
|
||||||
|
id: u64,
|
||||||
|
|
||||||
|
pub fn fromMaybe(raw: ?[]const u8) !?Snowflake {
|
||||||
|
if (raw) |id| {
|
||||||
|
return .{
|
||||||
|
.id = try std.fmt.parseInt(u64, id, 10),
|
||||||
|
};
|
||||||
|
} else return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn fromRaw(raw: []const u8) !Snowflake {
|
||||||
|
return .{
|
||||||
|
.id = try std.fmt.parseInt(u64, raw, 10),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn fromMany(many: [][]const u8) ![]Snowflake {
|
||||||
|
var array = try std.BoundedArray(Snowflake, 64).init(many.len);
|
||||||
|
|
||||||
|
for (many) |id| {
|
||||||
|
try array.append(try Snowflake.fromRaw(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
return array.slice();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn value(self: *Snowflake) u64 {
|
||||||
|
return self.value;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
483
src/types.zig
483
src/types.zig
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user