diff --git a/gateway/src/cache.rs b/gateway/src/cache.rs new file mode 100644 index 00000000..3306ed73 --- /dev/null +++ b/gateway/src/cache.rs @@ -0,0 +1,73 @@ +use twilight_gateway::Event; + +pub async fn handle_event<'a>( + event: Event, + rconn: redis::Client +) -> anyhow::Result<()> { + match event { + // todo: SaveDMChannelStub (?) + // todo: save user profiles for some reason (from message create, etc) + // todo(dotnet): remove relying on cache.OwnUserId + + Event::GuildCreate(guild) => { + // save guild itself + // save all roles in guild + // save guild-role map + // save all channels in guild + // save all threads in guild (as channels lol) + // save guild-channel map + // save self guild member + + // c# code also saves users in guildCreate.Members, but I'm pretty sure that doesn't have anything now because intents + } + Event::GuildUpdate(guild) => { + // save guild itself + } + Event::GuildDelete(guild) => { + // delete guild + // this probably should also delete all channels/roles/etc of the guild + } + Event::MemberUpdate(member) => { + // save self guild member + } + Event::ChannelCreate(channel) => { + // save channel + // update guild-channel map + } + Event::ChannelUpdate(channel) => { + // save channel + } + Event::ChannelDelete(channel) => { + // delete channel + // update guild-channel map + } + Event::RoleCreate(role) => { + // save role + // update guild-role map + } + Event::RoleUpdate(role) => { + // save role + } + Event::RoleDelete(role) => { + // delete role + // update guild-role map + } + Event::ThreadCreate(thread) => { + // save channel + // update guild-channel map + } + Event::ThreadUpdate(thread) => { + // save thread + } + Event::ThreadDelete(thread) => { + // delete channel + // update guild-channel map + } + Event::ThreadListSync(tls) => { + // save channels + } + _ => {} + } + + Ok(()) +} \ No newline at end of file diff --git a/gateway/src/evt.rs b/gateway/src/evt.rs index e0012bcc..42ce89d7 100644 --- a/gateway/src/evt.rs +++ b/gateway/src/evt.rs @@ -7,6 +7,8 @@ use tracing::info; use twilight_gateway::Event; use twilight_http::Client as HttpClient; +use crate::cache; + lazy_static::lazy_static! { static ref ALLOWED_EVENTS: Vec<&'static str> = [ "INTERACTION_CREATE", @@ -25,6 +27,8 @@ pub async fn handle_event<'a>( _db: Pool, rconn: redis::Client ) -> anyhow::Result<()> { + cache::handle_event(event.clone(), rconn.clone()).await?; + match event { Event::GatewayInvalidateSession(resumable) => { info!("shard {} session invalidated, resumable? {}", shard_id, resumable); diff --git a/gateway/src/main.rs b/gateway/src/main.rs index 1afaef4f..7b19d7d2 100644 --- a/gateway/src/main.rs +++ b/gateway/src/main.rs @@ -9,6 +9,7 @@ use twilight_gateway::{ }; use twilight_http::Client as HttpClient; +mod cache; mod config; mod evt; mod db; @@ -81,7 +82,7 @@ async fn init_gateway( if shard_count < 16 { scheme = ShardScheme::Auto; } else { - let cluster_id = env::var("NOMAD_ALLOC_INDEX").or("0").parse::().unwrap(); + let cluster_id = env::var("NOMAD_ALLOC_INDEX").or::(Result::Ok("0".to_string())).unwrap().parse::().unwrap(); let first_shard_id = 16 * cluster_id; scheme = ShardScheme::try_from((first_shard_id..first_shard_id+16, shard_count)).unwrap();