1
0

on_join.py 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import datetime, pytz
  2. from common import log, queries, font, userstatus
  3. def process_event(self, connection, event):
  4. log.info(event) # Log to console.
  5. # Get and update resources.
  6. channel = queries.create_or_get_and_update_last_event(self, 'channel', 'cj', channel_name=event.target, user_name=event.source.nick)
  7. user = queries.create_or_get_and_update_last_event(self, 'user', 'cj', channel_name=event.target, user_name=event.source.nick)
  8. queries.increment_join(self, channel.id, user.id)
  9. if event.source.nick == connection.get_nickname(): # The bot joined a channel.
  10. if self.channels[event.target].has_key(): # Passworded channel.
  11. if event.target in self.channelkeys: # New key used to join channel.
  12. log.info('Saving channel key for %s.' %s)
  13. self.db.run('UPDATE rotbot_channels SET key=%(key)s WHERE id=%(id)s', key=self.channelkeys[event.target], id=channel.id) # Save new key to DB.
  14. return # Stop if the bot joined the channel.
  15. # Op via x bot
  16. if self.network.services == 'x':
  17. connection.privmsg(self.xbot, 'op %s %s' % event.target, connection.get_nickname())
  18. # Greeting conditions.
  19. timeout = 10 # Amount of minutes to let pass before greeting again.
  20. joins = queries.get_user_channel_joins(self, user.id, channel.id)
  21. chan_lastgreet = queries.get_channel_last_greet(self, channel.id)
  22. user_lastgreet = queries.get_user_last_greet(self, user.id)
  23. greet = False
  24. amount_greet = False
  25. cet = pytz.timezone('Europe/Amsterdam')
  26. if not chan_lastgreet:
  27. chan_lastgreet = cet.localize(datetime.datetime.now()) - datetime.timedelta(minutes=timeout+1)
  28. if not user_lastgreet:
  29. user_lastgreet = cet.localize(datetime.datetime.now()) - datetime.timedelta(minutes=timeout+1)
  30. if chan_lastgreet < cet.localize(datetime.datetime.now()) - datetime.timedelta(minutes=timeout) and user_lastgreet < cet.localize(datetime.datetime.now()) - datetime.timedelta(minutes=timeout): # User or channel have not had greet message in the last timeout minutes, there is a curse available
  31. if joins % 200 == 0: # Has joined 200, or a multiple of 200 times. (Also 0 but the join has already been saved.)
  32. amount_greet = True
  33. elif userstatus.atleast_voiced(self, event.source.nick, self.network.home_channel) or queries.is_owner(self, event.source): # User has atleast voice in home channel or usermask is in database as owner.
  34. curse = queries.random_curse(self)
  35. if curse:
  36. greet = True
  37. # Promotion conditions.
  38. promote = False
  39. if queries.is_owner(self, event.source): # Usermask in database.
  40. promote = True
  41. elif userstatus.is_owner(self, event.source.nick, self.network.home_channel): # Owner of homechannel
  42. promote = True
  43. # Promote.
  44. if promote:
  45. log.info('Promoting %s in %s' % (event.source.nick, event.target))
  46. connection.mode(event.target, "+qaohv %s %s %s %s %s" % (event.source.nick, event.source.nick, event.source.nick, event.source.nick, event.source.nick))
  47. # Greet.
  48. if amount_greet:
  49. log.info('Greeting %s in %s' % (event.source.nick, event.target))
  50. adjective = queries.random_adjective(self)
  51. if adjective[0].lower() == 'a':
  52. adjective = 'n ' + adjective
  53. else:
  54. adjective = ' ' + adjective
  55. if adjective:
  56. connection.privmsg(event.target, 'Welcome back %s, I have seen you join here a%s %s times.' % (event.source.nick, adjective, joins))
  57. connection.action(self.network.home_channel, ' has seen %s%s%s join %s%s%s a%s %s%s%s times.' % (font.red, event.source.nick, font.reset, font.red, event.target, font.reset, adjective, font.green, joins, font.reset))
  58. else:
  59. connection.privmsg(event.target, 'Welcome back %s, have seen you join here an amazing %s times.' % (event.source.nick, joins))
  60. Inform.home_channel(self, connection, '%s%s%s has joined %s%s%s %s%s%s times.' % (font.red, event.source.nick, font.reset, font.red, event.target, font.reset, font.green, joins, font.reset))
  61. queries.update_channel_last_greet(self, channel.id)
  62. queries.update_user_last_greet(self, user.id)
  63. elif greet:
  64. log.info('Greeting %s in %s' % (event.source.nick, event.target))
  65. connection.privmsg(event.target, 'Welcome back %s, you %s.' % (event.source.nick, curse))
  66. queries.update_channel_last_greet(self, channel.id)
  67. queries.update_user_last_greet(self, user.id)