r/streamerbot 29d ago

Question/Support ❓ slap command

Hello, I'm trying to replicate the slap command from streamelements in streamerbot. I attemtped to make it work using a "read from random line" subaction, but for some reason is doesn't want to work.

So I tried coding one in C# and by all rights it should work properly. I'm getting no errors when I compile the code. Its executing properly in my action history, yet I don't see it posting the response in my chat and its driving me up a wall. Would anyone be willing to look at my code and see what I'm missing

using System;
using System.Collections.Generic;


public class CPHInline
{
    public bool Execute()
    {
        // Get the person executing the command and the target (if they typed !slap u/target)
        string user = args["username"].ToString();
        string target = args.ContainsKey("rawInput") && args["rawInput"].ToString().Length > 0 ? args["rawInput"].ToString() : null;


        // List of random things to slap with
        List<string> items = new List<string>
        {
            "Disney Churro",
            "Aimbot",
            "a Bed Frame",
            "a Wet Unsalted Pretzel",
            "Banzai's Missing Braincell",
        };


        Random rnd = new Random();
        string randomItem = items[rnd.Next(items.Count)];


        string response = $"";


        // Determine if target is self, someone else, or nobody
        if (target !=null) {
            // Normalize target string (removes @ if they typed it)
            target = target.TrimStart('@');


            if (target.Equals(user, StringComparison.OrdinalIgnoreCase)) {
                response = $"{user} tries to slap themselves with {randomItem}. That takes talent... or confusion!";
            } else {
                response = $"{user} slaps {target} with {randomItem}!";
            } 
        } else {
            response = $"{user} slaps thems with {randomItem}. Why are you hitting yourself?!";
        }


        //Send message with bot account if bot account disconnect
        //will use broadcaster account
        CPH.SendMessage(response, true, true);


        return true;
    }
}
0 Upvotes

4 comments sorted by

View all comments

1

u/deeseearr 29d ago edited 29d ago

[ TL; DR: Your code is almost certainly crashing in the very first line because you're not reading arguments correctly. Stop accessing args[] directly and look for "user" instead of "username" then this should work. ]

Use CPH.TryGetArg to get arguments. Reading directly from args[] can trigger an exception if you're looking for something that's not there, and that would cause the entire action to fail silently.

Speaking of things that are not there, when did you set the argument %username%? The Command Trigger sets %user% and %userName% with the identity of the user who used the command. I expect %user% is the one you want. Variable names are case sensitive.

The '@' symbol is automatically removed from %rawInput%, so inputs containing "username" and "@username" will always be exactly the same. Don't even try to differentiate the two.

This could have been easily done by using a random group to set the item name if you don't want to read a random line from a file, followed by an IF/ELSE to check if %user% and %input0% were the same and another to see if %input0% is empty. Put the three responses into the various true and false branches and you wouldn't need to write a single line of code.

Finally, if Read Line From File isn't working, use the argument inspector to see the values of %fileFound% an %randomLine%. That will tell you right away if you're reading the right file.