Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/Discord/Repository/Channel/MessageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@

namespace Discord\Repository\Channel;

use Discord\Builders\MessageBuilder;
use Discord\Http\Endpoint;
use Discord\Http\Exceptions\NoPermissionsException;
use Discord\Parts\Channel\Channel;
use Discord\Parts\Channel\Message;
use Discord\Repository\AbstractRepository;
use React\Promise\PromiseInterface;

use function React\Promise\reject;

/**
* Contains messages sent to a channel.
Expand Down Expand Up @@ -57,4 +63,43 @@ public function __construct($discord, array $vars = [])
unset($vars['thread_id']); // For thread
parent::__construct($discord, $vars);
}

/**
* Attempts to create a message in a channel.
*
* @since 10.41.0
*
* @link https://discord.com/developers/docs/resources/message#create-message
*
* @param Channel|string $channel Channel ID or Channel object.
* @param MessageBuilder $message MessageBuilder instance.

* @return PromiseInterface<Message>
*/
public function build($channel, MessageBuilder $message): PromiseInterface
{
if (! is_string($channel)) {
if (method_exists($channel, 'getBotPermissions')) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should just be checking whether or not the object is an instance of a channel

$botperms = $channel->getBotPermissions();
if ($botperms && ! $botperms->send_messages) {
return reject(new NoPermissionsException("You do not have permission to send messages in channel {$channel->id}."));
}
}
$channelId = $channel->id;
} else {
$channelId = $channel;
}

$endpoint = Endpoint::bind(Endpoint::CHANNEL_MESSAGES, $channelId);

if ($message->requiresMultipart()) {
$multipart = $message->toMultipart();

return $this->http->post($endpoint, (string) $multipart, $multipart->getHeaders())
->then(fn ($response) => $this->factory->part($this->class, (array) $response, true));
}

return $this->http->post($endpoint, $message)
->then(fn ($response) => $this->factory->part($this->class, (array) $response, true));
}
}