簡體   English   中英

如何限制bot在私信中回復bot命令?

[英]How to restrict bot to give response of bot command in private message?

我已經創建了機器人,但是當有人在私人消息中使用命令發送消息時,它會在私人消息中給出響應。

我想讓機器人僅在用戶在服務器和文本通道中時才響應命令。

有什么幫助嗎?

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

 if (message.Channel is SocketDMChannel) return;

只要通道是 SocketDMChannel,就會從方法中返回。

我不得不在很多地方使用這個驗證,所以我擴展了 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!");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM