python: IRC bot involunteer infinity loop - python

I've coded this python irc bot for twitch, and i have an issue:
after an average of 20 minutes without messages to read, the bot send in the prompt an infinity of useless messages, and the bot can't work, even if there is messsages to read (and after a few hours, the hosting machine crash... because of the place that it takes in RAM).
I putted here the code... If you know a way to transform the receiving line to an event or something else...
-*- coding: utf-8 -*-
import socket
import sys
import time
CHANNEL = "#twitch" #actually I tested my bot on an other channel, where I've the admins rights
s = socket.socket()
def connection():
print("connecting...")
s.connect(("irc.chat.twitch.tv", 6667))
print("identifing...")
s.send("PASS " + "oauth:*****************************" + "\r\n") #i censured for evident security reason
s.send("NICK " + "mistercraft" + "\r\n")
print("joining channel " + CHANNEL)
s.send("JOIN " + CHANNEL + "\r\n")
print("Connected")
def send(Message):
s.send("PRIVMSG "+CHANNEL+" :"+ Message + "\r\n")
print("Sent : " + Message)
connection()
send("Hello world !!!")
while 1:
text = ""
recu = s.recv(2040) #receive line, where there is the issue
if len(recu.split(":")) >= 3:
user = recu.split("!")[0]
user = user.split(":")[1]
for i in range(2, len(recu.split(":")), 1):
text = text + recu.split(":")[i] + ":"
print(user+" : "+text)
#Code here (like 'if' loops)
Thanks for help.

I found myself how to stop the issue : after the splitting lines, add elif "PING" in recu:
s.send("PONG :" + recu.split(":")[1]) the issue was that the bot don't respond to the ping from twitch, so twitch kicked him...

Related

I get Winerror 10038 when i try to reconnect

Twitch randomly disconnects my python bot. I googled a lot and found out that this is a common problem. Only solution seems to be an automated reconnect. Tried this, but my knowledge seems to be way too limited to make it work.
I tried to shut down the socket, close it and then use the same routine to connect that i initially use to connect. Tried a few variations, but nothing worked i Always get the Error-Code: "Winerror 10038" when i try to re-connect
import socket
import sys
import modules.cfg
import time
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
irc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
def connect():
'''
Connection to Twitch IRC using cfg.py
'''
irc.connect((modules.cfg.HOST,modules.cfg.PORT))
irc.send(str("PASS " + modules.cfg.PASS + "\r\n").encode("utf-8"))
irc.send(str("NICK " + modules.cfg.NICK + "\r\n").encode("utf-8"))
irc.send(str("JOIN " + modules.cfg.CHAN + "\r\n").encode("utf-8"))
irc.send(str("CAP REQ :twitch.tv/commands\r\n").encode("utf-8")) #whisper enable
irc.send(str("CAP REQ :twitch.tv/membership\r\n").encode("utf-8"))
def read_chat():
response = irc.recv(4096).decode('utf-8') #receive text
if response == "PING :tmi.twitch.tv\r\n":
print("Ping received")
irc.send("PONG :tmi.twitch.tv\r\n".encode("utf-8"))
return response
def send(msg):
try:
irc.send("PRIVMSG {} : {}\r\n".format(modules.cfg.CHAN, msg).encode("utf-8"))
except:
irc.shutdown(socket.SHUT_RDWR)
irc.close()
print("\n\nDisconnected\n")
time.sleep(10)
connect()
print("Reconnected\n\n")
I am pretty new to coding and it´s some kind of a hobby of mine. Hopefully someone can help me! Thank you guys
Thx to user207421 i finally found the way.. for me it is a bit strange, but it works.
def re_connect():
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
irc.connect((modules.cfg.HOST,modules.cfg.PORT))
irc.send(str("PASS " + modules.cfg.PASS + "\r\n").encode("utf-8"))
irc.send(str("NICK " + modules.cfg.NICK + "\r\n").encode("utf-8"))
irc.send(str("JOIN " + modules.cfg.CHAN + "\r\n").encode("utf-8"))
irc.send(str("CAP REQ :twitch.tv/commands\r\n").encode("utf-8")) #whisper enable
irc.send(str("CAP REQ :twitch.tv/membership\r\n").encode("utf-8"))
def send(msg):
try:
irc.send("PRIVMSG {} : {}\r\n".format(modules.cfg.CHAN, msg).encode("utf-8"))
except:
irc.shutdown(socket.SHUT_RDWR)
irc.close()
print("\n\nDisconnected\n")
time.sleep(10)
re_connect()
print("Reconnected\n\n")

IRC bot written in Python - join channel function

I'm trying to add to my python IRC bot a function that when I type "join #channel-name" on IRC, the bot will join the channel.
Here's my code:
# IRC bot written by syrius
import socket
server = "irc.freenode.net" # IRC server
channel = "#syrius-test" # Channel
botnick = "syrius-bot" # Nickname of the bot
master = "syrius_" # Nickname of the bot's master
exitcode = "bye " + botnick #Text that we will use to make the bot quit
def ircwrite(message):
global ircsock
ircsock.send(str(message).encode('latin-1', 'ignore'))
def ping():
ircwrite("PONG :pingis\n")
def sendmsg(chan , msg):
ircwrite("PRIVMSG "+ chan +" :"+ msg +"\n")
def joinchan(channel):
ircsock.send(bytes("JOIN "+ channel + "\n"))
def join():
ircsock.send(bytes("JOIN %s"))
def hello():
ircwrite("PRIVMSG "+ channel +" :Hello!\n")
def quitting():
ircwrite("PRIVMSG "+ channel +" :Okay boss, leaving now.\n")
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ircsock.connect((server, 6667))
ircwrite("USER "+ botnick +" "+ botnick +" "+ botnick +" :IRC bot coded by syrius.\n")
ircwrite("NICK "+ botnick +"\n")
joinchan(channel)
while 1:
ircmsg = ircsock.recv(2048).decode() # receive data from the server
ircmsg = ircmsg.strip('\n\r') # removing any unnecessary linebreaks.
print(ircmsg) # Here we print what's coming from the server
name = ircmsg.split('!',1)[0][1:] # We split out the name
if ircmsg.find(":Hello "+ botnick) != -1: # If we can find "Hello Mybot" it will call the function hello()
hello()
if ircmsg.find("PING :") != -1: # if the server pings us then we've got to respond!
ping()
if name.lower() == master.lower() and ircmsg.find(":quit " + botnick) != -1:
quitting()
ircsock.send(bytes("QUIT \n", "UTF-8"))
if name.lower() == master.lower() and ircmsg.find(":join %s") != -1:
join()
main()
Of course the following code is incorrect :
line23:
def join():
ircsock.send(bytes("JOIN %s"))
line56:
if name.lower() == master.lower() and ircmsg.find(":join %s") != -1:
join()
I would like to know what should I put there so the bot can join the channel.
Any help would be very appreciated.
I see a couple of problems with this solution, my recommendation is you should probably try to use an IRC framework such as Pydle or one of the billion others that already handle protocol.
The first issue I see is using latin-1 for your encoding, generally you should be using utf-8, you could also be using whatever the server advertises in the CHARSET from the RPL_ISUPPORT reply, though that is not common anymore. Along the lines of encoding also, you could decode the IRC lines from utf-8 so you can deal with strings instead of bytes, and just reencode it at the output.
The next IRC issue would be your line ends, IRC messages should always end with CR-LS (Carriage Return-Line Feed) which would be the \r\n characters instead of just the \n.
The last thing I felt like mentioning was your string formatting, you should be using "JOIN {}".format(channel) which is the preferred string formatting method these days, unless you're using python 3.7 then you would use fstrings which are similar.
With the way you are trying to do the formatting now, you are doing it through both concatenation (eg "USER" + channel) but you are also trying to use old style string formatting through %s. If you wanted to use %s formatting the correct way would be "JOIN %s" % (channel), though these days using .format and fstrings are the preferred method.
You are missing argument. ircsock.send(bytes("JOIN %s")).
It should be like this:
ircsock.send(bytes("JOIN %s \r\n") % (channel))

IRC Bot for Minecraft is not working well

Tried to make an IRC Bot for a Minecraft server called "ORE" (Open Redstone).
Btw it is Python.
But it doesn't output anything, just empty line.
Here is the code:
import sys
import socket
import string
import time
from time import gmtime, strftime, sleep
import math
import re
from string import ascii_letters
from sys import argv
def main():
operators = ["FreeProGamer"]
server = "irc.openredstone.org"
channel = "#openredstone"
botnick = "FPGBot"
readbuffer = ""
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #defines the socket
print ("connecting to: "+server)
irc.connect((server, 6667)) #connects to the server
irc.send("PASS password\r\n")
irc.send("USER "+ botnick +" "+ botnick +" "+ botnick + " :FPGBot\n") #user authentication
irc.send("NICK "+ botnick +"\n") #sets nick
sleep(5)
irc.send("JOIN " + channel + "\r\n") #join the chan
irc.send("PRIVMSG OREBuild :/msg Jan1902 Hello!\r\n");
So I hope you will find a way to fix it. Oh and how do I check if a player is typing for example ;help and stuff? I wanna make this bot a bit more than a passive one :P
Problem is with this line:
irc.send("PRIVMSG OREBuild :/msg Jan1902 Hello!\r\n");
BTW you better use some package for IRC like FrozenIdea or bosnobot.
The answer is:
As I am new to Python, I forgot to add main() at the end of the code, which calls the function main.

"NO IDENT RESPONSE" error when connecting to an IRC server

Usually I would attempt something like this with the twisted library, but that isn't available for python 3 - so I attempted it using the sockets library. The code does establish a connection, but the server quickly responds with "no ident response". I don't have much network programming experience, so I would appreciate it if someone could point out the error I'm making here. I'm also quite aware that there are functions/other code that aren't used, or that Ive used inconsistently. I just thought I would paste the entirety of my code here in case its relevant.
import socket
server = "irc.freenode.net"
channel = "put channel here"
nickname = "put nickname here"
def encode(text):
return text.encode("ascii")
def ping():
irc_socket.send(encode("PONG :pingis\n"))
def send_message(chan, msg):
irc_socket.send(encode("PRIVMSG " + chan + " :" + msg + "\n"))
def join_channel(chan):
irc_socket.send(encode("JOIN " + chan + "\n"))
def login(username='user', realname='Pythonist', hostname='Helena', servername='Server'):
irc_socket.send(encode("USER %s %s %s %s" % (username, hostname, servername, realname)))
irc_socket.send(encode("NICK " + nickname))
irc_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
irc_socket.connect((server, 6667))
login()
join_channel(channel)
while True:
buffer = irc_socket.recv(1024)
msg = str.split(str(buffer))
if msg[0] == "PING":
irc_socket.send("PONG %s" % msg[1])
print(msg)
The code was originally from: http://wiki.shellium.org/w/Writing_an_IRC_bot_in_Python and Ive made minor changes.
Teensy tiny little problem that’s causing all the trouble: you’re missing newlines in login. "USER %s %s %s %s" should be "USER %s %s %s %s\n" and "NICK " + nickname should be "NICK " + nickname + "\n". The server looks up your ident and doesn’t find it, and the next step after that is for you to register, but you never send it a complete line, so it keeps waiting… and waiting…
Ident is UNIX service which nobody has been using for twenty years or so.
It was used to identify the remote user when doing terminal server to remote server connections in terminal applications. Ident is no way safe for modern internet, so nobody is using it anymore.
So you have hit the ghost of the past. Also, as mentioned in the above answer, if you send correct NICK command the IRC server is happy with your client.

How do i program a simple IRC bot in python?

I need help writing a basic IRC bot that just connects to a channel.. is anyone able to explain me this? I have managed to get it to connect to the IRC server but i am unable to join a channel and log on. The code i have thus far is:
import sockethost = 'irc.freenode.org'
port = 6667
join_sock = socket.socket()
join_sock.connect((host, port))
<code here>
Any help would be greatly appreciated.
To connect to an IRC channel, you must send certain IRC protocol specific commands to the IRC server before you can do it.
When you connect to the server you must wait until the server has sent all data (MOTD and whatnot), then you must send the PASS command.
PASS <some_secret_password>
What follows is the NICK command.
NICK <username>
Then you must send the USER command.
USER <username> <hostname> <servername> :<realname>
Both are mandatory.
Then you're likely to see the PING message from server, you must reply to the server with PONG command every time the server sends PING message to you. The server might ask for PONG between NICK and USER commands too.
PING :12345678
Reply with the exact same text after "PING" with PONG command:
PONG :12345678
What's after PING is unique to every server I believe so make sure you reply with the value that the server sent you.
Now you can join a channel with JOIN command:
JOIN <#channel>
Now you can send messages to channels and users with PRIVMSG command:
PRIVMSG <#channel>|<nick> :<message>
Quit with
QUIT :<optional_quit_msg>
Experiment with Telnet! Start with
telnet irc.example.com 6667
See the IRC RFC for more commands and options.
Hope this helps!
I used this as the MAIN IRC code:
import socket
import sys
server = "server" #settings
channel = "#channel"
botnick = "botname"
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #defines the socket
print "connecting to:"+server
irc.connect((server, 6667)) #connects to the server
irc.send("USER "+ botnick +" "+ botnick +" "+ botnick +" :This is a fun bot!\n") #user authentication
irc.send("NICK "+ botnick +"\n") #sets nick
irc.send("PRIVMSG nickserv :iNOOPE\r\n") #auth
irc.send("JOIN "+ channel +"\n") #join the chan
while 1: #puts it in a loop
text=irc.recv(2040) #receive the text
print text #print text to console
if text.find('PING') != -1: #check if 'PING' is found
irc.send('PONG ' + text.split() [1] + '\r\n') #returnes 'PONG' back to the server (prevents pinging out!)
Then, you can start setting commands like: !hi <nick>
if text.find(':!hi') !=-1: #you can change !hi to whatever you want
t = text.split(':!hi') #you can change t and to :)
to = t[1].strip() #this code is for getting the first word after !hi
irc.send('PRIVMSG '+channel+' :Hello '+str(to)+'! \r\n')
Note that all irc.send texts must start with PRIVMSG or NOTICE + channel/user and the text should start with a : !
It'd probably be easiest to base it on twisted's implementation of the IRC protocol. Take a look at : http://github.com/brosner/bosnobot for inspiration.
This is an extension of MichaelvdNet's Post, which supports a few additional things:
Uses SSL wrapper for socket
Uses server password authentication
Uses nickserv password authentication
Uses nonblocking sockets, to allow other events to trigger
Logs changes to text files to channel
#!/usr/local/bin/python
import socket
import ssl
import time
## Settings
### IRC
server = "chat.freenode.net"
port = 6697
channel = "#meLon"
botnick = "meLon-Test"
password = "YOURPASSWORD"
### Tail
tail_files = [
'/tmp/file-to-tail.txt'
]
irc_C = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #defines the socket
irc = ssl.wrap_socket(irc_C)
print "Establishing connection to [%s]" % (server)
# Connect
irc.connect((server, port))
irc.setblocking(False)
irc.send("PASS %s\n" % (password))
irc.send("USER "+ botnick +" "+ botnick +" "+ botnick +" :meLon-Test\n")
irc.send("NICK "+ botnick +"\n")
irc.send("PRIVMSG nickserv :identify %s %s\r\n" % (botnick, password))
irc.send("JOIN "+ channel +"\n")
tail_line = []
for i, tail in enumerate(tail_files):
tail_line.append('')
while True:
time.sleep(2)
# Tail Files
for i, tail in enumerate(tail_files):
try:
f = open(tail, 'r')
line = f.readlines()[-1]
f.close()
if tail_line[i] != line:
tail_line[i] = line
irc.send("PRIVMSG %s :%s" % (channel, line))
except Exception as e:
print "Error with file %s" % (tail)
print e
try:
text=irc.recv(2040)
print text
# Prevent Timeout
if text.find('PING') != -1:
irc.send('PONG ' + text.split() [1] + '\r\n')
except Exception:
continue
That will open a socket, but you also need to tell the IRCd who you are. I've done something similar in perl ages ago, and I found the IRC RFCs to be very helpful.
Main RFC: http://irchelp.org/irchelp/rfc/rfc.html
Other RFCs: http://irchelp.org/irchelp/rfc/index.html

Categories

Resources