@@ -5,7 +5,7 @@ class DiscordBot {
5
5
6
6
protected static function send_api_request ($ URI , $ method = 'GET ' , $ content_type = "application/json " , $ data = null , $ files = null ) {
7
7
$ ch = curl_init ();
8
-
8
+
9
9
$ headers = array ();
10
10
$ headers [] = 'Authorization: Bot ' . static ::AUTH_TOKEN ;
11
11
$ headers [] = 'Content-Type: ' . $ content_type ; //multipart/form-data';
@@ -41,18 +41,18 @@ protected static function send_api_request($URI, $method = 'GET', $content_type
41
41
if (curl_errno ($ ch )) {
42
42
throw new DiscordBotException ("Error occurred when making API request: ' " . curl_error ($ ch ) . "' " . "( " . curl_errno ($ ch ) . ") " );
43
43
}
44
-
44
+
45
45
$ status_code = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
46
-
46
+
47
47
curl_close ($ ch );
48
-
48
+
49
49
$ response = new stdClass ();
50
50
$ response ->result = json_decode ($ result );
51
51
$ response ->status_code = $ status_code ;
52
52
53
53
return $ response ;
54
54
}
55
-
55
+
56
56
public static function send_message ($ message , $ channel_id , $ attachments = null ) {
57
57
if (is_string ($ message )) {
58
58
$ data = new stdClass (); // can't be bothered right now to make a class
@@ -63,13 +63,14 @@ public static function send_message($message, $channel_id, $attachments = null)
63
63
64
64
// Add files to array
65
65
$ files = array ();
66
+ $ tmp_files = []; // needed if more than 1 attachment to prevent gc for deleting the temp files
66
67
if ($ attachments ) {
67
68
foreach ($ attachments as $ k => $ attachment ) {
68
69
if (isset ($ attachment ['bin ' ])) // Function caller passed the raw data to the arg
69
70
{
70
71
// Since we have the data, we need to create a tmpfile to store that data for the CURLFile
71
- $ file = tmpfile ();
72
- $ path = stream_get_meta_data ($ file )['uri ' ];
72
+ $ tmp_files [ $ k ] = tmpfile ();
73
+ $ path = stream_get_meta_data ($ tmp_files [ $ k ] )['uri ' ];
73
74
file_put_contents ($ path ,$ attachment ['bin ' ]);
74
75
$ file_type = $ attachment ['type ' ];
75
76
$ file_mime = ext2mime ($ file_type );
@@ -83,8 +84,8 @@ public static function send_message($message, $channel_id, $attachments = null)
83
84
else // We assume the attachment is an online file that we need to download
84
85
{ // sebastian only insane people put curly braces on the same line
85
86
//$content = file_get_contents($attachment['url']);
86
- $ file = tmpfile ();
87
- $ path = stream_get_meta_data ($ file )['uri ' ];
87
+ $ tmp_files [ $ k ] = tmpfile ();
88
+ $ path = stream_get_meta_data ($ tmp_files [ $ k ] )['uri ' ];
88
89
$ ch = curl_init ();
89
90
curl_setopt ($ ch , CURLOPT_URL , $ attachment ['url ' ]);
90
91
curl_setopt ($ ch , CURLOPT_FOLLOWLOCATION , true );
@@ -108,18 +109,18 @@ public static function send_message($message, $channel_id, $attachments = null)
108
109
}
109
110
}
110
111
//error_log(var_export($files, true));
111
-
112
+
112
113
return static ::send_api_request ("/channels/ {$ channel_id }/messages " , 'POST ' , 'multipart/form-data ' , $ data , $ files );
113
114
}
114
-
115
+
115
116
public static function add_user_role ($ guild_id , $ user_id , $ role_id ) {
116
117
return static ::send_api_request ("/guilds/ {$ guild_id }/members/ {$ user_id }/roles/ {$ role_id }" , 'PUT ' );
117
118
}
118
-
119
+
119
120
public static function remove_user_role ($ guild_id , $ user_id , $ role_id ) {
120
121
return static ::send_api_request ("/guilds/ {$ guild_id }/members/ {$ user_id }/roles/ {$ role_id }" , 'DELETE ' );
121
122
}
122
-
123
+
123
124
public static function type ($ channel_id ) {
124
125
return static ::send_api_request ("/channels/ {$ channel_id }/typing " , 'POST ' );
125
126
}
@@ -133,7 +134,7 @@ public static function hasHitRateLimit($result) {
133
134
return $ result ->status_code == 429 ;
134
135
}
135
136
}
136
-
137
+
137
138
class DiscordBotException extends Exception {
138
139
public function __construct ($ message , $ code = 0 , Exception $ previous = null ) {
139
140
parent ::__construct ($ message , $ code , $ previous );
0 commit comments