Untitled

 avatar
unknown
php
2 years ago
6.3 kB
6
Indexable
<?php

namespace Services;

class Discordz
{

    /**
     * @var \GuzzleHttp\Client $client
     */

    public $client;

	/**
	 * @var array $config
	 */

	public $config;

	/** @var string file json guild id & channel id */

	public $guild_channel_file = ROOT_PATH . '/discord.json';

	/** @var int current index of channel id */

	public $current_index = 0;

    public function __construct() {
		if (!file_exists($this->guild_channel_file)) {
			$this->autoGenerate();
		}


		$this->config = [
			'TOKEN' => 'Mzk0MDE3MDczNjA2Njg4NzY4.YWuuCA.lu1SLvt5q2Dx6Si4Xz0ZqytM8jc',
			'CHANNELS' => json_decode(file_get_contents($this->guild_channel_file), true),
		];

		$channel_ids = [];

		foreach ($this->config['CHANNELS'] as $channel_id) {
			$channel_ids = array_merge($channel_ids, $channel_id);
		}

		$this->config['CHANNEL_IDS'] = $channel_ids;
		$this->current_index = array_rand($this->config['CHANNEL_IDS']);


		$this->client = new \GuzzleHttp\Client([
			'headers' => [
				'authorization' => $this->config['TOKEN'],
				'Content-Type' => 'multipart/form-data',
			]
		]);
    }
	/**
	 * @param mixed $file_path
	 * @param mixed $content
	 * @return mixed
	 */
	public function put($file_path, $content) {
		// $this->client->request("https://discord.com/api/v8/channels/{$this->config['CHANNEL_ID']}/messages", [
			
		// ]);
	}
	
	/**
	 *
	 * @param mixed $files
	 * @return mixed
	 */
	public function multiPut($files) {
		// multi put asyn 
		$promises = [];
		$urls = [];


		foreach ($files as $file_path => $content) {
			$CHANNEL_ID = $this->getChannelId();

			$promises[] = $this->client->requestAsync('POST', "https://discord.com/api/v9/channels/{$CHANNEL_ID}/messages", [
				'multipart' => [
					[
						'name' => 'file',
						'contents' => $content,
						'filename' => $file_path
					]
				]
			]);
		}

		$eachPromise = new \GuzzleHttp\Promise\EachPromise($promises, [
			'concurrency' => 5,
			'fulfilled' => function ($response, $index) use (&$urls) {
				// this is delivered each successful response
				$urls[$index] = $this->responseToURL($response);
			}, 
			'rejected' => function ($reason, $index) {
				// this is delivered each failed request
				echo "Error: ". $reason->getMessage() . PHP_EOL;
			}	
		]);


		$eachPromise->promise()->wait();
		ksort($ids);

		return $urls;
	}


	function responseToURL($response) {
		$body = $response->getBody();
		$body = json_decode($body, true);
		$attachments = $body['attachments'];
		$attachment = $attachments[0];
		$url = $attachment['url'];

		return $url;
	}

	function generateDiscordChannel($guild_id){
		// Random name
		$channel_name = 'channel-' . time() . '-' . rand(0, 999999);
		$channel = $this->client->request('POST', "https://discord-api.b-cdn.net/api/v9/guilds/$guild_id/channels", [
			'json' => [
				'name' => $channel_name, // random name
				'type' => 0,
			]
		]);

		$channel = json_decode($channel->getBody(), true);

		return $channel['id'];	
	}

	function generateDiscordGuild(){
		// Random name
		$guild_name = 'guild-' . time() . '-' . rand(0, 999999);
		$guild = $this->client->request('POST', 'https://discord.com/api/v9/guilds', [
			'json' => [
				'name' => $guild_name, // random name
			]
		]);

		$guild = json_decode($guild->getBody(), true);

		return $guild['id'];
	}

	function autoGenerate(){
		$data = $this->config['CHANNELS'];

		// generate 10 guild
		for ($k = 0; $k < 10; $k++) {
			$guild_id = $this->generateDiscordGuild();
			echo "Guild ID: $guild_id \r\n";

			// 1 guild = 10 channel
			for ($i = 0; $i < 10; $i++) {
				$data[$guild_id][] = $this->generateDiscordChannel($guild_id);

				// sleep 1s
				sleep(1);
				echo "Channel ID: {$data[$guild_id][$i]} \r\n";
			}
		}
		
		// save to json
		$json = json_encode($data, JSON_PRETTY_PRINT);
		file_put_contents($this->guild_channel_file, $json);
	}


	function generateDiscordWebhook($channel_id, $guild_id){
		// read the contents of the guild_channel_file file
		$guild_channel_data = file_get_contents($this->guild_channel_file);
		// parse the JSON data into a PHP array
		$guild_channel_data = json_decode($guild_channel_data, true);
	  
		// check if the channel already has 15 webhooks
		$webhooks = $this->client->request('GET', "https://discord.com/api/v9/channels/$channel_id/webhooks");
		$webhooks = json_decode($webhooks->getBody(), true);
		if(count($webhooks) >= 15){
		  // if the channel already has 15 webhooks, get the channel_id value from the guild_channel_data array
		  $channel_id = $guild_channel_data['channel_id'];
		}
	  
		$webhook = $this->client->request('POST', "https://discord.com/api/v9/channels/$channel_id/webhooks", [
		  'json' => [
			'name' => 'webhook-' . time() . '-' . rand(0, 999999), // random name
			'guild_id' => $guild_id,
		  ]
		]);
	  
		$webhook = json_decode($webhook->getBody(), true);
	  
		// return full webhook URL
		return "https://discord.com/api/webhooks/{$webhook['id']}/{$webhook['token']}";
	  }
		

	  function autoGenerateWebhook(){
		// Set the path to the webhooks file
		$webhook_file = ROOT_PATH . '/webhooks.txt';
	  
		// Get the guilds and channels from the config
		$guilds = $this->config['CHANNELS'];
		$webhooks = [];
	  
		// Iterate over the guilds and channels
		foreach ($guilds as $guild_id => $channels) {
		  foreach ($channels as $channel_id) {
			// Create 15 webhooks for each channel
			for ($i = 0; $i < 15; $i++) {
			  // Generate the webhook URL
			  $webhook_url = $this->generateDiscordWebhook($channel_id, $guild_id);
			  $webhooks[$guild_id][$channel_id][] = $webhook_url;
	  
			  // Echo the webhook URL
			  echo $webhook_url . "\n";
	  
			  // Append the webhook URL to the file
			  file_put_contents($webhook_file, $webhook_url . "\n", FILE_APPEND);
	  
			  // Sleep for 1 second
			  sleep(10);
			}
		  }
		}
	  }
				
	/**
	 * @return string | int channel id
	 */
	function getChannelId(){		
		if ($this->current_index >= count($this->config['CHANNEL_IDS'])) {
			$this->current_index = 0;
		}


		return $this->config['CHANNEL_IDS'][$this->current_index++];
	}
}
Editor is loading...