如何设置捕获空搜索消息?IRC Tcl脚本技术咨询
First off, your current script already has a solid basic check for empty search content—great start! But we can refine this logic to be more robust, covering edge cases like users entering nothing but whitespace (spaces, tabs, etc.) after !find.
Issues with the Current Approach
Your current check looks at $judul (the first word of the input) being empty, which works for cases where the user types just !find or !find . However, we can make the check more explicit to ensure we catch every scenario where no valid search term is provided.
Refined Code for Empty Search Capture
Here's an updated version of your pub:cari proc with improved empty search handling, plus a few extra user-friendly tweaks:
bind pub - "!find" pub:cari proc pub:cari { nick host hand chan text } { # Trim all leading/trailing whitespace to catch empty/whitespace-only input set cleaned_text [string trim $text] # Check if we have no valid search content at all if { $cleaned_text eq "" } { puthelp "notice $nick :ketik !find <penyanyi/artis>" return } # Proceed with processing the search term set judul [lindex $cleaned_text 0] putquick "notice $nick :being processed $judul" # Safely run the find command and handle errors if { [catch {exec find /home/gusman/mp3 -name "*$judul*" -type f -printf "%f\n"} data] } { puthelp "notice $nick :Terjadi kesalahan saat mencari: $data" return } # Inform user if no files match the search term if { $data eq "" } { puthelp "notice $nick :Tidak ditemukan file dengan kata kunci '$judul'" return } putserv "notice $nick :!putar $data" putserv "notice $nick :copy paste di channel !putar $data" } putlog "find.tcl loaded successfully"
Key Improvements:
- Whitespace-Resilient Check: Using
string trim $textstrips any leading/trailing spaces or tabs, so we reliably catch cases where the user enters!findfollowed by nothing but whitespace. We check this cleaned string directly instead of just the first word. - Error Handling: Added a check for the
catchresult to handle unexpected issues (like permission errors) from thefindcommand and notify the user clearly. - No-Results Feedback: If the search runs successfully but finds no matching files, we tell the user instead of sending empty
!putarcommands, which is much more intuitive.
This makes your empty search capture more reliable, while also polishing the overall user experience for valid searches.
内容的提问来源于stack exchange,提问作者Gusman

