【问题标题】:How to fix error: module 'discord' has no attribute 'Bot'如何修复错误:模块 'discord' 没有属性 'Bot'
【发布时间】:2021-10-26 08:39:33
【问题描述】:

情况:

我正在尝试使用 pycord 制作一个简单的不和谐机器人,但每次运行代码时都会出现此错误:

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    bot = discord.Bot()
AttributeError: module 'discord' has no attribute 'Bot'

代码:

import discord

bot = discord.Bot()

@bot.slash_command()
async def test(ctx):
  await ctx.send('Success!')

bot.run('token')

我做了什么:

我已经检查过我是否安装了 pycord 以及我的令牌是否正确。

【问题讨论】:

    标签: python discord pycord


    【解决方案1】:

    PyCord 2 beta 1 刚刚发布,您现在可以使用

    pip install py-cord==2.0.0b1
    

    而不是从源代码安装版本。

    为了让示例正常工作,您需要将 applications.commands 范围添加到您的 OAuth2 URL 并在您的测试服务器上重新注册您的机器人。

    另外,quickstart guide 现在建议在创建slash_command 时添加公会(服务器)ID 列表:

    guild_ids 属性包含一个行会列表,其中该命令 将是活跃的。如果省略它,该命令将是全局的 可用,最多可能需要一个小时才能注册。

    【讨论】:

      【解决方案2】:

      查看您的错误消息时:

      Traceback (most recent call last):
        File "main.py", line 3, in <module>
          bot = discord.Bot()
      AttributeError: module 'discord' has no attribute 'Bot'
      

      关键是要注意 AttributeError 告诉你你导入的模块没有属性 Bot()

      这说明你用错了。

      查看documentation 以了解正确用法以及此tutorial

      你会看到你需要使用。

      
      # bot.py
      import os
      
      import discord
      from dotenv import load_dotenv
      
      load_dotenv()
      TOKEN = os.getenv('DISCORD_TOKEN')
      
      client = discord.Client()
      
      @client.event
      async def on_ready():
          print(f'{client.user} has connected to Discord!')
      
      client.run(TOKEN)
      

      @Taku 发表评论后编辑

      在下面的评论之后,我认为可能需要像 example 中所做的那样升级库

      除了需要example in the url 中的命令前缀

      import discord
      from discord.ext import commands
      
      bot = commands.Bot(command_prefix=">")
      
      @bot.command()
      async def ping(ctx):
          await ctx.send("pong")
      
      bot.run("token")
      

      【讨论】:

      【解决方案3】:

      这个错误的原因是你的 pycord 版本是 v1.7.3,它不支持你使用的语法。您需要使用以下命令升级到 v2.0.0 (Windows):

      git clone https://github.com/Pycord-Development/pycord
      cd pycord
      pip install -U . 
      

      pip install -U .[voice],如果您需要语音支持

      【讨论】:

      • 最佳评论,实际上解决了问题并解释了问题所在,+1
      猜你喜欢
      • 2020-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-22
      • 2021-01-14
      • 1970-01-01
      • 1970-01-01
      • 2021-10-14
      相关资源
      最近更新 更多