PDA

View Full Version : Assistance with Python IRC bot



Datweakfreak
May 15th, 2011, 11:26 AM
I'm writing an IRC bot in Python, and wish to implement functionality so that it echoes any message that is written in the channel.

Normally, I could write for specific messages:


if data.find ( 'Hiya' ) != -1:
s.send ( 'PRIVMSG %s :%s\r\n' % (CHANNEL, 'Hiya' ))Now, how do I write a condition for detecting ANY string, not just 'Hiya', and echo it?

Like, if someone posts something in the channel, just re-post it.

Thanks in advance :)

simeon87
May 15th, 2011, 11:38 AM
Why not send the data right away?


s.send ( 'PRIVMSG %s :%s\r\n' % (CHANNEL, data ))

Datweakfreak
May 15th, 2011, 03:12 PM
Still, I'd need a condition, right?

As in, if some data is found, return that data.

Like you said:

if data.find ( 'Hiya' ) != -1:
s.send ( 'PRIVMSG %s :%s\r\n' % (CHANNEL, data))

That would work great, but I need a condition to replace the 'Hiya' with ANY possible string. That's what I'm looking for :)

simeon87
May 15th, 2011, 03:24 PM
Still, I'd need a condition, right?

As in, if some data is found, return that data.

Like you said:

if data.find ( 'Hiya' ) != -1:
s.send ( 'PRIVMSG %s :%s\r\n' % (CHANNEL, data))

That would work great, but I need a condition to replace the 'Hiya' with ANY possible string. That's what I'm looking for :)


if data:
s.send ( 'PRIVMSG %s :%s\r\n' % (CHANNEL, data))

Datweakfreak
May 15th, 2011, 04:01 PM
Tried that, but doesn't work :S

The thing is, I want to be able to get whatever string is found and pass it as a parameter to another function.

Say, if the user types Name, I'd pass it to another function like a string inverter, which would return emaN and the bot would say that.

Something like:


if data.find(anystring):
stringinverter.invert(anystring)But when I write that, it returns an error: name 'anystring' is not defined

I do get what it's trying to say, but how do I tell python that by 'anystring', I mean any string that is found?

simeon87
May 15th, 2011, 04:40 PM
Tried that, but doesn't work :S

The thing is, I want to be able to get whatever string is found and pass it as a parameter to another function.

Say, if the user types Name, I'd pass it to another function like a string inverter, which would return emaN and the bot would say that.

Something like:


if data.find(anystring):
stringinverter.invert(anystring)But when I write that, it returns an error: name 'anystring' is not defined

I do get what it's trying to say, but how do I tell python that by 'anystring', I mean any string that is found?

Why are you searching for something in data? If data is a string then all you need to do is check whether data is not an empty string, which is what I did with
if data:

If you wish to modify data, then you can do:
data = f(data) where f returns a modified string.

Datweakfreak
May 15th, 2011, 05:19 PM
I understand, but if I just use,


if data:the bot does not connect, it doesn't even return an error, it just keeps printing empty lines in the output like in an infinite loop :(

simeon87
May 15th, 2011, 05:21 PM
I understand, but if I just use,


if data:the bot does not connect, it doesn't even return an error, it just keeps printing empty lines in the output like in an infinite loop :(

Then there may be a problem elsewhere in the design of your IRC bot. Basically the idea should be that if you get some data, then do something, otherwise, do nothing.

Datweakfreak
May 15th, 2011, 05:55 PM
Alright, I found a solution.


if data.find("PRIVMSG"):

And then I could just parse out the contents.

Thanks a lot for your time and help! :)