Skip to content

Commit 63f702a

Browse files
Merge pull request #166 from untrobotics/URW-205-discord-attachments-fix
URW-205 Fix Discord bot not sending multiple attachments
2 parents 28562a1 + 7dad713 commit 63f702a

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

api/discord/bot.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class DiscordBot {
55

66
protected static function send_api_request($URI, $method = 'GET', $content_type = "application/json", $data = null, $files = null) {
77
$ch = curl_init();
8-
8+
99
$headers = array();
1010
$headers[] = 'Authorization: Bot ' . static::AUTH_TOKEN;
1111
$headers[] = 'Content-Type: ' . $content_type; //multipart/form-data';
@@ -41,18 +41,18 @@ protected static function send_api_request($URI, $method = 'GET', $content_type
4141
if (curl_errno($ch)) {
4242
throw new DiscordBotException("Error occurred when making API request: '" . curl_error($ch) . "'" . "(" . curl_errno($ch) . ")");
4343
}
44-
44+
4545
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
46-
46+
4747
curl_close($ch);
48-
48+
4949
$response = new stdClass();
5050
$response->result = json_decode($result);
5151
$response->status_code = $status_code;
5252

5353
return $response;
5454
}
55-
55+
5656
public static function send_message($message, $channel_id, $attachments = null) {
5757
if (is_string($message)) {
5858
$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)
6363

6464
// Add files to array
6565
$files = array();
66+
$tmp_files = []; // needed if more than 1 attachment to prevent gc for deleting the temp files
6667
if ($attachments) {
6768
foreach ($attachments as $k => $attachment) {
6869
if(isset($attachment['bin'])) // Function caller passed the raw data to the arg
6970
{
7071
// 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'];
7374
file_put_contents($path,$attachment['bin']);
7475
$file_type = $attachment['type'];
7576
$file_mime = ext2mime($file_type);
@@ -83,8 +84,8 @@ public static function send_message($message, $channel_id, $attachments = null)
8384
else // We assume the attachment is an online file that we need to download
8485
{ // sebastian only insane people put curly braces on the same line
8586
//$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'];
8889
$ch = curl_init();
8990
curl_setopt($ch, CURLOPT_URL, $attachment['url']);
9091
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
@@ -108,18 +109,18 @@ public static function send_message($message, $channel_id, $attachments = null)
108109
}
109110
}
110111
//error_log(var_export($files, true));
111-
112+
112113
return static::send_api_request("/channels/{$channel_id}/messages", 'POST', 'multipart/form-data', $data, $files);
113114
}
114-
115+
115116
public static function add_user_role($guild_id, $user_id, $role_id) {
116117
return static::send_api_request("/guilds/{$guild_id}/members/{$user_id}/roles/{$role_id}", 'PUT');
117118
}
118-
119+
119120
public static function remove_user_role($guild_id, $user_id, $role_id) {
120121
return static::send_api_request("/guilds/{$guild_id}/members/{$user_id}/roles/{$role_id}", 'DELETE');
121122
}
122-
123+
123124
public static function type($channel_id) {
124125
return static::send_api_request("/channels/{$channel_id}/typing", 'POST');
125126
}
@@ -133,7 +134,7 @@ public static function hasHitRateLimit($result) {
133134
return $result->status_code == 429;
134135
}
135136
}
136-
137+
137138
class DiscordBotException extends Exception {
138139
public function __construct($message, $code = 0, Exception $previous = null) {
139140
parent::__construct($message, $code, $previous);

0 commit comments

Comments
 (0)