[英]discord.py command in cogs file not working
當所有代碼都在main.py
中時,一切都可以正常工作,但后來我創建了 cogs 文件夾並創建了多個 cog files.py 並移動了代碼,現在?servers
命令不起作用。 我明白了
discord.ext.commands.errors.CommandNotFound:找不到命令“服務器”
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")
齒輪/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))
您不必在擴展中再次定義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))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.