numberoverzero / bottom

asyncio-based rfc2812-compliant IRC Client
http://bottom-docs.readthedocs.io
MIT License
74 stars 23 forks source link

IRC is not connecting to freenode. #58

Closed Vinchethescript closed 1 year ago

Vinchethescript commented 3 years ago

The client is not connecting to freenode. Here's my code:

import asyncio
import bottom

client = bottom.Client(host="chat.freenode.net", port=6697, ssl=True))

@client.on("ping")
def handle(message=None, **kwargs):
    message = message or ""
    client.send("pong", message=message)

@client.on("client_disconnect")
async def reconnect(**kwargs):
    await asyncio.sleep(2, loop=client.loop)

    client.loop.create_task(client.connect())

def waiter(client):
    async def wait_for(*events, return_when=asyncio.FIRST_COMPLETED):
        if not events:
            return
        done, pending = await asyncio.wait(
            [client.wait(event) for event in events],
            loop=client.loop,
            return_when=return_when,
        )

        # Get the result(s) of the completed task(s).
        ret = [future.result() for future in done]

        # Cancel any events that didn't come in.
        for future in pending:
            future.cancel()

        # Return list of completed event names.
        return ret

    return wait_for

# taken from :ref:`Patterns`
wait_for = waiter(client)

@client.on("client_connect")
async def connect(**kwargs):
    client.default_name = "qpowieurtyturiewqop"
    client.send("nick", nick=client.default_name)
    client.send("user", user=client.default_name, realname=client.default_name)

    events = await wait_for("rpl_endofmotd", "err_nomotd") # it gets stuck at this line
    print("Connected")

    client.send("join", channel="#channel")

client.loop.create_task(client.connect())
client.loop.run_forever()

I tried this on another PC and it worked.

Vinchethescript commented 3 years ago

Also, when I tried to put some prints to find out where it gets stuck

def waiter(client):
    async def wait_for(*events, return_when=asyncio.FIRST_COMPLETED):
        print(1)
        if not events:
            return
        print(2)
        done, pending = await asyncio.wait(
            [client.wait(event) for event in events],
            loop=client.loop,
            return_when=return_when,
        )
        print(3)

        # Get the result(s) of the completed task(s).
        ret = [future.result() for future in done]

        # Cancel any events that didn't come in.
        for future in pending:
            future.cancel()

        # Return list of completed event names.
        return ret

    return wait_for

it looped 1 and 2, so the output was like this:


1
2
1 # after some seconds
2
1 # same as before
2
1
2
...

It looks like it re-runs the function if there is a problem