You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP脚本中Discord角色分配失败(403权限不足错误)

WordPress-Discord角色同步插件:Discord API 403权限缺失问题

开发一款可将WordPress站点与Discord连接的插件,用户通过短码完成注册后,系统会自动根据其当前WordPress角色分配对应的Discord角色。目前整体流程基本正常,但在发送PUT请求为用户分配名为"low"的Discord角色时,Discord API返回403状态码,错误信息为Missing Permissions(错误代码50013)。

已确认以下信息无误:

  • Discord Bot已配置管理员权限
  • User ID、Server ID、Role ID、Bot Token等参数均正确
  • 已验证Bot的权限范围

但问题仍未解决,以下是相关日志、API请求格式及PHP函数代码,寻求技术帮助。

日志信息

[17-Sep-2023 10:49:52 UTC] Access token:
[17-Sep-2023 10:49:52 UTC] Discord User ID:
[17-Sep-2023 10:49:52 UTC] Server ID:
[17-Sep-2023 10:49:52 UTC] Bot Token:
[17-Sep-2023 10:49:52 UTC] Discord Role ID:
[17-Sep-2023 10:49:53 UTC] API Response: {
"message": "Missing Permissions",
"code": 50013
}
[17-Sep-2023 10:49:53 UTC] Received HTTP response code: 403
[17-Sep-2023 10:49:53 UTC] Received 403 from Discord API
[17-Sep-2023 10:49:53 UTC] Error assigning Discord role: Received 403 from Discord API

API请求格式

API请求格式:
Curl URL: https://discord.com/api/v8/guilds/%7B{SERVER ID}/members/{DISCORD USER ID}
Curl Headers: ["Authorization: Bot {BOT ID}","Content-Type: application/json"]
Curl Body: {"access_token":"{ACCESS_TOKEN}","roles":["{NEW ROLE ID}"]}

PHP函数代码

function assign_discord_role($access_token, $stored_discord_id) {
    global $wpdb;
    // 私有数据
    $bot_token = "MY PRIVATE BOT TOKEN HERE";
    $server_id = "MY SERVER ID HERE";

    // 读取已保存的Discord ID和WordPress角色
    $stored_discord_id = get_option('stored_discord_id');
    $stored_wp_role = get_option('current_wp_role');

    if (!$stored_discord_id) {
        error_log("Discord ID niet gevonden in WordPress-opties");
        return ['error' => 'Discord ID not found'];
    }

    // 记录诊断日志
    error_log("Function assign_discord_role is being invoked.");
    error_log("Assigning Discord role to Discord user ID: {$stored_discord_id}");
    error_log("Discord ID from WordPress options: {$stored_discord_id}");
    error_log("Access token: {$access_token}");
    error_log("Current WP role from WordPress options: {$stored_wp_role}");

    // 从数据库查询对应的Discord角色ID
    $query = "SELECT discord_role_id FROM {$wpdb->prefix}discord_role_mappings WHERE wp_role = %s";
    $discord_role_id = $wpdb->get_var($wpdb->prepare($query, $stored_wp_role));
    error_log("Discord role ID from DB: {$discord_role_id}");

    if (!$discord_role_id) {
        error_log("No corresponding Discord role found for WordPress role.");
        return ['error' => 'No corresponding Discord role found'];
    }

    // 记录待发送的请求数据
    error_log("API Request Data:");
    error_log("Discord User ID: " . $stored_discord_id);
    error_log("Server ID: " . $server_id);
    error_log("Bot Token: " . $bot_token);
    error_log("Discord Role ID: " . $discord_role_id);

    $url = "https://discord.com/api/v8/guilds/{$server_id}/members/{$stored_discord_id}";
    $headers = [
        "Authorization: Bot {$bot_token}",
        'Content-Type: application/json'
    ];

    $body = json_encode([
        'access_token' => $access_token,
        'roles' => [$discord_role_id]
    ]);

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $curl_response = curl_exec($ch);
    
    // 记录实际发送的API请求
    error_log("Curl URL: {$url}");
    error_log("Curl Headers: " . json_encode($headers));
    error_log("Curl Body: {$body}");
    error_log("API Response: " . json_encode(json_decode($curl_response), JSON_PRETTY_PRINT));


    if ($curl_response === false) {
        error_log("Something went wrong while contacting Discord API: " . curl_error($ch));
        return ['error' => "Something went wrong: " . curl_error($ch)];
    }

    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    error_log("Received HTTP response code: {$http_code}");

    if ($http_code === 200 || $http_code === 201 || $http_code === 204) {
        error_log("Successfully assigned Discord role: {$discord_role_id}");
        return ['success' => $discord_role_id];
    } else {
        error_log("Received {$http_code} from Discord API");
        return ['error' => "Received $http_code from Discord API"];
    }

    curl_close($ch);
}

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 13:22:33