Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using one webhook returns as a list #43

Closed
BarryThrill opened this issue Feb 17, 2021 · 10 comments
Closed

Using one webhook returns as a list #43

BarryThrill opened this issue Feb 17, 2021 · 10 comments

Comments

@BarryThrill
Copy link

BarryThrill commented Feb 17, 2021

Hello guys!

Today I discovered that when I used one webhook, it sometimes returns as just one string etc if I do

response = webhook.execute()
print(response)

as:
<Response [200]>

but lately I did find out that it sometimes returns as:
[<Response [200]>]

I assume this is sort of bug where it shouldn't mix between list and non-list when sending only using one webhook

This cause me an error where I cant use response.ok anymore since its inside a List but that shouldn't happen if im not using multiple webhooks url which I dont.

Name: discord-webhook
Version: 0.11.0
Summary: execute discord webhooks
Home-page: https://github.com/lovvskillz/python-discord-webhook
Author: Vadim Zifra
Author-email: vadim@minehub.de
License: MIT License
Location: /home/barry/.local/lib/python3.8/site-packages
Requires: requests
Required-by:
@outfielder
Copy link
Contributor

but lately I did find out that it sometimes returns as:
[<Response [200]>]

Could you please add more detail about the times that you get the response object in a list and the times you do not? Are you sending the same type of webhook each time (i.e webhooks with similar embedded information) or are you sending different types of webhooks?

@BarryThrill
Copy link
Author

but lately I did find out that it sometimes returns as:
[<Response [200]>]

Could you please add more detail about the times that you get the response object in a list and the times you do not? Are you sending the same type of webhook each time (i.e webhooks with similar embedded information) or are you sending different types of webhooks?

Hello! Sorry for the delay.

The situation that happens is abit hard for me to know exactly the reason why it happens but I did come up with a small "hotfix"

response = webhook.execute()

if isinstance(response, List):
    assert(len(response) == 1)
    response = response[0]

if response.ok:
    print("YAY")
    return

However the details I have is that currently I am not using the multi-webhook function you have created due to those are not in thread. Meaning if one webhook fails or hits the rate-limit. It will stop the second one to send until the first webhook is done. So what I do is that I put each webhook URL to a thread to send to response = webhook.execute() but each webhook URL is different but the webhook embed is the same. Example here:

webhook_dict = {
    "key1": "https://discord.....",
   "key2": "https://discord....."
}

for key, discord_webhook_url in webhook_dict.items():
	webhook = DiscordWebhook(
		url=discord_webhook_url,
		username="Test"
	)

	webhook.add_embed(embed)

	Thread(
		target=post_embed,
		args=(
			webhook
		)
	).start()

return


def post_embed(webhook):
    while True:
        try:

            response = webhook.execute()

            if isinstance(response, List):
                assert(len(response) == 1)
                response = response[0]

            if response.ok:
                logger.info("Succesfully sent to Discord")
                return
				
			else:
			   # Handling 429 etc etc...

@outfielder
Copy link
Contributor

I believe I have managed to replicate your issue where the response for one object is sometimes returned as a list and sometimes not, but as you have previously mentioned it does seem to be random.

Have you since found any pattern to when the response may be returned as a list?

@lovvskillz
Copy link
Owner

Actually, there should always be a list returned. No matter if only one webhook URL was given or several.
However, when I think about it, it would be better to just return the response directly if only one URL is given.

@BarryThrill
Copy link
Author

I believe I have managed to replicate your issue where the response for one object is sometimes returned as a list and sometimes not, but as you have previously mentioned it does seem to be random.

Have you since found any pattern to when the response may be returned as a list?

Hey! I did not found any pattern yet of why it does it. It seems super random which is really odd. Im not sure if its Discord themselves that returns it like that nor this package but I believe it wasn't like this before the latest update but I might be wrong as well :)

@BarryThrill
Copy link
Author

Actually, there should always be a list returned. No matter if only one webhook URL was given or several.
However, when I think about it, it would be better to just return the response directly if only one URL is given.

Thay would make sense I guess. Im pretty sure that is the way it supposed to do. If more than one webhook URL then return the response in the list but if only one then no need to be in a list. The bug is that it sometimes returns in list using only one webhook.

@lovvskillz
Copy link
Owner

I think I will change that later today or tomorrow and also provide a release.

@BarryThrill
Copy link
Author

I think I will change that later today or tomorrow and also provide a release.

Looking forward!

@lovvskillz
Copy link
Owner

This should be fixed in the version 0.12.0.
Let me know if it worked out for you.

@BarryThrill
Copy link
Author

This should be fixed in the version 0.12.0.
Let me know if it worked out for you.

Will do! By looking at the code, I do see that it should work fine since its similar code as I use today 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants