繁体   English   中英

Discord JS TOKEN + os.getenv

[英]Discord JS TOKEN + os.getenv

嘿,

我决定开始研究制作 python discord 机器人,我决定花一些时间研究免费代码营课程,展示如何掌握基础知识。 我的问题是,当我制作 main.py 和 the.env 时,代码中没有视觉错误,但是当我运行它时,我得到了这个。

$ python main.py
Traceback (most recent call last):
  File "D:\documents\python\Burger Bot\main.py", line 18, in <module>
    client.run(os.getenv('TOKEN'))
  File "C:\Users\keith\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 718, in run
    return future.result()
  File "C:\Users\keith\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 697, in runner
    await self.start(*args, **kwargs)
  File "C:\Users\keith\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 660, in start
    await self.login(*args, bot=bot)
  File "C:\Users\keith\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 509, in login
    await self.http.static_login(token.strip(), bot=bot)
AttributeError: 'NoneType' object has no attribute 'strip'

我的环境变量看起来像您期望的那样,TOKEN=[my token]。 除了 hello 命令开头的前缀之外,脚本完全相同。 但在这里。

import discord
import os

client = discord.Client()

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('?hello'):
        await message.channel.send('Hello!')

client.run(os.getenv('TOKEN'))

我不知道我是否做错了什么,但我正在本地解决这个问题,但我已经在 repli 上尝试过。 但我仍然得到相同的结果。 谁能告诉我我的问题是什么? ##

要从.env文件加载令牌,您应该使用python-dotenv

导入它

from dotenv import load_dotenv

并加载文件

load_dotenv() # You can put this wherever you want, but before retrieving the token

其他一切都应该保持原样

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM