IRC Python Stats Bot Script! v0.3

By OrFeAsGr on Jun 23, 2016

Hi again! After some months i corrected some mistakes in this code and added a few things to make it act like a typical IRC Client.
It will now use an alternate nick when the nick is already in use.
It will now attempt to join the channel correctly and not attempt before successfull registration.
It will now send a small adv message when it successfully joins the channel!
Change INPUT_CHAN_HERE with the channel you want!
Change INPUT_SERVER_HERE with the server you want!
v0.3 01 Oct 2016

Check my mIRC Scripts! to earn me some cents by watching an ad by

Or do you want to donate some bucks because you like my scripts??? Visit
Thx! ^_^

### ###
### StatsPy Python Script v0.3 ###
### OrFeAsGr ###
import sys
import socket
import string
import time

# Don't remove the " " #
PORT = 6667

NICK = "StatsPy"
IDENT = "Stats"

readbuffer = ""
words = 0
letters = 0
joins = 0
parts = 0
modes = 0
statsdelay = 0
altnicks = 0

s=socket.socket( )
s.connect((HOST, PORT))

s.send(bytes("NICK %s\r\n" % NICK, "UTF-8"))
s.send(bytes("USER %s %s bla :%s\r\n" % (IDENT, HOST, REALNAME), "UTF-8"))

while 1:
    readbuffer = readbuffer+s.recv(1024).decode("UTF-8")
    temp = str.split(readbuffer, "\n")
    readbuffer=temp.pop( )

    for line in temp:
        line = str.rstrip(line)
        line = str.split(line)
        # If the nick is already used inc altnicks by 1 and retry with nick combined with altnicks (as a string) # 
        if(line[1] == "433"):
            altnicks = altnicks + 1
            s.send(bytes("NICK %s\r\n" % (NICK + str(altnicks)) , "UTF-8"))
            # Send a small adv message when the End of names raw is sent. That way it knows it has successfully joined the channel.
        if(line[1] == "366"):
            if(line[3] == CHAN):
                 s.send(bytes("PRIVMSG %s :Hi! I'm here to keep the statistics of %s ! If you want to view them send !stats (StatsPy Python Script by OrFeAsGr\r\n" % (CHAN, CHAN), "UTF-8"))
            #send the join command only when it has completed registration (which it understands from the 001 raw event, the classic: Welcome to <server> server <nick> #
        if(line[1] == "001"):
            s.send(bytes("JOIN %s\r\n" % CHAN, "UTF-8"))
        if(len(line) > 2):
            if(line[2].strip(":") == CHAN):
                if(line[1] == "JOIN"):
                    joins = joins + 1
                elif(line[1] == "PART"):
                    parts = parts + 1
                elif(line[1] == "MODE"):
                    modes = modes + 1
        if(line[0] == "PING"):
            s.send(bytes("PONG %s\r\n" % line[1], "UTF-8"))
        if(line[1] == "PRIVMSG"):
            sender = ""
            for char in line[0]:
                if(char == "!"):
                if(char != ":"):
                    sender += char 
            size = len(line)
            i = 3
            message = ""
            while(i < size): 
                message += line[i] + " "
                i = i + 1
            if(size > 2):
                if(line[2] == CHAN):
                    words = words + len(line[3:])
                    letters = letters + len(message)
                if(line[3] == ":!stats"):
                    s.send(bytes("PRIVMSG %s :%s > Stats: Words: %s Letters: %s Joins: %s Parts: %s Modes: %s\r\n" % (line[2], line[2], words, letters, joins, parts, modes), "UTF-8"))


Sign in to comment.
Mi9   -  Oct 02, 2016

what #hellas do in line 78 ?

Add options to reply to CTCP version, ping, time :)

OrFeAsGr  -  Oct 03, 2016

hey :D sorry i was testing it on my channel and forgot to change xD please change it to CHAN
Also thanks xD i actually wanted to add that but forgot! I'll do it later!

Mi9  -  Oct 04, 2016

It's possible to make the script to run from mIRC for active channel? Something like this /run $chan
I hope you understend what i'm try to say, my english is very bad :D

OrFeAsGr  -  Oct 06, 2016

Hi again! So.. i'm not sure about this but this could work provided that you haven't changed any of the python scripts lines. A better way to do this is to write the channel and network in a .txt and then reading it with python to get the values. But this kinda worked for me.
do !py server channel
replace server with the server you want and channel with the channel you want!
Good luck!

ON *:TEXT:*:#: {
  if ($1 == !py) {
    write -l10 <script path here>  HOST = $+(",$strip($2),")
    write -l11 <script path here> CHAN = $+(",$strip($3),")
    run py <script path here>
Sign in to comment

Mi9   -  Sep 04, 2016

NOTICE AUTH : Looking up your hostname
Checking Ident
PING :2581793672
Traceback (most recent call last):
File "C:\Downloads\", line 41, in
if(line[2].strip(":") == CHAN):
IndexError: list index out of range

is becose i use python-3.5.2 64bit ?

OrFeAsGr  -  Sep 04, 2016

Not sure..
I'm kinda amateur on Python but i will check it

OrFeAsGr  -  Oct 01, 2016

Hey Mi9 ! Just wanted to let you know that i fixed that bug and added some things so it works better!
Check it out if you're still intrested!

Sign in to comment

dma   -  Jun 23, 2016

this is good thanks pal

dma  -  Jun 23, 2016

wait how do you get it to work LOL

OrFeAsGr  -  Jun 23, 2016

Install Python and run the script or install py2exe and make an exe for the script. - Python Website - py2exe website

dma  -  Jun 23, 2016

ok will do

Sign in to comment

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.