Search code examples
pythondiscorddiscord.py

discord.ext.commands.errors.MissingRequiredArgument: interaction is a required argument that is missing


import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.message_content = True

bot = commands.Bot(command_prefix='$', intents=intents)


@bot.command()
async def test(interaction: discord.Interaction):
    await interaction.response.send_message(content="Hello!")

bot.run('token')

I have tried to send a mesage by interaction but it doesen't work. I get this error : discord.ext.commands.errors.MissingRequiredArgument: interaction is a required argument that is missing.

I don't know how to fix it.


Solution

  • The error you're encountering is because the test command is defined as a regular command using the @bot.command() decorator, but you're trying to use it as an interaction (slash command) by specifying the interaction parameter.

    You have two options to fix this:

    1. If you want to use the test command as a regular command triggered by a message, remove the interaction parameter from the command definition:
    @bot.command()
    async def test(ctx):
        await ctx.send(content="Hello!")
    

    In this case, the command will be triggered when a user sends a message starting with $test (assuming $ is your command prefix).

    1. If you want to use the test command as a slash command (interaction), you need to use the @bot.tree.command() decorator instead of @bot.command():
    @bot.tree.command()
    async def test(interaction: discord.Interaction):
        await interaction.response.send_message(content="Hello!")
    
    bot.run('token')
    

    In this case, the test command will be registered as a slash command, and users can trigger it by typing /test in the chat.