【问题标题】:How to make a DM command in discord.py如何在 discord.py 中创建 DM 命令
【发布时间】:2021-03-19 23:41:51
【问题描述】:

我想创建一个仅在用户 DMing 他们的命令时才有效的命令。我怎么做?我正在使用 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')

当我尝试 DMing 命令时,机器人不会接收我发送的 DM。我应该在 on_message() 中指定命令吗?

【问题讨论】:

  • "有什么办法吗?"我确定有。 (可以用简单的“是”或“否”回答的问题通常不适合 Stack Overflow 的问答格式。编辑您的问题以分享更多相关细节,至少包括到目前为止您在尝试解决手头的问题,并详细说明在这些尝试中具体遇到的问题。堆栈溢出不会为您编写代码 你。另见:How to Ask)
  • 对不起,我已经更新了问题。

标签: python discord.py-rewrite


【解决方案1】:
@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')

【讨论】:

    【解决方案2】:

    这是我的查询代码:

    @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 频道,就是这样!如果您对我的代码有任何疑问或出现意外错误,请跟进!

    【讨论】:

    • @commands.command() 如果命令位于 cog 内部,则使用 @bot.command() 或在主文件中使用 @client.command()
    • 好的。谢谢指点!我实际上正在实施(至少尝试实施)我的第一个 cog,这将有很大帮助!
    猜你喜欢
    • 2021-05-17
    • 1970-01-01
    • 2019-08-22
    • 1970-01-01
    • 2021-12-21
    • 2020-12-12
    • 1970-01-01
    • 1970-01-01
    • 2023-02-26
    相关资源
    最近更新 更多