加入命令的回调缺少 "ctx" 参数

Callback for join command is missing "ctx" parameter

我正在尝试为我的机器人制作一个音乐齿轮。我目前正在为机器人发出加入和离开语音频道的命令。但是每次我尝试启动这两个命令时,都会出现相同的错误。

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\Daniel\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 663, in _parse_arguments
    next(iterator)
StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Daniel\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\Daniel\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 930, in on_message
    await self.process_commands(message)
  File "C:\Users\Daniel\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 927, in process_commands
    await self.invoke(ctx)
  File "C:\Users\Daniel\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Daniel\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 790, in invoke
    await self.prepare(ctx)
  File "C:\Users\Daniel\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 751, in prepare
    await self._parse_arguments(ctx)
  File "C:\Users\Daniel\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 666, in _parse_arguments
    raise discord.ClientException(fmt.format(self))
discord.errors.ClientException: Callback for join/leave command is missing "ctx" parameter.

这是我的代码:

import discord
from discord.ext import commands

import youtube_dl

class Music(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.command()
    async def join(ctx):
        channel = ctx.message.author.voice.channel
        await channel.connect()

    @commands.command()
    async def leave(ctx):
        await ctx.voice_client.diconnect()


def setup(client):
    client.add_cog(Music(client))

有人知道我该如何解决这个问题吗?

您需要将 self 放入您的函数 async def join 以及您的 leave 函数中:

@commands.command()
async def join(self, ctx):
    channel = ctx.message.author.voice.channel
    await channel.connect()

你正在使用一个齿轮,所以你有一个 class with self must 传递给 classes 没有用 @staticmethod 或 @ 装饰class方法。对于 discord.py,我建议您不要对上述 command/event 函数执行任何操作。

参见:https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html

对于 discord.py

中的齿轮