简体   繁体   中英

discord.py command in cogs file not working

Everything used to work fine when all code was in main.py but then I created cogs folder and created multiple cog files.py and moved code around, now ?servers command is not working. I get

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))

You don't have to define client again in the extension, simply delete that line. Also the decorator @commands.command should be called (with () )

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM