gpt4 book ai didi

python - Discord.py(重写): Error Handling “Improper Token” In Function

转载 作者:行者123 更新时间:2023-12-03 07:57:21 24 4
gpt4 key购买 nike

我觉得我缺少解决此问题的简单方法,但似乎找不到解决discord.errors.LoginFailure: Improper token has been passed.错误的方法。我想做的是运行一个实际上运行机器人的函数,如果遇到任何错误(使用try:except:)并且如果捕获到“不正确的 token ”错误,则重复自身,然后更改代码中的设置并重试它。

我相信正在发生的事情是try/except没有捕获错误,并且停止了程序(在过程中打印整个错误)。我已经尝试了一些快速的解决方案,例如将函数制作为while语句,直到到达程序末尾,它将不断重复自身,但是如果没有捕获错误,我将无法继续执行任何代码。

我的代码杂乱无章,并且与这个大型python文件中较早定义的许多变量有关,因此我不会展示我的整个函数。

这是简化版:

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())

完整的错误消息:

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.

在撰写本文时,我发现“从未检索到任务异常”是否很重要,或者是常见的“标记不当”错误?

谢谢您,对于我的不良编码习惯以及缺乏使用Stack Overflow的经验,表示抱歉。

最佳答案

尝试这个。我自己还没有尝试过。这是我从您的问题和错误消息中可以理解的最佳答案:

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

关于python - Discord.py(重写): Error Handling “Improper Token” In Function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56624876/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com