import os import socket import ssl import time import openai import re openai.api_key = "get your own!!!!!!" server = "irc.sageru.org" port = 6697 channel = "#jp" botnick = "chatgpt2" password = "" irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) irc.connect((server, port)) context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) context.check_hostname = False context.verify_mode = ssl.CERT_NONE irc_ssl = context.wrap_socket(irc, server_hostname=server) irc_ssl.send(("USER " + botnick + " " + botnick + " " + botnick + " :I am a bot!\r\n").encode()) irc_ssl.send(("NICK " + botnick + "\r\n").encode()) while True: data = irc_ssl.recv(4096).decode('utf-8') print(data) # print all data received from server to console if data.find("PING") != -1: irc_ssl.send(("PONG " + data.split()[1] + "\r\n").encode()) elif data.find("422") != -1 or data.find("376") != -1: # if no MOTD or end of MOTD irc_ssl.send(("JOIN " + channel + "\r\n").encode()) elif data.find("PRIVMSG") != -1: match = re.search(r'^:(.*)!(.*)@(.*) PRIVMSG (.*) :(.*)', data) if match: user = match.group(1) message = match.group(5) if message.startswith(botnick + ":"): prompt = message.split(botnick + ":")[1] response = openai.Completion.create( engine="text-davinci-003", prompt=prompt, max_tokens=2048, n=1, stop=None, temperature=0.3, ) print(response) if response.choices and response.choices[0].text.strip(): text = response.choices[0].text.strip() lines = (line.strip() for line in text.split("\n") if line.strip()) for i, line in enumerate(lines): if i >= 10: irc_ssl.send(f"PRIVMSG {channel} :\x0312Too many lines! Sorry sosu.\x0F\r\n".encode()) break prefix = "\x0306[ChatGPT]\x0F " if i == 0 else "" irc_ssl.send(f"PRIVMSG {channel} :{prefix}\x0312{line}\x0F\r\n".encode()) time.sleep(1)