示例#1
0
    async def start(self):
        log.info("Starting...")

        self._redis = await zangy.create_pool(self.config.redis_url, 5)
        self._amqp = await aio_pika.connect_robust(self.config.amqp_url)
        self._amqp_channel = await self._amqp.channel()
        self._amqp_queue = await self._amqp_channel.get_queue("gateway.recv")

        self._connection = await self._get_state()
        self._connection._get_client = lambda: self

        self.ws = DiscordWebSocket(socket=None, loop=self.loop)
        self.ws.token = self.http.token
        self.ws._connection = self._connection
        self.ws._discord_parsers = self._connection.parsers
        self.ws._dispatch = self.dispatch
        self.ws.call_hooks = self._connection.call_hooks

        await self.http.static_login(self.config.token, bot=True)

        for extension in self.config.cogs:
            try:
                self.load_extension("cogs." + extension)
            except Exception:
                log.error(f"Failed to load extension {extension}.",
                          file=sys.stderr)
                log.error(traceback.print_exc())

        async with self._amqp_queue.iterator() as queue_iter:
            async for message in queue_iter:
                async with message.process(ignore_processed=True):
                    await self.receive_message(message.body)
                    message.ack()
示例#2
0
    async def _connect(self):
        coro = DiscordWebSocket.from_client(self, shard_id=self.shard_id)
        self.ws = await asyncio.wait_for(coro, timeout=180.0, loop=self.loop)

        self._reconnecting.clear()  # We are connected at this point.

        while True:
            try:
                await self.ws.poll_event()
            except ResumeWebSocket as e:
                coro = DiscordWebSocket.from_client(self,
                                                    shard_id=self.shard_id,
                                                    session=self.ws.session_id,
                                                    sequence=self.ws.sequence,
                                                    resume=True)
                self.ws = await asyncio.wait_for(coro, timeout=180.0, loop=self.loop)
示例#3
0
文件: bot.py 项目: zKryp/modmail
    async def start(self):
        trace_config = aiohttp.TraceConfig()
        trace_config.on_request_start.append(self.on_http_request_start)
        trace_config.on_request_end.append(self.on_http_request_end)
        self.http._HTTPClient__session = aiohttp.ClientSession(
            connector=self.http.connector,
            ws_response_class=DiscordClientWebSocketResponse,
            trace_configs=[trace_config],
        )
        self.http._token(self.config.token, bot=True)

        self.pool = await asyncpg.create_pool(**self.config.database, max_size=10, command_timeout=60)

        self._redis = await aioredis.create_redis_pool(
            (self.config.redis["host"], self.config.redis["port"]),
            password=self.config.redis["password"],
            minsize=5,
            maxsize=10,
            loop=self.loop,
        )

        self._amqp = await aio_pika.connect_robust(
            login=self.config.rabbitmq["username"],
            password=self.config.rabbitmq["password"],
            host=self.config.rabbitmq["host"],
            port=self.config.rabbitmq["port"],
        )
        self._amqp_channel = await self._amqp.channel()
        self._amqp_queue = await self._amqp_channel.get_queue("gateway.recv")

        self.prom = Prometheus(self)
        await self.prom.start()

        self._connection = await self._get_state()
        self._connection._get_client = lambda: self

        self.ws = DiscordWebSocket(socket=None, loop=self.loop)
        self.ws.token = self.http.token
        self.ws._connection = self._connection
        self.ws._discord_parsers = self._connection.parsers
        self.ws._dispatch = self.dispatch
        self.ws.call_hooks = self._connection.call_hooks

        for extension in self._cogs:
            try:
                self.load_extension("cogs." + extension)
            except Exception:
                log.error(f"Failed to load extension {extension}.", file=sys.stderr)
                log.error(traceback.print_exc())

        log.info("Running...")

        async with self._amqp_queue.iterator() as queue_iter:
            async for message in queue_iter:
                async with message.process(ignore_processed=True):
                    await self.receive_message(message.body)
                    message.ack()
示例#4
0
    async def start(self, worker=True):
        trace_config = aiohttp.TraceConfig()
        trace_config.on_request_start.append(self.on_http_request_start)
        trace_config.on_request_end.append(self.on_http_request_end)
        self.http._HTTPClient__session = aiohttp.ClientSession(
            connector=self.http.connector,
            ws_response_class=DiscordClientWebSocketResponse,
            trace_configs=[trace_config],
        )
        self.http._token(self.config.BOT_TOKEN, bot=True)

        self.pool = await asyncpg.create_pool(
            database=self.config.POSTGRES_DATABASE,
            user=self.config.POSTGRES_USERNAME,
            password=self.config.POSTGRES_PASSWORD,
            host=self.config.POSTGRES_HOST,
            port=int(self.config.POSTGRES_PORT),
            max_size=10,
            command_timeout=60,
        )

        self._redis = await aioredis.create_redis_pool(
            (self.config.REDIS_HOST, int(self.config.REDIS_PORT)),
            password=self.config.REDIS_PASSWORD,
            minsize=5,
            maxsize=10,
            loop=self.loop,
        )

        if worker:
            self._amqp = await aio_pika.connect_robust(
                login=self.config.RABBIT_USERNAME,
                password=self.config.RABBIT_PASSWORD,
                host=self.config.RABBIT_HOST,
                port=int(self.config.RABBIT_PORT),
            )
            self._amqp_channel = await self._amqp.channel()
            self._amqp_queue = await self._amqp_channel.get_queue(
                "gateway.recv")

        self.prom = Prometheus(self)
        await self.prom.start()

        self._connection = State(
            id=self.id,
            dispatch=self.dispatch,
            handlers=self._handlers,
            hooks=self._hooks,
            http=self.http,
            loop=self.loop,
            redis=self._redis,
            shard_count=int(await self._redis.get("gateway_shards")),
        )
        self._connection._get_client = lambda: self

        self.ws = DiscordWebSocket(socket=None, loop=self.loop)
        self.ws.token = self.http.token
        self.ws._connection = self._connection
        self.ws._discord_parsers = self._connection.parsers
        self.ws._dispatch = self.dispatch
        self.ws.call_hooks = self._connection.call_hooks

        if not worker:
            return

        for extension in self._cogs:
            try:
                self.load_extension("cogs." + extension)
            except Exception:
                log.error(f"Failed to load extension {extension}.",
                          file=sys.stderr)
                log.error(traceback.print_exc())

        async with self._amqp_queue.iterator() as queue_iter:
            async for message in queue_iter:
                async with message.process(ignore_processed=True):
                    await self.receive_message(message.body)
                    message.ack()