Allowing a viewer to trigger a point giveaway to all viewers

points

#1

I am looking to develop a custom script that will create a command that a viewer can run at a cost. This command should then give away a set number of points to everyone in chat. Can someone provide me with an example of how to execute a normal bot command e.g. !points all 100 as the bot from a viewer executable command without having to set permissions on !points all to be accessible by viewers?


#2
/**
 * Give points away from a user.
 *
 */
(function() {
    $.bind('command', function(event) {
        var sender = event.getSender(),
            command = event.getCommand(),
            args = event.getArgs(),
            action = args[0];

        if (command.equalsIgnoreCase('givepoints')) {
            if (action === undefined) {
                $.say($.whisperPrefix(sender) + 'How many points do you want to give away?');
                return;
            }

            if (isNaN(action)) {
                $.say($.whisperPrefix(sender) + 'You cannot give that away! Try a number!');
                return;
            }

            var userPoints = $.getUserPoints(sender);
            if (userPoints < parseInt(action)) {
                $.say($.whisperPrefix(sender) + 'You do not have that much to give away!');
                return;
            }

            if ($.users.length - 1 > parseInt(action)) {
                $.say($.whisperPrefix(sender) + 'You need to give away more for everyone in chat!');
                return;
            }

            $.inidb.decr('points', sender, parseInt(action));
            var userShare = parseInt(parseInt(action) / $.users.length - 1);

            for (i in $.users) {
                var username = $.users[i][0].toLowerCase();
                if (!username.equals(sender)) {
                    $.inidb.incr('points', username, userShare);
                }
            }
            $.say($.whisperPrefix(sender) + 'You just gave everyone some points, what a nice person you are.');
        }
    });

    /**
     * @event initReady
     */
    $.bind('initReady', function() {
        $.registerChatCommand('./custom/givePoints.js', 'givepoints', 7);
    });
})();

#3

This will attempt to divide up a number and give to the chat. If you want to give the chosen number of points to everyone in chat, then change the logic to check for enough points to give away points and change userShare to parseInt(action). Also deduct the number of points differently.


#4

I am actually looking for something more like the “!points all” command not the !gift command. The command will cost points to run but that doesn’t necessarily equate to how many will be gifted out to all of the viewers and should not be subtracted from the viewer who ran the command. I could accomplish this easily enough without a custom script except to do so I would have to allow the “!points all” command to be executable by viewers and this is not desirable.


#5

You’ll probably need a custom script if you do not want users to be able to use !points all.

Even if you alias the command, it still requires the rights of the non-alias.

Why not just rip that part of the code out of the points command, throw it into a custom script, add a cost, and call it good?

(function() {
	var commandName = 'usergift';// Change the command name here. Requires Bot restart to take effect.
    var pointsPayout = 100;// Change the amount of points to give users here. Requires Bot restart to take effect.
    /**
    * @function giveAll
    * @param {String} sender
    */
    function giveAll(sender) {
        $.inidb.setAutoCommit(false);
        for (i in $.users) {
            $.inidb.incr('points', $.users[i][0].toLowerCase(), pointsPayout);
        }
        $.inidb.setAutoCommit(true);

        $.say($.whisperPrefix(sender) + 'just gifted everyone in the chat ' + $.getPointsString(pointsPayout) + '!');
    };

    /**
     * @event command
     */
    $.bind('command', function(event) {
        var sender = event.getSender().toLowerCase(),
            username = $.username.resolve(sender, event.getTags()),
            command = event.getCommand(),
            args = event.getArgs();

        /**
         * @commandpath usergift - Gives all users in chat a set amount of points.
         */
        if (command.equalsIgnoreCase(commandName)) {
			giveAll(sender);
        }
    });

    /**
     * @event initReady
     */
    $.bind('initReady', function() {
        if ($.bot.isModuleEnabled('./custom/commands/userGifts.js')) {// Don't forget to change this path to match yours!
            $.registerChatCommand('./custom/commands/userGifts.js',  commandName, 7);// 7 = regular user.
        }
    });
})();

Note: Untested. Should work I think.
The current path for the script would be /scripts/custom/commands/userGifts.js but you’re more than welcome to change that.

To set a cost, you do it via the UI or using !pricecom. You can change the command name and payout at the top, but it requires a bot restart to take effect. Yes, I could have set it up to use commands, but this was a quick 5 min edit.


#6

Thank you! This is what I needed!