如何使用 DiscordPY 获取 Discord 服务器中所有 Webhook 的列表

How to get a list of all Webhooks in a discord server with DiscordPY

我能否使用 discord.py 或其他 Discord 机器人 python 库获取 Discord 服务器中所有 webhook URL 的列表?抱歉,这太短了,我不确定我可以为我的问题提供哪些其他信息。

我也尝试过以下方法。

import discord

client = command.Bot()

@client.event
async def on_message(message):
    message.content.lower()
    if message.author == client.user:
        return
    if message.content.startswith("webhook"):
        async def urls(ctx):
            @client.command()
            async def urls(ctx):
                content = "\n".join([f"{w.name} - {w.url}" for w in await ctx.guild.webhooks()])
                await ctx.send(content)

client.run('tokennumber')

这是一个使用列表理解的示例命令,它会 return 每个 webhook 的 link:

@bot.command()
async def urls(ctx):
    content = "\n".join([f"{w.name} - {w.url}" for w in await ctx.guild.webhooks()])
    await ctx.send(content)

这是列表推导式正在做的事情:

@bot.command()
async def urls(ctx):
    wlist = []
    for w in await ctx.guild.webhooks():
        wlist.append(f"{w.name} - {w.url}")
    content = "\n".join(wlist)
    await ctx.send(content)

Post-编辑:
使用您的 on_message() 活动:

import discord

client = commands.Bot() # add command_prefix kwarg if you plan on using cmd decorators

@client.event
async def on_message(message):
    message.content.lower()
    if message.author == client.user:
        return
    if message.content.startswith("webhook"):
        content = "\n".join([f"{w.name} - {w.url}" for w in await message.guild.webhooks()])
        await message.channel.send(content)

client.run('token')

如果你不想打印出每个 webhooks 的名称,那么你可以只加入每个 url 而不是:

content = "\n".join([w.url for w in await message.guild.webhooks()])

参考文献: