Skip to content

Commit 473cef6

Browse files
authored
Merge branch 'main' into message-grouping
2 parents 9026478 + 415c6e5 commit 473cef6

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

src/discord/src/client.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,27 @@ impl RawEventHandler for RawClient {
7474
async fn raw_event(&self, _: Context, ev: serenity::model::prelude::Event) {
7575
if let Event::Unknown(unk) = ev {
7676
if unk.kind == "READY" {
77-
let user = unk.value.as_object().unwrap().get("user").unwrap().as_object().unwrap();
78-
79-
self.0.user.get_or_init(|| DiscordMessageAuthor {
80-
display_name: DisplayName(user.get("username").unwrap().as_str().unwrap().to_owned()),
81-
icon: format!(
82-
"https://cdn.discordapp.com/avatars/{}/{}",
83-
user.get("id").unwrap().as_str().unwrap(),
84-
user.get("avatar").unwrap().as_str().unwrap()
85-
),
86-
id: user.get("id").unwrap().as_str().unwrap().to_owned(),
87-
});
77+
if let Some(user) = unk.value.as_object().and_then(|obj| obj.get("user")).and_then(|u| u.as_object()) {
78+
let username = user.get("username").and_then(|u| u.as_str()).unwrap_or("Unknown User").to_owned();
79+
80+
let user_id = user.get("id").and_then(|id| id.as_str()).unwrap_or_default();
81+
82+
let icon = user
83+
.get("avatar")
84+
.and_then(|avatar| avatar.as_str())
85+
.map(|avatar| format!("https://cdn.discordapp.com/avatars/{}/{}", user_id, avatar))
86+
.unwrap_or_else(|| {
87+
format!(
88+
"https://cdn.discordapp.com/embed/avatars/{}.png",
89+
(user_id.parse::<u64>().unwrap_or(0) % 5)
90+
)
91+
});
92+
93+
self.0.user.get_or_init(|| DiscordMessageAuthor {
94+
display_name: DisplayName(username),
95+
icon,
96+
});
97+
}
8898
}
8999
}
90100
}

0 commit comments

Comments
 (0)