kyb3r / dhooks

A simple python Discord webhook API wrapper
https://discord.gg/etJNHCQ
MIT License
189 stars 50 forks source link

Webhook.delete() cant be awaited and raises an error when webhook is async #35

Closed ppyrzanowski closed 4 years ago

ppyrzanowski commented 4 years ago

My test code:

import asyncio
import dhooks
import aiohttp
import selectors

async def main():
    hook = dhooks.Webhook("url", is_async=True)

    await hook.send('hello')
    await hook.modify('bob')
    await hook.get_info()
    await hook.delete()  # since we await it, we have to return a corutine or simular, but method delete will return None

    await hook.close()  # close the client session

# monkeypatch for windows machine
selector = selectors.SelectSelector()
loop = asyncio.SelectorEventLoop(selector)
asyncio.set_event_loop(loop)

loop.run_until_complete(main())

Error:

C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\dhooks\client.py:323: RuntimeWarning: coroutine 'Webhook._async_request' was never awaited
  self._request(method='DELETE')
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Traceback (most recent call last):
  File "C:/Users/Administrator/PycharmProjects/-/-/-/-/dev/async_error.py", line 20, in <module>
    loop.run_until_complete(main())
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 616, in run_until_complete
    return future.result()
  File "C:/Users/Administrator/PycharmProjects/-/-/-/-/dev/async_error.py", line 12, in main
    await hook.delete()
TypeError: object NoneType can't be used in 'await' expression
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x0000020B56C3BD30>
Unclosed connector
connections: ['[(<aiohttp.client_proto.ResponseHandler object at 0x0000020B56C4E1C0>, 1322769.828)]']
connector: <aiohttp.connector.TCPConnector object at 0x0000020B56C3BD60>

Fix:

# dhooks/client.py
# line 318 -323

    def delete(self) -> 'Webhook':
        """
        Deletes the :class:`Webhook` permanently.

        """
        return self._request(method='DELETE')
# and since we return now the webhook object again it would be good to delete it in some way with an if statement in the _request method after it was executed, (if method == "DELETE: )
kyb3r commented 4 years ago

ah ty, fixed in 2a5c98085f15e59a10da93b0acd55577f2652046