Skip to content
Database dbTransfer

$dbTransfer

$dbTransfer takes the amount from one document and gives it to the other in one transaction: either both change or neither. The giver cannot go below 0 ($dbLastError is conflict when the balance is too low).

Syntax

$dbTransfer[collection;fromId;toId;path;amount;(returnValue)]

Parameters

Parameter Description
collection Collection name: lowercase letters, digits, _, . or - (up to 64 characters). It is created on first write.
fromId Document that gives.
toId Document that receives. Created if needed.
path Dotted path of the number in both documents.
amount A positive number.
returnValue Optional - yes to return the result, no to return nothing. Absent: a call written on its own prints nothing, a call used as a value ($var[r;$dbSet[...]]) returns it. Any other value raises an error.

Return value

Used on its own in a script, the function prints nothing, so no revision or JSON leaks into the message. Used as a value of another function, or with returnValue set to yes, it returns the result below.

  • true when done.
  • Empty when refused or failed.

Examples

$dbTransfer[wallet;$authorID;$mentioned[1];cash;$message[2]]
$if[$dbLastError==conflict]
  Not enough cash.
$endif

Try it

Paste an example into the playground to preview its message (functions that act on Discord are not run), or build the same thing with blocks.