簡體   English   中英

我不斷收到錯誤:AttributeError: 'NoneType' object has no attribute 'strip'

[英]I keep getting the error: AttributeError: 'NoneType' object has no attribute 'strip'

我在這里使用 python。

當我為正在使用 repli 制作的 discord 機器人運行代碼時,出現此錯誤: AttributeError: 'NoneType' object has no attribute 'strip'

我正在使用本教程制作 discord 機器人: https://www.freecodecamp.org/news/create-a-discord-bot-with-python/

代碼:

import discord
import os

client = discord.Client()

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

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

    if message.content.startswith('$IHaveNoBobux'):
        await message.channel.send('haha sucks to be you B)')
   
    if message.content.startswith('$GiveMeFreeBobuxNow'):
        await message.channel.send('no stupid noob B)')

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

有人可以幫我解決這個錯誤嗎? 我的大腦很小,幾乎沒有 python 所以有些事情可能需要大量解釋。 :/

這是我運行代碼時的完整控制台:

回溯(最后一次調用):文件“main.py”,第 21 行,在 client.run(os.getenv('TOKEN')) 文件“/home/runner/go-away-this-isnt-for-you /venv/lib/python3.8/site-packages/discord/client.py”,第 723 行,在運行中返回 future.result() 文件“/home/runner/go-away-this-isnt-for-you/ venv/lib/python3.8/site-packages/discord/client.py”,第 702 行,在運行器中等待 self.start(*args, **kwargs) 文件“/home/runner/go-away-this-isnt -for-you/venv/lib/python3.8/site-packages/discord/client.py”,第 665 行,在開始等待 self.login(*args, bot=bot) 文件“/home/runner/go- away-this-isnt-for-you/venv/lib/python3.8/site-packages/discord/client.py",第 511 行,登錄等待 self.http.static_login(token.strip(), bot=bot ) AttributeError: 'NoneType' object 沒有屬性 'strip'

我還有一個名為“env”的環境,其中包含以下內容:

TOKEN=[the discord bots token is would be right here but im not going to show it because its the password]

請按照上述步驟獲取令牌,它是空的。

當您在 Discord 上創建機器人用戶時,您復制了一個令牌。 現在我們將創建一個 .env 文件來存儲令牌。 如果您在本地運行代碼,則不需要 .env 文件。 只需將 os.getenv('TOKEN') 替換為令牌。

該文件應該被稱為.env而不僅僅是env 假設它已經命名並且它仍然對您不起作用,如果您需要將其上傳到遠程存儲庫,您可以使用 json 文件(不要忘記在.gitignore 中添加 json 文件)。

import json
import discord 

client = discord.Client()

env_json = json.loads("env.json")
key = env_json['TOKEN']

@client.event
async def on_ready():
    print(f'We have logged in as {client.username}')

client.run(key)

暫無
暫無

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

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