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
This commit is contained in:
BreadTube 2026-02-03 01:30:41 +09:00
commit d015927861
10 changed files with 315 additions and 13 deletions

View file

@ -0,0 +1,19 @@
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)