Discord bot 不会使用 client.user.edit 更改它的昵称

时间:2021-04-06 13:32:16

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

我正在使用以下 python 代码和 discord.py 来制作一个带有 discord.Client 实例的不和谐机器人。我希望机器人能够在 on_message(message:discord.Message) 函数中更改其在公会上的头像和昵称,以匹配发送最新消息的用户的头像和昵称。如果您了解 Not Pretty Nitro 机器人,我想做类似的事情(仅在更改机器人外观以匹配公会成员方面)。头像的改变奏效了,至少是第一次。然后,在我尝试对其进行测试的大多数情况下,它抛出以下异常:

discord.errors.HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body
In avatar: You are changing your avatar too fast. Try again later.

我认为换头像有某种冷却时间。有什么办法可以绕过吗?

另外,昵称一直没改成功。我在公会上给机器人起了个昵称,这样就可以改变一些东西,以防万一。这是代码: 所有不一致的缩进都是stackoverflow和复制粘贴的结果,它们不在实际代码中

async def on_message(message):
    if message.author==client.user:
        return
    #some irrelevant code removed here
        async with message.channel.typing():
            #the code omitted here is irrelevant and worked
            #the below code is the problematic portion
                    try:
                        await message.delete()
                        asset = message.author.avatar_url_as()
                        avatarBytes = await asset.read()
                        await client.user.edit(nick=message.author.display_name)
                        try:
                          await client.user.edit(avatar=avatarBytes)
                        except discord.errors.HTTPException:
                          #this error is the avatar changing too fast error
                          pass
                    await message.channel.send(content=message.content,files=fileList)
                except discord.errors.HTTPException:
                    await message.channel.send("Too many images to send")
                    #for reference, this except block only triggers when it would have to send more than 10 files at once                          

机器人有权删除消息和管理昵称。这里发生了什么? 提前致谢!

2 个答案:

答案 0 :(得分:0)

问题1(修补bot昵称)

您不能像这样编辑机器人的昵称,正如您在 documentation 中看到的那样。要在公会中编辑机器人的昵称,您必须执行以下操作:

await message.guild.me.edit(nick=message.author.display_name) 

me 是您的机器人在公会中的 discord.Member 实例。

问题 2(修补机器人的头像)

其次,根据 discord.py,修补机器人帐户的速率限制为 2/3600 秒(PATCH Username 是那里使用的词)。因此,您机器人的这一部分可用于执行您想做的事情。

其他解决方案(网络钩子)

但我有一个解决方案可以做你想做的事情,我认为 NQN 正在使用它:webhooks。 您可以创建一个 webhook 并使用它来发送带有您想要的头像和用户名的消息(在使用 webhook 发送的每条消息上定义)。但是你的机器人本身的外观不会改变,只会改变头像和使用的 webhook 的名称。

解决方案参考

create a webhook

get a webhook from an url

send a message with a webhook

答案 1 :(得分:0)

非常感谢!我给了我的机器人更多的权限并制作了所有的网络钩子,现在它可以工作了