├── .gitattributes ├── discord.tpl ├── readme.md ├── license └── discord.php /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /discord.tpl: -------------------------------------------------------------------------------- 1 |
2 |

{$message}

3 | Return to home 4 |
-------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # WHMCS Discord Connection 2 | This will let paying customers get a Discord role by going to /discord.php. 3 | I will not be providing support for this repository. 4 | 5 | If you like it then give it a star! 🌟 6 | 7 | ## Installation 8 | 1. Add the `discord.php` file to the root of your WHMCS installation 9 | 10 | 2. Change the variables in `discord.php`. 11 | 12 | 3. [Create a custom field to customers called `discord`. This will contain the customer's Discord ID.](https://docs.whmcs.com/Custom_Client_Fields) 13 | 14 | 4. Add the `discord.tpl` to your theme's directory. 15 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Gijsbert van Gemert 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /discord.php: -------------------------------------------------------------------------------- 1 | setPageTitle('Discord Connection'); 21 | $ca->initPage(); 22 | 23 | $currentUser = new CurrentUser(); 24 | $client = $currentUser->client(); 25 | 26 | if ($client) { 27 | if(isset($_GET['code'])) { 28 | 29 | /* Get user access token */ 30 | $ch = curl_init('https://discord.com/api/oauth2/token'); 31 | curl_setopt_array($ch, array( 32 | CURLOPT_HTTPHEADER => array('Authorization: Bearer '.$_GET['code']), 33 | CURLOPT_VERBOSE => 1, 34 | CURLOPT_CONTENT_LENGTH => 0, 35 | CURLOPT_POST => 1, 36 | CURLOPT_SSL_VERIFYPEER => 0 37 | )); 38 | curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=authorization_code&code=".$_GET['code']."&redirect_uri={$domainurl}/discord.php&client_id={$client_id}&client_secret={$secret_id}"); 39 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 40 | $data = curl_exec($ch); 41 | $error = curl_error($ch); 42 | curl_close($ch); 43 | $kaas = json_decode($data); 44 | 45 | if ($kaas->access_token) { 46 | /* Get user ids */ 47 | $ch = curl_init('https://discord.com/api/users/@me'); 48 | curl_setopt_array($ch, array( 49 | CURLOPT_HTTPHEADER => array('Authorization: Bearer '.$kaas->access_token), 50 | CURLOPT_VERBOSE => 1, 51 | CURLOPT_CONTENT_LENGTH => 0, 52 | CURLOPT_SSL_VERIFYPEER => 0 53 | )); 54 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 55 | $data = curl_exec($ch); 56 | $error = curl_error($ch); 57 | curl_close($ch); 58 | $kaas = json_decode($data); 59 | 60 | /* Add Discord ID to client profile */ 61 | $command = 'UpdateClient'; 62 | $customfields = array('discord' => $kaas->id); 63 | $postData = array( 64 | 'clientid' => $client->id, 65 | 'customfields' => base64_encode(serialize($customfields)) 66 | ); 67 | 68 | $results = localAPI($command, $postData); 69 | 70 | /* Add role to user */ 71 | $currentUser = new CurrentUser(); 72 | $authUser = $currentUser->user(); 73 | 74 | $command = 'GetClientsDetails'; 75 | $postData = array( 76 | 'clientid' => $authUser->id, 77 | 'stats' => true, 78 | ); 79 | $results = localAPI($command, $postData); 80 | 81 | if ($results['stats']['productsnumactive'] > 0) { 82 | $url = "https://discord.com/api/v9/guilds/{$guild_id}/members/{$kaas->id}/roles/{$role_id}"; 83 | $curl = curl_init(); 84 | 85 | $headers = [ 86 | 'Accept: application/json', 87 | 'Content-Type: application/json', 88 | 'Authorization: Bot ' . $bot_token 89 | ]; 90 | 91 | curl_setopt($curl, CURLOPT_URL, $url); 92 | curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 93 | curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); 94 | curl_setopt($curl, CURLOPT_POSTFIELDS, []); 95 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 96 | $response = curl_exec($curl); 97 | curl_close($curl); 98 | 99 | $ca->assign('message', "Discord Linked Successfully"); 100 | } 101 | } else { 102 | $ca->assign('message', "This Link has Expired"); 103 | } 104 | } else { 105 | header('Location: https://discordapp.com/oauth2/authorize?response_type=code&client_id=' . $client_id . '&redirect_uri=' . $domainurl . '/discord.php&scope=' . $scopes ); 106 | } 107 | } 108 | 109 | $ca->setTemplate('discord'); 110 | 111 | $ca->output(); 112 | --------------------------------------------------------------------------------