我遇到了一个不寻常的问题,我不知道如何解决。我试图开始编写Discord机器人的代码,并一直在遵循教程,但是,当我运行以下代码行时,会出现错误。在发布之前,我已经更改了令牌。
import os
import discord
from dotenv import load_dotenv
load_dotenv()
token = os.getenv('NjgzODg1NjczNjg5OTA3MjE1.XlyOfw.UMm8vjHOgEbaSgfRMUglAimOP7Q')
client = discord.Client()
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
client.run(token) # The error occurs here
任何尝试运行此程序的帮助将不胜感激。
给出错误:
Traceback (most recent call last):
File "C:/Users/Jeffr/PycharmProjects/HypixelAPI/DiscordStatsBot.py", line 18, in <module>
client.run(token)
File "C:\Users\Jeffr\PycharmProjects\HypixelAPI\venv\lib\site-packages\discord\client.py", line 640, in run
return future.result()
File "C:\Users\Jeffr\PycharmProjects\HypixelAPI\venv\lib\site-packages\discord\client.py", line 621, in runner
await self.start(*args, **kwargs)
File "C:\Users\Jeffr\PycharmProjects\HypixelAPI\venv\lib\site-packages\discord\client.py", line 584, in start
await self.login(*args, bot=bot)
File "C:\Users\Jeffr\PycharmProjects\HypixelAPI\venv\lib\site-packages\discord\client.py", line 442, in login
await self.http.static_login(token.strip(), bot=bot)
AttributeError: 'NoneType' object has no attribute 'strip'
答案 0 :(得分:0)
根据您的情况,您必须直接分配令牌
token = 'NjgzODg1NjczNjg5OTA3MjE1.XlyOfw.UMm8vjHOgEbaSgfRMUglAimOP7Q'
函数os.getenv()
用于从系统变量中获取值-即。来自'DISCORD_TOKEN'
token = os.getenv('DISCORD_TOKEN')
这样,您就不会直接将token
保留在代码中,并且可以在论坛或GitHub上安全地共享代码。
如果代码有效,则可以搜索有关如何设置系统变量的信息,然后可以使用os.getenv()
。