【问题标题】:how do i check if the ctx is a private channel(dm channel)我如何检查 ctx 是否是私人频道(dm 频道)
【发布时间】:2019-05-10 23:30:05
【问题描述】:

本质上,我希望这个命令只能在 DMS 中运行,并且不能在我的机器人所在的服务器中激活,我知道有一个用于检查的装饰器,但我不确定如何准确使用它,任何帮助感激不尽

class verify:
    def check():
        #something 

    @bot.command()
    @commands.check(check)
    async def verify(ctx):

【问题讨论】:

    标签: python python-3.x discord.py


    【解决方案1】:

    编辑:

    在我写完这篇文章几秒钟后,1.1.0 版发布了,其中包括一个内置的dm_only 检查

    原文:

    这与内置的guild_only检查相反,is defined as

    def guild_only():
        """A :func:`.check` that indicates this command must only be used in a
        guild context only. Basically, no private messages are allowed when
        using the command.
        This check raises a special exception, :exc:`.NoPrivateMessage`
        that is inherited from :exc:`.CheckFailure`.
        """
    
        def predicate(ctx):
            if ctx.guild is None:
                raise NoPrivateMessage()
            return True
    
        return check(predicate)
    

    所以dm_only 检查看起来像

    class NoGuildMessage(CheckFailure):
        pass
    
    def dm_only():
        def predicate(ctx):
            if ctx.guild is not None:
                raise NoGuildMessage()
            return True
    
        return check(predicate)
    

    【讨论】:

      猜你喜欢
      • 2019-03-18
      • 2021-02-19
      • 2019-03-25
      • 2021-04-13
      • 2022-12-16
      • 2021-12-13
      • 2021-05-23
      • 2020-12-22
      • 2018-01-07
      相关资源
      最近更新 更多