extract raise_expected_coro further
This commit is contained in:
parent
1829048c80
commit
73a4996aa9
@ -22,6 +22,7 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
"""
|
||||
|
||||
from TwitchIO.twitchio.ext.commands import utils
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
@ -43,6 +44,7 @@ from .context import Context
|
||||
from . import errors
|
||||
from .help import HelpCommand, DefaultHelpCommand
|
||||
from .cog import Cog
|
||||
from discord.utils import raise_expected_coro
|
||||
|
||||
if TYPE_CHECKING:
|
||||
import importlib.machinery
|
||||
@ -379,7 +381,7 @@ class BotBase(GroupMixin):
|
||||
TypeError
|
||||
The coroutine passed is not actually a coroutine.
|
||||
"""
|
||||
return self._raise_expected_coro(
|
||||
return raise_expected_coro(
|
||||
coro, 'The pre-invoke hook must be a coroutine.'
|
||||
)
|
||||
|
||||
@ -410,17 +412,11 @@ class BotBase(GroupMixin):
|
||||
TypeError
|
||||
The coroutine passed is not actually a coroutine.
|
||||
"""
|
||||
return self._raise_expected_coro(
|
||||
return raise_expected_coro(
|
||||
coro, 'The post-invoke hook must be a coroutine.'
|
||||
)
|
||||
|
||||
|
||||
def _raise_expected_coro(self, coro, arg1):
|
||||
if not asyncio.iscoroutinefunction(coro):
|
||||
raise TypeError(arg1)
|
||||
|
||||
return coro
|
||||
|
||||
# listener registration
|
||||
|
||||
def add_listener(self, func: CoroFunc, name: str = MISSING) -> None:
|
||||
|
@ -46,7 +46,9 @@ import traceback
|
||||
|
||||
from collections.abc import Sequence
|
||||
from discord.backoff import ExponentialBackoff
|
||||
from discord.utils import MISSING
|
||||
from discord.utils import MISSING, raise_expected_coro
|
||||
#from discord.utils import raise_expected_coro
|
||||
|
||||
|
||||
__all__ = (
|
||||
'loop',
|
||||
@ -488,7 +490,7 @@ class Loop(Generic[LF]):
|
||||
The function was not a coroutine.
|
||||
"""
|
||||
|
||||
return self._raise_expected_coro(coro)
|
||||
return raise_expected_coro(coro, f'Expected coroutine function, received {coro.__class__.__name__!r}.')
|
||||
|
||||
def after_loop(self, coro: FT) -> FT:
|
||||
"""A decorator that register a coroutine to be called after the loop finished running.
|
||||
@ -512,7 +514,7 @@ class Loop(Generic[LF]):
|
||||
The function was not a coroutine.
|
||||
"""
|
||||
|
||||
return self._raise_expected_coro(coro)
|
||||
return raise_expected_coro(coro, f'Expected coroutine function, received {coro.__class__.__name__!r}.')
|
||||
|
||||
def error(self, coro: ET) -> ET:
|
||||
"""A decorator that registers a coroutine to be called if the task encounters an unhandled exception.
|
||||
@ -534,15 +536,7 @@ class Loop(Generic[LF]):
|
||||
TypeError
|
||||
The function was not a coroutine.
|
||||
"""
|
||||
return self._raise_expected_coro(coro)
|
||||
|
||||
def _raise_expected_coro(self, coro):
|
||||
if not inspect.iscoroutinefunction(coro):
|
||||
raise TypeError(
|
||||
f'Expected coroutine function, received {coro.__class__.__name__!r}.'
|
||||
)
|
||||
|
||||
return coro
|
||||
return raise_expected_coro(coro, f'Expected coroutine function, received {coro.__class__.__name__!r}.')
|
||||
|
||||
def _get_next_sleep_time(self) -> datetime.datetime:
|
||||
if self._sleep is not MISSING:
|
||||
|
@ -499,14 +499,14 @@ else:
|
||||
|
||||
def _parse_ratelimit_header(request: Any, *, use_clock: bool = False) -> float:
|
||||
reset_after: Optional[str] = request.headers.get('X-Ratelimit-Reset-After')
|
||||
if use_clock or not reset_after:
|
||||
utc = datetime.timezone.utc
|
||||
now = datetime.datetime.now(utc)
|
||||
reset = datetime.datetime.fromtimestamp(float(request.headers['X-Ratelimit-Reset']), utc)
|
||||
return (reset - now).total_seconds()
|
||||
else:
|
||||
if not use_clock and reset_after:
|
||||
return float(reset_after)
|
||||
|
||||
utc = datetime.timezone.utc
|
||||
now = datetime.datetime.now(utc)
|
||||
reset = datetime.datetime.fromtimestamp(float(request.headers['X-Ratelimit-Reset']), utc)
|
||||
return (reset - now).total_seconds()
|
||||
|
||||
|
||||
async def maybe_coroutine(f, *args, **kwargs):
|
||||
value = f(*args, **kwargs)
|
||||
@ -659,11 +659,10 @@ def resolve_invite(invite: Union[Invite, str]) -> str:
|
||||
|
||||
if isinstance(invite, Invite):
|
||||
return invite.code
|
||||
else:
|
||||
rx = r'(?:https?\:\/\/)?discord(?:\.gg|(?:app)?\.com\/invite)\/(.+)'
|
||||
m = re.match(rx, invite)
|
||||
if m:
|
||||
return m.group(1)
|
||||
rx = r'(?:https?\:\/\/)?discord(?:\.gg|(?:app)?\.com\/invite)\/(.+)'
|
||||
m = re.match(rx, invite)
|
||||
if m:
|
||||
return m.group(1)
|
||||
return invite
|
||||
|
||||
|
||||
@ -687,11 +686,10 @@ def resolve_template(code: Union[Template, str]) -> str:
|
||||
|
||||
if isinstance(code, Template):
|
||||
return code.code
|
||||
else:
|
||||
rx = r'(?:https?\:\/\/)?discord(?:\.new|(?:app)?\.com\/template)\/(.+)'
|
||||
m = re.match(rx, code)
|
||||
if m:
|
||||
return m.group(1)
|
||||
rx = r'(?:https?\:\/\/)?discord(?:\.new|(?:app)?\.com\/template)\/(.+)'
|
||||
m = re.match(rx, code)
|
||||
if m:
|
||||
return m.group(1)
|
||||
return code
|
||||
|
||||
|
||||
@ -1017,3 +1015,9 @@ def format_dt(dt: datetime.datetime, /, style: Optional[TimestampStyle] = None)
|
||||
if style is None:
|
||||
return f'<t:{int(dt.timestamp())}>'
|
||||
return f'<t:{int(dt.timestamp())}:{style}>'
|
||||
|
||||
|
||||
def raise_expected_coro(coro, error: str)-> TypeError:
|
||||
if not asyncio.iscoroutinefunction(coro):
|
||||
raise TypeError(error)
|
||||
return coro
|
Loading…
x
Reference in New Issue
Block a user