def _db_try_update_device(device: Device) -> bool: key = util.db_convert_text(device.key) checksum = util.db_convert_text(device.checksum) can_login_until = util.db_convert_timestamp(device.can_login_until) where = util.db_get_where_string('key', device.key, is_text_type=True) query = f'UPDATE devices SET (key, checksum, loginuntil) = ({key}, {checksum}, {can_login_until}) WHERE {where}' success = core.db_try_execute(query) return success
def _db_try_create_device(device: Device) -> bool: db_key = util.db_convert_text(device.key) db_checksum = util.db_convert_text(device.checksum) db_login_until = util.db_convert_timestamp(device.can_login_until) query = f'INSERT INTO devices VALUES ({db_key}, {db_checksum}, {db_login_until})' success = core.db_try_execute(query) if success: DEVICES.add_device_by_key(device.key) return success
def db_update_autodaily_settings( guild_id: int, channel_id: int = None, can_post: bool = None, latest_message_id: int = None, delete_on_change: bool = None, notify_id: int = None, notify_type: AutoDailyNotifyType = None, latest_message_modify_date: datetime.datetime = None) -> bool: autodaily_settings = db_get_autodaily_settings(guild_id) current_can_post = None current_channel_id = None current_latest_message_id = None current_delete_on_change = None current_notify_id = None current_notify_type = None if autodaily_settings: (_, current_channel_id, current_can_post, current_latest_message_id, current_delete_on_change, current_notify_id, current_notify_type, current_latest_message_create_date, current_latest_message_modify_date) = autodaily_settings[0] if (current_channel_id != channel_id or current_latest_message_id != latest_message_id or current_can_post != can_post or current_delete_on_change != delete_on_change or current_notify_id != notify_id or current_notify_type != notify_type or latest_message_modify_date and (current_latest_message_create_date is None or current_latest_message_modify_date != latest_message_modify_date)): settings = {} if channel_id is not None: settings['dailychannelid'] = util.db_convert_text(channel_id) if can_post is not None: settings['dailycanpost'] = util.db_convert_to_boolean(can_post) if latest_message_id is not None: settings['dailylatestmessageid'] = util.db_convert_text( latest_message_id) if delete_on_change is not None: settings['dailydeleteonchange'] = util.db_convert_to_boolean( delete_on_change) if notify_id is not None: settings['dailynotifyid'] = util.db_convert_text(notify_id) if notify_type is not None: settings['dailynotifytype'] = util.db_convert_text( convert_from_autodaily_notify_type(notify_type)) if latest_message_modify_date is not None: settings[ 'dailylatestmessagemodifydate'] = util.db_convert_timestamp( latest_message_modify_date) if current_latest_message_create_date is None: settings['dailylatestmessagecreatedate'] = settings[ 'dailylatestmessagemodifydate'] success = not settings or _db_update_server_setting(guild_id, settings) return success return True
def db_update_daily_notify_settings(guild_id: int, notify_id: int, notify_type: AutoDailyNotifyType) -> bool: current_daily_notify_id, _ = db_get_daily_notify_settings(guild_id) if not current_daily_notify_id or notify_id != current_daily_notify_id: settings = { 'dailynotifyid': util.db_convert_text(notify_id), 'dailynotifytype': util.db_convert_text( convert_from_autodaily_notify_type(notify_type)) } success = _db_update_server_setting(guild_id, settings) return success return True
def db_set_setting(setting_name: str, value: object, utc_now: datetime = None) -> bool: column_name = None if isinstance(value, bool): db_value = util.db_convert_boolean(value) column_name = 'settingboolean' elif isinstance(value, int): db_value = util.db_convert_to_int(value) column_name = 'settingint' elif isinstance(value, float): db_value = util.db_convert_to_float(value) column_name = 'settingfloat' elif isinstance(value, datetime): db_value = util.db_convert_to_datetime(value) column_name = 'settingtimestamptz' else: db_value = util.db_convert_text(value) column_name = 'settingtext' setting, modify_date = db_get_setting(setting_name) if utc_now is None: utc_now = util.get_utcnow() modify_date = util.db_convert_timestamp(utc_now) query = '' if setting is None and modify_date is None: query = f'INSERT INTO settings (settingname, modifydate, {column_name}) VALUES ({util.db_convert_text(setting_name)}, {modify_date}, {db_value})' elif setting != value: where_string = util.db_get_where_string('settingname', setting_name, is_text_type=True) query = f'UPDATE settings SET {column_name} = {db_value}, modifydate = {modify_date} WHERE {where_string}' success = not query or db_try_execute(query) return success
def db_update_prefix(guild_id: int, prefix: str) -> bool: current_prefix = db_get_prefix(guild_id) if not current_prefix or prefix != current_prefix: settings = {'prefix': util.db_convert_text(prefix)} success = _db_update_server_setting(guild_id, settings) return success return True
def db_update_daily_latest_message(guild_id: int, message: discord.Message) -> bool: if message: current_daily_latest_message_id = db_get_daily_latest_message_id( guild_id) current_daily_latest_message_create_date = db_get_daily_latest_message_create_date( guild_id) new_day = message.created_at.day != current_daily_latest_message_create_date.day if new_day: modify_date = message.created_at else: modify_date = message.edited_at or message.created_at if not current_daily_latest_message_id or message.id != current_daily_latest_message_id: settings = { 'dailylatestmessageid': util.db_convert_text(message.id), 'dailylatestmessagemodifydate': util.db_convert_timestamp(modify_date) } if new_day: settings[ 'dailylatestmessagecreatedate'] = util.db_convert_timestamp( message.created_at) success = _db_update_server_setting(guild_id, settings) return success else: success = db_reset_autodaily_latest_message_id(guild_id) return success return True
async def set_settings(settings: Dict[str, Tuple[object, datetime]]) -> bool: __log_db_function_enter('set_settings', settings=settings) utc_now = util.get_utcnow() if settings: query_lines = [] args = [] success = True current_settings = await get_settings(settings.keys()) for setting_name, (value, modified_at) in settings.items(): query = '' column_name = None if isinstance(value, bool): column_name = 'settingboolean' value = util.db_convert_boolean(value) elif isinstance(value, int): column_name = 'settingint' elif isinstance(value, float): column_name = 'settingfloat' elif isinstance(value, datetime): column_name = 'settingtimestamptz' value = util.db_convert_timestamp(value) else: column_name = 'settingtext' value = util.db_convert_text(value) current_value, db_modify_date = current_settings[setting_name] modify_date = db_modify_date or utc_now setting_name = util.db_convert_text(setting_name) if current_value is None and db_modify_date is None: query = f'INSERT INTO settings ({column_name}, modifydate, settingname) VALUES ({value}, \'{modified_at}\', {setting_name});' elif current_value != value: query = f'UPDATE settings SET {column_name} = {value}, modifydate = \'{modified_at}\' WHERE settingname = {setting_name};' if query: query_lines.append(query) args.extend([value, modified_at, setting_name]) success = not query_lines or await try_execute('\n'.join(query_lines)) if success: __settings_cache.update(settings) return success else: return True
def db_update_daily_channel_id(guild_id: int, channel_id: int) -> bool: current_daily_channel_id = db_get_daily_channel_id(guild_id) if not current_daily_channel_id or channel_id != current_daily_channel_id: settings = { 'dailychannelid': util.db_convert_text(channel_id), 'dailylatestmessageid': 'NULL' } success = _db_update_server_setting(guild_id, settings) return success return True
def db_get_autodaily_settings(guild_id: int = None, can_post: bool = None, only_guild_ids: bool = False, no_post_yet: bool = False) -> list: wheres = ['(dailychannelid IS NOT NULL or dailynotifyid IS NOT NULL)'] if guild_id is not None: wheres.append( util.db_get_where_string('guildid', util.db_convert_text(str(guild_id)))) if can_post is not None: wheres.append( util.db_get_where_string('dailycanpost', util.db_convert_boolean(can_post))) if no_post_yet is True: wheres.append( util.db_get_where_string('dailylatestmessagecreatedate', None)) if only_guild_ids: setting_names = ['guildid'] else: setting_names = [ 'guildid', 'dailychannelid', 'dailycanpost', 'dailylatestmessageid', 'dailydeleteonchange', 'dailynotifyid', 'dailynotifytype', 'dailylatestmessagecreatedate', 'dailylatestmessagemodifydate' ] settings = _db_get_server_settings(guild_id, setting_names=setting_names, additional_wheres=wheres) result = [] for setting in settings: if only_guild_ids: intermediate = [util.db_convert_to_int(setting[0])] intermediate.extend([None] * (__AUTODAILY_SETTING_COUNT - 1)) result.append(tuple(intermediate)) else: result.append((util.db_convert_to_int(setting[0]), util.db_convert_to_int(setting[1]), util.db_convert_to_boolean(setting[2]), util.db_convert_to_int(setting[3]), util.db_convert_to_boolean(setting[4]), util.db_convert_to_int(setting[5]), convert_to_autodaily_notify_type(setting[6]), setting[7], setting[8])) if not result: if guild_id: intermediate = [guild_id] intermediate.extend([None] * (__AUTODAILY_SETTING_COUNT - 1)) result.append(tuple(intermediate)) else: result.append(tuple([None] * __AUTODAILY_SETTING_COUNT)) return result
def db_try_set_schema_version(version: str) -> bool: prior_version = db_get_schema_version() utc_now = util.get_utcnow() modify_date_for_db = util.db_convert_timestamp(utc_now) version_for_db = util.db_convert_text(version) if prior_version == '': query = f'INSERT INTO settings (settingname, modifydate, settingtext) VALUES (\'schema_version\', {modify_date_for_db}, {version_for_db})' else: where_string = util.db_get_where_string('settingname', 'schema_version', is_text_type=True) query = f'UPDATE settings SET settingtext = {version_for_db}, modifydate = {modify_date_for_db} WHERE {where_string}' success = db_try_execute(query) return success