【问题标题】:Callback for join command is missing "ctx" parameter加入命令的回调缺少“ctx”参数
【发布时间】:2020-04-28 16:02:05
【问题描述】:

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

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))

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

【问题讨论】:

    标签: python-3.x discord discord.py-rewrite


    【解决方案1】:

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

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

    你正在使用一个 cog,所以你有一个带有 self 的类,必须传递给没有用 @staticmethod 或 @classmethod 修饰的类。对于 discord.py,我建议您不要对上面的命令/事件函数执行上述任何一项操作。

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

    discord.py 中的齿轮

    【讨论】:

      猜你喜欢
      • 2021-01-24
      • 2021-02-01
      • 2020-04-02
      • 1970-01-01
      • 2020-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-11
      相关资源
      最近更新 更多