You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2.7 KiB

IRC

In [ ]:
import socket
import sys

# Adapted from https://pythonspot.com/building-an-irc-bot/

class IRC:

    irc = socket.socket()
  
    def __init__(self):  
        self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    def send(self, chan, msg):
        self.irc.send(f"PRIVMSG {chan} {msg}\n".encode())

    def connect(self, server, channel, botnick):
        #defines the socket
        print ("connecting to:"+server)
        self.irc.connect((server, 6667))                                                         #connects to the server
        self.irc.send(f"USER {botnick} {botnick} {botnick} :This is a fun bot!\n".encode()) #user authentication
        self.irc.send(f"NICK {botnick}\n".encode())               
        self.irc.send(f"JOIN {channel}\n".encode())        #join the chan

    def get_text(self):
        text=self.irc.recv(2040).decode()  #receive the text

        if text.find('PING') != -1:                      
            self.irc.send('PONG {text.split()[1]}\r\n'.encode()) 

        return text
In [ ]:
import os
import random

channel = "##xpub"
server = "irc.freenode.net"
nickname = "xpubot"

irc = IRC()
irc.connect(server, channel, nickname)


while 1:
    text = irc.get_text()
    print (text)
 
    if "PRIVMSG" in text and channel in text and "hello" in text:
        irc.send(channel, "Hello!")
In [ ]: