<?php
namespace phpstaff\Widgets;
use XF\Widget\AbstractWidget;
class DiscordWidgetStatic
{
/**
* Статический метод для виджета XenForo
*
* @param AbstractWidget $widget
* @return string HTML виджета
*/
public static function render(AbstractWidget $widget)
{
$inviteCode = "7jVr21H"; // Ваш код приглашения Discord например 7jVr21H
$cacheKey = "discord_widget_data";
$cacheTime = 600; // 10 минут
$app = \XF::app();
$cache = $app->cache();
// Получаем данные из кэша
if (method_exists($cache, 'get')) {
$data = $cache->get($cacheKey);
} else {
$data = $cache->fetch($cacheKey); // для RedisCache
}
// Если данных нет — загружаем с Discord
if (!$data) {
$url = "https://discord.com/api/v9/invites/{$inviteCode}?with_counts=true&with_expiration=true";
$json = @file_get_contents($url);
$data = $json ? json_decode($json, true) : null;
if (method_exists($cache, 'set')) {
$cache->set($cacheKey, $data, $cacheTime);
} else {
$cache->save($cacheKey, $data, $cacheTime); // для RedisCache
}
}
// Если данные получены
if ($data && isset($data['guild'])) {
$guild = $data['guild'];
$iconUrl = $guild['icon']
? "https://cdn.discordapp.com/icons/{$guild['id']}/{$guild['icon']}.png?size=64"
: "https://cdn.discordapp.com/embed/avatars/0.png";
$name = $guild['name'];
$members = $data['approximate_member_count'];
$online = $data['approximate_presence_count'];
} else {
// Заглушка
$iconUrl = "https://cdn.discordapp.com/embed/avatars/0.png";
$name = "Наш Discord";
$members = null;
$online = null;
}
// HTML виджета
$html = <<<HTML
<div class="block block--discord" style="max-width:360px;margin:auto;background:#2C2F33;border-radius:8px;color:#fff;font-family:sans-serif;">
<div class="block-container">
<h3 class="block-header" style="background:#5865F2;padding:10px;border-radius:8px 8px 0 0;text-align:center;margin:0;font-size:16px;font-weight:bold;">
Наш Discord
</h3>
<div class="block-body" style="padding:12px;text-align:center;">
<div style="display:flex;align-items:center;justify-content:center;gap:12px;margin-bottom:10px;">
<img src="{$iconUrl}" alt="Server Icon" style="width:64px;height:64px;border-radius:12px;">
<div style="text-align:left;">
<strong style="font-size:16px;">{$name}</strong><br>
HTML;
if ($members !== null && $online !== null) {
$html .= <<<HTML
<span style="font-size:13px;color:#b9bbbe;">👥 Всего: {$members}</span><br>
<span style="font-size:13px;color:#43b581;">🟢 Онлайн: {$online}</span>
HTML;
}
$html .= <<<HTML
</div>
</div>
<div>
<a href="https://discord.gg/{$inviteCode}" target="_blank"
style="display:inline-block;padding:8px 16px;background:#5865F2;color:#fff;font-weight:bold;border-radius:6px;text-decoration:none;">
Присоединиться
</a>
</div>
</div>
</div>
</div>
HTML;
return $html;
}
}