[Py] IRC Bot - RMB Scripting
You appear not have have an account with us, Click HERE to register an account...
RMB Scripting
Navigation
  Home
  Contact
  FAQ
  Forum
  Topsites
  Members
  Affiliates
  Arcade <-- New!
  IoxHost
Downloads
  Templates
  Scripts
  Fonts
  Image Sets
  Software
  Other
 
Tutorials
  .htaccess
  Photoshop
  C/C++
  CSS
  Macromedia Flash
  MySQL
  PHP
  PHP - User System
  HTML
  Javascript
  Macintosh
  Macromedia Fireworks
  Visual Basic
  Visual C#
  Windows
  Python
  Other
 
Latest Tutorials
All PHP / Apache Funct...
Block User\'s Direct A...
Generate a salt
Image Tag Tracking
Cpanel/webmail Login
Latest Comments
Friendlist/Blocklist +...
Friendlist/Blocklist +...
Friendlist/Blocklist +...
Steal msn picture
Simple and minimalisti...
Stats
Total Members: 562 [216]
Total Tutorials: 269
Newsest User: Stf
Todays Unique Hits: 48
Users Online: 0
 
5 Guests
Poll
Should we change our name?
Hmmmm, Yes!!
Maybe
Depends on what to
NO!
Main Content
Tutorial: [Py] IRC Bot By ShadowMage
I do not have any modifications. It is what it is. I created it real quick like to test sockets and the url library.

Lets begin.

First off we will want to import all of our libraries for various bot things.
   
CODE:
import socket, string, datetime, sys, os, platform, urllib


We use the socket, string, datetime, sys, os, platform, and urllib libraries for getting bot status, current date, connecting to the mIRC server, modifying strings, and other various items.

Now that our libraries are loaded, we can continue on with some variables.
   
CODE:
SERVER = 'irc.freenode.net' #the server to connect to
PORT = 6667 #the server port
NICKNAME = 'YourBotNick' #bot nickname
CHANNEL = '#YourChannel' #channel to join on connect
BOTPASS = 'BotPass' #the password to use to identify
IRC = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #the mIRC server socket.


This is all the variables used, there are more. but they are defined within the script. These ones are the basic information needed to connect to a server.

Continuing on now we have some application functions to connect, login, send data and a few other various things.

   
CODE:
def irc_conn(): #function to connect to the mIRC Server
IRC.connect((SERVER, PORT)) #Begin connect
print "Attempting to connect to " + SERVER #tell the user in the script that they are connecting.
def sendData(command): #Function to send data to server
IRC.send(command + '\n') #send the data "command" to the server
def joinChannel(channel): #tell bot to join channel
sendData("JOIN %s" % channel) #joins the channel
def login(nickname, username='YourBot', password = None, realname='Y0urB0t', hostname='localhost', servername='Server Name'): #tells the bot to connect to the server/login
sendData("USER %s %s %s %s" % (username, hostname, servername, realname)) #set all the user data
sendData("NICK " + nickname) #set nickname
sendData("PRIVMSG NickServ :ID " + BOTPASS + "") #automatically identify
def commands(SendTo): #commands listing command
sendData("PRIVMSG " + SendTo + " :@Help - Display this screen again") #help command
sendData("PRIVMSG " + SendTo + " :@datetime - Tells the current date and time") #gets the date and time
sendData("PRIVMSG " + SendTo + " :@botStats - Displays bot statistics") #send the user bot stats.


All these commands are required to log into the server, join channels, and send data to the server.

And now, for the final bulk of data.
   
CODE:
login(NICKNAME) #login with only the nickname given. All other data is set to defaults.
joinChannel(CHANNEL) #join the default channel
sendData("PRIVMSG " + CHANNEL + " :Bot online. For bot statistics please see @botStats. If you need any help you may private message me @Help") #message the channel that the bot is online.
while (1): #all while the script is running
buffer = IRC.recv(1024) #set the buffer to the received contents
msg = string.split(buffer) #split the string from buffer
message = ' '.join(msg[3:]) #set the message data
senderData = msg[0].split("!") #split the message for sender data
senderData = senderData[0].replace(":", "") #replace colons with nothing but a void
if msg[0] == "PING": #server sent a ping
sendData("PONG %s" % msg[1]) #lets reply with pong!
if msg[1] == 'JOIN' and senderData != NICKNAME: #someone joins, and its not the bot
print senderData + " has joined " + CHANNEL #print on the script screen that someone joined
sendData("PRIVMSG " + senderData + " :Welcome to " + CHANNEL + ", " + senderData + " If you need any help you may say @Help at any time.") #welcome the user in private message
sendData("PRIVMSG " + CHANNEL + " :Welcome, " + senderData + ".") #welcome the user in the channel itself
if msg[1] == 'PRIVMSG' and msg[2] == NICKNAME: #private message commands and handles if its the bot.
if "@Help" in message: #someone needs help
print senderData + " requested help." #echo locally that someone needed help
commands(senderData) #send them the commands list from earlier
if msg[1] == 'PRIVMSG' and msg[2] != NICKNAME: #some more PM commands
if "@Help" in message: #someone else requested help
print senderData + " requested help." #print locally
commands(senderData) #send commands listing
if "@datetime" in message: #date time requested
print senderData + " requested date and time." #print locally
now = datetime.datetime.now() #set the now time.
myTime = now.strftime("%A, %B %d, %Y %I:%M %p") #this is the time to send the user
sendData("PRIVMSG " + msg[2] + " :Date/Time: " + myTime) #send them a PM with the date/time
if "@botStats" in message: #someone wanted bot statistics
print senderData + " requested bot stats." #print locally.
botPlatform = sys.platform #the bot's platform
botOs = platform.platform() #the bot's operating system
osInfo = botOs.split("-") #split the info from -'s
sendData("PRIVMSG " + senderData + " :Bot Platform: " + botPlatform) #send the data: bot platform
sendData("PRIVMSG " + senderData + " :Operating System: " + osInfo[0] + " " + osInfo[1] + " Version: " + osInfo[2]) #send operating system name, and version



This concludes the Python IRC Bot tutorial. Enjoy! If you have any questions place them in the thread. If you find errors place them in the thread also.

Note: You must have a blank line at the end of the file to work right.
Difficulty: Easy
Views: 441
Rating:
Comments
There are currently no comments for this tutorial.
Notice:
Remember to post long codes on our pastbin! - (http://rmb.pastebin.com/)
Add Comment
You must be logged in to post a comment.
Good Morning Guest
Username: 
Password: 
Remember Username
Links
RMB Arcade
$2.50 Resellers
Free Domains
Free Games
Affiliates