Delete server on 404 instead of giving 500

This commit is contained in:
ok236449 2022-08-20 18:49:33 +02:00
parent 21af9d9e77
commit c26a47a982
2 changed files with 10 additions and 4 deletions

View file

@ -287,7 +287,7 @@ class Pterodactyl
* @param int $pterodactylId
* @return mixed
*/
public static function getServerAttributes(int $pterodactylId)
public static function getServerAttributes(int $pterodactylId, bool $deleteOn404 = false)
{
try {
$response = self::client()->get("/application/servers/{$pterodactylId}?include=egg,node,nest,location");
@ -299,7 +299,13 @@ class Pterodactyl
if ($response->failed()) throw self::getException("Failed to get server attributes from pterodactyl - ", $response->status());
if ($response->failed()){
if($deleteOn404){ //Delete the server if it does not exist (server deleted on pterodactyl)
Server::where('pterodactyl_id', $pterodactylId)->first()->delete();
return;
}
else throw self::getException("Failed to get server attributes from pterodactyl - ", $response->status());
}
return $response->json()['attributes'];
}

View file

@ -30,8 +30,8 @@ class ServerController extends Controller
foreach ($servers as $server) {
//Get server infos from ptero
$serverAttributes = Pterodactyl::getServerAttributes($server->pterodactyl_id);
$serverAttributes = Pterodactyl::getServerAttributes($server->pterodactyl_id, true);
if(!$serverAttributes) continue;
$serverRelationships = $serverAttributes['relationships'];
$serverLocationAttributes = $serverRelationships['location']['attributes'];