env->getDiscordEnvironment(); $curl = new Curl(); $curl->setHeader('Content-Type', 'application/x-www-form-urlencoded'); $curl->setBasicAuthentication($discordEnv->clientId, $discordEnv->clientSecret); $data =[ 'grant_type' => 'authorization_code', 'code' => $code, 'redirect_uri' => $redirectUri ]; $curl->post(self::OAUTH_TOKEN_URL, $data); if ($curl->error) { $curl->diagnose(); throw new ExtendedException($curl->errorMessage, [ 'response' => $curl->response, 'data' => $data ]); } $accessToken = $curl->response->access_token; $tokenType = $curl->response->token_type; $curl = new Curl(); $curl->setHeader("authorization", "$tokenType $accessToken"); $curl->get(self::USER_ME_URL); if ($curl->error) { $curl->diagnose(); throw new ExtendedException($curl->errorMessage, [ 'response' => $curl->response, ]); } return [ 'id' => $curl->response->id, 'global_name' => $curl->response->global_name, 'avatar' => $curl->response->avatar, 'discriminator' => (int) $curl->response->discriminator ]; } public function getAvatarURL(string $userId, string|int $avatarHash) { if (is_int($avatarHash)) { return "https://cdn.discordapp.com/embed/avatars/{$avatarHash}.png"; } $extension = 'png'; if (str_starts_with($avatarHash, 'a_')) { $extension = 'gif'; } return "https://cdn.discordapp.com/avatars/{$userId}/{$avatarHash}.{$extension}"; } }