Example 1

Send a Message to Discord

circle-info

SendMessage API is called inside the bot event listener

Can be also called outside of the listener

This example demonstrates on how to use it within the listener

circle-exclamation
<?php
require('vendor/autoload.php');

use \HobsRkm\SDK\PHPDiscordSDK\PHPDiscordSDKFactory;
use \HobsRkm\SDK\PHPDiscordSDK\Actions\Channels;

PHPDiscordSDKFactory::getInstance()
    ->botConnect("<<<<BOT TOKEN><<<<")
    ->then(
        function ($bot) {
            $bot->on('message', function ($event) {
                PHPDiscordSDKFactory::getInstance()
                ->formatEvent($event)->then(function($message){
                //Bot Event Listener
                //Server details
                print_r($message);
                /**call Message API**/
                $body = array(
                        "TYPE"=>"CHANNEL_MESSAGE",
                        "body"=>array(
                            "channel_id"=><<YOUR CHANNEL ID>>>,
                            "content"=>"Test Message"
                            )
                        );
                PHPDiscordSDKFactory::getInstance('Messages')
                    ->sendMessage($body)
                    ->then(function($data){
                        //Sent message details, including message id
                        print_r($data);
                    }, 
                    function ($error) {
                        //message event errors
                        print_r($error->getMessage());
                    });
                }, function ($reason) {
                //message event errors
            });
        });
    },
    function ($reason) {
    //other errors, bot startup, authentication
    }
);

Last updated

Was this helpful?