• Модуль: imbot
  • Путь к файлу: ~/bitrix/modules/imbot/lib/bot/network.php
  • Класс: BitrixImBotBotNetwork
  • Вызов: Network::registerCommands
static function registerCommands(?int $botId = null): bool
{
	$botId = $botId ?: static::getBotId();
	if (!$botId)
	{
		return false;
	}

	if (!MainLoader::includeModule('im'))
	{
		return false;
	}

	$commandList = [];
	$res = ImModelCommandTable::getList([
		'filter' => [
			'=MODULE_ID' => static::MODULE_ID,
			'=BOT_ID' => $botId,
		]
	]);
	while ($row = $res->fetch())
	{
		$commandList[$row['COMMAND']] = $row;
	}

	ImCommand::clearCache();
	foreach (static::getCommandList() as $commandParam)
	{
		if (!isset($commandList[$commandParam['command']]))
		{
			ImCommand::register([
				'MODULE_ID' => static::MODULE_ID,
				'BOT_ID' => $botId,
				'COMMAND' => $commandParam['command'],
				'HIDDEN' => $commandParam['visible'] === true ? 'N' : 'Y',
				'CLASS' => $commandParam['class'] ?? static::class,
				'METHOD_COMMAND_ADD' => $commandParam['handler'] ?? 'onCommandAdd'
			]);
		}
		elseif (
			($commandList[$commandParam['command']]['CLASS'] != ($commandParam['class'] ?? static::class))
			|| ($commandList[$commandParam['command']]['METHOD_COMMAND_ADD'] != ($commandParam['handler'] ?? 'onCommandAdd'))
		)
		{
			ImCommand::update(
				['COMMAND_ID' => $commandList[$commandParam['command']]['ID']],
				[
					'HIDDEN' => $commandParam['visible'] === true ? 'N' : 'Y',
					'CLASS' => $commandParam['class'] ?? static::class,
					'METHOD_COMMAND_ADD' => $commandParam['handler'] ?? 'onCommandAdd'
				]
			);
		}
		unset($commandList[$commandParam['command']]);
	}
	foreach ($commandList as $commandParam)
	{
		ImCommand::unRegister(['COMMAND_ID' => $commandParam['ID']]);
	}

	return true;
}