discord.py 循环导入(AttributeError:部分初始化模块'discord') [英] discord.py circular import (AttributeError: partially initialized module 'discord')

查看:15
本文介绍了discord.py 循环导入(AttributeError:部分初始化模块'discord')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个 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')

这篇关于discord.py 循环导入(AttributeError:部分初始化模块'discord')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆