<?php

use Database\{Channels, Listbots, Settings};

$channelRank = ['creator', 'member', 'administrator'];
$canPass = true;

if ($data == 'CheckJoin') {
    if ($user_rank != 'admin') {
        foreach (Channels::getAll() as $channel) {
            $beforToken = $Telegram->bot_token;
            $Telegram->bot_token = Listbots::getBotDef()->token;
            $get = $Telegram->getChatMember($channel->chatid, $Userid);
            $Telegram->bot_token = $beforToken;
            $chanel_id = '';
            if ($get->getOk() && !in_array($get->getResult()->getStatus(), $channelRank)) {
                $chanel_id .= $channel->link  . "\n";
                $canPass = false;
            }
            if (!$canPass) {
                $Telegram->editMessageText($chatid, $MessageID, str_replace(['#ch'], [$chanel_id], msg('lock')), 'html', '', true, $button_Locks);
                die();
            }
            Channels::addCount($channel->id);
        }
    }
    $Telegram->editMessageText($chatid, $MessageID, msg('def'), 'html', '', true, $button_start);

} 
elseif (Settings::getSettings('lock') == 1 && Channels::channelCount() > 0) {
    if ($user_rank != 'admin') {
        $chanel_id = '';
        foreach (Channels::getAll() as $channel) {
            $beforToken = $Telegram->bot_token;
            $Telegram->bot_token = getStore('botDefToken');
            $get = $Telegram->getChatMember($channel->chatid, $Userid);
            $Telegram->bot_token = $beforToken;
            if ($get->getOk() && !in_array($get->getResult()->getStatus(), $channelRank)) {
                $chanel_id .= "\n" . $channel->link . "\n";
                $canPass = false;

            }
        }
        if (!$canPass) {
            $a = $Telegram->sendMessage($chatid, '.', 'html', false, $button_remove);
            if ($a->getOk()) {
                $Telegram->deleteMessage($chatid, $a->getResult()->getMessageId());
            }
            $Telegram->sendMessage($chatid, str_replace(['#ch'], [$chanel_id], msg('lock')), 'html', false, $button_Locks);
            die();
        }
    }
}
unset($canPass);