breadtube-bot/breadtube_bot/unidecode.py
BreadTube d015927861 Special characters handling
* Fix README for python version
* Add discord-friendly unidecode function for channel names (avoiding
  special characted)
* Check if "items" is present before accessing in channel id request
  response
2026-02-03 01:30:41 +09:00

19 lines
543 B
Python

import re
from breadtube_bot.unidecode_data import UNIDECODE_DICT
DISCORD_PATTERN = re.compile(r'[^a-zA-Z0-9\-_]')
def unidecode(text: str) -> str:
result: list[str] = []
for char in text:
codepoint: int = ord(char)
if codepoint < 0x80: # noqa: PLR2004
result.append(DISCORD_PATTERN.sub('_', char).lower())
elif codepoint >> 8 in UNIDECODE_DICT:
result.append(UNIDECODE_DICT[codepoint >> 8][codepoint % 256])
else:
result.append('_')
return ''.join(result)