Search code examples
pythonfunctiondiscord.pyargumentsparameter-passing

Can't pass multiple arguments to function in Python


It doesn't let me pass multiple arguments to my function. Python keeps thinking I'm still defining the first argument. The code:

async def find(ctx, user, parachannel):
    user = int(user)
    parachannel = int(parachannel)
    funchannel = bot.get_channel(parachannel)
    fun_guild = bot.get_guild(880108797820026881)
    memberuser = fun_guild.get_member(user)
    fun_calc = 0
    async for message in funchannel.history(limit=10):
        if message.author == memberuser:
            fun_calc = fun_calc + 1
    return fun_calc


gen_calc = find(user, 880123663318409277)
print(gen_calc)

Getting this error:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: find() missing 1 required positional argument: 'parachannel'

Full Traceback:

2021-09-16T11:40:52.403142+00:00 app[worker.1]: Ignoring exception in command analyze:
2021-09-16T11:40:52.404102+00:00 app[worker.1]: Traceback (most recent call last):
2021-09-16T11:40:52.404120+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core .py", line 85, in wrapped
2021-09-16T11:40:52.404121+00:00 app[worker.1]:     ret = await coro(*args, **kwargs)
2021-09-16T11:40:52.404122+00:00 app[worker.1]:   File "main.py", line 132, in analyze
2021-09-16T11:40:52.404123+00:00 app[worker.1]:     gen_calc = find(user, 880123663318409277)
2021-09-16T11:40:52.404142+00:00 app[worker.1]: TypeError: find() missing 1 required positional argument: 'parachannel'
2021-09-16T11:40:52.404151+00:00 app[worker.1]: 
2021-09-16T11:40:52.404152+00:00 app[worker.1]: The above exception was the direct cause of the following exception:
2021-09-16T11:40:52.404152+00:00 app[worker.1]: 
2021-09-16T11:40:52.404154+00:00 app[worker.1]: Traceback (most recent call last):
2021-09-16T11:40:52.404170+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 939, in invoke
2021-09-16T11:40:52.404170+00:00 app[worker.1]:     await ctx.command.invoke(ctx)
2021-09-16T11:40:52.404172+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 863, in invoke
2021-09-16T11:40:52.404173+00:00 app[worker.1]:     await injected(*ctx.args, **ctx.kwargs)
2021-09-16T11:40:52.404181+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 94, in wrapped
2021-09-16T11:40:52.404181+00:00 app[worker.1]:     raise CommandInvokeError(exc) from exc
2021-09-16T11:40:52.404195+00:00 app[worker.1]: discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: find() missing 1 required positional argument: 'parachannel'

Solution

  • Your function requires three arguments but you only pass two to it. So, naturally you see an error. You should give your parameters some default values. For example something like this:

    async def find(ctx = None, user = "Mushtaq", parachannel = 0):
        user = int(user)
        parachannel = int(parachannel)
    ... and the rest of your code.