Paste #46921: Untitled Paste

Date: 2018/03/16 19:12:20 UTC-07:00
Type: Denizen Script

View Raw Paste Download This Paste
Copy Link


# This is more a simple script error check that runs through all scripts and check if anything is missing

# This script checks for the following errors of a script:
# - file can't be parse as YAML
# - contains the right types (and correct format)
# - contains the script keys
# - typo mistakes (has definition which should be definitions for task types)

# This script cannot:
# - fix the errors for you as that can result in re-formatting your scripts
# - show the exact line of the error (recommended to use monkeybot on IRC/Discord)
# - find all errors as that requires knowledge of available commands/events/tags etc.

ScriptErrorHelper_Data:
    type: yaml data
    config:
        types:
        - "world"
        - "task"
        - "command"
        - "interact"
        - "assignment"
        - "yaml data"
        - "yaml data"
        - "format"
        - "inventory"

ScriptErrorHelper_Events:
    type: world
    events:

        on reload scripts:
        - if <context.haderror> {
            - wait 10t
            - inject ScriptErrorHelper_CheckAllScripts
        }

ScriptErrorHelper_CheckAllScripts:
    type: task
    script:
    - announce "<&e>Starting search of errors in denizen scripts..." to_ops
    - flag server SEH_Errors:0
    - foreach <server.list_files[/scripts]> {
        - if <def[value].ends_with[.yml]> {
            - ~run ScriptErrorHelper_CheckScripts_Task instantly context:<def[value]>
        }
    }
    - if <server.flag[SEH_Errors].round> > 0 {
        - announce "<&e>Found <&c><server.flag[SEH_Errors].round> <&e>errors in total"
    }
    else {
        - announce "<&a>All scripts has been verfied"
    }

ScriptErrorHelper_Found:
    type: task
    definitions: message
    script:
    - announce "SEH<&co> <&c><def[message]>" to_ops
    - flag server SEH_Errors:++

ScriptErrorHelper_CheckScripts_Task:
    type: task
    definitions: file
    script:
    - define full_path "/scripts/<def[file]>"
    - define id "SEH_File"
    - if <server.has_file[<def[full_path]>].not> {
        - define message "Could not find script file <def[file]>"
        - run ScriptErrorHelper_Found instantly context:<def[message]>
        - queue clear
    }
    - yaml "load:<def[full_path]>" id:<def[id]>
    - if <yaml.list.contains[<def[id]>].not> {
        - define message "could not parse YAML to <def[file]>"
        - run ScriptErrorHelper_Found instantly context:<def[message]>
        - queue clear
    }
    - foreach <yaml[<def[id]>].list_keys[]> {
        - narrate "<&7><def[value]>"
        - define path <def[value]>
        - inject ScriptErrorHelper_CheckScript
    }

ScriptErrorHelper_CheckScript:
    type: task
    # definitions: path
    script:
    - define check_path <def[path]>
    - inject ScriptErrorHelper_CheckPath
    - if <def[result]> == end_path {
        - define message "<def[path]> is not a right script"
        - run ScriptErrorHelper_Found instantly context:<def[message]>
        - queue clear
    }
    - if <def[list].contains[type].not> {
        - define message "<def[path]> does not contain a type"
        - run ScriptErrorHelper_Found instantly context:<def[message]>
        - queue clear
    }
    - define type <yaml[<def[id]>].read[<def[path]>.type]>
    - if <s@ScriptErrorHelper_Data.yaml_key[config.types].contains[<def[type]>].not> {
        - define message "<def[type]> is not a valid type in <def[path]>"
        - run ScriptErrorHelper_Found instantly context:<def[message]>
        - queue clear
    }
    - if <def[type]> == task {
        - if <def[list].contains[script].not> {
            - define message "<def[path]> as <def[type]> type does not have the script key"
            - run ScriptErrorHelper_Found instantly context:<def[message]>
            - queue clear
        }
        - if <def[list].contains[definition]> {
            - define message "<def[path]> has typo for definitions"
            - run ScriptErrorHelper_Found instantly context:<def[message]>
            - queue clear
        }
    }
    else if <def[type]> == world {
        - if <def[list].contains[events].not> {
            - define message "<def[path]> as <def[type]> type does not have the events key"
            - run ScriptErrorHelper_Found instantly context:<def[message]>
            - queue clear
        }
    }
    else if <def[type]> == command {
        - if <def[list].contains[name].not> {
            - define message "<def[path]> as <def[type]> type does not have the name key"
            - run ScriptErrorHelper_Found instantly context:<def[message]>
            - queue clear
        }
        - if <def[list].contains[script].not> {
            - define message "<def[path]> as <def[type]> type does not have the script key"
            - run ScriptErrorHelper_Found instantly context:<def[message]>
            - queue clear
        }
    }

ScriptErrorHelper_CheckPath:
    type: task
    # definitions: check_path
    script:
    - define list <yaml[<def[id]>].list_keys[<def[check_path]>]||null>
    - if <def[list]> == null {
        - define result end_path
    }
    else {
        - define result continues
    }