python - discord\client.py 错误中的 python 脚本上的追溯错误?
问题描述
我无法在 Visual Studio Code 中正确运行任何 python 代码以防不和谐。错误消息是持久的,无论我使用什么方法访问文件,错误都会不断返回。代码如下:
def read_token():
path = '/path/used/to.txt'
with open(path, "r") as f:
lines = f.read()
return lines[0].strip()
token = read_token()
print(token)
try:
bot.run(token)
except discord.errors.LoginFailure as e:
print('login failed, ERROR 401 unauthorized')
这样做将在终端上输出:
<function read_token at 0x7f2d0edba268>
login failed, ERROR 401 unauthorized
该错误提供了以下信息:
File "/home/falwaeth/.local/lib/python3.6/site-packages/discord/http.py", line 256, in static_login
data = await self.request(Route('GET', '/users/@me'))
File "/home/falwaeth/.local/lib/python3.6/site-packages/discord/http.py", line 220, 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 "/home/falwaeth/BotDev/bot.py", line 32, in <module>
bot.run(f'{token}')
File "/home/falwaeth/.local/lib/python3.6/site-packages/discord/client.py", line 640, in run
return future.result()
File "/home/falwaeth/.local/lib/python3.6/site-packages/discord/client.py", line 621, in runner
await self.start(*args, **kwargs)
File "/home/falwaeth/.local/lib/python3.6/site-packages/discord/client.py", line 584, in start
await self.login(*args, bot=bot)
File "/home/falwaeth/.local/lib/python3.6/site-packages/discord/client.py", line 442, in login
await self.http.static_login(token.strip(), bot=bot)
File "/home/falwaeth/.local/lib/python3.6/site-packages/discord/http.py", line 260, in static_login
raise LoginFailure('Improper token has been passed.') from exc
discord.errors.LoginFailure: Improper token has been passed.
我不明白的是不正确的令牌。如果我将 bot.run() 更改为其中包含实际令牌,它将起作用。.txt 文件的权限设置为所有人都可以阅读。有任何想法吗?
bot.run() 仅在令牌位于内部时才起作用,我不认为我打算从 python 中的 .txt 文件中读取。我在这个工作目录上改变了一些东西,它现在运行 python3.7.5。输入此代码后,我发现了另一个问题:
path = '/path/used/to.txt'
with open(path,'r') as f:
lines = f.readlines()
return lines[0].strip()
token = read_token
bot.run(token)
现在给我这个错误:
File "/home/falwaeth/BotDev/welcomeBot.py", line 31, in <module>
bot.run(token)
File "/home/falwaeth/.local/lib/python3.7/site-packages/discord/client.py", line 640, in run
return future.result()
File "/home/falwaeth/.local/lib/python3.7/site-packages/discord/client.py", line 621, in runner
await self.start(*args, **kwargs)
File "/home/falwaeth/.local/lib/python3.7/site-packages/discord/client.py", line 584, in start
await self.login(*args, bot=bot)
File "/home/falwaeth/.local/lib/python3.7/site-packages/discord/client.py", line 442, in login
await self.http.static_login(token.strip(), bot=bot)
AttributeError: 'function' object has no attribute 'strip'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
from apport.packaging_impl import impl as packaging
File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 24, in <module>
import apt
File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Original exception was:
Traceback (most recent call last):
File "/home/falwaeth/BotDev/welcomeBot.py", line 31, in <module>
bot.run(token)
File "/home/falwaeth/.local/lib/python3.7/site-packages/discord/client.py", line 640, in run
return future.result()
File "/home/falwaeth/.local/lib/python3.7/site-packages/discord/client.py", line 621, in runner
await self.start(*args, **kwargs)
File "/home/falwaeth/.local/lib/python3.7/site-packages/discord/client.py", line 584, in start
await self.login(*args, bot=bot)
File "/home/falwaeth/.local/lib/python3.7/site-packages/discord/client.py", line 442, in login
await self.http.static_login(token.strip(), bot=bot)
AttributeError: 'function' object has no attribute 'strip'
知道为什么吗?
解决方案
f.read()
将文件的内容作为字符串返回,因此lines[0].strip()
将是该文件的第一个字符。我怀疑你想要这个:
path = '/path/used/to.txt'
with open(path, "r") as f:
lines = f.readlines()
return lines[0].strip()
推荐阅读
- bash - 在 bash 脚本中处理一组 git 命令
- php - 如何使用 kartik mpdf 扩展强制 PDF 在 Yii2 中打开
- java - 在 Java 中,如何在评估 emptyConstructor 条件时区别对待空类构造函数?
- c# - 在 Pcap.Net 中实现 TCP/UDP 数据包的 IP 分片
- python - 如何在 Python 文件中的函数前添加一行
- mysql - Rails:通过连接表查询两步关系+有很多关系
- c# - 创建多个 IBM Watson 服务
- java - 如何使用groovy从soapui Json Response构造JsonPath?
- spring-boot - zuul(没有尤里卡) - 总是以“转发错误”结束
- ruby - NLS_LANG = 'AMERICAN_AMERICA.UTF8 Ruby rake 问题