Fix exception.

This commit is contained in:
AZ Huang 2013-12-12 00:02:01 +08:00
parent 5c906153e9
commit c2e68b9e40

View file

@ -44,13 +44,12 @@ class SyncBot(Tox):
def ensure_exe(self, func, args): def ensure_exe(self, func, args):
count = 0 count = 0
THRESHOLD = 10 THRESHOLD = 50
while True: while True:
try: try:
return func(*args) return func(*args)
except: except:
print func, count
assert count < THRESHOLD assert count < THRESHOLD
count += 1 count += 1
for i in range(10): for i in range(10):
@ -60,6 +59,7 @@ class SyncBot(Tox):
def loop(self): def loop(self):
checked = False checked = False
self.joined = False self.joined = False
self.request = False
try: try:
while True: while True:
@ -67,7 +67,10 @@ class SyncBot(Tox):
if not checked and status: if not checked and status:
print('Connected to DHT.') print('Connected to DHT.')
checked = True checked = True
self.ensure_exe(self.add_friend, (GROUP_BOT, "")) try:
self.bid = self.get_friend_id(GROUP_BOT)
except:
self.ensure_exe(self.add_friend, (GROUP_BOT, "Hi"))
self.bid = self.get_friend_id(GROUP_BOT) self.bid = self.get_friend_id(GROUP_BOT)
if checked and not status: if checked and not status:
@ -105,9 +108,11 @@ class SyncBot(Tox):
self.save_to_file('data') self.save_to_file('data')
def on_connection_status(self, friendId, status): def on_connection_status(self, friendId, status):
if not self.joined and friendId == self.bid and status: if not self.request and not self.joined \
and friendId == self.bid and status:
print('Groupbot online, trying to join group chat.') print('Groupbot online, trying to join group chat.')
self.ensure_exe(self.send_message, (tid, 'invite')) self.request = True
self.ensure_exe(self.send_message, (self.bid, 'invite'))
def on_group_invite(self, friendid, pk): def on_group_invite(self, friendid, pk):
if not self.joined: if not self.joined: