Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/discord/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ gpui = { git = "https://github.com/huacnlee/zed.git", branch = "export-platform-
"font-kit",
] }
scope-chat = { version = "0.1.0", path = "../chat" }
serenity = { git = "https://github.com/scopeclient/serenity", version = "0.13.0" }
serenity = { git = "https://github.com/scopeclient/serenity", version = "0.12" }
tokio = "1.41.1"
chrono.workspace = true
scope-backend-cache = { version = "0.1.0", path = "../cache" }
43 changes: 8 additions & 35 deletions src/discord/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
};

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

let mut discord = serenity::Client::builder(token, GatewayIntents::all())
.event_handler_arc(client.clone())
.raw_event_handler(RawClient(client.clone()))
.await
.expect("Error creating client");

Expand Down Expand Up @@ -110,42 +109,16 @@ impl DiscordClient {
}
}

struct RawClient(Arc<DiscordClient>);

#[async_trait]
impl RawEventHandler for RawClient {
async fn raw_event(&self, _: Context, ev: serenity::model::prelude::Event) {
if let Event::Unknown(unk) = ev {
if unk.kind == "READY" {
if let Some(user) = unk.value.as_object().and_then(|obj| obj.get("user")).and_then(|u| u.as_object()) {
let username = user.get("username").and_then(|u| u.as_str()).unwrap_or("Unknown User").to_owned();

let user_id = user.get("id").and_then(|id| id.as_str()).unwrap_or_default();

let icon = user
.get("avatar")
.and_then(|avatar| avatar.as_str())
.map(|avatar| format!("https://cdn.discordapp.com/avatars/{}/{}", user_id, avatar))
.unwrap_or_else(|| {
format!(
"https://cdn.discordapp.com/embed/avatars/{}.png",
(user_id.parse::<u64>().unwrap_or(0) % 5)
)
});

self.0.user.get_or_init(|| DiscordMessageAuthor {
display_name: DisplayName(username),
icon,
id: user_id.to_owned(),
});
}
}
}
}
}

#[async_trait]
impl EventHandler for DiscordClient {
async fn ready(&self, _: Context, ready: Ready) {
self.user.get_or_init(|| DiscordMessageAuthor {
display_name: DisplayName(ready.user.name.clone()),
icon: ready.user.face(),
id: ready.user.id.to_string(),
});
}
async fn message(&self, _: Context, msg: Message) {
let snowflake = Snowflake {
content: msg.channel_id.get(),
Expand Down
6 changes: 5 additions & 1 deletion src/discord/src/message/author.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ impl MessageAuthor for DiscordMessageAuthor {
}

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

fn get_id(&self) -> String {
Expand Down