【问题标题】: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)