How to properly get avatar's URL

Hello! I’m trying to get the player’s avatar for a webhook but it doesn’t seem to work. I believe the problem is at the URL. Is there a way to fix this?

I’ve looked at other documents as well and tried all of their solutions but it doesn’t seem to work.

Helps are appreciated!

local function mainhandler(p, msg)
	if iscommand(p, msg) then
		
		local fields = {
			{
				['name'] = "User",
				['value'] = p.Name,
				['inline'] = true
			},
			
			{
				['name'] = "UserID",
				['value'] = p.UserId,
				['inline'] = true
			},
			
			{
				['name'] = "Command",
				['value'] = msg,
				['inline'] = true
			},
			
			{
				['name'] = "Rank",
				['value'] = p:GetRoleInGroup(13804229),
				['inline'] = true
			}
		}
		
	
		
		discord:createEmbed(url, "HD Admin Command", "A command was used in Serenity:", fields, "https://thumbnails.roproxy.com/v1/users/avatar-headshot?userIds="..p.UserId.."&size=180x180&format=Png&isCircular=false")
		
		
	end
end

The url seems to work for me, returning a png image and all. You could try adding a tostring() to the p.UserId.

But with the information you provided It’s hard to diagnose the problem.

Hm, I tried adding tostring() but it still doesn’t seem to work…

Maybe this will help? I used a module that helps connect webhooks and this is the function I was using.

function webhookService:createEmbed(url, title : string, message :string , fields, image)
	local data = {
		['content'] = "",
		['embeds'] = {{
			["thumbnail"] = {["url"] = image},
			['title'] = "**"..title.."**",
			['description'] = message,
			['type'] = "rich",
			["color"] = tonumber(0xffffff),
			['fields'] = fields

		},
		},
	}

Here’s the updated line to see if I did anything wrong:

discord:createEmbed(url, "HD Admin Command", "A command was used in Serenity:", fields, "https://thumbnails.roproxy.com/v1/users/avatar-headshot?userIds="..tostring(p.UserId).."&size=720x720&format=Png&isCircular=false")

Here is the issue:
The url for getting the avatar headshot returns a json dictionary similar to the following:

{"data":[{
"targetId":1,
"state":"Completed",
"imageUrl":"https://tr.rbxcdn.com/30DAY-AvatarHeadshot-A91C0E7E0F594224906140CE8B4D1479-Png/180/180/AvatarHeadshot/Png/noFilter",
"version":"TN3"
}]
}

That is a dictionary containing an array, which contains a dictionary. Inside that last dictionary there is a key, “imageUrl”. That is what you want to pass through to the module.

I notice that too, although how should I go about it?

You could modify the module script to get the imageUrl, or get it before calling the :createEmbed function.

To get it you just need to do a simple Http request:

local HttpService = game:GetService("HttpService")


function getAvatarThumbnail(UserId:number) : (boolean,string)
	local Url = "https://thumbnails.roproxy.com/v1/users/avatar-headshot?userIds="..UserId.."&size=180x180&format=Png&isCircular=false"
	local Response = HttpService:RequestAsync({
		Url = Url,
		Method = "GET",
		Headers = {},
		Body = nil
	})
	if Response.Success then
		local Data = HttpService:JSONDecode(Response.Body)
		return true, Data["data"][1]["imageUrl"]
	else
		return false, Response.Body
	end
end


local Success, Data = getAvatarThumbnail(2906349392)
1 Like

May I ask what the number inside getAvatarThumbnail() is?

The UserId.

You should pass p.UserId into it.

local Success, ImageUrl = getAvatarThumbnail(p.UserId)

discord:createEmbed(
url,
 "HD Admin Command",
 "A command was used in Serenity:",
 fields,
 imageUrl
)

Make sure to handle errors, i.e. If Success is false you might want to pass a placeholder image instead of imageUrl as it will be nil

It works! Thank you so much for your help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.