python - 我如何检查 ctx 是否是私人频道(dm 频道)
问题描述
本质上,我希望这个命令只能在 DMS 中运行,并且不能在我的机器人所在的服务器中激活,我知道有一个用于检查的装饰器,但我不确定如何准确地使用它,感谢任何帮助
class verify:
def check():
#something
@bot.command()
@commands.check(check)
async def verify(ctx):
解决方案
编辑:
在我写完这篇文章几秒钟后,1.1.0 版发布了,其中包括一个内置的dm_only
检查
原来的:
这与内置检查相反,内置guild_only
检查定义为:
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)
推荐阅读
- java - RxJava - 如何使用两个可观察对象重复 takeUntil 操作,一个可观察对象取决于另一个
- sql - 使用模式时,Postgresql ILIKE/LIKE ANY 不返回任何结果
- java - Java:将每个字母转换为数字,例如 a = 1、b = 2、c = 3
- java - 使用 Thread 和 Runnable 时,Lamba 在 Java 中如何工作?
- javascript - 从数组 MongoDB 中删除对象
- multidimensional-array - 复制到另一个可分配数组后,Fortran 90 不保留数组下限/上限
- reactjs - 如何绑定选择?
- tensorflow.js - LSTM 模型中的不良形状
- android - 以编程方式向片段添加新视图时无法解决符号错误
- c# - StringContent 作为空对象发送到节点