/// <summary> /// <para>Called by the <c>ReadCommand</c> method when it deems a message suitable for parsing.</para> /// <para>Uses the parsing rules as defined by classes that use the <c>VerbAttribute</c> with the <c>IDiscordAction</c> interface.</para> /// <para>If a parse is successful then the <c>PerformAction</c> method of the parsed verb class is called.</para> /// </summary> /// <param name="messageSentToBot">The message which is currently sending a command to the bot.</param> protected virtual void Parse(SocketMessage messageSentToBot) { ParserResult <object> parseResult; string interceptedOutput; m_errorStream.GetStringBuilder().Clear(); var commandLineArgs = FunctionalExtras.CommandLineToArgs(messageSentToBot.ToString()).Skip(1); parseResult = m_commandParser.ParseArguments(commandLineArgs, m_commands.ToArray()); interceptedOutput = m_errorStream.GetStringBuilder().ToString(); parseResult.MapResult( (IDiscordAction opt) => Task.Run(() => opt.PerformAction(messageSentToBot, m_client)), errs => Task.Run(() => HandleErrors(errs, interceptedOutput, messageSentToBot)) ); }
/// <summary> /// <para>Creates an instance of the discord bot class. Will search the calling assembly for all <c>VerbAttributes</c> and apply them to its command parsing rules..</para> /// </summary> /// <param name="loginToken">The client secret discord uses to login.</param> public DiscordBotBase(string loginToken) { m_loginToken = loginToken; m_errorStream = new StringWriter(); //We gather everything with the VerbAttribute, which will be used as the parsing rules. var commandList = FunctionalExtras.GetTypesWithAttribute(typeof(VerbAttribute), Assembly.GetEntryAssembly()); if (commandList.Count() < 1) { commandList = new Type[] { typeof(ExampleVerb) }; } m_commands = commandList; m_commandParser = new Parser(settings => { settings.HelpWriter = m_errorStream; }); }