You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在私人DM中使用Pycord/discord.py的消息应用命令

解决Pycord消息命令无法在用户私人DM中使用的问题

你可以通过为消息命令指定上下文类型集合来实现需求,Pycord(2.4.0及以上版本)已经支持对应配置,无需绕开框架直接调用API。

修改后的代码示例

首先导入上下文类型枚举:

from discord import ApplicationCommandContextType

然后更新消息命令的装饰器配置:

@commands.message_command(
    name="Remind Me",
    contexts={
        ApplicationCommandContextType.BOT_DM,
        ApplicationCommandContextType.GUILD,
        ApplicationCommandContextType.PRIVATE_CHANNEL
    }
)
async def message_com(self, ctx, message):
    view = util.reminderInteraction.ReminderSelectView(
        self.process_reminder,
        ctx=ctx,
        message_content=message.content,
        message_by=message.author.global_name,
    )
    await ctx.respond(view=view, ephemeral=True)

关键说明

  • ApplicationCommandContextType.PRIVATE_CHANNEL对应用户之间的私人DM场景,将其加入contexts参数后,命令就会在该场景下显示。
  • 若当前Pycord版本不支持该参数,直接升级到最新稳定版即可解决兼容性问题。

内容的提问来源于stack exchange,提问作者IAM々Unknown

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 02:34:58