-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiscord_bot.php
77 lines (68 loc) · 2.31 KB
/
discord_bot.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/**
* This file provides functions to use Discord Bots.
* Its main purpose is to just wrap teh CURL requests, showing how the Authorization header needs to be used.
*
* Use this as a guide only. It duplicates code for ease of assessibility.
*
* Author: Lachee
* Last Updated: Feb 2021
* License: MIT
*/
if (!defined("DISCORD_API"))
define("DISCORD_API", "https://discordapp.com/api/v8");
/**
* Sends a GET request to discord using the bot token
*/
function discord_get($route, $botToken)
{
return discord_http("GET", DISCORD_API . $route, [ 'Authorization: Bot ' . $botToken ], null);
}
/**
* Sends a DELETE request to discord using the bot token
*/
function discord_delete($route, $botToken)
{
return discord_http("DELETE", DISCORD_API . $route, [ 'Authorization: Bot ' . $botToken ], null);
}
/**
* Sends a PATCH request to discord using the bot token and json data.
*/
function discord_patch($route, $botToken, $data)
{
$json = json_encode($data);
return discord_http("PATCH", DISCORD_API . $route, [ 'Authorization: Bot ' . $botToken, 'Content-Type: application/json' ], $json);
}
/**
* Sends a POST request to discord using the bot token and json data.
*/
function discord_post($route, $botToken, $data)
{
$json = json_encode($data);
return discord_http("POST", DISCORD_API . $route, [ 'Authorization: Bot ' . $botToken, 'Content-Type: application/json' ], $json);
}
/**
* Sends a PUT request to discord using the bot token and json data.
*/
function discord_put($route, $botToken, $data)
{
$json = json_encode($data);
return discord_http("PUT", DISCORD_API . $route, [ 'Authorization: Bot ' . $botToken, 'Content-Type: application/json' ], $json);
}
/**
* Creates a cURL request to discord with specified method, uri, headers and data.
*/
function discord_http($method, $uri, $headers, $data)
{
//Make the request
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers );
$response = curl_exec($ch);
$json = json_decode($response, true);
return $json;
}