Mise en forme de code: pensez à utiliser les balises [ tcl ] et [ /tcl ] (sans les espaces bien sûr) autour de vos codes tcl afin d'avoir un meilleur rendu et une coloration syntaxique. x


Diffuser une annonce sur un channel toutes les x minutes
#1
Bonsoir,

Voici un script assez demandé qui vous permet de diffuser des annonces importantes (ou pas) toutes les 10 minutes dans votre channel et dans l'ordre du fichier. (Elles ne sont pas diffusées aléatoirement)

Vous avez 3 commandes à votre disposition qui sont à exécuter dans le salon de log (modifiable dans la partie #variable du tcl) :

- !addannonce <votre annonce> (cette commande vous permettra d'ajouter une annonce)
- !delannonce <votre annonce> (cette commande vous permettra de supprimer une annonce)
- !annonce (cette commande vous permettra d'avoir la liste des annonces)


tcl
if {[::tcl::info::commands ::msgannonce::uninstall] eq "::msgannonce::uninstall"} { ::msgannonce::uninstall }
if { [package vcompare [regexp -inline {^[[:digit:]\.]+} $::version] 1.6.20] == -1 } {
putloglev o * "\00304\[msgannonce - erreur\]\003 La version de votre Eggdrop est\00304 ${::version}\003; msgannonce ne fonctionnera correctement que sur les Eggdrops version 1.6.20 ou supérieure." ; return }
if { [::tcl::info::tclversion] < 8.5 } {
putloglev o * "\00304\[msgannonce - erreur\]\003 msgannonce nécessite que Tcl 8.5 (ou plus) soit installé pour fonctionner. Votre version actuelle de Tcl est\00304 ${::tcl_version}\003." ; return }
package require Tcl 8.5
 
namespace eval ::msgannonce {
 
 
############
# VARIABLE #
############
 
# Chemin vers le fichier des annonces
set ::msgannonce::fichier(annonces) "scripts/Eggdrop/annonces.txt"
# Salon de log ou vous pourrez faire les commandes !addannonce/!delannonce/!annonce
set ::msgannonce::back "#Log"
# Salon ou sera diffusé les annonces
set ::msgannonce::salon "#Home"
 
set ::msgannonce::current_line 0
 
###########
# FICHIER #
###########
 
set ::msgannonce::fichiers [list $::msgannonce::fichier(annonces)]
 
foreach txt $fichiers {
  if ![file exists $txt] {
      close [open $txt w+]
  }
}
 
# Procédure de désinstallation : le script se désinstalle totalement avant
# chaque rehash ou à chaque relecture au moyen de la commande "source" ou
# autre.
 
proc ::msgannonce::uninstall {args} {
putlog "Désallocation des ressources de \002msgannonce\002..."
# Suppression des binds.
foreach binding [lsearch -inline -all -regexp [binds *[set ns [::tcl::string::range [namespace current] 2 end]]*] " \{?(::)?$ns"] {
unbind [lindex $binding 0] [lindex $binding 1] [lindex $binding 2] [lindex $binding 4]
}
# Arrêt des utimers en cours.
foreach running_utimer [utimers] {
if { [::tcl::string::match "*[namespace current]::*" [lindex $running_utimer 1]] } { killutimer [lindex $running_utimer 2] }
}
 
namespace delete ::msgannonce
}
}
 
############
#  PROC  #
############
 
bind cron - "*/10 * * * *" ::msgannonce::annoncetime
proc ::msgannonce::annoncetime {minute hour day month year} {
  set fi [open $::msgannonce::fichier(annonces) r]
  for { set cpt 0 } { ($cpt <= $::msgannonce::current_line) && ![eof $fi] } { incr cpt } {
    set line [gets $fi]
  }
  if { $line != "" } {
    puthelp "PRIVMSG $::msgannonce::salon :$line"
  }
  if { [eof $fi]} {
  set ::msgannonce::current_line 0
  } else {
  incr ::msgannonce::current_line
  }
  close $fi
}
 
proc ::msgannonce::+annonce {nick uhost handle chan text} {
  set annonce [join [lrange $text 0 end]]
  if {$chan eq $::msgannonce::back} {
    if {$annonce eq "" } {
    puthelp "PRIVMSG $::msgannonce::back :\002Erreur :\002 !addannonce <annonce>"
        return
    }
    if {[verify:b $msgannonce::fichier(annonces) $annonce]} {
      puthelp "PRIVMSG $::msgannonce::back :\002Erreur, annonce existante :\002 $annonce ";
      return
    }
    puthelp "PRIVMSG $::msgannonce::back :\02\[\002Annonce ajouté\02]\002 $annonce "
    ::msgannonce::ajouter:donnee $::msgannonce::fichier(annonces) $annonce
  } else {
    return
  }
}
 
proc ::msgannonce::-annonce {nick uhost handle chan text} {
  set num [lindex $text 0]
  if {$chan ne $::msgannonce::back} {return}
  if {$num eq ""} {
    puthelp "PRIVMSG $::msgannonce::back :\002Erreur :\002 !delannonce <ANNONCE>";
    return
  }
  ::msgannonce::effacer:donnee $::msgannonce::fichier(annonces) $num
}
 
proc ::msgannonce::pub:annonce {nick uhost hand chan args} {
  if {$chan eq $::msgannonce::back} {
      set fp [open $::msgannonce::fichier(annonces) "r"]
      set fc -1
      while {![eof $fp]} {
          set data [gets $fp]
          incr fc
          if {$data !=""} {
              puthelp "NOTICE $nick :$fc - $data"
          }
          unset data
      }
      close $fp
      puthelp "NOTICE $nick :Fin de liste des annonces"
  } else {
      return
  }
}
 
######################
# TRAITEMENT FICHIER #
######################
 
proc ::msgannonce::verify:b {fichiers arg} {
      set liste [open $fichiers r]
      set fc -1
      while {![eof $liste]} {
      incr fc
      gets $liste a
      regsub -all -- {\{|\}|\\{|[}{|]}} $a "" a
      if {$a ne ""} {if {[string equal -nocase $a $arg]} {close $liste;return $fc}}
      }
      close $liste
      return 0
}
 
 
proc ::msgannonce::ajouter:donnee {fichiers arg} {
      set liste [open $fichiers a]
      puts $liste "$arg"
      close $liste
}
 
proc ::msgannonce::effacer:donnee {file arg} {
      incr arg 0
      set fileID [open $file r]
 
      while { ![eof $fileID] } {
          if { [set line [gets $fileID]] ne {} } {
                  lappend lines_list $line
 
          }
      }
      if {$arg > [llength $lines_list]} {
        puthelp "PRIVMSG $::msgannonce::back :$arg n'existe pas "
        return
      } else {
        puthelp "PRIVMSG $::msgannonce::back :L'Annonce $arg supprimé. "
      }
      close $fileID
      set fileID [open $file w]
      incr arg -1
      puts $fileID [join [lreplace $lines_list $arg $arg] "\x0D\x0A"]
      close $fileID
}
 
 
############
#  BIND  #
############
 
bind pub - !addannonce ::msgannonce::+annonce
bind pub - !delannonce ::msgannonce::-annonce
bind pub - !annonce ::msgannonce::pub:annonce
bind evnt - prerehash ::msgannonce::uninstall

  Reply
#2

tcl
namespace eval ::msgAnnounce {
  variable announcementFile "scripts/Eggdrop/announcements.txt"
  variable logChannel "#Log"
 
  proc addAnnouncement {nick uhost handle chan text} {
    variable announcementFile
    variable logChannel
 
    set announcement [join [lrange $text 0 end]]
    if {$chan ne $logChannel} {return}
    if {$announcement eq ""} {
      putquick "PRIVMSG $logChannel :\002Error:\002 !addAnnouncement <announcement>"
      return
    }
    if {[verifyEntry $announcementFile $announcement]} {
      putquick "PRIVMSG $logChannel :\002Error, existing announcement:\002 $announcement "
      return
    }
    putquick "PRIVMSG $logChannel :\02\[\002Announcement added\02]\002 $announcement "
    appendData $announcementFile $announcement
  }
 
  proc deleteAnnouncement {nick uhost handle chan text} {
    variable announcementFile
    variable logChannel
 
    set num [lindex $text 0]
    if {$chan ne $logChannel} {return}
    if {$num eq ""} {
      putquick "PRIVMSG $logChannel :\002Error:\002 !delAnnouncement <ANNOUNCEMENT>"
      return
    }
    removeData $announcementFile $num
  }
 
  proc showAnnouncements {nick uhost hand chan args} {
    variable announcementFile
    variable logChannel
 
    if {$chan ne $logChannel} {return}
    set fileHandle [open $announcementFile "r"]
    set lineNumber 0
    while {![eof $fileHandle]} {
      set line [gets $fileHandle]
      if {$line ne ""} {
        putquick "NOTICE $nick :$lineNumber - $line"
        incr lineNumber
      }
    }
    close $fileHandle
    putquick "NOTICE $nick :End of announcements list"
  }
 
  proc verifyEntry {filePath arg} {
    set fileHandle [open $filePath r]
    while {![eof $fileHandle]} {
      set line [gets $fileHandle]
      if {$line ne "" && [string equal -nocase $line $arg]} {
        close $fileHandle
        return 1
      }
    }
    close $fileHandle
    return 0
  }
 
  proc appendData {filePath arg} {
    set fileHandle [open $filePath a]
    puts $fileHandle "$arg"
    close $fileHandle
  }
 
  proc removeData {filePath arg} {
    variable logChannel
 
    set linesList [readLines $filePath]
    set listLength [llength $linesList]
    if {$arg >= $listLength} {
      putquick "PRIVMSG $logChannel :$arg doesn't exist in the announcement list. Please double-check the number and try again."
      return
    }
    set removedLine [lindex $linesList $arg]
    set updatedList [lreplace $linesList $arg $arg]
    writeLines $filePath $updatedList
    putquick "PRIVMSG $logChannel :\02\[\002Announcement deleted\02]\002 $removedLine"
  }
 
  proc readLines {filePath} {
    set fileHandle     [open $filePath r]
    set linesList {}
    while {![eof $fileHandle]} {
      set line [gets $fileHandle]
      if {$line ne ""} {
        lappend linesList $line
      }
    }
    close $fileHandle
    return $linesList
  }
 
  proc writeLines {filePath linesList} {
    set fileHandle [open $filePath w]
    foreach line $linesList {
      puts $fileHandle $line
    }
    close $fileHandle
  }
 
  proc uninstall {args} {
    bind pub - !addAnnouncement {}
    bind pub - !delAnnouncement {}
    bind pub - !showAnnouncements {}
    bind evnt - prerehash {}
    putquick "PRIVMSG $logChannel :Message Announcement script uninstalled."
  }
 
  # Initialization
  if {[file exists $announcementFile]} {
    putquick "PRIVMSG $logChannel :Message Announcement script loaded."
  } else {
    set fp [open $announcementFile "w"]
    close $fp
    putquick "PRIVMSG $logChannel :Message Announcement script loaded. Created a new announcement file."
  }
}
bind pub - !addAnnouncement ::msgAnnounce::addAnnouncement
bind pub - !delAnnouncement ::msgAnnounce::deleteAnnouncement
bind pub - !showAnnouncements ::msgAnnounce::showAnnouncements
bind evnt - prerehash ::msgAnnounce::uninstall

Retrouvez les dernières modifications de mes scripts TCL (versions Alpha/Bêta) ainsi que d'autres de mes réalisations sur ma page GitHub et les versions stables dans la section scripts de ce site  8-)
  Reply
#3
Ce qui pourrait être utile, c'est de le rendre :

- multichannel
- supprimer le numéro de la ligne au lieu de supprimer la chaine, apparemment c'est embêtant quand il y a des codes couleurs
  Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Débannir un utilisateur au bout de X minutes Amand 0 90 07/04/2023, 23:43
Last Post: Amand

Forum Jump:


Users browsing this thread: 1 Guest(s)