egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Super simple random msg from text file

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Script Requests
View previous topic :: View next topic  
Author Message
crymsun
Voice


Joined: 13 Nov 2018
Posts: 31

PostPosted: Fri Jan 11, 2019 11:20 am    Post subject: Super simple random msg from text file Reply with quote

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
View user's profile Send private message
willyw
Revered One


Joined: 15 Jan 2009
Posts: 1043

PostPosted: Fri Jan 11, 2019 1:25 pm    Post subject: Re: Super simple random msg from text file Reply with quote

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=2057
#
# 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
View user's profile Send private message
crymsun
Voice


Joined: 13 Nov 2018
Posts: 31

PostPosted: Fri Jan 11, 2019 10:09 pm    Post subject: Reply with quote

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
View user's profile Send private message
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3583
Location: Mint Factory

PostPosted: Sat Jan 12, 2019 7:14 am    Post subject: Reply with quote

Have a look at Basic File Operations like willyw mentioned in his code.
_________________
I tawt I taw a puddy tat!
Back to top
View user's profile Send private message
willyw
Revered One


Joined: 15 Jan 2009
Posts: 1043

PostPosted: Sat Jan 12, 2019 10:17 am    Post subject: Reply with quote

crymsun wrote:
Thanks so much! Tested and worked beautifully.



Good to hear.
And - you're welcome. Smile

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. Wink
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. Smile

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Script Requests All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber