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 

reducing usage of set/variable

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


Joined: 22 Feb 2020
Posts: 398

PostPosted: Fri Jun 25, 2021 2:30 pm    Post subject: reducing usage of set/variable Reply with quote

Anyway possible to not use variable soo many times, maybe like this.
Code:

foreach {foo bar moo} [split $args] {break}


For this code
Code:

namespace eval CT-IP {

############################
#  Start of configuration  #
#--------------------------#

##########################
# Trigger
##
variable trig "!ip"

##########################
# API
##
variable api "2bbd882cec5fa5f26d53fe6049a21343"


##########################
# Flags
##
# n = Owner
# m = Master
# o = Op
# h = Halfop
# v = Voice
# f = Friend
# - = Everyone
##
variable flag "-|-"


##########################
# Output
##
# M = Message
# N = Notice
# N = Nick
# C = Channel
##
variable outp "MC"

 
############################
#   End of configuration   #
#--------------------------#

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

variable author "ComputerTech"
variable version "v0.1"
variable name "CT-IP"

package require http
package require json

bind PUB $flag $trig [namespace current]::IP:proc

proc putout {text} {
   variable outp
   upvar 1 nick nick chan chan
   switch -- [string index $outp 0] {
      "M" {variable action "PRIVMSG"}
      "N" {variable action "NOTICE"}
   }
   switch -- [string index $outp 1] {
      "N" {variable target $nick}
      "C" {variable target $chan}
   }
   putserv "${action} ${target} :$text"
}

proc IP:proc {nick host hand chan text} {
 variable api
 variable url "http://api.ipstack.com/"
 variable params [::http::formatQuery q [lindex $text 0] $api]
 variable data [http::data [http::geturl "$url?$params" -timeout 10000]]
      variable data2 [::json::json2dict $data]
      http::cleanup $data2
  variable ip [dict get $data2 "ip"]
  variable hostname [dict get $data2 "hostname"]
  variable type [dict get $data2 "type"]
  variable continent [dict get $data2 "continent_name"]
  variable country [dict get $data2 "country_name"]
  variable region [dict get $data2 "region_name"]
  variable city [dict get $data2 "city"]
  variable latitude [dict get $data2 "latitude"]
  variable longitude [dict get $data2 "longitude"]
  variable location [dict get $data2 "location"]
  variable language [dict get $location "languages"]
  variable name [dict get $language "name"]
putout "\002IP\002: $ip | \002Hostname\002: $hostname | \002Type\002: $type"
putout "\002Continent\002: $continent | \002Country\002: $country | \002Region\002: $region"
putout "\002City\002: $city | \002Latitude\002: $latitude | \002Longitude\002 $longitude |\002Language\002 $name"
  }
putlog "$name $version by $author Loaded!"
}


Imo it looks messy with multiple usage of variable or set
thanks in advanced Razz
_________________
ComputerTech
Back to top
View user's profile Send private message Send e-mail Visit poster's website
CrazyCat
Revered One


Joined: 13 Jan 2002
Posts: 1108
Location: France

PostPosted: Fri Jun 25, 2021 3:16 pm    Post subject: Reply with quote

You don't want to name all the variables in the dict ?
Code:
foreach varname [dict keys $data2] {
   set ip($varname) [dict get $data2 $varname]
}

And after, use $ip(ip), $ip(hostname), ...
_________________
https://www.eggdrop.fr - French IRC network
Offer me a coffee - Do not ask me help in PM, we are a community.
Back to top
View user's profile Send private message Visit poster's website
ComputerTech
Master


Joined: 22 Feb 2020
Posts: 398

PostPosted: Fri Jun 25, 2021 7:19 pm    Post subject: Reply with quote

That looks perfect CrazyCat, i'll go test it Very Happy
_________________
ComputerTech
Back to top
View user's profile Send private message Send e-mail Visit poster's website
ComputerTech
Master


Joined: 22 Feb 2020
Posts: 398

PostPosted: Fri Jun 25, 2021 7:38 pm    Post subject: Reply with quote

Okay so, i tried it and i get this error
Code:

[00:37:20] Tcl error [::CT-IP::IP:proc]: can't define "ip(ip)": name refers to an element in an array

with this code
Code:


namespace eval CT-IP {

   ############################
   #  Start of configuration  #
   #--------------------------#

   ##########################
   # Trigger
   ##
   variable trig "!ip"

   ##########################
   # API
   ##
   variable api "2bbd882cec5fa5f26d53fe6049a21343"


   ##########################
   # Flags
   ##
   # n = Owner
   # m = Master
   # o = Op
   # h = Halfop
   # v = Voice
   # f = Friend
   # - = Everyone
   ##
   variable flag "-|-"


   ##########################
   # Output
   ##
   # M = Message
   # N = Notice
   # N = Nick
   # C = Channel
   ##
   variable outp "MC"

   ############################
   #   End of configuration   #
   #--------------------------#

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

   variable author "ComputerTech"
   variable version "v0.1"
   variable name "CT-IP"

   package require http
   package require json

   bind PUB $flag $trig [namespace current]::IP:proc

   proc putout {text} {
      variable outp
      upvar 1 nick nick chan chan
      switch -- [string index $outp 0] {
         "M" {variable action "PRIVMSG"}
         "N" {variable action "NOTICE"}
      }
      switch -- [string index $outp 1] {
         "N" {variable target $nick}
         "C" {variable target $chan}
      }
      putserv "${action} ${target} :$text"
   }
   proc IP:proc {nick host hand chan text} {
      variable api
                       variable url "http://api.ipstack.com/[lindex $text 0]?access_key=$api&hostname=1"
      variable data [http::data [http::geturl "$url" -timeout 10000]]
      variable data2 [::json::json2dict $data]
      http::cleanup $data2
                        foreach varname [dict keys $data2] {
                        variable ip($varname) [dict get $data2 $varname]
}
        putout "$ip(ip)"
   }
   putlog "$name $version by $author Loaded!"
}


And here's a example from the json output
Code:

{
  "ip": "2a07:4840::1:0:0:0:2f",
  "hostname": "2a07:4840::1:0:0:0:2f",
  "type": "ipv6",
  "continent_code": "EU",
  "continent_name": "Europe",
  "country_code": "GB",
  "country_name": "United Kingdom",
  "region_code": "ENG",
  "region_name": "England",
  "city": "Maidenhead",
  "zip": "SL6",
  "latitude": 51.531681060791016,
  "longitude": -0.7307500243186951,
  "location": {
    "geoname_id": 2643186,
    "capital": "London",
    "languages": [
      {
        "code": "en",
        "name": "English",
        "native": "English"
      }
    ],
    "country_flag": "http://assets.ipstack.com/flags/gb.svg",
    "country_flag_emoji": "🇬🇧",
    "country_flag_emoji_unicode": "U+1F1EC U+1F1E7",
    "calling_code": "44",
    "is_eu": true
  }
}

_________________
ComputerTech
Back to top
View user's profile Send private message Send e-mail Visit poster's website
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3767
Location: Mint Factory

PostPosted: Sat Jun 26, 2021 2:58 am    Post subject: Reply with quote

He said
Code:

set ip($varname) [dict get $data2 $varname]

and you used:
Code:

variable ip($varname) [dict get $data2 $varname]

To make it less confusing I would replace varname with key, since that's what you are looping over the keys from that dictionary.
_________________
Once the game is over, the king and the pawn go back in the same box.
Back to top
View user's profile Send private message
ComputerTech
Master


Joined: 22 Feb 2020
Posts: 398

PostPosted: Sat Jun 26, 2021 10:26 am    Post subject: Reply with quote

oh damn lol, i missed that Laughing

Thanks caesar Razz
_________________
ComputerTech
Back to top
View user's profile Send private message Send e-mail Visit poster's website
ComputerTech
Master


Joined: 22 Feb 2020
Posts: 398

PostPosted: Sat Jun 26, 2021 8:33 pm    Post subject: Reply with quote

Still don't understand why it generates a error in Partyline.
Code:

[01:32:57] Tcl error [::CT-IP::IP:proc]: can't define "ip 2001:bc8:32d7:26c:: hostname 2001:bc8:32d7:26c:: type ipv6 continent_code EU continent_name Europe country_code FR country_name France region_code IDF region_name Île-de-France city Saint-Ouen zip 75008 latitude 48.861000061035156 longitude 2.3380000591278076 location {geoname_id 2977824 capital Paris languages {{code fr name French native Français}} country_flag http://assets.i

With this code
Code:

namespace eval CT-IP {

   ############################
   #  Start of configuration  #
   #--------------------------#

   ##########################
   # Trigger
   ##
   variable trig "!ip"

   ##########################
   # API
   ##
   variable api "2bbd882cec5fa5f26d53fe6049a21343"


   ##########################
   # Flags
   ##
   # n = Owner
   # m = Master
   # o = Op
   # h = Halfop
   # v = Voice
   # f = Friend
   # - = Everyone
   ##
   variable flag "-|-"


   ##########################
   # Output
   ##
   # M = Message
   # N = Notice
   # N = Nick
   # C = Channel
   ##
   variable outp "MC"

   ############################
   #   End of configuration   #
   #--------------------------#

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

   variable author "ComputerTech"
   variable version "v0.1"
   variable name "CT-IP"

   package require http
   package require json

   bind PUB $flag $trig [namespace current]::IP:proc

   proc putout {text} {
      variable outp
      upvar 1 nick nick chan chan
      switch -- [string index $outp 0] {
         "M" {variable action "PRIVMSG"}
         "N" {variable action "NOTICE"}
      }
      switch -- [string index $outp 1] {
         "N" {variable target $nick}
         "C" {variable target $chan}
      }
      putserv "${action} ${target} :$text"
   }
   proc IP:proc {nick host hand chan text} {
      variable api
                       variable url "http://api.ipstack.com/[lindex $text 0]?access_key=$api&hostname=1"
      variable data [http::data [http::geturl "$url" -timeout 10000]]
      variable data2 [::json::json2dict $data]
      http::cleanup $data2
                        foreach key [dict keys $data2] {
                        set ip($key) [dict get $data2 $key]
}
        putout "$ip(ip)"
   }
   putlog "$name $version by $author Loaded!"
}

Rolling Eyes
_________________
ComputerTech
Back to top
View user's profile Send private message Send e-mail Visit poster's website
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3767
Location: Mint Factory

PostPosted: Sun Jun 27, 2021 3:45 am    Post subject: Reply with quote

Most likely cos you are doing a cleanup on $data2, instead of $data.. anyway your variables confuses me so I made it from scratch.
Code:

proc IP:proc {nick host hand chan text} {
   variable api
   if {[scan $text {%s} ip] < 1} return
   set url "http://api.ipstack.com/$ip?access_key=$api&hostname=1"
   catch {set http [::http::geturl $url -timeout 6000]} error
   set data [::http::data $http]
   set dict [::json::json2dict $data]
   ::http::cleanup $http
   foreach key [dict keys $dict] {
      set result($key) [dict get $dict $key]
   }
   putout "$result(ip)"
}

_________________
Once the game is over, the king and the pawn go back in the same box.
Back to top
View user's profile Send private message
ComputerTech
Master


Joined: 22 Feb 2020
Posts: 398

PostPosted: Sun Jun 27, 2021 10:29 am    Post subject: Reply with quote

That works perfect caesar! thanks a ton mate Wink
_________________
ComputerTech
Back to top
View user's profile Send private message Send e-mail Visit poster's website
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