Skip to content

Commit 0ed9757

Browse files
authored
Use Ready event handler instead of raw event handler (#36)
1 parent 7e392ba commit 0ed9757

File tree

4 files changed

+18
-41
lines changed

4 files changed

+18
-41
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/discord/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ gpui = { git = "https://github.com/huacnlee/zed.git", branch = "export-platform-
99
"font-kit",
1010
] }
1111
scope-chat = { version = "0.1.0", path = "../chat" }
12-
serenity = { git = "https://github.com/scopeclient/serenity", version = "0.13.0" }
12+
serenity = { git = "https://github.com/scopeclient/serenity", version = "0.12" }
1313
tokio = "1.41.1"
1414
chrono.workspace = true
1515
scope-backend-cache = { version = "0.1.0", path = "../cache" }

src/discord/src/client.rs

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
};
55

66
use serenity::{
7-
all::{Cache, ChannelId, Context, CreateMessage, Event, EventHandler, GatewayIntents, GetMessages, Http, Message, MessageId, RawEventHandler},
7+
all::{Cache, ChannelId, Context, CreateMessage, EventHandler, GatewayIntents, GetMessages, Http, Message, MessageId, Ready},
88
async_trait,
99
};
1010
use tokio::sync::{broadcast, RwLock};
@@ -40,7 +40,6 @@ impl DiscordClient {
4040

4141
let mut discord = serenity::Client::builder(token, GatewayIntents::all())
4242
.event_handler_arc(client.clone())
43-
.raw_event_handler(RawClient(client.clone()))
4443
.await
4544
.expect("Error creating client");
4645

@@ -110,42 +109,16 @@ impl DiscordClient {
110109
}
111110
}
112111

113-
struct RawClient(Arc<DiscordClient>);
114-
115-
#[async_trait]
116-
impl RawEventHandler for RawClient {
117-
async fn raw_event(&self, _: Context, ev: serenity::model::prelude::Event) {
118-
if let Event::Unknown(unk) = ev {
119-
if unk.kind == "READY" {
120-
if let Some(user) = unk.value.as_object().and_then(|obj| obj.get("user")).and_then(|u| u.as_object()) {
121-
let username = user.get("username").and_then(|u| u.as_str()).unwrap_or("Unknown User").to_owned();
122-
123-
let user_id = user.get("id").and_then(|id| id.as_str()).unwrap_or_default();
124-
125-
let icon = user
126-
.get("avatar")
127-
.and_then(|avatar| avatar.as_str())
128-
.map(|avatar| format!("https://cdn.discordapp.com/avatars/{}/{}", user_id, avatar))
129-
.unwrap_or_else(|| {
130-
format!(
131-
"https://cdn.discordapp.com/embed/avatars/{}.png",
132-
(user_id.parse::<u64>().unwrap_or(0) % 5)
133-
)
134-
});
135-
136-
self.0.user.get_or_init(|| DiscordMessageAuthor {
137-
display_name: DisplayName(username),
138-
icon,
139-
id: user_id.to_owned(),
140-
});
141-
}
142-
}
143-
}
144-
}
145-
}
146112

147113
#[async_trait]
148114
impl EventHandler for DiscordClient {
115+
async fn ready(&self, _: Context, ready: Ready) {
116+
self.user.get_or_init(|| DiscordMessageAuthor {
117+
display_name: DisplayName(ready.user.name.clone()),
118+
icon: ready.user.face(),
119+
id: ready.user.id.to_string(),
120+
});
121+
}
149122
async fn message(&self, _: Context, msg: Message) {
150123
let snowflake = Snowflake {
151124
content: msg.channel_id.get(),

src/discord/src/message/author.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ impl MessageAuthor for DiscordMessageAuthor {
2525
}
2626

2727
fn get_small_icon(&self) -> String {
28-
self.icon.clone() + "?size=32"
28+
let icon = match self.icon.strip_suffix("?size=1024") {
29+
Some(strip) => strip.to_owned(),
30+
None => self.icon.to_owned(),
31+
};
32+
icon + "?size=32"
2933
}
3034

3135
fn get_id(&self) -> String {

0 commit comments

Comments
 (0)