如何检查ctx是否为私人频道(dm频道) - python

本质上,我希望此命令仅在DMS中运行,并且不能在我的机器人所在的服务器中激活,我知道有一个检查器,但我不确定如何准确地使用它,感谢您的帮助

class verify:
    def check():
        #something 

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

python大神给出的解决方案

编辑:

在我写这篇文章的几秒钟后,发布了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)

Discord.py重写所有命令的收集列表 - python

我正在尝试获取Discord机器人中所有命令的列表以进行重写。我正在使用Python 3.6编写此我试图通过执行打印命令列表print(bot.commands)这仅为我提供以下回报:{<discord.ext.commands.core.Command object at 0x00000209EE6AD4E0>, <discord.ext…

Python sqlite3数据库已锁定 - python

我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…

Python pytz时区函数返回的时区为9分钟 - python

由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD…

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…

如何打印浮点数的全精度[Python] - python

我编写了以下函数,其中传递了x,y的值:def check(x, y): print(type(x)) print(type(y)) print(x) print(y) if x == y: print "Yes" 现在当我打电话check(1.00000000000000001, 1.0000000000000002)它正在打印:<…