Search code examples
phpdiscordpermissionsbitwise-operatorsbitwise-and

Discord permissions fields in PHP


I have to check permissions for each guild of the current logged in user.

I tried this but it does not works because PHP convert string to int, and if it is too large, it will not be the right number.

/* Deprecated: Implicit conversion from float-string "..." to int loses precision */
if ($g->permissions & 0x20) {
    # ...
}

/* Always FALSE if $g->permissions is bigger than php max integer value */
if (intval($g->permissions) & 0x20)
{
    # ...
}

And I tried this, but it returns a string (mostly "02", "10", "12" and "20") and I don't know what to do with it.

if ($g->permissions & strval(0x20)) {
    # ...
}

I searched a lot but can't find anything. Does someone have a solution to my problem ? Thanks


Solution

  • It is possible to make bitwise operations on big integers using GMP (gmp_and).

    Instead of doing:

    if ($g->permissions & 0x20) {
        # ...
    }
    

    I should do:

    if (gmp_and($g->permissions, 0x20)) {
        # ...
    }
    

    Thanks to @vinceAmstoutz