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