public function onEnable() { $this->saveDefaultConfig(); self::$token = $this->getConfig()->get("token", ""); self::$defaultChannel = $this->getConfig()->get("defaultChannel", ""); self::$updateInterval = $this->getConfig()->get("updateInterval", 20); if (self::$token === "") { $this->getLogger()->alert("You need to set your Telegram bot token to enable this plugin"); $this->getLogger()->alert("-> " . $this->getDataFolder() . "config.yml"); $this->getServer()->getPluginManager()->disablePlugin($this); return; } self::$broadcastToTelegram = $this->getConfig()->get("broadcastToTelegram", true); self::$broadcastTelegramMessages = $this->getConfig()->get("broadcastTelegramMessages", true); self::$enableTelegramCommands = $this->getConfig()->get("enableTelegramCommands", true); self::$disableWebPagePreview = $this->getConfig()->get("disableWebPagePreview", true); self::$enableMarkdownParsing = $this->getConfig()->get("enableMarkdownParsing", false); self::$debugMode = $this->getConfig()->get("debugMode", false); $handler = new EventHandler(); $this->getServer()->getPluginManager()->registerEvents($handler, $this); $this->getServer()->getPluginManager()->subscribeToPermission(Server::BROADCAST_CHANNEL_USERS, $handler); PocketTelegram::getMe(); PocketTelegram::getUpdates(); if ($this->getConfig()->get("broadcastServerStart", false) === true) { PocketTelegram::sendMessage(PocketTelegram::translateString("pocketmine.server.start", [TextFormat::AQUA . $this->getServer()->getVersion()]) . PHP_EOL . PocketTelegram::translateString("pocketmine.server.defaultGameMode", [Server::getGamemodeString($this->getServer()->getGamemode())]) . PHP_EOL . PHP_EOL . "#" . $this->getServer()->getMotd(), self::$defaultChannel); } }
/** * @param TextMessage $message */ private function handleTelegramCommands(TextMessage $message) { if (time() - $message->getDate() > 30) { return; } $chatId = $message->getChat()->getId(); if (!isset(self::$lastCommand[$chatId])) { self::$lastCommand[$chatId] = 0; } if (time() - self::$lastCommand[$chatId] < 2) { return; } $commands = $message->getCommands(); if (count($command = explode('@', $commands[0])) > 1) { if (!is_null($me = PocketTelegram::getMe()) and strToLower($command[1]) !== strToLower($me->getUsername())) { return; } $commands[0] = $command[0]; } switch (strToLower($commands[0])) { case "chat_id": PocketTelegram::sendMessage($chatId, $message->getChat(), $message); break; case "online": $players = array_map(function (Player $player) { return $player->getDisplayName(); }, array_filter(Server::getInstance()->getOnlinePlayers(), function (Player $player) { return $player->isOnline(); })); PocketTelegram::sendMessage(PocketTelegram::translateString("commands.players.list", [count($players), Server::getInstance()->getMaxPlayers()]) . PHP_EOL . implode(", ", $players), $message->getChat(), $message); break; case "stop": if (!is_null($from = $message->getFrom()) and !is_null($username = $from->getUsername()) and Server::getInstance()->getOfflinePlayer($username)->isOp()) { Command::broadcastCommandMessage($this, new TranslationContainer("commands.stop.start")); Server::getInstance()->shutdown(); } break; } self::$lastCommand[$chatId] = time(); }