Skip to content

ServerManager

Class  ·  Sharkord\Managers\ServerManager

Class ServerManager

Manages server lifecycle events, delegating all cache storage to a Servers collection instance.

Accessible via $sharkord->servers.

// Most bots only operate in one server — getFirst() is the common pattern.
$server = $sharkord->servers->getFirst();
echo "Connected to: {$server->name}\n";
$sharkord->on(\Sharkord\Events::SERVER_UPDATE, function(\Sharkord\Models\Server $server): void {
echo "Server settings updated. Name is now: {$server->name}\n";
});

ServerManager constructor.

Parameters

ParameterTypeDescription
$sharkordSharkordThe main bot instance.

Fetches the full administrative settings for the server via others.getSettings.

Returns a {@see \ServerSettings} model containing privileged fields such as secretToken and allowNewUsers that are not included in the public settings payload available on the {@see \Sharkord\Models\Server} model.

Requires the MANAGE_SETTINGS permission. The guard check runs locally before any network request is made, so callers receive a rejected Promise immediately rather than waiting for a round-trip API error.

Always performs a live API request — settings are not cached between calls.

Returns \PromiseInterface — Resolves with a {@see \ServerSettings} instance, rejects on failure.

Example

$sharkord->servers->getSettings()->then(function (\Sharkord\Models\ServerSettings $settings) {
echo "Name: {$settings->name}\n";
echo "Allow signup: " . ($settings->allowNewUsers ? 'Yes' : 'No') . "\n";
echo "DMs enabled: " . ($settings->directMessagesEnabled ? 'Yes' : 'No') . "\n";
if ($settings->logo) {
echo "Logo file: {$settings->logo->originalName}\n";
}
})->catch(function (\Throwable $e) {
// Fires immediately (no round-trip) if the bot lacks MANAGE_SETTINGS.
echo "Access denied: {$e->getMessage()}\n";
});

Retrieves the first (or only) server in the cache.

The preferred pattern for single-server bots.

Returns \Server|null — The first cached Server model, or null if the cache is empty.

Example

$server = $sharkord->servers->getFirst();
if ($server) {
echo "Connected to: {$server->name}\n";
}

Retrieves a server by ID.

Parameters

ParameterTypeDescription
$serverIdstringThe server ID.

Returns \Server|null — The cached Server model, or null if not found.

Example

$server = $sharkord->servers->get('abc123');
if ($server) {
echo "Server: {$server->name}\n";
}

Returns the underlying Servers collection.

Returns \ServersCollection

Example

foreach ($sharkord->servers->collection() as $id => $server) {
echo "{$id}: {$server->name}\n";
}