stat.us stat.us - 3 months ago 16
Python Question

Decorator not working correctly

I'm trying to get my decorator working but I keep getting returned with

TypeError: command() takes 1 positional argument but 2 were given


I'm not sure what is happening, is someone able to explain to me what I'm doing wrong?

Decorator Code

def command(command):
def method(self, *args, **kwargs):
return command(self, *args, **kwargs)
return method


Function using Decorator

@bot.command
async def speak(msg : str):
await bot.say(msg)


Demonstration

I would like to have a bot that'll react to commands on a trigger.

For example, I'll send a chat message:

?eval 1 * 2


which the bot will reply with

2


By using the
@bot.command
, I'll be able to create a flexible system which tells the application that the function is a command on-trigger.

e.g

@bot.command
async def eval(self, *args)
await bot.send(eval(*args))

Answer

You are decorating with bot.command.

If bot is an object, then bot.command is a method and is called with a self parameter before the command argument, hence the “2 were given”.

Comments