Skip to content
SQKo edited this page Dec 9, 2023 · 7 revisions

Channels can be either part of Guild (Server), Group Channel (Group Direct Message), User Private Channel (Direct Message).

use Discord\Parts\Channel\Channel;

A Channel can be either a text or voice channel on a Discord guild.

Requires GUILD intent

CHANNEL_CREATE

CHANNEL_DELETE

CHANNEL_UPDATE

CHANNEL_PINS_UPDATE


Create a Text Channel in the Guild

Your BOT must have manage_channels permission

You must first get the Guild object to use the code below

Create a text channel #mychannel with topic "Welcome to my channel!" with NSFW off:

$newchannel = $guild->channels->create([
    'name' => 'mychannel',
    // All other options are optional
    'type' => Channel::TYPE_TEXT,
    'topic' => 'Welcome to my channel!',
    'nsfw' => false,
    // more options in Docs
]);

$guild->channels->save($newchannel)->then(function (Channel $channel) {
    echo 'Created a new text channel - ID: ', $channel->id;
})->done();

https://discord.com/developers/docs/resources/guild#create-guild-channel

Get a Specific Channel

Change 123123123123123123 below with the Channel ID you want to retrieve

Cached, synchronous:

$channel = $discord->getChannel('123123123123123123');

You must first get the Guild object to use the code below

Cached, synchronous:

$channel = $guild->channels->get('id', '123123123123123123');

If the code above returns null, you may need to fetch it first (Promise, asynchronous):

$guild->channels->fetch('123123123123123123')->then(function (Channel $channel) {
    // ...
})->done();

https://discord.com/developers/docs/resources/channel#get-channel

Get channel from object

Channel can be also retrieved from related objects:

  • Message $channel = $message->channel; channel where the message is on
  • Invite $channel = $invite->channel; channel where the invite is made for
  • Webhook $channel = $webhook->channel; channel where the webhook belongs to

Send a Message to a Text Channel

Your BOT must have send_messages permission

Send "Hello world!" to the $channel:

$channel->sendMessage('Hello world!');

https://discord.com/developers/docs/resources/channel#create-message

Move a Member to a Voice Channel

Your BOT must have move_members permission

You must first get the Member object to use the code below

Move the $member to the $channel:

$channel->moveMember($member)->then(function () {
    // ...
})->done();

Or, change 123123123123123123 below with the Member ID:

$channel->moveMember('123123123123123123')->then(function () {
    // ...
})->done();

https://discord.com/developers/docs/resources/guild#modify-guild-member

Set Permissions of a Member in a Channel

Your BOT must have manage_roles permission

You must first get the Member object to use the code below

Set $member permission in the $channel to allow send messages, attach files, but deny add reaction:

$channel->setPermissions($member, [
    'send_messages',
    'attach_files',
], [
    'add_reactions',
])
->then(function () {
    // ...
})
->done();

https://discord.com/developers/docs/resources/channel#edit-channel-permissions

Update a Channel

You can update channel properties like the name, topic, nsfw, slowmode, etc

Your BOT must have manage_channels permission

You must first get the Guild and the Channel object to use the code below

Rename a text channel to #mycoolchannel:

$myChannel = $guild->channels->get('123123123123123123');

$myChannel->name = 'mycoolchannel';

$guild->channels->save($myChannel)->then(function (Channel $channel) {
    echo 'Renamed my channel to: ', $channel->name;
})->done();

https://discord.com/developers/docs/resources/channel#modify-channel

Delete a Channel

Your BOT must have manage_channels permission

You must first get the Guild object to use the code below. You can either use the Channel object or the snowflake ID

$guild->channels->delete('123123123123123123')->then(function (Channel $channel) {
    echo 'Deleted channel: ', $channel->name;
})->done();

https://discord.com/developers/docs/resources/channel#deleteclose-channel

Clone this wiki locally