user_handlers.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. from flask import current_app
  2. from sqlite3 import Error
  3. from re import sub
  4. def get_users_points(db, user_id): # Look up one user's points by user id
  5. try:
  6. cursor = db.execute(
  7. "SELECT points FROM points WHERE id = ?",
  8. (user_id,)
  9. )
  10. return cursor.fetchone()[0]
  11. except Error as guperror:
  12. current_app.logger.error(f'Couldn\'t look up points for {user_id}: {guperror.args[0]}')
  13. def get_all_users_by_name(db, username): # Look up all users' points by username
  14. try:
  15. cursor = db.execute(
  16. "SELECT name, points FROM points WHERE name = ?",
  17. (username,)
  18. )
  19. users = cursor.fetchall()
  20. return users
  21. except Error as gaubnerror:
  22. current_app.logger.error(f'Couldn\'t look up points for {username} by username: {gaubnerror.args[0]}')
  23. def get_all_users_with_user_id(db, user_id): # Look up all users' points by user id
  24. try:
  25. cursor = db.execute(
  26. "SELECT id, name, points FROM points WHERE id = ?",
  27. (user_id,)
  28. )
  29. users = cursor.fetchall()
  30. return users
  31. except Error as gauwuierror:
  32. current_app.logger.error(f'Couldn\'t look up points for {user_id} by user_id: {gauwuierror.args[0]}')
  33. def get_all_users(db): # Get all users' details from points database
  34. try:
  35. cursor = db.execute(
  36. "SELECT name, points, id FROM points"
  37. )
  38. users = cursor.fetchall()
  39. return users
  40. except Error as gauerror:
  41. current_app.logger.error(f'Couldn\'t get all users\' points: {gauerror.args[0]}')
  42. def award_chat_points(db, user_id, points): # Award points to user by user id
  43. try:
  44. db.execute(
  45. "UPDATE points SET points = points + ? WHERE id = ?",
  46. (points, user_id,)
  47. )
  48. db.commit()
  49. except Error as acperror:
  50. current_app.logger.error(f'Couldn\'t give {points} points to {user_id}: {acperror.args[0]}')
  51. def adjust_points(db, user_id, points): # For streamer to manually adjust a user's points
  52. try:
  53. db.execute(
  54. "UPDATE points SET points = ? WHERE id = ?",
  55. (points, user_id,)
  56. )
  57. db.commit()
  58. except Error as aperror:
  59. current_app.logger.error(f'Couldn\'t adjust points for {user_id}: {aperror.args[0]}')
  60. def spend_points(db, user_id, points): # A user spends points on a redeem
  61. try:
  62. db.execute(
  63. "UPDATE points SET points = points - ? WHERE id = ?",
  64. (points, user_id,)
  65. )
  66. db.commit()
  67. return True
  68. except Error as sperror:
  69. current_app.logger.error(f'Couldn\'t spend {user_id}\'s {points} points: {sperror.args[0]}')
  70. return False
  71. def refund_points(db, user_id, points): # Streamer refunds points for a redeem
  72. try:
  73. db.execute(
  74. "UPDATE points SET points = points + ? WHERE id = ?",
  75. (points, user_id,)
  76. )
  77. db.commit()
  78. return True
  79. except Error as rerror:
  80. current_app.logger.error(f'Couldn\'t refund {points} points for {user_id}: {rerror.args[0]}')
  81. return False
  82. def user_in_points(db, user_id): # Check if a user is in the points database
  83. try:
  84. cursor = db.execute(
  85. "SELECT points FROM points WHERE id = ?",
  86. (user_id,)
  87. )
  88. if cursor.fetchone() is None:
  89. return False
  90. return True
  91. except Error as uiperror:
  92. current_app.logger.error(f'Couldn\'t for {user_id} in points database: {uiperror.args[0]}')
  93. def add_user_to_points(db, user_id, display_name): # Add a user to the points database
  94. try:
  95. cursor = db.execute(
  96. "SELECT points, name FROM points WHERE id = ?",
  97. (user_id,)
  98. )
  99. user = cursor.fetchone()
  100. if user is None:
  101. cursor.execute(
  102. "INSERT INTO points(id, name, points, user_authed) VALUES(?, ?, 10, 0)",
  103. (user_id, display_name)
  104. )
  105. if user is not None and user[1] is None:
  106. cursor.execute(
  107. """UPDATE points
  108. SET name = ?
  109. WHERE id = ?""",
  110. (display_name, user_id)
  111. )
  112. db.commit()
  113. except Error as autperror:
  114. current_app.logger.error(f'Couldn\'t add {user_id}/{display_name} to points database: {autperror.args[0]}')
  115. def change_name(db, user_id, new_name): # Change a user name in the points database
  116. try:
  117. db.execute(
  118. "UPDATE points SET name = ? WHERE id = ?",
  119. (new_name, user_id)
  120. )
  121. db.commit()
  122. except Error as cnerror:
  123. current_app.logger.error(f'Couldn\'t change name to {new_name} for {user_id}: {cnerror.args[0]}')
  124. def remove_duplicates(db, user_id, username): # Remove duplicate usernames
  125. try:
  126. db.execute(
  127. """UPDATE points
  128. SET name = NULL
  129. WHERE name = ? AND NOT id = ?""",
  130. (username, user_id)
  131. )
  132. db.commit()
  133. except Error as rderror:
  134. current_app.logger.error(f'Couldn\'t remove duplicate username {username} for {user_id}: {rderror.args[0]}')