在discord \ client.py错误内的python脚本上有回溯错误?

时间:2020-02-03 15:46:47

标签: python bots discord

我无法正确运行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'

知道为什么吗?

1 个答案:

答案 0 :(得分:0)

f.read()将以字符串形式返回文件的内容,因此lines[0].strip()将是该文件的第一个字符。我怀疑您想要这个:

path = '/path/used/to.txt'
with open(path, "r") as f:
    lines = f.readlines()
    return lines[0].strip()