【问题标题】:How to restrict bot to give response of bot command in private message?如何限制机器人在私人消息中给出机器人命令的响应?
【发布时间】:2019-04-01 01:51:07
【问题描述】:

我已经创建了机器人,但是当有人在私信中使用命令发送消息时,它会在私信中给出回复。

我想让机器人仅在用户在服务器和文本通道中时响应命令。

有什么帮助吗?

【问题讨论】:

    标签: c# discord discord.net


    【解决方案1】:

    所以基本上你需要在你的 CommandHandler 中添加一个类似的行,这就是你可以阻止任何非 Guild 消息的方法。

     if (message.Channel is SocketDMChannel) return;
    

    一旦通道是 SocketDMChannel,它就会从方法中返回。

    【讨论】:

    • 在我的 CommandHandler 中添加了它,它就像魅力一样。谢谢@Twenty
    【解决方案2】:

    我不得不在很多地方使用这个验证,所以我扩展了 ModuleBase 并将我所有的验证放在里面。 :

    public bool IsFromGuildChat()
    {
         var IsFromGuildChat = Context.Guild.Id != 0;
         if (IsFromGuildChat == false)
             throw new RequiresDiscordGuildException(); //custom exception 
    
          return IsFromGuildChat;
    }
    

    然后在我的命令顶部:

    [Command("test")]
    [Alias("t")]
    public async Task Test()
    {
        //validation
        if (!IsFromGuildChat())
            return;
    
        await ReplyAsync("This is only called from Guild Chat!");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-19
      • 2021-07-19
      • 2012-09-21
      • 2019-11-23
      • 2021-10-06
      • 2019-06-27
      • 2021-05-13
      • 2021-05-11
      相关资源
      最近更新 更多