Skip to content

CategoryManager

Class  ·  Sharkord\Managers\CategoryManager

Class CategoryManager

Manages category lifecycle events and exposes actions for creating, reordering, and fetching categories. Delegates all cache storage to a Categories collection instance.

Accessible via $sharkord->categories.

// Create a new category
$sharkord->categories->add('🎮 Gaming')->then(function(\Sharkord\Models\Category $category) {
echo "Created category '{$category->name}' (ID: {$category->id})\n";
});
// Rename an existing category
$sharkord->categories->get(3)?->edit('🏉 Footy Talk');
// Delete a category
$sharkord->categories->get(7)?->delete();
// Reorder all categories by their IDs
$sharkord->categories->reorder(1, 7, 3, 5);
$sharkord->on(\Sharkord\Events::CATEGORY_CREATE, function(\Sharkord\Models\Category $category): void {
echo "New category created: {$category->name}\n";
});

CategoryManager constructor.

Parameters

ParameterTypeDescription
$sharkordSharkordThe main bot instance.

Creates a new category on the server.

Sends the categories.add mutation, then fetches the full category data via categories.get to hydrate it into the local cache. The returned Category model is immediately usable.

Requires the MANAGE_CATEGORIES permission.

Parameters

ParameterTypeDescription
$namestringThe name for the new category.

Returns \PromiseInterface — Resolves with the new Category model, rejects on failure.

Example

$sharkord->categories->add('🎮 Gaming')->then(function(\Sharkord\Models\Category $category) {
echo "Created '{$category->name}' at position {$category->position}\n";
});

Returns the cached category IDs sorted by their current position.

Useful as a starting point before calling reorder() — retrieve the current order, move items around, then pass the result straight in.

Returns int[] — Category IDs in ascending position order.

Example

// Get current order, move the last category to the front
$ids = $sharkord->categories->getOrder();
array_unshift($ids, array_pop($ids));
$sharkord->categories->reorder(...$ids);

Reorders all categories on the server by providing a full ordered list of IDs.

The server will push a categories.onUpdate event for each affected category. All cached models are updated automatically via the subscription handler.

The provided list must contain every existing category ID — omitting an ID may result in undefined server behaviour.

Requires the MANAGE_CATEGORIES permission.

Parameters

ParameterTypeDescription
...$categoryIdsintThe category IDs in the desired display order.

Returns \PromiseInterface — Resolves with true on success, rejects on failure.

Throws

  • \InvalidArgumentException — If no category IDs are provided, if duplicate IDs are present, or if the number of IDs does not match the number of cached categories.

Example

// Move category 7 to the second position
$sharkord->categories->reorder(1, 7, 3, 5)->then(function() {
echo "Categories reordered.\n";
});

Fetches fresh data for a category directly from the server.

The returned model is hydrated into the local cache. Useful when you need guaranteed up-to-date data for a specific category without waiting for a subscription event.

Parameters

ParameterTypeDescription
$idintThe category ID to fetch.

Returns \PromiseInterface — Resolves with the Category model, rejects on failure.

Example

$sharkord->categories->fetch(3)->then(function(\Sharkord\Models\Category $category) {
echo "Fetched: {$category->name} (position {$category->position})\n";
});

Retrieves a category by ID from the local cache.

Parameters

ParameterTypeDescription
$idintThe category ID.

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

Example

$category = $sharkord->categories->get(3);
if ($category) {
echo "Found category: {$category->name}\n";
}

Returns the underlying Categories collection.

Returns \CategoriesCollection

Example

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