Jump to content
Sign in to follow this  
Jamie

[Kitsune] How to setup discord webhooks [AS2/AS3]

Recommended Posts

Hello all,

This is just a fun small project I've been working on to see how to get Discord webhooks working, nothing too serious and I kind of don't recommend using it on heavily populated servers.

What does it do? It sends live notifications to your Discord when someone signs in - you can build off this and create bigger and better things. 

What do you need?

- PHP 7

- CURL

- Kitsune AS2/AS3


Preview

HU11fyfcSvqKQUqfgLbQRQ.png

How to add

First you need to create a webhook on your Discord server.

Go to your server settings, and then webhooks.

T0y7_N68T-mY5WwLd-Oz-g.png

Click on "Create Webhook" and enter a name for your bot and also select the channel you want it in, you can also add an avatar for it (webhook icon).

Copy the webhook url and save it somewhere.

Now we need to go to your source and go to /Kitsune/Kitsune/ClubPenguin/Login.php

Find 

Quote

    public function __construct() {    
        parent::__construct();
        
        Logger::Fine("Login server is online");
    }

 

After it add the following

Quote

 

public function discordwebhook($content)
{
    $curl = curl_init("REPLACE WITH YOUR DISCORD WEB HOOK LINK");

    curl_setopt_array($curl, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSL_VERIFYHOST => false,
        
        CURLOPT_USERAGENT => 'Mozilla/5.0 (+https://flash.moe)',
        CURLOPT_HTTPHEADER => ['Content-Type: application/json'],

        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => json_encode($content),
    ]);

    curl_exec($curl);
}

 

 

Where REPLACE WITH YOUR DISCORD WEB HOOK LINK is, add the webhook link.

 

Now find

Quote

 $penguin->send("%xt%l%-1%{$penguinData["ID"]}|{$penguinData["SWID"]}|{$penguinData["Username"]}|$encryptedPassword|1|45|2|false|true|$loginTime%$confirmationHash%$friendsKey%101,1%{$penguinData["Email"]}%");

 

After it add the following

 

Quote

 

  print_r($this->discordwebhook(['embeds' => [

    [
        'title' => 'CP Reborn Server',
        'description' => $penguinData["Username"].' has signed into CP Reborn and is on the island',
        'url' => 'https://play.clubpenguinreborn.pro/',
        'timestamp' => gmdate("Y-m-d\TH:i:s.u\Z", strtotime("now")),
    ],
]]));        

 

 

You can now save this file and launch your server.

It should work great and you should then see your bot put live notifications for when someone logins.

Have some fun with it, you can do all types of things with this - let it be a starter to create bigger and better things with it.

Thanks to @Jad for helping me fix a few errors with it, life saver.

Edited by Jamie
  • Like 2

Share this post


Link to post
Share on other sites

Unfortunately, because curl_exec is a blocking call, your server will actually hang whenever a post is made to the Discord webhook API. I did this a few weeks ago, and even with two users online, the effect on performance was noticeable, and this could very easily be abused by creating bots which login to the server repeatedly.

PHP is able to execute multiple curl requests at once, but there isn't a way (as far as I know), to carry on with your socket server loop before the requests are complete, not without using an extension which brings real multithreading to PHP, like pthreads.

A nice idea, and it works, to a degree, don't use this if your server has many players. :)

  • Like 1

Share this post


Link to post
Share on other sites
56 minutes ago, Ben said:

Unfortunately, because curl_exec is a blocking call, your server will actually hang whenever a post is made to the Discord webhook API. I did this a few weeks ago, and even with two users online, the effect on performance was noticeable, and this could very easily be abused by creating bots which login to the server repeatedly.

PHP is able to execute multiple curl requests at once, but there isn't a way (as far as I know), to carry on with your socket server loop before the requests are complete, not without using an extension which brings real multithreading to PHP, like pthreads.

A nice idea, and it works, to a degree, don't use this if your server has many players. :)

Tested this for several hours and effect on performance isn't' really noticeable at the moment, working on it even more, just a test for now - as I stated in thread it isn't wise to use this with many players.

 

Share this post


Link to post
Share on other sites
On 6/27/2017 at 1:19 PM, Ben said:

Unfortunately, because curl_exec is a blocking call, your server will actually hang whenever a post is made to the Discord webhook API. I did this a few weeks ago, and even with two users online, the effect on performance was noticeable, and this could very easily be abused by creating bots which login to the server repeatedly.

PHP is able to execute multiple curl requests at once, but there isn't a way (as far as I know), to carry on with your socket server loop before the requests are complete, not without using an extension which brings real multithreading to PHP, like pthreads.

A nice idea, and it works, to a degree, don't use this if your server has many players. :)

Just wanted to say the same thing, whenever Discord's API would act up or have high latency, Club Penguin Rewritten would freeze up entirely.

 

Use it for minor things.

Share this post


Link to post
Share on other sites
Sign in to follow this  

×