Skip to content

Commit e787c0f

Browse files
author
Eviee Py
committed
Add bool converter
1 parent f79b783 commit e787c0f

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

twitchio/ext/commands/converters.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,35 @@
3737

3838
__all__ = ("_BaseConverter",)
3939

40+
_BOOL_MAPPING: dict[str, bool] = {
41+
"true": True,
42+
"false": False,
43+
"t": True,
44+
"f": False,
45+
"1": True,
46+
"0": False,
47+
"y": True,
48+
"n": False,
49+
"yes": True,
50+
"no": False,
51+
}
52+
4053

4154
class _BaseConverter:
4255
def __init__(self, client: Bot) -> None:
4356
self.__client: Bot = client
4457

4558
self._MAPPING: dict[Any, Any] = {User: self._user}
46-
self._DEFAULTS: dict[type, type] = {str: str, int: int, float: float}
59+
self._DEFAULTS: dict[type, Any] = {str: str, int: int, float: float, bool: self._bool, type(None): type(None)}
60+
61+
def _bool(self, arg: str) -> bool:
62+
try:
63+
result = _BOOL_MAPPING[arg.lower()]
64+
except KeyError:
65+
pretty: str = " | ".join(f'"{k}"' for k in _BOOL_MAPPING)
66+
raise BadArgument(f'Failed to convert "{arg}" to type bool. Expected any: [{pretty}]', value=arg)
67+
68+
return result
4769

4870
async def _user(self, context: Context, arg: str) -> User:
4971
arg = arg.lower()

0 commit comments

Comments
 (0)