1

Discord ボットの書き方を学んでいます。ボットの秘密鍵用に .env ファイルを設定しましたが、discord.errors.LoginFailure: Improper token has been passed.

秘密鍵を に直接入力するclient.run(KEY_HERE)と、問題なく接続され、エラーは発生しません。.env ファイルから呼び出すときにのみ問題が発生します。

ファイル ツリーは単なるプロジェクト ルートであり、bot.py と .env が含まれています。私はPyCharmを使用しています。

bot.py:

import os

import discord
import dotenv
from dotenv import load_dotenv

load_dotenv()
token = os.getenv('DISCORD_TOKEN')

client = discord.Client()

@client.event
async def on_ready():
    print("connected")

client.run(token)

.env:

DISCORD_TOKEN={"secret_key_here"}

完全なエラー出力は次のとおりです。

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/http.py", line 258, in static_login
    data = await self.request(Route('GET', '/users/@me'))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/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 "bot.py", line 16, in <module>
    client.run(token)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/client.py", line 598, in run
    return future.result()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/client.py", line 579, in runner
    await self.start(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/client.py", line 542, in start
    await self.login(*args, bot=bot)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/client.py", line 400, in login
    await self.http.static_login(token, bot=bot)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/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.

ありがとう!!

4

0 に答える 0