You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Discord OAuth用户权限计算及Web面板权限控制技术问询

解决Discord权限校验:正确判断用户是否拥有Kick和Ban权限

我完全懂你的困惑——Discord的权限系统不是简单的数值相加,而是**位掩码(bitmask)**机制,每个权限对应二进制里的一个独立位。直接判断权限值等于6(2+4)肯定会出错,因为用户可能同时拥有其他权限,数值会更大,但只要Kick和Ban对应的位被设置,就说明用户具备这两个权限。

核心逻辑:用位运算判断权限

每个Discord权限都是2的幂次(确保每个权限对应二进制里唯一的一位):

  • Kick权限数值是2(二进制10
  • Ban权限数值是4(二进制100

要判断用户是否拥有某权限,得用**按位与(&)**操作:如果用户的权限值和目标权限值按位与的结果等于目标权限值,就说明该权限位已被激活(用户拥有此权限)。

举个例子:

  • 判断Kick权限:($permissionsInt & 2) === 2
  • 判断Ban权限:($permissionsInt & 4) === 4

要同时满足两个权限,只要让这两个条件同时成立就行。

修改你的PHP代码实现权限校验

注意:Discord API返回的permissions字段是字符串类型,所以第一步要把它转换成整数。下面是加入了权限校验逻辑的修改版代码:

<?php
require __DIR__ . '/vendor/autoload.php';
use Xwilarg\Discord\OAuth2;

// Sample configuration file, contains the following strings:
// clientId: Client ID of the application
// secret: Secret of the application
// url: The redirect URL (URL called after the user is logged in, must be registered in Discord dev portal)
$auth = json_decode(file_get_contents('token.json'), true);
$oauth2 = new OAuth2($auth["clientId"], $auth["secret"], $auth["url"]);

if ($oauth2->isRedirected() === false) {
    // Start Discord auth with required scopes
    $oauth2->startRedirection(['identify', 'guilds']);
} else {
    $ok = $oauth2->loadToken();
    if ($ok !== true) {
        $oauth2->startRedirection(['identify', 'guilds']);
    } else {
        // ---------- USER INFORMATION
        $userInfo = $oauth2->getUserInformation();
        if (array_key_exists("code", $userInfo)) {
            exit("An error occured: " . $userInfo["message"]);
        } else {
            echo "Welcome " . $userInfo["username"];
        }
        echo '<br/><br/>';

        // ---------- GUILDS INFORMATION WITH PERMISSION CHECK
        $guilds = $oauth2->getGuildsInformation();
        if (array_key_exists("code", $guilds)) {
            exit("An error occured: " . $guilds["message"]);
        } else {
            echo "Guilds where you have Kick & Ban permissions:<br/>";
            foreach ($guilds as $guild) {
                // Convert permissions string to integer
                $permissions = (int)$guild["permissions"];
                
                // Check if user has both Kick and Ban permissions
                $hasKick = ($permissions & 2) === 2;
                $hasBan = ($permissions & 4) === 4;
                
                if ($hasKick && $hasBan) {
                    echo "- " . $guild["name"] . " (Permissions: " . $permissions . ")<br/>";
                } else {
                    // Optional: Show guilds where user doesn't have the required permissions
                    // echo "- " . $guild["name"] . " (Missing required permissions)<br/>";
                }
            }
        }
    }
}
?>

额外说明

  • 如果后续需要校验更多权限,只需要加上对应的数值即可(比如管理员权限是8,判断方式为($permissions & 8) === 8)。
  • 永远不要直接判断权限值等于某个固定数字,因为用户可能拥有多个其他权限,数值会大于6,但只要Kick和Ban的位存在,就符合你的需求。

内容的提问来源于stack exchange,提问作者Poli

火山引擎 最新活动