forked from Talishar/Talishar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetDiscordContentCarousel.php
More file actions
195 lines (161 loc) · 6.86 KB
/
GetDiscordContentCarousel.php
File metadata and controls
195 lines (161 loc) · 6.86 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
* GetDiscordContentCarousel.php
*
* Fetches video links from the Discord #talishar-content channel
* Returns YouTube and Twitch videos in a carousel format
*
* Graceful error handling - returns empty carousel on any error (HTTP 200)
* Uses APCu caching (10 minute TTL) to reduce Discord API calls
*/
include 'Libraries/APCuCache.php';
header('Content-Type: application/json');
// CORS validation - whitelist trusted domains
$origin = $_SERVER['HTTP_ORIGIN'] ?? '';
$allowedDomains = ['localhost', 'talishar.net', 'legacy.talishar.net', 'preview.talishar.net'];
$isAllowed = false;
foreach ($allowedDomains as $domain) {
if (strpos($origin, $domain) !== false) {
$isAllowed = true;
break;
}
}
if ($isAllowed) {
header('Access-Control-Allow-Origin: ' . $origin);
header('Access-Control-Allow-Methods: GET, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
}
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit;
}
http_response_code(200);
try {
// Check cache first
$cacheKey = 'discord_carousel_videos';
$cachedResult = APCuCache::get($cacheKey);
if ($cachedResult !== null) {
echo json_encode($cachedResult);
exit;
}
$maxMessages = isset($_GET['maxMessages']) ? (int)$_GET['maxMessages'] : 100;
$maxMessages = min($maxMessages, 100);
// Get credentials
$botToken = getenv('DISCORD_BOT_TOKEN') ?: ($_ENV['DISCORD_BOT_TOKEN'] ?? null);
$channelId = getenv('DISCORD_CONTENT_CHANNEL_ID') ?: ($_ENV['DISCORD_CONTENT_CHANNEL_ID'] ?? null);
if (!$botToken || !$channelId) {
// Try multiple .env file locations
$envPaths = [
__DIR__ . '/.env',
dirname(__DIR__) . '/.env',
'/opt/lampp/htdocs/game/.env',
'/opt/lampp/htdocs/.env',
];
foreach ($envPaths as $envFile) {
if (file_exists($envFile) && is_readable($envFile)) {
foreach (file($envFile, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES) as $line) {
if (strpos($line, 'DISCORD_BOT_TOKEN=') === 0) {
$botToken = trim(str_replace('DISCORD_BOT_TOKEN=', '', $line), '"\'');
} elseif (strpos($line, 'DISCORD_CONTENT_CHANNEL_ID=') === 0) {
$channelId = trim(str_replace('DISCORD_CONTENT_CHANNEL_ID=', '', $line), '"\'');
}
}
if ($botToken && $channelId) {
break; // Found both values, stop searching
}
}
}
}
if (!$botToken || !$channelId) {
echo json_encode(['success' => true, 'count' => 0, 'videos' => [], 'channelName' => '#talishar-content']);
exit;
}
// Fetch channel info to get the channel name
$channelInfo = null;
$channelUrl = "https://discord.com/api/v10/channels/{$channelId}";
$ch = curl_init($channelUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Bot {$botToken}", "User-Agent: Talishar"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$channelResponse = curl_exec($ch);
$channelHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($channelHttpCode === 200) {
$channelInfo = json_decode($channelResponse, true);
}
// Fetch from Discord with pagination to get more messages
$messages = [];
$before = null;
$totalFetched = 0;
$maxTotal = 50; // Fetch up to 100 messages total
while ($totalFetched < $maxTotal) {
$url = "https://discord.com/api/v10/channels/{$channelId}/messages?limit=100";
if ($before) {
$url .= "&before={$before}";
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Bot {$botToken}", "User-Agent: Talishar"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode !== 200) {
break;
}
$batch = json_decode($response, true);
if (!is_array($batch) || count($batch) === 0) {
break;
}
$messages = array_merge($messages, $batch);
$totalFetched += count($batch);
$before = $batch[count($batch) - 1]['id'];
}
if (!is_array($messages)) {
echo json_encode(['success' => true, 'count' => 0, 'videos' => [], 'channelName' => $channelInfo ? '#' . $channelInfo['name'] : '#talishar-content']);
exit;
}
// Extract videos
$videos = [];
$ytRegex = '/(?:youtube\.com|youtu\.be)\/(?:watch\?v=|embed\/|v\/)?([a-zA-Z0-9_-]{11})/';
$twRegex = '/twitch\.tv\/(?:videos\/(\d+)|([a-zA-Z0-9_]+))/';
foreach ($messages as $msg) {
if (!is_array($msg)) continue;
$content = $msg['content'] ?? '';
// Replace channel mentions in content
$content = str_replace('<#868488473378684938>', '#release-notes', $content);
$content = str_replace('<#1014193064736194691>', '#talishar-content', $content);
// Preserve line breaks
$content = nl2br(htmlspecialchars($content, ENT_QUOTES, 'UTF-8'), false);
$author = $msg['author']['username'] ?? 'Unknown';
$timestamp = $msg['timestamp'] ?? date('c');
if (preg_match($ytRegex, $content, $m)) {
$videos[] = ['videoId' => $m[1], 'type' => 'youtube', 'title' => $content, 'author' => $author, 'timestamp' => $timestamp];
} elseif (preg_match($twRegex, $content, $m)) {
$vid = $m[1] ?? $m[2] ?? null;
if ($vid) $videos[] = ['videoId' => $vid, 'type' => 'twitch', 'title' => $content, 'author' => $author, 'timestamp' => $timestamp];
}
}
// Limit to 1 per author, most recent first
$byAuthor = [];
foreach ($videos as $v) {
if (!isset($byAuthor[$v['author']]) || strtotime($v['timestamp']) > strtotime($byAuthor[$v['author']]['timestamp'])) {
$byAuthor[$v['author']] = $v;
}
}
$final = array_values($byAuthor);
usort($final, fn($a, $b) => strtotime($b['timestamp'] ?? '0') - strtotime($a['timestamp'] ?? '0'));
$result = [
'success' => true,
'count' => count($final),
'videos' => array_slice($final, 0, 12),
'channelName' => $channelInfo ? '#' . $channelInfo['name'] : '#talishar-content'
];
// Cache the result for 10 minutes
APCuCache::set($cacheKey, $result, 600);
echo json_encode($result);
} catch (Exception $e) {
error_log('GetDiscordContentCarousel: ' . $e->getMessage());
echo json_encode(['success' => true, 'count' => 0, 'videos' => [], 'channelName' => '#talishar-content']);
}
?>