我试图让我的机器人更干净,我不想使用 COGS,所以我进行文件分割,但如果我尝试导入 .py 文件,它会导入,但会输出一个错误,指出它无法找到命令
所以,这是 Main.py 中的代码
import discord
from discord import Webhook, AsyncWebhookAdapter
from discord.ext import commands, tasks
from discord.ext.commands import Bot
client = commands.Bot(command_prefix = ">", intents=discord.Intents().all())
@commands.command()
async def test1(self, ctx, choice: int):
await ctx.message.delete()
await ctx.send("This is a Command in the Main py!")
from me import *
client.run("tokenKey", bot=True)
这段代码在me.py中
@commands.command()
async def suggest(self, ctx, *, message):
await ctx.send(message)
我总是收到此错误!
Traceback (most recent call last):
File "C:\Users\lequi\Desktop\main.py", line 13, in <module>
from me import *
File "C:\Users\lequi\Desktop\me.py", line 1, in <module>
@commands.command()
NameError: name 'commands' is not defined
您需要在主文件中导入
commands
from discord.ext import commands
还有一个更好的替代方法是使用 cogs
这是一个例子
import discord
from discord.ext import commands
class Me(commands.Cog):
def __init__(self, client):
self.client = client
@commands.command()
async def suggest(self, ctx, *, message):
# do some stuff
def setup(client):
client.add_cog(Me(client))
加载齿轮:
client.load_extension('me')
我认为这是因为你还没有定义命令.. 您可以将
commands.command()
更改为 client.command()
或者你可以改变
client = commands.Bot(command_prefix = ">", intents=discord.Intents().all())
到
commands = commands.Bot(command_prefix = ">", intents=discord.Intents().all())