Character Vault
Any Concept / Any System
Compendium
Your System Come To Life
Roll20 for Android
Streamlined for your Tablet
Roll20 for iPad
Streamlined for your Tablet

Personal tools

Script:Store Commands

From Roll20 Wiki

Revision as of 06:45, 2 September 2013 by Brian (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The following script creates the API commands !delay, !store, !clearstore, !echostore, and !run.

Syntax

!delay time !store [!time] command !clearstore !echostore !run
Parameter Values
time A time delay in milliseconds. The !delay command will set the default delay for the !store command. A delay set using the !store command will override that default, but only for that command. Using !delay will not change the delays of already stored commands. 500ms will be used if no delay is ever specified.
command A command to store for later use. This can be anything you could normally enter in chat, as well as the /direct command.

Code

var store_commands = store_commands || {};

store_commands.list = {};

on('chat:message', function(msg) {
    if(msg.type != 'api') return;
    
    var parts = msg.content.split(' ');
    var command = parts.shift().substring(1);
    var id = msg.playerid;
    
    if(!store_commands.list[id]) store_commands.list[id] = { cmds: [], delay: 500 };
    
    switch(command)
    {
        case 'delay':
            store_commands.list[id].delay = parseInt(parts[0], 10);
            break;
        case 'store':
            var delay = 500;
            if(parts[0].indexOf('!') == 0)
                delay = parseInt(parts.shift().substring(1), 10);
            else if(store_commands.list[id].delay)
                delay = store_commands.list[id].delay;
            
            var obj = { text: parts.join(' '), delay: delay };
            store_commands.list[id].cmds.push(obj);
            break;
        case 'clearstore':
            store_commands.list[id].cmds = [];
            break;
        case 'echostore':
            for(var i = 0; i < store_commands.list[id].cmds.length; i++)
            {
                var obj = store_commands.list[id].cmds[i];
                sendChat('Store Commands.js', '/w ' + msg.who + ' {' + obj.delay + 'ms, ' + obj.text + '}');
            }
            break;
        case 'run':
            var count = 0;
            for(var i = 0; i < store_commands.list[id].cmds.length; i++)
            {
                var obj = store_commands.list[id].cmds[i];
                store_commands.echo(id, obj.text, count + obj.delay);
                count += obj.delay;
            }
            break;
        default:
            break;
    }
});

store_commands.echo = function(id, text, delay)
{
    setTimeout(function(){ sendChat('player|'+id, text); }, delay);
};