Warm tip: This article is reproduced from serverfault.com, please click

discord.py get webhooks of channel

发布于 2020-12-01 17:57:09

I'm trying to make a webhook so if anyone says 'ez' it deletes it and sends a message with the webhook with a random message. Originally what I was doing was

    if "ez" in message.content:
        webhook = await message.create_webhook(name=ctx.author.name)
        await webhook.send(ezmessages[random.randint(0, len(ezmessages))-1], username=message.author.name, avatar_url=message.author.avatar_url)
        await message.delete()
        await webhook.delete()

but the problem is this gets rate limited if webhooks are created and deleted too quickly. So instead what I want to do is check if the bot already has a webhook for the text channel, and if there is one use that but if not use a different one. I thought this would work:

    for webhook in message.channel.webhooks:
        await webhook.send(ezmessages[random.randint(0, len(ezmessages))-1], username=message.author.name, avatar_url=message.author.avatar_url)

but I get the error

TypeError: 'method' object is not iterable

Even though it should return a list

Anyone know how to correctly iterate over this?

Questioner
Tanuj KS
Viewed
0
Łukasz Kwieciński 2020-12-02 02:21:32

TextChannel.webhooks it's not an attribute, its a function and a coroutine, so you need to call it and await it

webhooks = await message.channel.webhooks()
for webhook in webhooks:
    ...

docs