A
A
AdvoKappa2020-06-07 15:28:39
Python
AdvoKappa, 2020-06-07 15:28:39

Discord.ext.commands.errors.MissingRequiredArgument: users is a required argument that is missing, what should I do?

The code is like this:

import discord
from discord.ext import commands
from discord.ext.commands import Bot
import json
import os

bot = commands.Bot(command_prefix='$')
os.chdir(r'D:\Sublime Text 3\testbot-master')

@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')

@bot.event
async def on_member_join(member):
    with open('users.json', 'r') as f:
        users = json.load(f)

    await update_data(users, member)

    with open('users.json','w') as f:
        json.dump(users, f)

@bot.event
async def on_message(message):
    await bot.process_commands(message)
    with open('users.json', 'r') as f:
        users = json.load(f)

        async def update_data(users, user):
            if not str(user.id) in users:
                users[str(user.id)] = {}
                users[str(user.id)]['experience'] = 0
                users[str(user.id)]['level'] = 1

        await update_data(users, message.author)
        await add_experience(users, message.author, 5)
        await level_up(users, message.author, message.channel)

    with open('users.json','w') as f:
        json.dump(users, f)

    if message.content == '!Rank':
        async def sent(users, user):
            experience = users[str(user.id)]['experience']
            lvl_start = users[str(user.id)]['level']
            lvl_end = int(experience ** (1/50))

            channel = message.channel
            await channel.send(':thumbsup: {}, Ваш уровень: {} :thumbsup: '.format(user.mention, lvl_end))
            users[str(user.id)]['level'] = lvl_end

async def add_experience(users, user, exp):
    users[str(user.id)]['experience'] += exp

async def level_up(users, user, channel):
    experience = users[str(user.id)]['experience']
    lvl_start = users[str(user.id)]['level']
    lvl_end = int(experience * (1/50))

    if lvl_start < lvl_end:
        await channel.send(channel, f":thumbsup: {user.mention}, вы повысились до {lvl_end} уровня! :thumbsup: ")
        users[user.id]["level"] = lvl_end

@bot.command(pass_content=True)
async def Rank(message, users, user):
    if message.content == '!Rank':
            experience = users[str(user.id)]['experience']
            lvl_start = users[str(user.id)]['level']
            lvl_end = int(experience ** (1/50))

            channel = message.channel
            await channel.send(':thumbsup: {}, Ваш уровень: {} :thumbsup: '.format(user.mention, lvl_end))
            users[str(user.id)]['level'] = lvl_end

When I write a normal message, everything works, but when I write the '$Rank' command, an error pops up:
Ignoring exception in command Rank:
Traceback (most recent call last):
  File "C:\Users\denko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\denko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 790, in invoke
    await self.prepare(ctx)
  File "C:\Users\denko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 751, in prepare
    await self._parse_arguments(ctx)
  File "C:\Users\denko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 670, in _parse_arguments
    transformed = await self.transform(ctx, param)
  File "C:\Users\denko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 516, in transform
    raise MissingRequiredArgument(param)
discord.ext.commands.errors.MissingRequiredArgument: users is a required argument that is missing.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Nevzorov, 2020-06-07
@AdvoKappa

discord.ext.commandsThe first argument to a command is always commands.Context .
The whole point of the commands extension is that you don't have to deal with processing messages and command arguments in them .
Make the variable usersglobal.
For example, declaring it after os.chdir(r'D:\Sublime Text 3\testbot-master'):
users = {}
As a result, the command code should look something like this:

@bot.command()
async def Rank(ctx, user: discord.Member):
    experience = users[str(user.id)]['experience']
    lvl_start = users[str(user.id)]['level']
    lvl_end = int(experience ** (1/50))
    await ctx.send(':thumbsup: {}, Ваш уровень: {} :thumbsup:'.format(user.mention, lvl_end))
    users[str(user.id)]['level'] = lvl_end

The MissingRequiredArgument error will occur if the entered command lacks arguments:
!Rankwill cause an error in this case
!Rank @DiscordTag#0000- no
If you need the command to work without an argument, make it optional by setting a standard value, for example "None" most None in the code of the command itself):
@bot.command()
async def Rank(ctx, user: discord.Member = None):
    if not user:
        user = ctx.author
    ...

And also:
In the current version of discord.py pass_context, the commands have no argument, the context argument is always passed first automatically
from discord.ext.commands import Bot- an unused import in this code

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question