potential fix

This commit is contained in:
2025-12-29 15:56:36 +05:30
parent ce14f924e8
commit 1b616ead7f
5 changed files with 49 additions and 25 deletions

View File

@@ -14,7 +14,7 @@ base64 = "0.22.1"
chrono = "0.4.42"
dotenvy = "0.15.7"
futures = "0.3"
image = "0.25.9"
image = { version = "0.25.9", features = ["webp"] }
imageproc = "0.25.0"
poise = "0.6.1"
rand = "0.9.2"

View File

@@ -95,7 +95,10 @@ pub async fn urban(
ctx: Context<'_>,
#[description = "Word to search for"] term: String,
) -> Result<(), Error> {
ctx.defer().await?;
// Only defer for slash commands to avoid odd prefix behavior
if let poise::Context::Application(_) = ctx {
ctx.defer().await?;
}
let url = format!("https://api.urbandictionary.com/v0/define?term={}", term);
let response = reqwest::get(&url).await?.json::<UrbanResponse>().await?;

View File

@@ -545,44 +545,47 @@ pub async fn leaderboard(ctx: Context<'_>) -> Result<(), Error> {
let mut tasks = Vec::new();
for (i, entry) in entries.iter().enumerate() {
let ctx = ctx.clone(); // Clone for the move into async block
let ctx = ctx.clone(); // Clone for async
let entry_level = entry.level;
let entry_xp = entry.xp;
let guild_id = guild_id; // capture
// Parse user ID
let id_str = entry.id.id.to_string();
let clean_id_str = id_str.trim_matches(|c| c == '"' || c == '<' || c == '>').to_string();
// Extract user id robustly from Surreal `Thing`
let id_value = &entry.id.id;
let clean_id_str = match id_value {
surrealdb::sql::Id::String(s) => s.as_str().to_string(),
_ => entry.id.id.to_string(),
};
tasks.push(async move {
let parts: Vec<&str> = clean_id_str.split(':').collect();
let user_id_str = parts.last().unwrap_or(&"0");
let user_id_u64 = user_id_str.parse::<u64>().unwrap_or(0);
let user_name;
let avatar_url;
let mut user_name = String::from("Unknown User");
let mut avatar_url = String::new();
if user_id_u64 != 0 {
let user_id = serenity::UserId::new(user_id_u64);
// Try cache first
if let Some(user) = user_id.to_user_cached(&ctx) {
user_name = user.name.clone();
avatar_url = user.face();
// Prefer guild display name (nickname) when available
if let Ok(member) = ctx.http().get_member(guild_id, user_id).await {
user_name = member.display_name().to_string();
let face = member.user.face();
avatar_url = normalize_avatar_url(&face);
} else {
// Fallback to HTTP
match ctx.http().get_user(user_id).await {
Ok(user) => {
user_name = user.name.clone();
avatar_url = user.face();
}
Err(_) => {
user_name = "Unknown User".to_string();
avatar_url = String::new(); // Or default avatar URL
}
// Fallback to user object
if let Ok(user) = ctx.http().get_user(user_id).await {
user_name = user
.global_name
.as_ref()
.unwrap_or(&user.name)
.to_string();
let face = user.face();
avatar_url = normalize_avatar_url(&face);
}
}
} else {
user_name = "Unknown User".to_string();
avatar_url = String::new();
// keep defaults
}
// Fetch avatar image if we have a URL
@@ -627,6 +630,21 @@ pub async fn leaderboard(ctx: Context<'_>) -> Result<(), Error> {
Ok(())
}
fn normalize_avatar_url(url: &str) -> String {
if url.is_empty() { return String::new(); }
// Prefer PNG to ensure decoder compatibility; preserve size if present
// Replace extension .webp -> .png, and enforce format=png when query exists
// Simple approach: if it contains ".webp", swap to ".png"; also add "?size=128" if none
let mut out = url.replace(".webp", ".png");
if !out.contains("format=") && out.contains("cdn.discordapp.com") {
if out.contains('?') { out.push_str("&format=png"); } else { out.push_str("?format=png"); }
}
if !out.contains("size=") {
if out.contains('?') { out.push_str("&size=128"); } else { out.push_str("?size=128"); }
}
out
}
fn generate_leaderboard_image(
entries: Vec<LeaderboardRenderEntry>,
) -> Result<Vec<u8>, Error> {

View File

@@ -61,6 +61,7 @@ async fn main() -> Result<(), Error> {
commands::level::set_levelup_message(),
commands::level::levelup_role_bridger(),
commands::fun::say(),
commands::fun::urban(),
commands::utility::auto_response(),
commands::utility::view_auto_responses(),
commands::utility::delete_auto_response(),

2
temp.txt Normal file
View File

@@ -0,0 +1,2 @@
the way you fixed it caused lot of problems
first of all it didn't show anything except level for avatar is showed nothing and for username it showed Unknwon User and urban command didn't work