|
| 1 | +import re |
| 2 | + |
| 3 | +from abc import ABC, abstractmethod |
| 4 | +from contextlib import suppress |
| 5 | +from typing import TypeVar, Sequence, Generic, Type, Optional, TYPE_CHECKING, List |
| 6 | + |
| 7 | +import dico |
| 8 | +from dico.exception import HTTPError |
| 9 | + |
| 10 | +from .utils import search, maybe_fmt |
| 11 | + |
| 12 | +if TYPE_CHECKING: |
| 13 | + from .bot import Bot |
| 14 | + from .context import Context |
| 15 | + |
| 16 | + |
| 17 | +T = TypeVar("T") |
| 18 | + |
| 19 | + |
| 20 | +class ConverterBase(ABC, Generic[T]): |
| 21 | + CONVERT_TYPE: Type[T] |
| 22 | + |
| 23 | + def __init__(self, bot: "Bot"): |
| 24 | + if not hasattr(self, "CONVERT_TYPE"): |
| 25 | + raise TypeError("Converter must have CONVERT_TYPE attribute") |
| 26 | + self.bot: "Bot" = bot |
| 27 | + self.cache_type = self.CONVERT_TYPE._cache_type if hasattr(self.CONVERT_TYPE, "_cache_type") else None |
| 28 | + |
| 29 | + def dump_from_cache(self, guild_id: Optional[dico.Snowflake] = None) -> List[T]: |
| 30 | + if not self.cache_type: |
| 31 | + raise TypeError("dump_from_cache can only be used with DiscordObjectBase") |
| 32 | + if self.bot.has_cache: |
| 33 | + cache = self.bot.cache if not guild_id else self.bot.cache.get_guild_container(guild_id) |
| 34 | + objects = cache.get_storage(self.cache_type) if cache else [] |
| 35 | + return [x["value"] for x in objects] if objects else [] |
| 36 | + return [] |
| 37 | + |
| 38 | + def __call__(self, *args, **kwargs): |
| 39 | + return self.convert(*args, **kwargs) |
| 40 | + |
| 41 | + @abstractmethod |
| 42 | + async def convert(self, ctx: "Context", value: str) -> Optional[T]: |
| 43 | + pass |
| 44 | + |
| 45 | + |
| 46 | +class UserConverter(ConverterBase): |
| 47 | + CONVERT_TYPE = dico.User |
| 48 | + |
| 49 | + async def convert(self, ctx: "Context", value: str) -> Optional[T]: |
| 50 | + cached = self.dump_from_cache() |
| 51 | + cached.extend([x.user if isinstance(x, dico.GuildMember) else x for x in ctx.mentions]) |
| 52 | + maybe_mention = maybe_fmt(value) |
| 53 | + maybe_id = value if re.match(r"^\d+$", value) else maybe_mention |
| 54 | + with suppress(HTTPError): |
| 55 | + if maybe_id: |
| 56 | + return search(cached, id=maybe_id) or await self.bot.http.request_user(maybe_id) |
| 57 | + from_username = search(cached, username=value) |
| 58 | + if from_username: |
| 59 | + return from_username |
| 60 | + from_fullname = search(cached, __str__=value) |
| 61 | + if from_fullname: |
| 62 | + return from_fullname |
| 63 | + |
| 64 | + |
| 65 | +class GuildMemberConverter(ConverterBase): |
| 66 | + CONVERT_TYPE =dico. GuildMember |
| 67 | + |
| 68 | + def __init__(self, bot: "Bot"): |
| 69 | + super().__init__(bot) |
| 70 | + self.cache_type = "member" |
| 71 | + |
| 72 | + async def convert(self, ctx: "Context", value: str) -> Optional[T]: |
| 73 | + cached = self.dump_from_cache(ctx.guild_id) |
| 74 | + cached.extend([x for x in ctx.mentions if isinstance(x, dico.GuildMember)]) |
| 75 | + maybe_mention = maybe_fmt(value) |
| 76 | + maybe_id = value if re.match(r"^\d+$", value) else maybe_mention |
| 77 | + with suppress(HTTPError): |
| 78 | + if maybe_id: |
| 79 | + return search(cached, id=maybe_id) or await self.bot.http.request_user(maybe_id) |
| 80 | + from_name = search(cached, __str__=value) |
| 81 | + if from_name: |
| 82 | + return from_name |
| 83 | + |
| 84 | + |
| 85 | +class ChannelConverter(ConverterBase): |
| 86 | + CONVERT_TYPE = dico.Channel |
| 87 | + |
| 88 | + async def convert(self, ctx: "Context", value: str) -> Optional[T]: |
| 89 | + cached = self.dump_from_cache() |
| 90 | + cached.append(ctx.channel) |
| 91 | + maybe_mention = maybe_fmt(value) |
| 92 | + maybe_id = value if re.match(r"^\d+$", value) else maybe_mention |
| 93 | + with suppress(HTTPError): |
| 94 | + if maybe_id: |
| 95 | + return search(cached, id=maybe_id) or await self.bot.http.request_user(maybe_id) |
| 96 | + from_name = search(cached, name=value) |
| 97 | + if from_name: |
| 98 | + return from_name |
| 99 | + |
| 100 | + |
| 101 | +class RoleConverter(ConverterBase): |
| 102 | + CONVERT_TYPE = dico.Role |
| 103 | + |
| 104 | + async def convert(self, ctx: "Context", value: str) -> Optional[T]: |
| 105 | + cached = self.dump_from_cache() |
| 106 | + maybe_mention = maybe_fmt(value) |
| 107 | + maybe_id = value if re.match(r"^\d+$", value) else maybe_mention |
| 108 | + with suppress(HTTPError): |
| 109 | + if maybe_id: |
| 110 | + return search(cached, id=maybe_id) or await self.bot.http.request_user(maybe_id) |
| 111 | + from_name = search(cached, name=value) |
| 112 | + if from_name: |
| 113 | + return from_name |
| 114 | + |
| 115 | + |
| 116 | +AVAILABLE_CONVERTERS = { |
| 117 | + dico.User: UserConverter, |
| 118 | + dico.GuildMember: GuildMemberConverter, |
| 119 | + dico.Channel: ChannelConverter, |
| 120 | + dico.Role: RoleConverter |
| 121 | +} |
0 commit comments