TypeError:预期令牌是 str,却收到了 <class 'NoneType'>

问题描述 投票:0回答:4

我想制作一个简单的不和谐机器人。除了我希望它在特定时间发送消息之外,它的作用并不是太重要。下面的代码是非常基本的,不是成品。

# bot.py
import os

import discord
from dotenv import load_dotenv

intents = discord.Intents().all()
intents.messages = True

load_dotenv()
TOKEN = os.getenv("FAKETOKENBLAHBLAHBLAH")

client = discord.Client(command_prefix=',', intents=discord.Intents().all())

@client.event
async def on_ready():
    print(f'{client.user} has connected to Discord!')

client.run(TOKEN)

我做错了什么吗?如果是这样,请告诉我。我已经被难住了好几个小时了,我所能参考的就是这个错误:

TypeError: expected token to be a str, received <class 'NoneType'> instead

我理解回溯中对模块和行号的其他引用,但我就是不明白哪里出了问题,因为我没有编写discord.py模块。

我读过很多关于它的文章,但似乎没有一个起作用。我读的第一篇文章告诉我将“discord.Client()”的参数完全留空,这给了我这个错误:

TypeError: Client.__init__() missing 1 required keyword-only argument: 'intents'

来自链接:https://realpython.com/how-to-make-a-discord-bot-python/#creating-a-discord-account

我不明白,但我肯定知道最大的问题是关于discord.Client()的参数以及与意图有关的东西。

我觉得我还应该附上我的 .env 代码:

#.env
DISCORD_TOKEN={FAKETOKENBLAHBLAHBLAH}

更新: 我修复了这个代码:

TOKEN = os.getenv("DISCORD_TOKEN")

但是,现在它给了我这个例外:

[2022-08-22 01:20:03] [INFO    ] discord.client: logging in using static token
Traceback (most recent call last):
  File "C:\Users\domin\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\http.py", line 801, in static_login
    data = await self.request(Route('GET', '/users/@me'))
  File "C:\Users\domin\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\http.py", line 744, in request
    raise HTTPException(response, 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 "C:\Users\domin\AppData\Local\Programs\Python\Python310\Projects\Discord Bot Text Game\bot.py", line 19, in <module>
    client.run(TOKEN)
  File "C:\Users\domin\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py", line 828, in run
    asyncio.run(runner())
  File "C:\Users\domin\AppData\Local\Programs\Python\Python310\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Users\domin\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 646, in run_until_complete
    return future.result()
  File "C:\Users\domin\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py", line 817, in runner
    await self.start(token, reconnect=reconnect)
  File "C:\Users\domin\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py", line 745, in start
    await self.login(token)
  File "C:\Users\domin\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py", line 580, in login
    data = await self.http.static_login(token)
  File "C:\Users\domin\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\http.py", line 805, in static_login
    raise LoginFailure('Improper token has been passed.') from exc
discord.errors.LoginFailure: Improper token has been passed.

我该如何解决这个问题?我需要生成新的令牌吗?

python discord.py bots
4个回答
1
投票

虽然拥有完整的回溯通常很有用,但错误

TypeError:预期令牌是 str,却收到了

提示传入的

token
None
,而不是字符串。

您的代码

load_dotenv()
TOKEN = os.getenv("FAKETOKENBLAHBLAHBLAH")

尝试加载 .env 文件(将变量从该文件加载到进程环境),然后访问环境变量

FAKETOKENBLAHBLAHBLAH

如果您的

FAKETOKENBLAHBLAHBLAH=...
文件或运行程序的环境中没有
.env
,则
os.getenv()
将返回
None
,而
TOKEN
将为 None。

听起来您可能需要

os.getenv('DISCORD_TOKEN')
或类似的名称,因此您可以使用
DISCORD_TOKEN
作为环境变量名称。


1
投票

我有同样的问题,我在其他子目录中有具有相同标记的 .env 文件。所以我删除了它,只保留了一个 .env 文件,它就可以工作了。


0
投票
当使用错误的凭据发出请求时,通常会引发

HTTP 401错误。仔细检查您的代币。


0
投票

我猜您正在使用 Replit,但现在它不支持 .env 文件。相反,他们有秘密。 转到“工具”→“秘密”并创建一个秘密。

这是一个如何使用秘密的 Python 示例:

导入操作系统

值 = os.environ['key']

打印(值)

© www.soinside.com 2019 - 2024. All rights reserved.