【发布时间】:2020-08-31 00:21:31
【问题描述】:
我正在尝试创建一个简单的命令,在 discord.py 中用 cog 打招呼,但收到一个错误: TypeError: __init__() missing 1 required positional argument: 'func'。我知道应该使用 cogs 来组织一组相关的命令。我只是在测试,以便我可以创建一个更复杂的机器人。我查看了docs,但没有发现任何重要的东西。也许我错过了一个非常明显的事情。请帮忙。我是初学者。
这是我的代码:
class Test_Cog(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.Command()
async def sayhi(self, ctx):
await ctx.send('hi')
class Test_Application_Bot(commands.Bot):
async def on_ready(self):
print("Logged on as", self.user)
async def on_message(self, message):
# so that it doesn't respond to itself
if message.author == self.user:
return
print(message.content)
if message.content == "ping":
await message.channel.send("pong")
await self.process_commands(message)
def read_token():
with open("token.txt", "r") as f:
lines = f.readlines()
return lines[0].strip()
def main():
bot = Test_Application_Bot(command_prefix="!")
bot.add_cog(Test_Cog(bot))
bot.run(read_token())
if __name__ == "__main__":
main()
【问题讨论】:
-
您能添加完整的错误跟踪吗?
-
@commands.Command()应该是@commands.command() -
@PatrickHaugh 谢谢!它完美地工作。 @Anwarvic 如果您想要完整的错误跟踪,它是
Traceback (most recent call last): File "discord_bot.py", line 7, in <module> class Test_Cog(commands.Cog): File "discord_bot.py", line 11, in Test_Cog @commands.Command() TypeError: __init__() missing 1 required positional argument: 'func'