1
0

on_join.py 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import datetime
  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. log.info('Joined %s' % event.target)
  11. if self.channels[event.target].has_key(): # Passworded channel.
  12. if event.target in self.channelkeys: # New key used to join channel.
  13. log.info('Saving channel key for %s.' %s)
  14. 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.
  15. return # Stop if the bot joined the channel.
  16. # Greeting conditions.
  17. timeout = 10 # Amount of minutes to let pass before greeting again.
  18. joins = queries.get_user_channel_joins(self, user.id, channel.id)
  19. chan_lastgreet = queries.get_channel_last_greet(self, channel.id)
  20. user_lastgreet = queries.get_user_last_greet(self, user.id)
  21. greet = False
  22. amount_greet = False
  23. if not chan_lastgreet:
  24. chan_lastgreet = datetime.datetime.now() - datetime.timedelta(minutes=timeout+1)
  25. if not user_lastgreet:
  26. user_lastgreet = datetime.datetime.now() - datetime.timedelta(minutes=timeout+1)
  27. if chan_lastgreet < datetime.datetime.now() - datetime.timedelta(minutes=timeout) and user_lastgreet < 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. greet = True
  32. # Promotion conditions.
  33. promote = False
  34. if queries.is_owner(self, event.source): # Usermask in database.
  35. promote = True
  36. elif userstatus.is_owner(self, event.source.nick, self.network.home_channel): # Owner of homechannel
  37. promote = True
  38. # Promote.
  39. if promote:
  40. log.info('Promoting %s in %s' % (event.source.nick, event.target))
  41. connection.mode(event.target, "+aohv %s %s %s %s" % (event.source.nick, event.source.nick, event.source.nick, event.source.nick))
  42. # Greet.
  43. if greet or amount_greet:
  44. curse = queries.random_curse(self)
  45. if not curse:
  46. return
  47. if amount_greet:
  48. log.info('Greeting %s in %s' % (event.source.nick, event.target))
  49. adjective = queries.random_adjective(self)
  50. if adjective[0].lower() == 'a':
  51. adjective = 'n ' + adjective
  52. else:
  53. adjective = ' ' + adjective
  54. if adjective:
  55. connection.privmsg(event.target, 'Welcome back %s, I have seen you join here a%s %s times.' % (event.source.nick, adjective, joins))
  56. 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))
  57. else:
  58. connection.privmsg(event.target, 'Welcome back %s, have seen you join here an amazing %s times.' % (event.source.nick, joins))
  59. 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))
  60. queries.update_channel_last_greet(self, channel.id)
  61. queries.update_user_last_greet(self, user.id)
  62. elif greet:
  63. log.info('Greeting %s in %s' % (event.source.nick, event.target))
  64. connection.privmsg(event.target, 'Welcome back %s, you %s.' % (event.source.nick, curse))
  65. queries.update_channel_last_greet(self, channel.id)
  66. queries.update_user_last_greet(self, user.id)