checkers.apology
1from . import Checker 2import src.utils.db 3 4 5class ApologyChecker(Checker): 6 def __init__(self): 7 self.conn = src.utils.db.DbConnector() 8 self.conn.connect() 9 10 def check_message(self, message): 11 """ 12 Just checks for Sorry message 13 :param message: message string 14 :return:True or False 15 """ 16 return message.find("sorry") != -1 17 18 def check_user_for_warning(self, user_id, server_name): 19 """ 20 Function to check whether a user has any outstanding warnings 21 """ 22 try: 23 self.add_user(user_id, server_name) 24 out = 0 25 cursor = self.conn.connector.cursor() 26 sql_query = "SELECT offense_count - apology_count FROM discorddb.user_activity WHERE user_id = %s AND server_name = %s" 27 cursor.execute(sql_query, (user_id, server_name)) 28 result = cursor.fetchone() 29 out = result[0] 30 except Exception as error: 31 print("Failed to get record from MySQL table: {}".format(error)) 32 finally: 33 cursor.close() 34 if out > 0: 35 print("Offenses more than expected. Banning User") 36 self.ban_user(user_id, server_name, 1) 37 return False 38 39 self.ban_user(user_id, server_name, 0) 40 return True 41 42 def add_warning(self, user_id, server_name): 43 try: 44 self.add_user(user_id, server_name) 45 cursor = self.conn.connector.cursor() 46 sql_query = "UPDATE discorddb.user_activity SET offense_count = offense_count+1 WHERE user_id = %s AND server_name = %s" 47 48 val = (user_id, server_name) 49 cursor.execute(sql_query, val) 50 self.conn.connector.commit() 51 except Exception as error: 52 print("Failed to get record from MySQL table: {}".format(error)) 53 finally: 54 cursor.close() 55 return True 56 57 def add_user(self, user_id, server_name): 58 """ 59 Function to add user onto the database. If already banned, can't add user. 60 :param user_id: 61 :param server_name: 62 :return: 63 """ 64 try: 65 cursor = self.conn.connector.cursor() 66 sql_query = "SELECT is_banned FROM discorddb.user_activity WHERE user_id = %s AND server_name = %s" 67 cursor.execute(sql_query, (user_id, server_name)) 68 result = cursor.fetchone() 69 if not result: 70 print("User not present in the channel.") 71 sql_query = "INSERT INTO discorddb.user_activity (user_id, server_name) VALUES (%s,%s)" 72 val = (user_id, server_name) 73 cursor.execute(sql_query, val) 74 self.conn.connector.commit() 75 print("User is added into the channel") 76 else: 77 if result[0] == 1: 78 print("User is banned from this server. Can't insert into the channel") 79 80 except Exception as error: 81 print("Failed to get record from MySQL table: {}".format(error)) 82 finally: 83 cursor.close() 84 return True 85 86 def add_apology(self, user_id, server_name): 87 try: 88 self.add_user(user_id, server_name) 89 cursor = self.conn.connector.cursor() 90 sql_query = "UPDATE discorddb.user_activity SET apology_count = apology_count+1 WHERE user_id = %s AND server_name = %s" 91 val = (user_id, server_name) 92 cursor.execute(sql_query, val) 93 sql_query = "UPDATE discorddb.user_activity SET offense_count = CASE WHEN offense_count < apology_count THEN apology_count ELSE offense_count END WHERE user_id = %s AND server_name = %s" 94 cursor.execute(sql_query, val) 95 self.conn.connector.commit() 96 except Exception as error: 97 print("Failed to get record from MySQL table: {}".format(error)) 98 finally: 99 cursor.close() 100 return True 101 102 def ban_user(self, user_id, server_name, is_banned): 103 try: 104 self.add_user(user_id, server_name) 105 cursor = self.conn.connector.cursor() 106 sql_query = "UPDATE discorddb.user_activity SET is_banned = %s WHERE user_id = %s AND server_name = %s" 107 val = (is_banned, user_id, server_name) 108 cursor.execute(sql_query, val) 109 self.conn.connector.commit() 110 except Exception as error: 111 print("Failed to get record from MySQL table: {}".format(error)) 112 finally: 113 cursor.close() 114 return True
6class ApologyChecker(Checker): 7 def __init__(self): 8 self.conn = src.utils.db.DbConnector() 9 self.conn.connect() 10 11 def check_message(self, message): 12 """ 13 Just checks for Sorry message 14 :param message: message string 15 :return:True or False 16 """ 17 return message.find("sorry") != -1 18 19 def check_user_for_warning(self, user_id, server_name): 20 """ 21 Function to check whether a user has any outstanding warnings 22 """ 23 try: 24 self.add_user(user_id, server_name) 25 out = 0 26 cursor = self.conn.connector.cursor() 27 sql_query = "SELECT offense_count - apology_count FROM discorddb.user_activity WHERE user_id = %s AND server_name = %s" 28 cursor.execute(sql_query, (user_id, server_name)) 29 result = cursor.fetchone() 30 out = result[0] 31 except Exception as error: 32 print("Failed to get record from MySQL table: {}".format(error)) 33 finally: 34 cursor.close() 35 if out > 0: 36 print("Offenses more than expected. Banning User") 37 self.ban_user(user_id, server_name, 1) 38 return False 39 40 self.ban_user(user_id, server_name, 0) 41 return True 42 43 def add_warning(self, user_id, server_name): 44 try: 45 self.add_user(user_id, server_name) 46 cursor = self.conn.connector.cursor() 47 sql_query = "UPDATE discorddb.user_activity SET offense_count = offense_count+1 WHERE user_id = %s AND server_name = %s" 48 49 val = (user_id, server_name) 50 cursor.execute(sql_query, val) 51 self.conn.connector.commit() 52 except Exception as error: 53 print("Failed to get record from MySQL table: {}".format(error)) 54 finally: 55 cursor.close() 56 return True 57 58 def add_user(self, user_id, server_name): 59 """ 60 Function to add user onto the database. If already banned, can't add user. 61 :param user_id: 62 :param server_name: 63 :return: 64 """ 65 try: 66 cursor = self.conn.connector.cursor() 67 sql_query = "SELECT is_banned FROM discorddb.user_activity WHERE user_id = %s AND server_name = %s" 68 cursor.execute(sql_query, (user_id, server_name)) 69 result = cursor.fetchone() 70 if not result: 71 print("User not present in the channel.") 72 sql_query = "INSERT INTO discorddb.user_activity (user_id, server_name) VALUES (%s,%s)" 73 val = (user_id, server_name) 74 cursor.execute(sql_query, val) 75 self.conn.connector.commit() 76 print("User is added into the channel") 77 else: 78 if result[0] == 1: 79 print("User is banned from this server. Can't insert into the channel") 80 81 except Exception as error: 82 print("Failed to get record from MySQL table: {}".format(error)) 83 finally: 84 cursor.close() 85 return True 86 87 def add_apology(self, user_id, server_name): 88 try: 89 self.add_user(user_id, server_name) 90 cursor = self.conn.connector.cursor() 91 sql_query = "UPDATE discorddb.user_activity SET apology_count = apology_count+1 WHERE user_id = %s AND server_name = %s" 92 val = (user_id, server_name) 93 cursor.execute(sql_query, val) 94 sql_query = "UPDATE discorddb.user_activity SET offense_count = CASE WHEN offense_count < apology_count THEN apology_count ELSE offense_count END WHERE user_id = %s AND server_name = %s" 95 cursor.execute(sql_query, val) 96 self.conn.connector.commit() 97 except Exception as error: 98 print("Failed to get record from MySQL table: {}".format(error)) 99 finally: 100 cursor.close() 101 return True 102 103 def ban_user(self, user_id, server_name, is_banned): 104 try: 105 self.add_user(user_id, server_name) 106 cursor = self.conn.connector.cursor() 107 sql_query = "UPDATE discorddb.user_activity SET is_banned = %s WHERE user_id = %s AND server_name = %s" 108 val = (is_banned, user_id, server_name) 109 cursor.execute(sql_query, val) 110 self.conn.connector.commit() 111 except Exception as error: 112 print("Failed to get record from MySQL table: {}".format(error)) 113 finally: 114 cursor.close() 115 return True
An Abstract Class for all Checker Classes. It is inherited by all the Checker objects
def
check_message(self, message):
11 def check_message(self, message): 12 """ 13 Just checks for Sorry message 14 :param message: message string 15 :return:True or False 16 """ 17 return message.find("sorry") != -1
Just checks for Sorry message
Parameters
- message: message string
Returns
True or False
def
check_user_for_warning(self, user_id, server_name):
19 def check_user_for_warning(self, user_id, server_name): 20 """ 21 Function to check whether a user has any outstanding warnings 22 """ 23 try: 24 self.add_user(user_id, server_name) 25 out = 0 26 cursor = self.conn.connector.cursor() 27 sql_query = "SELECT offense_count - apology_count FROM discorddb.user_activity WHERE user_id = %s AND server_name = %s" 28 cursor.execute(sql_query, (user_id, server_name)) 29 result = cursor.fetchone() 30 out = result[0] 31 except Exception as error: 32 print("Failed to get record from MySQL table: {}".format(error)) 33 finally: 34 cursor.close() 35 if out > 0: 36 print("Offenses more than expected. Banning User") 37 self.ban_user(user_id, server_name, 1) 38 return False 39 40 self.ban_user(user_id, server_name, 0) 41 return True
Function to check whether a user has any outstanding warnings
def
add_warning(self, user_id, server_name):
43 def add_warning(self, user_id, server_name): 44 try: 45 self.add_user(user_id, server_name) 46 cursor = self.conn.connector.cursor() 47 sql_query = "UPDATE discorddb.user_activity SET offense_count = offense_count+1 WHERE user_id = %s AND server_name = %s" 48 49 val = (user_id, server_name) 50 cursor.execute(sql_query, val) 51 self.conn.connector.commit() 52 except Exception as error: 53 print("Failed to get record from MySQL table: {}".format(error)) 54 finally: 55 cursor.close() 56 return True
def
add_user(self, user_id, server_name):
58 def add_user(self, user_id, server_name): 59 """ 60 Function to add user onto the database. If already banned, can't add user. 61 :param user_id: 62 :param server_name: 63 :return: 64 """ 65 try: 66 cursor = self.conn.connector.cursor() 67 sql_query = "SELECT is_banned FROM discorddb.user_activity WHERE user_id = %s AND server_name = %s" 68 cursor.execute(sql_query, (user_id, server_name)) 69 result = cursor.fetchone() 70 if not result: 71 print("User not present in the channel.") 72 sql_query = "INSERT INTO discorddb.user_activity (user_id, server_name) VALUES (%s,%s)" 73 val = (user_id, server_name) 74 cursor.execute(sql_query, val) 75 self.conn.connector.commit() 76 print("User is added into the channel") 77 else: 78 if result[0] == 1: 79 print("User is banned from this server. Can't insert into the channel") 80 81 except Exception as error: 82 print("Failed to get record from MySQL table: {}".format(error)) 83 finally: 84 cursor.close() 85 return True
Function to add user onto the database. If already banned, can't add user.
Parameters
- user_id:
- server_name:
Returns
def
add_apology(self, user_id, server_name):
87 def add_apology(self, user_id, server_name): 88 try: 89 self.add_user(user_id, server_name) 90 cursor = self.conn.connector.cursor() 91 sql_query = "UPDATE discorddb.user_activity SET apology_count = apology_count+1 WHERE user_id = %s AND server_name = %s" 92 val = (user_id, server_name) 93 cursor.execute(sql_query, val) 94 sql_query = "UPDATE discorddb.user_activity SET offense_count = CASE WHEN offense_count < apology_count THEN apology_count ELSE offense_count END WHERE user_id = %s AND server_name = %s" 95 cursor.execute(sql_query, val) 96 self.conn.connector.commit() 97 except Exception as error: 98 print("Failed to get record from MySQL table: {}".format(error)) 99 finally: 100 cursor.close() 101 return True
def
ban_user(self, user_id, server_name, is_banned):
103 def ban_user(self, user_id, server_name, is_banned): 104 try: 105 self.add_user(user_id, server_name) 106 cursor = self.conn.connector.cursor() 107 sql_query = "UPDATE discorddb.user_activity SET is_banned = %s WHERE user_id = %s AND server_name = %s" 108 val = (is_banned, user_id, server_name) 109 cursor.execute(sql_query, val) 110 self.conn.connector.commit() 111 except Exception as error: 112 print("Failed to get record from MySQL table: {}".format(error)) 113 finally: 114 cursor.close() 115 return True