【问题标题】:Context Parameter missing in discordpy botdiscordpy bot 中缺少上下文参数
【发布时间】:2022-12-18 23:04:07
【问题描述】:

我刚开始使用 Discord.py 并遇到以下错误说明:

Traceback (most recent call last):
  File "C:\Users\Entropy\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\Entropy\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\bot.py", line 979, in on_message
    await self.process_commands(message)
  File "C:\Users\Entropy\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\bot.py", line 976, in process_commands
    await self.invoke(ctx)
  File "C:\Users\Entropy\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Entropy\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 855, in invoke
    await self.prepare(ctx)
  File "C:\Users\Entropy\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 789, in prepare
    await self._parse_arguments(ctx)
  File "C:\Users\Entropy\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 693, in _parse_arguments
    raise discord.ClientException(fmt.format(self))
discord.errors.ClientException: Callback for ping command is missing "ctx" parameter.

每当我在 Discord 中输入 .hello、.ping 或 .close 时都会给出错误消息

这是主文件:

import os
import discord
from discord.ext import commands
from dotenv import load_dotenv
from botclass import *

#esc = True
load_dotenv()
tken = os.getenv('DISCORD_TOKEN')

intents = discord.Intents.default()
intents.members = True

client = commands.Bot(command_prefix='.', intents =intents)
client.load_extension('botclass')
print('\n'+tken+'\n')
client.run(tken)

这是包含您可以给出的命令的文件

from attr import attributes
import discord
from discord.ext import commands


class DaCommands(commands.Cog):

    def __init__(self, bot: commands.Bot):
        self.bot = bot
        
    @commands.command(name='hello',pass_context=True)
    async def hello(ctx):
        await ctx.reply('hello')

    @commands.command(name="ping",pass_context=True)
    async def ping(ctx: commands.Context):
        await ctx.send(f'ping! ' + str(round(commands.latency * 1000)) + 'ms')

    @commands.command(name='close',pass_context=True)
    async def exits(ctx: commands.Context):
        await ctx.send('Closed')
        await quit()

def setup(client: commands.Bot):
    client.add_cog(DaCommands(client))

感谢您提前解决我的问题

【问题讨论】:

    标签: python discord.py runtime-error


    【解决方案1】:

    命令方法中缺少 self

    async def hello(self, ctx: commands.Context):
    

    这是您在使用机器人时应该知道和理解的基本 OOP / 类功能。

    也不要使用手动导入你的齿轮

    from botclass import *
    

    这会污染命名空间并且是不必要的。 您应该使用 lib(例如 pathlib)来获取保存机器人扩展的目录中的文件,然后使用 await bot.load_extension(file)

    请注意,这在 dpy 2.x 中是异步的

    pass_context=True 是非常陈旧和过时的代码,不再需要。

    我只能假设您遵循的是非常过时的在线教程。我建议您查看官方 dpy github repo 上的示例,他们的文档并加入 discord 以获得进一步的帮助。

    【讨论】:

    • 好吧,谢谢你,我仍然是一个学习者,我在学习每一行
    猜你喜欢
    • 2021-08-23
    • 2021-08-05
    • 2022-11-12
    • 1970-01-01
    • 2020-09-25
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    相关资源
    最近更新 更多