cogs文件中的discord.py命令不起作用

2024-04-16 13:47:56 发布

您现在位置:Python中文网/ 问答频道 /正文

当所有代码都在main.py中时,一切都正常工作,但后来我创建了cogs文件夹,创建了多个cog files.py并移动了代码,现在?servers命令不起作用。我明白了

discord.ext.commands.errors.CommandNotFound: Command "servers" is not found

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.members = True
intents.presences = True
client = commands.Bot(command_prefix='?', intents=intents, help_command=None)

@client.event
async def on_ready():
    print('ARB is logged in.')
    await client.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name='@ARB'))
   
client.load_extension("cogs.commands")
client.load_extension("cogs.owner")
client.load_extension("cogs.fun")
client.load_extension("cogs.arb")

cogs/owner.py

import discord
from discord.ext import commands

client = commands.Bot

class Owner(commands.Cog):
    def __init__(self, client):
        self.client = client 
    
    @commands.command
    @commands.is_owner()
    async def servers(self, ctx):
        guildcount = 0
        activeservers = client.guilds
        print('********************** GUILD LIST **********************')
        for guild in activeservers:
            guildcount += 1
            print(f'{guild.name} - ({guild.id})')
        print(f'ARB is in {guildcount} servers.')        
        
def setup(client): 
    client.add_cog(Owner(client))

Tags: pyimportclientisdefextensionloadext
1条回答
网友
1楼 · 发布于 2024-04-16 13:47:56

您不必在扩展中再次定义client,只需删除该行即可。还应该调用装饰程序@commands.command(使用()

import discord
from discord.ext import commands

class Owner(commands.Cog):
    def __init__(self, client):
        self.client = client 
    
    @commands.command()
    @commands.is_owner()
    async def servers(self, ctx):
        guildcount = 0
        activeservers = client.guilds
        print('********************** GUILD LIST **********************')
        for guild in activeservers:
            guildcount += 1
            print(f'{guild.name} - ({guild.id})')
        print(f'ARB is in {guildcount} servers.')        
        
def setup(client): 
    client.add_cog(Owner(client))

相关问题 更多 >