Custom Actions in RuriLib


  • Admin

    This guide will walk you through the custom action feature of the RuriLib runner, introduced in commit #369

    What is it
    This feature allows you to specify a custom action (basically C# code) and give it to the bots so they can run it instead of being limited to the LoliScript / Config framework that RuriLib has always been based upon.

    How to use it
    First of all open the OpenBulletCLI project that you cloned from the Official repo.
    Then define an action like this

    private static Action<BotData> MyAction = new Action<BotData>(botData => { 
        // Type anything you want here
    });
    

    You will have to change the code a bit to not require a Config to be specified in order to run.
    Finally, set it in the runner (before calling the Start method)

    Runner.CustomAction = MyAction;
    

    How does it work
    Now Bots, instead of executing a Config, will execute the code specified in your Action. As you can notice, the Action takes as an input a BotData, which is the data structure used by Bots for example to get the data line to process and to set the status of the check once they finish. Your action will need to be interlaced with this object and get / set its properties accordingly.

    The latest RuriLib refactor will allow you to access Requests, Parse and Keycheck functionalities very easily without the need of blocks, so that you can have the basic most essential functionalities easily accessible. I suggest looking at their block implementation to see how they can be used in your own code.

    Example
    For example, this action will retrieve a data line, search it on duckduckgo and return the first result as a capture.

    private static Action<BotData> MyAction = new Action<BotData>(botData => {
        
        // Let's assume the data line is "Wikipedia"
    
        // We initialize the request, set it up and perform it
        Request request = new Request();
        request.Setup(botData.GlobalSettings);
        request.Perform("https://duckduckgo.com/?q=" + botData.Data.Data, HttpMethod.GET);
    
        // We save the response source to a variable
        var source = request.SaveString(true);
    
        // We parse the variable using CSS Selector
        var firstURL = Parse.CSS(source, "result__url__domain", "innerHTML").First();
    
        // We check if it's wikipedia.org
        if (Condition.VerifyAll(new KeycheckCondition[]
            {
                new KeycheckCondition() { Left = source, Comparer = Comparer.Contains, Right = "wikipedia.org"}
            }))
        {
            botData.Status = BotStatus.SUCCESS;
        }
    
        // The above is the equivalent of
        // if (source.Contains("wikipedia.org")) botData.Status = BotStatus.SUCCESS;
        // but it allows you to execute checks according to the same logic you use in the Keycheck block
    
        // Finally let's set the parsed data as capture.
        botData.Variables.Set(new CVar("URL", firstURL, true));
    
        // The method will now exit and the bot will mark the data line as a hit since the status is set to SUCCESS.
    });
    

Log in to reply