Skip to content

Commands

RononDex edited this page Oct 23, 2018 · 1 revision

Commands

A quick example

Let's start with a quick example to get you started:

This command will listen for any message mentioning the bot, and matching the string test (?'TestParam'.*\w). Assuming your bot is called "TestBot" and you are using discord, you can test this command sending a message in the format "@TestBot test test1234". If everything works it will answer with "IT'S WORKING!!! You entered test1234".

Note that for this command to work, you will have to register it with the bot framework and also have the RegexCommandHandler registered (see chapter "Registering commands").

    public class TestCommand : AwesomeChatBot.Commands.Command, IRegexCommand
    {
        /// <summary>
        /// A list of regex patterns that trigger the command
        /// </summary>
        public List<string> Regex => new List<string>() { "test (?'TestParam'.*\\w)" };

        /// <summary>
        ///  Unique name of the command
        /// </summary>
        public override string Name => "Test";

        /// <summary>
        /// Execute the command
        /// </summary>
        /// <param name="recievedMessage"></param>
        /// <param name="regexMatch"></param>
        /// <returns></returns>
        public Task<bool> ExecuteRegexCommand(RecievedMessage recievedMessage, Match regexMatch) {
            return Task<bool>.Factory.StartNew(() => {

                var testParam = regexMatch.Groups["TestParam"].Value;

                recievedMessage.Channel.SendMessageAsync(new SendMessage($"IT'S WORKING!!! You entered {testParam}")).Wait();

                return true;
            });

        }
    }
Clone this wiki locally