on_join.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. # Greeting conditions.
  16. timeout = 10 # Amount of minutes to let pass before greeting again.
  17. joins = queries.get_user_channel_joins(self, user.id, channel.id)
  18. chan_lastgreet = queries.get_channel_last_greet(self, channel.id)
  19. user_lastgreet = queries.get_user_last_greet(self, user.id)
  20. greet = False
  21. amount_greet = False
  22. cet = pytz.timezone('Europe/Amsterdam')
  23. if not chan_lastgreet:
  24. chan_lastgreet = cet.localize(datetime.datetime.now()) - datetime.timedelta(minutes=timeout+1)
  25. if not user_lastgreet:
  26. user_lastgreet = cet.localize(datetime.datetime.now()) - datetime.timedelta(minutes=timeout+1)
  27. 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
  28. if joins % 200 == 0: # Has joined 200, or a multiple of 200 times. (Also 0 but the join has already been saved.)
  29. amount_greet = True
  30. elif userstatus.atleast_voiced(self, event.source.nick, self.network.home_channel): # User has atleast voice in home channel.
  31. curse = queries.random_curse(self)
  32. if curse:
  33. greet = True
  34. # Promotion conditions.
  35. promote = False
  36. if queries.is_owner(self, event.source): # Usermask in database.
  37. promote = True
  38. elif userstatus.is_owner(self, event.source.nick, self.network.home_channel): # Owner of homechannel
  39. promote = True
  40. # Promote.
  41. if promote:
  42. log.info('Promoting %s in %s' % (event.source.nick, event.target))
  43. connection.mode(event.target, "+qaohv %s %s %s %s %s" % (event.source.nick, .source.nick, event.source.nick, event.source.nick, event.source.nick))
  44. # Greet.
  45. if amount_greet:
  46. log.info('Greeting %s in %s' % (event.source.nick, event.target))
  47. adjective = queries.random_adjective(self)
  48. if adjective[0].lower() == 'a':
  49. adjective = 'n ' + adjective
  50. else:
  51. adjective = ' ' + adjective
  52. if adjective:
  53. connection.privmsg(event.target, 'Welcome back %s, I have seen you join here a%s %s times.' % (event.source.nick, adjective, joins))
  54. 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))
  55. else:
  56. connection.privmsg(event.target, 'Welcome back %s, have seen you join here an amazing %s times.' % (event.source.nick, joins))
  57. 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))
  58. queries.update_channel_last_greet(self, channel.id)
  59. queries.update_user_last_greet(self, user.id)
  60. elif greet:
  61. log.info('Greeting %s in %s' % (event.source.nick, event.target))
  62. connection.privmsg(event.target, 'Welcome back %s, you %s.' % (event.source.nick, curse))
  63. queries.update_channel_last_greet(self, channel.id)
  64. queries.update_user_last_greet(self, user.id)