Beispiel #1
0
        protected virtual void HandleQueryEntry(
            Dictionary <Command, List <PrivateMessageCommandHandler> > commandsHandlers, object sender,
            IPrivateMessageEventArgs msg, MessageFlags flags)
        {
            string line = msg.Message;

            if (Config.AllowWhitespaceBeforeCommandPrefix)
            {
                line = line.TrimStart();
            }
            if (!line.StartsWith(Config.CommandPrefix))
            {
                // not a command
                return;
            }
            string commandAndArgs = line.Substring(Config.CommandPrefix.Length);

            foreach (KeyValuePair <Command, List <PrivateMessageCommandHandler> > commandHandler in commandsHandlers)
            {
                // attempt to parse this command
                CommandMatch match = commandHandler.Key.Match(Config, commandAndArgs, flags);
                if (match == null)
                {
                    continue;
                }

                // check result of global callbacks
                if (!ApplyGlobalCallbacks(match, msg))
                {
                    continue;
                }

                // distribute
                foreach (PrivateMessageCommandHandler handler in commandHandler.Value)
                {
                    try
                    {
                        handler.Invoke(match, msg);
                    }
                    catch (Exception exc)
                    {
                        Logger.LogError(
                            "error when {Handler} was handling command {Command}: {Exception}",
                            handler, match.CommandName, exc
                            );
                    }
                }
            }
        }
Beispiel #2
0
        protected virtual bool ApplyGlobalCallbacks(CommandMatch match, IUserMessageEventArgs msg)
        {
            bool callbackSaidNo = false;

            foreach (GlobalCommandCallback callback in GlobalCommandCallbacks)
            {
                try
                {
                    if (!callback.Invoke(match, msg))
                    {
                        callbackSaidNo = true;
                    }
                }
                catch (Exception exc)
                {
                    Logger.LogError(
                        "error when global callback {GlobalCommandCallback} was processing command {Command}: {Exception}",
                        callback, match.CommandName, exc
                        );
                }
            }
            return(!callbackSaidNo);
        }