大佬教程收集整理的这篇文章主要介绍了类型错误:func() 缺少 1 个必需的位置参数:从 discord.py 中的不同文件夹导入函数时出现“self”,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在文件夹 (cog) 中的 code.py 中有一个类,如下所示:
import discord from discord.ext import commands class Class(commands.Cog): def __init__(self): # Basic initialisation @commands.command() async def func(self,ctx = commands.Context): # Code for the command
@H_301_5@当我尝试导入它(使用
APscheduler
将其添加到调度程序)时,如下所示:from folder import code async def schedule_func(): func_class = code.Class() await func_class.func()
@H_301_5@我收到以下错误:
file "bot/main.py",line 45,in schedule_meme await func_class.func() file "/python/lib/python3.8/site-packages/discord/ext/commands/core.py",line 374,in __call__ return await self.callBACk(*args,**kwargs) TypeError: func() missing 1 required positional argument: 'self'
@H_301_5@我到处查看并仔细检查我是否首先初始化了课程,但仍然无法克服错误。
我在这里做错了什么?解决方法
@H_944_30@@H_944_30@@H_944_30@不确定这里发生了什么,它是可重现的,并且当我简单地将 ctx 作为随机值传递时它就起作用了。
await func_class.func(commands.Context) #if you need to use it,or just 1 or any literal
@H_944_30@编辑:
commands.command()
装饰器似乎是这里的问题,删除它使其在没有参数的情况下运行,我的猜测是装饰器将 ctx 设为必需参数,如果您不想使用 ctx。您可以将装饰器更改为@commands.command(pass_context=falsE)
并且无需任何参数即可正常运行大佬总结
以上是大佬教程为你收集整理的类型错误:func() 缺少 1 个必需的位置参数:从 discord.py 中的不同文件夹导入函数时出现“self”全部内容,希望文章能够帮你解决类型错误:func() 缺少 1 个必需的位置参数:从 discord.py 中的不同文件夹导入函数时出现“self”所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。