Add GuildRoleCreate and -Update

This commit is contained in:
bitfl0wer 2023-08-16 14:05:37 +02:00
parent e0340047fb
commit 2af8388947
2 changed files with 50 additions and 7 deletions

View File

@ -1,7 +1,8 @@
use crate::errors::GatewayError;
use crate::gateway::events::Events;
use crate::types::{
self, Channel, ChannelUpdate, Composite, JsonField, Snowflake, UpdateMessage, WebSocketEvent,
self, Channel, ChannelUpdate, Composite, Guild, GuildRoleCreate, GuildRoleUpdate, JsonField,
RoleObject, Snowflake, UpdateMessage, WebSocketEvent,
};
use async_trait::async_trait;
use std::any::Any;
@ -231,6 +232,8 @@ impl GatewayHandle {
}
}
/// Recursively observes and updates all updateable fields on the struct T. Returns an object
/// with all of its observable fields being observed.
pub async fn observe_and_get<T: Updateable + Clone + Composite<T>>(
&self,
object: Arc<RwLock<T>>,
@ -240,6 +243,8 @@ impl GatewayHandle {
object
}
/// Recursively observes and updates all updateable fields on the struct T. Returns an object `T`
/// with all of its observable fields being observed.
pub async fn observe_and_into_inner<T: Updateable + Clone + Composite<T>>(
&self,
object: Arc<RwLock<T>>,
@ -592,8 +597,8 @@ impl Gateway {
"GUILD_MEMBER_REMOVE" => guild.member_remove,
"GUILD_MEMBER_UPDATE" => guild.member_update,
"GUILD_MEMBERS_CHUNK" => guild.members_chunk,
"GUILD_ROLE_CREATE" => guild.role_create,
"GUILD_ROLE_UPDATE" => guild.role_update,
"GUILD_ROLE_CREATE" => guild.role_create GuildRoleCreate: Guild,
"GUILD_ROLE_UPDATE" => guild.role_update GuildRoleUpdate: RoleObject,
"GUILD_ROLE_DELETE" => guild.role_delete,
"GUILD_SCHEDULED_EVENT_CREATE" => guild.role_scheduled_event_create,
"GUILD_SCHEDULED_EVENT_UPDATE" => guild.role_scheduled_event_update,

View File

@ -1,13 +1,17 @@
use std::sync::{Arc, RwLock};
use chorus_macros::JsonField;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::types::entities::{Guild, PublicUser, UnavailableGuild};
use crate::types::events::WebSocketEvent;
use crate::types::{
AuditLogEntry, Emoji, GuildMember, GuildScheduledEvent, RoleObject, Snowflake, Sticker,
AuditLogEntry, Emoji, GuildMember, GuildScheduledEvent, JsonField, RoleObject, Snowflake,
Sticker,
};
use super::PresenceUpdate;
use super::{PresenceUpdate, UpdateMessage};
#[derive(Debug, Deserialize, Serialize, Default, Clone)]
/// See <https://discord.com/developers/docs/topics/gateway-events#guild-create>;
@ -164,24 +168,58 @@ pub struct GuildMembersChunk {
impl WebSocketEvent for GuildMembersChunk {}
#[derive(Debug, Default, Deserialize, Serialize, Clone)]
#[derive(Debug, Default, Deserialize, Serialize, Clone, JsonField)]
/// See <https://discord.com/developers/docs/topics/gateway-events#guild-role-create>
pub struct GuildRoleCreate {
pub guild_id: Snowflake,
pub role: RoleObject,
#[serde(skip)]
pub json: String,
}
impl WebSocketEvent for GuildRoleCreate {}
#[derive(Debug, Default, Deserialize, Serialize, Clone)]
impl UpdateMessage<Guild> for GuildRoleCreate {
fn id(&self) -> Snowflake {
self.guild_id
}
fn update(&mut self, object_to_update: Arc<RwLock<Guild>>) {
let mut object_to_update = object_to_update.write().unwrap();
if object_to_update.roles.is_some() {
object_to_update
.roles
.as_mut()
.unwrap()
.push(Arc::new(RwLock::new(self.role.clone())));
} else {
object_to_update.roles = Some(Vec::from([Arc::new(RwLock::new(self.role.clone()))]));
}
}
}
#[derive(Debug, Default, Deserialize, Serialize, Clone, JsonField)]
/// See <https://discord.com/developers/docs/topics/gateway-events#guild-role-update>
pub struct GuildRoleUpdate {
pub guild_id: Snowflake,
pub role: RoleObject,
#[serde(skip)]
pub json: String,
}
impl WebSocketEvent for GuildRoleUpdate {}
impl UpdateMessage<RoleObject> for GuildRoleUpdate {
fn id(&self) -> Snowflake {
self.role.id
}
fn update(&mut self, object_to_update: Arc<RwLock<RoleObject>>) {
let mut write = object_to_update.write().unwrap();
*write = self.role.clone();
}
}
#[derive(Debug, Default, Deserialize, Serialize, Clone)]
/// See <https://discord.com/developers/docs/topics/gateway-events#guild-role-delete>
pub struct GuildRoleDelete {