forked from yuzucchii/discord.zig
breaking changes
This commit is contained in:
parent
b8c42bb22c
commit
32d9688234
@ -17,7 +17,7 @@ pub fn build(b: *std.Build) void {
|
||||
const zlib = b.dependency("zlib", .{});
|
||||
|
||||
const dzig = b.addModule("discord.zig", .{
|
||||
.root_source_file = b.path("src/lib.zig"),
|
||||
.root_source_file = b.path("src/root.zig"),
|
||||
.link_libc = true,
|
||||
});
|
||||
|
||||
|
@ -11,7 +11,7 @@
|
||||
|
||||
// This is a [Semantic Version](https://semver.org/).
|
||||
// In a future version of Zig it will be used for package deduplication.
|
||||
.version = "0.0.0",
|
||||
.version = "1.0.0",
|
||||
|
||||
// This field is optional.
|
||||
// This is currently advisory only; Zig does not yet do anything
|
||||
@ -25,8 +25,8 @@
|
||||
// internet connectivity.
|
||||
.dependencies = .{
|
||||
.zlib = .{
|
||||
.url = "https://github.com/yuzudev/zig-zlib/archive/refs/heads/main.zip",
|
||||
.hash = "zlib-0.1.0-AAAAAKm6QADNBB6NBPHanW9G0EOPTmgJsRO6NFT__arp",
|
||||
.url = "https://git.yuzucchii.xyz/yuzucchii/zlib/archive/master.tar.gz",
|
||||
.hash = "zlib-0.1.0-AAAAALW6QABbti7dQfKuK0IQb-xdcp3SI8zdTvs5ouUD",
|
||||
},
|
||||
.websocket = .{ .url = "https://github.com/karlseguin/websocket.zig/archive/refs/heads/master.zip", .hash = "websocket-0.1.0-ZPISdYBIAwB1yO6AFDHRHLaZSmpdh4Bz4dCmaQUqNNWh" },
|
||||
},
|
||||
|
2071
src/http/api.zig
2071
src/http/api.zig
File diff suppressed because it is too large
Load Diff
@ -310,7 +310,7 @@ pub const CacheLike = @import("cache/cache.zig").CacheLike;
|
||||
pub const DefaultCache = @import("cache/cache.zig").DefaultCache;
|
||||
|
||||
pub const Permissions = @import("utils/permissions.zig").Permissions;
|
||||
pub const Shard = @import("shard/shard.zig").Shard;
|
||||
pub const Shard = @import("shard/shard.zig");
|
||||
pub const zjson = @compileError("Deprecated.");
|
||||
|
||||
pub const Internal = @import("utils/core.zig");
|
||||
@ -319,12 +319,13 @@ const GatewayBotInfo = @import("shard/util.zig").GatewayBotInfo;
|
||||
const Log = Internal.Log;
|
||||
|
||||
// sharder
|
||||
pub const Sharder = @import("shard/sharder.zig").ShardManager;
|
||||
pub const Sharder = @import("shard/sharder.zig");
|
||||
|
||||
pub const cache = @import("cache/cache.zig");
|
||||
|
||||
pub const FetchReq = @import("http/http.zig").FetchReq;
|
||||
pub const FileData = @import("http/http.zig").FileData;
|
||||
pub const API = @import("http/api.zig");
|
||||
|
||||
const std = @import("std");
|
||||
const mem = std.mem;
|
||||
@ -336,14 +337,20 @@ pub fn CustomisedSession(comptime Table: cache.TableTemplate) type {
|
||||
const Self = @This();
|
||||
|
||||
allocator: mem.Allocator,
|
||||
sharder: Sharder(Table),
|
||||
token: []const u8,
|
||||
sharder: Sharder,
|
||||
authorization: []const u8,
|
||||
cache: cache.CacheTables(Table),
|
||||
|
||||
// there is only 1 api, therefore we don't need pointers
|
||||
api: API,
|
||||
|
||||
pub fn init(allocator: mem.Allocator) Self {
|
||||
return .{
|
||||
.allocator = allocator,
|
||||
.sharder = undefined,
|
||||
.token = undefined,
|
||||
.authorization = undefined,
|
||||
.api = undefined,
|
||||
.cache = .defaults(allocator),
|
||||
};
|
||||
}
|
||||
|
||||
@ -352,7 +359,7 @@ pub fn CustomisedSession(comptime Table: cache.TableTemplate) type {
|
||||
}
|
||||
|
||||
pub fn start(self: *Self, settings: struct {
|
||||
token: []const u8,
|
||||
authorization: []const u8,
|
||||
intents: Intents,
|
||||
options: struct {
|
||||
spawn_shard_delay: u64 = 5300,
|
||||
@ -362,17 +369,20 @@ pub fn CustomisedSession(comptime Table: cache.TableTemplate) type {
|
||||
},
|
||||
run: GatewayDispatchEvent,
|
||||
log: Log,
|
||||
cache: cache.TableTemplate,
|
||||
cache: ?cache.CacheTables(Table),
|
||||
}) !void {
|
||||
if (!std.mem.startsWith(u8, settings.token, "Bot")) {
|
||||
if (!std.mem.startsWith(u8, settings.authorization, "Bot")) {
|
||||
var buffer = [_]u8{undefined} ** 128;
|
||||
const printed = try std.fmt.bufPrint(&buffer, "Bot {s}", .{settings.token});
|
||||
self.token = printed;
|
||||
const printed = try std.fmt.bufPrint(&buffer, "Bot {s}", .{settings.authorization});
|
||||
self.authorization = printed;
|
||||
} else {
|
||||
self.token = settings.token;
|
||||
self.authorization = settings.authorization;
|
||||
}
|
||||
|
||||
var req = FetchReq.init(self.allocator, self.token);
|
||||
self.api = API.init(self.allocator, self.authorization);
|
||||
self.cache = settings.cache orelse .defaults(self.allocator);
|
||||
|
||||
var req = FetchReq.init(self.allocator, self.authorization);
|
||||
defer req.deinit();
|
||||
|
||||
const res = try req.makeRequest(.GET, "/gateway/bot", null);
|
||||
@ -387,11 +397,11 @@ pub fn CustomisedSession(comptime Table: cache.TableTemplate) type {
|
||||
const parsed = try json.parseFromSlice(GatewayBotInfo, self.allocator, body, .{});
|
||||
defer parsed.deinit();
|
||||
|
||||
self.sharder = try Sharder(Table).init(self.allocator, .{
|
||||
.token = self.token,
|
||||
self.sharder = try Sharder.init(self.allocator, .{
|
||||
.authorization = self.authorization,
|
||||
.intents = settings.intents,
|
||||
.run = settings.run,
|
||||
.options = Sharder(Table).SessionOptions{
|
||||
.options = Sharder.SessionOptions{
|
||||
.info = parsed.value,
|
||||
.shard_start = settings.options.shard_start,
|
||||
.shard_end = @intCast(parsed.value.shards),
|
||||
@ -399,7 +409,6 @@ pub fn CustomisedSession(comptime Table: cache.TableTemplate) type {
|
||||
.spawn_shard_delay = settings.options.spawn_shard_delay,
|
||||
},
|
||||
.log = settings.log,
|
||||
.cache = settings.cache,
|
||||
});
|
||||
|
||||
try self.sharder.spawnShards();
|
||||
@ -423,7 +432,7 @@ pub const GatewayIntents = @import("./shard/intents.zig").GatewayIntents;
|
||||
pub const Intents = @import("./shard/intents.zig").Intents;
|
||||
|
||||
pub fn start(self: *Session, settings: struct {
|
||||
token: []const u8,
|
||||
authorization: []const u8,
|
||||
intents: Intents,
|
||||
options: struct {
|
||||
spawn_shard_delay: u64 = 5300,
|
||||
@ -433,7 +442,7 @@ pub fn start(self: *Session, settings: struct {
|
||||
},
|
||||
run: GatewayDispatchEvent,
|
||||
log: Log,
|
||||
cache: cache.TableTemplate,
|
||||
cache: cache.CacheTables(DefaultTable),
|
||||
}) !void {
|
||||
return self.start(settings);
|
||||
}
|
2076
src/shard/shard.zig
2076
src/shard/shard.zig
File diff suppressed because it is too large
Load Diff
@ -23,23 +23,19 @@ const ShardDetails = @import("util.zig").ShardDetails;
|
||||
const ConnectQueue = @import("connect_queue.zig").ConnectQueue;
|
||||
const GatewayDispatchEvent = @import("../utils/core.zig").GatewayDispatchEvent;
|
||||
const Log = @import("../utils/core.zig").Log;
|
||||
const Shard = @import("shard.zig").Shard;
|
||||
const Shard = @import("shard.zig");
|
||||
const std = @import("std");
|
||||
const mem = std.mem;
|
||||
const debug = @import("../utils/core.zig").debug;
|
||||
const TableTemplate = @import("../cache/cache.zig").TableTemplate;
|
||||
const CacheTables = @import("../cache/cache.zig").CacheTables;
|
||||
|
||||
pub fn ShardManager(comptime Table: TableTemplate) type {
|
||||
return struct {
|
||||
const Self = @This();
|
||||
|
||||
shard_details: ShardDetails,
|
||||
allocator: mem.Allocator,
|
||||
|
||||
/// Queue for managing shard connections
|
||||
connect_queue: ConnectQueue(Shard(Table)),
|
||||
shards: std.AutoArrayHashMap(usize, Shard(Table)),
|
||||
connect_queue: ConnectQueue(Shard),
|
||||
shards: std.AutoArrayHashMap(usize, Shard),
|
||||
handler: GatewayDispatchEvent,
|
||||
|
||||
/// where we dispatch work for every thread, threads must be spawned upon shard creation
|
||||
@ -50,9 +46,6 @@ pub fn ShardManager(comptime Table: TableTemplate) type {
|
||||
options: SessionOptions,
|
||||
log: Log,
|
||||
|
||||
// must be initialised
|
||||
cache: *CacheTables(Table),
|
||||
|
||||
pub const ShardData = struct {
|
||||
/// resume seq to resume connections
|
||||
resume_seq: ?usize,
|
||||
@ -83,24 +76,21 @@ pub fn ShardManager(comptime Table: TableTemplate) type {
|
||||
};
|
||||
|
||||
pub fn init(allocator: mem.Allocator, settings: struct {
|
||||
token: []const u8,
|
||||
authorization: []const u8,
|
||||
intents: Intents,
|
||||
options: SessionOptions,
|
||||
run: GatewayDispatchEvent,
|
||||
log: Log,
|
||||
cache: TableTemplate,
|
||||
}) mem.Allocator.Error!Self {
|
||||
const concurrency = settings.options.info.session_start_limit.?.max_concurrency;
|
||||
const cache = try allocator.create(CacheTables(Table));
|
||||
cache.* = CacheTables(Table).defaults(allocator);
|
||||
|
||||
return .{
|
||||
.allocator = allocator,
|
||||
.connect_queue = try ConnectQueue(Shard(Table)).init(allocator, concurrency, 5000),
|
||||
.connect_queue = try ConnectQueue(Shard).init(allocator, concurrency, 5000),
|
||||
.shards = .init(allocator),
|
||||
.workers = undefined,
|
||||
.shard_details = ShardDetails{
|
||||
.token = settings.token,
|
||||
.token = settings.authorization,
|
||||
.intents = settings.intents,
|
||||
},
|
||||
.handler = settings.run,
|
||||
@ -116,7 +106,6 @@ pub fn ShardManager(comptime Table: TableTemplate) type {
|
||||
.workers_per_shard = settings.options.workers_per_shard,
|
||||
},
|
||||
.log = settings.log,
|
||||
.cache = cache,
|
||||
};
|
||||
}
|
||||
|
||||
@ -132,11 +121,11 @@ pub fn ShardManager(comptime Table: TableTemplate) type {
|
||||
return shard.identify(null);
|
||||
}
|
||||
|
||||
pub fn disconnect(self: *Self, shard_id: usize) Shard(Table).CloseError!void {
|
||||
pub fn disconnect(self: *Self, shard_id: usize) Shard.CloseError!void {
|
||||
return if (self.shards.get(shard_id)) |shard| shard.disconnect();
|
||||
}
|
||||
|
||||
pub fn disconnectAll(self: *Self) Shard(Table).CloseError!void {
|
||||
pub fn disconnectAll(self: *Self) Shard.CloseError!void {
|
||||
while (self.shards.iterator().next()) |shard| shard.value_ptr.disconnect();
|
||||
}
|
||||
|
||||
@ -146,7 +135,7 @@ pub fn ShardManager(comptime Table: TableTemplate) type {
|
||||
/// Assign each shard to a bucket
|
||||
/// Return list of buckets
|
||||
/// https://discord.com/developers/docs/events/gateway#sharding-max-concurrency
|
||||
fn spawnBuckets(self: *Self) ![][]Shard(Table) {
|
||||
fn spawnBuckets(self: *Self) ![][]Shard {
|
||||
const concurrency = self.options.info.session_start_limit.?.max_concurrency;
|
||||
|
||||
self.logif("{d}-{d}", .{ self.options.shard_start, self.options.shard_end });
|
||||
@ -156,12 +145,12 @@ pub fn ShardManager(comptime Table: TableTemplate) type {
|
||||
|
||||
self.logif("#0 preparing buckets", .{});
|
||||
|
||||
const buckets = try self.allocator.alloc([]Shard(Table), bucket_count);
|
||||
const buckets = try self.allocator.alloc([]Shard, bucket_count);
|
||||
|
||||
for (buckets, 0..) |*bucket, i| {
|
||||
const bucket_size = if ((i + 1) * concurrency > range) range - (i * concurrency) else concurrency;
|
||||
|
||||
bucket.* = try self.allocator.alloc(Shard(Table), bucket_size);
|
||||
bucket.* = try self.allocator.alloc(Shard, bucket_size);
|
||||
|
||||
for (bucket.*, 0..) |*shard, j| {
|
||||
shard.* = try self.create(self.options.shard_start + i * concurrency + j);
|
||||
@ -181,19 +170,18 @@ pub fn ShardManager(comptime Table: TableTemplate) type {
|
||||
}
|
||||
|
||||
/// creates a shard and stores it
|
||||
fn create(self: *Self, shard_id: usize) !Shard(Table) {
|
||||
fn create(self: *Self, shard_id: usize) !Shard {
|
||||
if (self.shards.get(shard_id)) |s| return s;
|
||||
|
||||
const shard: Shard(Table) = try .init(self.allocator, shard_id, self.options.total_shards, .{
|
||||
const shard: Shard = try .init(self.allocator, shard_id, self.options.total_shards, .{
|
||||
.token = self.shard_details.token,
|
||||
.intents = self.shard_details.intents,
|
||||
.options = Shard(Table).ShardOptions{
|
||||
.options = Shard.ShardOptions{
|
||||
.info = self.options.info,
|
||||
.ratelimit_options = .{},
|
||||
},
|
||||
.run = self.handler,
|
||||
.log = self.log,
|
||||
.cache = self.cache,
|
||||
.sharder_pool = &self.workers,
|
||||
});
|
||||
|
||||
@ -215,7 +203,7 @@ pub fn ShardManager(comptime Table: TableTemplate) type {
|
||||
});
|
||||
}
|
||||
|
||||
fn callback(self: *ConnectQueue(Shard(Table)).RequestWithShard) anyerror!void {
|
||||
fn callback(self: *ConnectQueue(Shard).RequestWithShard) anyerror!void {
|
||||
try self.shard.connect();
|
||||
}
|
||||
|
||||
@ -237,7 +225,7 @@ pub fn ShardManager(comptime Table: TableTemplate) type {
|
||||
//self.startResharder();
|
||||
}
|
||||
|
||||
pub fn send(self: *Self, shard_id: usize, data: anytype) Shard(Table).SendError!void {
|
||||
pub fn send(self: *Self, shard_id: usize, data: anytype) Shard.SendError!void {
|
||||
if (self.shards.get(shard_id)) |shard| try shard.send(data);
|
||||
}
|
||||
|
||||
@ -258,5 +246,3 @@ pub fn ShardManager(comptime Table: TableTemplate) type {
|
||||
inline fn logif(self: *Self, comptime format: []const u8, args: anytype) void {
|
||||
internalLogif(self.log, format, args);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ const Types = @import("../structures/types.zig");
|
||||
|
||||
pub const debug = std.log.scoped(.@"discord.zig");
|
||||
|
||||
const Shard = @import("../shard/shard.zig");
|
||||
pub const Log = union(enum) { yes, no };
|
||||
|
||||
pub inline fn logif(log: Log, comptime format: []const u8, args: anytype) void {
|
||||
@ -29,82 +30,82 @@ pub inline fn logif(log: Log, comptime format: []const u8, args: anytype) void {
|
||||
}
|
||||
|
||||
pub const GatewayDispatchEvent = struct {
|
||||
application_command_permissions_update: ?*const fn (save: *anyopaque, application_command_permissions: Types.ApplicationCommandPermissions) anyerror!void = undefined,
|
||||
auto_moderation_rule_create: ?*const fn (save: *anyopaque, rule: Types.AutoModerationRule) anyerror!void = undefined,
|
||||
auto_moderation_rule_update: ?*const fn (save: *anyopaque, rule: Types.AutoModerationRule) anyerror!void = undefined,
|
||||
auto_moderation_rule_delete: ?*const fn (save: *anyopaque, rule: Types.AutoModerationRule) anyerror!void = undefined,
|
||||
auto_moderation_action_execution: ?*const fn (save: *anyopaque, action_execution: Types.AutoModerationActionExecution) anyerror!void = undefined,
|
||||
application_command_permissions_update: ?*const fn (shard: *Shard, application_command_permissions: Types.ApplicationCommandPermissions) anyerror!void = undefined,
|
||||
auto_moderation_rule_create: ?*const fn (shard: *Shard, rule: Types.AutoModerationRule) anyerror!void = undefined,
|
||||
auto_moderation_rule_update: ?*const fn (shard: *Shard, rule: Types.AutoModerationRule) anyerror!void = undefined,
|
||||
auto_moderation_rule_delete: ?*const fn (shard: *Shard, rule: Types.AutoModerationRule) anyerror!void = undefined,
|
||||
auto_moderation_action_execution: ?*const fn (shard: *Shard, action_execution: Types.AutoModerationActionExecution) anyerror!void = undefined,
|
||||
|
||||
channel_create: ?*const fn (save: *anyopaque, chan: Types.Channel) anyerror!void = undefined,
|
||||
channel_update: ?*const fn (save: *anyopaque, chan: Types.Channel) anyerror!void = undefined,
|
||||
channel_create: ?*const fn (shard: *Shard, chan: Types.Channel) anyerror!void = undefined,
|
||||
channel_update: ?*const fn (shard: *Shard, chan: Types.Channel) anyerror!void = undefined,
|
||||
/// this isn't send when the channel is not relevant to you
|
||||
channel_delete: ?*const fn (save: *anyopaque, chan: Types.Channel) anyerror!void = undefined,
|
||||
channel_pins_update: ?*const fn (save: *anyopaque, chan_pins_update: Types.ChannelPinsUpdate) anyerror!void = undefined,
|
||||
thread_create: ?*const fn (save: *anyopaque, thread: Types.Channel) anyerror!void = undefined,
|
||||
thread_update: ?*const fn (save: *anyopaque, thread: Types.Channel) anyerror!void = undefined,
|
||||
channel_delete: ?*const fn (shard: *Shard, chan: Types.Channel) anyerror!void = undefined,
|
||||
channel_pins_update: ?*const fn (shard: *Shard, chan_pins_update: Types.ChannelPinsUpdate) anyerror!void = undefined,
|
||||
thread_create: ?*const fn (shard: *Shard, thread: Types.Channel) anyerror!void = undefined,
|
||||
thread_update: ?*const fn (shard: *Shard, thread: Types.Channel) anyerror!void = undefined,
|
||||
/// has `id`, `guild_id`, `parent_id`, and `type` fields.
|
||||
thread_delete: ?*const fn (save: *anyopaque, thread: Types.Partial(Types.Channel)) anyerror!void = undefined,
|
||||
thread_list_sync: ?*const fn (save: *anyopaque, data: Types.ThreadListSync) anyerror!void = undefined,
|
||||
thread_member_update: ?*const fn (save: *anyopaque, guild_id: Types.ThreadMemberUpdate) anyerror!void = undefined,
|
||||
thread_members_update: ?*const fn (save: *anyopaque, thread_data: Types.ThreadMembersUpdate) anyerror!void = undefined,
|
||||
thread_delete: ?*const fn (shard: *Shard, thread: Types.Partial(Types.Channel)) anyerror!void = undefined,
|
||||
thread_list_sync: ?*const fn (shard: *Shard, data: Types.ThreadListSync) anyerror!void = undefined,
|
||||
thread_member_update: ?*const fn (shard: *Shard, guild_id: Types.ThreadMemberUpdate) anyerror!void = undefined,
|
||||
thread_members_update: ?*const fn (shard: *Shard, thread_data: Types.ThreadMembersUpdate) anyerror!void = undefined,
|
||||
// TODO: implement // guild_audit_log_entry_create: null = null,
|
||||
guild_create: ?*const fn (save: *anyopaque, guild: Types.Guild) anyerror!void = undefined,
|
||||
guild_create_unavailable: ?*const fn (save: *anyopaque, guild: Types.UnavailableGuild) anyerror!void = undefined,
|
||||
guild_update: ?*const fn (save: *anyopaque, guild: Types.Guild) anyerror!void = undefined,
|
||||
guild_create: ?*const fn (shard: *Shard, guild: Types.Guild) anyerror!void = undefined,
|
||||
guild_create_unavailable: ?*const fn (shard: *Shard, guild: Types.UnavailableGuild) anyerror!void = undefined,
|
||||
guild_update: ?*const fn (shard: *Shard, guild: Types.Guild) anyerror!void = undefined,
|
||||
/// this is not necessarily sent upon deletion of a guild
|
||||
/// but from when a user is *removed* therefrom
|
||||
guild_delete: ?*const fn (save: *anyopaque, guild: Types.UnavailableGuild) anyerror!void = undefined,
|
||||
guild_ban_add: ?*const fn (save: *anyopaque, gba: Types.GuildBanAddRemove) anyerror!void = undefined,
|
||||
guild_ban_remove: ?*const fn (save: *anyopaque, gbr: Types.GuildBanAddRemove) anyerror!void = undefined,
|
||||
guild_emojis_update: ?*const fn (save: *anyopaque, fields: Types.GuildEmojisUpdate) anyerror!void = undefined,
|
||||
guild_stickers_update: ?*const fn (save: *anyopaque, fields: Types.GuildStickersUpdate) anyerror!void = undefined,
|
||||
guild_integrations_update: ?*const fn (save: *anyopaque, fields: Types.GuildIntegrationsUpdate) anyerror!void = undefined,
|
||||
guild_member_add: ?*const fn (save: *anyopaque, guild_id: Types.GuildMemberAdd) anyerror!void = undefined,
|
||||
guild_member_update: ?*const fn (save: *anyopaque, fields: Types.GuildMemberUpdate) anyerror!void = undefined,
|
||||
guild_member_remove: ?*const fn (save: *anyopaque, user: Types.GuildMemberRemove) anyerror!void = undefined,
|
||||
guild_members_chunk: ?*const fn (save: *anyopaque, data: Types.GuildMembersChunk) anyerror!void = undefined,
|
||||
guild_role_create: ?*const fn (save: *anyopaque, role: Types.GuildRoleCreate) anyerror!void = undefined,
|
||||
guild_role_delete: ?*const fn (save: *anyopaque, role: Types.GuildRoleDelete) anyerror!void = undefined,
|
||||
guild_role_update: ?*const fn (save: *anyopaque, role: Types.GuildRoleUpdate) anyerror!void = undefined,
|
||||
guild_scheduled_event_create: ?*const fn (save: *anyopaque, s_event: Types.ScheduledEvent) anyerror!void = undefined,
|
||||
guild_scheduled_event_update: ?*const fn (save: *anyopaque, s_event: Types.ScheduledEvent) anyerror!void = undefined,
|
||||
guild_scheduled_event_delete: ?*const fn (save: *anyopaque, s_event: Types.ScheduledEvent) anyerror!void = undefined,
|
||||
guild_scheduled_event_user_add: ?*const fn (save: *anyopaque, data: Types.ScheduledEventUserAdd) anyerror!void = undefined,
|
||||
guild_scheduled_event_user_remove: ?*const fn (save: *anyopaque, data: Types.ScheduledEventUserRemove) anyerror!void = undefined,
|
||||
integration_create: ?*const fn (save: *anyopaque, guild_id: Types.IntegrationCreateUpdate) anyerror!void = undefined,
|
||||
integration_update: ?*const fn (save: *anyopaque, guild_id: Types.IntegrationCreateUpdate) anyerror!void = undefined,
|
||||
integration_delete: ?*const fn (save: *anyopaque, guild_id: Types.IntegrationDelete) anyerror!void = undefined,
|
||||
interaction_create: ?*const fn (save: *anyopaque, interaction: Types.MessageInteraction) anyerror!void = undefined,
|
||||
invite_create: ?*const fn (save: *anyopaque, data: Types.InviteCreate) anyerror!void = undefined,
|
||||
invite_delete: ?*const fn (save: *anyopaque, data: Types.InviteDelete) anyerror!void = undefined,
|
||||
message_create: ?*const fn (save: *anyopaque, message: Types.Message) anyerror!void = undefined,
|
||||
message_update: ?*const fn (save: *anyopaque, message: Types.Message) anyerror!void = undefined,
|
||||
message_delete: ?*const fn (save: *anyopaque, log: Types.MessageDelete) anyerror!void = undefined,
|
||||
message_delete_bulk: ?*const fn (save: *anyopaque, log: Types.MessageDeleteBulk) anyerror!void = undefined,
|
||||
message_reaction_add: ?*const fn (save: *anyopaque, log: Types.MessageReactionAdd) anyerror!void = undefined,
|
||||
message_reaction_remove_all: ?*const fn (save: *anyopaque, data: Types.MessageReactionRemoveAll) anyerror!void = undefined,
|
||||
message_reaction_remove: ?*const fn (save: *anyopaque, data: Types.MessageReactionRemove) anyerror!void = undefined,
|
||||
message_reaction_remove_emoji: ?*const fn (save: *anyopaque, data: Types.MessageReactionRemoveEmoji) anyerror!void = undefined,
|
||||
presence_update: ?*const fn (save: *anyopaque, presence: Types.PresenceUpdate) anyerror!void = undefined,
|
||||
stage_instance_create: ?*const fn (save: *anyopaque, stage_instance: Types.StageInstance) anyerror!void = undefined,
|
||||
stage_instance_update: ?*const fn (save: *anyopaque, stage_instance: Types.StageInstance) anyerror!void = undefined,
|
||||
stage_instance_delete: ?*const fn (save: *anyopaque, stage_instance: Types.StageInstance) anyerror!void = undefined,
|
||||
typing_start: ?*const fn (save: *anyopaque, data: Types.TypingStart) anyerror!void = undefined,
|
||||
guild_delete: ?*const fn (shard: *Shard, guild: Types.UnavailableGuild) anyerror!void = undefined,
|
||||
guild_ban_add: ?*const fn (shard: *Shard, gba: Types.GuildBanAddRemove) anyerror!void = undefined,
|
||||
guild_ban_remove: ?*const fn (shard: *Shard, gbr: Types.GuildBanAddRemove) anyerror!void = undefined,
|
||||
guild_emojis_update: ?*const fn (shard: *Shard, fields: Types.GuildEmojisUpdate) anyerror!void = undefined,
|
||||
guild_stickers_update: ?*const fn (shard: *Shard, fields: Types.GuildStickersUpdate) anyerror!void = undefined,
|
||||
guild_integrations_update: ?*const fn (shard: *Shard, fields: Types.GuildIntegrationsUpdate) anyerror!void = undefined,
|
||||
guild_member_add: ?*const fn (shard: *Shard, guild_id: Types.GuildMemberAdd) anyerror!void = undefined,
|
||||
guild_member_update: ?*const fn (shard: *Shard, fields: Types.GuildMemberUpdate) anyerror!void = undefined,
|
||||
guild_member_remove: ?*const fn (shard: *Shard, user: Types.GuildMemberRemove) anyerror!void = undefined,
|
||||
guild_members_chunk: ?*const fn (shard: *Shard, data: Types.GuildMembersChunk) anyerror!void = undefined,
|
||||
guild_role_create: ?*const fn (shard: *Shard, role: Types.GuildRoleCreate) anyerror!void = undefined,
|
||||
guild_role_delete: ?*const fn (shard: *Shard, role: Types.GuildRoleDelete) anyerror!void = undefined,
|
||||
guild_role_update: ?*const fn (shard: *Shard, role: Types.GuildRoleUpdate) anyerror!void = undefined,
|
||||
guild_scheduled_event_create: ?*const fn (shard: *Shard, s_event: Types.ScheduledEvent) anyerror!void = undefined,
|
||||
guild_scheduled_event_update: ?*const fn (shard: *Shard, s_event: Types.ScheduledEvent) anyerror!void = undefined,
|
||||
guild_scheduled_event_delete: ?*const fn (shard: *Shard, s_event: Types.ScheduledEvent) anyerror!void = undefined,
|
||||
guild_scheduled_event_user_add: ?*const fn (shard: *Shard, data: Types.ScheduledEventUserAdd) anyerror!void = undefined,
|
||||
guild_scheduled_event_user_remove: ?*const fn (shard: *Shard, data: Types.ScheduledEventUserRemove) anyerror!void = undefined,
|
||||
integration_create: ?*const fn (shard: *Shard, guild_id: Types.IntegrationCreateUpdate) anyerror!void = undefined,
|
||||
integration_update: ?*const fn (shard: *Shard, guild_id: Types.IntegrationCreateUpdate) anyerror!void = undefined,
|
||||
integration_delete: ?*const fn (shard: *Shard, guild_id: Types.IntegrationDelete) anyerror!void = undefined,
|
||||
interaction_create: ?*const fn (shard: *Shard, interaction: Types.MessageInteraction) anyerror!void = undefined,
|
||||
invite_create: ?*const fn (shard: *Shard, data: Types.InviteCreate) anyerror!void = undefined,
|
||||
invite_delete: ?*const fn (shard: *Shard, data: Types.InviteDelete) anyerror!void = undefined,
|
||||
message_create: ?*const fn (shard: *Shard, message: Types.Message) anyerror!void = undefined,
|
||||
message_update: ?*const fn (shard: *Shard, message: Types.Message) anyerror!void = undefined,
|
||||
message_delete: ?*const fn (shard: *Shard, log: Types.MessageDelete) anyerror!void = undefined,
|
||||
message_delete_bulk: ?*const fn (shard: *Shard, log: Types.MessageDeleteBulk) anyerror!void = undefined,
|
||||
message_reaction_add: ?*const fn (shard: *Shard, log: Types.MessageReactionAdd) anyerror!void = undefined,
|
||||
message_reaction_remove_all: ?*const fn (shard: *Shard, data: Types.MessageReactionRemoveAll) anyerror!void = undefined,
|
||||
message_reaction_remove: ?*const fn (shard: *Shard, data: Types.MessageReactionRemove) anyerror!void = undefined,
|
||||
message_reaction_remove_emoji: ?*const fn (shard: *Shard, data: Types.MessageReactionRemoveEmoji) anyerror!void = undefined,
|
||||
presence_update: ?*const fn (shard: *Shard, presence: Types.PresenceUpdate) anyerror!void = undefined,
|
||||
stage_instance_create: ?*const fn (shard: *Shard, stage_instance: Types.StageInstance) anyerror!void = undefined,
|
||||
stage_instance_update: ?*const fn (shard: *Shard, stage_instance: Types.StageInstance) anyerror!void = undefined,
|
||||
stage_instance_delete: ?*const fn (shard: *Shard, stage_instance: Types.StageInstance) anyerror!void = undefined,
|
||||
typing_start: ?*const fn (shard: *Shard, data: Types.TypingStart) anyerror!void = undefined,
|
||||
/// remember this is only sent when you change your profile yourself/your bot does
|
||||
user_update: ?*const fn (save: *anyopaque, user: Types.User) anyerror!void = undefined,
|
||||
user_update: ?*const fn (shard: *Shard, user: Types.User) anyerror!void = undefined,
|
||||
// will do these someday, music is rather pointless at this point in time
|
||||
// TODO: implement // voice_channel_effect_send: null = null,
|
||||
// TODO: implement // voice_state_update: null = null,
|
||||
// TODO: implement // voice_server_update: null = null,
|
||||
webhooks_update: ?*const fn (save: *anyopaque, fields: Types.WebhookUpdate) anyerror!void = undefined,
|
||||
entitlement_create: ?*const fn (save: *anyopaque, entitlement: Types.Entitlement) anyerror!void = undefined,
|
||||
entitlement_update: ?*const fn (save: *anyopaque, entitlement: Types.Entitlement) anyerror!void = undefined,
|
||||
webhooks_update: ?*const fn (shard: *Shard, fields: Types.WebhookUpdate) anyerror!void = undefined,
|
||||
entitlement_create: ?*const fn (shard: *Shard, entitlement: Types.Entitlement) anyerror!void = undefined,
|
||||
entitlement_update: ?*const fn (shard: *Shard, entitlement: Types.Entitlement) anyerror!void = undefined,
|
||||
/// discord claims this is infrequent, therefore not throughoutly tested - Yuzu
|
||||
entitlement_delete: ?*const fn (save: *anyopaque, entitlement: Types.Entitlement) anyerror!void = undefined,
|
||||
message_poll_vote_add: ?*const fn (save: *anyopaque, poll: Types.PollVoteAdd) anyerror!void = undefined,
|
||||
message_poll_vote_remove: ?*const fn (save: *anyopaque, poll: Types.PollVoteRemove) anyerror!void = undefined,
|
||||
entitlement_delete: ?*const fn (shard: *Shard, entitlement: Types.Entitlement) anyerror!void = undefined,
|
||||
message_poll_vote_add: ?*const fn (shard: *Shard, poll: Types.PollVoteAdd) anyerror!void = undefined,
|
||||
message_poll_vote_remove: ?*const fn (shard: *Shard, poll: Types.PollVoteRemove) anyerror!void = undefined,
|
||||
|
||||
ready: ?*const fn (save: *anyopaque, data: Types.Ready) anyerror!void = undefined,
|
||||
ready: ?*const fn (shard: *Shard, data: Types.Ready) anyerror!void = undefined,
|
||||
// TODO: implement // resumed: null = null,
|
||||
any: ?*const fn (save: *anyopaque, data: std.json.Value) anyerror!void = undefined,
|
||||
any: ?*const fn (shard: *Shard, data: std.json.Value) anyerror!void = undefined,
|
||||
};
|
||||
|
@ -20,18 +20,17 @@ const Shard = Discord.Shard;
|
||||
const Intents = Discord.Intents;
|
||||
|
||||
const INTENTS = 53608447;
|
||||
const Cache = Discord.cache.TableTemplate{};
|
||||
|
||||
fn ready(_: *anyopaque, payload: Discord.Ready) !void {
|
||||
var session: *Discord.Session = undefined;
|
||||
|
||||
fn ready(_: *Shard, payload: Discord.Ready) !void {
|
||||
std.debug.print("logged in as {s}\n", .{payload.user.username});
|
||||
// cache demonstration TODO
|
||||
}
|
||||
|
||||
fn message_create(shard_ptr: *anyopaque, message: Discord.Message) !void {
|
||||
// set custom cache
|
||||
const session: *Shard(Cache) = @ptrCast(@alignCast(shard_ptr));
|
||||
|
||||
fn message_create(_: *Shard, message: Discord.Message) !void {
|
||||
if (message.content != null and std.ascii.eqlIgnoreCase(message.content.?, "!hi")) {
|
||||
var result = try session.sendMessage(message.channel_id, .{ .content = "hi :)" });
|
||||
var result = try session.api.sendMessage(message.channel_id, .{ .content = "hi :)" });
|
||||
defer result.deinit();
|
||||
|
||||
const m = result.value.unwrap();
|
||||
@ -43,8 +42,9 @@ pub fn main() !void {
|
||||
var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
var handler = Discord.init(allocator);
|
||||
defer handler.deinit();
|
||||
session = try allocator.create(Discord.Session);
|
||||
session.* = Discord.init(allocator);
|
||||
defer session.deinit();
|
||||
|
||||
const env_map = try allocator.create(std.process.EnvMap);
|
||||
env_map.* = try std.process.getEnvMap(allocator);
|
||||
@ -54,12 +54,12 @@ pub fn main() !void {
|
||||
@panic("DISCORD_TOKEN not found in environment variables");
|
||||
};
|
||||
|
||||
try handler.start(.{
|
||||
.token = token,
|
||||
try session.start(.{
|
||||
.authorization = token,
|
||||
.intents = Intents.fromRaw(INTENTS),
|
||||
.run = .{ .message_create = &message_create, .ready = &ready },
|
||||
.log = .yes,
|
||||
.options = .{},
|
||||
.cache = Cache,
|
||||
.cache = .defaults(allocator),
|
||||
});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user