如何在 discord.py 中修复 "discord.ext.commands.errors.MissingRequiredArgument: ctx is a required argument that is missing."

How to fix "discord.ext.commands.errors.MissingRequiredArgument: ctx is a required argument that is missing." in discord.py

我正在尝试使用子类创建一个简单的机器人来处理命令,但我遇到了这个错误

这是回溯:

Ignoring exception in command teste: Traceback (most recent call last): File "/home/mrtrue/.local/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 863, in invoke await ctx.command.invoke(ctx) File "/home/mrtrue/.local/lib/python3.8/site-packages/discord/ext/commands/core.py", line 721, in invoke await self.prepare(ctx) File "/home/mrtrue/.local/lib/python3.8/site-packages/discord/ext/commands/core.py", line 685, in prepare await self._parse_arguments(ctx) File "/home/mrtrue/.local/lib/python3.8/site-packages/discord/ext/commands/core.py", line 599, in _parse_arguments transformed = await self.transform(ctx, param) File "/home/mrtrue/.local/lib/python3.8/site-packages/discord/ext/commands/core.py", line 445, in transform raise MissingRequiredArgument(param) discord.ext.commands.errors.MissingRequiredArgument: ctx is a required argument that is missing.

代码如下:

import discord
from discord.ext import commands
import inspect

class BotLibertarin(commands.Bot):
    client = discord.Client()

    @client.event
    async def on_message(self,message):
        print(f"message from {message.author} what he said {message.content}")
        await self.process_commands(message)
class CommandsHandler(BotLibertarin):
    def __init__(self):
        super().__init__(command_prefix=".")

        members = inspect.getmembers(self)
        for name, member in members:
            if isinstance(member,commands.Command):
                if member.parent is None:
                    self.add_command(member)

    @commands.command()
    async def teste(self,ctx):
        await ctx.channel.send("teste")

我真的建议你花时间阅读 docs。 我还要提醒您,在没有发出特定命令(例如 !commandname)的情况下发送消息是违反 ToS 的[即使显然没有人遵循 ToS]。

无论哪种方式。试试这个:

import discord
from discord.ext import commands

client = commands.Bot(command.prefix='!')

@client.event
async def on_message(message):
    print(f"message from {message.author} what he said {message.content}")
    await message.channel.send(message.content)

@client.command()
async def teste(ctx):
    await ctx.channel.send("teste")