简体   繁体   English

Discord.py(重写):在函数中处理“不正确的令牌”时出错

[英]Discord.py (rewrite): Error Handling "Improper Token" In Function

I feel like I'm missing a simple way to solve this problem, but I can't seem to find a way to handle the discord.errors.LoginFailure: Improper token has been passed.我觉得我缺少一个简单的方法来解决这个问题,但我似乎找不到处理discord.errors.LoginFailure: Improper token has been passed.的方法discord.errors.LoginFailure: Improper token has been passed. error.错误。 What I'm trying to do is run a function that essentially runs a bot and repeats itself if it comes across any errors (with try: and except: ) and if it catches the 'improper token' error then change a setting in my code and retry it.我想要做的是运行一个函数,该函数本质上运行一个机器人,如果遇到任何错误(使用try:except: ),并且如果它捕获“不正确的令牌”错误,则在我的代码中更改设置并重试。

What I believe is happening is that try/except isn't catching the error and it stops the program (printing the entire error in the process).我相信正在发生的是 try/except 没有捕获错误并且它停止了程序(在过程中打印整个错误)。 I've tried some quick solutions like making the function into a while statement and until it reaches the end of the program it will keep repeating itself, however without catching the error I can't continue any code.我已经尝试了一些快速的解决方案,比如将函数变成一个 while 语句,直到它到达程序的末尾,它会不断重复自己,但是如果没有捕捉到错误,我就无法继续任何代码。

My code is messy and has to do with a lot of variables that are defines earlier in this large python file, so I won't show my entire function.我的代码很乱,并且与这个大型 python 文件中之前定义的许多变量有关,所以我不会展示我的整个函数。

Here is the simplified version:这是简化版:

def code(mainText):
    mainLines = mainText.split("\n")
    # Do some stuff editing mainText
    final = "\n".join(mainLines)
    try:
        exec(final, globals())
    except Exception as e:
        print(str(e))
        # edit 'final' a bit
        exec(final, globals())

The full error message:完整的错误信息:

Task exception was never retrieved
future: <Task finished coro=<Client.start() done, defined at E:\DiscordBot\lib\site-packages\discord\client.py:526> exception=LoginFailure('Improper token has been passed.')>
Traceback (most recent call last):
  File "E:\DiscordBot\lib\site-packages\discord\http.py", line 258, in static_login
    data = await self.request(Route('GET', '/users/@me'))
  File "E:\DiscordBot\lib\site-packages\discord\http.py", line 222, in request
    raise HTTPException(r, data)
discord.errors.HTTPException: 401 UNAUTHORIZED (error code: 0): 401: Unauthorized

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "E:\DiscordBot\lib\site-packages\discord\client.py", line 542, in start
    await self.login(*args, bot=bot)
  File "E:\DiscordBot\lib\site-packages\discord\client.py", line 400, in login
    await self.http.static_login(token, bot=bot)
  File "E:\DiscordBot\lib\site-packages\discord\http.py", line 262, in static_login
    raise LoginFailure('Improper token has been passed.') from exc
discord.errors.LoginFailure: Improper token has been passed.

As of writing this I found 'Task exception never retrieved' is this anything of importance or is that the usual 'improper token' error?在撰写本文时,我发现“从未检索到任务异常”是重要的事情还是通常的“不正确的令牌”错误?

Thank you, sorry in advance for my bad coding practices and lack of experience using Stack Overflow.谢谢,对于我糟糕的编码实践和缺乏使用 Stack Overflow 的经验,提前表示歉意。

seen the fact there is a HTTPException and a Login Failure, you should have those both behind the except like this:看到有一个 HTTPException 和一个登录失败的事实,你应该把它们都放在 except 后面,像这样:


try:
    bot.run(BOT_TOKEN)
except discord.errors.HTTPException and discord.errors.LoginFailure as e:
    print("Login unsuccessful.")

this will catch the HTTPException as well.这也将捕获 HTTPException。 Becuse like I said, if you read the error you see there is a HTTPException and a LoginFailure.因为就像我说的,如果您阅读错误,您会看到有一个 HTTPException一个 LoginFailure。 Both of those need to be catched if you want to catch the error and make a custom error message.如果您想捕获错误并生成自定义错误消息,则需要同时捕获这两者。

Try this.尝试这个。 I haven't tried it myself.我自己没试过。 This is my best answer as to what I can understand from your problem and error message:这是我从您的问题和错误消息中可以理解的最佳答案:

#Put this at the bottom of your .py file
try:
    bot.run(BOT_TOKEN)
except discord.errors.LoginFailure as e:
    print("Login unsuccessful.")

Is your token a string?你的令牌是一个字符串吗? Did you get it from the discord developer portal?您是从 Discord 开发者门户网站获取的吗? ie are you sure it's the right token?即你确定这是正确的令牌吗?

Make sure BOT_TOKEN is a string and your token is valid确保 BOT_TOKEN 是一个字符串并且您的令牌有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM