Skip to content

Instantly share code, notes, and snippets.

@VerySweetBread
Last active January 10, 2023 14:55
Show Gist options
  • Save VerySweetBread/2ef1c251465b91df562afe6bc54c62a2 to your computer and use it in GitHub Desktop.
Save VerySweetBread/2ef1c251465b91df562afe6bc54c62a2 to your computer and use it in GitHub Desktop.
import asyncio
import requests
from random import choice
from string import digits, ascii_lowercase
from colorama import init, Fore, Style
init()
class Error(Exception): pass
class Bot():
def __init__(self, nick):
self.url = "http://webchat.ircnet.net/"
headers = {"UserAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/86.0.4240.183 Safari/537.36"}
self.r = "".join([choice(digits + ascii_lowercase) for _ in range(32)])
self.session = requests.Session()
self.session.headers = headers
self.nick = nick
self.loop = asyncio.get_event_loop()
data = self.session.post(self.url + f"e/n?r={self.r}", data={"nick": self.nick}).json() # Отправка ника
print(data, self.nick)
if data[0]:
self.s = data[1] # Получение ключа сиссии
if not self.dosmth(self.session.post(self.url + f"e/s?r={self.r}",
data={"s": self.s}).json()):
raise Error("Something went wrong")
else:
print(Fore.GREEN)
else:
print(Fore.RED)
print(data[0])
print(Style.RESET_ALL)
def command(self, cmd):
data = self.session.post(self.url + f"e/p?r={self.r}",
data={"s": self.s, "c": cmd}).json()
if data[0]:
print(Fore.GREEN)
else:
print(Fore.RED)
print(cmd, "\t", data[1])
print(Style.RESET_ALL)
def join(self, channel):
self.command(f"JOIN #{channel}")
def send(self, channel, message):
self.command(f"PRIVMSG #{channel} :{message}")
def commands(self, *args, **kwargs):
if args[0] in ("выход", "выйти", "exit"):
try:
self.command(f"QUIT :{' '.join(args[1:])}")
except:
self.command(f"QUIT :")
def main_loop(self):
while True:
data = self.session.post(self.url + f"e/s?r={self.r}",
data={"s": self.s}).json()
self.dosmth(data)
def dosmth(self, data):
if data:
if type(data[0]) is bool: # Если есть код выполнения команды
print(Fore.GREEN if data[0] else Fore.RED)
print(data[1])
print(Style.RESET_ALL)
if not data[0]: return
elif type(data[0]) is list:
for mes in data:
if mes[0] == 'c':
if mes[1] == "MODE": # Оповещение о подключении (или о подтверждении подключения, я ХЗ)
self.command(f"MODE {self.nick} +x")
print(f"MODE {self.nick} +x")
if mes[1] == "NOTICE": # Системные сообщения
print(Fore.YELLOW + '\t'.join(mes[3]) + Style.RESET_ALL)
return True
elif mes[1] == "ERROR": # Ошибки
print(Fore.RED + '\t'.join(mes[3]) + Style.RESET_ALL)
return False
elif mes[1] == "PRIVMSG": # Сообщения
user = mes[2].split('!~')[0]
chnl = mes[3][0]
msg = mes[3][1]
print(f"Сообщение от {user} в канале {chnl}:")
print(msg, "\n")
# МЯУ #
if "мяу" in msg.lower():
self.send(chnl[1:], "Мяу!")
elif "meow" in msg.lower():
self.send(chnl[1:], "Meow!")
if msg.startswith("*"):
self.commands(msg[1:].split(" "))
# МЯУ #
elif mes[1] == "PING": # Пинг
self.command("PONG :" + mes[3][0])
print("Пинг понг!")
elif mes[1].isdigit(): # Цифровые коды
if mes[1] == "020":
self.command(f"MODE {self.nick} +x")
else:
print(Fore.YELLOW + "|" + mes[1] + "|" + '\t'.join(mes[3]) + Style.RESET_ALL)
elif mes[0] == "disconnect": # Сообщение об отключении
print("Бот отключен")
return False
else:
print(mes)
bot = Bot("Koteika_")
bot.join("HowdyChat")
bot.join("python")
bot.send("HowdyChat", "Мяу!")
bot.main_loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment