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

标签 python python-3.x discord.py

我能否使用 discord.py 或其他 Discord bot 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')

最佳答案

这是一个使用列表理解的示例命令,它将返回每个 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)

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

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

后期编辑:
使用您的 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')

如果您不想打印出每个 webhook 的名称,那么您可以只加入每个 url:

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

引用资料:

关于python - 如何使用 DiscordPY 获取 Discord 服务器中所有 Webhook 的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62243902/

相关文章:

python - 如何找到图像中最常见的像素值?

python - 替换文件中的一行将另一行移动到上一级

python - 为 Python 3.3 安装 opencv

python - 如何在一条不和谐消息中发送 for 循环的结果?

python-3.x - Discord 音乐机器人 - 队列 (discord.py)

python - 如何删除 Python 工具的 Visual Studio(2016 年 6 月)更新通知?已经安装好了

python - Plotly 静态图像导出出现 OSError : [WinError 193] %1 is not a valid Win32 application

Python:从视频中截取屏幕截图

python-3.x - python 包未通过 CI/CD 管道安装,导入错误,找不到包

python - Discord.pylogs_from 不工作