【问题标题】:Discord.py How to make bot not respond to PM'sDiscord.py 如何让机器人不响应 PM
【发布时间】:2018-07-09 03:42:18
【问题描述】:

正如标题所问,有没有办法让机器人不能在 PM 中工作,而只能在频道中使用时工作?

我现在所做的就是在我的@client.command 前面有这个声明

@client.command(pass_context=True)
async def profile(ctx):
    if ctx.message.server == None:
        pass
    else:
        # Code

有没有更简单的方法来做这件事?我读过我可以使用全局检查,但我不确定如何实现。

编辑:我正在使用Commands Extension对此进行编码

【问题讨论】:

  • 我觉得不错;为什么你认为会有不同的方式?
  • @AdamBarnes 因为我必须为我创建的每个命令添加它,所以我觉得每次重复该行会使代码不必要地变长。

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


【解决方案1】:

"no_pm = true" 现在似乎不起作用。 所以在@client.command()下面添加“@commands.guild_only()”

示例:

@client.command()
@commands.guild_only()
async def commandname():

【讨论】:

    【解决方案2】:

    如果你想用@bot.event 编码,你必须这样做:

    @bot.event
    async def on_message(message):
        if message.content.startswith('!dm')
            await message.author.send('Hi. This message is send in DM!')
    

    【讨论】:

      【解决方案3】:

      documentation 中没有我可以看到的任何内容,其中提到了关于全局禁用私人消息事件的任何内容。

      你所做的正是我要做的。你说你不想每次都重复这一行,但你只能有一个on_message(),这样那一行就只需要出现一次;每个需要遵守这些规则的命令都将在 if 块中:

      @client.event
      async def on_message(message):
          if message.server is not None:
              if message.content.startswith('!dice'):
                  ...
      
              if message.content.startswith('!roulette'):
                  ...
      

      编辑:您似乎正在使用 commands 扩展名,这几乎没有记录。

      不过,在阅读 Command 的文档字符串时,我遇到了 this 部分:

      no_pm : bool
          If ``True``\, then the command is not allowed to be executed in
          private messages. Defaults to ``False``. Note that if it is executed
          in private messages, then :func:`on_command_error` and local error handlers
          are called with the :exc:`NoPrivateMessage` error.
      

      所以如果你这样定义你的命令:

      @client.command(pass_context=True, no_pm=True)
      async def profile(ctx):
          ...
      

      它将在 PM 中被禁用。

      【讨论】:

      • 这就是你应该如何执行命令的方式吗?通过@client.event 和on_message,而不是@client.command?
      • 啊。我想知道我们是否正在寻找不同的东西...您能否将我链接到command 的文档?我can't find anything...
      • 我编写命令的方式类似于他们 GitHub 示例中的 this example
      • 我已经编辑了我的答案,以反映您正在使用我以前不知道的 commands 扩展名。如果您可以在您的问题中添加一行说明您正在使用它,或者您的代码可能更大的 sn-p 以帮助未来的 Google 员工,我将不胜感激。
      • no_pm=True 成功了!谢谢你的帮助!我不知道@client.command 是扩展的一部分,我现在更新了我的问题以包含它。
      猜你喜欢
      • 2019-02-09
      • 2021-10-06
      • 2021-08-18
      • 2021-11-03
      • 2021-11-28
      • 2018-11-17
      • 2021-01-20
      • 2021-10-22
      • 2021-12-01
      相关资源
      最近更新 更多