Skip to content

InviteManager

Class  ·  Sharkord\Managers\InviteManager

Class InviteManager

Manages server invite links via the Sharkord API.

Accessible via $sharkord->invites.

Invite codes may be generated automatically or supplied manually. Use getAll() to list and cache existing invites, and create() to produce new ones. Deletion is handled on the {@see \Invite} model itself via $invite->delete().

// List all existing invites
$sharkord->invites->getAll()->then(function (array $invites) {
foreach ($invites as $invite) {
echo "{$invite->code} — created by {$invite->creator?->name}\n";
}
});
// Create an unlimited, non-expiring invite for a specific role
$sharkord->invites->create(roleId: 2)->then(function (\Sharkord\Models\Invite $invite) {
echo "New invite code: {$invite->code}\n";
});
// Create a 10-use invite with a specific code and expiry
$sharkord->invites->create(
maxUses: 10,
expiresAt: strtotime('+7 days') * 1000,
code: 'my.custom.code',
)->then(function (\Sharkord\Models\Invite $invite) {
echo "Invite expires at: " . date('Y-m-d', (int) ($invite->expiresAt / 1000)) . "\n";
});

InviteManager constructor.

Parameters

ParameterTypeDescription
$sharkordSharkordThe main bot instance.

Fetches all server invites from the API and refreshes the local cache.

The cache is cleared and repopulated on each call to ensure it reflects the current state of invites on the server.

Returns \PromiseInterface — Resolves with an array of {@see \Invite} objects, rejects on failure.

Example

$sharkord->invites->getAll()->then(function (array $invites) {
echo count($invites) . " invite(s) active.\n";
foreach ($invites as $invite) {
$uses = $invite->maxUses !== null ? "{$invite->uses}/{$invite->maxUses}" : "{$invite->uses}/∞";
$expiry = $invite->expiresAt !== null
? date('Y-m-d', (int) ($invite->expiresAt / 1000))
: 'never';
echo " [{$invite->code}] — used {$uses} — expires {$expiry}\n";
}
});

Creates a new server invite via the invites.add RPC.

If no $code is provided a cryptographically random 24-character alphanumeric code is generated automatically.

Parameters

ParameterTypeDescription
$maxUses (optional)intMaximum number of uses. Pass 0 for unlimited.
$expiresAt (optional)?intExpiry timestamp in milliseconds, or null for no expiry.
$code (optional)?stringA custom invite code, or null to auto-generate one.
$roleId (optional)?intID of the role to assign on join, or null for the server default.

Returns \PromiseInterface — Resolves with the new {@see \Invite} object, rejects on failure.

Example

// Auto-generated code, unlimited uses, no expiry, default role
$sharkord->invites->create()->then(function (\Sharkord\Models\Invite $invite) {
echo "Created: {$invite->code}\n";
});
// Custom code, 5 uses, expires in 24 hours, specific role
$sharkord->invites->create(
maxUses: 5,
expiresAt: (time() + 86400) * 1000,
code: 'welcome.2026',
roleId: 3,
)->then(function (\Sharkord\Models\Invite $invite) {
echo "Invite ready: {$invite->code}\n";
});

Retrieves a cached invite by ID without making an API request.

Returns null if the invite is not in the local cache. Call getAll() first to populate the cache if needed.

Parameters

ParameterTypeDescription
$idintThe invite ID.

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

Example

$sharkord->invites->getAll()->then(function () use ($sharkord) {
$invite = $sharkord->invites->get(5);
if ($invite) {
echo "Found: {$invite->code}\n";
}
});

Returns the underlying Invites collection.

Returns \InvitesCollection

Example

foreach ($sharkord->invites->collection() as $id => $invite) {
echo "{$id}: {$invite->code}\n";
}