| View previous topic :: View next topic |
| Author |
Message |
crymsun Voice
Joined: 13 Nov 2018 Posts: 33
|
Posted: Fri Jan 11, 2019 11:20 am Post subject: Super simple random msg from text file |
|
|
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. |
|
| Back to top |
|
 |
willyw Revered One
Joined: 15 Jan 2009 Posts: 1175
|
Posted: Fri Jan 11, 2019 1:25 pm Post subject: Re: Super simple random msg from text file |
|
|
| 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: |
# 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 ! |
|
| Back to top |
|
 |
crymsun Voice
Joined: 13 Nov 2018 Posts: 33
|
Posted: Fri Jan 11, 2019 10:09 pm Post subject: |
|
|
Thanks so much! Tested and worked beautifully.
Now to compare that one with all the failed attempts to learn "what went wrong" lol |
|
| Back to top |
|
 |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3741 Location: Mint Factory
|
Posted: Sat Jan 12, 2019 7:14 am Post subject: |
|
|
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. |
|
| Back to top |
|
 |
willyw Revered One
Joined: 15 Jan 2009 Posts: 1175
|
Posted: Sat Jan 12, 2019 10:17 am Post subject: |
|
|
| crymsun wrote: | Thanks so much! Tested and worked beautifully.
|
Good to hear.
And - you're welcome.
| Quote: |
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 ! |
|
| Back to top |
|
 |
|