How do I make this command DM only in discord.py?

Viewed 422
@client.command()
async def submitclue(ctx):
    await ctx.send('Submit your answer')
    message = await client.wait_for('message')
    print(f"{message.author} is an author of the message.")
    if message.content == 'twopeople':
         await ctx.send('Answer correct.')
    else:
         await ctx.send('Answer wrong')

So I need this command to be DM only to make sure the answers stay private. The bot should say something like "DM me this command to use it" when someone tries to use it in the server.

1 Answers

You can add commands.dm_only() decorator to your command:

from discord.ext import commands


@client.command()
@commands.dm_only()
async def submitclue(ctx):
    await ctx.send("Submit your answer")
    message = await client.wait_for('message')
    print(f"{message.author} is an author of the message.")
    if message.content == "twopeople":
         await ctx.send("Answer correct.")
    else:
         await ctx.send("Answer wrong")

And also write an error handler to handle this type of errors:

@client.event
async def on_command_error(ctx, exception):
    if isinstance(exception, commands.PrivateMessageOnly):
        await ctx.send("DM me this command to use it.")
Related