首页 > 解决方案 > Discord.py 重写在 DM 中从用户消息中更改昵称

问题描述

所以,我正在尝试为我的机器人执行一个不和谐的命令。命令是每次用户进入服务器时,机器人都会向用户打招呼并询问其名称。然后用户回复一条包含其名称的消息,然后机器人将服务器中用户的昵称更改为第一个用户回复的昵称。所以fa我没有运气找到如何但是我制作了这个代码并发送dm并阅读回复,但它不会改变昵称,有什么帮助吗?

编辑:所以我听从了某人的建议,这就是编码所发生的事情

 @client.event
async def on_member_join(member):
        await member.send(f"""Welcome to the server {member.name}!""")
        await member.send("Please enter your full name: ")

        def check(m): #checks if message was sent by someone other than the bot
            return m.author != client.user
        name = await client.wait_for('message', check=check)
        await name.author.edit(nick=name.content)

现在的问题是,每当我尝试用最后一行更改昵称时,都会弹出此错误:

Ignoring exception in on_member_join
Traceback (most recent call last):
  File "/Users/bermed28/opt/anaconda3/envs/pyBot/lib/python3.6/site-packages/discord/client.py", line 303, in _run_event
    await coro(*args, **kwargs)
  File "/Users/bermed28/Desktop/pyBot/bot.py", line 56, in on_member_join
    await name.author.edit(nick=name.content)
AttributeError: 'User' object has no attribute 'edit'

标签: pythondiscord.pydiscord.py-rewrite

解决方案


找到了答案,我所要做的就是以下代码:

@client.event
async def on_member_join(member: discord.Member):
    await member.send(f"""Welcome to the server {member.name}!""")
    await member.send("Please enter your full name: ")

    def check(m):  # checks if message was sent by someone other than the bot
            return m.author != client.user

    name = await client.wait_for("message",check=check)
    await member.edit(nick=str(name.content))

推荐阅读