Discord.py昵称更新程序由于未知原因无法在循环中工作,没有错误

2024-09-28 21:01:53 发布

您现在位置:Python中文网/ 问答频道 /正文

提前感谢您的帮助

我的目标是编辑服务器中任何不符合我指定格式的用户的昵称

下面的循环运行得非常好没有行await user.edit(nick=f"SAS{jsonPilotID} - {jsonName}"),这是一个应该编辑实际昵称的循环。但是,当我添加行时,循环只会在遇到第一个用户时阻塞自己,不想前进不会抛出错误,也不会编辑昵称

@tasks.loop(seconds=LoopTimer)
async def NicknameUpdater(msg):
    url = rankUrl

    req = requests.post(url)
    req = json.loads(req.text)

    y = msg.guild.members

    print("Start nickname edit loop \n")

    for user in y:
        for i in req['records']:
            if not i['discordid'] == None:
                jsonDiscordID = int(i['discordid'])
                jsonName = i['firstname']
                jsonPilot = i['pilotid']
                if len(jsonPilot) == 1:
                    userPilotIDZero = "000"
                elif len(jsonPilot) == 2:
                    userPilotIDZero = "00"
                elif len(jsonPilot) == 3:
                    userPilotIDZero = "0"
                elif len(jsonPilot) == 4:
                    userPilotIDZero = ""

                jsonPilotID = userPilotIDZero + jsonPilot

                if jsonDiscordID == user.id:

                    print(f"User {user} discord ID {user.id} name {jsonName}")

                    print(jsonPilotID)

                    print(f"SAS{jsonPilotID} - {jsonName}")

                    if not user.display_name == f"SAS{jsonPilotID} - {jsonName}":
                        await user.edit(nick=f"SAS{jsonPilotID} - {jsonName}")
                        print(f"Changed username for {jsonName} to SAS{jsonPilotID} - {jsonName}")
                    else:
                        print("no need to change nickname.")
            else:
                pass

    t = time.localtime()
    current_time = time.strftime("%H:%M:%S", t)

    loopCompleteMessage = f"\n\nNickname edit loop end at {current_time}. Next loop in {LoopTimer} seconds"

    print(loopCompleteMessage)

我很乐意回答任何问题。 提前感谢您的帮助


Tags: loop编辑leniftimeeditreqprint