简体   繁体   English

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

[英]How to make a DM command in discord.py

I want to create a command that only works if a user is DMing their command.我想创建一个仅在用户 DMing 他们的命令时才有效的命令。 How do I do that?我怎么做? I am using discord.py 1.5.1, and I'm fairly new to discord.py.我正在使用 discord.py 1.5.1,而且我对 discord.py 还很陌生。 Here is my code so far:到目前为止,这是我的代码:

from discord.ext import commands, tasks
import discord
intents = discord.Intents.all
bot = commands.Bot(command_prefix='$', description='- shows this message', intents=intents)
---- snip ----

@commands.command(brief='a DM command')
async def dm_command(ctx):
    # do stuff here

bot.run('TOKEN')

When I try DMing the command, the bot doesn't pick up the DM I sent.当我尝试 DMing 命令时,机器人不会接收我发送的 DM。 Should I specify the command in on_message()?我应该在 on_message() 中指定命令吗?

Here is my code for your query:这是我的查询代码:

@bot.command()
async def dm_command(ctx):
    if isinstance(ctx.channel, discord.channel.DMChannel):
        #do stuff here

First of all, the decorator you use is not what I was taught when i first learned discord.py, so I changed the decorator from @commands.command(brief='a DM command') to @bot.command() (Feel free to change back if it works for you).首先,你使用的装饰器不是我第一次学习discord.py时教的,所以我把装饰器从@commands.command(brief='a DM command')改成了@bot.command() (随意如果它适合你,请改回来)。 Then, the rest is fairly simple.然后,rest 相当简单。 I just checked if the channel was a DM channel, and thats it, If you have any questions about my code or if you have unforseen errors, follow up!我刚刚检查了该频道是否为 DM 频道,仅此而已,如果您对我的代码有任何疑问,或者如果您有不可预见的错误,请跟进!

@client.command()
async def dm(ctx):
     await ctx.author.send('Hi im a discord bot!')#this is the message that will pop up in ur dms when u input the cmd

client.run('Token')

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

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