Okay, so yet another post here due to my utter confusion with this language...in all honesty, this isn't a question about threading, per say, lemme just demonstrate what I mean.

This:

class RevoiceThread( threading.Thread ):
	def run (self):
		print "Remember to replace this line with the actual code"

is the code for a thread I want to run, the print line I wish to replace with this(well, I want to replace it with something more complex, but this would be a proof of concept)

time.sleep(10)
self.connection.mode(channel + " +v", vname)

That would be wrong, however, because the variables in there (as well as the whole "mode" function, so the self.connection is wrong too...) are in a separate class (see the full code below). Is there a simple way to implement what I'm trying to do here, or should I try re-thinking how to do this? (I'd honestly prefer to avoid threading, but there isn't really any other way to do this I can think of)

Full source code, for reference:

import ircbot
import time
import irclib
import threading

print "Mehbot v0.0.2 - August 18, 2010"


#network info
network = 'irc.geekshed.net'
port = 6667
channel = '#meh'
nick = 'Tarkbot'
name = 'The Meh'

#making the log file (opening it, rather)
logFile = open ( 'log.txt', 'a' )

#more sketcy work
#class forcepingThread ( threading.Thread ):

class RevoiceThread( threading.Thread ):
	def run (self):
		print "Remember to replace this line with the actual code"
	
#make bot class
class MehBot ( ircbot.SingleServerIRCBot ):
	#join channel after connecting
	def on_welcome ( self, connection, event):
		connection.join ( channel )
	
	#these are reactions to text in the channel the bot is in.
	def on_pubmsg ( self, connection, event ):
		#log messages from channel
		source = event.source().split ( '!' ) [ 0 ]
		text = event.arguments() [ 0 ]
		logFile.write ( "[" + time.strftime("%m/%d/%Y %H:%M", time.gmtime()) + "] " + source + ": " + text + "\n" )
		#temp statement to quit bot
		if event.arguments() [ 0 ] == "die":
			self.die()
		
		#here be sketchyworkythingies. If user is de-voiced, revoices them (need timer) 
	def on_mode ( self, connection, event ):
		if event.arguments() [ 0 ] == "-v":
			vname = event.arguments() [ 1 ]
			self.connection.mode(channel + " +v", vname)
					
#create bot		
bot = MehBot ( [( network, port )], nick, name )
bot.start()

Recommended Answers

All 8 Replies

If you don't mind me asking, what are the "ircbot" and "irclib" modules or packages you imported, and where could we acquire them or view the source?

http://sourceforge.net/projects/python-irclib/files/
That's the library(module...meh), both of the modules are in there. I used version 0.4.8, there is almost no documentation, so you'd have to just look in the actual files to get anything sorted out. In essence, it's just a module that handles all the annoying work with the sockets one would need to do to make something to connect to IRC servers.

(the ircbot module isn't actually really needed for connecting to things, but...I'm lazy and this is easier...)

Mate, I believe that all you need to do, it's to use my example of timer, and use it to call the function to voice, instead of the threading being the voice function, the thread will calll the object voice after that time.

Hope is clear enough.

Cheers and Happy coding.

Oh no, that did indeed worked, but I need to implement threading to handle multiple, simultaneous devoicings, see the bot will be going into a channel that at it's peak, has about 600 people in it, so the possibility of more than one person getting devoiced at the same time was too great.

Python isn't a bad language, even when it comes to threading.

The 'vibe' I'm getting from the modules you are using is that the objects and process of creating and operating a bot are overly complex.

There isn't much that can be done about that except a rewrite with different design principles, philosophies, and use specifications in mind. (I'm working on an irc bot library at the moment. When I publish the source hopefully it will be more suited for your use and for other people.)

But in the meantime we must make do with what we have here.

Seems like to me you need to add your global handler to an event and that should do the trick.

Doesn't the on_mode method you defined in the bot do what you want?

If so when you initiate your bot you should do something like this I think.

self.add_global_handler("mode", self.on_mode)

P.S. I posted after you said you needed to add threads.

A quick hack would be something like this:

def run_async(fnc):
    def newfnc(*args, **kwargs):
        thread = threading.Thread(target=fnc, args = args, kwargs = kwargs)
        thread.start()
    return newfnc

@run_async
def on_mode(self, connection, event):
    if event.arguments() [ 0 ] == "-v":
    vname = event.arguments() [ 1 ]
    self.connection.mode(channel + " +v", vname)

Python isn't a bad language, even when it comes to threading.

The 'vibe' I'm getting from the modules you are using is that the objects and process of creating and operating a bot are overly complex.

There isn't much that can be done about that except a rewrite with different design principles, philosophies, and use specifications in mind. (I'm working on an irc bot library at the moment. When I publish the source hopefully it will be more suited for your use and for other people.)

But in the meantime we must make do with what we have here.

Seems like to me you need to add your global handler to an event and that should do the trick.

Doesn't the on_mode method you defined in the bot do what you want?

If so when you initiate your bot you should do something like this I think.

self.add_global_handler("mode", self.on_mode)

P.S. I posted after you said you needed to add threads.

Like I said, I used the Ircbot module because I was lazy, it handles most of the "generic" def and handlers.

Finally got time to work on the bot again, and this did indeed work, it's definitely not gonna be a permanent solution, but it will work until I can find a better way. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.