如何在 discord.py 中创建 DM 命令

How to make a DM command in discord.py

我想创建一个只有在用户私信他们的命令时才有效的命令。我怎么做?我正在使用 discord.py 1.5.1,而且我对 discord.py 还很陌生。 到目前为止,这是我的代码:

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')

当我尝试使用私信发送命令时,机器人没有收到我发送的私信。我应该在 on_message() 中指定命令吗?

这是我的查询代码:

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

首先,你用的装饰器不是我刚学的时候教的discord.py,所以我把装饰器从@commands.command(brief='a DM command')改成@bot.command()(随意如果对您有用,请改回来)。然后,剩下的就相当简单了。我刚刚检查了该频道是否是 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')