简体   繁体   English

如何仅在 discord.py 中制作此命令 DM?

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

@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.所以我需要这个命令是 DM 只是为了确保答案保持私密。 The bot should say something like "DM me this command to use it" when someone tries to use it in the server.当有人试图在服务器中使用它时,机器人应该说类似“DM我这个命令来使用它”。

You can add commands.dm_only() decorator to your command:您可以将commands.dm_only()装饰器添加到您的命令中:

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.")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM