phpdiscordsdk/phpdiscordsdk

A Light Weight PHP Discord SDK Library to build Discord Bots with Web Panel

v1.1 2021-09-12 16:13 UTC

This package is auto-updated.

Last update: 2024-04-15 18:58:57 UTC


README

build Codacy Security Scan

PHPDiscordSDK

A Light Weight PHP Discord SDK Library to build Discord Bots with Web Panel

PHPDiscordSDK

A DiscordSDK to build Discord Bots using PHP, with Web Panel.

  1. Installation
  2. Getting Started
  3. Listening to Bot Events
  4. APIs
    1. SendMessage
    2. GetChannelDetails
    3. UpdateChannelDetails
    4. DeletChannel
    5. Activity
    6. Visit documentation site for complete list

Documentation

Complete Docs of PHPDiscordSDK

API Demo with Web Panel Integration

discordapidemo.com

Installation

  • Requires PHP >=7.0
composer require phpdiscordsdk/phpdiscordsdk

Getting Started

To start the bot you need to start it using CLI (Command Line)

Once the bot is up and running, you can start using the APIs

You can also use the APIs outside of bot event listener , but it is recomended to first start the bot using CLI before consuming the APIs

If you want to integerate into a framework and communicate with bot you can implement something like Web Panel

See Example (Web Panel) (Supports both Send and Recieve)

Start the bot

$ php bot.php

bot.php

<?php

require('vendor/autoload.php');

use \HobsRkm\SDK\PHPDiscordSDK\PHPDiscordSDKFactory;

class Bot {

    public function start() {
        PHPDiscordSDKFactory::getInstance()
            ->botConnect(<<<<BOT TOKEN>>>>>)
            ->then(
                function ($bot) {
                    $bot->on('message', function ($event) {
                        PHPDiscordSDKFactory::getInstance()
                        ->formatEvent($event)->then(function($message){
                        //Bot Event Listener
                        //call other APIs
                            print_r($message);//prints server details
                        }, function ($reason) {
                        //message event errors
                    });
                     $bot->on('close', function ($event) {
                       $this->start();
                    });
                });
            },
            function ($reason) {
            //other errors, bot startup, authentication
            }
        );
    }
}

$bot = new Bot();
$bot->start();

bot.php

Example 1 - Send Message

Calling APIs inside the Bot Listener ( Send Message to Discord)

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

use \HobsRkm\SDK\PHPDiscordSDK\PHPDiscordSDKFactory;

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
	}
);

Example 2 - Get Channel Messages

Calling APIs From Anywhere outside of listener( Get Channel Messages)

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

use \HobsRkm\SDK\PHPDiscordSDK\PHPDiscordSDKFactory;
  
/**
Required Parameters
**/
$body = array(
        "TYPE"=>"CHANNEL_DETAILS",
        "body"=>array(
            "channel_id"=><<YOUR CHANNEL ID>>>,
            )
        );

PHPDiscordSDKFactory::getInstance('Channels')
	->getChannelMessages($body)
	->then(function($data){
		//Channel Messages
		print_r($data);
	}, 
	function ($error) {
		//message event errors
		print_r($error->getMessage());
		//echo $error->getMessage();	
	});

Important

The following APIs cannot be called outside of Listener

  • Presence: This is used to Update your bot status such as
    • Online
    • DND
    • AFK
    • Actvity - Streaming, Playing or Watching Status

APIs

The bot should have necessary Permissions on Discord Server

Ex: Send message requires Send Message Perms on a channel

For more information on specific permission, check the response body on each APIs.

API Demo with Web Panel Integration

discordapidemo.com

SendMessage

    require('vendor/autoload.php');

    use \HobsRkm\SDK\PHPDiscordSDK\PHPDiscordSDKFactory;
	
    /**
    Required - Channel Id
    Content - You message, embeds supported - see Example Folder	
    **/
    $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());
        });
	

GetChannelDetails

    require('vendor/autoload.php');

    use \HobsRkm\SDK\PHPDiscordSDK\PHPDiscordSDKFactory;
	
    /**
    Required - Channel Id
    **/
    $body = array(
    "TYPE"=>"CHANNEL_DETAILS",
    "body"=>array(
        "channel_id"=><<YOUR CHANNEL ID>>>,
        )
    );
    PHPDiscordSDKFactory::getInstance('Channels')
        ->getChannelDetails($body)
        ->then(function($data){
            //channel details
            print_r($data);
        }, 
        function ($error) {
            //message event errors
            print_r($error->getMessage());
        });

DeleteChannel

    require('vendor/autoload.php');

    use \HobsRkm\SDK\PHPDiscordSDK\PHPDiscordSDKFactory;
	
    /**
    Required - Channel Id
    **/
    $body = array(
    "TYPE"=>"CHANNEL_DETAILS",
    "body"=>array(
        "channel_id"=><<YOUR CHANNEL ID>>>,
        )
    );
    PHPDiscordSDKFactory::getInstance('Channels')
        ->deleteChannel($body)
        ->then(function($data){
            //channel details
            print_r($data);
        }, 
        function ($error) {
            //message event errors
            print_r($error->getMessage());
        });

UpdateChannelDetails

    require('vendor/autoload.php');

    use \HobsRkm\SDK\PHPDiscordSDK\PHPDiscordSDKFactory;
	
	/**
	Required - Channel Id
	Name - New channel Name
	icon - base64 encoded icon
	**/
	$body = array(
	"TYPE"=>"CHANNEL_DETAILS",
	"body"=>array(
		"channel_id"=><<YOUR CHANNEL ID>>>,
		"name"=><<YOUR NEW CHANNEL NAME>>,
		"icon"=><<<BASE 64 ENCODED ICON>>
		)
	);
	PHPDiscordSDKFactory::getInstance('Channels')
		->updateChannelDetails($body)
		->then(function($data){
			//channel details
			print_r($data);
		}, 
		function ($error) {
			//message event errors
			print_r($error->getMessage());
		});

Presence

Cannot be used outside of bot listener events, has to be used inside the startup script

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

use \HobsRkm\SDK\PHPDiscordSDK\PHPDiscordSDKFactory;

PHPDiscordSDKFactory::getInstance()
            ->botConnect("<<<<BOT TOKEN><<<<")
            ->then( 
                function ($bot) {
                    $bot->on('message', function ($event) {
                        PHPDiscordSDKFactory::getInstance()->formatEvent($event)->then(function($message){
                         //All events sent from client will be available here, including the server details the bot is listening on
                        /**
                        call Presence API
                        The change is not instant, discord has a delay to update your status not to often
                        status - dnd | offline |online | afk
                        type - one of PLAYING,STREAMING,
                        LISTENING,WATCHING,COMPETING
                        **/
                        PHPDiscordSDKFactory::getInstance('Presence')
                        ->setActivity(
                            array(
                                "activity"=>"Playing CS:GO",
                                "status"=>"dnd",
                                "type"=>'PLAYING'
                                )
                            );                        
                        }, function ($reason) {
                            //message event errors
                            print_r($reason->getMessage());
                            //echo $reason->getMessage();
                        });
                    });
                },
                function ($reason) {
                    print_r($reason);
                    //other errors, bot startup, authentication
                }
            );

Documentation

Complete Docs of PHPDiscordSDK

Web Panel

discordapidemo.com

Project is Open for contributions , please feel free to fork

Contribution Guide

  • All Pull requests should be from a Local Branch of Develop
  • Be sure to check out Develop Branch
  • Any other pull requests to master or other branches will be rejected

©MIT License