How can I properly check if a user owns a badge using the HTTP API?

I’m making a verification bot for my server, I’ve got it mostly set up, however there’s a “false negative” when giving a role to a user that owns a badge. If the badge does not appear in the last 100 badges the user got, the bot won’t detect it. Here’s my current code to check if the user owns the specific badge in Node.JS:

request.get(`https://badges.roblox.com/v1/users/${json.sub}/badges?limit=100&sortOrder=Desc`, async function(error, response, body) {
	if (!error && response.statusCode == 200) {
		const badges = JSON.parse(body);
		for (obj of badges.data) {
			if (obj.id == 2148126273) {
				const donatorRole = await sessions[state].guild.roles.fetch("1138509817783263363");
				sessions[state].roles.add(donatorRole, `donated as ${json.sub}`);
			};
		};
	} else {
		console.log(`[ERROR] Getting badges ${state}: ${err} (${res.statusCode})`);
	};
});

I’ve tested this with people that do own the badge, and they didn’t receive the role, by manually checking, the badge ID doesn’t appear in their last 100 badges.
How can I properly check if the user owns a badge without checking if it appears in the user’s last 100 badges?

It’s not a false negative. Look closely to the link you are using.
https://badges.roblox.com/v1/users/${json.sub}/badges?limit=100&sortOrder=Desc
There is a clear limit set of 100 badges.

To get the next page, when you get the response body from your first request, it includes nextPageCursor. It is needed to get access to the next page in your second request.
image

So to get the next page, you include that cursor in the requesting link, adding "?cursor=" + previousRespone.nextPageCursor

1 Like

There is an API endpoint that tells you when someone earned a badge, just check if they have ever had it, and if there is a valid date then they do own it.

https://badges.roblox.com/v1/users/USER_ID/badges/awarded-dates?badgeIds=BADGE_ID

It returns nothing but an empty array like this if they don’t own it:

if they own it:

4 Likes

Best solution for me, much more efficient, thank you so much! I’ll do testing and see if it works better now!
Back, worked perfectly, thank you so much!

1 Like

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