簡體   English   中英

我正在制作 discord 機器人,但出現錯誤(python)

[英]Im making a discord bot and i got an error (python)

我離開了一個網站( https://realpython.com/how-to-make-a-discord-bot-python/#how-to-make-a-discord-bot-in-python )並且我按照那里的步驟但我不斷收到錯誤消息

回溯(最近一次通話):文件“C:\Users\Bryce.Persello346\Desktop\bot.py”,第 15 行,在 client.run(TOKEN) 文件“C:\Users\Bryce.Persello346\AppData\Local \Programs\Python\Python39\lib\site-packages\discord\client.py”,第 718 行,在運行中返回 future.result() 文件“C:\Users\Bryce.Persello346\AppData\Local\Programs\Python\ Python39\lib\site-packages\discord\client.py”,第 697 行,在運行器中等待 self.start(*args, **kwargs) 文件“C:\Users\Bryce.Persello346\AppData\Local\Programs\Python \Python39\lib\site-packages\discord\client.py”,第 660 行,在開始等待 self.login(*args, bot=bot) 文件“C:\Users\Bryce.Persello346\AppData\Local\Programs\ Python\Python39\lib\site-packages\discord\client.py",第 509 行,登錄等待 self.http.static_login(token.strip(), bot=bot) AttributeError: 'NoneType' object has no attribute'strip '

我的代碼:


import os

import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('"token here"')

client = discord.Client()

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

client.run(TOKEN)

在實際 python 的示例中,您可能犯了將密鑰放在花括號中的錯誤。 所以基本上只是像這樣格式化你的env文件......

DISCORD_TOKEN = token_goes_here

代替...

DISCORD_TOKEN = {token_goes_here}

問題似乎在於令牌的存儲方式

正如錯誤所說,discord.py 無法使用令牌,因為它沒有正確存儲

如果您在自己的本地系統上運行代碼,請盡量不要使用環境變量

import os
import discord
TOKEN = "ABC.123.XYZ"
client = discord.Client()

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

client.run(TOKEN)

如果您在在線 ide(replit 或其他)上運行代碼,請執行

import os
import discord
TOKEN = os.environ["token"]
client = discord.Client()

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

client.run(TOKEN)

希望這可以幫助:)

暫無
暫無

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

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