Home > database >  Discord API - Get Real Flag name
Discord API - Get Real Flag name

Time:11-08

I have a question, I saw that the api discord makes a calculation of all our badges, ie if we are staff hypesquad, it gives the number "5". However, I would like to know how to make a reliable calculation that gives the real result of the badge (Ex: that 5 gives well and truly the word "Staff & Hypesquad Event"

Here is the api code:

{
    "id": "N/A",
    "username": "developer",
    "avatar": "N/A",
    "discriminator": "0001",
    "public_flags": 644,
    "banner": null,
    "banner_color": null,
    "accent_color": null
}

as you can see, public_flags = 644

Real Flags:

Discord_Employee = 1;
Partnered_Server_Owner = 2;
HypeSquad_Events = 4;
Bug_Hunter_Level_1 = 8;
House_Bravery = 64;
House_Brilliance = 128;
House_Balance = 256;
Early_Supporter = 512;
Bug_Hunter_Level_2 = 16384;
Early_Verified_Bot_Developer = 131072;

It is necessary to know that the user with the 644, contains in reality the following badges:

  • Hypesquad Event Brilliance Early_Supporter

The calculation system must be done in php

Thank to all,

CodePudding user response:

You can use bitwise operation:

$badges = [
   1 => 'Discord_Employee',
   2 => 'Partnered_Server_Owner',
   4 => 'HypeSquad_Events',
   8 => 'Bug_Hunter_Level_1'
];

$flags = 6;

var_dump(array_filter($badges, function($value, $key) use($flags) { 
   return ($key & $flags) > 0; 
}, ARRAY_FILTER_USE_BOTH));

This code prints

array(2) {
  [2]=>
  string(22) "Partnered_Server_Owner"
  [4]=>
  string(16) "HypeSquad_Events"
}

CodePudding user response:

the way I did it was handling discord tasks via nodejs using discordjs / express and just processing the data in PHP, the nodejs app output a JSON format, that way I was able to do it, but the script by Pavel Třupek is way better if you just get user object, something like a checker, in my case I used more data than just flags so this way was optimized for my use.

  • Related