我能否使用 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()])
引用资料:
- List comprehension in Python
- f-Strings - Python 3.6.0+
-
Guild.webhooks()
-coroutine
,因此需要await
ed。 -
Webhook.name
-
Webhook.url
关于python - 如何使用 DiscordPY 获取 Discord 服务器中所有 Webhook 的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62243902/