This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Super simple random msg from text file

Requests for complete scripts or modifications/fixes for scripts you didn't write. Response not guaranteed, and no thread bumping!
Post Reply
c
crymsun
Voice
Posts: 33
Joined: Tue Nov 13, 2018 8:24 pm

Super simple random msg from text file

Post by crymsun »

Have gone out of my mind trying to edit "random timed message" and advertise scripts - so far three have crashed the bot and I'm out of patience. Have used scripts from here and the tcl archive. The problem is, I don't want a timer at all or for users to add/delete.

Real simple... just want any user to be able to type !topic making the bot pull a random line from the topic.txt file and post to the channel with "Your topic is..." preceding the line.

That's it. For some reason, trying to modify more complex scripts is not working... there must be something important that I'm editing or deleting.
w
willyw
Revered One
Posts: 1196
Joined: Thu Jan 15, 2009 12:55 am

Re: Super simple random msg from text file

Post by willyw »

crymsun wrote: ...
Real simple... just want any user to be able to type !topic making the bot pull a random line from the topic.txt file and post to the channel with "Your topic is..." preceding the line.

That's it.
...
Tested only briefly. Worked.

Code: Select all

# Jan 11, 2019
# http://forum.egghelp.org/viewtopic.php?t=20572
#
# Real simple... just want any user to be able to type !topic making the bot pull a random line from the topic.txt file and post to the channel
# with "Your topic is..." preceding the line.
#
###################################################################


###  config   ###

# set the path/filename to the topic.txt file
set topicfile "scripts/added/experiment_for_somebody/random_line_from_file/topic.txt"


### end config ###




bind pub - "!topic" random_line


#####
proc random_line {nick uhost handle chan text} {
global topicfile


        if {![file exists $topicfile]} {
                putserv "privmsg $chan :Sorry $nick, but $topicfile doesn't exist"
                return 0
        }


        # reference : http://forum.egghelp.org/viewtopic.php?t=6885

        set fp [open $topicfile "r"]
        set data [read -nonewline $fp]
        close $fp

        set lines [split $data "\n"]

        set numlines [llength $lines]

        set num [rand $numlines]
        set randline [lindex $lines $num]


        putserv "privmsg $chan :Your topic is: $randline"

}
#####

For a fun (and popular) Trivia game, visit us at: irc.librairc.net #science-fiction . Over 300K Q & A to play in BogusTrivia !
c
crymsun
Voice
Posts: 33
Joined: Tue Nov 13, 2018 8:24 pm

Post by crymsun »

Thanks so much! Tested and worked beautifully.

Now to compare that one with all the failed attempts to learn "what went wrong" lol
User avatar
caesar
Mint Rubber
Posts: 3776
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Have a look at Basic File Operations like willyw mentioned in his code.
Once the game is over, the king and the pawn go back in the same box.
w
willyw
Revered One
Posts: 1196
Joined: Thu Jan 15, 2009 12:55 am

Post by willyw »

crymsun wrote:Thanks so much! Tested and worked beautifully.
Good to hear.
And - you're welcome. :)
Now to compare that one with all the failed attempts to learn "what went wrong" lol
As caesar said - follow that link that I put in a comment in the script.
It's a great reference.
Whenever I'm thinking through just how I want to get something done, and realize I'm going to need to do at least one of the 4 or 5 things described there, I just come here to this forum, to that Tcl FAQ section, and get it in front of me. It's terrible... I've used those things many, many times, and since I don't HAVE to remember them - exactly - , I don't. I just click up those posts. ;)
It sure beats digging through all my old scripts to find examples.

Next, here are a couple links that I think are essential to open and keep in tabs, handy for quick reference, whenever one is messing with Tcl :

The Eggdrop specific Tcl commands:
http://docs.eggheads.org/mainDocs/tcl-commands.html

The rest of Tcl commands:
http://www.tcl.tk/man/tcl8.6/TclCmd/contents.htm


Then, there is this old tutorial. Still great for getting one started, and for quick reference for a lot of things.
http://suninet.the-demon.de/

I hope this helps.

As you experiment, feel free to post the code you are working on, with your questions. Somewhere around here is an appropriate section of the forum for that. And somebody will advise you.

If the code gets long (and it will), rather than use the [ code ] [ /code ] bbcode in here, you might like to use:
http://paste.tclhelp.net/
Bonus ! - that pastebin even tries to show you your errors. :)

Have fun with your bot, and with writing Tcl for it.
For a fun (and popular) Trivia game, visit us at: irc.librairc.net #science-fiction . Over 300K Q & A to play in BogusTrivia !
Post Reply