| View previous topic :: View next topic |
| Author |
Message |
BigToe Halfop
Joined: 30 Dec 2010 Posts: 99
|
Posted: Thu Aug 30, 2012 10:39 am Post subject: Search txt file for matching text and display |
|
|
Hi, I`m looking for a script to work with the following format of a text file:
[Category1]
line1
line2
line3
[Category2]
line1
line2
line3
When a user types !categories
The bot will display all the existing [Category] that exists in a text file (the text file name is "scripts/Categories.txt") to the user
After displaying the categories, or even before (if a user knows what the categories are without asking for displaying them), the user can pick a category via !search Category1 and the bot will display to him all the lines in that category.
Example:
Categories.txt format is as following:
[Fruits]
Apple
Banana
Orange
[Movies]
Matrix
Kill Bill
Fast and Furious
[Music]
Oasis
Metallica
Aerosmith
| Quote: |
<User> !categories
<Bot> Fruits, Movies, Music.
<User> !Category Fruits
<Bot> Apple
<Bot> Banana
<Bot> Orange
<User> !Category Music
<Bot> Oasis
<Bot> Metallica
<Bot> Aerosmith |
* In addition, a !randCategory function that will display 1 random line from 1 random category, would be brilliant as-well.
Please make sure the interval of displaying is such that will not force him to quit due to flood.
Thanks! |
|
| Back to top |
|
 |
SpiKe^^ Owner

Joined: 12 May 2006 Posts: 792 Location: Tennessee, USA
|
Posted: Thu Aug 30, 2012 10:36 pm Post subject: |
|
|
Try this and see what we have so far:)
| Code: |
# set the route and file name of the categories file #
set cats(file) "scripts/Categories.txt"
# set the command to list all the categories #
set cats(ccmd) "!categories"
# set the command to read one of the categories #
set cats(rcmd) "!category"
# set the user file flags required to use this script ("-" = everyone) #
set cats(flag) "-"
###### END OF SETTINGS ######
set cats(cats) ""
if {![file exists $cats(file)]} { return }
set cats(tm-open) [open $cats(file)]
while {![eof $cats(tm-open)]} {
set cats(tm-line) [gets $cats(tm-open)]
if {[string match \[*\] $cats(tm-line)]} {
lappend cats(cats) [string range $cats(tm-line) 1 end-1]
}
}
close $cats(tm-open)
if {$cats(cats) eq ""} { return }
bind pub $cats(flag) $cats(ccmd) cats:listcats
proc cats:listcats {nk uh hn ch tx} {
if {$cats(cats) eq ""} { return }
puthelp "PRIVMSG $ch :[join $cats(cats) ", "]."
}
bind pub $cats(flag) $cats(rcmd) cats:readacat
proc cats:readacat {nk uh hn ch tx} {
if {$cats(cats) eq ""} { return }
set tx [string trim $tx]
if {[lsearch -exact $cats(cats) $tx]>"-1"} { set patrn \[$tx\]
} elseif {[lsearch $cats(cats) $tx*]>"-1"} { set patrn \[$tx*\]
} elseif {[lsearch $cats(cats) *$tx*]>"-1"} { set patrn \[*$tx*\]
} else { return }
set open [open $cats(file)] ; set found 0
while {![eof $open]} {
set line [gets $open]
if {$line eq ""} { continue }
if {[string match $patrn $line]} { set found 1
} elseif {[string match \[*\] $line]} {
if {$found>"0"} { break }
} elseif {$found>"0"} {
puthelp "PRIVMSG $ch :$line"
}
}
close $open
}
putlog "cats.tcl v1.1 loaded."
|
_________________ SpiKe^^
Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
. |
|
| Back to top |
|
 |
BigToe Halfop
Joined: 30 Dec 2010 Posts: 99
|
Posted: Fri Aug 31, 2012 2:24 am Post subject: |
|
|
Hey SpiKe^^,
Thanks for the reply.
I've loaded a script and created Categories.txt in the following format:
[Fruits]
Banana
Apple
[Old Movies]
Kill Bill
Matrix
Tried to trigger it via !Categories, it did not work. Also tried !category Fruits and that didn't work either |
|
| Back to top |
|
 |
SpiKe^^ Owner

Joined: 12 May 2006 Posts: 792 Location: Tennessee, USA
|
Posted: Fri Aug 31, 2012 5:00 pm Post subject: Cats, take 2 |
|
|
You're right, that code won't run.
Had a few string match issues:)
This code should run without error.
| Code: |
# set the route and file name of the categories file #
set cats(file) "scripts/Categories.txt"
# set the command to list all the categories #
set cats(ccmd) "!categories"
# set the command to read one of the categories #
set cats(rcmd) "!category"
# set the user file flags required to use this script ("-" = everyone) #
set cats(flag) "-"
###### END OF SETTINGS ######
set cats(cats) ""
if {![file exists $cats(file)]} { return }
set cats(tm-open) [open $cats(file)]
while {![eof $cats(tm-open)]} {
set cats(tm-line) [string trim [gets $cats(tm-open)]]
if {[string match {\[*\]} $cats(tm-line)]} {
lappend cats(cats) [string range $cats(tm-line) 1 end-1]
}
}
close $cats(tm-open)
if {$cats(cats) eq ""} { return }
bind pub $cats(flag) $cats(ccmd) cats:listcats
proc cats:listcats {nk uh hn ch tx} { global cats
if {$cats(cats) eq ""} { return }
puthelp "PRIVMSG $ch :[join $cats(cats) ", "]."
}
bind pub $cats(flag) $cats(rcmd) cats:readacat
proc cats:readacat {nk uh hn ch tx} { global cats
if {$cats(cats) eq ""} { return }
set tx [string trim [string tolower $tx]]
if {$tx eq ""} {
puthelp "PRIVMSG $ch :${nk}: Use: $cats(rcmd) <category-name>"
return
}
set pexact 0
if {[lsearch -exact [string tolower $cats(cats)] $tx]>"-1"} { set patrn $tx ; set pexact 1
} elseif {[lsearch [string tolower $cats(cats)] $tx*]>"-1"} { set patrn $tx*
} elseif {[lsearch [string tolower $cats(cats)] *$tx*]>"-1"} { set patrn *$tx*
} else {
puthelp "PRIVMSG $ch :${nk}: No matching category found."
return
}
set open [open $cats(file)] ; set found 0
while {![eof $open]} {
set line [string trim [gets $open]]
if {$line eq ""} { continue }
set x ""
if {[string match {\[*\]} $line]} { set x [string range $line 1 end-1] }
if {$x ne "" && [string match -nocase $patrn $x]} { set found 1 ; set z $x
} elseif {$x ne ""} {
if {$found>"0"} { break }
} elseif {$found>"0"} {
if {$found=="1"} { set found 2
if {$pexact=="0"} { puthelp "PRIVMSG $ch :Reading category: $z" }
}
puthelp "PRIVMSG $ch :$line"
}
}
close $open
}
putlog "cats.tcl v1.1 loaded."
|
_________________ SpiKe^^
Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
Last edited by SpiKe^^ on Fri Aug 31, 2012 6:08 pm; edited 1 time in total |
|
| Back to top |
|
 |
BigToe Halfop
Joined: 30 Dec 2010 Posts: 99
|
Posted: Fri Aug 31, 2012 5:28 pm Post subject: |
|
|
Perfect Spike^^!
Great job, appreciate it! |
|
| Back to top |
|
 |
SpiKe^^ Owner

Joined: 12 May 2006 Posts: 792 Location: Tennessee, USA
|
Posted: Fri Aug 31, 2012 6:09 pm Post subject: |
|
|
Edited the above code some...
You should get the code again. _________________ SpiKe^^
Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
. |
|
| Back to top |
|
 |
|