Last active
March 10, 2021 18:26
-
-
Save matheusfillipe/689c3146a91af89d9dc2fdb907394091 to your computer and use it in GitHub Desktop.
A Quick and simple Irc Bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket, time, re | |
# Not supposed to look pretty, supposed to look short and ugly | |
send_ = lambda client, channel, m: client.send(f"PRIVMSG {channel} :{m}\r\n".encode()) #sends a private message to the channel or user PM if you pass a nickname | |
raw_send_ = lambda client, m: client.send(f"{m}\r\n".encode()) #Use this to implrement more methods: https://tools.ietf.org/html/rfc1459 | |
#This is a work in progress... pass in a number like 3 or 4 and send some messages | |
recv_ = lambda client, n: list(map(lambda g: [g.group(1), g.group(2)], filter(lambda m: m, map(lambda m: re.match(r'^.*PRIVMSG (.*) :(.*)\r$', m), filter(lambda m: "PRIVMSG" in m, [client.recv(1024).decode('utf-8') for i in range(n)]))))) | |
# Why not create a class? Because it's not funny! | |
def Bot(host, port=6667, name="botty", channel="#lobby"): | |
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
client.connect((host, port)) | |
#We will keep username a nickname the same and not really care about password | |
client.send(f"NICK {name}\r\n \ | |
USER {name} 0 * :{name}\r\n \ | |
JOIN {channel}\r\n \ | |
PRIVMSG NickServ :set always-on true\r\n".encode()) | |
time.sleep(1) #Good to do, not necessary | |
return client, lambda m: send_(client, channel, m), lambda m: raw_send_(client, m), lambda m=1: recv_(client, m) | |
# Sample usage | |
bot, msg, raw, recv = Bot("irc.dot.org.es", 6667, "botty", "#bots") | |
msg("Sending message") | |
print(recv(3)) | |
raw("PRIVMSG handyc :https://gist.github.com/matheusfillipe/689c3146a91af89d9dc2fdb907394091") | |
## BOT ALMOST ONE LINER VERSION (that runs on python REPL bot) | |
""" | |
! import socket, time, re; send_ = lambda client, channel, m: client.send(f"PRIVMSG {channel} :{m}\r\n".encode()); raw_send = lambda client, m: client.send(f"{m}\r\n".encode());recv_ = lambda client, n: list(map(lambda g: [g.group(1), g.group(2)], filter(lambda m: m, map(lambda m: re.match(r'^.*PRIVMSG (.*) :(.*)\r$', m), filter(lambda m: "PRIVMSG" in m, [client.recv(1024).decode('utf-8') for i in range(n)]))))) | |
! def Bot(name, channel): client = socket.socket(socket.AF_INET, socket.SOCK_STREAM);client.connect(("irc.dot.org.es", 6667));client.send(f"NICK {name}\r\nUSER {name} 0 * :+name+\r\nJOIN {channel}\r\nPRIVMSG {channel} :I am alive\r\nPRIVMSG NickServ :set always-on true\r\n".encode());time.sleep(1);return client, lambda m: send_(client, channel, m), lambda m: raw_send(client, m), lambda m=1: recv_(client, m) | |
! bot, msg, raw, recv = Bot("botty", "#bots") | |
! while not "stop" in (m:=list(map(lambda m: m[1],recv()))): [msg(ms) for ms in m] #echo bot | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment