Skip to content

Channel

Class  ·  Sharkord\Models\Channel

Class Channel

Represents a chat channel on the server.

PropertyTypeDescription
$category (read-only)`\Categorynull`

Channel constructor.

Parameters

ParameterTypeDescription
$sharkordSharkordReference to the main bot instance.
$rawDataarrayThe raw array of data from the API.

Factory method to create a Channel from raw API data.

Parameters

ParameterTypeDescription
$rawarrayThe raw channel data from the server.
$sharkordSharkordReference to the main bot instance.

Returns self


Sends a message to this channel.

Accepts either a plain text string or a {@see \MessageBuilder} instance. When a builder is provided, each queued file is read synchronously and then all HTTP uploads are dispatched concurrently. If the builder has no queued files, the upload step is skipped entirely.

Parameters

ParameterTypeDescription
$message`stringMessageBuilder`

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

Example

// Plain text
$sharkord->channels->get('general')->sendMessage('Hello, world!');
// With attachments via MessageBuilder
$builder = \Sharkord\Builders\MessageBuilder::create()
->setContent('Here are your files!')
->addFile('/tmp/photo.jpg')
->addFile('/tmp/report.pdf');
$sharkord->channels->get('media')->sendMessage($builder);

Sends a single typing indicator signal to this channel.

The indicator will be visible to other users for approximately 800ms. For longer operations, use sendTypingWhile() instead.

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

Example

$channel->sendTyping()->then(function() {
echo "Typing indicator sent.\n";
});

Sends a repeating typing indicator for the duration of a pending Promise.

Fires a typing signal immediately and then every 700ms until the given Promise resolves or rejects, ensuring the indicator stays visible throughout. The result or rejection of the given Promise is passed through transparently.

Parameters

ParameterTypeDescription
$promisePromiseInterfaceThe operation to show a typing indicator for.

Returns \PromiseInterface — Resolves or rejects with the same value as the given Promise.

Example

// Show typing while fetching data from an external API
$channel->sendTypingWhile(
$httpClient->get('https://api.example.com/data')
)->then(function($response) use ($channel) {
$channel->sendMessage("Got the data: {$response}");
});

Marks all messages in this channel (or DM thread) as read.

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

Example

$sharkord->channels->get('general')->markAsRead();

Uploads a local file to the Sharkord storage endpoint.

Reads the file at $filePath synchronously and posts it as a raw binary stream to /upload. Resolves with the server-assigned file UUID, which is then queued internally by {@see \Sharkord\Builders\MessageBuilder} when building a message with attachments.

The MIME type is detected automatically via mime_content_type() when omitted. If detection fails, application/octet-stream is used as a safe fallback.

Note: file_get_contents() is synchronous and will block the event loop for the duration of the read. Avoid uploading very large files without considering the impact on other pending operations.

Parameters

ParameterTypeDescription
$filePathstringThe absolute or relative path to the file to upload.
$mimeType (optional)?stringMIME type override. Detected automatically when null.

Returns \PromiseInterface — Resolves with the file UUID string, rejects on failure.

Example

// Upload a file and retrieve its UUID for use elsewhere.
$sharkord->channels->get('media')
->uploadFile('/tmp/screenshot.png')
->then(function(string $fileId) {
echo "File ID: {$fileId}\n";
});

Fetches the latest channel data from the server and updates the local cache.

Useful after making changes to a channel to confirm the server’s current state. The cached Channel model is updated in-place; any existing references remain valid.

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

Example

$channel->edit('Renamed Channel')->then(function() use ($channel) {
return $channel->fetch();
})->then(function(\Sharkord\Models\Channel $channel) {
echo "Server confirms name: {$channel->name}\n";
});

Edits this channel’s name, topic, and/or visibility.

Requires the MANAGE_CHANNELS permission.

Parameters

ParameterTypeDescription
$namestringThe new channel name.
$topic (optional)?stringThe new channel topic, or null to leave it unchanged.
$private (optional)?boolWhether the channel should be private, or null to leave it unchanged.

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

Example

// Rename and set a topic
$sharkord->channels->get('general')->edit('general', 'Welcome to general chat!');
// Make a channel private
$sharkord->channels->get('staff')->edit('staff', null, true);
// Rename, set a topic, and make private in one call
$sharkord->channels->get('announcements')->edit('news', 'Official news feed.', true);

Permanently deletes this channel from the server.

Requires the MANAGE_CHANNELS permission. The channel is removed from the local cache once the server emits the corresponding channeldelete event.

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

Example

$sharkord->channels->get('old-channel')->delete()->then(function() {
echo "Channel deleted.\n";
});

Retrieves all current role and user permission entries for this channel.

Requires the MANAGE_CHANNEL_PERMISSIONS permission.

Resolves with an associative array containing:

  • rolePermissions — array of {@see \ChannelRolePermission} objects, one per flag per role.
  • userPermissions — raw array of user-level overrides (not yet modelled).

Returns \PromiseInterface — Resolves with array{rolePermissions: ChannelRolePermission[], userPermissions: array}, rejects on failure.

Example

$channel->getPermissions()->then(function(array $permissions) {
foreach ($permissions['rolePermissions'] as $entry) {
$state = $entry->allow ? 'allow' : 'deny';
echo "Role {$entry->roleId} — {$entry->permission->value}: {$state}\n";
}
});

Adds a role to this channel’s permission set, initialising all flags at their defaults.

This must be called before setRolePermissions() when granting a role access to a channel for the first time. Subsequent permission changes should use setRolePermissions().

Requires the MANAGE_CHANNEL_PERMISSIONS permission.

Parameters

ParameterTypeDescription
$roleIdintThe ID of the role to add.

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

Example

$channel->addRolePermission(2)->then(function() use ($channel) {
return $channel->setRolePermissions(
2,
\Sharkord\ChannelPermissionFlag::VIEW_CHANNEL,
\Sharkord\ChannelPermissionFlag::SEND_MESSAGES,
);
});

Sets the allowed permission flags for a role on this channel.

Replaces the role’s current permission set with the provided flags. Any flag not included in the list will be set to denied. The role must already have been added via addRolePermission() before calling this method.

Requires the MANAGE_CHANNEL_PERMISSIONS permission.

Parameters

ParameterTypeDescription
$roleIdintThe ID of the role to update.
...$permissionsChannelPermissionFlagOne or more permission flags to allow.

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

Example

$channel->setRolePermissions(
2,
\Sharkord\ChannelPermissionFlag::VIEW_CHANNEL,
\Sharkord\ChannelPermissionFlag::SEND_MESSAGES,
\Sharkord\ChannelPermissionFlag::JOIN,
);

Removes a role entirely from this channel’s permission set.

All per-flag entries for the role are deleted. Requires the MANAGE_CHANNEL_PERMISSIONS permission.

Parameters

ParameterTypeDescription
$roleIdintThe ID of the role to remove.

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

Example

$channel->removeRolePermission(2)->then(function() {
echo "Role removed from channel.\n";
});

Returns all the attributes as a plain array. Useful for debugging.

Returns array

Example

var_dump($channel->toArray());

Magic isset check. Allows isset() and empty() to work correctly against both stored attributes and virtual relational properties.

Parameters

ParameterTypeDescription
$namestringProperty name.

Returns bool


Magic getter. Triggered when accessing any property not explicitly defined.

Virtual properties:

  • $channel->category Returns the resolved Category via CategoryManager.

Any other name is looked up directly in the raw attributes array.

Parameters

ParameterTypeDescription
$namestringProperty name.

Returns mixed