divinity76 / msgme

cli tool to send a message.
The Unlicense
6 stars 1 forks source link

add Discord relay #2

Open divinity76 opened 7 years ago

divinity76 commented 7 years ago

very crude code i made for playing with discord API (which works, just replace the IJIFJQIUUIJFQFAKEFAKEFAKEFAKE.Cwm9rQ.IJIFJQIUUIJFQFAKEFAKEFAKEFAKE with a real token, and 209972668449947648 with the channel you're interested in) :

<?php
declare(strict_types = 1);
require_once ('hhb_.inc.php');
define ( 'BASE_URL', 'https://discordapp.com/api', true );
$dbot = new DiscordBot ();
$dbot->postMessage ( "@takeoded#1532 test" );
$dbot->postMessage ( "@takeoded test" );

class DiscordBot {
    private $hc = NULL;
    private $data;
    function __construct() {
        $hc = &$this->hc;
        $hc = new hhb_curl ();
        $hc->_setComfortableOptions ();
        $hc->setopt_array ( array (
                CURLOPT_TIMEOUT => 18,
                CURLOPT_CONNECTTIMEOUT => 17,
                CURLOPT_USERAGENT => 'trolling (http://shadowfied.com/ , 1.0)'
        ) );

        $postjson = json_encode ( array (
                'name' => 'wat',
                'avatar' => file_get_contents ( 'avatar.b64' )
        ) );

        $hc->setopt_array ( array (
                CURLOPT_POST => true,
                CURLOPT_HTTPHEADER => array (
                        'Authorization: Bot IJIFJQIUUIJFQFAKEFAKEFAKEFAKE.Cwm9rQ.IJIFJQIUUIJFQFAKEFAKEFAKEFAKE',
                        'Accept: application/json',
                        'Content-Type: application/json'
                ),
                //
                CURLOPT_POSTFIELDS => $postjson
        ) );
        $url = BASE_URL . '/channels/209972668449947648/webhooks';
        $hc->exec ( $url );
        $this->data = json_decode ( $hc->getResponseBody (), true );
        hhb_var_dump ( $hc->getResponseBody (), $this->data, $url );
    }
    function postMessage(string $message) {
        // POST
        $hc = &$this->hc;
        $postdata = array (
                'content' => $message,
                'tts' => true
        );
        $postjson = json_encode ( $postdata );
        $hc->setopt_array ( array (
                CURLOPT_POST => true,
                CURLOPT_HTTPHEADER => array (
                        'Authorization: Bot IJIFJQIUUIJFQFAKEFAKEFAKEFAKE.Cwm9rQ.IJIFJQIUUIJFQFAKEFAKEFAKEFAKE',
                        'Accept: application/json',
                        'Content-Type: application/json'
                ),
                //
                CURLOPT_POSTFIELDS => $postjson
        ) );
        echo "POSTING MESSAGE";
        $url = BASE_URL . '/webhooks/' . $this->data ['id'] . '/' . $this->data ['token'];
        $hc->exec ( $url );
        hhb_var_dump ( $hc->getResponseBody (), $hc->getResponseHeaders (), $url );
    }
    function __destruct() {
        echo "destructing...";
        $hc = &$this->hc;
        $hc->setopt_array ( array (
                CURLOPT_CUSTOMREQUEST => 'DELETE',
                CURLOPT_HTTPHEADER => array (
                        'Authorization: Bot IJIFJQIUUIJFQFAKEFAKEFAKEFAKE.Cwm9rQ.IJIFJQIUUIJFQFAKEFAKEFAKEFAKE',
                        'Accept: application/json',
                        'Content-Type: application/json'
                ),
                CURLOPT_POSTFIELDS => json_encode ( array () )
        ) );
        $url = BASE_URL . '/webhooks/' . $this->data ['id'];
        $hc->exec ( $url );
        hhb_var_dump ( $hc->getResponseBody (), $url );
    }
}

(and it was written and tested in a hurry too, thus the ugly global define)