【发布时间】:2021-10-29 12:49:41
【问题描述】:
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”命令时,我仍然没有得到机器人的任何回复
我希望你们中的一些人能够理解我错在哪里,并帮助我理解什么。
【问题讨论】:
-
“我没有得到回应”是什么意思?详细解释您如何使用代码、您希望看到的确切内容(在终端窗口和 Discord 上)以及您实际看到的内容。另外:即使没有 cogs 功能,您能否使命令正常工作?例如,您是否能够验证
MusicCog.__init__运行?还有setup函数? -
我希望当有人写“-test”时机器人回复“简单命令测试”目前当我发送“-test”消息时,机器人没有回复“简单命令测试”我'我确定机器人能够阅读聊天,因为我尝试使用 on_message 事件它可以正确看到消息
-
我刚刚检查并正确调用了
MusicCog.__ init__和setup
标签: python discord.py command