Created
May 31, 2025 21:43
-
-
Save i-am-unknown-81514525/9fd2269897ec58428165f9071105131f to your computer and use it in GitHub Desktop.
d.py related: compare local slash command against discord server record
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # !!! Any check checks for they are not the same | |
| from typing import Mapping | |
| class __X(type):... | |
| class MISSING(__X, metaclass=__X): | |
| def __new__(cls, *args, **kwargs): | |
| return cls | |
| def __init__(self, *args, **kwargs):... | |
| MISSING.__class__ = MISSING | |
| del __X | |
| class DefaultComparsion[T]: | |
| def __init__(self, defaults: tuple[...], check: Callable[[T, T], bool] = lambda x, y: x != y): | |
| self.defaults = defaults | |
| self.callback = check | |
| def _check_default(self, left, right) -> bool: | |
| return left not in self.defaults or right not in self.defaults | |
| def check(self, left, right): | |
| return self._check_default(left, right) and self.callback(left, right) | |
| class NoDefaultComparsion[T](DefaultComparsion[T]): | |
| def __init__(self, check: Callable[[T, T], bool] = lambda x, y: x != y): | |
| super().__init__((), check) | |
| class DefaultDictComparsion[T](DefaultComparsion[T]): | |
| def __init__(self, defaults: tuple[...], schema: Mapping[str, DefaultComparsion], check: Callable[[T, T], bool] = lambda x, y: x != y): | |
| super().__init__(defaults, check) | |
| self.schema = schema | |
| def check(self, left, right): | |
| if not super().check(left, right): # If they are the same by default value | |
| return False | |
| if not isinstance(left, Mapping) and not isinstance(right, Mapping): | |
| return True | |
| for field, comp in schema: | |
| left_value = left.get(field, MISSING) | |
| right_value = right.get(field, MISSING) | |
| if comp.check(left_value, right_value): | |
| return True | |
| return False | |
| class DefaultListComparsion[T](DefaultComparsion[T]): | |
| def __init__(self, defaults: tuple[...], schema: DefaultComparsion, check: Callable[[T, T], bool] = lambda x, y: x != y): | |
| super().__init__(defaults, check) | |
| self.schema = schema | |
| def check(self, left, right): | |
| if not super().check(left, right): # If they are the same by default value | |
| return False | |
| if not isinstance(left, list) and not isinstance(right, list): | |
| return True | |
| left_clone = left.copy() | |
| right_clone = right.copy() | |
| for left_idx, left_item in enumerate(left_clone): | |
| for right_idx, right_item in enumerate(right_clone): | |
| if not schema.callback(left_item, right_item): | |
| right_clone.pop(right_idx) | |
| break | |
| else: | |
| for right_idx, right_item in enumerate(right_clone): | |
| if not schema._check_default(left_item, right_item): | |
| right_clone.pop(right_idx) | |
| break | |
| else: | |
| if right_clone: | |
| return True # No valid match from left to right when right still have item | |
| else: | |
| if left_item not in schema.defaults: | |
| return True # No valid match from left to right when left is not default | |
| return any(right_item not in schema.defaults for right_item in right_clone) | |
| class MultiSchemaComparsion[T](DefaultComparsion[T]): | |
| def __init__(self, schemas: list[DefaultComparsion], check: Callable[[T, T], bool] = lambda x, y: x != y): | |
| super().__init__((), check) | |
| self.schemas = list(schemas) | |
| def check(self, left, right): | |
| for schema in schemas: | |
| if not schema.check(left, right): | |
| return False | |
| return True | |
| locale_key = x[ | |
| 'id', 'da', 'de', 'en-GB', 'en-US', 'es-ES', 'es-419', 'fr', 'hr', 'it', | |
| 'lt', 'hu', 'nl', 'no', 'pl', 'pt-BR', 'ro', 'fi', 'sv-SE', 'vi', | |
| 'tr', 'cs', 'el', 'bg', 'ru', 'uk', 'hi', 'th', 'zh-CN', 'ja', | |
| 'zh-TW', 'ko' | |
| ] | |
| recur_opt = DefaultDictComparsion((), {}) # pending | |
| slash_cmd_opt = ( | |
| DefaultDictComparsion( | |
| (MISSING, None), | |
| { | |
| "type": NoDefaultComparsion(), | |
| "name": NoDefaultComparsion(), | |
| "name_localizations": DefaultDictComparsion( | |
| (None, MISSING, {}), | |
| { | |
| k: DefaultComparsion((MISSING,)) | |
| for k in locale_key | |
| } | |
| ), | |
| "description": NoDefaultComparsion(), | |
| "description_localizations": DefaultDictComparsion( | |
| (None, MISSING, {}), | |
| { | |
| k: DefaultComparsion((MISSING,)) | |
| for k in locale_key | |
| } | |
| ), | |
| "required": DefaultComparsion((MISSING, False)), | |
| "choices": DefaultListComparsion( | |
| (MISSING, [], None), | |
| DefaultDictComparsion( | |
| { | |
| "name": NoDefaultComparsion(), | |
| "name_localizations": DefaultDictComparsion( | |
| (None, MISSING, {}), | |
| { | |
| k: DefaultComparsion((MISSING,)) | |
| for k in locale_key | |
| } | |
| ), | |
| "value": NoDefaultComparsion() | |
| } | |
| ) | |
| ), | |
| "options": DefaultListComparsion( | |
| (MISSING, None, []), | |
| recur_opt | |
| ), | |
| "channel_types": DefaultListComparsion( | |
| (MISSING, None, []), | |
| NoDefaultComparsion() | |
| ), | |
| "min_value": DefaultComparsion((None, MISSING)), | |
| "max_value": DefaultComparsion((None, MISSING)), | |
| "min_length": DefaultComparsion((None, MISSING, 0)), | |
| "max_length": DefaultComparsion((None, MISSING, 6000)), | |
| "autocomplete": DefaultComparsion((None, MISSING, False)), | |
| } | |
| ) | |
| ) | |
| recur_opt.schema = slash_cmd_opt | |
| slash_cmd = DefaultDictComparsion( | |
| (), | |
| { | |
| # Skipped id as it is not known on local | |
| "type": DefaultComparsion((MISSING, 1, None)), | |
| "guild_id": DefaultComparsion((MISSING, None)), | |
| "name": NoDefaultComparsion(), | |
| "name_localizations": DefaultDictComparsion( | |
| (None, MISSING, {}), | |
| { | |
| k: DefaultComparsion((MISSING,)) | |
| for k in locale_key | |
| } | |
| ), | |
| "description": NoDefaultComparsion(), | |
| "description_localizations": DefaultDictComparsion( | |
| (None, MISSING, {}), | |
| { | |
| k: DefaultComparsion((MISSING,)) | |
| for k in locale_key | |
| } | |
| ), | |
| "options": DefaultListComparsion( | |
| (MISSING, [], None), | |
| slash_cmd_opt | |
| ), | |
| "default_member_permissions": DefaultComparsion(("", MISSING, None)), | |
| "dm_permission": DefaultComparsion((True, MISSING, None)), | |
| "default_permission": DefaultComparsion((True, MISSING, None)), | |
| "nsfw": DefaultComparsion((False, MISSING, None)), | |
| "integration_types": DefaultListComparsion( | |
| (None, MISSING, []), | |
| NoDefaultComparsion() | |
| ), | |
| "contexts": DefaultListComparsion( | |
| (None, MISSING, []), | |
| NoDefaultComparsion() | |
| ), | |
| # Skipped version as it might not be known by local | |
| "handler": NoDefaultComparsion() | |
| } | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment