【问题标题】:I got no response when I call my cogs commands当我调用我的 cogs 命令时没有响应
【发布时间】: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


【解决方案1】:

几个小时后,我找到了解决问题的方法。

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 类以与 commands.Bot 一起使用

其他类的代码(MusicCog)没有改过

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-15
    • 2022-10-24
    • 1970-01-01
    • 1970-01-01
    • 2022-10-12
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    相关资源
    最近更新 更多