How can I make the writefile variable write a user's TTV username and their arguments and readfile read the same line that it just created for the user?

#1

PhantomBot Version: Latest Stable (Can’t tell which version…)
OS Version: Mac OSX High Sierra
Java Version: I don’t know.
Browser and Version (for Panel Support): Safari. Yet again, I don’t know the version… Lol
Stock PhantomBot: Yes/No (Yes if you have not modified the scripts or Java Core) No? I added one custom script that doesn’t work…

Let us know what help you need:
Right now I’m trying to either create a script or a command to write and retrieve text from a user’s input. As of today, I’ve finally gotten my stubborn self to look at the Command Variables page and it says readfile and writefile, but I can’t figure out how to read a line that the user’s arguments created. I currently have a script in my ‘/scripts/commands/’ folder, which I used to have on the root of my PhantomBot directory. I don’t know what I did wrong in this script and, tbh, I just copied the code from another thread from here, https://community.phantombot.tv/t/add-a-property-to-a-user/1964/4 Also, there were no Console Errors.

I just can’t figure this out. Is there a way to make the readfile make a line on a TXT file with the user’s TTV username and then use readfile to grab that and relay it to chat?

Here’s my code for the script:

(function() {
    $.bind('command', function(event) {
        var sender = event.getSender();
        var command = event.getCommand();
        var args = event.getArgs();

        if (command.equalsIgnoreCase('myfc')) {
            
            var arrayVar = $.readFile('./fc.txt');
            $.setIniDbString('myfc', sender, args[0]);
            $.getIniDbString('myfc', sender, 'Not provided');
            $.say(str);
            return;
        
            if (args[0] === undefined) {

                $.say('Invalid Usage: Use Command !fc as !fc [YOUR FRIEND CODE HERE]');
                return;

            };

        };
    });


    $.bind('initReady', function() {
        $.registerChatCommand('./scripts/commands/fc.js', 'myfc', 6);
    });
})();

And, yes, it is for 3DS and Switch Friend Codes.

Thank you!

0 Likes

#2

This may have been confusing to some. I got the option to write to the .txt file working. Now all I have to do is figure out how to select the Username that (touser) is receiving. Can someone tell me how to query that?

Thanks!

0 Likes

#3

Here is a quick script i think this does what you want??? just change the getFriendCode() area to what you need it as

PhantomBot/scripts/custom/commands/fcCommand.js

/**
 * fcCommand.js
 *
 * Command handler to say a custom friendcode based on the current system.
 * Add new game definitions to the getFriendCode switch statement.
 */
(function() {
    var system = $.getSetIniDbString('fcSettings', 'system', 'switch');

    function reloadSettings() {
        system = $.getIniDbString('fcSettings', 'system');
    }

    function getFriendCode(system) {
        switch (system) {
            case "3ds":
                return "A cool name";
            case "switch":
                return "Another cool name";
            default: // Default means if the string game does not match any of the existing cases. keep this.
                return "Another name";
        }
    }

    /**
     * @event command
     */
    $.bind('command', function(event) {
        var sender = event.getSender(),
            command = event.getCommand(),
            args = event.getArgs(),
            action = args[0],
            action2 = args[1];

        if (command.equalsIgnoreCase('fc')) {
            if (!action) {
                $.say('My friendcode is ' + getFriendCode(system.toLowerCase()));
                return;
            } else {
                if (action.equalsIgnoreCase('system')) {
                    if (action2 !== undefined) {
                        $.setIniDbString('fcSettings', 'system', action2.toLowerCase());
                        $.say('System has been updated to ' + action2);
                        reloadSettings();
                    } else {
                        $.say(sender + ' Usage: !fc  system [SYSTEM] ');
                    }
                } else {
                    var fcUser = $.getIniDbString('fcUsers', action.replace("@", "").toLowerCase());
                    if (fcUser !== undefined) {
                        $.say(action + '\'s friendcode is ' + fcUser);
                    } else {
                        $.say(action + ' has no friendcode');
                    }
                }
            }
        }

        if (command.equalsIgnoreCase('myfc')) {
            if (!action) {
                var fcUser = $.getIniDbString('fcUsers', sender);
                if (fcUser !== undefined) {
                    $.say(sender + ' your friendcode is ' + fcUser);
                } else {
                    $.say(sender + ' you have no friendcode. "!myfc [YOUR FRIEND CODE HERE]" to add one');
                }
            } else {
                $.setIniDbString('fcUsers', sender, action);
                $.say(sender + ' your friendcode has been set to ' + action);
            }
        }
    });

    /**
     * @event initReady
     */
    $.bind('initReady', function() {
        if($.bot.isModuleEnabled('./custom/commands/fcCommand.js')){
            $.registerChatCommand('./custom/commands/fcCommand.js', 'fc', 2);
            $.registerChatSubcommand('fc', 'system', 1);

            $.registerChatCommand('./custom/commands/fcCommand.js', 'myfc', 7);
        }
    });
})();
0 Likes

#4

I’ll definitely try that out. How can I make it read a TXT file. I have added a command via the Add Command function in the Panel. How can I make another Command read that file for a specific TTV Username?
Here is the Command & response:
!setfc - (writefile fc.txt, true, (sender): (echo))
writefile fc.txt - Writes to that file.
(sender) - Prints the Sender’s Username in the TXT file.
(echo) - Prints the arguments the User types after using the !setfc command.

These items should be self-explanatory, but I typed it out just to be safe. I just didn’t know if readfile could find a specific word or phrase while reading my fc.txt file.

Thanks!

0 Likes

#5

why use a read file when you can pretty much just use the database unless your trying to display it onto obs?

0 Likes

#6

To make it all local. I don’t want to have to use DBs because it’s just too much for one command that only regulars and up can use. Plus I don’t really know how to code in this variation of language.
If there is a way to make readfile to find and echo that line of the TTV Username the Command User specified, if they didn’t, then their line, by looking up via (touser). Like I stated before, the !setfc command runs as (writefile fc.txt, true, (sender): (echo)) thus adding the Command User’s TTV Username to the TXT file as well as the Arguments passed.

Reitterating:
But all I want to do is read the line of the User who was specified’s Friend Code. All I want to do is look up a line of the TXT file that contains the specified User’s TTV Username.

I’m guessing I’d probably have to pass some extra (inner-?)variables for the Command to read (touser). Am I right?

0 Likes

#7

This was the result of a test of the !setfc command:
29%20AM

I edited out my Friend Code, BTW.

0 Likes

#8

you would need to do something even more complicated and save it as a json format then read the file as json its much easyer to do it the way i did then with files

0 Likes

#9

OK, I will try to figure that out then. Thank you so much!

0 Likes

#10

the other thing you can do is read and write to files of the users name that way there is only 1 line

0 Likes

#11

Can’t you just do:

!addcom yourcommand (writefile test.txt, true, (sender) (echo))

Anything specific like regex and “friend code verification” will have to be in a script itself.

And you could easily retrieve the codes from a text file without it being in json format, you’d just have to structure the file in a way that you can easily identify keys. Problem with that is users may enter something you defined as a key as one of their values.

0 Likes

#12

the other issue is you cant define what line to edit in a text file like if you have 12 users friend codes in 1 text file you cant edit just 1 of them without doing it the way that the ignore bots system does it

0 Likes

closed #13

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.

0 Likes