1
0

queries.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import random, secrets, datetime, uuid, random
  2. from slugify import slugify
  3. from common import log
  4. def create_or_get_and_update_last_event(self, table, event_type, channel_name=None, user_name=None, event_content=None, event_subject_name=None):
  5. if table == 'channel':
  6. name = channel_name
  7. if table == 'user':
  8. name = user_name
  9. record = self.db.one('SELECT * FROM rotbot_' + table + ' WHERE LOWER(name)=LOWER(%(name)s) AND network_id=%(network_id)s', name=name, network_id=self.network.id) # Look up the record.
  10. if record: # On record.
  11. fields = 'name=%(name)s, last_event_type=%(last_event_type)s' # All required fields
  12. event_channel_id = None
  13. event_user_id = None
  14. event_subject_id = None
  15. if 'event_channel' in locals():
  16. event_channel_id = get_channel_id(self, channel_name)
  17. fields += ', last_event_channel_id=%(last_event_channel_id)s'
  18. if 'event_user' in locals():
  19. event_user_id = get_user_id(self, user_name)
  20. fields += ', last_event_user_id=%(last_event_user_id)s'
  21. if 'event_content' in locals():
  22. fields += ', last_event_content=%(last_event_content)s'
  23. if 'event_subject' in locals():
  24. event_subject_id = get_user_id(self, user_name)
  25. fields +- ', last_event_subject_id=%(last_event_subject_id)s'
  26. self.db.run( # Correct capitalisation and update last event.
  27. 'UPDATE rotbot_' + table + ' SET ' + fields + ' WHERE LOWER(name)=LOWER(%(name)s) AND network_id=%(network_id)s',
  28. name=name,
  29. network_id=self.network.id,
  30. last_event_type=event_type,
  31. last_event_channel_id=event_channel_id,
  32. last_event_user_id=event_user_id,
  33. last_event_content=event_content,
  34. last_event_subject_id=event_subject_id)
  35. else: # Not on record.
  36. new_slug = slugify('%s-%s' % (name, self.network.name), max_length=50) # Create a slug from the name.
  37. slug_exists = self.db.one('SELECT id FROM rotbot_' + table + ' WHERE slug=%(slug)s', slug=new_slug) # Try to lookup a record with the proposed slug.
  38. # Keep adding random letters from the name to the slug until it is unique.
  39. while slug_exists and slug_exists <= 50:
  40. new_slug = new_slug + random.choice(name)
  41. slug_exists = self.db.one('SELECT id FROM rotbot_' + table + ' WHERE slug=%(slug)s', slug=new_slug)
  42. log.notice('Requested slug already taken on table %s with network %s: %s' % (table, self.network.name, new_slug))
  43. while slug_exists:
  44. new_slug = name + str(uuid.uuid4())
  45. slug_exists = self.db.one('SELECT id FROM rotbot_' + table + ' WHERE slug=%(slug)s', slug=new_slug)
  46. log.warning('Please clean database. Newly generated uuid4 already taken on table %s with network %s: %s' % (table, self.network.name, new_slug))
  47. fields = 'name, network_id, slug, last_event_type, last_event_datetime'
  48. values = '%(name)s, %(network_id)s, %(slug)s, %(last_event_type)s, %(last_event_datetime)s'
  49. event_datetime = datetime.datetime.now
  50. event_channel_id = None
  51. event_user_id = None
  52. event_subject_id = None
  53. if 'event_channel' in locals():
  54. event_channel_id = get_channel_id(self, channel_name)
  55. fields += ', last_event_channel_id'
  56. values += ', %(last_event_channel_id)s'
  57. if 'event_user' in locals():
  58. event_user_id = get_user_id(self, user_name)
  59. fields += ', last_event_user_id'
  60. values += ', %(last_event_user_id)s'
  61. if 'event_content' in locals():
  62. fields += ', last_event_content'
  63. values += ', %(last_event_content)s'
  64. if 'event_subject' in locals():
  65. event_subject_id = get_user_id(self, user_name)
  66. fields += ', last_event_subject_id'
  67. values += ', %(last_event_subject_id)s'
  68. if table == 'channel':
  69. fields += ', autojoin, key, games, statistic_commands, chat'
  70. values += ', %(autojoin)s, %(key)s, %(games)s, %(statistic_commands'
  71. self.db.run( # Create record.
  72. 'INSERT INTO rotbot_' + table + ' (' + fields + ') VALUES (' + values + ')s, %(chat)s)',
  73. name=name,
  74. network_id=self.network.id,
  75. slug=new_slug,
  76. last_event_type=event_type,
  77. last_event_datetime=datetime.datetime.now(),
  78. last_event_channel_id=event_channel_id,
  79. last_event_user_id=event_user_id,
  80. last_event_content=event_content,
  81. last_event_subject_id=event_subject_id,
  82. autojoin=False,
  83. key='',
  84. games=False,
  85. statistic_commands=False,
  86. chat=False,
  87. )
  88. else:
  89. fields += ', xp_spent, level, coin, coin_given, coin_spent, ap_spent, karma_correction, no_chat'
  90. values += ', %(xp_spent)s, %(level)s, %(coin)s, %(coin_given)s, %(coin_spent)s, %(ap_spent)s, %(karma_correction)s, %(no_chat)s'
  91. self.db.run( # Create record.
  92. 'INSERT INTO rotbot_' + table + ' (' + fields + ') VALUES (' + values + ')',
  93. name=name,
  94. network_id=self.network.id,
  95. slug=new_slug, last_event_type=event_type,
  96. last_event_datetime=datetime.datetime.now(),
  97. last_event_channel_id=event_channel_id,
  98. last_event_user_id=event_user_id,
  99. last_event_content=event_content,
  100. last_event_subject_id=event_subject_id,
  101. xp_spent=0,
  102. level=0,
  103. coin=0,
  104. coin_given=0,
  105. coin_spent=0,
  106. ap_spent=0,
  107. karma_correction=0,
  108. no_chat=False,
  109. )
  110. record = self.db.one('SELECT * FROM rotbot_' + table + ' WHERE slug=%(slug)s', slug=new_slug) # Lookup newly created record to return to call..
  111. return record
  112. def get_owners(self):
  113. return self.db.all('SELECT * FROM rotbot_owner')
  114. # Channel
  115. def get_channel_id(self, channel_name):
  116. return self.db.one('SELECT id FROM rotbot_channel WHERE network_id=%(network_id)s AND LOWER(name)=LOWER(%(channel_name)s)', network_id=self.network.id, channel_name=channel_name)
  117. def get_channel_slug(self, channel_name):
  118. return self.db.one('SELECT slug FROM rotbot_channel WHERE network_id=%(network_id)s AND LOWER(name)=LOWER(%(channel_name)s)', network_id=self.network.id, channel_name=channel_name)
  119. def get_channel_setting_statistic_commands(self, channel_id):
  120. return self.db.one('SELECT statistic_commands FROM rotbot_channel WHERE id=%(channel_id)s', channel_id=channel_id)
  121. def get_channel_setting_game_commands(self, channel_id):
  122. return self.db.one('SELECT games FROM rotbot_channel WHERE id=%(channel_id)s', channel_id=channel_id)
  123. def get_autojoin_channels(self):
  124. return self.db.all('SELECT name FROM rotbot_channel WHERE network_id=%(network_id)s AND autojoin=True', network_id=self.network.id)
  125. #def update_channel_last_event(self, channel_id, event_type, event_content):
  126. # self.db.run('UPDATE rotbot_channel SET last_event_type=%(last_event_type)s, last_event_content=%(last_event_content)s WHERE channel.id=%(channel_id)s', last_event_type=event_type, last_event_content=event_content, channel_id=channel_id)
  127. def create_tempchannelkey(self, channel_id):
  128. temp_key = secrets.token_urlsafe(40)[:40]
  129. self.db.run('INSERT INTO rotbot_tempchannelkey (key, network_id, channel_id, created) VALUES (%(key)s, %(network_id)s, %(channel_id)s, %(created)s)', key=temp_key, network_id=self.network.id, channel_id=channel_id, created=datetime.datetime.now())
  130. return temp_key
  131. # User
  132. def get_user(self, user_name):
  133. return self.db.one('SELECT * FROM rotbot_user WHERE network_id=%(network_id)s AND LOWER(name)=LOWER(%(user_name)s)', network_id=self.network.id, user_name=user_name)
  134. def get_user_id(self, user_name):
  135. return self.db.one('SELECT id FROM rotbot_user WHERE network_id=%(network_id)s AND LOWER(name)=LOWER(%(user_name)s)', network_id=self.network.id, user_name=user_name)
  136. def get_user_slug(self, user_name):
  137. return self.db.one('SELECT slug FROM rotbot_user WHERE network_id=%(network_id)s AND LOWER(name)=LOWER(%(user_name)s)', network_id=self.network.id, user_name=user_name)
  138. def get_user_name(self, user_id):
  139. return self.db.one('SELECT name FROM rotbot_user WHERE id=%(id)s', id=user_id)
  140. def get_user_total_joins(self, user_id):
  141. recordset = self.db.all('SELECT amount FROM rotbot_join WHERE user_id=%(user_id)s AND network_id=%(network_id)s', user_id=user_id, network_id=self.network.id)
  142. return total_amount_in_recordset(recordset)
  143. def get_user_total_kicks(self, user_id):
  144. recordset = self.db.all('SELECT amount FROM rotbot_kick WHERE kicker_id=%(user_id)s AND network_id=%(network_id)s', user_id=user_id, network_id=self.network.id)
  145. return total_amount_in_recordset(recordset)
  146. def get_user_total_kicked(self, user_id):
  147. recordset = self.db.all('SELECT amount FROM rotbot_kick WHERE kicked_id=%(user_id)s AND network_id=%(network_id)s', user_id=user_id, network_id=self.network.id)
  148. return total_amount_in_recordset(recordset)
  149. def get_user_total_messages(self, user_id):
  150. recordset = self.db.all('SELECT amount FROM rotbot_message WHERE user_id=%(user_id)s AND network_id=%(network_id)s', user_id=user_id, network_id=self.network.id)
  151. return total_amount_in_recordset(recordset)
  152. def get_user_total_actions(self, user_id):
  153. recordset = self.db.all('SELECT amount FROM rotbot_action WHERE user_id=%(user_id)s AND network_id=%(network_id)s', user_id=user_id, network_id=self.network.id)
  154. return total_amount_in_recordset(recordset)
  155. def get_user_total_notices(self, user_id):
  156. recordset = self.db.all('SELECT amount FROM rotbot_notice WHERE user_id=%(user_id)s AND network_id=%(network_id)s', user_id=user_id, network_id=self.network.id)
  157. return total_amount_in_recordset(recordset)
  158. def get_user_total_curseword_added(self, user_id):
  159. recordset = self.db.all('SELECT id FROM rotbot_curseword WHERE irc_user_id=%(user_id)s ', user_id=user_id)
  160. return recordset.count(recordset)
  161. def get_user_total_curseadjective_added(self, user_id):
  162. recordset = self.db.all('SELECT id FROM rotbot_curseadjective WHERE irc_user_id=%(user_id)s', user_id=user_id)
  163. return recordset.count(recordset)
  164. # Chat
  165. def random_curse(self):
  166. adjective = self.db.one('SELECT word FROM rotbot_curseadjective ORDER BY RANDOM() LIMIT 1')
  167. curse = self.db.one('SELECT word FROM rotbot_curseword ORDER BY RANDOM() LIMIT 1')
  168. return '%s %s' % (adjective, curse)
  169. def get_curse(self, word):
  170. return self.db.one('SELECT * FROM rotbot_curseword WHERE LOWER(word)=LOWER(%(word)s)')
  171. def add_curseword(self, word, user_id):
  172. user_id = get_user_id(self, user_name)
  173. return self.db.run('INSERT INTO rotbot_curseword (word, created, irc_user_id, banned) VALUES (LOWER(%(word)s), %(created)s, %(irc_user_id)s, %(banned)s)', word=word, created=datetime.datetime.now, irc_user_id=user_id, banned=False)
  174. # Statistics
  175. def update_message_statistics(self, type, channel_id, user_id):
  176. if not self.db.one('SELECT id FROM rotbot_' + type + ' WHERE network_id=%(network_id)s AND channel_id=%(channel_id)s AND user_id=%(user_id)s', network_id=self.network.id, channel_id=channel_id ,user_id=user_id): # Not on record.
  177. self.db.run('INSERT INTO rotbot_' + type + ' (network_id, channel_id, user_id, amount) VALUES (%(network_id)s, %(channel_id)s, %(user_id)s, 1)', network_id=self.network.id, channel_id=channel_id ,user_id=user_id) # Create record.
  178. else: # On record.
  179. self.db.run('UPDATE rotbot_' + type + ' SET amount = amount +1 WHERE network_id=%(network_id)s AND channel_id=%(channel_id)s AND user_id=%(user_id)s', network_id=self.network.id, channel_id=channel_id ,user_id=user_id) # Increment record.
  180. def increment_join(self, channel_id, user_id):
  181. if not self.db.one('SELECT id FROM rotbot_join WHERE network_id=%(network_id)s AND channel_id=%(channel_id)s AND user_id=%(user_id)s', network_id=self.network.id, channel_id=channel_id, user_id=user_id): # No record yet
  182. self.db.run('INSERT INTO rotbot_join (network_id, channel_id, user_id, amount) VALUES (%(network_id)s, %(channel_id)s, %(user_id)s, 1)', network_id=self.network.id, channel_id=channel_id, user_id=user_id) # Create record.
  183. else:
  184. self.db.run('UPDATE rotbot_join SET amount = amount + 1 WHERE network_id=%(network_id)s AND channel_id=%(channel_id)s AND user_id=%(user_id)s', network_id=self.network.id, channel_id=channel_id, user_id=user_id) # Update existing record.
  185. def increment_kick(self, channel_id, kicker_id, kicked_id):
  186. if not self.db.one('SELECT id FROM rotbot_kick WHERE channel_id=%(channel_id)s AND kicker_id=%(kicker_id)s AND kicked_id=%(kicked_id)s', network_id=self.network.id, channel_id=channel_id, kicker_id=kicker_id, kicked_id=kicked_id): # No records for kicker channel combination.
  187. self.db.run('INSERT INTO rotbot_kick (channel_id, kicker_id, kicked_id, amount) VALUES (%(channel_id)s, %(kicker_id)s, %(kicked_id)s), 1', channel_id=channel_id, kicker_id=kicker_id, kicked_id=kicked_id) # Create record.
  188. else:
  189. self.db.run('UPDATE rotbot_kick SET amount = amount + 1 WHERE channel_id=%(channel_id)s AND kicker_id=%(kicker_id)s AND kicked_id=%(kicked_id)s', channel_id=channel_id, kicker_id=kicker_id, kicked_id=kicked_id) # Update record.
  190. # Game
  191. def get_top_users(self, sort):
  192. return self.db.all('SELECT id, name, level, xp_spent, coin FROM rotbot_user WHERE network_id=%(network_id)s ORDER BY ' + sort + ' DESC LIMIT 3 ', network_id=self.network.id)
  193. def get_user_gamestats(self, user_id):
  194. return self.db.one('SELECT xp_spent, level, coin, coin_given, coin_spent, ap_spent, karma_correction FROM rotbot_user WHERE id=%(id)s', id=user_id)
  195. def levelup_user(self, user_id, xp):
  196. self.db.run('UPDATE rotbot_user SET level=level+1, xp_spent=xp_spent+%(xp)s, ap_spent=ap_spent+1 WHERE id=%(user_id)s', xp=xp, user_id=user_id)
  197. coin = random.uniform(0.1, 0.5)
  198. payday(self, coin)
  199. def punish_user(self, user_id, coin, karma):
  200. self.db.run('UPDATE website_users SET coin=coin-%(coin)s, karma_correction=karma_correction-%(karma)s WHERE id=%(user_id)s', coin=coin, karma=karma, user_id=user_id)
  201. def payday(self, coin):
  202. self.db.run('UPDATE rotbot_user SET coin=coin+%(coin)s WHERE level>0', user_id=user.id)
  203. def cointransfer(self, sender_id, receiver_id, coin):
  204. self.db.run('UPDATE rotbot_user SET coin=coin-%(coin)s, coin_spent=coin_spent+%(coin)s, coin_given=coin_given+%(coin)s, ap_spent=ap_spent+1 WHERE id=%(sender_id)s', coin=coin, sender_id=sender_id)
  205. self.db.run("UPDATE rotbot_user SET coin=coin+%(coin)s WHERE id=%(user_id)s", coin=coin, id=receiver_id)
  206. coin = random.uniform(0.1, 0.3)
  207. payday(self, coin)
  208. # Common.
  209. def total_amount_in_recordset(recordset):
  210. amount = 0
  211. for record in recordset:
  212. #amount += record.amount
  213. amount += record
  214. return amount