• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • 2026 staff recruitment is open! Check it out and consider applying!

OTClient OTCv8 - discord/server online panel

Wilku93

Member
Joined
Jul 7, 2024
Messages
43
Reaction score
11
GitHub
Wilku93
Hi ;)

I'm trying to get it to display the online count from my Discord server and the current online players on the server, but something's not working.

I've changed status.php by adding JSON and my server, and I've made changes to topmenu.lua.

I'd appreciate any help :)


OTCv8>api>status.php

LUA:
<?php
$online_otservlist = 0;
try {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://otservlist.org/");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return data inplace of echoing on screen
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Skip SSL Verification
    curl_setopt($ch, CURLOPT_ENCODING , "");
    $site = curl_exec($ch);
    curl_close($ch);
    
    preg_match('/There are <strong>([0-9]*)<\/strong>/', $site, $matches);
    $online_otservlist = $matches[1];
} catch(Exception $e) {}
$online_discord = 0;
try {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://discord.com/api/guilds/1315805221087088710/widget.json");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Accept: application/json'
    ));
    curl_setopt($ch, CURLOPT_USERAGENT, 'OTCv8-Status/1.0');
    $discordResponse = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($discordResponse !== false && $httpCode === 200) {
        $discordData = json_decode($discordResponse, true);
        if (json_last_error() === JSON_ERROR_NONE && isset($discordData['presence_count'])) {
            $online_discord = (int)$discordData['presence_count'];
        }
    }
} catch(Exception $e) {}

$response = array(
    "online" => "$online_otservlist Players online",
    "discord_online" => $online_discord,
    "discord_link" => "https://discord.com/invite/pfwpxME9"
);
echo json_encode($response);
?>


topmenu.lua:

LUA:
function updateStatus()
  removeEvent(statusUpdateEvent)
  if not topMenu.onlineLabel then return end
  if g_game.isOnline() then return end

  if Services and Services.status and Services.status:len() >= 4 then
    HTTP.postJSON(Services.status, {type="cacheinfo"}, function(data, err)
      if err then
        g_logger.warning("HTTP error for " .. Services.status .. ": " .. err)
        statusUpdateEvent = scheduleEvent(updateStatus, 5000)
        return
      end
      if topMenu.onlineLabel then
        local players
        if data.playersonline then
          players = tonumber(data.playersonline) or data.playersonline
        elseif data.online then
          players = tonumber(data.online) or tonumber(string.match(data.online, '%d+')) or data.online
        end
        if players then
          topMenu.onlineLabel:setText("Players online: " .. players)
          topMenu.onlineLabel:setColor('green')
        end
      end
      if data.discord_online and topMenu.discordLabel then
        local countText = data.discord_online .. " online"
        topMenu.discordLabel:setText(countText)
        topMenu.discordLabel:setColor('green')
      end
      if data.discord_link and topMenu.discordLabel and topMenu.discord then
        local discordOnClick = function()
          g_platform.openUrl(data.discord_link)
        end
        topMenu.discordLabel.onClick = discordOnClick
        topMenu.discord.onClick = discordOnClick
      end
      statusUpdateEvent = scheduleEvent(updateStatus, 60000)
    end)
  else
    statusUpdateEvent = scheduleEvent(updateStatus, 60000)
  end
end

-- invite code used to fetch presence data
local discordInvite = 'pfwpxME9'
local discordGuildId = '1315805221087088710'

function updateDiscord()
  removeEvent(discordUpdateEvent)
  if not topMenu.discordLabel then return end

  local url = "https://discord.com/api/guilds/" .. discordGuildId .. "/widget.json"
  HTTP.getJSON(url, function(data, err)
    if not err and data and data.presence_count then
      local countText = data.presence_count .. " online"
      topMenu.discordLabel:setText(countText)
      topMenu.discordLabel:setColor('green')
    end
  end)

  local discordOnClick = function()
    g_platform.openUrl("https://discord.gg/" .. discordInvite)
  end
  topMenu.discordLabel.onClick = discordOnClick
  if topMenu.discord then
    topMenu.discord.onClick = discordOnClick
  end

  discordUpdateEvent = scheduleEvent(updateDiscord, 60000)
end

function setupVelestaButton()
  if not topMenu.velestaButton then return end
  topMenu.velestaButton.onClick = function()
    g_platform.openUrl("https://Velesta.pl")
  end
end
a.webp
 
Back
Top