Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. import os
  2. import random
  3. import socket
  4. import sys
  5.  
  6. class IRC:
  7.  
  8.     irc = socket.socket()
  9.  
  10.     def __init__(self):  
  11.        
  12.         self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  13.        
  14.     def send_raw_text(self, msg):
  15.         self.irc.send(msg.encode())
  16.        
  17.     def send(self, chan, msg):
  18.         self.irc.send_raw_text("PRIVMSG " + chan + " " + msg + "n")
  19.  
  20.     def connect(self, server, channel, botnick):
  21.         #defines the socket
  22.         print ("connecting to:"+server)
  23.         self.irc.connect((server, 6667))                                                         #connects to the server
  24.         self.send_raw_text("USER " + botnick + " " + botnick +" " + botnick + " :This is a fun bot!n") #user authentication
  25.         self.send_raw_text("NICK " + botnick + "n")              
  26.  
  27.     def get_text(self):
  28.         text=self.irc.recv(2040)  #receive the text
  29.  
  30.  
  31.         if text.find('PING'.encode()) != -1:                      
  32.             self.irc.send_raw_text('PONG ' + text.split() [1] + 'rn')
  33.  
  34.         return text
  35.  
  36.  
  37.  
  38.  
  39. ## IRC Config
  40. channel = "#python"
  41. server = "irc.freenode.net"
  42. nickname = "bender"
  43.  
  44. irc = IRC()
  45. irc.connect(server, channel, nickname)
  46.  
  47.  
  48. while 1:
  49.     text = irc.get_text()
  50.     print (text)
  51.  
  52.     if "PRIVMSG".encode() in text and channel in text and "hello".encode() in text:
  53.         irc.send(channel, "Hello!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement