discord.py 循环导入(AttributeError:部分初始化模块'discord')

2022-01-15 00:00:00 python circular-dependency discord.py

问题描述

我正在尝试编写一个 Discord 机器人,但该机器人无法启动并引发此错误:

I am trying to write a Discord bot, but the bot won't start and raises this error:

AttributeError: partially initialized module 'discord' has no attribute 'Client' (most likely due to a circular import)


解决方案

你的标题(在你有任何函数或方法调用之前)应该包括以下项目:

Your header (before you have any functions or method calls) should include the following items:

import discord
from discord.ext import commands

您还需要初始化您的 discord 客户端.在这种情况下,客户端代表您的脚本与不和谐服务器的连接.如果你不初始化,你的命令都不会起作用.

You also need to initialize your discord client. In this case, a client represents your script's connection to the discord server. If you don't initialize, none of your commands will work.

client = commands.Bot(command_prefix=".")

在括号内,您可以声明命令的前缀.例如,.help 与 !help.

within the parenthesis, you can declare the prefix for your commands. Ex, .help vs !help.

最后,您需要将客户端连接到服务器并在连续事件循环中运行它.这应该在文件的末尾.

Lastly, you need to connect your client to the server and run it in a continuous event loop. This should be at the end of your file.

client.run('your bot's token')

相关文章