简体   繁体   English

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

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

I have created the bot but when someone DMs with the command in private message it gives responses in private message.我已经创建了机器人,但是当有人在私人消息中使用命令发送消息时,它会在私人消息中给出响应。

I would like to make bot respond to to command only when the user is in server and in text channel.我想让机器人仅在用户在服务器和文本通道中时才响应命令。

Any help?有什么帮助吗?

So basically you need to add a line similar to this to your CommandHandler, this is how you can block any none Guild messages.所以基本上你需要在你的 CommandHandler 中添加一个类似于此的行,这就是你可以阻止任何非 Guild 消息的方法。

 if (message.Channel is SocketDMChannel) return;

This will return from the method as soon as the channel is a SocketDMChannel.只要通道是 SocketDMChannel,就会从方法中返回。

I've had to use this validation in many places so I extended the ModuleBase and placed all my validation inside it.我不得不在很多地方使用这个验证,所以我扩展了 ModuleBase 并将我所有的验证放在里面。 :

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

      return IsFromGuildChat;
}

Then in the top of my command:然后在我的命令的顶部:

[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.

相关问题 如何在Microsoft Bot Framework中给出延迟的响应 - How do I give a delayed response in Microsoft Bot Framework 如何从电报机器人创建私人消息? - How can i create a private message from telegram bot? 在 VS 2019 问题中调试 C# Bot - 通过 MS Teams 向 Bot 发送消息后,Bot 中没有响应 - Debugging C# Bot in VS 2019 issue - no response in Bot after sending Bot a message through MS Teams Microsoft bot LUIS-如何验证实体的存在并给出适当的响应 - Microsoft bot LUIS- How to verify the presence of entity and give appropriate response 如何使用MS bot框架从团队私人消息中获取用户上下文 - How to get user context from Teams private message using MS bot framework 如何从 Bot Framework 中的数据库中获取 Bot Activity Message? - How to bring the Bot Activity Message from database in Bot Framework? 通过 MS Teams Bot 中的主动私人消息对用户进行身份验证 - Authenticating a user via proactive private message in MS Teams Bot 电报机器人。 如何在发送特定消息后记录用户的响应 - Telegram bot. How to record a user's response after sending a specific message 如何在Bot的SQL Server中存储响应 - How to store in sql server response of bot 机器人框架欢迎消息。 如何CardAction? - bot framework welcome message. how to CardAction?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM