删除 Webhooks Discord.py

Delete Webhooks Discord.py

在 discord.py 中,我知道您可以使用以下方法创建 webhook: message.channel.create_webhook(name = 'something')

但我不知道是否有删除它们的选项。这可能吗?谢谢

没有 built-in 方法可以做到这一点,you will need to send an http request manually

(编辑 - 看来我错了:如 Bagle 的回答所示,确实有一个内置的方法来删除 webhooks。但是,这个方法仍然有效,而且更容易看。)

类似于:

await ctx.bot.http.request(Route('DELETE', '/webhooks/{webhook_id}', webhook_id=webhook_id))

您可能需要调整它以在您的实施中起作用。

AxelSparrow's method is definitely cleaner, but there is a built-in method of deleting them via webhook.delete(),尽管这可能有点 'scuffed',因为如果您只有 webhook 的名称,则必须使用 for 循环。在下面的代码片段中,我将提供两种方法供您使用。

# Method 1: Directly using the information from creating the webhook
hook = message.channel.create_webhook(name = 'something') # create a variable
# await hook.send("test")
await hook.delete() # then delete based on variable

# Method 2: Only use this if you don't have the webhook's other information
# get all the webhooks in a channel ⬇️
channel_webhooks = await message.channel.webhooks() 
for webhook in channel_webhooks: # iterate through the webhooks
    if webhook.name == "something":
        await webhook.delete()
        break

如果您需要更多信息或理解,请随时查看 class discord.Webhook 文档。