简体   繁体   中英

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

Am i able to get a list of all webhook URLs in a discord server using discord.py or another Discord bot python library? Sorry that this is so short im not sure what other information i could provide for my question.

I have also tried the following.

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')

Here's an example command using list comprehension that would return the link for each webhook:

@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)

Here's what the list comprehension is doing:

@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-edit:
Using your on_message() event:

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')

If you don't want to print out each webhooks' name, then you can just join each url instead:

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

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM