【问题标题】:How to get a list of all Webhooks in a discord server with DiscordPY如何使用 DiscordPY 获取不和谐服务器中所有 Webhook 的列表
【发布时间】:2020-06-07 10:19:09
【问题描述】:

我是否能够使用 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 文档可以解决您的问题:discordpy.readthedocs.io/en/latest/api.html#webhook-support
  • @merive_ 我检查了,但是我找不到任何涉及获取 webhook URL 的内容。如果您能找到任何有关检索 URL 或创建一个然后获取 URL 的信息,请告诉我。

标签: python python-3.x discord.py discord.py-rewrite


【解决方案1】:

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

参考资料:

【讨论】:

  • 他们都做同样的事情。至于错误,可能是您的机器人实例被称为client,因此只需将bot 替换为client 或您所称的任何名称。 @EchoTheAlpha
  • 尝试改用client = commands.Bot()。它继承自 discord.Client(),并且功能更多。
  • 抱歉回复慢!编辑答案^
  • 对不起,我总是错过答案中的重要内容!确保您的代码顶部有 from discord.ext import commands
  • 这很好用,我使用 Pickle 库将它保存到一个文件中,并将 URL 复制并粘贴到一个脚本中,将它们保存为一个文件,它们都是相同的,这意味着它实际上确实获得了所有的 URL。谢谢你在我迷茫的时候坚持我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-24
  • 2021-11-18
  • 1970-01-01
  • 2020-11-27
  • 2018-05-23
  • 2021-08-30
  • 1970-01-01
相关资源
最近更新 更多