【问题标题】:I got an error on programming a discord bot我在编写不和谐机器人时出错
【发布时间】:2021-11-04 02:19:09
【问题描述】:

我正在编写一个 python discord 机器人。我收到了这个错误:

代码:

    # jokeybotmain.py
import os

import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('ODg0NjkxODkzNTk2ODU2MzQw.YTcLiA.pKu1RIyV2HOTFhLOWQX0ryliyco')

client = discord.Client()

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

client.run(TOKEN)

【问题讨论】:

  • 您似乎使用了您的 实际令牌 作为环境变量名称,这可能不存在。这也意味着您已经公开共享了您的凭据,因此现在需要轮换它们。使用 env vars 的目的是在源代码中有秘密。

标签: python-3.x discord bots attributeerror


【解决方案1】:

错误


您的问题是您将令牌(从不和谐开发者门户复制)作为os.getenv 的参数。
您可以改为删除有关 dotenv 的部分并让您的机器人工作,如下所示:

import discord
TOKEN = "Your token here" # Remember to delete it before sending your code
# Use Bot instead of Client
# https://stackoverflow.com/questions/51234778/what-are-the-differences-between-bot-and-client
bot = discord.Bot(prefixes=['!'], intents=discord.Intents.all())
Bot.run(TOKEN)

【讨论】:

    【解决方案2】:
    import discord
    from discord.ext import commands
    
    TOKEN = "YOUR_TOKEN"
    client = commands.Bot(command_prefix="!")
    
    @client.event
    async def on_ready():
        print(f'{client.user} has connected to Discord!')
    
    client.run(TOKEN)
    

    【讨论】:

      猜你喜欢
      • 2020-12-18
      • 2020-07-08
      • 2021-07-09
      • 1970-01-01
      • 2020-07-25
      • 2019-12-10
      • 1970-01-01
      • 1970-01-01
      • 2021-05-23
      相关资源
      最近更新 更多