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 

Help with logging script that dupe checks

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
Felix2003
Voice


Joined: 06 Feb 2009
Posts: 24

PostPosted: Wed Feb 24, 2016 5:06 pm    Post subject: Help with logging script that dupe checks Reply with quote

i was wondering if anybody could help me with a script i found many moons ago

i am currently on a forums that has a private radio for members and we also have an irc server

i wanted to have a channel were users could connect say add song and it gets written to a file i can upload to the djs so they can build a playlist from the songs suggested

i ask members when requesting to say add Artist - song after testing and trying to edit it myself i have failed Sad (not the best with tcl)

it writes to the file fine but when checking to see if the artist - song is already in the file it fails unless its one word example song-artists with no spaces

an example of what the script does now

User1: add Mariah Carey - Without You
Bot: Added Mariah Carey - Without You to file
User2: add Mariah Carey - Without You
Bot: Added Mariah Carey - Without You to file

User1: add Mariah.Carey-Without.You
Bot: Added Mariah.Carey-Without.You to file
User2: add Mariah.Carey-Without.You
Bot: Song already in the list please choose another

#################

an example of what i want it to do

User1: add Mariah Carey - Without You
Bot: Added Mariah Carey - Without You to file
User2: add Mariah Carey - Without You
Bot: Song already in the list please choose another





Code:
set datafile "/usr/home/Felix2003/Eggdrop/scripts/data.txt"

bind pub - add procadd
bind pub - del procdel

proc procadd {nick uhost hand chan text} {
        global datafile
        set text [split $text]
        set ahand [lindex $text 0]

        # check for duplicate songs
        if {[file exists $datafile]} {
                set input [open $datafile r]
                while {![eof $input]} {
                        set curline [gets $input];set curline [split $curline]
                        if {[lindex $curline 0] == $text} {
                                puthelp "PRIVMSG $chan :$text already exists.. Pick another song"
                                catch {close $input}
                                return
                        }
                }
                catch {close $input}
        }
        # no dupes found, so add the new line
        set output [open $datafile a]
        puts $output "$text"
        flush $output
        catch {close $output}
        puthelp "PRIVMSG $chan :Wrote $text to file.."
}               
       
proc procdel {nick uhost hand chan text} {
        global datafile
        set text [split $text]

        # check for valid input
        if {$text == ""} {
                puthelp "PRIVMSG $chan :meh"
                return
        }

        # check for data file
        if {![file exists $datafile]} {
                puthelp "PRIVMSG $chan :No data file, nothing to delete!"
                return
        }

        # check for requested handle
        set data ""
        set input [open $datafile r]
        while {![eof $input]} {
                set curline [gets $input];set curline [split $curline]
                if {$curline != ""} {
                        set data [linsert $data end $curline]
                }
        }
        catch {close $input}

        set mark -1;set match ""
        foreach line $data {
                incr mark
                if {[lindex $line 0] == $text} {
                        set match $mark
                        break
                }
        }
        if {$match == ""} {
                puthelp "PRIVMSG $chan :No handle matching $text found.."
                return
        }
        set newdata [lreplace $data $mark $mark]
        set output [open $datafile w]
        foreach newline $newdata {
                if {$newline != ""} {
                        puts $output $newline
                }
        }
        flush $output
        catch {close $output}
        puthelp "PRIVMSG $chan :Deleted $text"
        return                 
}
        global datafile
        if {![file exists $datafile]} {
                puthelp "PRIVMSG $nick :No data file.."
                return
        }
        set input [open $datafile r]
        set lines [split [read $input] \n]
        catch {close $input}
        set cnt 0
        foreach line $lines {
                if {$line != ""} {
                        puthelp "PRIVMSG $nick :$line"
                        incr cnt
                }
        }
        if {$cnt == 0} {
                puthelp "PRIVMSG $nick :No saved data";return
        } else {
                puthelp "PRIVMSG $nick :End of list.."
        }
#####################################################################################################################################
putlog "add/del script loaded.. Orig file created by rosc2112"
Back to top
View user's profile Send private message
SpiKe^^
Owner


Joined: 12 May 2006
Posts: 792
Location: Tennessee, USA

PostPosted: Thu Feb 25, 2016 2:10 am    Post subject: add/del/list script Reply with quote

Try this....
Also fixed the del and list procs.
Code:

set datafile "/usr/home/Felix2003/Eggdrop/scripts/data.txt"

bind pub - add procadd
bind pub - del procdel
bind pub - list proclist

proc procadd {nick uhost hand chan text} {
  global datafile
  set text [string trim $text]

  if {$text eq "" || ![string match "*?-?*" $text]} {
    puthelp "PRIVMSG $chan :Correct syntax: add <Artist> - <Song>"
    return 0
  }

  # check for duplicate songs
  if {[file exists $datafile]} {
    set input [open $datafile r]
    while {![eof $input]} {
      set curline [gets $input]
      if {[string match -nocase $text $curline]} {
        puthelp "PRIVMSG $chan :$text already exists.. Pick another song"
        close $input
        return 0
      }
    }
    close $input
  }
  # no dupes found, so add the new line
  set output [open $datafile a]
  puts $output "$text"
  close $output
  puthelp "PRIVMSG $chan :Wrote $text to file.."
  return 0
}

proc procdel {nick uhost hand chan text} {
  global datafile
  set text [string trim $text]

  # check for valid input
  if {$text eq ""} {
    puthelp "PRIVMSG $chan :Correct syntax: del <Artist> - <Song>"
    return 0
  }

  # check for data file
  if {![file exists $datafile]} {
    puthelp "PRIVMSG $chan :No data file, nothing to delete!"
    return 0
  }

  # check for requested song
  set data ""  ;  set found 0
  set input [open $datafile r]
  while {![eof $input]} {
    set curline [gets $input]
    if {$curline eq ""} {  continue  }
    if {[string match -nocase $text $curline]} {  set found 1
    } else {  lappend data $curline  }
  }
  close $input

  # if request not found
  if {$found == 0} {
    puthelp "PRIVMSG $chan :No request matching $text found.."
    return 0
  }

  # if request is found
  if {$data eq ""} {  file delete $datafile
  } else {
    set output [open $datafile w]
    foreach newline $data {
      puts $output $newline
    }
    close $output
  }
  puthelp "PRIVMSG $chan :Deleted $text"
  return 0
}

proc proclist {nick uhost hand chan text} {
  global datafile
  if {![file exists $datafile]} {
    puthelp "PRIVMSG $nick :No data file.."
    return 0
  }
  set input [open $datafile r]
  set lines [split [read $input] \n]
  close $input
  set cnt 0
  foreach line $lines {
    if {$line ne ""} {
      puthelp "PRIVMSG $nick :$line"
      incr cnt
    }
  }
  if {$cnt == 0} {
    puthelp "PRIVMSG $nick :No saved data."
  } else {
    puthelp "PRIVMSG $nick :End of list.."
  }
  return 0
}

#######################################################
putlog "add/del/list script loaded.. Orig file created by rosc2112"


_________________
SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
Back to top
View user's profile Send private message Visit poster's website
Felix2003
Voice


Joined: 06 Feb 2009
Posts: 24

PostPosted: Thu Feb 25, 2016 7:17 am    Post subject: Reply with quote

Thank you spike for having a look Smile

tested your version and isn't doing exactly what i needed

User1: add Mariah Carey - Infinity
Bot: added song to file
User2: add Mariah Carey - Without you
Bot: handle Mariah already exists

seems it just reading the 1st word when searching not the entire line
Back to top
View user's profile Send private message
SpiKe^^
Owner


Joined: 12 May 2006
Posts: 792
Location: Tennessee, USA

PostPosted: Thu Feb 25, 2016 11:59 am    Post subject: Reply with quote

Felix2003 wrote:
tested your version and isn't doing exactly what i needed
You did not use my edited script as it is posted above.
The original script did use just the first word for matching, mine does not.
If you are just going to pick a line or 2 of my code, and edit them back into your copy you will get strange results.
You MUST use my edited script as it is wrote above for it to work as you want it to.

Please test my edited script as I wrote it.
_________________
SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
Back to top
View user's profile Send private message Visit poster's website
Felix2003
Voice


Joined: 06 Feb 2009
Posts: 24

PostPosted: Thu Feb 25, 2016 6:33 pm    Post subject: Reply with quote

SpiKe your correct i loaded the wrong script rushing this morning before heading to work

i loaded your modified file correctly this time and it works perfectly Very Happy

Thank you man you made my life that little bit easier Very Happy

one more question which i forgot to say is there a way to make it channel specific? like setting a channel mode or just writing a channel in the tcl?
Back to top
View user's profile Send private message
SpiKe^^
Owner


Joined: 12 May 2006
Posts: 792
Location: Tennessee, USA

PostPosted: Thu Feb 25, 2016 9:37 pm    Post subject: Reply with quote

Channels specific version...

Do multiple channels space separated... {#chan1 #chan2}


Code:

# set the channel(s) for the script to run in #
set adddelchan {#yourchan}

set datafile "/usr/home/Felix2003/Eggdrop/scripts/data.txt"

############ END SETTINGS ############

bind pub - add procadd
bind pub - del procdel
bind pub - list proclist

set adddelchan [split [string trim $adddelchan]]

proc procadd {nick uhost hand chan text} {
  global datafile adddelchan
  set text [string trim $text]

  if {[lsearch -nocase $adddelchan $chan] == -1} { return 0 }

  if {$text eq "" || ![string match "*?-?*" $text]} {
    puthelp "PRIVMSG $chan :Correct syntax: add <Artist> - <Song>"
    return 0
  }

  # check for duplicate songs
  if {[file exists $datafile]} {
    set input [open $datafile r]
    while {![eof $input]} {
      set curline [gets $input]
      if {[string match -nocase $text $curline]} {
        puthelp "PRIVMSG $chan :$text already exists.. Pick another song"
        close $input
        return 0
      }
    }
    close $input
  }
  # no dupes found, so add the new line
  set output [open $datafile a]
  puts $output "$text"
  close $output
  puthelp "PRIVMSG $chan :Wrote $text to file.."
  return 0
}

proc procdel {nick uhost hand chan text} {
  global datafile adddelchan
  set text [string trim $text]

  if {[lsearch -nocase $adddelchan $chan] == -1} { return 0 }

  # check for valid input
  if {$text eq ""} {
    puthelp "PRIVMSG $chan :Correct syntax: del <Artist> - <Song>"
    return 0
  }

  # check for data file
  if {![file exists $datafile]} {
    puthelp "PRIVMSG $chan :No data file, nothing to delete!"
    return 0
  }

  # check for requested song
  set data ""  ;  set found 0
  set input [open $datafile r]
  while {![eof $input]} {
    set curline [gets $input]
    if {$curline eq ""} {  continue  }
    if {[string match -nocase $text $curline]} {  set found 1
    } else {  lappend data $curline  }
  }
  close $input

  # if request not found
  if {$found == 0} {
    puthelp "PRIVMSG $chan :No request matching $text found.."
    return 0
  }

  # if request is found
  if {$data eq ""} {  file delete $datafile
  } else {
    set output [open $datafile w]
    foreach newline $data {
      puts $output $newline
    }
    close $output
  }
  puthelp "PRIVMSG $chan :Deleted $text"
  return 0
}

proc proclist {nick uhost hand chan text} {
  global datafile adddelchan

  if {[lsearch -nocase $adddelchan $chan] == -1} { return 0 }

  if {![file exists $datafile]} {
    puthelp "PRIVMSG $nick :No data file.."
    return 0
  }
  set input [open $datafile r]
  set lines [split [read $input] \n]
  close $input
  set cnt 0
  foreach line $lines {
    if {$line ne ""} {
      puthelp "PRIVMSG $nick :$line"
      incr cnt
    }
  }
  if {$cnt == 0} {
    puthelp "PRIVMSG $nick :No saved data."
  } else {
    puthelp "PRIVMSG $nick :End of list.."
  }
  return 0
}

#######################################################
putlog "add/del/list script loaded.. Orig file created by rosc2112"


_________________
SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
Back to top
View user's profile Send private message Visit poster's website
Felix2003
Voice


Joined: 06 Feb 2009
Posts: 24

PostPosted: Fri Feb 26, 2016 8:17 am    Post subject: Reply with quote

Spike you are amazing thank you so much for your help on this Very Happy
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 -> Scripting Help 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