Okay, so in all honesty, I began learning Python about 2 days ago, so this is a rather noobish question(well, I've programmed in other languages, so it's not THAT noobish), but I'm making an IRC bot and can't figure out how to make it so that a line of code execute after a set period of time, the block of code in question is this:

#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 ]
			# this doesn't work cause that's not how timer's supposed to be, but I tried it anyways
			# t = threading.Timer(50.0, self.connection.mode(channel + " +v", vname))
			# t.start()
			
			#set the mode
			self.connection.mode(channel + " +v", vname)

As it is right now, the code works, aside from the commented out part. All I need to do is to execute the line that starts with "self.connection.mode" after 5 minutes, as you can see I've tried the timer in the threading library, and failed at that. So, what would be the simplest way to correctly use the "timer" thing in this situation?

Or, if that would be too tedious, what would be the easiest way to make a timer from scratch?

Thanks in advance.


Here is the full code, for reference.

import ircbot
import time
import irclib
import threading

print "Mehbot v0.0.1 - August 12, 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' )

#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 ]
			# t = threading.Timer(50.0, self.connection.mode(channel + " +v", vname))
			# t.start()
			self.connection.mode(channel + " +v", vname) 
			
		else:
			print "meh2"
			
	
#create bot		
bot = MehBot ( [( network, port )], nick, name )
bot.start()

Create function to voice users and run the function using the timer, instead of passing commands to the timer function.

Well, just tried that now...and it didn't work...might just be me not understanding the language...

Hope it helps.

def voice(x):
    print x

v = threading.Timer(50, voice)
v.start()

Cheers and Happy codings

Ah that did work, didn't at first, but I did something stupid; it works now. 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.