首页 > 解决方案 > Discord.py 从其他文件导入命令总是失败

问题描述

我尝试让我的机器人更清洁,我不想使用 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("I like ya Cut G", 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

标签: pythondiscorddiscord.pydiscord.py-rewrite

解决方案


首先,您从该client.run行发布了您的不和谐客户端令牌。如果我是你,我会尽快编辑并更改机器人令牌。除了您之外,没有人应该有权访问它。


推荐阅读