Najova

DocsBuilt-in functions

Files and the computer

Only in najova run, not in game scripts. With --safe, scripts can only reach their own folder and cannot use the web.

$readFilenajova run only

$readFile[path]

The text of a file (paths start at the script's folder).

readFile.naja
$print[$readFile[notes.txt]]
najova run readFile.naja
Buy milk

$writeFilenajova run only

$writeFile[path;text]

Writes the text to a file, in place of what was in it.

writeFile.naja
$writeFile[save.txt;level 3]
$print[$readFile[save.txt]]
najova run writeFile.naja
level 3

$appendFilenajova run only

$appendFile[path;text]

Adds the text to the end of a file.

appendFile.naja
$writeFile[log.txt;started\n]
$appendFile[log.txt;finished]
$print[$readFile[log.txt]]
najova run appendFile.naja
started
finished

$existsnajova run only

$exists[path]

true when there is a file or folder at the path.

exists.naja
$print[$exists[nothing-here.txt]]
najova run exists.naja
false

$filesnajova run only

$files or $files[folder]

A list of the names in a folder (the script's folder when none is given).

files.naja
$each[name;$files[photos]]
  $print[$get[name]]
$endeach
najova run files.naja
a.png
b.png

$makeFoldernajova run only

$makeFolder[path]

Makes a folder (and the folders it is in).

makeFolder.naja
$makeFolder[saves/slot1]
$print[$exists[saves/slot1]]
najova run makeFolder.naja
true

$deleteFilenajova run only

$deleteFile[path]

Deletes a file.

deleteFile.naja
$writeFile[old.txt;x]
$deleteFile[old.txt]
$print[$exists[old.txt]]
najova run deleteFile.naja
false

$envnajova run only

$env[name]

An environment value, like $env[DISCORD_TOKEN] (nil when it is not set), so secrets stay out of scripts.

env.najanot run here
$let[token;$env[DISCORD_TOKEN]]
$if[$get[token] == nil]
  $error[Set DISCORD_TOKEN first.]
$endif
Keeps secrets such as bot tokens out of your scripts.

$argsnajova run only

$args

A list of what was written after the script's name: najova run bot.naja a b.

args.naja
$print[Hello $item[$args;1], you are $item[$args;2]]
najova run greet.naja Sam 12
Hello Sam, you are 12

$exitnajova run only

$exit or $exit[code]

Ends the program at once (code 0 means all went well).

exit.naja
$print[Before]
$exit[0]
$print[Never printed]
najova run exit.naja
Before

$helpernajova run only

$helper

In an extension that names a helper program: that program (started once). $call[helper.name;values...] asks it, $on[helper.Event;data] hears it.

helper.najanot run here
$call[helper.render;card.png]
$on[helper.Done;file]
  $print[Made $get[file]]
$endon
Only for extension makers whose extension.json names a helper program.

$fetchnajova run only

$fetch[url] or $fetch[url;options]

A web request; gives a table with status, ok, text and headers. Options: $table[method;POST;body;...;headers;$table[...]].

fetch.najanot run here
$let[answer;$fetch[https://example.com]]
$if[$get[answer.ok]]
  $print[Got $count[$get[answer.text]] letters]
$else
  $print[Failed: $get[answer.status]]
$endif
Shown without running, so the docs build does not need the internet.