Channel
Class · Sharkord\Models\Channel
Class Channel
Represents a chat channel on the server.
Properties
Section titled “Properties”| Property | Type | Description |
|---|---|---|
$category (read-only) | `\Category | null` |
Methods
Section titled “Methods”__construct()
Section titled “__construct()”Channel constructor.
Parameters
| Parameter | Type | Description |
|---|---|---|
$sharkord | Sharkord | Reference to the main bot instance. |
$rawData | array | The raw array of data from the API. |
static fromArray()
Section titled “static fromArray()”Factory method to create a Channel from raw API data.
Parameters
| Parameter | Type | Description |
|---|---|---|
$raw | array | The raw channel data from the server. |
$sharkord | Sharkord | Reference to the main bot instance. |
Returns self
sendMessage()
Section titled “sendMessage()”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
| Parameter | Type | Description |
|---|---|---|
$message | `string | MessageBuilder` |
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);sendTyping()
Section titled “sendTyping()”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";});sendTypingWhile()
Section titled “sendTypingWhile()”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
| Parameter | Type | Description |
|---|---|---|
$promise | PromiseInterface | The 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}");});markAsRead()
Section titled “markAsRead()”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();uploadFile()
Section titled “uploadFile()”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
| Parameter | Type | Description |
|---|---|---|
$filePath | string | The absolute or relative path to the file to upload. |
$mimeType (optional) | ?string | MIME 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"; });fetch()
Section titled “fetch()”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";});edit()
Section titled “edit()”Edits this channel’s name, topic, and/or visibility.
Requires the MANAGE_CHANNELS permission.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The new channel name. |
$topic (optional) | ?string | The new channel topic, or null to leave it unchanged. |
$private (optional) | ?bool | Whether 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);delete()
Section titled “delete()”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";});getPermissions()
Section titled “getPermissions()”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"; }});addRolePermission()
Section titled “addRolePermission()”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
| Parameter | Type | Description |
|---|---|---|
$roleId | int | The 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, );});setRolePermissions()
Section titled “setRolePermissions()”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
| Parameter | Type | Description |
|---|---|---|
$roleId | int | The ID of the role to update. |
...$permissions | ChannelPermissionFlag | One 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,);removeRolePermission()
Section titled “removeRolePermission()”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
| Parameter | Type | Description |
|---|---|---|
$roleId | int | The 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";});toArray()
Section titled “toArray()”Returns all the attributes as a plain array. Useful for debugging.
Returns array
Example
var_dump($channel->toArray());__isset()
Section titled “__isset()”Magic isset check. Allows isset() and empty() to work correctly against both stored attributes and virtual relational properties.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | Property name. |
Returns bool
__get()
Section titled “__get()”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
| Parameter | Type | Description |
|---|---|---|
$name | string | Property name. |
Returns mixed