当我调用我的 cogs 命令时,我没有得到任何回应
I got no response when I call my cogs commands
main.py
from dotenv import load_dotenv
import discord
import os
class BotClient(discord.Client):
"""Main bot class where all methods and vars are defined"""
def __init__(self, *args, **kwargs):
load_dotenv()
self.extensions = ['cogs.music']
intents = discord.Intents().default()
intents.members = True
self.client = commands.Bot(command_prefix="-", intents=intents)
for extension in self.extensions:
try:
self.client.load_extension(extension)
except Exception as e:
exc = '{}: {}'.format(type(e).__name__, e)
print('Failed to load extension {}\n{}'.format(extension, exc))
print("{} loaded".format(extension))
try:
super().__init__(*args, **kwargs)
self.client = super().run(os.getenv("TOKEN"), bot=True, reconnect=True)
except (KeyboardInterrupt, RuntimeError):
pass
client = BotClient()
cogs/music.py
import discord
from discord.ext import commands
class MusicCog(commands.Cog):
"""Music cogs class"""
def __init__(self, client):
self.client = client
@commands.command(name="test")
async def test(self, ctx):
print('executed')
try:
await ctx.send("simple command test")
except Exception as e:
print(str(e))
def setup(client):
client.add_cog(MusicCog(client))
我相信代码写得很好,因为我反复检查了它,但是当我运行“-test”命令
我希望你们中的一些人能够理解我错在哪里,并帮助我让我明白什么。
几小时后我找到了解决问题的方法。
main.py
from dotenv import load_dotenv
from discord.ext import commands
import discord
import os
class BotClient(commands.Bot):
"""Main bot class where all methods and vars are defined"""
def __init__(self, **options):
load_dotenv()
self._cogs = ['cogs.music']
intents = discord.Intents().all()
super().__init__(command_prefix="-", intents=intents)
for extension in self._cogs:
try:
super().load_extension(extension)
except Exception as e:
exc = '{}: {}'.format(type(e).__name__, e)
print('Failed to load extension {}\n{}'.format(extension, exc))
print("{} loaded".format(extension))
super().run(os.getenv("TOKEN"))
client = BotClient()
我将 discord.Client
更改为 commands.Bot
并调整了 BotClient
class 以便与 commands.Bot
一起使用
另一个class(MusicCog
)的代码没有更改
main.py
from dotenv import load_dotenv
import discord
import os
class BotClient(discord.Client):
"""Main bot class where all methods and vars are defined"""
def __init__(self, *args, **kwargs):
load_dotenv()
self.extensions = ['cogs.music']
intents = discord.Intents().default()
intents.members = True
self.client = commands.Bot(command_prefix="-", intents=intents)
for extension in self.extensions:
try:
self.client.load_extension(extension)
except Exception as e:
exc = '{}: {}'.format(type(e).__name__, e)
print('Failed to load extension {}\n{}'.format(extension, exc))
print("{} loaded".format(extension))
try:
super().__init__(*args, **kwargs)
self.client = super().run(os.getenv("TOKEN"), bot=True, reconnect=True)
except (KeyboardInterrupt, RuntimeError):
pass
client = BotClient()
cogs/music.py
import discord
from discord.ext import commands
class MusicCog(commands.Cog):
"""Music cogs class"""
def __init__(self, client):
self.client = client
@commands.command(name="test")
async def test(self, ctx):
print('executed')
try:
await ctx.send("simple command test")
except Exception as e:
print(str(e))
def setup(client):
client.add_cog(MusicCog(client))
我相信代码写得很好,因为我反复检查了它,但是当我运行“-test”命令
我希望你们中的一些人能够理解我错在哪里,并帮助我让我明白什么。
几小时后我找到了解决问题的方法。
main.py
from dotenv import load_dotenv
from discord.ext import commands
import discord
import os
class BotClient(commands.Bot):
"""Main bot class where all methods and vars are defined"""
def __init__(self, **options):
load_dotenv()
self._cogs = ['cogs.music']
intents = discord.Intents().all()
super().__init__(command_prefix="-", intents=intents)
for extension in self._cogs:
try:
super().load_extension(extension)
except Exception as e:
exc = '{}: {}'.format(type(e).__name__, e)
print('Failed to load extension {}\n{}'.format(extension, exc))
print("{} loaded".format(extension))
super().run(os.getenv("TOKEN"))
client = BotClient()
我将 discord.Client
更改为 commands.Bot
并调整了 BotClient
class 以便与 commands.Bot
另一个class(MusicCog
)的代码没有更改