PHP code example of buzzmoody / sharkordbot

1. Go to this page and download the library: Download buzzmoody/sharkordbot library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

buzzmoody / sharkordbot example snippets




	declare(strict_types=1);
	
	error_reporting(E_ALL);

	harkord\Models\User;
	use Sharkord\Events;

	/*
	* Supports pulling environment variables from .env file as well as Docker container.
	* Hardcode your values at your own peril
	* Example uses SHARKORD_IDENTITY, SHARKORD_PASSWORD and SHARKORD_HOST env vars.
	*/
	$sharkord = new Sharkord(
		config: [
			'identity' 	=> $_ENV['SHARKORD_IDENTITY'] ?? 'your-username',
 			'password'	=> $_ENV['SHARKORD_PASSWORD'] ?? 'your-password',
 			'host'		=> $_ENV['SHARKORD_HOST'] ?? 'server.example.com',
		],
		logLevel: 'Notice',
		reconnect: true,
		maxReconnectAttempts: 5	
	);
	
	/*
	* If you want to use dynamically loaded commands as per the examples directory
	* uncomment the below along with the preg_match if statement further down
	*/
	# $sharkord->commands->loadFromDirectory(__DIR__ . '/Commands');

	$sharkord->on(Events::READY, function(User $bot) use ($sharkord) {
 		$sharkord->logger->notice("Logged in as {$bot->name} and ready to chat!");
	});

	$sharkord->on(Events::MESSAGE_CREATE, function(Message $message) use ($sharkord) {
		
		$sharkord->logger->notice(sprintf(
			"[#%s] %s: %s",
			$message->channel->name,
			$message->author->name,
			$message->content
		));
		
		/*
		* Uncomment if you're using dynamically loaded Commands.
		* Make sure to delete the ping/pong if statement.
		*/
		# if (preg_match('/^!([a-zA-Z]{2,})(?:\s+(.*))?$/', $message->content, $matches)) {
		#	 $sharkord->commands->handle($message, $matches);
		# }
		
		if ($message->content == '!ping') $message->reply('Pong!');
		
	});

	$sharkord->run();




	use Sharkord\Commands\CommandInterface;
	use Sharkord\Models\Message;
	use Sharkord\Sharkord;

	/**
	 * Class Ping
	 *
	 * A simple command to check if the bot is responsive.
	 * Responds with "Pong!" when invoked.
	 *
	 * @package Sharkord\Commands
	 */
	class Ping implements CommandInterface {
		
		private const RESPONSES = [
			"Pong! Right back at ya.",
			"Ping received. Pong!",
			"Got it!",
			"Ping received, initiating pong sequence... Pong!",
			"Did someone say ping? Pong!",
			"You rang? Pong!",
			"Copy that. Pong!",
			"The answer is always... pong."
		];

		/**
		 * @inheritDoc
		 */
		public function getName(): string {
			return 'ping';
		}

		/**
		 * @inheritDoc
		 */
		public function getDescription(): string {
			return 'Responds with Pong!';
		}
		
		/**
		 * @inheritDoc
		 */
		public function getPattern(): string {
			return '/^ping$/';
		}

		/**
		 * @inheritDoc
		 */
		public function handle(Sharkord $sharkord, Message $message, string $args, array $matches): void {
			$message->reply(self::RESPONSES[array_rand(self::RESPONSES)]);
		}

	}

bash
php bot.php