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