Esempio n. 1
0
class MQBot(
        hikari.traits.RESTAware,
        hikari.traits.Runnable,
        hikari.traits.ShardAware,
        hikari.traits.EventFactoryAware,
        hikari.traits.EventManagerAware,
        hikari.api.EventManager,
):
    __slots__ = (
        "_entity_factory",
        "_event_factory",
        "_event_manager",
        "_executor",
        "_http_settings",
        "_intents",
        "_is_alive",
        "_is_closing",
        "_join_event",
        "_me",
        "_proxy_settings",
        "_receiver",
        "_rest",
        "_shard_count",
        "_shards",
    )

    def __init__(
        self,
        receiver: _receivers.abc.AbstractReceiver,
        token: str,
        /,
        *,
        http_settings: typing.Optional[hikari.config.HTTPSettings] = None,
        log_level: typing.Union[str, int, None] = logging.INFO,
        max_rate_limit: float = 300.0,
        max_retries: int = 3,
        proxy_settings: typing.Optional[hikari.config.ProxySettings] = None,
        discord_url: typing.Optional[str] = None,
    ) -> None:
        if log_level is not None and not logging.root.handlers:
            logging.basicConfig(
                level=log_level,
                format=
                "%(levelname)-1.1s %(asctime)23.23s %(name)s: %(message)s",
            )

            warnings.simplefilter("default", DeprecationWarning)
            logging.captureWarnings(True)

        # TODO: logging stuff?
        self._intents = hikari.Intents.NONE
        self._is_alive = False
        self._is_closing = False
        self._me: typing.Optional[hikari.OwnUser] = None
        self._receiver = receiver
        self._shards: dict[int, shards_.Shard] = {}

        self._http_settings = http_settings or hikari.config.HTTPSettings()
        self._proxy_settings = proxy_settings or hikari.config.ProxySettings()
        self._shard_count = -1

        self._executor: typing.Optional[futures.Executor] = None

        self._entity_factory = hikari.impl.EntityFactoryImpl(self)
        self._event_factory = event_factory.EventFactoryImpl(self)
        self._event_manager = _event_manager.EventManager(
            self._receiver, self._event_factory, hikari.Intents.ALL)
        self._join_event: typing.Optional[asyncio.Event] = None
        self._rest = hikari.impl.RESTClientImpl(
            cache=None,
            entity_factory=self._entity_factory,
            executor=self._executor,
            http_settings=self._http_settings,
            max_rate_limit=max_rate_limit,
            max_retries=max_retries,
            proxy_settings=self._proxy_settings,
            token=token,
            token_type=hikari.TokenType.BOT,
            rest_url=discord_url,
        )
Esempio n. 2
0
File: bot.py Progetto: Reliku/hikari
    def __init__(
        self,
        token: str,
        *,
        allow_color: bool = True,
        banner: typing.Optional[str] = "hikari",
        chunking_limit: int = 200,
        enable_cache: bool = True,
        max_messages: int = 300,
        executor: typing.Optional[concurrent.futures.Executor] = None,
        force_color: bool = False,
        http_settings: typing.Optional[config.HTTPSettings] = None,
        intents: intents_.Intents = intents_.Intents.ALL_UNPRIVILEGED,
        logs: typing.Union[None, LoggerLevelT,
                           typing.Dict[str, typing.Any]] = "INFO",
        max_rate_limit: float = 300,
        proxy_settings: typing.Optional[config.ProxySettings] = None,
        rest_url: typing.Optional[str] = None,
    ) -> None:
        # Beautification and logging
        ux.init_logging(logs, allow_color, force_color)
        self.print_banner(banner, allow_color, force_color)

        # Settings and state
        self._banner = banner
        self._closing_event = asyncio.Event()
        self._closed = False
        self._executor = executor
        self._http_settings = http_settings if http_settings is not None else config.HTTPSettings(
        )
        self._intents = intents
        self._proxy_settings = proxy_settings if proxy_settings is not None else config.ProxySettings(
        )
        self._token = token

        # Caching, chunking, and event subsystems.
        self._cache: cache.Cache
        self._chunker: chunker.GuildChunker
        self._events: event_dispatcher.EventDispatcher
        events_obj: event_manager_base.EventManagerBase

        if enable_cache:
            from hikari.impl import stateful_cache
            from hikari.impl import stateful_event_manager
            from hikari.impl import stateful_guild_chunker

            cache_obj = stateful_cache.StatefulCacheImpl(
                self, intents, max_messages)
            self._cache = cache_obj
            self._chunker = stateful_guild_chunker.StatefulGuildChunkerImpl(
                self, chunking_limit)

            events_obj = stateful_event_manager.StatefulEventManagerImpl(
                self, cache_obj, intents)
            self._raw_event_consumer = events_obj.consume_raw_event
            self._events = events_obj
        else:
            from hikari.impl import stateless_cache
            from hikari.impl import stateless_event_manager
            from hikari.impl import stateless_guild_chunker

            self._cache = stateless_cache.StatelessCacheImpl()
            self._chunker = stateless_guild_chunker.StatelessGuildChunkerImpl()

            events_obj = stateless_event_manager.StatelessEventManagerImpl(
                self, intents)
            self._raw_event_consumer = events_obj.consume_raw_event
            self._events = events_obj

        # Entity creation
        self._entity_factory = entity_factory_impl.EntityFactoryImpl(self)

        # Event creation
        self._event_factory = event_factory_impl.EventFactoryImpl(self)

        # Voice subsystem
        self._voice = voice_impl.VoiceComponentImpl(self, self._events)

        # RESTful API.
        self._rest = rest_impl.RESTClientImpl(
            connector_factory=rest_impl.BasicLazyCachedTCPConnectorFactory(
                self._http_settings),
            connector_owner=True,
            entity_factory=self._entity_factory,
            executor=self._executor,
            http_settings=self._http_settings,
            max_rate_limit=max_rate_limit,
            proxy_settings=self._proxy_settings,
            rest_url=rest_url,
            token=token,
        )

        # We populate these on startup instead, as we need to possibly make some
        # HTTP requests to determine what to put in this mapping.
        self._shards: typing.Dict[int, shard.GatewayShard] = {}
        self.shards: typing.Mapping[
            int,
            gateway_shard.GatewayShard] = types.MappingProxyType(self._shards)
Esempio n. 3
0
    def __init__(
        self,
        token: str,
        *,
        allow_color: bool = True,
        banner: typing.Optional[str] = "hikari",
        executor: typing.Optional[concurrent.futures.Executor] = None,
        force_color: bool = False,
        cache_settings: typing.Optional[config.CacheSettings] = None,
        http_settings: typing.Optional[config.HTTPSettings] = None,
        intents: intents_.Intents = intents_.Intents.ALL_UNPRIVILEGED,
        logs: typing.Union[None, int, str, typing.Dict[str, typing.Any]] = "INFO",
        max_rate_limit: float = 300,
        proxy_settings: typing.Optional[config.ProxySettings] = None,
        rest_url: typing.Optional[str] = None,
    ) -> None:
        # Beautification and logging
        ux.init_logging(logs, allow_color, force_color)
        self.print_banner(banner, allow_color, force_color)

        # Settings and state
        self._closing_event = asyncio.Event()
        self._closed = False
        self._is_alive = False
        self._executor = executor
        self._http_settings = http_settings if http_settings is not None else config.HTTPSettings()
        self._intents = intents
        self._proxy_settings = proxy_settings if proxy_settings is not None else config.ProxySettings()
        self._token = token

        # Caching
        cache_settings = cache_settings if cache_settings is not None else config.CacheSettings()
        self._cache = cache_impl.CacheImpl(self, cache_settings)

        # Event handling
        self._events = event_manager_impl.EventManagerImpl(self, cache=self._cache)

        # Entity creation
        self._entity_factory = entity_factory_impl.EntityFactoryImpl(self)

        # Event creation
        self._event_factory = event_factory_impl.EventFactoryImpl(self)

        # Voice subsystem
        self._voice = voice_impl.VoiceComponentImpl(self)

        # RESTful API.
        self._rest = rest_impl.RESTClientImpl(
            connector_factory=rest_impl.BasicLazyCachedTCPConnectorFactory(self._http_settings),
            connector_owner=True,
            entity_factory=self._entity_factory,
            executor=self._executor,
            http_settings=self._http_settings,
            max_rate_limit=max_rate_limit,
            proxy_settings=self._proxy_settings,
            rest_url=rest_url,
            token=token,
        )

        # We populate these on startup instead, as we need to possibly make some
        # HTTP requests to determine what to put in this mapping.
        self._shards: typing.Dict[int, gateway_shard.GatewayShard] = {}
        self.shards: typing.Mapping[int, gateway_shard.GatewayShard] = types.MappingProxyType(self._shards)