<?php

if (file_exists("config.php")) {
    require_once("config.php");
} else {
    if (!defined("admins")) {
        define("admins", [admins[0]]);
    }
}
if (phpversion() <= 8) {
    die("Php Version +8 Reuiqre");
}
ini_set('memory_limit', -1);
ini_set('max_execution_time', -1);
date_default_timezone_set("Asia/Tehran");
/*if (!\file_exists('madeline.php')) {
    \copy('https://phar.madelineproto.xyz/madeline.php', 'madeline.php');
}*/
require_once("jdf.php");
require_once('vendor/autoload.php');
use danog\MadelineProto\API;
use danog\MadelineProto\EventHandler;
use danog\MadelineProto\Exception;
use Amp\Loop;
use Amp\Mysql;
use danog\MadelineProto\Logger;
use danog\MadelineProto\Settings;
use danog\MadelineProto\Settings\AppInfo;
use danog\MadelineProto\Settings\Peer;
use danog\MadelineProto\Settings\Serialization;
use danog\MadelineProto\RPCErrorException;
use danog\Loop\Generic\GenericLoop;
class MrPoKeR extends EventHandler
{
    public static $db;
    protected static array $dbProperties = [
        'dataStoredOnDb' => 'array'
    ];

    /**
    * @var DbArray<array>
    */
    protected $dataStoredOnDb;
    public static $start = 0;
    public function onStart() {
    	
        $session = explode("/", $this->getSessionName());
        $c = count($session)-1;
        $session = str_replace("Harim", "", $session[$c]);
        if (!self::$start) {
            $config = Mysql\MysqlConfig::fromString("host=".hostname.";user=".dbuser.";pass=".dbpass.";db=".dbname);
            //  $config = $config->withCharset("utf8mb4_general_ci");
            self::$db = Mysql\connect($config);
        }
        self::$start = time();
        $q = $this;
        $Loop = new GenericLoop(function () use($q, $session) {
            $f = $q->getManager("foshlist");
            $time = $q->getManager("spamtime");
            if (($q->getCoulmn("accounts", "stats", ["sessionName" => $session])) == "on") {
                try {

                    if (!empty($f) && (($q->getCoulmn("accounts", "spm", ["sessionName" => $session])) == "on" or ($q->getCoulmn("accounts", "timerz", ["sessionName" => $session])) == "off")) {
                        $g = $q->getManager("gp");
                        $me = $q->getManager("mens");
                        $pt = $q->getManager("pText");
                        $spv = $q->getManager("spampv");
                        if ($spv != 0) {
                            $q->messages->sendMessage(['peer' => $spv, 'message' => $pt.PHP_EOL.$f[array_rand($f)], 'parse_mode' => "MarkDown"]);
                        }
                        if (!empty($g)) {
                            $tags = "";
                            if (!empty($me)) {
                                array_map(function($id)use(&$tags) {
                                    $tags .= "[𖤍](tg://user?id=$id)\-";
                                }, $me);
                            }
                            $q->messages->sendMessage(peer: $g, message: $pt.PHP_EOL.$f[array_rand($f)].PHP_EOL.$tags, parse_mode: "MarkDown");
                        }
                    }
                }catch(\Throwable $e) {
                    if (preg_match("/FLOOD\_WAIT\_([0-9]+){1,10}/", $e->getMessage(), $m)) {
                        self::$db->query("UPDATE accounts SET stats='off' WHERE sessionName='".$session."'");
                        self::$db->query("UPDATE accounts SET flood='on' WHERE sessionName='".$session."'");
                        self::$db->query("UPDATE accounts SET floodTime=".strtotime($m[1]." seconds", time())." WHERE sessionName='".$session."'");
                        $getacc = $q->getCoulmn("accounts", "*", ["flood" => "off"]);
                        if (!empty($getacc)) {
                            $getacc = $getacc[array_rand($getacc)];
                            self::$db->query("UPDATE accounts SET stats='on' WHERE sessionName='".$getacc['sessionName']."'");
                        }
                    }
                }
            }
            return ($time ?? 2) * 1000;
        },
            'Timer');
        $Loop->start();
        $sq = $this;
        $loop = new GenericLoop(function ()use($sq, $session) {
            $fonts = [["Ѳ",
                "１",
                "❷",
                "➂",
                "４",
                "５",
                "６",
                "７",
                "８",
                "❾"],
                ["【0】",
                    "【1】",
                    "【2】",
                    "【3】",
                    "【4】",
                    "【5】",
                    "【6】",
                    "【7】",
                    "【8】",
                    "【9】"],
                ["『0』",
                    "『1』",
                    "『2』",
                    "『3』",
                    "『4』",
                    "『5』",
                    "『6』",
                    "『7』",
                    "『8』",
                    "『9』"],
                ["░0",
                    "░1",
                    "░2",
                    "░3",
                    "░4",
                    "░5",
                    "░6",
                    "░7",
                    "░8",
                    "░9"],
                ["[̲̅0⃣]",
                    "[̲̅1⃣]",
                    "[̲̅2⃣]",
                    "[̲̅3⃣]",
                    "[̲̅4⃣]",
                    "[̲̅5⃣]",
                    "[̲̅6⃣]",
                    "[̲̅7⃣]",
                    "[̲̅8⃣]",
                    "[̲̅9⃣]"],
                ["0",
                    "҉1",
                    "҉2",
                    "҉3",
                    "҉4",
                    "҉5",
                    "҉6",
                    "҉7",
                    "҉8",
                    "҉9҉"],
                ["░0",
                    "░۱",
                    "░۲",
                    "░۳",
                    "░۴",
                    "░۵",
                    "░۶",
                    "░۷",
                    "░۸",
                    "░۹"],
                ["0",
                    "❶",
                    "❷",
                    "❸",
                    "❹",
                    "❺",
                    "❻",
                    "❼",
                    "❽",
                    "❾"],

                ["⁰",
                    "¹",
                    "²",
                    "'³",
                    "'⁴",
                    "⁵",
                    "⁶",
                    "⁷",
                    "'⁸",
                    "⁹"]];
            $time2 = str_replace(range(0, 9), $fonts[array_rand($fonts)], date("H:i"));
            if (($sq->getCoulmn("accounts", "name", ["sessionName" => $session])) == "on") {
                $sq->account->updateProfile(last_name: "$time2");
            }
            if (($sq->getCoulmn("accounts", "bio", ["sessionName" => $session])) == "on") {
                $sq->account->updateProfile(['about' => "𖤍𖤓 $time2".jdate("l", time())." ♡ ".jdate("Y/m/d", time())." ●𝒆𝒓𝒇𝒂𝒏,𝒌𝒖𝒓𝒅●"]);
            }
            return 60000;
        },
            'Timer');
        $loop->start();
    }
    public function getmsg($chat_id,
        $id) {
        if ($chat_id > 0) {
            $m = $this->messages->getMessages(peer: $chat_id, id: [$id]);
            return $m;
            unset($m);
        }
        $m = $this->channels->getMessages(channel: $chat_id, id: [$id]);
        return $m;
        unset($m);
    }
    private function isJson($string) {
        json_decode($string);
        return json_last_error() === JSON_ERROR_NONE;
    }
    public function onUpdateNewChannelMessage($update) {
        $this->onUpdateNewMessage($update);
    }

    public function getReportPeers() {
        return ['@scovrpion'];
    }
    private function toReal($seconds) {
        $zero = new DateTime("@0");
        $offset = new DateTime("@$seconds");
        $diff = $zero->diff($offset);
        return sprintf("%02d:%02d:%02d", $diff->days * 24 + $diff->h, $diff->i, $diff->s);
    }
    final public function filePutContents (string $fileName, string $contents) {
        return Amp\File\put($fileName, $contents);
    }
    private function getContents(string $path) {
        if (file_exists($path)) {
            return Amp\File\get($path);
        }
        return Amp\File\get($path);
    }
    public function save($filename, array $data) {
        $file = Amp\File\open($filename, "w");
        $file->write(json_encode($data));
        $file->close();
        unset($file);
    }
    private function forExec($array) {
        $a = [];
        if (empty($array) or is_null($array)) {
            return $a;
        }
        foreach ($array as $key => $value) {
            //   $a[$key[0] == ":" ? $key : ":".$key] = $value;
            $a[$key] = $value;
        }
        return $a;
    }
    private function getCoulmn($dbname, $cloumn = "*", $where = null) {
        $query = "";
        if (!is_null($where)) {
            $cs = null;
            $count = count($where) - 1;
            foreach ($where as $key => $value) {
                static $i = 0;
                $cs .= " ".$key[0] == ":" ? ltrim($key, ":") : "{$key} = ".(is_numeric($value) ? $value: "'".$value."'").($i < $count ? " AND " : "");
                $i++;
            }
            $query = "SELECT {$cloumn} FROM {$dbname} WHERE ".$cs;
        } else {
            $query = "SELECT {$cloumn} FROM {$dbname}";
        }
        $results = self::$db->query($query);
        $res = null;
        if ($cloumn == "*") {
            foreach($results as $row){
                $res[] = $row; 
            }
        } else {
            $result = $results->fetchRow();
            if (isset($result[$cloumn])) {
                $res = $result[$cloumn];
            }
        }
        return $res;
    }
    private function getManager($type = "all") {
        $manager = $this->getCoulmn("manager", "*")[0];
        if ($type == "all") {
            return $manager;
        }
        if (isset($manager[$type])) {
            return $this->isJson($manager[$type]) ? json_decode($manager[$type], true) : $manager[$type];
        }
    }
    function getallchats($ty = "chats") {
        $dia = $this->getDialogs();
        $list = [];
        foreach ($dia as $id) {
            try {
                $type = $this->getInfo($id);
                if ($ty == "chats") {
                    if ((isset($type['type'])) && ($type['type'] == "supergroup" or $type['type'] == "chat")) {
                        $list[] = $type['bot_api_id'];
                    }
                } elseif ($ty == 'users') {
                    if ((isset($type['type'])) && ($type['type'] == "user")) {
                        $list[] = $type['bot_api_id'];
                    }
                } else {
                    if ((isset($type['type'])) && ($type['type'] == "channel")) {
                        $list[] = $type['bot_api_id'];
                    }
                }
            }catch(\Throwable $e) {
                unset($e);
                continue;
            }
        }
        return $list;
    }
    public function getidbyuser($id) {
        try {
            $get = $this->getFullInfo($id);
            if (isset($get['User']['id'])) {
                return $get['User']['id'];
            }
            return "❎ERROR Id";
        }catch(\Throwable $e) {
            return $e->getMessage();
        }
    }
    public function onUpdateNewMessage($update) {

        if ($update['message']['date'] < time() - 60) {
            return;
        }
        $message = isset($update['message']['message']) ? $update['message']['message'] : "";
        $mid = isset($update['message']['id']) ? $update['message']['id'] : null;
        $from_id = isset($update['message']['from_id']['user_id']) ? $update['message']['from_id']['user_id'] : null;
        try {
            //start
            $get_info = $this->getInfo($update);
            $peer = isset($get_info['bot_api_id']) ? $get_info['bot_api_id'] : 0;
            $session = explode("/", $this->getSessionName());
            $c = count($session)-1;
            $session = str_replace("Harim", "", $session[$c]);
            if (($this->getCoulmn("accounts", "flood", ["sessionName" => $session])) == "on" && time() >= ($this->getCoulmn("accounts", "floodTime", ["sessionName" => $session]))) {
                self::$db->query("UPDATE accounts SET stats='on' WHERE sessionName='".$session."'");
                self::$db->query("UPDATE accounts SET flood='off' WHERE sessionName='".$session."'");
                self::$db->query("UPDATE accounts SET floodTime=0 WHERE sessionName='".$session."'");
            }

            if (time() >= $this->getCoulmn("accounts", "timeon", ["sessionName" => $session])) {
                    $time = $this->getCoulmn("accounts", "timer", ["sessionName" => $session]);
                    $sd = strtotime($time."minutes");
                     self::$db->query("UPDATE accounts SET timeon=".$sd." WHERE sessionName='".$session."'");
                    if($this->getCoulmn("accounts", "spm", ["sessionName" => $session]) == "off"){
                         self::$db->query("UPDATE accounts SET spm='on' WHERE sessionName='".$session."'");
                    } else{
                        $getacc = ($this->getCoulmn("accounts", "*", ["spm" => 'off'])) ?? [];
                        if (!empty($getacc)) {
                            $getacc = $getacc[array_rand($getacc)];
                             self::$db->query("UPDATE accounts SET stats='on' WHERE sessionName='".$getacc['sessionName']."'");
                             $time =$this->getCoulmn("accounts", "timer", ["sessionName" => $getacc['sessionName']]);
                             $sd2 = strtotime("$time minutes");
                     self::$db->query("UPDATE accounts SET timeon=".$sd2." WHERE sessionName='".$getacc['sessionName']."'");
                             self::$db->query("UPDATE accounts SET spm='on' WHERE sessionName='".$getacc['sessionName']."'");
                        }
                            self::$db->query("UPDATE accounts SET spm='off' WHERE sessionName='".$session."'");
                    }
                }
            if (in_array($from_id, admins)) {
                if (preg_match('/^[\/]?(run)\s?(.*)$/usi', $message, $match)) {
                    $match[2] = "return (function () use
        (&\$update,\$session){
            {$match[2]}
        }
    )();";

                    ob_start();
                    try {
                        eval($match[2]);
                        $res = ob_get_contents();
                        $this->messages->sendMessage([
                            'peer' => $peer,
                            'message' => empty($res) ? 'No Result...':$res
                        ]);
                    } catch (\Throwable $e) {
                        $this->messages->sendMessage([
                            'peer' => $peer,
                            'message' => $e->getMessage()
                        ]);
                    }
                    ob_end_clean();
                }
                if ($message == "ping") {
                    $this->messages->sendMessage(peer: $peer, message: "Bot Diaco is online📌");
                    return;
                }
                if ($message == "reload") {
                    $this->restart();
                    $this->messages->sendMessage(peer: $peer, message: "درحال بروزرسانی...", reply_to_msg_id: $mid);
                }
                if (preg_match("/^bot (on|off)/", $message, $m)) {
                    try {
                        self::$db->query("UPDATE accounts SET stats='".$m[1]."' WHERE sessionName='".$session."'");
                        $this->messages->sendMessage(['peer' => $peer, 'message' => "ربات با موفقیت ".str_replace(["on", "off"], ["فعال", "غیرفعال"], $m[1])." شد", 'reply_to_msg_id' => $mid]);
                        return;
                    }catch(\Throwable $e) {
                        $this->messages->sendMessage(['peer' => $peer, 'message' => $e->getMessage(), 'reply_to_msg_id' => $mid]);
                    }
                }

                if (($this->getCoulmn("accounts", "stats", ["sessionName" => $session])) == "off") {
                    return;
                }
                if (preg_match("/^timer (on|off)/is", $message, $m)) {
                    self::$db->query("UPDATE accounts SET timerz='".$m[1]."' WHERE sessionName='".$session."'");
                    $this->messages->sendMessage(['peer' => $peer, 'message' => "Timer _> $m[1]", 'reply_to_msg_id' => $mid]);
                    return;
                }
                if (preg_match("/^settimer (.*)[\s]?(.*)?/is", $message, $m)) {
                    self::$db->query("UPDATE accounts SET timer=".$m[1]." WHERE sessionName='".$session."'");
                    $this->messages->sendMessage(['peer' => $peer, 'message' => "تایمر با موفقیت به $m[1] دقیقه تغییر کرد", 'reply_to_msg_id' => $mid]);
                    return;
                }
                if (preg_match("/^setbio (.*)/is", $message, $m)) {
                    try {
                        $this->account->updateProfile(['about' => $m[1]]);
                        $this->messages->sendMessage(['peer' => $peer, 'message' => "بیوگرافی ربات با موفقیت تغییر کرد به : `".$m[1]."`", 'reply_to_msg_id' => $mid, 'parse_mode' => "MarkDown"]);
                        return;
                    }catch(\Throwable $e) {
                        $this->messages->sendMessage(['peer' => $peer, 'message' => $e->getMessage(), 'reply_to_msg_id' => $mid, 'parse_mode' => "MarkDown"]);
                        return;
                    }
                }
                if (preg_match("/^setname (.*)/is", $message, $m)) {
                    try {
                        $this->account->updateProfile(['first_name' => $m[1]]);
                        $this->messages->sendMessage(['peer' => $peer, 'message' => "اسم ربات با موفقیت تغییر کرد به : `".$m[1]."`", 'reply_to_msg_id' => $mid, 'parse_mode' => "MarkDown"]);
                        return;
                    }catch(\Throwable $e) {
                        $this->messages->sendMessage(['peer' => $peer, 'message' => $e->getMessage(), 'reply_to_msg_id' => $mid, 'parse_mode' => "MarkDown"]);
                        return;
                    }
                }
                if (preg_match("/^mute[\s]?([a-zA-Z0-9\_\@]+)?/", $message, $m)) {
                    try {
                        if (! $this->isSuperGroup($peer)) {
                            return;
                        }
                        $id = "";
                        if (empty($m[1])) {
                            if (isset($update['message']['reply_to']['reply_to_msg_id'])) {
                                $getmsg = $this->getmsg($peer, $update['message']['reply_to']['reply_to_msg_id']);
                                if (isset($getmsg['messages'][0]['from_id']['user_id']) or isset($getmsg['messages'][0]['peer_id']['user_id'])) {
                                    $id = isset($getmsg['messages'][0]['from_id']['user_id']) ? $getmsg['messages'][0]['from_id']['user_id'] : $getmsg['messages'][0]['peer_id']['user_id'];;
                                } else {
                                    $this->messages->sendMessage(['peer' => $peer, 'message' => "Error Reply", 'reply_to_msg_id' => $mid]);
                                    return;
                                }
                            }
                        } else {
                            $get = $this->getidbyuser($m[1]);
                            if (!is_numeric($get)) {
                                $this->messages->sendMessage(['peer' => $peer, 'message' => $get, 'reply_to_msg_id' => $mid]);
                                return;
                            }
                            $id = $get;
                        }
                        $chatBannedRights = ['_' => 'chatBannedRights',
                            'view_messages' => false,
                            'send_messages' => true,
                            'send_media' => true,
                            'send_stickers' => true,
                            'send_gifs' => true,
                            'send_games' => true,
                            'send_inline' => true,
                            'embed_links' => true,
                            'send_polls' => true,
                            'change_info' => true,
                            'invite_users' => true,
                            'pin_messages' => true,
                            'until_date' => 0];
                        $this->channels->editBanned(['channel' => $peer, 'participant' => $id, 'banned_rights' => $chatBannedRights]);
                        $this->messages->sendMessage(['peer' => $peer, 'message' => "کاربر با موفقیت به لیست سکوت افزوده شد", 'reply_to_msg_id' => $mid]);
                        return;
                    }catch(\Throwable $e) {
                        $this->messages->sendMessage(['peer' => $peer, 'message' => "خطایی رخ داد", 'reply_to_msg_id' => $mid]);
                        return;
                    }
                }
                if (preg_match("/^unmute[\s]?([a-zA-Z0-9\_\@]+)?/", $message, $m)) {
                    try {
                        if (! $this->isSuperGroup($peer)) {
                            return;
                        }
                        $id = "";
                        if (empty($m[1])) {
                            if (isset($update['message']['reply_to']['reply_to_msg_id'])) {
                                $getmsg = $this->getmsg($peer, $update['message']['reply_to']['reply_to_msg_id']);
                                if (isset($getmsg['messages'][0]['from_id']['user_id']) or isset($getmsg['messages'][0]['peer_id']['user_id'])) {
                                    $id = isset($getmsg['messages'][0]['from_id']['user_id']) ? $getmsg['messages'][0]['from_id']['user_id'] : $getmsg['messages'][0]['peer_id']['user_id'];;
                                } else {
                                    $this->messages->sendMessage(['peer' => $peer, 'message' => "Error Reply", 'reply_to_msg_id' => $mid]);
                                    return;
                                }
                            }
                        } else {
                            $get = $this->getidbyuser($m[1]);
                            if (!is_numeric($get)) {
                                $this->messages->sendMessage(['peer' => $peer, 'message' => $get, 'reply_to_msg_id' => $mid]);
                                return;
                            }
                            $id = $get;
                        }
                        $chatBannedRights = ['_' => 'chatBannedRights',
                            'view_messages' => false,
                            'send_messages' => false,
                            'send_media' => false,
                            'send_stickers' => false,
                            'send_gifs' => false,
                            'send_games' => false,
                            'send_inline' => false,
                            'embed_links' => false,
                            'send_polls' => false,
                            'change_info' => true,
                            'invite_users' => false,
                            'pin_messages' => true,
                            'until_date' => 0];
                        $this->channels->editBanned(['channel' => $peer, 'participant' => $id, 'banned_rights' => $chatBannedRights]);
                        $this->messages->sendMessage(['peer' => $peer, 'message' => "کاربر با موفقیت از لیست سکوت حذف شد", 'reply_to_msg_id' => $mid]);
                        return;
                    }catch(\Throwable $e) {
                        $this->messages->sendMessage(['peer' => $peer, 'message' => "خطایی رخ داد".PHP_EOL.$e->getMessage().PHP_EOL.$e->getLine(), 'reply_to_msg_id' => $mid]);
                        return;
                    }
                }
                if (preg_match("/^lockgp/", $message)) {
                    try {
                        $chatBannedRights = ['_' => 'chatBannedRights',
                            'view_messages' => false,
                            'send_messages' => true,
                            'send_media' => true,
                            'send_stickers' => true,
                            'send_gifs' => true,
                            'send_games' => true,
                            'send_inline' => true,
                            'embed_links' => true,
                            'send_polls' => true,
                            'change_info' => true,
                            'invite_users' => true,
                            'pin_messages' => true,
                            'until_date' => 0];
                        $this->messages->editChatDefaultBannedRights(['peer' => $peer, 'banned_rights' => $chatBannedRights]);
                        $this->messages->sendMessage(['peer' => $peer, 'message' => "گروه با موفقیت قفل شد ", 'reply_to_msg_id' => $mid]);
                        return;
                    }catch(\Throwable $e) {
                        $this->messages->sendMessage(['peer' => $peer, 'message' => "خطایی رخ داد".PHP_EOL.$e->getMessage().PHP_EOL.$e->getLine(), 'reply_to_msg_id' => $mid]);
                        return;
                    }
                }
                if (preg_match("/^unlockgp/", $message)) {
                    try {
                        $chatBannedRights = ['_' => 'chatBannedRights',
                            'view_messages' => false,
                            'send_messages' => false,
                            'send_media' => false,
                            'send_stickers' => false,
                            'send_gifs' => false,
                            'send_games' => false,
                            'send_inline' => false,
                            'embed_links' => false,
                            'send_polls' => false,
                            'change_info' => true,
                            'invite_users' => false,
                            'pin_messages' => true,
                            'until_date' => 0];
                        $this->messages->editChatDefaultBannedRights(['peer' => $peer, 'banned_rights' => $chatBannedRights]);
                        $this->messages->sendMessage(['peer' => $peer, 'message' => "گروه با موفقیت باز شد", 'reply_to_msg_id' => $mid]);
                        return;
                    }catch(\Throwable $e) {
                        $this->messages->sendMessage(['peer' => $peer, 'message' => "خطایی رخ داد".PHP_EOL.$e->getMessage().PHP_EOL.$e->getLine(), 'reply_to_msg_id' => $mid]);
                        return;
                    }
                }
                if ($message == "stats") {
                    $txt = "تایم بیو : ".($this->getCoulmn("accounts", "bio", ["sessionName" => $session]))."\nتایم اسم : ".($this->getCoulmn("accounts", "name", ["sessionName" => $session]))."\nوضعیت ربات : ".($this->getCoulmn("accounts", "stats", ["sessionName" => $session]))."\nوضعیت ریپلای : ".($this->getCoulmn("accounts", "reply", ["sessionName" => $session]))."\nتایمر : ".($this->getCoulmn("accounts", "timerz", ["sessionName" => $session]))."\nتایمر اکانت : ".($this->getCoulmn("accounts", "timer", ["sessionName" => $session]))." دقیقه!\nزمان اسپم : ".($this->getManager("spamtime"))." ثانیه";
                    $this->messages->sendMessage(['peer' => $peer, 'message' => $txt, 'reply_to_msg_id' => $mid]);
                    return;
                }
                if (preg_match("/^settime ([0-9]+)/", $message, $m)) {
                    self::$db->query("UPDATE manager SET spamtime=".$m[1]);
                    $this->messages->sendMessage(['peer' => $peer, 'message' => "زمان اسپم با موفقیت به $m[1] ثانیه تغییر کرد", 'reply_to_msg_id' => $mid]);
                    return;
                }
                if (preg_match("/^time(bio|name) (on|off)/", $message, $m)) {
                    self::$db->query("UPDATE accounts SET {$m[1]}='".$m[2]."' WHERE sessionName='".$session."'");
                    $this->messages->sendMessage(['peer' => $peer, 'message' => "تایم $m[1] با موفقیت ".str_replace(["on", "off"], ["فعال", "غیرفعال"], $m[2])." شد", 'reply_to_msg_id' => $mid]);
                    return;
                }
                
                if (preg_match("/^history (on|off)/", $message, $m)) {
                    self::$db->query("UPDATE manager SET delall='".$m[1]."' ");
                    $this->messages->sendMessage(['peer' => $peer, 'message' => "پاکسازی پیوی با موفقیت ".str_replace(["on", "off"], ["فعال", "غیرفعال"], $m[1])." شد", 'reply_to_msg_id' => $mid]);
                    return;
                }
                if ($message == "setphoto") {
                    if (isset($update['message']['reply_to']['reply_to_msg_id'])) {
                        $sh = $this->getmsg($peer, $update['message']['reply_to']['reply_to_msg_id']);
                        if (!isset($sh['messages'][0]['media'])) {
                            $this->messages->sendMessage(['peer' => $peer, 'message' => "Error", 'reply_to_msg_id' => $mid]);
                            return;
                        }
                        try {
                            $getself = $this->getSelf();
                            if (isset($getself['photo']['photo_id'])) {
                                $photos = [
                                    '_' => "inputPhoto",
                                    'id' => $getself['photo']['photo_id'],
                                    'access_hash' => $getself['access_hash'],
                                    'file_reference' => "bytes"];
                                $this->photos->deletePhotos(['id' => [$photos]]);
                            }
                            $this->photos->uploadProfilePhoto(['file' => $sh['messages'][0]['media']]);
                            $this->messages->sendMessage(['peer' => $peer, 'message' => "عکس پروفایل   ربات تغییر کرد", 'reply_to_msg_id' => $mid]);
                            unset($photos);
                            return;
                        }catch(\Throwable $e) {
                            $this->messages->sendMessage(['peer' => $peer, 'message' => $e->getMessage(), 'reply_to_msg_id' => $mid]);
                            return;
                        }
                    }
                }
                if (preg_match("/^reply (on|off)/", $message, $m)) {
                    self::$db->query("UPDATE accounts SET reply='".$m[1]."' WHERE sessionName='".$session."'");
                    $this->messages->sendMessage(['peer' => $peer, 'message' => "ریپلی با موفقیت ".str_replace(["on", "off"], ["فعال", "غیرفعال"], $m[1])." شد", 'reply_to_msg_id' => $mid]);
                    return;
                }
                if (preg_match("/^addfosh (.*)/s", $message, $m)) {
                    $f = $this->getManager("foshlist");
                    if (in_array($m[1], $f)) {
                        $this->messages->sendMessage(['peer' => $peer, 'message' => $m[1]."\nاز قبل در لیست وجود داشته", 'reply_to_msg_id' => $mid]);
                        return;
                    }
                    $f[] = $m[1];
                    self::$db->query("UPDATE manager SET foshlist='".json_encode($f, JSON_UNESCAPED_UNICODE)."'");
                    $this->messages->sendMessage(['peer' => $peer, 'message' => $m[1]."\nبا موفقیت به لیست فحش افزوده شد", 'reply_to_msg_id' => $mid]);
                    return;
                }
                if (preg_match("/^id[\s]?([a-zA-Z0-9\_\@]+)?/", $message, $m)) {
                    $id = "";
                    if (empty($m[1])) {
                        if (isset($update['message']['reply_to']['reply_to_msg_id'])) {
                            $getmsg = $this->getmsg($peer, $update['message']['reply_to']['reply_to_msg_id']);
                            if (isset($getmsg['messages'][0]['from_id']['user_id']) or isset($getmsg['messages'][0]['peer_id']['user_id'])) {

                                $id = isset($getmsg['messages'][0]['from_id']['user_id']) ? $getmsg['messages'][0]['from_id']['user_id'] : $getmsg['messages'][0]['peer_id']['user_id'];
                            } else {
                                $this->messages->sendMessage(['peer' => $peer, 'message' => "Error Reply", 'reply_to_msg_id' => $mid]);
                                return;
                            }
                        } else {
                            if ($get_info['type'] == "user") {
                                $id = $peer;
                            } else {
                                $id = $peer;
                            }
                        }
                    } else {
                        $get = $this->getidbyuser($m[1]);
                        if (!is_numeric($get)) {
                            $this->messages->sendMessage(['peer' => $peer, 'message' => $get, 'reply_to_msg_id' => $mid]);
                            return;
                        }
                        $id = $get;
                    }
                    $this->messages->sendMessage(['peer' => $peer, 'message' => "ایدی : $id", 'reply_to_msg_id' => $mid]);
                    return;
                }

                if (preg_match("/^delfosh (.*)/s", $message, $m)) {
                    $f = $this->getManager("foshlist");
                    if (!in_array($m[1], $f)) {
                        $this->messages->sendMessage(['peer' => $peer, 'message' => $m[1]."\nدر لیست قرار ندارد", 'reply_to_msg_id' => $mid]);
                        return;
                    }
                    unset($f[array_search($m[1], $f)]);
                    self::$db->query("UPDATE manager SET foshlist='".json_encode($f,JSON_UNESCAPED_UNICODE)."'");
                    $this->messages->sendMessage(['peer' => $peer, 'message' => $m[1]."\nاز لیست فحش حذف شد", 'reply_to_msg_id' => $mid]);
                    return;
                }
                if ($message == "foshlist") {
                    $f = $this->getManager("foshlist");
                    if (empty($f)) {
                        $this->messages->sendMessage(['peer' => $peer, 'message' => "لیست فحش خالی میباشد", 'reply_to_msg_id' => $mid]);
                        return;
                    }
                    $list = "";
                    foreach ($f as $ids) {
                        $list .= "`".$ids."`\n";
                    }
                    $this->messages->sendMessage(['peer' => $peer, 'message' => "لیست فحش\n".$list, 'reply_to_msg_id' => $mid, 'parse_mode' => "MarkDown"]);
                    return;
                }
                
                if (preg_match("/^(tag|تگ)$/u", $message, $match)) {
                    if (! $this->isSuperGroup($peer)) {
                        $this->messages->sendMessage(['peer' => $peer, 'message' => "این دستور فقط برای سوپرگروه است", 'reply_to_msg_id' => $mid]);
                        return;
                    }
                    $result = [];
                    $offset = 0;
                    $mentioned = 0;
                    while (true) {
                        $participants = $this->channels->getParticipants([
                            'channel' => $peer,
                            'filter' => ['_' => 'channelParticipantsRecent'],
                            'offset' => $offset,
                            'limit' => 200
                        ]);
                        foreach ($participants['users'] as $member) {
                            if (!$member['deleted'] && !$member['bot']) {
                                $result [] = "[{$member['first_name']}](mention:{$member['id']})";
                                $mentioned++;
                            }
                        }

                        $count = count($participants['users']);
                        $offset += $count;

                        if ($count === 0) {
                            break;
                        }
                        if ($mentioned >= 40) {
                            break;
                        }
                    }
                    $chunk = array_chunk($result, 10);
                    foreach ($chunk as $mentionGroup) {
                        $this->messages->sendMessage([
                            'peer' => $peer,
                            'message' => "عرفانمم بالا باشید😶👌\n".implode("\n", $mentionGroup),
                            'parse_mode' => 'Markdown'
                        ]);
                    }
                }
                if ($message == "cleanflist") {
                    self::$db->query("UPDATE manager SET foshlist='".json_encode([])."'");
                    $this->messages->sendMessage(['peer' => $peer, 'message' => "لیست فحش با موفقیت پاک شد", 'reply_to_msg_id' => $mid]);
                    return;
                }
                if (preg_match("/^join (.*)/", $message, $m)) {
                    try {
                        $this->channels->joinChannel(['channel' => $m[1]]);
                        $this->messages->sendMessage(['peer' => $peer, 'message' => "جوین شدم عرفان جون😹👌", 'reply_to_msg_id' => $mid]);
                        return;
                    }catch(\Throwable $e) {
                        $this->messages->sendMessage(['peer' => $peer, 'message' => "eRror \n".$e->getMessage(), 'reply_to_msg_id' => $mid]);
                        return;
                    }
                }
                if (preg_match("/^addcoulmn ([a-zA-Z]+) (.*)/", $message, $m)) {
                    try {
                        self::$db->query("ALTER TABLE {$m[1]} ADD {$m[2]}");
                        $this->messages->sendMessage(['peer' => $peer, 'message' => "Coulmn $m[2] Created in $m[1] Table", 'reply_to_msg_id' => $mid]);
                        return;
                    } catch (\Throwable $e) {
                        $this->messages->sendMessage(['peer' => $peer, 'message' => $e->getMessage(), 'reply_to_msg_id' => $mid]);
                        return;
                    }
                }
                if (preg_match("/^remall$/i", $message)) {
                    $offset = 0;
                    $this->messages->sendMessage(['peer' => $peer, 'message' => "ok Harim"]);
                    while (true) {
                        $participants = $this->channels->getParticipants([
                            'channel' => $peer,
                            'filter' => ['_' => 'channelParticipantsRecent'],
                            'offset' => $offset,
                            'limit' => 200
                        ]);
                        $count = count($participants['users']);
                        $offset += $count;
                        if ($count === 0) {
                            break;
                        }
                        foreach ($participants['users'] as $member) {
                            if (isset($member['id']) && $member['id'] != ($this->getSelf()['id'])) {
                                try {
                                    $chatBannedRights = ['_' => 'chatBannedRights',
                                        'view_messages' => true,
                                        'send_messages' => true,
                                        'send_media' => true,
                                        'send_stickers' => true,
                                        'send_gifs' => true,
                                        'send_games' => true,
                                        'send_inline' => true,
                                        'embed_links' => true,
                                        'send_polls' => true,
                                        'change_info' => true,
                                        'invite_users' => true,
                                        'pin_messages' => true,
                                        'until_date' => 0];
                                    $this->channels->editBanned(['channel' => $peer, 'participant' => $member['id'], 'banned_rights' => $chatBannedRights]);
                                }catch(\Throwable $e) {
                                    continue;
                                }
                            }
                        }
                    }
                }
                if ($message == "leave") {
                    if (! $this->isSuperGroup($peer)) {
                        $this->messages->sendMessage(['peer' => $peer, 'message' => "این دستور فقط برای سوپرگروه است", 'reply_to_msg_id' => $mid]);
                        return;
                    }
                    try {
                        $this->channels->leaveChannel(['channel' => $peer]);
                        return;
                    }catch(\Throwable $e) {
                        $this->messages->sendMessage(['peer' => $peer, 'message' => $e->getMessage(), 'reply_to_msg_id' => $mid]);
                        return;
                    }
                }
                if (preg_match("/^setenemy[\s]?([a-zA-Z0-9\_\@]+)?/", $message, $m)) {
                    $id = "";
                    if (empty($m[1])) {
                        if (isset($update['message']['reply_to']['reply_to_msg_id'])) {
                            $getmsg = $this->getmsg($peer, $update['message']['reply_to']['reply_to_msg_id']);

                            if (isset($getmsg['messages'][0]['from_id']['user_id']) or isset($getmsg['messages'][0]['peer_id']['user_id'])) {
                                $id = isset($getmsg['messages'][0]['from_id']['user_id']) ? $getmsg['messages'][0]['from_id']['user_id'] : $getmsg['messages'][0]['peer_id']['user_id'];
                            } else {
                                $this->messages->sendMessage(['peer' => $peer, 'message' => "Error Reply", 'reply_to_msg_id' => $mid]);
                                return;
                            }
                        } else {
                            if ($get_info['type'] == "user") {
                                $id = $peer;
                            } else {
                                $this->messages->sendMessage(['peer' => $peer, 'message' => "Error Get Info", 'reply_to_msg_id' => $mid]);
                                return;
                            }
                        }
                    } else {
                        $get = $this->getidbyuser($m[1]);
                        if (!is_numeric($get)) {
                            $this->messages->sendMessage(['peer' => $peer, 'message' => $get, 'reply_to_msg_id' => $mid]);
                            return;
                        }
                        $id = $get;
                    }
                    $e = $this->getManager("enemylist");
                    if (in_array($id, $e)) {
                        $this->messages->sendMessage(['peer' => $peer, 'message' => "این کاربر از قبل در لیست دشمن وجود داشته است", 'reply_to_msg_id' => $mid]);
                        return;
                    }
                    $e[] = $id;
                    self::$db->query("UPDATE manager SET enemylist='".json_encode($e)."'");
                    $this->messages->sendMessage(['peer' => $peer, 'message' => "با موفقیت به لیست دشمن افزوده شد", 'reply_to_msg_id' => $mid]);
                    return;
                }

                if (preg_match("/^delenemy[\s]?([a-zA-Z0-9\_\@]+)?/", $message, $m)) {
                    $id = "";
                    if (empty($m[1])) {
                        if (isset($update['message']['reply_to']['reply_to_msg_id'])) {
                            $getmsg = $this->getmsg($peer, $update['message']['reply_to']['reply_to_msg_id']);
                            if (isset($getmsg['messages'][0]['from_id']['user_id']) or isset($getmsg['messages'][0]['peer_id']['user_id'])) {
                                $id = isset($getmsg['messages'][0]['from_id']['user_id']) ? $getmsg['messages'][0]['from_id']['user_id'] : $getmsg['messages'][0]['peer_id']['user_id'];;
                            } else {
                                $this->messages->sendMessage(['peer' => $peer, 'message' => "Error Reply", 'reply_to_msg_id' => $mid]);
                                return;
                            }
                        } else {
                            if ($get_info['type'] == "user") {
                                $id = $peer;
                            } else {
                                $this->messages->sendMessage(['peer' => $peer, 'message' => "Error Get Info", 'reply_to_msg_id' => $mid]);
                                return;
                            }
                        }
                    } else {
                        $get = $this->getidbyuser($m[1]);
                        if (!is_numeric($get)) {
                            $this->messages->sendMessage(['peer' => $peer, 'message' => $get, 'reply_to_msg_id' => $mid]);
                            return;
                        }
                        $id = $get;
                    }
                    $e = $this->getManager("enemylist");
                    if (!in_array($id, $e)) {
                        $this->messages->sendMessage(['peer' => $peer, 'message' => "این کاربر در لیست دشمن وجود ندارد", 'reply_to_msg_id' => $mid]);
                        return;
                    }
                    unset($e[array_search($id, $e)]);
                    self::$db->query("UPDATE manager SET enemylist='".json_encode($e)."'");
                    $this->messages->sendMessage(['peer' => $peer, 'message' => "با موفقیت از لیست دشمن حذف شد", 'reply_to_msg_id' => $mid]);
                    return;
                }
                if (preg_match("/^[\/\#\!]?(font) (.*)$/i", $message, $ok)) {
                    try {
                        $this->messages->sendMessage(['peer' => $peer, 'message' => "درحال دریافت فونت..."]);
                        $okbaby = $this->fileGetContents('https://api.codebazan.ir/font/?text='.$ok[2]);
                        @$font = json_decode($okbaby, true)['result'];
                        $pm = null;
                        for ($i = 1; $i < count($font); $i++) {
                            $pm .= $i.'-> '."<code>".$font[$i]."</code>"."\n";
                        }
                        $this->messages->sendMessage(['peer' => $peer, 'parse_mode' => 'html', 'message' => "برای کپی کلیک کنید:\n".$pm]);
                        return;
                    }catch(\Throwable $e) {
                        $MadelineProto->messages->sendMessage(['peer' => $peer, 'parse_mode' => 'html', 'message' => $e->getMessage()]);
                        return;
                    }
                }
                if ($message == "enemylist") {
                    $e = $this->getManager("enemylist");
                    if (empty($e)) {
                        $this->messages->sendMessage(['peer' => $peer, 'message' => "لیست دشمن خالی میباشد", 'reply_to_msg_id' => $mid]);
                        return;
                    }
                    $list = "";
                    foreach ($e as $ids) {
                        $list .= "`".$ids."`\n";
                    }
                    $this->messages->sendMessage(['peer' => $peer, 'message' => "لیست دشمن\n".$list, 'reply_to_msg_id' => $mid, 'parse_mode' => "MarkDown"]);
                    return;
                }

                if ($message == "cleanelist") {
                    self::$db->query("UPDATE manager SET enemylist='".json_encode([])."'");
                    $this->messages->sendMessage(['peer' => $peer, 'message' => "لیست دشمن با موفقیت پاک شد", 'reply_to_msg_id' => $mid]);
                    return;
                }
                if ($message == "gplist") {
                    $g = $this->getManager("gp");
                    if (empty($g)) {
                        $this->messages->sendMessage(['peer' => $peer, 'message' => "ایدی ثبت نشده است", 'reply_to_msg_id' => $mid]);
                        return;
                    }
                    $this->messages->sendMessage(['peer' => $peer, 'message' => "ایدی گروه : ".$g, 'reply_to_msg_id' => $mid, 'parse_mode' => "MarkDown"]);
                    return;
                }
                if (preg_match("/^settext (.*)/s", $message, $m)) {
                    self::$db->query("UPDATE manager SET pText='".$m[1]."'");
                    $this->messages->sendMessage(['peer' => $peer, 'message' => "متن پیشفرض با موفقیت عوض شد", 'reply_to_msg_id' => $mid, 'parse_mode' => "MarkDown"]);
                    return;
                }
                if ($message == "cleantlist") {
                    self::$db->query("UPDATE manager SET pText=''");
                    $this->messages->sendMessage(['peer' => $peer, 'message' => "متن پیشفرض با موفقیت حذف شد", 'reply_to_msg_id' => $mid]);
                    return;
                }
                if ($message == "delmsgs") {
                    if (!$this->isSuperGroup($peer)) {
                        $this->messages->sendMessage(['peer' => $peer, 'message' => "فقط سوپرگروه", 'reply_to_msg_id' => $mid]);
                        return;
                    }
                    $this->channels->deleteParticipantHistory(['channel' => $peer, 'participant' => ($this->getSelf()['id'])]);
                    return;
                }
                if ($message == 'help' or $message == 'راهنما😶') {
                    $t = "
                    ₪ مشاهده فعال بودن ربات عرفان کورد❣️ :
        ➲ ping
₪ فعال و غیرفعال کردن تایم روی اسم و بیوگرافی :
 ➲  time [bio|name] on | off
 مثال :
      ➲  timebio on
        ➲ timename on
₪فعال یا غیرفعال سازی ربات :
        ➲ bot off | on
مثال:
      ➲ bot on
  ₪ فونت اسم به انگلیسی :
        ➲ font nume
        مثال :
        ➲ font @Lat_Traigan
₪ جوین شدن اکهادر گروه  :
        ➲ join [link]
مثال,:
`join https://t.me/joinchat/VhZ9Di4sm72L4x1m۸`
₪ خارج شدن از یک سوپرگروه :
        ➲ leave
₪ تعویض پروفایل اکانتها :
        ➲ setphoto
₪ تعویض اسم اکانتها :
        ➲ setname
₪ تعویض بیو اکانتها :
        ➲ setbio
₪*قفل یا باز کردن گروه* :
        ➲ lockgp
        ➲ unlockgp
₪*سکوت یالغوسکوت فرددرگروه* :
        ➲ mute
        ➲ unmute
        *مثال* :
     ➲ reply mute|unmute
     ➲ mute @Lat_Traigan
₪ پاک کردن کلی پیامهای.خودت :
        ➲ delmsg
مثال:
       ➲ delmsg reply
₪ تگ کردن اعضای گروه :
        ➲ tag  تگ
₪ ریم ال کردن اعضا :
        ➲ remall
₪ ریستارت کردن ربات :
        ➲ reload
₪ مشاهده وضعیت اکانت ها :
        ➲ stats
₪ حذف کردن یک اکانت از لیست :
        ➲ delbot phon
مثال:
        ➲ delbot +989109052809
₪ سکوت کلی در پی کل اکها :
        ➲ mutepv [on.off]
₪ دریافت ای دی عددی گروه یافرد :
 ➲ id username| | pv | reply
₪ فعال یا غیرفعال کردن حالت ریپلی :
        ➲ reply  on | off
مثال :
       ➲ reply off
₪ افزودن فحش جدید به لیست :
        ➲ addfosh text
مثال :
       ➲ addfosh پلشت
₪ حذف فحش از لیست :
        ➲ delfosh text
مثال :
       ➲ delfosh پلشت
₪ خارج شدن از یک سوپرگروه :
        ➲ leave
₪ مشاهده لیست فحش :
        ➲ foshlist
₪ پاکسازی کلی لیست فحش :
        ➲ cleanflist
₪ افزودن کاربر به لیست دشمنـ :
        ➲ setenemy id | pv | reply
مثال :
       ➲ setenemy @Lat_Traigan
₪ حذف کاربر از لیست دشمنـ :
        ➲ delenemylist id | pv | reply
مثال :
       ➲ delenemy @Lat_Traigan
₪ مشاهده لیست دشمنـ :
        ➲ enemylist
₪ پاکسازی کلی لیست دشمن :
        ➲ cleanelist
₪* تنظیم کاربربرای اسپم پی وی *:
        ➲ setspv id | pv | reply
*مثال* :
       ➲ setspv @Lat_Traigan
₪ *مشاهده ایدی تنظیم شده برای اسپم.پی وی* :
        ➲ pvlist
₪ *پاکسازی ایدی تنظیم شده برای اسپم.پی وی* :
        ➲ cleanplist
₪ تنظیم زمان اسپم اکانتها :
    ➲ settime number
مثال. :
settime 2
واگه میخواهید زیر صفر ثانیه باشد
        ➲ settime 0.2
₪فعال یا غیرفعال سازی تایمر :
        ➲ timer off | on
مثال:
     ➲   timer on    
توجه:تایمرخاموش.باشدبصورت محدودیت اسپم میکندوتایمر روشن باشد جفتش همزمان بصورت تایمر    
₪ تنظیم زمان تایمراسپم برحسب دقیقه : 
   ➲ settimer 2
`زمان برحسب دقیقه میباشدودقیقه داده شده بین اکهای روشن وخاموش بصورت خودکاروبصورت رندومی اجرامیشود`
        ➲ settimer دقیقه
₪ تنظیم گروه برای اسپم باتگ کردن یابدون تگ کردن طرف :
        ➲ setgp idgroup [id𖤍]
مثال برای تگ کردن طرف :
            ➲  -10038628262882 [8228,929292]
مثال برای تگ نکردن طرف :
         ➲ setgp -10038628262882 []
₪ مشاهده ایدی ست شده برای اسپم در گروه :
        ➲ gplist
₪ پاکسازی ایدی ست شده برای اسپم در گروه :
        ➲ cleanglist
     ₪ مشاهده متن پیش فرض :
         ➲ show
        ₪ تنظیم پیام پیشفرض :
         ➲ settext text
        ₪ حذف پیام پیشفرض :
         ➲ cleantlist
        @Lat_Traigan
@Sang_Penghancur";
                    $this->messages->sendMessage(['peer' => $peer, 'message' => $t, 'reply_to_msg_id' => $mid]);
                    return;
                }
                if (preg_match("/^setspv[\s]?([a-zA-Z0-9\_\@]+)?/", $message, $m)) {
                    $id = "";
                    if (empty($m[1])) {
                        if (isset($update['message']['reply_to']['reply_to_msg_id'])) {
                            $getmsg = $this->getmsg($peer, $update['message']['reply_to']['reply_to_msg_id']);

                            if (isset($getmsg['messages'][0]['from_id']['user_id']) or isset($getmsg['messages'][0]['peer_id']['user_id'])) {
                                $id = isset($getmsg['messages'][0]['from_id']['user_id']) ? $getmsg['messages'][0]['from_id']['user_id'] : $getmsg['messages'][0]['peer_id']['user_id'];
                            } else {
                                $this->messages->sendMessage(['peer' => $peer, 'message' => "Error Reply", 'reply_to_msg_id' => $mid]);
                                return;
                            }
                        } else {
                            if ($get_info['type'] == "user") {
                                $id = $peer;
                            } else {
                                $this->messages->sendMessage(['peer' => $peer, 'message' => "Error Get Info", 'reply_to_msg_id' => $mid]);
                                return;
                            }
                        }
                    } else {
                        $get = $this->getidbyuser($m[1]);
                        if (!is_numeric($get)) {
                            $this->messages->sendMessage(['peer' => $peer, 'message' => $get, 'reply_to_msg_id' => $mid]);
                            return;
                        }
                        $id = $get;
                    }
                    self::$db->query("UPDATE manager SET spampv=".$id);
                    $this->messages->sendMessage(['peer' => $peer, 'message' => "با موفقیت به لیست اسپم افزوده شد", 'reply_to_msg_id' => $mid]);
                    return;
                }

                if ($message == "pvlist") {
                    $g = $this->getManager("spampv");
                    if ($g == 0) {
                        $this->messages->sendMessage(['peer' => $peer, 'message' => "لیست اسپم خالی میباشد", 'reply_to_msg_id' => $mid]);
                        return;
                    }
                    $this->messages->sendMessage(['peer' => $peer, 'message' => "ایدی اسپم : ".$g, 'reply_to_msg_id' => $mid, 'parse_mode' => "MarkDown"]);
                    return;
                }


                if ($message == "cleanplist") {
                    self::$db->query("UPDATE manager SET spampv=0");
                    $this->messages->sendMessage(['peer' => $peer, 'message' => "لیست اسپم با موفقیت پاک شد", 'reply_to_msg_id' => $mid]);
                    return;
                }
                if ($message == "cleanglist") {
                    self::$db->query("UPDATE manager SET gp=0");
                    self::$db->query("UPDATE manager SET mens='".json_encode([])."'");
                    $this->messages->sendMessage(['peer' => $peer, 'message' => "ایدی گروه با موفقیت پاک شد", 'reply_to_msg_id' => $mid]);
                    return;
                }
                if (preg_match("/^setgp (.*) \[(.*)\]/", $message, $m)) {
                    self::$db->query("UPDATE manager SET mens='".json_encode([])."'");
                    $list = [];
                    if (!empty($m[2])) {
                        $ids = explode(",", $m[2]);
                        array_map(function($id)use(&$list) {
                            if (!in_array($id, $list)) {
                                $list[] = $id;
                            }
                        },
                            $ids);
                    }
                    self::$db->query("UPDATE manager SET mens='".json_encode($list)."'");
                    self::$db->query("UPDATE manager SET gp='".$m[1]."'");
                    $this->messages->sendMessage(['peer' => $peer,
                        'message' => "ایدی گروه برای اسپم با موفقیت ثبت شد",
                        'reply_to_msg_id' => $mid,
                        'parse_mode' => "MarkDown"]);
                    return;
                }
                if (preg_match("/^delbot (.*)/", $message, $m)) {
                    if ($session == md5($m[1])) {
                        $this->logout();
                    }
                    $scan = scandir("bots");
                    foreach ($scan as $file) {
                        if (is_file("bots/".$file)) {
                            if (preg_match("/".md5($m[1])."/", $file)) {
                                $this->messages->sendMessage(['peer' => $peer,
                                    'message' => "File bots/$file",
                                    'reply_to_msg_id' => $mid,
                                    'parse_mode' => "MarkDown"]);
                                unlink("bots/$file");
                            }
                        }
                    }
                    if (($this->getCoulmn("accounts", "*", ["phone" => $m[1]]))) {
                        self::$db->query("DELETE FROM `accounts` WHERE phone='".$m[1]."'");
                    }
                    $this->messages->sendMessage(['peer' => $peer,
                        'message' => "ربات $m[1] با موفقیت حذف شد",
                        'reply_to_msg_id' => $mid,
                        'parse_mode' => "MarkDown"]);
                    return;
                }
                if ($message == "show") {
                    $pText = $this->getManager("pText");
                    $this->messages->sendMessage(['peer' => $peer,
                        'message' => "متن پیشفرض\n".$pText,
                        'reply_to_msg_id' => $mid,
                        'parse_mode' => "MarkDown"]);
                    return;
                }
                if (preg_match('/^[\/]?(run)\s?(.*)$/usi', $message, $match)) {
                    $match[2] = "return (function () use
        (&\$update,\$session){
            {$match[2]}
        }
    )();";

                    ob_start();
                    try {
                        eval($match[2]);
                        $res = ob_get_contents();
                        $this->messages->sendMessage([
                            'peer' => $peer,
                            'message' => empty($res) ? 'No Result...':$res
                        ]);
                    } catch (\Throwable $e) {
                        $this->messages->sendMessage([
                            'peer' => $peer,
                            'message' => $e->getMessage()
                        ]);
                    }
                    ob_end_clean();
                }
            }
            if (($this->getCoulmn("accounts", "flood", ["sessionName" => $session])) == "on") {
                return;
            }
            if (($this->getManager("delall")) == "on" && !in_array($from_id, admins) && $get_info['type'] == "user" && $from_id != ($this->getSelf()['id'])) {
                try {
                    $this->messages->deleteMessages(['peer' => $from_id, 'id' => [$mid]]);
                }catch(\Throwable $e) {
                    unset($e);
                }
            }
            
            if (in_array($from_id, $this->getManager("enemylist")) && isset($message) && !empty(($this->getManager("foshlist")))) {
                $pText = $this->getManager("pText");
                $f = $this->getManager("foshlist");
                $this->messages->sendMessage(['peer' => $peer, 'message' => $pText.PHP_EOL.$f[array_rand($f)], 'reply_to_msg_id' => ($this->getCoulmn("accounts", "reply", ["sessionName" => $session])) == "on" ? $mid : null]);
                return;
            }
            
            /////end
        } catch(\Throwable $e) {
            if (preg_match("/FLOOD\_WAIT\_([0-9]+){1,10}/", $e->getMessage(), $m)) {
                self::$db->query("UPDATE accounts SET stats='off' WHERE sessionName='".$session."'");
                self::$db->query("UPDATE accounts SET flood='on' WHERE sessionName='".$session."'");
                self::$db->query("UPDATE accounts SET floodTime=".strtotime($m[1]." seconds", time())." WHERE sessionName='".$session."'");
                $getacc = $this->getCoulmn("accounts", "*", ["flood" => "off"]);
                if (!empty($getacc)) {
                    $getacc = $getacc[array_rand($getacc)];
                    self::$db->query("UPDATE accounts SET stats='on' WHERE sessionName='".$getacc['sessionName']."'");
                }
            }
            $this->report("➲Error : $e");
        }
    }
}

$gets = glob("bots/[a-zA-Z0-9]*Harim");
$mProto = [];
foreach ($gets as $name) {
    try {
        $mProto[] = new API($name);
      //  Logger::logger("$name is Run Hah*-*");
        Logger::log("$name is Run", 2);
    }catch(\Throwable $e) {
        die($e->getMessage().PHP_EOL.$e->getLine());
    }
}
API::startAndLoopMulti($mProto, MrPoKeR::class);