简体   繁体   中英

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.

 if (message.Channel is SocketDMChannel) return;

This will return from the method as soon as the channel is a SocketDMChannel.

I've had to use this validation in many places so I extended the ModuleBase and placed all my validation inside it. :

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!");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM