无法仅在一台服务器中发送webhook



代码

webhooks = await message.channel.webhooks()
webhook = discord.utils.get(webhooks, name=message.author.display_name)
if webhook is None:
webhook = await message.channel.create_webhook(name=message.author.display_name)
await webhook.send(content=content, username=message.author.display_name, avatar_url=message.author.avatar)
await message.delete()

追溯

Ignoring exception in on_message
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.9/site-packages/discord/client.py", line 382, in _run_event
await coro(*args, **kwargs)
File "/app/cogs/events.py", line 105, in on_message
await webhook.send(content=content, username=message.author.display_name, avatar_url=message.author.avatar)
File "/app/.heroku/python/lib/python3.9/site-packages/discord/webhook/async_.py", line 1508, in send
raise InvalidArgument("This webhook does not have a token associated with it")
discord.errors.InvalidArgument: This webhook does not have a token associated with it

Webhook在意图和文本通道中启用。此异常仅在一台服务器中引发,在其他服务器中运行良好。

我在本地系统上测试了相同的代码,在本地系统中运行的客户端发送webhook时没有遇到任何问题。

创建每个webhook本身是个坏主意——你可以很快达到每个频道10个webhook的速率限制。

相反,API允许您使用自定义用户名和头像的send
在当前版本的discord客户端中,单击webhook仍然会显示假用户。没有必要为每个用户制作一个单独的。

然后你可以做这样的事情:

async def impostor(ctx, user: discord.User, *, msg: str):
hooks = await ctx.channel.webhooks()
hook = utils.get(hooks, name='My Impostor Webhook')  # Change this to whatever you want, nobody will see it. This is only displayed to admins checking the webhook list and for the bot to find it again.
if hook is None:
hook = await ctx.channel.create_webhook(name='My Impostor Webhook', avatar=None, reason=None)
await hook.send(content=msg, username=user.name, avatar_url=user.avatar_url)

(注意,在较新版本中,这将是.avatar.url)

相关内容

最新更新