use std::sync::Arc;
use std::time::Duration;
use async_trait::async_trait;
use serde_json::Value;
use crate::cache::CachedGuild;
use crate::client::Client;
use crate::error::{BoxError, Error};
use crate::interaction::Interaction;
use crate::message::Message;
use crate::types::*;
pub type HandlerResult = Result<(), BoxError>;
#[derive(Debug, Clone)]
pub struct ReconnectInfo {
pub code: u16,
pub reason: String,
pub wait: Duration,
}
#[derive(Debug, Clone)]
#[non_exhaustive]
#[allow(clippy::large_enum_variant)]
pub enum Event {
Ready(User),
MessageCreate(Message),
MessageUpdate(Message),
MessageDelete(MessageDeleteEvent),
ReactionAdd(ReactionEvent),
ReactionRemove(ReactionEvent),
TypingStart(TypingEvent),
GuildCreate(CachedGuild),
GuildUpdate(CachedGuild),
GuildDelete(Id),
ChannelCreate(Channel),
ChannelUpdate {
channel: Channel,
previous: Option<Channel>,
},
ChannelDelete(Channel),
MemberAdd(Member),
MemberUpdate {
member: Member,
previous: Option<Member>,
},
MemberRemove(MemberRemoveEvent),
PresenceUpdate(PresenceEvent),
VoiceStateUpdate {
state: VoiceState,
previous: Option<VoiceState>,
},
Interaction(Interaction),
Raw {
kind: String,
data: Value,
},
Reconnecting(ReconnectInfo),
Error(Arc<Error>),
}
#[async_trait]
#[allow(unused_variables)]
pub trait EventHandler: Send + Sync + 'static {
async fn ready(&self, ctx: Client, user: User) -> HandlerResult {
Ok(())
}
async fn message_create(&self, ctx: Client, message: Message) -> HandlerResult {
Ok(())
}
async fn message_update(&self, ctx: Client, message: Message) -> HandlerResult {
Ok(())
}
async fn message_delete(&self, ctx: Client, event: MessageDeleteEvent) -> HandlerResult {
Ok(())
}
async fn reaction_add(&self, ctx: Client, reaction: ReactionEvent) -> HandlerResult {
Ok(())
}
async fn reaction_remove(&self, ctx: Client, reaction: ReactionEvent) -> HandlerResult {
Ok(())
}
async fn typing_start(&self, ctx: Client, typing: TypingEvent) -> HandlerResult {
Ok(())
}
async fn guild_create(&self, ctx: Client, guild: CachedGuild) -> HandlerResult {
Ok(())
}
async fn guild_update(&self, ctx: Client, guild: CachedGuild) -> HandlerResult {
Ok(())
}
async fn guild_delete(&self, ctx: Client, guild_id: Id) -> HandlerResult {
Ok(())
}
async fn channel_create(&self, ctx: Client, channel: Channel) -> HandlerResult {
Ok(())
}
async fn channel_update(
&self,
ctx: Client,
channel: Channel,
previous: Option<Channel>,
) -> HandlerResult {
Ok(())
}
async fn channel_delete(&self, ctx: Client, channel: Channel) -> HandlerResult {
Ok(())
}
async fn member_add(&self, ctx: Client, member: Member) -> HandlerResult {
Ok(())
}
async fn member_update(
&self,
ctx: Client,
member: Member,
previous: Option<Member>,
) -> HandlerResult {
Ok(())
}
async fn member_remove(&self, ctx: Client, event: MemberRemoveEvent) -> HandlerResult {
Ok(())
}
async fn presence_update(&self, ctx: Client, presence: PresenceEvent) -> HandlerResult {
Ok(())
}
async fn voice_state_update(
&self,
ctx: Client,
state: VoiceState,
previous: Option<VoiceState>,
) -> HandlerResult {
Ok(())
}
async fn interaction(&self, ctx: Client, interaction: Interaction) -> HandlerResult {
Ok(())
}
async fn raw(&self, ctx: Client, kind: String, data: Value) -> HandlerResult {
Ok(())
}
async fn reconnecting(&self, ctx: Client, info: ReconnectInfo) -> HandlerResult {
Ok(())
}
async fn error(&self, ctx: Client, error: Arc<Error>) {
eprintln!("zynko: {error}");
}
}
pub(crate) async fn call(handler: &dyn EventHandler, ctx: Client, event: Event) -> HandlerResult {
match event {
Event::Ready(u) => handler.ready(ctx, u).await,
Event::MessageCreate(m) => handler.message_create(ctx, m).await,
Event::MessageUpdate(m) => handler.message_update(ctx, m).await,
Event::MessageDelete(d) => handler.message_delete(ctx, d).await,
Event::ReactionAdd(r) => handler.reaction_add(ctx, r).await,
Event::ReactionRemove(r) => handler.reaction_remove(ctx, r).await,
Event::TypingStart(t) => handler.typing_start(ctx, t).await,
Event::GuildCreate(g) => handler.guild_create(ctx, g).await,
Event::GuildUpdate(g) => handler.guild_update(ctx, g).await,
Event::GuildDelete(id) => handler.guild_delete(ctx, id).await,
Event::ChannelCreate(c) => handler.channel_create(ctx, c).await,
Event::ChannelUpdate { channel, previous } => {
handler.channel_update(ctx, channel, previous).await
}
Event::ChannelDelete(c) => handler.channel_delete(ctx, c).await,
Event::MemberAdd(m) => handler.member_add(ctx, m).await,
Event::MemberUpdate { member, previous } => {
handler.member_update(ctx, member, previous).await
}
Event::MemberRemove(d) => handler.member_remove(ctx, d).await,
Event::PresenceUpdate(p) => handler.presence_update(ctx, p).await,
Event::VoiceStateUpdate { state, previous } => {
handler.voice_state_update(ctx, state, previous).await
}
Event::Interaction(i) => handler.interaction(ctx, i).await,
Event::Raw { kind, data } => handler.raw(ctx, kind, data).await,
Event::Reconnecting(info) => handler.reconnecting(ctx, info).await,
Event::Error(e) => {
handler.error(ctx, e).await;
Ok(())
}
}
}