有没有办法在discord.py中的事件中更改昵称

问题描述 投票:1回答:2

我想在发送消息时将人名更改为原始名称+ 1。

我已经获得了消息检测的所有代码,但没有更改名称。

@bot.event
async def on_message(message):
      if message.author == bot.user: return
      await client.change_nickname(message.author, "...a")

我希望改名为。 。 .a当他们发送消息但没有做任何事情时。

任何帮助将不胜感激,谢谢。

python discord.py discord.py-rewrite
2个回答
0
投票

正如我在评论中已提到的那样:

到目前为止,我从文档中看到change_nickname代表了公会特定的权限。为了改变用户的昵称,你必须调用edit on a member and pass the nick parameter

像这样的东西:

@bot.event
async def on_message(message):
      if message.author == bot.user: return
      await message.author.edit(nick="some new nick")

为了您对original name + 1形式的特殊需求,您可以使用以下内容:

import re 

# Extracts digits that are at the end of string user_name
#
# @param user_name a string to extract digits from
# @return extracted digits as integer or -1 if error
def extract_number_suffix(user_name):
    # Using regex to find all digits at the end of the user_name
    # source https://docs.python.org/2/library/re.html#re.search
    # regex entry: https://regex101.com/r/WQkzhw/1
    extracted= re.search(r"[0-9]+$", user_name)
    try:
        return int(extracted[0])
    except:
        return -1


@bot.event
async def on_message(message):
      if message.author == bot.user: return
      previous_name = message.author.name
      number = extract_number_suffix(previous_name)
      if number == -1:
          # If number is -1 that means that the function extract_number_suffix
          # failed to extract a number because user has no number at the end of
          # it's name. So we just set the name to name1
          await message.author.edit(nick="{}1".format(previous_name))
      else:
          # Remove the current digit suffix
          clean_name = previous_name.replace(str(number ), "")
          # Increment current number and change nick
          new_number = number + 1
          await message.author.edit(nick="{0}{1}".format(clean_name , new_number ))

你也可以使用message.author.display_name

返回用户的显示名称。

对于普通用户,这只是他们的用户名,但如果他们有一个公会特定的昵称,那么返回它。 source


0
投票

Discord.py-rewriteDiscord.py是API的不同分支。重写分支是与Discord应用程序及其官方API最新的分支

rewrite内你会改变它

@bot.event
async def on_message(msg):
    await msg.author.edit(nick=f'{msg.author.name}1')

async(discord.py)分支内部,它会像这样完成

@bot.event
async def on_message(msg):
    await client.change_nickname(msg.author,'{msg.author.name}1')

客户端的async属性可以找到hererewrite文档here

© www.soinside.com 2019 - 2024. All rights reserved.