【问题标题】:Discord bot isn't changing it's nickname using client.user.editDiscord bot 没有使用 client.user.edit 更改它的昵称
【发布时间】:2021-07-02 07:08:35
【问题描述】:

我正在使用以下 python 代码和discord.py 来创建一个带有discord.Client 实例的不和谐机器人。我希望机器人能够在 on_message(message:discord.Message) 函数中更改其在公会上的头像和昵称,以匹配发送最新消息的用户。如果你知道 Not Quite 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                          

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

【问题讨论】:

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


    【解决方案1】:

    问题 1(修补 bot 的昵称)

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

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

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

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

    其次,根据 discord.py,修补机器人帐户的速率限制是 2/3600s(PATCH Username 是那里使用的词)。所以,你的机器人的这一部分可以用来做你想做的事。

    其他解决方案(webhook)

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

    解决方案参考

    create a webhook

    get a webhook from an url

    send a message with a webhook

    【讨论】:

    • 谢谢,这对我有所帮助。但是,现在我遇到了一个新问题。一旦读取了资产的字节,它就会跳过其他所有内容,而不是编辑 webhook 用户、删除消息或发送消息的副本。 not webhook==None 在这种情况下。 asset = message.author.avatar_url_as() avatarBytes = await asset.read() await webhook.edit(name=message.author.display_name, avatar=avatarBytes) await webhook.send(message.content, files=fileList) await message.delete()
    • 我在想这可能是权限错误。我正在等待服务器创建者赋予它管理员角色,我授权它具有管理员权限以确保安全,但根据我过去在此特定服务器上的经验,至少它似乎也需要管理员角色
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-15
      • 2017-12-28
      • 2019-02-09
      • 2019-07-26
      • 2017-08-04
      • 1970-01-01
      • 2017-09-09
      • 2021-05-20
      相关资源
      最近更新 更多