Random Quote

By Get_A_Fix on May 01, 2010

A Basic RANDOM quote script for eggdrop.
Load script to any tcl file, then: .rehash

Commands (DEFAULT TRIGGER SET TO: ! <-)

!aquote - adds new quote.
!dquote - removes quote, based on nickname that added it.
!rquote - triggers a random quote.
!quotes <on/off> - enables/disables quote system for channel.
!qsearch - returns the number of quotes in database.
!qsearch <pattern> - returns number of quotes matching pattern.
!qhelp - triggers help line, command reference.

/msg yourbot quotes #channelname on/off - enables/disables quote system for channel. (message command)

# Random Quote Module - Get_A_Fix/istok (DALnet/GameSurge/FREEnode/IRCSpeed)
# $Id: quote.tcl,v1 09/05/2008 02:57:42am GMT +12 (NZST) getafix Exp $

# Feel free to edit this setting. Change the ! to any character you would like as a trigger.
set pubtrig "!"

# DONT EDIT BELOW HERE!!
putlog "Loaded: Random Quote Module."

bind pub - ${pubtrig}aquote aquote:pub
bind pub - ${pubtrig}rquote rquote:pub
bind pub - ${pubtrig}dquote dquote:pub
bind pub - ${pubtrig}quotes quotes:pub
bind pub - ${pubtrig}qsearch qsearch:pub
bind pub - ${pubtrig}qhelp quotes:help
bind msg - quotes quotes:msg

setudef flag quotes
set checkfile "./scripts/quote.txt"

proc getTrigger {} {
  global pubtrig
  return $pubtrig
}

proc quotes:help {nick uhost hand chan arg} {
  if {![channel get $chan quotes] && ![regexp c [getchanmode $chan]]} {putquick "PRIVMSG $chan :\037ERROR\037: Quote System is currently not enabled for $chan"; return}
  if {![channel get $chan quotes] && [regexp c [getchanmode $chan]]} {putquick "PRIVMSG $chan :ERROR: Quote System is currently not enabled for $chan"; return}
  if {[validuser $hand] || [isop $nick $chan] || [isvoice $nick $chan]} {
    putquick "PRIVMSG $chan :Quote Commands are: [getTrigger]quotes on/off - [getTrigger]aquote <new-quote-to-add> - [getTrigger]rquote - [getTrigger]dquote <nickname-of-adder> <quote-to-del> - [getTrigger]qsearch - [getTrigger]qsearch *pattern*"
  }
}

proc aquote:pub {nick uhost hand chan arg} {
  global checkfile
  if {![matchattr $hand o|o $chan]} {putquick "NOTICE $nick :\037ERROR\037: You do not have access to this command."; return}
  if {![channel get $chan quotes] && ![regexp c [getchanmode $chan]]} {putquick "PRIVMSG $chan :\037ERROR\037: Quote System is currently not enabled for $chan"; return}
  if {![channel get $chan quotes] && [regexp c [getchanmode $chan]]} {putquick "PRIVMSG $chan :ERROR: Quote System is currently not enabled for $chan"; return}
  if {[lindex [split $arg] 0] == ""} {
    if {![regexp c [getchanmode $chan]]} {putquick "PRIVMSG $chan :\037ERROR\037: Incorrect Parameters. \037SYNTAX\037: [getTrigger]aquote <new-quote-to-add>"; return}
    if {[regexp c [getchanmode $chan]]} {putquick "PRIVMSG $chan :ERROR: Incorrect Parameters. SYNTAX: [getTrigger]aquote <new-quote-to-add>"; return}
    } else {
    set data [open $checkfile a]
    puts $data "$nick $arg"
    close $data
    putquick "PRIVMSG $chan :Added Quote!"
    return 1
  }
}

proc dquote:pub {nick uhost hand chan arg} {
  global checkfile
  if {![matchattr $hand o|o $chan]} {putquick "NOTICE $nick :\037ERROR\037: You do not have access to this command."; return}
  if {![channel get $chan quotes] && ![regexp c [getchanmode $chan]]} {putquick "PRIVMSG $chan :\037ERROR\037: Quote System is currently not enabled for $chan"; return}
  if {![channel get $chan quotes] && [regexp c [getchanmode $chan]]} {putquick "PRIVMSG $chan :ERROR: Quote System is currently not enabled for $chan"; return}
  if {[lindex [split $arg] 0] == ""} {
    if {![regexp c [getchanmode $chan]]} {putquick "PRIVMSG $chan :\037ERROR\037: Incorrect Parameters. \037SYNTAX\037: [getTrigger]dquote <nickname-of-adder> <quote-to-del>"; return}
    if {[regexp c [getchanmode $chan]]} {putquick "PRIVMSG $chan :ERROR: Incorrect Parameters. SYNTAX: [getTrigger]dquote <nickname-of-adder> <quote-to-del>"; return}
    } else {
    set fid [open $checkfile r]
    if {[set pos [lsearch -glob [set ldata [split [read -nonewline $fid] \n]] $arg]] != -1} {
      close $fid
      set fid [open $checkfile w]
      puts $fid [join [lreplace $ldata $pos $pos] \n]
      close $fid
      putquick "PRIVMSG $chan :Deleted Quote!"
      } else {
      close $fid
      if {![regexp c [getchanmode $chan]]} {putquick "PRIVMSG $chan :\037ERROR\037: Unable to find -> $arg"; return}
      if {[regexp c [getchanmode $chan]]} {putquick "PRIVMSG $chan :ERROR: Unable to find -> $arg"; return}
    }
  }
}

proc rquote:pub {nick uhost hand chan arg} {
  global checkfile
  if {![channel get $chan quotes] && ![regexp c [getchanmode $chan]]} {putquick "PRIVMSG $chan :\037ERROR\037: Quote System is currently not enabled for $chan"; return}
  if {![channel get $chan quotes] && [regexp c [getchanmode $chan]]} {putquick "PRIVMSG $chan :ERROR: Quote System is currently not enabled for $chan"; return}
  if {[validuser $hand] || [isop $nick $chan] || [isvoice $nick $chan]} {
    set quotes ""
    set data [open $checkfile r]
    while {![eof $data]} {
      set quote [gets $data]
      if {$quote != ""} {
        set quotes [linsert $quotes end $quote]
      }
    }
    close $data
    set random [rand [llength $quotes]]
    if {($random >= 0) && ($random < [llength $quotes])} {
      set quote [lindex $quotes $random]
      set out1 [join [lrange [split $quote] 1 end]]
      set out [lindex $quote 0]
    }
    if {$quote != ""} {
      if {![regexp c [getchanmode $chan]]} {putquick "PRIVMSG $chan :Added by \037$out\037: $out1"; return}
      if {[regexp c [getchanmode $chan]]} {putquick "PRIVMSG $chan :Added by $out: $out1"; return}
    }
  }
}

proc quotes:pub {nick uhost hand chan arg} {
  if {![matchattr $hand o|m $chan]} {putquick "NOTICE $nick :\037ERROR\037: You do not have access to this command."; return}
  if {([lindex [split $arg] 0] == "") && ![regexp c [getchanmode $chan]]} {putquick "PRIVMSG $chan :\037ERROR\037: Incorrect Parameters. \037SYNTAX\037: [getTrigger]quotes on/off"; return}
  if {([lindex [split $arg] 0] == "") && [regexp c [getchanmode $chan]]} {putquick "PRIVMSG $chan :ERROR: Incorrect Parameters. SYNTAX: [getTrigger]quotes on/off"; return}

  if {[lindex [split $arg] 0] == "on"} {
    if {[channel get $chan quotes] && ![regexp c [getchanmode $chan]]} {putquick "PRIVMSG $chan :\037ERROR\037: This setting is already enabled."; return}
    if {[channel get $chan quotes] && [regexp c [getchanmode $chan]]} {putquick "PRIVMSG $chan :ERROR: This setting is already enabled."; return}
    channel set $chan +quotes
    puthelp "PRIVMSG $chan :Enabled Quote Commands for $chan"
  }

  if {[lindex [split $arg] 0] == "off"} {
    if {![channel get $chan quotes] && ![regexp c [getchanmode $chan]]} {putquick "PRIVMSG $chan :\037ERROR\037: This setting is already disabled."; return}
    if {![channel get $chan quotes] && [regexp c [getchanmode $chan]]} {putquick "PRIVMSG $chan :ERROR: This setting is already disabled."; return}
    channel set $chan -quotes
    puthelp "PRIVMSG $chan :Disabled Quote Commands for $chan"
  }
}

proc quotes:msg {nick uhost hand arg} {
  global botnick
  set chan [strlwr [lindex $arg 0]]
  if {![matchattr $hand o|m $chan]} {putquick "NOTICE $nick :\037ERROR\037: You do not have access to this command."; return}
  if {[lindex [split $arg] 0] == ""} {putquick "NOTICE $nick :\037ERROR\037: Incorrect Parameters. \037SYNTAX\037: /msg $botnick quotes #channel on/off"; return}
  if {[lindex [split $arg] 1] == ""} {putquick "NOTICE $nick :\037ERROR\037: Incorrect Parameters. \037SYNTAX\037: /msg $botnick quotes $chan on/off"; return}

  if {[lindex [split $arg] 1] == "on"} {
    if {[channel get $chan quotes]} {putquick "NOTICE $nick :\037ERROR\037: This setting is already enabled."; return}
    channel set $chan +quotes
    putquick "NOTICE $nick :Enabled Quote Commands for $chan"
  }

  if {[lindex [split $arg] 1] == "off"} {
    if {![channel get $chan quotes]} {putquick "NOTICE $nick :\037ERROR\037: This setting is already disabled."; return}
    channel set $chan -quotes
    putquick "NOTICE $nick :Disabled Quote Commands for $chan"
  }
}

proc qsearch:pub {nick uhost hand chan arg} {
  global checkfile
  if {![matchattr $hand o|o $chan]} {putquick "NOTICE $nick :\037ERROR\037: You do not have access to this command."; return}
  if {![channel get $chan quotes] && ![regexp c [getchanmode $chan]]} {putquick "PRIVMSG $chan :\037ERROR\037: Quote System is currently not enabled for $chan"; return}
  if {![channel get $chan quotes] && [regexp c [getchanmode $chan]]} {putquick "PRIVMSG $chan :ERROR: Quote System is currently not enabled for $chan"; return}
  set quotes ""
  if {[file exists $checkfile]} {
    set file [open $checkfile r]
    set line 0
    while {![eof $file]} {
      set quote [gets $file]
      if {$quote != ""} {
        set quotes [linsert $quotes end $quote]
      }
    }
    close $file
    if {$arg != ""} {
      foreach quote $quotes {
        set pattern [string tolower $arg]
        set lowquote [string tolower $quote]
        if {[string match $pattern $lowquote]} {
          set line [expr $line+1]
        }
      }
      } else {
      foreach quote $quotes {
        set line [expr $line+1]
      }
    }
    if {$arg != ""} {
      if {[channel get $chan quotes] && ![regexp c [getchanmode $chan]]} {putquick "PRIVMSG $chan :\002Q\002\037search\037: There are \002$line\002 quotes matching \037$arg\037"; return}
      if {[channel get $chan quotes] && [regexp c [getchanmode $chan]]} {putquick "PRIVMSG $chan :Qsearch: There are $line quotes matching $arg"; return}
    }
    if {$arg == ""} {
      if {[channel get $chan quotes] && ![regexp c [getchanmode $chan]]} {putquick "PRIVMSG $chan :\002Q\002\037count\037: Database currently supports \002$line\002 Quotes."; return}
      if {[channel get $chan quotes] && [regexp c [getchanmode $chan]]} {putquick "PRIVMSG $chan :Qcount: Database currently supports $line Quotes."; return}
    }
    return 1
  }
}

if {![file exists $checkfile]} {
  putlog "Couldn't detect quote.txt.... Creating file!"
  set newquote [open $checkfile a]
  puts $newquote "Get_A_Fix Smith & Wesson: The original point and click interface."
  close $newquote
} else {
set line 0
set quotes ""
set data [open $checkfile r]
while {![eof $data]} {
  set quote [gets $data]
  if {$quote != ""} {
    set quotes [linsert $quotes end $quote]
  }
}
close $data
}
if {$quote == ""} {
foreach quote $quotes {
  set line [expr $line+1]
}
putlog "Quote Database currently supports \002[list $line]\002 Quotes."
}

Comments

Sign in to comment.
Get_A_Fix   -  May 19, 2010

Jethro_, that isn't what I posted. This is the opposite to FordLawnMower's script here - This was the method I was asked for, so this is the method I made. Most, if not all of the code/snippets i've posted on this site have been because someone asked me for it, or asked for it a certain way.

 Respond  
Jethro   -  May 19, 2010

I've seen TCL sockets written before, and you can do the same by pulling random quotes from a website. The best thing about using a socket is that the storage of quotes won't be required.

 Respond  
mnhead   -  May 19, 2010

Cool Ratter...

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.