簡體   English   中英

discord\\client.py 錯誤中的python 腳本上的回溯錯誤?

[英]Traceback errors on python script within the discord\client.py error?

我無法在 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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM