Formatting of code : You can (and must) use [ tcl ] an [ /tcl ] tags (without spaces in tags) to format your TCL codes and have syntax coloration x


help me fix small issue
#1
Hi All, need help from this script (credit to the owner), I've modified the script based on what I wanted. i just want the list of account users to be in  one line.

as of the moment it shows like this


Code:
<mabrook> !gc list
<botname> The GCASH USers list for #Channel: 1. John Doe +639564874562
<botname> The GCASH USers list for #Channel: 2. Jane Doe +639564874444
and i want like this;


Code:
<mabrook> !gc list
<botname> The GCASH USers list for #Channel: 1. John Doe +639564874562 2. Jane Doe +639564874444

and for the timer channel show; it will be the same

Code:
<botname>[1] John Doe +639564874562 [2] Jane Doe +639564874444


tcl
#Here you can set the start char of the command
set gcash(char) "!"
 
#Here you can set what flags can use the command
set gcash(flags) "nm|MNnA"
 
#Here you can set the time interval of listed gcash users
set gcash(time) "20"
 
#If you want the listed gcash users to be done by the PRIVMSG $chan method #set here "0" or if you want by the /me (action) set here "1"
set gcash(how) "0"
 
#######################################################################
#                         GCASH USERS
#######################################################################
 
bind pub $gcash(flags) $gcash(char)gc anounce:process
setudef flag announce
 
if {![info exists announce:timer_running]} {
timer $gcash(time) announce:timer
set announce:timer_running 1
}
 
proc anounce:process {nick host hand chan arg} {
global gcash
set flag "announce"
set dir "logs/gc($chan).txt"
set who [lindex [split $arg] 0]
set message [join [lrange [split $arg] 1 end]]
set number [lindex [split $arg] 1]
if {[channel get $chan announce]} { set status "on" } else { set status "off" }
if {$who == ""} { puthelp "NOTICE $nick :Please use $gcash(char)gc <on> | <off> | <add> <mesaj> | <list> | <del> <number>"
return 0
}
 
if {([regexp -nocase -- {(#[0-9]+|on|off|add|list|del)} $who tmp a]) && (![regexp -nocase -- {\S#} $who])} {
    switch $a {
on {
channel set $chan +$flag
puthelp "NOTICE $nick :I activated the GCASH system. To add an GCASH User Account just type $gcash(char)gc add <your name + mobile number>."
}
 
off {
channel set $chan -$flag
puthelp "NOTICE $nick :I deactivated the GCASH system."
}
 
add {
if {$message == ""} { puthelp "NOTICE $nick :Please use $gcash(char)gc add <your name + mobile number>"
return 0
}
if {$status == "off" } { puthelp "NOTICE $nick :First you have to activate the GCASH system by typing $gcash(char)gc on"
return 0
}
if {[file exists $dir] == 0} {
set file [open $dir a]
close $file
}
set file [open $dir a]
puts $file $message
close $file
puthelp "NOTICE $nick : GCASH Account Added"
puthelp "NOTICE $nick :$message"
}
 
list {
if {$status == "off" } { puthelp "NOTICE $nick :First you have to activate the GCASH system by typing $gcash(char)gc on"
return 0
}
if {[file exists $dir] == 0} {
set file [open $dir a]
close $file
}
set dir "logs/gc($chan).txt"
set file [open $dir "r"]
set w [read -nonewline $file]
close $file
set data [split $w "\n"]
set i 0
if {$data == ""} { puthelp "NOTICE $nick :There are no GCASH Account Added"
return 0
}
foreach account $data {
set i [expr $i +1]
puthelp "NOTICE $nick :The GCASH USers list for $chan: $i. $account"
}
}
 
del {
if {$message == ""} { puthelp "NOTICE $nick :Please use $gcash(char)gc del <number>. To list the GCASH Account type $gcash(char)gc list"
return 0
}
 
if {$status == "off" } { puthelp "NOTICE $nick :First you have to activate the GCASH system by typing $gcash(char)gc on"
return 0
}
set dir "logs/gc($chan).txt"
if {[file exists $dir] == 0} {
set file [open $dir a]
close $file
}
 
set file [open $dir "r"]
set data [read -nonewline $file]
close $file
set lines [split $data "\n"]
set i [expr $number - 1]
set delete [lreplace $lines $i $i]
set files [open $dir "w"]
puts $files [join $delete "\n"]
close $files
set file [open $dir "r"]
set data [read -nonewline $file]
close $file
if {$data == ""} {
set files [open $dir "w"]
close $files
}
 
puthelp "NOTICE $nick :Deleted the GCASH listed number :$number for $chan."
puthelp "NOTICE $nick :Please check if i am right by typing $gcash(char)gc list"
}
 
}
}
}
 
proc announce:timer {} {
global gcash count
foreach chan [channels] {
set dir "logs/gc($chan).txt"
if {[channel get $chan announce]} {
if {![info exists count(gc:$chan)]} {
set count(gc:$chan) 0
}
if {[file exists $dir] == 0} {
set file [open $dir a]
close $file
}
set file [open $dir "r"]
set data [read -nonewline $file]
close $file
set lines [split $data "\n"]
set line [lindex $lines $count(gc:$chan)]
incr count(gc:$chan)
if {$line == ""} {
unset count(gc:$chan)
}
if {$gcash(how) == "1"} {
puthelp "PRIVMSG $chan :\001ACTION $line\001"
} else { 
puthelp "PRIVMSG $chan :$line"
}
}
}
timer $gcash(time) announce:timer
return 1
}




Thank you guys in advance.
  Reply
#2
Wow, who wrote this awfull script ? And I'm not sure to understand its purpose... Just having a list of users and their phone number ?

Here are some horrors :)

tcl
if {[file exists $dir] == 0} {
set file [open $dir a]
close $file
}
set file [open $dir a]


tcl
set i [expr $i +1]


BTW, the better way imho is to have a setting "items per line" (to avoid to create a line with too much characters) and a small proc which will transform your datas in a kind of array, each entry containing X items.
zeolia: tchat gratuit, sans inscription ni publicité
Merci de ne pas demander d'aide en MP
Away
  Reply
#3
the original script is not intended for what i want, it works for another function. i am just modifying the variables because i am thinking it is closer to what i want.

maybe if you could help a little script with the same output above.
  Reply
#4
Explain exactly what you want to do with your script, how it has to work.
If the purpose is to add/remove members and display them, it could be with a really few lines of code.
zeolia: tchat gratuit, sans inscription ni publicité
Merci de ne pas demander d'aide en MP
Away
  Reply
#5
the script request will be like this;



Code:
** add the name & number

<botname> !add John Doe +639564874562
<botname> Added

<botname> !add Jane Doe +639514874573
<botname> Added

<botname> !gc list
<botname>[1] John Doe +639564874562 [2] Jane Doe +639514874573
That's it.
  Reply
#6
(24/04/2023, 20:07)mabrook Wrote: the script request will be like this;



Code:
** add the name & number

<botname> !add John Doe +639564874562
<botname> Added

<botname> !add Jane Doe +639514874573
<botname> Added

<botname> !gc list
<botname>[1] John Doe +639564874562 [2] Jane Doe +639514874573
That's it.

Like this ?

tcl
# Definition of namespace
namespace eval gc {
    variable contacts [dict create]
    variable char "!"
    variable flags "nm|MNnA"
}
 
# Function to add a contact
proc gc::add {name number} {
    set contact [list $name $number]
    dict lappend gc::contacts [dict size $gc::contacts] $contact
    return -code 1 "Added"
}
 
# Function to list contacts
proc gc::list {} {
    set i 1
    set result ""
    foreach index [lsort [dict keys $gc::contacts]] {
        set contact [dict get $gc::contacts $index]
        set name [lindex $contact 0]
        set number [lindex $contact 1]
        append result "[format {[%d] %s %s} $i $name $number] "
        incr i
    }
    return -code 1 $result
}
 
# Command association
bind pub -|- $gc(flags) $gc(char)gc gc::process
 
# Command processing procedure
proc gc::process {nick host hand chan arg} {
    set command [lindex [split $arg] 0]
 
    switch -exact $command {
        "!add" {
            set name [lindex [split $arg] 1]
            set number [lindex [split $arg] 2]
            try {
                gc::add $name $number
                putlog "Added contact $name $number"
            } on error {msg} {
                putlog "Error adding contact: $msg"
            }
        }
        "!gc" {
            set subcommand [lindex [split $arg] 1]
 
            switch -exact $subcommand {
                "list" {
                    try {
                        set contacts [gc::list]
                        putlog "Contacts: $contacts"
                    } on error {msg} {
                        putlog "Error listing contacts: $msg"
                    }
                }
            }
        }
    }
}


Note: putlog show in partyline, fix for your usage.
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
#7
hi, I've tried but this shows:


Code:
<mabrook> [01:09:19] can't read "gc(flags)": no such variable
<mabrook>     while executing
<mabrook> "bind pub -|- $gc(flags) $gc(char)gc gc::process"
<mabrook>     (file "scripts/gc.tcl" line 30)
<mabrook>     invoked from within

thanks
  Reply
#8
Replace respectivaly $gc(flags) $gc(char)gc with ${::gc::flags} and ${::gc::char}
zeolia: tchat gratuit, sans inscription ni publicité
Merci de ne pas demander d'aide en MP
Away
  Reply
#9
script is updated...

but when I type: !add John Doe +639564874562

nothing happen, and no error is showing..
  Reply
#10
This is because the code is messed up...

tcl
namespace eval gc {
    variable contacts [dict create]
    variable char "!"
    variable flags "nm|MNnA"
}
bind pub -|- ${::gc::flags} ${::gc::char}gc gc::process


Result of that: bind pub -|- nm|MNnA !gc gc::process
Double flags, I don't understand why it doesn't throw an error


tcl
proc gc::process {nick host hand chan arg} {
    set command [lindex [split $arg] 0]
    switch -exact $command {
        "!add" {

Looks like the waited command is !gc !add user number

Nice try from @ZarTek, but he wanted to do a complex code for simple thing but the basics things aren't here.

Here is the basic version of the script you want:

tcl
namespace eval gc {
 
   variable flags n|-
 
   variable db "gc.db"
 
   proc process {nick uhost handle chan text} {
      set args [split $text]
      if {[llength $args]<1} {
         putserv "PRIVMSG $chan :Use !gc <add|list> \[datas\]"
         return
      }
      switch -nocase -- [lindex $args 0] {
         "add" {
            set fo [open $::gc::db a]
            puts $fo [join [lrange $args 1 end]]
            close $fo
            putserv "PRIVMSG $chan :Added [join [lrange $args 1 end]] in database"
         }
         "list" {
            if {![file exists $::gc::db]} {
               putserv "PRIVMSG $chan :DB doesn't exists"
               return
            }
            set fi [open $::gc::db r]
            set datas [read -nonewline $fi]
            close $fi
            if {[llength $datas]==0} {
               putserv "PRIVMSG $chan :DB is empty"
               return
            }
            set i 0
            set lines {}
            foreach d [split $datas "\n"] {
               incr i
               lappend lines "\[$i\] $d"
            }
            putserv "PRIVMSG $chan :[join $lines]"
         }
      }
   }
 
   bind pub $::gc::flags !gc ::gc::process
}


(command is !gc add and !gc list)
zeolia: tchat gratuit, sans inscription ni publicité
Merci de ne pas demander d'aide en MP
Away
  Reply
#11
thanks @CrazyCat, indeed it works perfect. hopefully you will not get tired of helping newbie users for simple scripts.

thanks also to Zartek.
  Reply
#12
(26/04/2023, 10:11)CrazyCat Wrote: Nice try from @ZarTek, but he wanted to do a complex code for simple thing but the basics things aren't here.

Thank you, yes indeed it was not a turnkey solution. Only one direction to help. That's why I created separate functions for each task. Using best practices such as namespaces, error checking with "try". I also voluntarily put "putlog" rather than putquick. On the other hand the double flags in the bind was not wanted. Obviously if we want to make a basic code and perhaps simple and short I imagine that I could have given something like this:


tcl
proc gc:p {nick uhost handle chan text} {
  if {[lindex [split $text] 0] eq "add"} {
    set o [open "gc.db" a]; puts $o [join [lrange [split $text] 1 end]]; close $o
    putserv "PRIVMSG $chan :Added [join [lrange [split $text] 1 end]] in database"
  } elseif {[lindex [split $text] 0] eq "list"} {
    if {[catch {set i [open "gc.db" r]}] || ![llength [set d [read -nonewline $i]]]} {putserv "PRIVMSG $chan :DB doesn't exist or is empty"; close $i; return}
    close $i
    foreach e [split $d "\n"] {lappend l "\[[incr x]\] $e"}
    putserv "PRIVMSG $chan :[join $l]"
  }
}
bind pub n|- !gc gc:p


You are welcome @mabrook
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
#13
hello guys, sorry for bothering much..

can we add !delete function from the scripts




Code:
<mabrook>!delete 1
<botname>List already deleted from db.
  Reply
#14
Here is the code.

Note that when we ask you what you expect the script to do, you have to tell everything. This means I won't do any new thing on this script, particularly because you now have a lot of elements to understand it and adapt to your need. We try to teach how to script, we are not here to answer freely to any ask of lazzy pple.


tcl
namespace eval gc {
 
   variable flags n|-
 
   variable db "gc.db"
 
   proc process {nick uhost handle chan text} {
      set args [split $text]
      if {[llength $args]<1} {
         putserv "PRIVMSG $chan :Use !gc <add|list|del> \[datas\]"
         return
      }
      switch -nocase -- [lindex $args 0] {
         "add" {
            set fo [open $::gc::db a]
            puts $fo [join [lrange $args 1 end]]
            close $fo
            putserv "PRIVMSG $chan :Added [join [lrange $args 1 end]] in database"
         }
         "list" {
            if {![file exists $::gc::db]} {
               putserv "PRIVMSG $chan :DB doesn't exists"
               return
            }
            set fi [open $::gc::db r]
            set datas [read -nonewline $fi]
            close $fi
            if {[llength $datas]==0} {
               putserv "PRIVMSG $chan :DB is empty"
               return
            }
            set i 0
            set lines {}
            foreach d [split $datas "\n"] {
               incr i
               lappend lines "\[$i\] $d"
            }
            putserv "PRIVMSG $chan :[join $lines]"
         }
         "del" {
            if {![file exists $::gc::db]} {
               putserv "PRIVMSG $chan :DB doesn't exists"
               return
            }
            if {[llength $args]!=2 || ![string is integer [lindex $args 1]]} {
               putserv "PRIVMSG $chan :Give an index to delete"
               return
            }
            set fi [open $::gc::db r]
            set datas [read -nonewline $fi]
            close $fi
            set fo [open $::gc::db w]
            set i 0
            set dline ""
            foreach d [split $datas "\n"] {
               incr i
               if {$i == [lindex $args 1]} {
                  set dline $d
                  continue
               }
               puts $fo $d
            }
            close $fo
            if {$dline ne ""} {
               putserv "PRIVMSG $chan :Deleted $dline"
            }
         }
      }
   }
 
   bind pub $::gc::flags !gc ::gc::process
}

zeolia: tchat gratuit, sans inscription ni publicité
Merci de ne pas demander d'aide en MP
Away
  Reply
#15
yes, CrazyCat. all is perfect. thank you for the support
  Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)