woohyunjng / discord.py-components

An unofficial third-party library of discord.py for discord components.
https://devkiki7000.gitbook.io/discord-components/
MIT License
128 stars 43 forks source link

[Bug] Very high message roundtrip ping when the lib is loaded. #72

Closed FightMan01 closed 3 years ago

FightMan01 commented 3 years ago

Bug Line

I don't know where is the actual bug. My bot is over 8000 servers.

image

After a bot restart with the Cog unloaded the bot works fine. When I load the Cog it slows down.

Excpected Action

Just work

Actual Action

Well it works but slows down the bot.

Reproducing the action

Module loaded in a Cog:

def __init__(self, client):
self.client = client
self.connection = client.dbconnection
if not hasattr(client, "component_client"):
self.component_client = DiscordComponents(client)
else:
self.component_client = client.component_client

Also it is present in my on_ready() function as well. The cog loads when the bot is ready.

Possible Solution

I didn't manage to find anything.

Environment

Check

FightMan01 commented 3 years ago

Network: https://www.speedtest.net/result/c/53809c07-3978-4096-be0f-d5219dffb35a

kiki7000 commented 3 years ago

Fixed on 1ba7404bd1b167a2145cfe332720ab6a34daa8da but not released

FightMan01 commented 3 years ago

Well it did not fix for me. I installed this lib yesterday using pip install git+https://github.com/kiki7000/discord.py-components.git

FightMan01 commented 3 years ago

But I made some modifications for example to always add a listener even I turn change_discord_methods to False. Somehow if I turn that to True my bot slows down but when I turn it to False the performance will be normal. Also I reworked the on_socket_response a bit when I tried. to investigate the issue but I don't think that's important. https://github.com/FightMan01/discord.py-components/commit/b3a44dd4a772cb87b7d167f58e0ee212eea0a89d

kiki7000 commented 3 years ago

Was the ping lower when the previous version?

FightMan01 commented 3 years ago

No, the ping was very high even in the version 0.5.2.4

kiki7000 commented 3 years ago

I mean on version 0.5.1 and lower

FightMan01 commented 3 years ago

I didn't try that. I just tried the PyPi version and the GitHub version.

kiki7000 commented 3 years ago

Did you try with add_listener disabled?

FightMan01 commented 3 years ago

Well, the on_button_click event is very important for me, so I didn't do that.

kiki7000 commented 3 years ago

Oh, that is not related to add_listener. That is for users who are using discord-py-slash-command

FightMan01 commented 3 years ago

Oh :D But I'm using discord-py-slash-command as well 😅

kiki7000 commented 3 years ago

Oh Could you try it?

FightMan01 commented 3 years ago

Well yes, but I noticed one more thing with interaction.respond(). When I used it, the roundtrip was about 1200ms. So I decided to write the request myself:

                #await res.respond(type=InteractionType.UpdateMessage, embed=embed, content=None)
                token = res.interaction_token
                compid = res.interaction_id
                data = {}
                data["content"] = None
                embeds = [embed]
                if embeds:
                    embeds = list(map(lambda x: x.to_dict(), embeds))
                    data["embeds"] = embeds
                async with aiohttp.ClientSession() as session:
                    await session.post(f"https://discord.com/api/v8/interactions/{compid}/{token}/callback", json={"type": InteractionType.UpdateMessage, "data": data})

And I noticed with this I get about 310ms which is acceptable in Hungary. To be honest I don't really understand what cause this problem.

kiki7000 commented 3 years ago

Could you replace

                async with aiohttp.ClientSession() as session:
                    await session.post(f"https://discord.com/api/v8/interactions/{compid}/{token}/callback", json={"type": InteractionType.UpdateMessage, "data": data})

in you code with


bot.http.request(discord.http.Route("POST", f"/interactions/{compid}/{token}/callback"), json={"type": InteractionType.UpdateMessage, "data": data})
FightMan01 commented 3 years ago

Hmm, about 300ms

FightMan01 commented 3 years ago

I use this formula:

                t1 = time.perf_counter()
                await self.client.http.request(discord.http.Route("POST", f"/interactions/{compid}/{token}/callback"), json={"type": InteractionType.UpdateMessage, "data": data})
                t2 = time.perf_counter()
                print(f"Latency: {round((t2-t1)*1000)}ms")
FightMan01 commented 3 years ago

But it's still thinking a lot and sometimes fail with this: discord.errors.NotFound: 404 Not Found (error code: 10062): Unknown interaction

https://user-images.githubusercontent.com/29693718/120505330-29ab5500-c3c5-11eb-8bd4-48aae7847fde.mov

FightMan01 commented 3 years ago

Ok, I managed to figure out something. I made another bot client and added it only 1 server. And it works very fast and fine. Same code, same environment, same everything but a different bot client. My main bot is over 8000 servers. Maybe that is the "problem"?

kiki7000 commented 3 years ago

But your bot's ping was fine until you used this library

FightMan01 commented 3 years ago

Yes, so that's why this is very confusing for me