text: //////////////////////////////////////////
  
  const trees = require("../src/models");
  
  //////////////////////////////////////////
  
  const yread = require("../tools/yaml");
  
  //////////////////////////////////////////
  
  const CMNDS = {};
  const STATES = {};
  const SINFO = {};
  
  build();
  function build() {
    const source = yread(__dirname, "..", "constants", "commands.yaml");
    const { commands, templates } = source;
  
    for (const cmnd in commands) {
      const template = commands[cmnd].template;
      const target = { ...templates[template] };
  
      if (!target) throw new Error("Empty template");
      if (target.make)
        target.content = target.content.map((e) => e.map((i) => cmnd + i));
      commands[cmnd].template = target.content;
      commands[cmnd].trigger = commands[cmnd].trigger.map(
        (e) => new RegExp(`^${e}`, "i")
      );
  
      let forest = target.trees;
      if (commands[cmnd].trees) {
        const list = [];
        for (let i = 0; i < forest.length; ++i) {
          list.push(
            commands[cmnd].trees[i] === "+" ? forest[i] : commands[cmnd].trees[i]
          );
        }
        forest = list;
      }
  
      forest = forest.map((t) => trees[t]);
      commands[cmnd].trees = forest;
  
      CMNDS[cmnd] = commands[cmnd];
    }
  }
  
  states();
  function states() {
    const source = yread(__dirname, "..", "constants", "main.yaml");
  
    for (const state in source) {
      const one = source[state];
      STATES[state] = {};
  
      SINFO[state] = {};
  
      for (const cmnd of one.commands) STATES[state][cmnd] = CMNDS[cmnd];
      if (one.solution) {
        SINFO[state][one.solution] = CMNDS[one.solution];
        SINFO[state].$solv = CMNDS[one.solution];
      }
      if (one.base_access) {
        const base = source["base"]
        for (const cmnd of base.commands) STATES[state][cmnd] = CMNDS[cmnd];
      }
  
      SINFO[state].$reason = one.reason;
    }
  }
  
  function $cmnds(name) {
    if (!name) return CMNDS;
    return CMNDS[name];
  }
  
  function $states(mode) {
    if (!mode) return { ...STATES, ...SINFO };
    return { ...STATES[mode], ...SINFO[mode] };
  }
  
  function find(mode, cmnd) {
    let found = false;
    for (const ctr in STATES[mode]) {
      if (STATES[mode][ctr].trigger.filter((c) => c.test(cmnd)).length) {
        found = STATES[mode][ctr];
        break;
      }
    }
    return found;
  }
  
  function run(brick, ctx) {
    ctx.actions = brick.actions;
    ctx.btemplate = brick.btemplate;
    ctx.template = brick.template;
    ctx.trees = brick.trees;
    return brick.handler(ctx);
  }
  
  async function $handle(ctx) {
    const { user, command } = ctx;
    let { mode } = user;
    try {
      if(ctx.inner)
      {
        return await run(STATES["$inner"][ctx.command], ctx);
      }
      let brick = find(mode, command);
      if (brick) {
        ctx.reqCommand = brick.trigger[0].toString().replace(/[\/\\\^i]/g, "")  
        return await run(brick, ctx);
      }
      ctx.$reset();
      brick = find("base", command);
      if (brick && SINFO[mode].$solv)
        return await run(SINFO[mode].$solv, { ...ctx });
      ctx.$reset();
      return await run(STATES["$inner"]["cmnd_not_found"], ctx);
    } catch (ex) {
      console.log(ex);
      ctx.$reset();
      return await run(STATES["$inner"]["internal_bot"], ctx);
    }
  }
  
  function $fill(backet) {
    for (const name in backet.storage) {
      if (!CMNDS[name]) throw new Error(`Not existing command ${name}`);
      CMNDS[name].handler = backet.storage[name];
      CMNDS[name].frombacket = backet.name;
    }
  }
  
  module.exports = {
    $cmnds,
    $states,
    $handle,
    $fill,
  };
  