Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9093689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:09:57+00:00 2026-06-16T23:09:57+00:00

I have an IRC bot and I’m trying to get information for game server

  • 0

I have an IRC bot and I’m trying to get information for game server (GTA SA Multiplayer).
I have ready-to-use query, but I can’t implement it into my bot. It works if I try to load the same script, but without getting it into bot’s structure. The error it gives me is

NameError: name ‘ip’ is not defined

I’ve tried adding the ip address as argument in def(inp,say=None), but it still didn’t work. That’s the code:

from util import Query
from util import hook
import sys

@hook.command
def serverinfo(inp,ip="",port="",say=None):
    ip = "78.129.221.58"
    port = 7777 
if len(sys.argv) >= 3:
    ip = str(sys.argv[1])
    port = int(sys.argv[2])

query = Query(ip,port)
info = query.GetInformation()
say(info)
if info['players'] <= 100:
    say(query.GetPlayers())
    say(query.GetDetailedPlayers())
else: say('can\' get players because players is above 100')
say(query.Ping())
query.Close()

That’s Query that I import:

import socket, struct, random, datetime
from cStringIO import StringIO

class Query:
    def __init__(self, ip, port):
        self.ip, self.port = socket.gethostbyname(ip), port
        self.data = StringIO("")
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.sock.connect((ip, port))
        self.sock.settimeout(1)

    def CreatePacket(self, opcode):
        ips = self.ip.split('.');
        packet = "SAMP{0}{1}{2}{3}{4}{5}{6}".format(chr(int(ips[0])), chr(int(ips[1])), chr(int(ips[2])), chr(int(ips[3])), chr(self.port & 0xFF), chr(self.port >> 8 & 0xFF), opcode)
        if opcode == 'p':
            packet += struct.pack("BBBB", random.randint(0, 255), random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
        return packet

    def GetInformation(self):
        try:
            self.sock.send(self.CreatePacket('i'))
            info = {}
            self.data = StringIO(self.sock.recv(2048))
            self.data.read(11)
            info['passworded'] = struct.unpack('?', self.data.read(1))[0]
            info['players'] = struct.unpack('h', self.data.read(2))[0]
            info['maxplayers'] = struct.unpack('h', self.data.read(2))[0]

            info['hostname'] = self.data.read(struct.unpack('i', self.data.read(4))[0])
            info['gamemode'] = self.data.read(struct.unpack('i', self.data.read(4))[0])
            info['mapname'] = self.data.read(struct.unpack('i', self.data.read(4))[0])
        except socket.timeout:
            info['error'] = 1
        return info

    def GetRules(self):
        try:
            self.sock.send(self.CreatePacket('r'))
            rules = {}
            self.data = StringIO(self.sock.recv(2048))
            self.data.read(11)
            rulecount = struct.unpack('h', self.data.read(2))[0]
            for i in range(rulecount):
                name = self.data.read(struct.unpack('b', self.data.read(1))[0])
                rules[name] = self.data.read(struct.unpack('b', self.data.read(1))[0])
        except socket.timeout:
            rules['error'] = 1
        return rules

    def GetPlayers(self):
        try:
            self.sock.send(self.CreatePacket('c'))
            players = []
            self.data = StringIO(self.sock.recv(2048))
            self.data.read(11)
            playercount = struct.unpack('h', self.data.read(2))[0]
            for i in range(playercount):
                name = self.data.read(struct.unpack('b', self.data.read(1))[0])
                players.append([name, struct.unpack('i', self.data.read(4))[0]])
        except socket.timeout:
            players = {'error': 1}
        return players

    def GetDetailedPlayers(self):
        try:
            self.sock.send(self.CreatePacket('d'))
            players = []
            self.data = StringIO(self.sock.recv(2048))
            self.data.read(11)
            playercount = struct.unpack('h', self.data.read(2))[0]
            for i in range(playercount):
                playerid = struct.unpack('b', self.data.read(1))[0]
                name = self.data.read(struct.unpack('b', self.data.read(1))[0])
                score = struct.unpack('i', self.data.read(4))[0]
                ping = struct.unpack('i', self.data.read(4))[0]
                players.append([playerid, name, score, ping])
        except socket.timeout:
            players = {'error': 1}
        return players

    def Close(self):
        self.sock.close()   

    def Ping(self):
        packet = self.CreatePacket('p')
        a = datetime.datetime.now()
        self.sock.send(packet)
        self.sock.recv(2048)
        b = datetime.datetime.now()
        c = b - a
        return int((c.days * 24 * 60 * 60 + c.seconds) * 1000 + c.microseconds / 1000.0)

class Rcon:
    def __init__(self, ip, port, password):
        self.ip, self.port, self.password = socket.gethostbyname(ip), port, password
        self.data = StringIO("")
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.sock.connect((ip, port))
        self.sock.settimeout(0.5)

    def CreatePacket(self, opcode, password, command):
        ips = self.ip.split('.');
        packet = "SAMP{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}".format(chr(int(ips[0])), chr(int(ips[1])), chr(int(ips[2])), chr(int(ips[3])), chr(self.port & 0xFF), chr(self.port >> 8 & 0xFF), opcode, chr(len(password) & 0xFF), chr(len(password) >> 8 & 0xFF), password, chr(len(command) & 0xFF), chr(len(command) >> 8 & 0xFF), command)
        return packet

    def Send(self, command):
        self.sock.send(self.CreatePacket('x', self.password, command))
        output = []
        while 1:
            try:
                self.data = StringIO(self.sock.recv(2048))
                self.data.read(11)
                strlen = struct.unpack('h', self.data.read(2))[0]
                if strlen == 0: break
                output += [self.data.read(strlen)]
            except: break;
        return output

    def Close(self):
        self.sock.close()

Any ideas?

Edit: After some changes I did, gives me the following error:

query = Query(ip,port)

TypeError: ‘module’ object is not callable

I’ve basically changed the location of ip and port, moved it out of the serverinfo command.

    from util import Query
from util import hook

ip = "78.129.221.58"
port = 7777
query = Query(ip,port)
info = query.GetInformation()

@hook.command
def serverinfo(inp,ip="",port="",say=None):
    say(info)
if info['players'] <= 100:
    say(query.GetPlayers())
    say(query.GetDetailedPlayers())
else: say('can\' get players because players are above 100')
say(query.Ping())
query.Close()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T23:09:59+00:00Added an answer on June 16, 2026 at 11:09 pm

    Try adding this to the start of the python code :

    ip = "78.129.221.58"
    port = 7777
    

    Try

    query = Query.Query(ip, port)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to make a poker game bot for IRC, but I can't
i been trying to code an IRC bot, while i have succeed. I am
I have an IRC bot and I'm parsing data, but to make it refresh
I have been figuring about and trying to make my IRC bot send private
I have an IRC bot which hosts game servers given a few arguments. The
I have an IRC bot and I'm trying to create a new thread to
I have a python IRC bot that I'm starting to work on to practice
For our company I'd like to have a Python based IRC bot which checks
I'm trying to run an irc bot with cakephp. My problem is referencing the
I used to have a working IRC bot, which I affectionately named shakybot, for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.